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