1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Instantiate a public key crypto key from an X.509 Certificate
3  *
4  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define pr_fmt(fmt) "X.509: "fmt
9 #include <crypto/hash.h>
10 #include <crypto/sm2.h>
11 #include <keys/asymmetric-parser.h>
12 #include <keys/asymmetric-subtype.h>
13 #include <keys/system_keyring.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include "asymmetric_keys.h"
19 #include "x509_parser.h"
20 
21 /*
22  * Set up the signature parameters in an X.509 certificate.  This involves
23  * digesting the signed data and extracting the signature.
24  */
25 int x509_get_sig_params(struct x509_certificate *cert)
26 {
27 	struct public_key_signature *sig = cert->sig;
28 	struct crypto_shash *tfm;
29 	struct shash_desc *desc;
30 	size_t desc_size;
31 	int ret;
32 
33 	pr_devel("==>%s()\n", __func__);
34 
35 	sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
36 	if (!sig->s)
37 		return -ENOMEM;
38 
39 	sig->s_size = cert->raw_sig_size;
40 
41 	/* Allocate the hashing algorithm we're going to need and find out how
42 	 * big the hash operational data will be.
43 	 */
44 	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
45 	if (IS_ERR(tfm)) {
46 		if (PTR_ERR(tfm) == -ENOENT) {
47 			cert->unsupported_sig = true;
48 			return 0;
49 		}
50 		return PTR_ERR(tfm);
51 	}
52 
53 	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
54 	sig->digest_size = crypto_shash_digestsize(tfm);
55 
56 	ret = -ENOMEM;
57 	sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
58 	if (!sig->digest)
59 		goto error;
60 
61 	desc = kzalloc(desc_size, GFP_KERNEL);
62 	if (!desc)
63 		goto error;
64 
65 	desc->tfm = tfm;
66 
67 	if (strcmp(cert->pub->pkey_algo, "sm2") == 0) {
68 		ret = strcmp(sig->hash_algo, "sm3") != 0 ? -EINVAL :
69 		      crypto_shash_init(desc) ?:
70 		      sm2_compute_z_digest(desc, cert->pub->key,
71 					   cert->pub->keylen, sig->digest) ?:
72 		      crypto_shash_init(desc) ?:
73 		      crypto_shash_update(desc, sig->digest,
74 					  sig->digest_size) ?:
75 		      crypto_shash_finup(desc, cert->tbs, cert->tbs_size,
76 					 sig->digest);
77 	} else {
78 		ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size,
79 					  sig->digest);
80 	}
81 
82 	if (ret < 0)
83 		goto error_2;
84 
85 	ret = is_hash_blacklisted(sig->digest, sig->digest_size,
86 				  BLACKLIST_HASH_X509_TBS);
87 	if (ret == -EKEYREJECTED) {
88 		pr_err("Cert %*phN is blacklisted\n",
89 		       sig->digest_size, sig->digest);
90 		cert->blacklisted = true;
91 		ret = 0;
92 	}
93 
94 error_2:
95 	kfree(desc);
96 error:
97 	crypto_free_shash(tfm);
98 	pr_devel("<==%s() = %d\n", __func__, ret);
99 	return ret;
100 }
101 
102 /*
103  * Check for self-signedness in an X.509 cert and if found, check the signature
104  * immediately if we can.
105  */
106 int x509_check_for_self_signed(struct x509_certificate *cert)
107 {
108 	int ret = 0;
109 
110 	pr_devel("==>%s()\n", __func__);
111 
112 	if (cert->raw_subject_size != cert->raw_issuer_size ||
113 	    memcmp(cert->raw_subject, cert->raw_issuer,
114 		   cert->raw_issuer_size) != 0)
115 		goto not_self_signed;
116 
117 	if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
118 		/* If the AKID is present it may have one or two parts.  If
119 		 * both are supplied, both must match.
120 		 */
121 		bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
122 		bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
123 
124 		if (!a && !b)
125 			goto not_self_signed;
126 
127 		ret = -EKEYREJECTED;
128 		if (((a && !b) || (b && !a)) &&
129 		    cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
130 			goto out;
131 	}
132 
133 	ret = public_key_verify_signature(cert->pub, cert->sig);
134 	if (ret < 0) {
135 		if (ret == -ENOPKG) {
136 			cert->unsupported_sig = true;
137 			ret = 0;
138 		}
139 		goto out;
140 	}
141 
142 	pr_devel("Cert Self-signature verified");
143 	cert->self_signed = true;
144 
145 out:
146 	pr_devel("<==%s() = %d\n", __func__, ret);
147 	return ret;
148 
149 not_self_signed:
150 	pr_devel("<==%s() = 0 [not]\n", __func__);
151 	return 0;
152 }
153 
154 /*
155  * Attempt to parse a data blob for a key as an X509 certificate.
156  */
157 static int x509_key_preparse(struct key_preparsed_payload *prep)
158 {
159 	struct asymmetric_key_ids *kids;
160 	struct x509_certificate *cert;
161 	const char *q;
162 	size_t srlen, sulen;
163 	char *desc = NULL, *p;
164 	int ret;
165 
166 	cert = x509_cert_parse(prep->data, prep->datalen);
167 	if (IS_ERR(cert))
168 		return PTR_ERR(cert);
169 
170 	pr_devel("Cert Issuer: %s\n", cert->issuer);
171 	pr_devel("Cert Subject: %s\n", cert->subject);
172 	pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
173 	pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
174 
175 	cert->pub->id_type = "X509";
176 
177 	if (cert->unsupported_sig) {
178 		public_key_signature_free(cert->sig);
179 		cert->sig = NULL;
180 	} else {
181 		pr_devel("Cert Signature: %s + %s\n",
182 			 cert->sig->pkey_algo, cert->sig->hash_algo);
183 	}
184 
185 	/* Don't permit addition of blacklisted keys */
186 	ret = -EKEYREJECTED;
187 	if (cert->blacklisted)
188 		goto error_free_cert;
189 
190 	/* Propose a description */
191 	sulen = strlen(cert->subject);
192 	if (cert->raw_skid) {
193 		srlen = cert->raw_skid_size;
194 		q = cert->raw_skid;
195 	} else {
196 		srlen = cert->raw_serial_size;
197 		q = cert->raw_serial;
198 	}
199 
200 	ret = -ENOMEM;
201 	desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
202 	if (!desc)
203 		goto error_free_cert;
204 	p = memcpy(desc, cert->subject, sulen);
205 	p += sulen;
206 	*p++ = ':';
207 	*p++ = ' ';
208 	p = bin2hex(p, q, srlen);
209 	*p = 0;
210 
211 	kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
212 	if (!kids)
213 		goto error_free_desc;
214 	kids->id[0] = cert->id;
215 	kids->id[1] = cert->skid;
216 	kids->id[2] = asymmetric_key_generate_id(cert->raw_subject,
217 						 cert->raw_subject_size,
218 						 "", 0);
219 	if (IS_ERR(kids->id[2])) {
220 		ret = PTR_ERR(kids->id[2]);
221 		goto error_free_kids;
222 	}
223 
224 	/* We're pinning the module by being linked against it */
225 	__module_get(public_key_subtype.owner);
226 	prep->payload.data[asym_subtype] = &public_key_subtype;
227 	prep->payload.data[asym_key_ids] = kids;
228 	prep->payload.data[asym_crypto] = cert->pub;
229 	prep->payload.data[asym_auth] = cert->sig;
230 	prep->description = desc;
231 	prep->quotalen = 100;
232 
233 	/* We've finished with the certificate */
234 	cert->pub = NULL;
235 	cert->id = NULL;
236 	cert->skid = NULL;
237 	cert->sig = NULL;
238 	desc = NULL;
239 	kids = NULL;
240 	ret = 0;
241 
242 error_free_kids:
243 	kfree(kids);
244 error_free_desc:
245 	kfree(desc);
246 error_free_cert:
247 	x509_free_certificate(cert);
248 	return ret;
249 }
250 
251 static struct asymmetric_key_parser x509_key_parser = {
252 	.owner	= THIS_MODULE,
253 	.name	= "x509",
254 	.parse	= x509_key_preparse,
255 };
256 
257 /*
258  * Module stuff
259  */
260 extern int __init certs_selftest(void);
261 static int __init x509_key_init(void)
262 {
263 	int ret;
264 
265 	ret = register_asymmetric_key_parser(&x509_key_parser);
266 	if (ret < 0)
267 		return ret;
268 	return fips_signature_selftest();
269 }
270 
271 static void __exit x509_key_exit(void)
272 {
273 	unregister_asymmetric_key_parser(&x509_key_parser);
274 }
275 
276 module_init(x509_key_init);
277 module_exit(x509_key_exit);
278 
279 MODULE_DESCRIPTION("X.509 certificate parser");
280 MODULE_AUTHOR("Red Hat, Inc.");
281 MODULE_LICENSE("GPL");
282