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