1c26fd69fSDavid Howells /* Instantiate a public key crypto key from an X.509 Certificate 2c26fd69fSDavid Howells * 3c26fd69fSDavid Howells * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 4c26fd69fSDavid Howells * Written by David Howells (dhowells@redhat.com) 5c26fd69fSDavid Howells * 6c26fd69fSDavid Howells * This program is free software; you can redistribute it and/or 7c26fd69fSDavid Howells * modify it under the terms of the GNU General Public Licence 8c26fd69fSDavid Howells * as published by the Free Software Foundation; either version 9c26fd69fSDavid Howells * 2 of the Licence, or (at your option) any later version. 10c26fd69fSDavid Howells */ 11c26fd69fSDavid Howells 12c26fd69fSDavid Howells #define pr_fmt(fmt) "X.509: "fmt 13c26fd69fSDavid Howells #include <linux/module.h> 14c26fd69fSDavid Howells #include <linux/kernel.h> 15c26fd69fSDavid Howells #include <linux/slab.h> 16c26fd69fSDavid Howells #include <linux/err.h> 17c26fd69fSDavid Howells #include <linux/mpi.h> 18c26fd69fSDavid Howells #include <linux/asn1_decoder.h> 19c26fd69fSDavid Howells #include <keys/asymmetric-subtype.h> 20c26fd69fSDavid Howells #include <keys/asymmetric-parser.h> 2109fbc473SMimi Zohar #include <keys/system_keyring.h> 22c26fd69fSDavid Howells #include <crypto/hash.h> 23c26fd69fSDavid Howells #include "asymmetric_keys.h" 24c26fd69fSDavid Howells #include "public_key.h" 25c26fd69fSDavid Howells #include "x509_parser.h" 26c26fd69fSDavid Howells 27c26fd69fSDavid Howells /* 2809fbc473SMimi Zohar * Find a key in the given keyring by issuer and authority. 2909fbc473SMimi Zohar */ 3009fbc473SMimi Zohar static struct key *x509_request_asymmetric_key( 3109fbc473SMimi Zohar struct key *keyring, 3209fbc473SMimi Zohar const char *signer, size_t signer_len, 3309fbc473SMimi Zohar const char *authority, size_t auth_len) 3409fbc473SMimi Zohar { 3509fbc473SMimi Zohar key_ref_t key; 3609fbc473SMimi Zohar char *id; 3709fbc473SMimi Zohar 3809fbc473SMimi Zohar /* Construct an identifier. */ 3909fbc473SMimi Zohar id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL); 4009fbc473SMimi Zohar if (!id) 4109fbc473SMimi Zohar return ERR_PTR(-ENOMEM); 4209fbc473SMimi Zohar 4309fbc473SMimi Zohar memcpy(id, signer, signer_len); 4409fbc473SMimi Zohar id[signer_len + 0] = ':'; 4509fbc473SMimi Zohar id[signer_len + 1] = ' '; 4609fbc473SMimi Zohar memcpy(id + signer_len + 2, authority, auth_len); 4709fbc473SMimi Zohar id[signer_len + 2 + auth_len] = 0; 4809fbc473SMimi Zohar 4909fbc473SMimi Zohar pr_debug("Look up: \"%s\"\n", id); 5009fbc473SMimi Zohar 5109fbc473SMimi Zohar key = keyring_search(make_key_ref(keyring, 1), 5209fbc473SMimi Zohar &key_type_asymmetric, id); 5309fbc473SMimi Zohar if (IS_ERR(key)) 5409fbc473SMimi Zohar pr_debug("Request for module key '%s' err %ld\n", 5509fbc473SMimi Zohar id, PTR_ERR(key)); 5609fbc473SMimi Zohar kfree(id); 5709fbc473SMimi Zohar 5809fbc473SMimi Zohar if (IS_ERR(key)) { 5909fbc473SMimi Zohar switch (PTR_ERR(key)) { 6009fbc473SMimi Zohar /* Hide some search errors */ 6109fbc473SMimi Zohar case -EACCES: 6209fbc473SMimi Zohar case -ENOTDIR: 6309fbc473SMimi Zohar case -EAGAIN: 6409fbc473SMimi Zohar return ERR_PTR(-ENOKEY); 6509fbc473SMimi Zohar default: 6609fbc473SMimi Zohar return ERR_CAST(key); 6709fbc473SMimi Zohar } 6809fbc473SMimi Zohar } 6909fbc473SMimi Zohar 7009fbc473SMimi Zohar pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key))); 7109fbc473SMimi Zohar return key_ref_to_ptr(key); 7209fbc473SMimi Zohar } 7309fbc473SMimi Zohar 7409fbc473SMimi Zohar /* 75b426beb6SDavid Howells * Set up the signature parameters in an X.509 certificate. This involves 76b426beb6SDavid Howells * digesting the signed data and extracting the signature. 77c26fd69fSDavid Howells */ 78b426beb6SDavid Howells int x509_get_sig_params(struct x509_certificate *cert) 79c26fd69fSDavid Howells { 80c26fd69fSDavid Howells struct crypto_shash *tfm; 81c26fd69fSDavid Howells struct shash_desc *desc; 82c26fd69fSDavid Howells size_t digest_size, desc_size; 83b426beb6SDavid Howells void *digest; 84c26fd69fSDavid Howells int ret; 85c26fd69fSDavid Howells 86c26fd69fSDavid Howells pr_devel("==>%s()\n", __func__); 87c26fd69fSDavid Howells 88b426beb6SDavid Howells if (cert->sig.rsa.s) 89b426beb6SDavid Howells return 0; 90b426beb6SDavid Howells 91b426beb6SDavid Howells cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size); 92b426beb6SDavid Howells if (!cert->sig.rsa.s) 93b426beb6SDavid Howells return -ENOMEM; 94b426beb6SDavid Howells cert->sig.nr_mpi = 1; 95b426beb6SDavid Howells 96c26fd69fSDavid Howells /* Allocate the hashing algorithm we're going to need and find out how 97c26fd69fSDavid Howells * big the hash operational data will be. 98c26fd69fSDavid Howells */ 99b426beb6SDavid Howells tfm = crypto_alloc_shash(pkey_hash_algo_name[cert->sig.pkey_hash_algo], 0, 0); 100c26fd69fSDavid Howells if (IS_ERR(tfm)) 101c26fd69fSDavid Howells return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm); 102c26fd69fSDavid Howells 103c26fd69fSDavid Howells desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); 104c26fd69fSDavid Howells digest_size = crypto_shash_digestsize(tfm); 105c26fd69fSDavid Howells 106b426beb6SDavid Howells /* We allocate the hash operational data storage on the end of the 107b426beb6SDavid Howells * digest storage space. 108c26fd69fSDavid Howells */ 109c26fd69fSDavid Howells ret = -ENOMEM; 110b426beb6SDavid Howells digest = kzalloc(digest_size + desc_size, GFP_KERNEL); 111b426beb6SDavid Howells if (!digest) 112b426beb6SDavid Howells goto error; 113c26fd69fSDavid Howells 114b426beb6SDavid Howells cert->sig.digest = digest; 115b426beb6SDavid Howells cert->sig.digest_size = digest_size; 116c26fd69fSDavid Howells 117b426beb6SDavid Howells desc = digest + digest_size; 118c26fd69fSDavid Howells desc->tfm = tfm; 119c26fd69fSDavid Howells desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 120c26fd69fSDavid Howells 121c26fd69fSDavid Howells ret = crypto_shash_init(desc); 122c26fd69fSDavid Howells if (ret < 0) 123c26fd69fSDavid Howells goto error; 124b426beb6SDavid Howells might_sleep(); 125b426beb6SDavid Howells ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest); 126c26fd69fSDavid Howells error: 127c26fd69fSDavid Howells crypto_free_shash(tfm); 128c26fd69fSDavid Howells pr_devel("<==%s() = %d\n", __func__, ret); 129c26fd69fSDavid Howells return ret; 130c26fd69fSDavid Howells } 131b426beb6SDavid Howells EXPORT_SYMBOL_GPL(x509_get_sig_params); 132b426beb6SDavid Howells 133b426beb6SDavid Howells /* 134b426beb6SDavid Howells * Check the signature on a certificate using the provided public key 135b426beb6SDavid Howells */ 136b426beb6SDavid Howells int x509_check_signature(const struct public_key *pub, 137b426beb6SDavid Howells struct x509_certificate *cert) 138b426beb6SDavid Howells { 139b426beb6SDavid Howells int ret; 140b426beb6SDavid Howells 141b426beb6SDavid Howells pr_devel("==>%s()\n", __func__); 142b426beb6SDavid Howells 143b426beb6SDavid Howells ret = x509_get_sig_params(cert); 144b426beb6SDavid Howells if (ret < 0) 145b426beb6SDavid Howells return ret; 146b426beb6SDavid Howells 147b426beb6SDavid Howells ret = public_key_verify_signature(pub, &cert->sig); 148b426beb6SDavid Howells pr_debug("Cert Verification: %d\n", ret); 149b426beb6SDavid Howells return ret; 150b426beb6SDavid Howells } 151b426beb6SDavid Howells EXPORT_SYMBOL_GPL(x509_check_signature); 152c26fd69fSDavid Howells 153c26fd69fSDavid Howells /* 15409fbc473SMimi Zohar * Check the new certificate against the ones in the trust keyring. If one of 15509fbc473SMimi Zohar * those is the signing key and validates the new certificate, then mark the 15609fbc473SMimi Zohar * new certificate as being trusted. 15709fbc473SMimi Zohar * 15809fbc473SMimi Zohar * Return 0 if the new certificate was successfully validated, 1 if we couldn't 15909fbc473SMimi Zohar * find a matching parent certificate in the trusted list and an error if there 16009fbc473SMimi Zohar * is a matching certificate but the signature check fails. 16109fbc473SMimi Zohar */ 16209fbc473SMimi Zohar static int x509_validate_trust(struct x509_certificate *cert, 16309fbc473SMimi Zohar struct key *trust_keyring) 16409fbc473SMimi Zohar { 16509fbc473SMimi Zohar const struct public_key *pk; 16609fbc473SMimi Zohar struct key *key; 16709fbc473SMimi Zohar int ret = 1; 16809fbc473SMimi Zohar 16909fbc473SMimi Zohar key = x509_request_asymmetric_key(trust_keyring, 17009fbc473SMimi Zohar cert->issuer, strlen(cert->issuer), 17109fbc473SMimi Zohar cert->authority, 17209fbc473SMimi Zohar strlen(cert->authority)); 17309fbc473SMimi Zohar if (!IS_ERR(key)) { 17409fbc473SMimi Zohar pk = key->payload.data; 17509fbc473SMimi Zohar ret = x509_check_signature(pk, cert); 17609fbc473SMimi Zohar } 17709fbc473SMimi Zohar return ret; 17809fbc473SMimi Zohar } 17909fbc473SMimi Zohar 18009fbc473SMimi Zohar /* 181c26fd69fSDavid Howells * Attempt to parse a data blob for a key as an X509 certificate. 182c26fd69fSDavid Howells */ 183c26fd69fSDavid Howells static int x509_key_preparse(struct key_preparsed_payload *prep) 184c26fd69fSDavid Howells { 185c26fd69fSDavid Howells struct x509_certificate *cert; 186c26fd69fSDavid Howells size_t srlen, sulen; 187c26fd69fSDavid Howells char *desc = NULL; 188c26fd69fSDavid Howells int ret; 189c26fd69fSDavid Howells 190c26fd69fSDavid Howells cert = x509_cert_parse(prep->data, prep->datalen); 191c26fd69fSDavid Howells if (IS_ERR(cert)) 192c26fd69fSDavid Howells return PTR_ERR(cert); 193c26fd69fSDavid Howells 194c26fd69fSDavid Howells pr_devel("Cert Issuer: %s\n", cert->issuer); 195c26fd69fSDavid Howells pr_devel("Cert Subject: %s\n", cert->subject); 1962ecdb23bSDavid Howells 1972ecdb23bSDavid Howells if (cert->pub->pkey_algo >= PKEY_ALGO__LAST || 1982ecdb23bSDavid Howells cert->sig.pkey_algo >= PKEY_ALGO__LAST || 1992ecdb23bSDavid Howells cert->sig.pkey_hash_algo >= PKEY_HASH__LAST || 2002ecdb23bSDavid Howells !pkey_algo[cert->pub->pkey_algo] || 2012ecdb23bSDavid Howells !pkey_algo[cert->sig.pkey_algo] || 2022ecdb23bSDavid Howells !pkey_hash_algo_name[cert->sig.pkey_hash_algo]) { 2032ecdb23bSDavid Howells ret = -ENOPKG; 2042ecdb23bSDavid Howells goto error_free_cert; 2052ecdb23bSDavid Howells } 2062ecdb23bSDavid Howells 20767f7d60bSDavid Howells pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]); 2082f1c4fefSDavid Howells pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n", 209a5752d11SDavid Howells cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1, 210a5752d11SDavid Howells cert->valid_from.tm_mday, cert->valid_from.tm_hour, 211a5752d11SDavid Howells cert->valid_from.tm_min, cert->valid_from.tm_sec); 2122f1c4fefSDavid Howells pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n", 213a5752d11SDavid Howells cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1, 214a5752d11SDavid Howells cert->valid_to.tm_mday, cert->valid_to.tm_hour, 215a5752d11SDavid Howells cert->valid_to.tm_min, cert->valid_to.tm_sec); 216c26fd69fSDavid Howells pr_devel("Cert Signature: %s + %s\n", 217b426beb6SDavid Howells pkey_algo_name[cert->sig.pkey_algo], 218b426beb6SDavid Howells pkey_hash_algo_name[cert->sig.pkey_hash_algo]); 219c26fd69fSDavid Howells 22017334cabSDavid Howells if (!cert->fingerprint) { 22117334cabSDavid Howells pr_warn("Cert for '%s' must have a SubjKeyId extension\n", 222c26fd69fSDavid Howells cert->subject); 223c26fd69fSDavid Howells ret = -EKEYREJECTED; 224c26fd69fSDavid Howells goto error_free_cert; 225c26fd69fSDavid Howells } 226c26fd69fSDavid Howells 22767f7d60bSDavid Howells cert->pub->algo = pkey_algo[cert->pub->pkey_algo]; 228c26fd69fSDavid Howells cert->pub->id_type = PKEY_ID_X509; 229c26fd69fSDavid Howells 23017334cabSDavid Howells /* Check the signature on the key if it appears to be self-signed */ 23117334cabSDavid Howells if (!cert->authority || 23217334cabSDavid Howells strcmp(cert->fingerprint, cert->authority) == 0) { 23309fbc473SMimi Zohar ret = x509_check_signature(cert->pub, cert); /* self-signed */ 234c26fd69fSDavid Howells if (ret < 0) 235c26fd69fSDavid Howells goto error_free_cert; 23609fbc473SMimi Zohar } else { 23709fbc473SMimi Zohar ret = x509_validate_trust(cert, system_trusted_keyring); 23809fbc473SMimi Zohar if (!ret) 23909fbc473SMimi Zohar prep->trusted = 1; 240c26fd69fSDavid Howells } 241c26fd69fSDavid Howells 242c26fd69fSDavid Howells /* Propose a description */ 243c26fd69fSDavid Howells sulen = strlen(cert->subject); 244c26fd69fSDavid Howells srlen = strlen(cert->fingerprint); 245c26fd69fSDavid Howells ret = -ENOMEM; 246c26fd69fSDavid Howells desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL); 247c26fd69fSDavid Howells if (!desc) 248c26fd69fSDavid Howells goto error_free_cert; 249c26fd69fSDavid Howells memcpy(desc, cert->subject, sulen); 250c26fd69fSDavid Howells desc[sulen] = ':'; 251c26fd69fSDavid Howells desc[sulen + 1] = ' '; 252c26fd69fSDavid Howells memcpy(desc + sulen + 2, cert->fingerprint, srlen); 253c26fd69fSDavid Howells desc[sulen + 2 + srlen] = 0; 254c26fd69fSDavid Howells 255c26fd69fSDavid Howells /* We're pinning the module by being linked against it */ 256c26fd69fSDavid Howells __module_get(public_key_subtype.owner); 257c26fd69fSDavid Howells prep->type_data[0] = &public_key_subtype; 258c26fd69fSDavid Howells prep->type_data[1] = cert->fingerprint; 259c26fd69fSDavid Howells prep->payload = cert->pub; 260c26fd69fSDavid Howells prep->description = desc; 261c26fd69fSDavid Howells prep->quotalen = 100; 262c26fd69fSDavid Howells 263c26fd69fSDavid Howells /* We've finished with the certificate */ 264c26fd69fSDavid Howells cert->pub = NULL; 265c26fd69fSDavid Howells cert->fingerprint = NULL; 266c26fd69fSDavid Howells desc = NULL; 267c26fd69fSDavid Howells ret = 0; 268c26fd69fSDavid Howells 269c26fd69fSDavid Howells error_free_cert: 270c26fd69fSDavid Howells x509_free_certificate(cert); 271c26fd69fSDavid Howells return ret; 272c26fd69fSDavid Howells } 273c26fd69fSDavid Howells 274c26fd69fSDavid Howells static struct asymmetric_key_parser x509_key_parser = { 275c26fd69fSDavid Howells .owner = THIS_MODULE, 276c26fd69fSDavid Howells .name = "x509", 277c26fd69fSDavid Howells .parse = x509_key_preparse, 278c26fd69fSDavid Howells }; 279c26fd69fSDavid Howells 280c26fd69fSDavid Howells /* 281c26fd69fSDavid Howells * Module stuff 282c26fd69fSDavid Howells */ 283c26fd69fSDavid Howells static int __init x509_key_init(void) 284c26fd69fSDavid Howells { 285c26fd69fSDavid Howells return register_asymmetric_key_parser(&x509_key_parser); 286c26fd69fSDavid Howells } 287c26fd69fSDavid Howells 288c26fd69fSDavid Howells static void __exit x509_key_exit(void) 289c26fd69fSDavid Howells { 290c26fd69fSDavid Howells unregister_asymmetric_key_parser(&x509_key_parser); 291c26fd69fSDavid Howells } 292c26fd69fSDavid Howells 293c26fd69fSDavid Howells module_init(x509_key_init); 294c26fd69fSDavid Howells module_exit(x509_key_exit); 295*e19aaa7dSKonstantin Khlebnikov 296*e19aaa7dSKonstantin Khlebnikov MODULE_DESCRIPTION("X.509 certificate parser"); 297*e19aaa7dSKonstantin Khlebnikov MODULE_LICENSE("GPL"); 298