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