X.509: Extract signature digest and make self-signed cert checks earlier
[deliverable/linux.git] / crypto / asymmetric_keys / x509_public_key.c
CommitLineData
c26fd69f
DH
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>
c26fd69f
DH
16#include <keys/asymmetric-subtype.h>
17#include <keys/asymmetric-parser.h>
3be4beaf 18#include <keys/system_keyring.h>
c26fd69f
DH
19#include <crypto/hash.h>
20#include "asymmetric_keys.h"
c26fd69f
DH
21#include "x509_parser.h"
22
32c4741c 23static bool use_builtin_keys;
46963b77 24static struct asymmetric_key_id *ca_keyid;
ffb70f61
DK
25
26#ifndef MODULE
f2b3dee4
MZ
27static struct {
28 struct asymmetric_key_id id;
29 unsigned char data[10];
30} cakey;
31
ffb70f61
DK
32static int __init ca_keys_setup(char *str)
33{
34 if (!str) /* default system keyring */
35 return 1;
36
46963b77 37 if (strncmp(str, "id:", 3) == 0) {
f2b3dee4
MZ
38 struct asymmetric_key_id *p = &cakey.id;
39 size_t hexlen = (strlen(str) - 3) / 2;
40 int ret;
41
42 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
43 pr_err("Missing or invalid ca_keys id\n");
44 return 1;
45 }
46
47 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
48 if (ret < 0)
49 pr_err("Unparsable ca_keys id hex string\n");
50 else
46963b77
DH
51 ca_keyid = p; /* owner key 'id:xxxxxx' */
52 } else if (strcmp(str, "builtin") == 0) {
32c4741c 53 use_builtin_keys = true;
46963b77 54 }
ffb70f61
DK
55
56 return 1;
57}
58__setup("ca_keys=", ca_keys_setup);
59#endif
60
5ce43ad2
DH
61/**
62 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
63 * @keyring: The keys to search.
4573b64a
DH
64 * @id: The issuer & serialNumber to look for or NULL.
65 * @skid: The subjectKeyIdentifier to look for or NULL.
f1b731db 66 * @partial: Use partial match if true, exact if false.
5ce43ad2 67 *
4573b64a
DH
68 * Find a key in the given keyring by identifier. The preferred identifier is
69 * the issuer + serialNumber and the fallback identifier is the
70 * subjectKeyIdentifier. If both are given, the lookup is by the former, but
71 * the latter must also match.
3be4beaf 72 */
5ce43ad2 73struct key *x509_request_asymmetric_key(struct key *keyring,
4573b64a
DH
74 const struct asymmetric_key_id *id,
75 const struct asymmetric_key_id *skid,
f1b731db 76 bool partial)
3be4beaf 77{
4573b64a
DH
78 struct key *key;
79 key_ref_t ref;
80 const char *lookup;
81 char *req, *p;
82 int len;
83
84 if (id) {
85 lookup = id->data;
86 len = id->len;
87 } else {
88 lookup = skid->data;
89 len = skid->len;
90 }
864e7a81 91
46963b77 92 /* Construct an identifier "id:<keyid>". */
4573b64a
DH
93 p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
94 if (!req)
3be4beaf
MZ
95 return ERR_PTR(-ENOMEM);
96
f1b731db
DK
97 if (partial) {
98 *p++ = 'i';
99 *p++ = 'd';
100 } else {
101 *p++ = 'e';
102 *p++ = 'x';
103 }
46963b77 104 *p++ = ':';
4573b64a 105 p = bin2hex(p, lookup, len);
46963b77 106 *p = 0;
3be4beaf 107
4573b64a 108 pr_debug("Look up: \"%s\"\n", req);
3be4beaf 109
4573b64a
DH
110 ref = keyring_search(make_key_ref(keyring, 1),
111 &key_type_asymmetric, req);
112 if (IS_ERR(ref))
113 pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
114 kfree(req);
3be4beaf 115
4573b64a
DH
116 if (IS_ERR(ref)) {
117 switch (PTR_ERR(ref)) {
3be4beaf
MZ
118 /* Hide some search errors */
119 case -EACCES:
120 case -ENOTDIR:
121 case -EAGAIN:
122 return ERR_PTR(-ENOKEY);
123 default:
4573b64a
DH
124 return ERR_CAST(ref);
125 }
126 }
127
128 key = key_ref_to_ptr(ref);
129 if (id && skid) {
130 const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
131 if (!kids->id[1]) {
132 pr_debug("issuer+serial match, but expected SKID missing\n");
133 goto reject;
134 }
135 if (!asymmetric_key_id_same(skid, kids->id[1])) {
136 pr_debug("issuer+serial match, but SKID does not\n");
137 goto reject;
3be4beaf
MZ
138 }
139 }
864e7a81 140
4573b64a
DH
141 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
142 return key;
3be4beaf 143
4573b64a
DH
144reject:
145 key_put(key);
146 return ERR_PTR(-EKEYREJECTED);
3be4beaf 147}
cf5b50fd 148EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
3be4beaf 149
c26fd69f 150/*
b426beb6
DH
151 * Set up the signature parameters in an X.509 certificate. This involves
152 * digesting the signed data and extracting the signature.
c26fd69f 153 */
b426beb6 154int x509_get_sig_params(struct x509_certificate *cert)
c26fd69f 155{
77d0910d 156 struct public_key_signature *sig = cert->sig;
c26fd69f
DH
157 struct crypto_shash *tfm;
158 struct shash_desc *desc;
77d0910d 159 size_t desc_size;
c26fd69f
DH
160 int ret;
161
162 pr_devel("==>%s()\n", __func__);
b426beb6 163
6c2dc5ae
DH
164 if (!cert->pub->pkey_algo)
165 cert->unsupported_key = true;
166
167 if (!sig->pkey_algo)
168 cert->unsupported_sig = true;
169
170 /* We check the hash if we can - even if we can't then verify it */
171 if (!sig->hash_algo) {
172 cert->unsupported_sig = true;
b426beb6 173 return 0;
6c2dc5ae 174 }
b426beb6 175
77d0910d
DH
176 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
177 if (!sig->s)
b426beb6 178 return -ENOMEM;
db6c43bd 179
77d0910d 180 sig->s_size = cert->raw_sig_size;
b426beb6 181
c26fd69f
DH
182 /* Allocate the hashing algorithm we're going to need and find out how
183 * big the hash operational data will be.
184 */
77d0910d 185 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
41559420
DH
186 if (IS_ERR(tfm)) {
187 if (PTR_ERR(tfm) == -ENOENT) {
6c2dc5ae
DH
188 cert->unsupported_sig = true;
189 return 0;
41559420
DH
190 }
191 return PTR_ERR(tfm);
192 }
c26fd69f
DH
193
194 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
77d0910d 195 sig->digest_size = crypto_shash_digestsize(tfm);
c26fd69f 196
c26fd69f 197 ret = -ENOMEM;
77d0910d
DH
198 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
199 if (!sig->digest)
b426beb6 200 goto error;
c26fd69f 201
77d0910d
DH
202 desc = kzalloc(desc_size, GFP_KERNEL);
203 if (!desc)
204 goto error;
c26fd69f 205
b426beb6
DH
206 desc->tfm = tfm;
207 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
c26fd69f
DH
208
209 ret = crypto_shash_init(desc);
210 if (ret < 0)
77d0910d 211 goto error_2;
b426beb6 212 might_sleep();
77d0910d
DH
213 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);
214
215error_2:
216 kfree(desc);
b426beb6
DH
217error:
218 crypto_free_shash(tfm);
219 pr_devel("<==%s() = %d\n", __func__, ret);
220 return ret;
221}
c26fd69f 222
b426beb6 223/*
6c2dc5ae
DH
224 * Check for self-signedness in an X.509 cert and if found, check the signature
225 * immediately if we can.
b426beb6 226 */
6c2dc5ae 227int x509_check_for_self_signed(struct x509_certificate *cert)
b426beb6 228{
6c2dc5ae 229 int ret = 0;
c26fd69f 230
b426beb6 231 pr_devel("==>%s()\n", __func__);
c26fd69f 232
6c2dc5ae
DH
233 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
234 /* If the AKID is present it may have one or two parts. If
235 * both are supplied, both must match.
236 */
237 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
238 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
239
240 if (!a && !b)
241 goto not_self_signed;
242
243 ret = -EKEYREJECTED;
244 if (((a && !b) || (b && !a)) &&
245 cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
246 goto out;
247 }
248
249 ret = public_key_verify_signature(cert->pub, cert->sig);
250 if (ret < 0) {
251 if (ret == -ENOPKG) {
252 cert->unsupported_sig = true;
253 ret = 0;
254 }
255 goto out;
256 }
257
258 pr_devel("Cert Self-signature verified");
259 cert->self_signed = true;
c26fd69f 260
6c2dc5ae
DH
261out:
262 pr_devel("<==%s() = %d\n", __func__, ret);
c26fd69f 263 return ret;
6c2dc5ae
DH
264
265not_self_signed:
266 pr_devel("<==%s() = 0 [not]\n", __func__);
267 return 0;
c26fd69f
DH
268}
269
3be4beaf
MZ
270/*
271 * Check the new certificate against the ones in the trust keyring. If one of
272 * those is the signing key and validates the new certificate, then mark the
273 * new certificate as being trusted.
274 *
275 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
276 * find a matching parent certificate in the trusted list and an error if there
277 * is a matching certificate but the signature check fails.
278 */
279static int x509_validate_trust(struct x509_certificate *cert,
280 struct key *trust_keyring)
281{
77d0910d 282 struct public_key_signature *sig = cert->sig;
3be4beaf
MZ
283 struct key *key;
284 int ret = 1;
285
6c2dc5ae
DH
286 if (!sig->auth_ids[0] && !sig->auth_ids[1])
287 return 1;
288
3be4beaf
MZ
289 if (!trust_keyring)
290 return -EOPNOTSUPP;
77d0910d 291 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
ffb70f61 292 return -EPERM;
6c2dc5ae
DH
293 if (cert->unsupported_sig)
294 return -ENOPKG;
ffb70f61 295
4573b64a 296 key = x509_request_asymmetric_key(trust_keyring,
77d0910d 297 sig->auth_ids[0], sig->auth_ids[1],
f1b731db 298 false);
6c2dc5ae
DH
299 if (IS_ERR(key))
300 return PTR_ERR(key);
301
302 if (!use_builtin_keys ||
303 test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
304 ret = public_key_verify_signature(
305 key->payload.data[asym_crypto], cert->sig);
306 if (ret == -ENOPKG)
307 cert->unsupported_sig = true;
3be4beaf 308 }
6c2dc5ae 309 key_put(key);
3be4beaf
MZ
310 return ret;
311}
312
c26fd69f
DH
313/*
314 * Attempt to parse a data blob for a key as an X509 certificate.
315 */
316static int x509_key_preparse(struct key_preparsed_payload *prep)
317{
46963b77 318 struct asymmetric_key_ids *kids;
c26fd69f 319 struct x509_certificate *cert;
46963b77 320 const char *q;
c26fd69f 321 size_t srlen, sulen;
46963b77 322 char *desc = NULL, *p;
c26fd69f
DH
323 int ret;
324
325 cert = x509_cert_parse(prep->data, prep->datalen);
326 if (IS_ERR(cert))
327 return PTR_ERR(cert);
328
329 pr_devel("Cert Issuer: %s\n", cert->issuer);
330 pr_devel("Cert Subject: %s\n", cert->subject);
2ecdb23b 331
6c2dc5ae 332 if (cert->unsupported_key) {
2ecdb23b
DH
333 ret = -ENOPKG;
334 goto error_free_cert;
335 }
336
4e8ae72a 337 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
fd19a3d1 338 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
c26fd69f 339
4e8ae72a 340 cert->pub->id_type = "X509";
c26fd69f 341
6c2dc5ae
DH
342 /* See if we can derive the trustability of this certificate.
343 *
344 * When it comes to self-signed certificates, we cannot evaluate
345 * trustedness except by the fact that we obtained it from a trusted
346 * location. So we just rely on x509_validate_trust() failing in this
347 * case.
348 *
349 * Note that there's a possibility of a self-signed cert matching a
350 * cert that we have (most likely a duplicate that we already trust) -
351 * in which case it will be marked trusted.
352 */
353 if (cert->unsupported_sig || cert->self_signed) {
354 public_key_signature_free(cert->sig);
355 cert->sig = NULL;
356 } else {
357 pr_devel("Cert Signature: %s + %s\n",
358 cert->sig->pkey_algo, cert->sig->hash_algo);
359
3be4beaf 360 ret = x509_validate_trust(cert, get_system_trusted_keyring());
41c89b64
PM
361 if (ret)
362 ret = x509_validate_trust(cert, get_ima_mok_keyring());
6c2dc5ae
DH
363 if (ret == -EKEYREJECTED)
364 goto error_free_cert;
3be4beaf 365 if (!ret)
6c2dc5ae 366 prep->trusted = true;
c26fd69f
DH
367 }
368
369 /* Propose a description */
370 sulen = strlen(cert->subject);
dd2f6c44
DH
371 if (cert->raw_skid) {
372 srlen = cert->raw_skid_size;
373 q = cert->raw_skid;
374 } else {
375 srlen = cert->raw_serial_size;
376 q = cert->raw_serial;
377 }
46963b77 378
c26fd69f 379 ret = -ENOMEM;
46963b77 380 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
c26fd69f
DH
381 if (!desc)
382 goto error_free_cert;
46963b77
DH
383 p = memcpy(desc, cert->subject, sulen);
384 p += sulen;
385 *p++ = ':';
386 *p++ = ' ';
387 p = bin2hex(p, q, srlen);
388 *p = 0;
389
390 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
391 if (!kids)
392 goto error_free_desc;
393 kids->id[0] = cert->id;
394 kids->id[1] = cert->skid;
c26fd69f
DH
395
396 /* We're pinning the module by being linked against it */
397 __module_get(public_key_subtype.owner);
146aa8b1
DH
398 prep->payload.data[asym_subtype] = &public_key_subtype;
399 prep->payload.data[asym_key_ids] = kids;
400 prep->payload.data[asym_crypto] = cert->pub;
77d0910d 401 prep->payload.data[asym_auth] = cert->sig;
c26fd69f
DH
402 prep->description = desc;
403 prep->quotalen = 100;
404
405 /* We've finished with the certificate */
406 cert->pub = NULL;
46963b77
DH
407 cert->id = NULL;
408 cert->skid = NULL;
77d0910d 409 cert->sig = NULL;
c26fd69f
DH
410 desc = NULL;
411 ret = 0;
412
46963b77
DH
413error_free_desc:
414 kfree(desc);
c26fd69f
DH
415error_free_cert:
416 x509_free_certificate(cert);
417 return ret;
418}
419
420static struct asymmetric_key_parser x509_key_parser = {
421 .owner = THIS_MODULE,
422 .name = "x509",
423 .parse = x509_key_preparse,
424};
425
426/*
427 * Module stuff
428 */
429static int __init x509_key_init(void)
430{
431 return register_asymmetric_key_parser(&x509_key_parser);
432}
433
434static void __exit x509_key_exit(void)
435{
436 unregister_asymmetric_key_parser(&x509_key_parser);
437}
438
439module_init(x509_key_init);
440module_exit(x509_key_exit);
e19aaa7d
KK
441
442MODULE_DESCRIPTION("X.509 certificate parser");
443MODULE_LICENSE("GPL");
This page took 0.129481 seconds and 5 git commands to generate.