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