blob: 4ba13c1adc3a3590482666006063a8018f5f1ec9 [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__
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090010#include <image.h>
Simon Glass61b29b82020-02-03 07:36:15 -070011#include <dm/devres.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090012#include <linux/compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070013#include <linux/err.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090014#include <linux/errno.h>
Simon Glass1e94b462023-09-14 18:21:46 -060015#include <linux/printk.h>
AKASHI Takahirob4adf622019-11-13 09:45:00 +090016#else
17#include <linux/module.h>
18#endif
19#include <linux/kernel.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090020#ifdef __UBOOT__
21#include <crypto/x509_parser.h>
Alexandru Gagniuc0bcb28d2021-02-19 12:45:10 -060022#include <u-boot/hash-checksum.h>
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090023#else
AKASHI Takahirob4adf622019-11-13 09:45:00 +090024#include <linux/slab.h>
25#include <keys/asymmetric-subtype.h>
26#include <keys/asymmetric-parser.h>
27#include <keys/system_keyring.h>
28#include <crypto/hash.h>
29#include "asymmetric_keys.h"
AKASHI Takahirob4adf622019-11-13 09:45:00 +090030#include "x509_parser.h"
AKASHI Takahiroe3f5c9c2020-04-21 09:38:17 +090031#endif
AKASHI Takahirob4adf622019-11-13 09:45:00 +090032
33/*
34 * Set up the signature parameters in an X.509 certificate. This involves
35 * digesting the signed data and extracting the signature.
36 */
37int x509_get_sig_params(struct x509_certificate *cert)
38{
39 struct public_key_signature *sig = cert->sig;
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090040#ifdef __UBOOT__
41 struct image_region region;
42#else
AKASHI Takahirob4adf622019-11-13 09:45:00 +090043 struct crypto_shash *tfm;
44 struct shash_desc *desc;
45 size_t desc_size;
46#endif
47 int ret;
48
49 pr_devel("==>%s()\n", __func__);
50
51 if (!cert->pub->pkey_algo)
52 cert->unsupported_key = true;
53
54 if (!sig->pkey_algo)
55 cert->unsupported_sig = true;
56
57 /* We check the hash if we can - even if we can't then verify it */
58 if (!sig->hash_algo) {
59 cert->unsupported_sig = true;
60 return 0;
61 }
62
63 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
64 if (!sig->s)
65 return -ENOMEM;
66
67 sig->s_size = cert->raw_sig_size;
68
69#ifdef __UBOOT__
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090070 if (!sig->hash_algo)
71 return -ENOPKG;
72 if (!strcmp(sig->hash_algo, "sha256"))
73 sig->digest_size = SHA256_SUM_LEN;
Dhananjay Phadkee146a2c2022-03-15 10:19:32 -070074 else if (!strcmp(sig->hash_algo, "sha384"))
75 sig->digest_size = SHA384_SUM_LEN;
76 else if (!strcmp(sig->hash_algo, "sha512"))
77 sig->digest_size = SHA512_SUM_LEN;
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +090078 else if (!strcmp(sig->hash_algo, "sha1"))
79 sig->digest_size = SHA1_SUM_LEN;
80 else
81 return -ENOPKG;
82
83 sig->digest = calloc(1, sig->digest_size);
84 if (!sig->digest)
85 return -ENOMEM;
86
87 region.data = cert->tbs;
88 region.size = cert->tbs_size;
89 hash_calculate(sig->hash_algo, &region, 1, sig->digest);
90
91 /* TODO: is_hash_blacklisted()? */
92
AKASHI Takahirob4adf622019-11-13 09:45:00 +090093 ret = 0;
94#else
95 /* Allocate the hashing algorithm we're going to need and find out how
96 * big the hash operational data will be.
97 */
98 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
99 if (IS_ERR(tfm)) {
100 if (PTR_ERR(tfm) == -ENOENT) {
101 cert->unsupported_sig = true;
102 return 0;
103 }
104 return PTR_ERR(tfm);
105 }
106
107 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
108 sig->digest_size = crypto_shash_digestsize(tfm);
109
110 ret = -ENOMEM;
111 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
112 if (!sig->digest)
113 goto error;
114
115 desc = kzalloc(desc_size, GFP_KERNEL);
116 if (!desc)
117 goto error;
118
119 desc->tfm = tfm;
120
121 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
122 if (ret < 0)
123 goto error_2;
124
125 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
126 if (ret == -EKEYREJECTED) {
127 pr_err("Cert %*phN is blacklisted\n",
128 sig->digest_size, sig->digest);
129 cert->blacklisted = true;
130 ret = 0;
131 }
132
133error_2:
134 kfree(desc);
135error:
136 crypto_free_shash(tfm);
137#endif /* __UBOOT__ */
138 pr_devel("<==%s() = %d\n", __func__, ret);
139 return ret;
140}
141
Raymond Maofa1289c2024-10-03 14:50:26 -0700142#endif /* !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) */
AKASHI Takahirob4adf622019-11-13 09:45:00 +0900143
AKASHI Takahiro6244b3c2020-07-21 19:35:18 +0900144#ifndef __UBOOT__
AKASHI Takahirob4adf622019-11-13 09:45:00 +0900145/*
146 * Attempt to parse a data blob for a key as an X509 certificate.
147 */
148static int x509_key_preparse(struct key_preparsed_payload *prep)
149{
150 struct asymmetric_key_ids *kids;
151 struct x509_certificate *cert;
152 const char *q;
153 size_t srlen, sulen;
154 char *desc = NULL, *p;
155 int ret;
156
157 cert = x509_cert_parse(prep->data, prep->datalen);
158 if (IS_ERR(cert))
159 return PTR_ERR(cert);
160
161 pr_devel("Cert Issuer: %s\n", cert->issuer);
162 pr_devel("Cert Subject: %s\n", cert->subject);
163
164 if (cert->unsupported_key) {
165 ret = -ENOPKG;
166 goto error_free_cert;
167 }
168
169 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
170 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
171
172 cert->pub->id_type = "X509";
173
174 if (cert->unsupported_sig) {
175 public_key_signature_free(cert->sig);
176 cert->sig = NULL;
177 } else {
178 pr_devel("Cert Signature: %s + %s\n",
179 cert->sig->pkey_algo, cert->sig->hash_algo);
180 }
181
182 /* Don't permit addition of blacklisted keys */
183 ret = -EKEYREJECTED;
184 if (cert->blacklisted)
185 goto error_free_cert;
186
187 /* Propose a description */
188 sulen = strlen(cert->subject);
189 if (cert->raw_skid) {
190 srlen = cert->raw_skid_size;
191 q = cert->raw_skid;
192 } else {
193 srlen = cert->raw_serial_size;
194 q = cert->raw_serial;
195 }
196
197 ret = -ENOMEM;
198 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
199 if (!desc)
200 goto error_free_cert;
201 p = memcpy(desc, cert->subject, sulen);
202 p += sulen;
203 *p++ = ':';
204 *p++ = ' ';
205 p = bin2hex(p, q, srlen);
206 *p = 0;
207
208 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
209 if (!kids)
210 goto error_free_desc;
211 kids->id[0] = cert->id;
212 kids->id[1] = cert->skid;
213
214 /* We're pinning the module by being linked against it */
215 __module_get(public_key_subtype.owner);
216 prep->payload.data[asym_subtype] = &public_key_subtype;
217 prep->payload.data[asym_key_ids] = kids;
218 prep->payload.data[asym_crypto] = cert->pub;
219 prep->payload.data[asym_auth] = cert->sig;
220 prep->description = desc;
221 prep->quotalen = 100;
222
223 /* We've finished with the certificate */
224 cert->pub = NULL;
225 cert->id = NULL;
226 cert->skid = NULL;
227 cert->sig = NULL;
228 desc = NULL;
229 ret = 0;
230
231error_free_desc:
232 kfree(desc);
233error_free_cert:
234 x509_free_certificate(cert);
235 return ret;
236}
237
238static struct asymmetric_key_parser x509_key_parser = {
239 .owner = THIS_MODULE,
240 .name = "x509",
241 .parse = x509_key_preparse,
242};
243
244/*
245 * Module stuff
246 */
247static int __init x509_key_init(void)
248{
249 return register_asymmetric_key_parser(&x509_key_parser);
250}
251
252static void __exit x509_key_exit(void)
253{
254 unregister_asymmetric_key_parser(&x509_key_parser);
255}
256
257module_init(x509_key_init);
258module_exit(x509_key_exit);
259#endif /* !__UBOOT__ */
260
261MODULE_DESCRIPTION("X.509 certificate parser");
262MODULE_AUTHOR("Red Hat, Inc.");
263MODULE_LICENSE("GPL");