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"
27 static bool use_builtin_keys;
28 static struct asymmetric_key_id *ca_keyid;
32 struct asymmetric_key_id id;
33 unsigned char data[10];
36 static int __init ca_keys_setup(char *str)
38 if (!str) /* default system keyring */
41 if (strncmp(str, "id:", 3) == 0) {
42 struct asymmetric_key_id *p = &cakey.id;
43 size_t hexlen = (strlen(str) - 3) / 2;
46 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
47 pr_err("Missing or invalid ca_keys id\n");
51 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
53 pr_err("Unparsable ca_keys id hex string\n");
55 ca_keyid = p; /* owner key 'id:xxxxxx' */
56 } else if (strcmp(str, "builtin") == 0) {
57 use_builtin_keys = true;
62 __setup("ca_keys=", ca_keys_setup);
66 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
67 * @keyring: The keys to search.
68 * @id: The issuer & serialNumber to look for or NULL.
69 * @skid: The subjectKeyIdentifier to look for or NULL.
70 * @partial: Use partial match if true, exact if false.
72 * Find a key in the given keyring by identifier. The preferred identifier is
73 * the issuer + serialNumber and the fallback identifier is the
74 * subjectKeyIdentifier. If both are given, the lookup is by the former, but
75 * the latter must also match.
77 struct key *x509_request_asymmetric_key(struct key *keyring,
78 const struct asymmetric_key_id *id,
79 const struct asymmetric_key_id *skid,
96 /* Construct an identifier "id:<keyid>". */
97 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
99 return ERR_PTR(-ENOMEM);
109 p = bin2hex(p, lookup, len);
112 pr_debug("Look up: \"%s\"\n", req);
114 ref = keyring_search(make_key_ref(keyring, 1),
115 &key_type_asymmetric, req);
117 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
121 switch (PTR_ERR(ref)) {
122 /* Hide some search errors */
126 return ERR_PTR(-ENOKEY);
128 return ERR_CAST(ref);
132 key = key_ref_to_ptr(ref);
134 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
136 pr_debug("issuer+serial match, but expected SKID missing\n");
139 if (!asymmetric_key_id_same(skid, kids->id[1])) {
140 pr_debug("issuer+serial match, but SKID does not\n");
145 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
150 return ERR_PTR(-EKEYREJECTED);
152 EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
155 * Set up the signature parameters in an X.509 certificate. This involves
156 * digesting the signed data and extracting the signature.
158 int x509_get_sig_params(struct x509_certificate *cert)
160 struct crypto_shash *tfm;
161 struct shash_desc *desc;
162 size_t digest_size, desc_size;
166 pr_devel("==>%s()\n", __func__);
168 if (cert->unsupported_crypto)
173 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
174 if (!cert->sig.rsa.s)
176 cert->sig.nr_mpi = 1;
178 /* Allocate the hashing algorithm we're going to need and find out how
179 * big the hash operational data will be.
181 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
183 if (PTR_ERR(tfm) == -ENOENT) {
184 cert->unsupported_crypto = true;
190 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
191 digest_size = crypto_shash_digestsize(tfm);
193 /* We allocate the hash operational data storage on the end of the
194 * digest storage space.
197 digest = kzalloc(ALIGN(digest_size, __alignof__(*desc)) + desc_size,
202 cert->sig.digest = digest;
203 cert->sig.digest_size = digest_size;
205 desc = PTR_ALIGN(digest + digest_size, __alignof__(*desc));
207 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
209 ret = crypto_shash_init(desc);
213 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
215 crypto_free_shash(tfm);
216 pr_devel("<==%s() = %d\n", __func__, ret);
219 EXPORT_SYMBOL_GPL(x509_get_sig_params);
222 * Check the signature on a certificate using the provided public key
224 int x509_check_signature(const struct public_key *pub,
225 struct x509_certificate *cert)
229 pr_devel("==>%s()\n", __func__);
231 ret = x509_get_sig_params(cert);
235 ret = public_key_verify_signature(pub, &cert->sig);
237 cert->unsupported_crypto = true;
238 pr_debug("Cert Verification: %d\n", ret);
241 EXPORT_SYMBOL_GPL(x509_check_signature);
244 * Check the new certificate against the ones in the trust keyring. If one of
245 * those is the signing key and validates the new certificate, then mark the
246 * new certificate as being trusted.
248 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
249 * find a matching parent certificate in the trusted list and an error if there
250 * is a matching certificate but the signature check fails.
252 static int x509_validate_trust(struct x509_certificate *cert,
253 struct key *trust_keyring)
261 if (ca_keyid && !asymmetric_key_id_partial(cert->akid_skid, ca_keyid))
264 key = x509_request_asymmetric_key(trust_keyring,
265 cert->akid_id, cert->akid_skid,
268 if (!use_builtin_keys
269 || test_bit(KEY_FLAG_BUILTIN, &key->flags))
270 ret = x509_check_signature(key->payload.data[asym_crypto],
278 * Attempt to parse a data blob for a key as an X509 certificate.
280 static int x509_key_preparse(struct key_preparsed_payload *prep)
282 struct asymmetric_key_ids *kids;
283 struct x509_certificate *cert;
286 char *desc = NULL, *p;
289 cert = x509_cert_parse(prep->data, prep->datalen);
291 return PTR_ERR(cert);
293 pr_devel("Cert Issuer: %s\n", cert->issuer);
294 pr_devel("Cert Subject: %s\n", cert->subject);
296 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
297 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
298 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
299 !pkey_algo[cert->pub->pkey_algo] ||
300 !pkey_algo[cert->sig.pkey_algo] ||
301 !hash_algo_name[cert->sig.pkey_hash_algo]) {
303 goto error_free_cert;
306 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
307 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
308 pr_devel("Cert Signature: %s + %s\n",
309 pkey_algo_name[cert->sig.pkey_algo],
310 hash_algo_name[cert->sig.pkey_hash_algo]);
312 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
313 cert->pub->id_type = PKEY_ID_X509;
315 /* Check the signature on the key if it appears to be self-signed */
316 if ((!cert->akid_skid && !cert->akid_id) ||
317 asymmetric_key_id_same(cert->skid, cert->akid_skid) ||
318 asymmetric_key_id_same(cert->id, cert->akid_id)) {
319 ret = x509_check_signature(cert->pub, cert); /* self-signed */
321 goto error_free_cert;
322 } else if (!prep->trusted) {
323 ret = x509_validate_trust(cert, get_system_trusted_keyring());
328 /* Propose a description */
329 sulen = strlen(cert->subject);
330 if (cert->raw_skid) {
331 srlen = cert->raw_skid_size;
334 srlen = cert->raw_serial_size;
335 q = cert->raw_serial;
339 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
341 goto error_free_cert;
342 p = memcpy(desc, cert->subject, sulen);
346 p = bin2hex(p, q, srlen);
349 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
351 goto error_free_desc;
352 kids->id[0] = cert->id;
353 kids->id[1] = cert->skid;
355 /* We're pinning the module by being linked against it */
356 __module_get(public_key_subtype.owner);
357 prep->payload.data[asym_subtype] = &public_key_subtype;
358 prep->payload.data[asym_key_ids] = kids;
359 prep->payload.data[asym_crypto] = cert->pub;
360 prep->description = desc;
361 prep->quotalen = 100;
363 /* We've finished with the certificate */
373 x509_free_certificate(cert);
377 static struct asymmetric_key_parser x509_key_parser = {
378 .owner = THIS_MODULE,
380 .parse = x509_key_preparse,
386 static int __init x509_key_init(void)
388 return register_asymmetric_key_parser(&x509_key_parser);
391 static void __exit x509_key_exit(void)
393 unregister_asymmetric_key_parser(&x509_key_parser);
396 module_init(x509_key_init);
397 module_exit(x509_key_exit);
399 MODULE_DESCRIPTION("X.509 certificate parser");
400 MODULE_LICENSE("GPL");