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