blob: 30071233ee79c0972b5fb9aab50e54de764f45b7 [file] [log] [blame]
AKASHI Takahirob4adf622019-11-13 09:45:00 +09001// 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#ifdef __UBOOT__
10#include <common.h>
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090011#include <image.h>
Simon Glass61b29b82020-02-03 07:36:15 -070012#include <dm/devres.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090013#include <linux/compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070014#include <linux/err.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090015#include <linux/errno.h>
Simon Glass1e94b462023-09-14 18:21:46 -060016#include <linux/printk.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090017#else
18#include <linux/module.h>
19#endif
20#include <linux/kernel.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090021#ifdef __UBOOT__
22#include <crypto/x509_parser.h>
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060023#include <u-boot/hash-checksum.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090024#else
AKASHI Takahirob4adf622019-11-13 09:45:00 +090025#include <linux/slab.h>
26#include <keys/asymmetric-subtype.h>
27#include <keys/asymmetric-parser.h>
28#include <keys/system_keyring.h>
29#include <crypto/hash.h>
30#include "asymmetric_keys.h"
AKASHI Takahirob4adf622019-11-13 09:45:00 +090031#include "x509_parser.h"
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090032#endif
AKASHI Takahirob4adf622019-11-13 09:45:00 +090033
34/*
35 * Set up the signature parameters in an X.509 certificate. This involves
36 * digesting the signed data and extracting the signature.
37 */
38int x509_get_sig_params(struct x509_certificate *cert)
39{
40 struct public_key_signature *sig = cert->sig;
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090041#ifdef __UBOOT__
42 struct image_region region;
43#else
AKASHI Takahirob4adf622019-11-13 09:45:00 +090044 struct crypto_shash *tfm;
45 struct shash_desc *desc;
46 size_t desc_size;
47#endif
48 int ret;
49
50 pr_devel("==>%s()\n", __func__);
51
52 if (!cert->pub->pkey_algo)
53 cert->unsupported_key = true;
54
55 if (!sig->pkey_algo)
56 cert->unsupported_sig = true;
57
58 /* We check the hash if we can - even if we can't then verify it */
59 if (!sig->hash_algo) {
60 cert->unsupported_sig = true;
61 return 0;
62 }
63
64 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
65 if (!sig->s)
66 return -ENOMEM;
67
68 sig->s_size = cert->raw_sig_size;
69
70#ifdef __UBOOT__
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090071 if (!sig->hash_algo)
72 return -ENOPKG;
73 if (!strcmp(sig->hash_algo, "sha256"))
74 sig->digest_size = SHA256_SUM_LEN;
Dhananjay Phadkee146a2c2022-03-15 10:19:32 -070075 else if (!strcmp(sig->hash_algo, "sha384"))
76 sig->digest_size = SHA384_SUM_LEN;
77 else if (!strcmp(sig->hash_algo, "sha512"))
78 sig->digest_size = SHA512_SUM_LEN;
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090079 else if (!strcmp(sig->hash_algo, "sha1"))
80 sig->digest_size = SHA1_SUM_LEN;
81 else
82 return -ENOPKG;
83
84 sig->digest = calloc(1, sig->digest_size);
85 if (!sig->digest)
86 return -ENOMEM;
87
88 region.data = cert->tbs;
89 region.size = cert->tbs_size;
90 hash_calculate(sig->hash_algo, &region, 1, sig->digest);
91
92 /* TODO: is_hash_blacklisted()? */
93
AKASHI Takahirob4adf622019-11-13 09:45:00 +090094 ret = 0;
95#else
96 /* Allocate the hashing algorithm we're going to need and find out how
97 * big the hash operational data will be.
98 */
99 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
100 if (IS_ERR(tfm)) {
101 if (PTR_ERR(tfm) == -ENOENT) {
102 cert->unsupported_sig = true;
103 return 0;
104 }
105 return PTR_ERR(tfm);
106 }
107
108 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
109 sig->digest_size = crypto_shash_digestsize(tfm);
110
111 ret = -ENOMEM;
112 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
113 if (!sig->digest)
114 goto error;
115
116 desc = kzalloc(desc_size, GFP_KERNEL);
117 if (!desc)
118 goto error;
119
120 desc->tfm = tfm;
121
122 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
123 if (ret < 0)
124 goto error_2;
125
126 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
127 if (ret == -EKEYREJECTED) {
128 pr_err("Cert %*phN is blacklisted\n",
129 sig->digest_size, sig->digest);
130 cert->blacklisted = true;
131 ret = 0;
132 }
133
134error_2:
135 kfree(desc);
136error:
137 crypto_free_shash(tfm);
138#endif /* __UBOOT__ */
139 pr_devel("<==%s() = %d\n", __func__, ret);
140 return ret;
141}
142
AKASHI Takahirob4adf622019-11-13 09:45:00 +0900143/*
144 * Check for self-signedness in an X.509 cert and if found, check the signature
145 * immediately if we can.
146 */
147int x509_check_for_self_signed(struct x509_certificate *cert)
148{
149 int ret = 0;
150
151 pr_devel("==>%s()\n", __func__);
152
153 if (cert->raw_subject_size != cert->raw_issuer_size ||
154 memcmp(cert->raw_subject, cert->raw_issuer,
155 cert->raw_issuer_size) != 0)
156 goto not_self_signed;
157
158 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
159 /* If the AKID is present it may have one or two parts. If
160 * both are supplied, both must match.
161 */
162 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
163 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
164
165 if (!a && !b)
166 goto not_self_signed;
167
168 ret = -EKEYREJECTED;
169 if (((a && !b) || (b && !a)) &&
170 cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
171 goto out;
172 }
173
174 ret = -EKEYREJECTED;
175 if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)
176 goto out;
177
178 ret = public_key_verify_signature(cert->pub, cert->sig);
179 if (ret < 0) {
180 if (ret == -ENOPKG) {
181 cert->unsupported_sig = true;
182 ret = 0;
183 }
184 goto out;
185 }
186
187 pr_devel("Cert Self-signature verified");
188 cert->self_signed = true;
189
190out:
191 pr_devel("<==%s() = %d\n", __func__, ret);
192 return ret;
193
194not_self_signed:
195 pr_devel("<==%s() = 0 [not]\n", __func__);
196 return 0;
197}
198
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +0900199#ifndef __UBOOT__
AKASHI Takahirob4adf622019-11-13 09:45:00 +0900200/*
201 * Attempt to parse a data blob for a key as an X509 certificate.
202 */
203static int x509_key_preparse(struct key_preparsed_payload *prep)
204{
205 struct asymmetric_key_ids *kids;
206 struct x509_certificate *cert;
207 const char *q;
208 size_t srlen, sulen;
209 char *desc = NULL, *p;
210 int ret;
211
212 cert = x509_cert_parse(prep->data, prep->datalen);
213 if (IS_ERR(cert))
214 return PTR_ERR(cert);
215
216 pr_devel("Cert Issuer: %s\n", cert->issuer);
217 pr_devel("Cert Subject: %s\n", cert->subject);
218
219 if (cert->unsupported_key) {
220 ret = -ENOPKG;
221 goto error_free_cert;
222 }
223
224 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
225 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
226
227 cert->pub->id_type = "X509";
228
229 if (cert->unsupported_sig) {
230 public_key_signature_free(cert->sig);
231 cert->sig = NULL;
232 } else {
233 pr_devel("Cert Signature: %s + %s\n",
234 cert->sig->pkey_algo, cert->sig->hash_algo);
235 }
236
237 /* Don't permit addition of blacklisted keys */
238 ret = -EKEYREJECTED;
239 if (cert->blacklisted)
240 goto error_free_cert;
241
242 /* Propose a description */
243 sulen = strlen(cert->subject);
244 if (cert->raw_skid) {
245 srlen = cert->raw_skid_size;
246 q = cert->raw_skid;
247 } else {
248 srlen = cert->raw_serial_size;
249 q = cert->raw_serial;
250 }
251
252 ret = -ENOMEM;
253 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
254 if (!desc)
255 goto error_free_cert;
256 p = memcpy(desc, cert->subject, sulen);
257 p += sulen;
258 *p++ = ':';
259 *p++ = ' ';
260 p = bin2hex(p, q, srlen);
261 *p = 0;
262
263 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
264 if (!kids)
265 goto error_free_desc;
266 kids->id[0] = cert->id;
267 kids->id[1] = cert->skid;
268
269 /* We're pinning the module by being linked against it */
270 __module_get(public_key_subtype.owner);
271 prep->payload.data[asym_subtype] = &public_key_subtype;
272 prep->payload.data[asym_key_ids] = kids;
273 prep->payload.data[asym_crypto] = cert->pub;
274 prep->payload.data[asym_auth] = cert->sig;
275 prep->description = desc;
276 prep->quotalen = 100;
277
278 /* We've finished with the certificate */
279 cert->pub = NULL;
280 cert->id = NULL;
281 cert->skid = NULL;
282 cert->sig = NULL;
283 desc = NULL;
284 ret = 0;
285
286error_free_desc:
287 kfree(desc);
288error_free_cert:
289 x509_free_certificate(cert);
290 return ret;
291}
292
293static struct asymmetric_key_parser x509_key_parser = {
294 .owner = THIS_MODULE,
295 .name = "x509",
296 .parse = x509_key_preparse,
297};
298
299/*
300 * Module stuff
301 */
302static int __init x509_key_init(void)
303{
304 return register_asymmetric_key_parser(&x509_key_parser);
305}
306
307static void __exit x509_key_exit(void)
308{
309 unregister_asymmetric_key_parser(&x509_key_parser);
310}
311
312module_init(x509_key_init);
313module_exit(x509_key_exit);
314#endif /* !__UBOOT__ */
315
316MODULE_DESCRIPTION("X.509 certificate parser");
317MODULE_AUTHOR("Red Hat, Inc.");
318MODULE_LICENSE("GPL");