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