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