1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Instantiate a public key crypto key from an X.509 Certificate
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "X.509: "fmt
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <keys/asymmetric-subtype.h>
13 #include <keys/asymmetric-parser.h>
14 #include <keys/system_keyring.h>
15 #include <crypto/hash.h>
16 #include "asymmetric_keys.h"
17 #include "x509_parser.h"
20 * Set up the signature parameters in an X.509 certificate. This involves
21 * digesting the signed data and extracting the signature.
23 int x509_get_sig_params(struct x509_certificate *cert)
25 struct public_key_signature *sig = cert->sig;
26 struct crypto_shash *tfm;
27 struct shash_desc *desc;
31 pr_devel("==>%s()\n", __func__);
33 sig->data = cert->tbs;
34 sig->data_size = cert->tbs_size;
36 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
40 sig->s_size = cert->raw_sig_size;
42 /* Allocate the hashing algorithm we're going to need and find out how
43 * big the hash operational data will be.
45 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
47 if (PTR_ERR(tfm) == -ENOENT) {
48 cert->unsupported_sig = true;
54 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
55 sig->digest_size = crypto_shash_digestsize(tfm);
58 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
62 desc = kzalloc(desc_size, GFP_KERNEL);
68 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
72 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
73 if (ret == -EKEYREJECTED) {
74 pr_err("Cert %*phN is blacklisted\n",
75 sig->digest_size, sig->digest);
76 cert->blacklisted = true;
83 crypto_free_shash(tfm);
84 pr_devel("<==%s() = %d\n", __func__, ret);
89 * Check for self-signedness in an X.509 cert and if found, check the signature
90 * immediately if we can.
92 int x509_check_for_self_signed(struct x509_certificate *cert)
96 pr_devel("==>%s()\n", __func__);
98 if (cert->raw_subject_size != cert->raw_issuer_size ||
99 memcmp(cert->raw_subject, cert->raw_issuer,
100 cert->raw_issuer_size) != 0)
101 goto not_self_signed;
103 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
104 /* If the AKID is present it may have one or two parts. If
105 * both are supplied, both must match.
107 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
108 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
111 goto not_self_signed;
114 if (((a && !b) || (b && !a)) &&
115 cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
119 ret = public_key_verify_signature(cert->pub, cert->sig);
121 if (ret == -ENOPKG) {
122 cert->unsupported_sig = true;
128 pr_devel("Cert Self-signature verified");
129 cert->self_signed = true;
132 pr_devel("<==%s() = %d\n", __func__, ret);
136 pr_devel("<==%s() = 0 [not]\n", __func__);
141 * Attempt to parse a data blob for a key as an X509 certificate.
143 static int x509_key_preparse(struct key_preparsed_payload *prep)
145 struct asymmetric_key_ids *kids;
146 struct x509_certificate *cert;
149 char *desc = NULL, *p;
152 cert = x509_cert_parse(prep->data, prep->datalen);
154 return PTR_ERR(cert);
156 pr_devel("Cert Issuer: %s\n", cert->issuer);
157 pr_devel("Cert Subject: %s\n", cert->subject);
158 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
159 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
161 cert->pub->id_type = "X509";
163 if (cert->unsupported_sig) {
164 public_key_signature_free(cert->sig);
167 pr_devel("Cert Signature: %s + %s\n",
168 cert->sig->pkey_algo, cert->sig->hash_algo);
171 /* Don't permit addition of blacklisted keys */
173 if (cert->blacklisted)
174 goto error_free_cert;
176 /* Propose a description */
177 sulen = strlen(cert->subject);
178 if (cert->raw_skid) {
179 srlen = cert->raw_skid_size;
182 srlen = cert->raw_serial_size;
183 q = cert->raw_serial;
187 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
189 goto error_free_cert;
190 p = memcpy(desc, cert->subject, sulen);
194 p = bin2hex(p, q, srlen);
197 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
199 goto error_free_desc;
200 kids->id[0] = cert->id;
201 kids->id[1] = cert->skid;
202 kids->id[2] = asymmetric_key_generate_id(cert->raw_subject,
203 cert->raw_subject_size,
205 if (IS_ERR(kids->id[2])) {
206 ret = PTR_ERR(kids->id[2]);
207 goto error_free_kids;
210 /* We're pinning the module by being linked against it */
211 __module_get(public_key_subtype.owner);
212 prep->payload.data[asym_subtype] = &public_key_subtype;
213 prep->payload.data[asym_key_ids] = kids;
214 prep->payload.data[asym_crypto] = cert->pub;
215 prep->payload.data[asym_auth] = cert->sig;
216 prep->description = desc;
217 prep->quotalen = 100;
219 /* We've finished with the certificate */
233 x509_free_certificate(cert);
237 static struct asymmetric_key_parser x509_key_parser = {
238 .owner = THIS_MODULE,
240 .parse = x509_key_preparse,
246 static int __init x509_key_init(void)
248 return register_asymmetric_key_parser(&x509_key_parser);
251 static void __exit x509_key_exit(void)
253 unregister_asymmetric_key_parser(&x509_key_parser);
256 module_init(x509_key_init);
257 module_exit(x509_key_exit);
259 MODULE_DESCRIPTION("X.509 certificate parser");
260 MODULE_AUTHOR("Red Hat, Inc.");
261 MODULE_LICENSE("GPL");