1 /* Instantiate a public key crypto key from an X.509 Certificate
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #define pr_fmt(fmt) "X.509: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/mpi.h>
18 #include <linux/asn1_decoder.h>
19 #include <keys/asymmetric-subtype.h>
20 #include <keys/asymmetric-parser.h>
21 #include <keys/system_keyring.h>
22 #include <crypto/hash.h>
23 #include "asymmetric_keys.h"
24 #include "public_key.h"
25 #include "x509_parser.h"
28 * Find a key in the given keyring by issuer and authority.
30 static struct key *x509_request_asymmetric_key(
32 const char *signer, size_t signer_len,
33 const char *authority, size_t auth_len)
38 /* Construct an identifier. */
39 id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
41 return ERR_PTR(-ENOMEM);
43 memcpy(id, signer, signer_len);
44 id[signer_len + 0] = ':';
45 id[signer_len + 1] = ' ';
46 memcpy(id + signer_len + 2, authority, auth_len);
47 id[signer_len + 2 + auth_len] = 0;
49 pr_debug("Look up: \"%s\"\n", id);
51 key = keyring_search(make_key_ref(keyring, 1),
52 &key_type_asymmetric, id);
54 pr_debug("Request for module key '%s' err %ld\n",
59 switch (PTR_ERR(key)) {
60 /* Hide some search errors */
64 return ERR_PTR(-ENOKEY);
70 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
71 return key_ref_to_ptr(key);
75 * Set up the signature parameters in an X.509 certificate. This involves
76 * digesting the signed data and extracting the signature.
78 int x509_get_sig_params(struct x509_certificate *cert)
80 struct crypto_shash *tfm;
81 struct shash_desc *desc;
82 size_t digest_size, desc_size;
86 pr_devel("==>%s()\n", __func__);
91 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
96 /* Allocate the hashing algorithm we're going to need and find out how
97 * big the hash operational data will be.
99 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
101 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
103 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
104 digest_size = crypto_shash_digestsize(tfm);
106 /* We allocate the hash operational data storage on the end of the
107 * digest storage space.
110 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
114 cert->sig.digest = digest;
115 cert->sig.digest_size = digest_size;
117 desc = digest + digest_size;
119 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
121 ret = crypto_shash_init(desc);
125 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
127 crypto_free_shash(tfm);
128 pr_devel("<==%s() = %d\n", __func__, ret);
131 EXPORT_SYMBOL_GPL(x509_get_sig_params);
134 * Check the signature on a certificate using the provided public key
136 int x509_check_signature(const struct public_key *pub,
137 struct x509_certificate *cert)
141 pr_devel("==>%s()\n", __func__);
143 ret = x509_get_sig_params(cert);
147 ret = public_key_verify_signature(pub, &cert->sig);
148 pr_debug("Cert Verification: %d\n", ret);
151 EXPORT_SYMBOL_GPL(x509_check_signature);
154 * Check the new certificate against the ones in the trust keyring. If one of
155 * those is the signing key and validates the new certificate, then mark the
156 * new certificate as being trusted.
158 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
159 * find a matching parent certificate in the trusted list and an error if there
160 * is a matching certificate but the signature check fails.
162 static int x509_validate_trust(struct x509_certificate *cert,
163 struct key *trust_keyring)
165 const struct public_key *pk;
169 key = x509_request_asymmetric_key(trust_keyring,
170 cert->issuer, strlen(cert->issuer),
172 strlen(cert->authority));
174 pk = key->payload.data;
175 ret = x509_check_signature(pk, cert);
181 * Attempt to parse a data blob for a key as an X509 certificate.
183 static int x509_key_preparse(struct key_preparsed_payload *prep)
185 struct x509_certificate *cert;
190 cert = x509_cert_parse(prep->data, prep->datalen);
192 return PTR_ERR(cert);
194 pr_devel("Cert Issuer: %s\n", cert->issuer);
195 pr_devel("Cert Subject: %s\n", cert->subject);
197 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
198 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
199 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
200 !pkey_algo[cert->pub->pkey_algo] ||
201 !pkey_algo[cert->sig.pkey_algo] ||
202 !hash_algo_name[cert->sig.pkey_hash_algo]) {
204 goto error_free_cert;
207 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
208 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
209 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
210 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
211 cert->valid_from.tm_min, cert->valid_from.tm_sec);
212 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
213 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
214 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
215 cert->valid_to.tm_min, cert->valid_to.tm_sec);
216 pr_devel("Cert Signature: %s + %s\n",
217 pkey_algo_name[cert->sig.pkey_algo],
218 hash_algo_name[cert->sig.pkey_hash_algo]);
220 if (!cert->fingerprint) {
221 pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
224 goto error_free_cert;
227 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
228 cert->pub->id_type = PKEY_ID_X509;
230 /* Check the signature on the key if it appears to be self-signed */
231 if (!cert->authority ||
232 strcmp(cert->fingerprint, cert->authority) == 0) {
233 ret = x509_check_signature(cert->pub, cert); /* self-signed */
235 goto error_free_cert;
237 ret = x509_validate_trust(cert, system_trusted_keyring);
242 /* Propose a description */
243 sulen = strlen(cert->subject);
244 srlen = strlen(cert->fingerprint);
246 desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
248 goto error_free_cert;
249 memcpy(desc, cert->subject, sulen);
251 desc[sulen + 1] = ' ';
252 memcpy(desc + sulen + 2, cert->fingerprint, srlen);
253 desc[sulen + 2 + srlen] = 0;
255 /* We're pinning the module by being linked against it */
256 __module_get(public_key_subtype.owner);
257 prep->type_data[0] = &public_key_subtype;
258 prep->type_data[1] = cert->fingerprint;
259 prep->payload = cert->pub;
260 prep->description = desc;
261 prep->quotalen = 100;
263 /* We've finished with the certificate */
265 cert->fingerprint = NULL;
270 x509_free_certificate(cert);
274 static struct asymmetric_key_parser x509_key_parser = {
275 .owner = THIS_MODULE,
277 .parse = x509_key_preparse,
283 static int __init x509_key_init(void)
285 return register_asymmetric_key_parser(&x509_key_parser);
288 static void __exit x509_key_exit(void)
290 unregister_asymmetric_key_parser(&x509_key_parser);
293 module_init(x509_key_init);
294 module_exit(x509_key_exit);
296 MODULE_DESCRIPTION("X.509 certificate parser");
297 MODULE_LICENSE("GPL");