blob: 24f17e6c590488d66ca01ec1f0e6f93a3f013163 [file] [log] [blame]
David Howellsc26fd692012-09-24 17:11:48 +01001/* 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 <linux/err.h>
17#include <linux/mpi.h>
18#include <linux/asn1_decoder.h>
19#include <keys/asymmetric-subtype.h>
20#include <keys/asymmetric-parser.h>
Mimi Zohar3be4bea2013-08-20 14:36:27 -040021#include <keys/system_keyring.h>
David Howellsc26fd692012-09-24 17:11:48 +010022#include <crypto/hash.h>
23#include "asymmetric_keys.h"
24#include "public_key.h"
25#include "x509_parser.h"
26
Dmitry Kasatkin32c47412014-06-17 11:56:59 +030027static bool use_builtin_keys;
David Howells46963b72014-09-16 17:36:13 +010028static struct asymmetric_key_id *ca_keyid;
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030029
30#ifndef MODULE
Mimi Zoharf2b3dee2015-02-11 07:33:34 -050031static struct {
32 struct asymmetric_key_id id;
33 unsigned char data[10];
34} cakey;
35
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030036static int __init ca_keys_setup(char *str)
37{
38 if (!str) /* default system keyring */
39 return 1;
40
David Howells46963b72014-09-16 17:36:13 +010041 if (strncmp(str, "id:", 3) == 0) {
Mimi Zoharf2b3dee2015-02-11 07:33:34 -050042 struct asymmetric_key_id *p = &cakey.id;
43 size_t hexlen = (strlen(str) - 3) / 2;
44 int ret;
45
46 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
47 pr_err("Missing or invalid ca_keys id\n");
48 return 1;
49 }
50
51 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
52 if (ret < 0)
53 pr_err("Unparsable ca_keys id hex string\n");
54 else
David Howells46963b72014-09-16 17:36:13 +010055 ca_keyid = p; /* owner key 'id:xxxxxx' */
56 } else if (strcmp(str, "builtin") == 0) {
Dmitry Kasatkin32c47412014-06-17 11:56:59 +030057 use_builtin_keys = true;
David Howells46963b72014-09-16 17:36:13 +010058 }
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030059
60 return 1;
61}
62__setup("ca_keys=", ca_keys_setup);
63#endif
64
David Howells5ce43ad2014-07-28 14:11:32 +010065/**
66 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
67 * @keyring: The keys to search.
David Howells46963b72014-09-16 17:36:13 +010068 * @kid: The key ID.
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010069 * @partial: Use partial match if true, exact if false.
David Howells5ce43ad2014-07-28 14:11:32 +010070 *
71 * Find a key in the given keyring by subject name and key ID. These might,
72 * for instance, be the issuer name and the authority key ID of an X.509
73 * certificate that needs to be verified.
Mimi Zohar3be4bea2013-08-20 14:36:27 -040074 */
David Howells5ce43ad2014-07-28 14:11:32 +010075struct key *x509_request_asymmetric_key(struct key *keyring,
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010076 const struct asymmetric_key_id *kid,
77 bool partial)
Mimi Zohar3be4bea2013-08-20 14:36:27 -040078{
79 key_ref_t key;
David Howells46963b72014-09-16 17:36:13 +010080 char *id, *p;
Mimi Zohar3be4bea2013-08-20 14:36:27 -040081
David Howells46963b72014-09-16 17:36:13 +010082 /* Construct an identifier "id:<keyid>". */
83 p = id = kmalloc(2 + 1 + kid->len * 2 + 1, GFP_KERNEL);
Mimi Zohar3be4bea2013-08-20 14:36:27 -040084 if (!id)
85 return ERR_PTR(-ENOMEM);
86
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +010087 if (partial) {
88 *p++ = 'i';
89 *p++ = 'd';
90 } else {
91 *p++ = 'e';
92 *p++ = 'x';
93 }
David Howells46963b72014-09-16 17:36:13 +010094 *p++ = ':';
95 p = bin2hex(p, kid->data, kid->len);
96 *p = 0;
Mimi Zohar3be4bea2013-08-20 14:36:27 -040097
98 pr_debug("Look up: \"%s\"\n", id);
99
100 key = keyring_search(make_key_ref(keyring, 1),
101 &key_type_asymmetric, id);
102 if (IS_ERR(key))
David Howells5ce43ad2014-07-28 14:11:32 +0100103 pr_debug("Request for key '%s' err %ld\n", id, PTR_ERR(key));
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400104 kfree(id);
105
106 if (IS_ERR(key)) {
107 switch (PTR_ERR(key)) {
108 /* Hide some search errors */
109 case -EACCES:
110 case -ENOTDIR:
111 case -EAGAIN:
112 return ERR_PTR(-ENOKEY);
113 default:
114 return ERR_CAST(key);
115 }
116 }
117
118 pr_devel("<==%s() = 0 [%x]\n", __func__,
119 key_serial(key_ref_to_ptr(key)));
120 return key_ref_to_ptr(key);
121}
David Howellscf5b50f2014-08-03 12:54:48 +0100122EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400123
David Howellsc26fd692012-09-24 17:11:48 +0100124/*
David Howellsb426beb2013-08-30 16:18:02 +0100125 * Set up the signature parameters in an X.509 certificate. This involves
126 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +0100127 */
David Howellsb426beb2013-08-30 16:18:02 +0100128int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +0100129{
David Howellsc26fd692012-09-24 17:11:48 +0100130 struct crypto_shash *tfm;
131 struct shash_desc *desc;
132 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +0100133 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +0100134 int ret;
135
136 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +0100137
David Howells41559422014-09-16 17:36:15 +0100138 if (cert->unsupported_crypto)
139 return -ENOPKG;
David Howellsb426beb2013-08-30 16:18:02 +0100140 if (cert->sig.rsa.s)
141 return 0;
142
143 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
144 if (!cert->sig.rsa.s)
145 return -ENOMEM;
146 cert->sig.nr_mpi = 1;
147
David Howellsc26fd692012-09-24 17:11:48 +0100148 /* Allocate the hashing algorithm we're going to need and find out how
149 * big the hash operational data will be.
150 */
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300151 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howells41559422014-09-16 17:36:15 +0100152 if (IS_ERR(tfm)) {
153 if (PTR_ERR(tfm) == -ENOENT) {
154 cert->unsupported_crypto = true;
155 return -ENOPKG;
156 }
157 return PTR_ERR(tfm);
158 }
David Howellsc26fd692012-09-24 17:11:48 +0100159
160 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
161 digest_size = crypto_shash_digestsize(tfm);
162
David Howellsb426beb2013-08-30 16:18:02 +0100163 /* We allocate the hash operational data storage on the end of the
164 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +0100165 */
166 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +0100167 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
168 if (!digest)
169 goto error;
David Howellsc26fd692012-09-24 17:11:48 +0100170
David Howellsb426beb2013-08-30 16:18:02 +0100171 cert->sig.digest = digest;
172 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +0100173
David Howellsb426beb2013-08-30 16:18:02 +0100174 desc = digest + digest_size;
175 desc->tfm = tfm;
176 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +0100177
178 ret = crypto_shash_init(desc);
179 if (ret < 0)
180 goto error;
David Howellsb426beb2013-08-30 16:18:02 +0100181 might_sleep();
182 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +0100183error:
David Howellsc26fd692012-09-24 17:11:48 +0100184 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100185 pr_devel("<==%s() = %d\n", __func__, ret);
186 return ret;
187}
David Howellsb426beb2013-08-30 16:18:02 +0100188EXPORT_SYMBOL_GPL(x509_get_sig_params);
189
190/*
191 * Check the signature on a certificate using the provided public key
192 */
193int x509_check_signature(const struct public_key *pub,
194 struct x509_certificate *cert)
195{
196 int ret;
197
198 pr_devel("==>%s()\n", __func__);
199
200 ret = x509_get_sig_params(cert);
201 if (ret < 0)
202 return ret;
203
204 ret = public_key_verify_signature(pub, &cert->sig);
David Howells41559422014-09-16 17:36:15 +0100205 if (ret == -ENOPKG)
206 cert->unsupported_crypto = true;
David Howellsb426beb2013-08-30 16:18:02 +0100207 pr_debug("Cert Verification: %d\n", ret);
208 return ret;
209}
210EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100211
212/*
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400213 * Check the new certificate against the ones in the trust keyring. If one of
214 * those is the signing key and validates the new certificate, then mark the
215 * new certificate as being trusted.
216 *
217 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
218 * find a matching parent certificate in the trusted list and an error if there
219 * is a matching certificate but the signature check fails.
220 */
221static int x509_validate_trust(struct x509_certificate *cert,
222 struct key *trust_keyring)
223{
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400224 struct key *key;
225 int ret = 1;
226
227 if (!trust_keyring)
228 return -EOPNOTSUPP;
229
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100230 if (ca_keyid && !asymmetric_key_id_partial(cert->authority, ca_keyid))
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +0300231 return -EPERM;
232
Dmitry Kasatkinf1b731d2014-10-06 15:21:05 +0100233 key = x509_request_asymmetric_key(trust_keyring, cert->authority,
234 false);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400235 if (!IS_ERR(key)) {
Dmitry Kasatkin32c47412014-06-17 11:56:59 +0300236 if (!use_builtin_keys
237 || test_bit(KEY_FLAG_BUILTIN, &key->flags))
238 ret = x509_check_signature(key->payload.data, cert);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400239 key_put(key);
240 }
241 return ret;
242}
243
244/*
David Howellsc26fd692012-09-24 17:11:48 +0100245 * Attempt to parse a data blob for a key as an X509 certificate.
246 */
247static int x509_key_preparse(struct key_preparsed_payload *prep)
248{
David Howells46963b72014-09-16 17:36:13 +0100249 struct asymmetric_key_ids *kids;
David Howellsc26fd692012-09-24 17:11:48 +0100250 struct x509_certificate *cert;
David Howells46963b72014-09-16 17:36:13 +0100251 const char *q;
David Howellsc26fd692012-09-24 17:11:48 +0100252 size_t srlen, sulen;
David Howells46963b72014-09-16 17:36:13 +0100253 char *desc = NULL, *p;
David Howellsc26fd692012-09-24 17:11:48 +0100254 int ret;
255
256 cert = x509_cert_parse(prep->data, prep->datalen);
257 if (IS_ERR(cert))
258 return PTR_ERR(cert);
259
260 pr_devel("Cert Issuer: %s\n", cert->issuer);
261 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100262
263 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
264 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
265 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
266 !pkey_algo[cert->pub->pkey_algo] ||
267 !pkey_algo[cert->sig.pkey_algo] ||
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300268 !hash_algo_name[cert->sig.pkey_hash_algo]) {
David Howells2ecdb232013-08-30 16:18:15 +0100269 ret = -ENOPKG;
270 goto error_free_cert;
271 }
272
David Howells67f7d60b2013-08-30 16:15:24 +0100273 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100274 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100275 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
276 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
277 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100278 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100279 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
280 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
281 cert->valid_to.tm_min, cert->valid_to.tm_sec);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300282 pr_devel("Cert Signature: %s + %s\n",
283 pkey_algo_name[cert->sig.pkey_algo],
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300284 hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100285
David Howells67f7d60b2013-08-30 16:15:24 +0100286 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100287 cert->pub->id_type = PKEY_ID_X509;
288
David Howells17334ca2013-08-30 16:18:31 +0100289 /* Check the signature on the key if it appears to be self-signed */
290 if (!cert->authority ||
David Howells46963b72014-09-16 17:36:13 +0100291 asymmetric_key_id_same(cert->skid, cert->authority)) {
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400292 ret = x509_check_signature(cert->pub, cert); /* self-signed */
David Howellsc26fd692012-09-24 17:11:48 +0100293 if (ret < 0)
294 goto error_free_cert;
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400295 } else if (!prep->trusted) {
296 ret = x509_validate_trust(cert, get_system_trusted_keyring());
297 if (!ret)
298 prep->trusted = 1;
David Howellsc26fd692012-09-24 17:11:48 +0100299 }
300
301 /* Propose a description */
302 sulen = strlen(cert->subject);
David Howellsdd2f6c42014-10-03 16:17:02 +0100303 if (cert->raw_skid) {
304 srlen = cert->raw_skid_size;
305 q = cert->raw_skid;
306 } else {
307 srlen = cert->raw_serial_size;
308 q = cert->raw_serial;
309 }
David Howells46963b72014-09-16 17:36:13 +0100310 if (srlen > 1 && *q == 0) {
311 srlen--;
312 q++;
313 }
314
David Howellsc26fd692012-09-24 17:11:48 +0100315 ret = -ENOMEM;
David Howells46963b72014-09-16 17:36:13 +0100316 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
David Howellsc26fd692012-09-24 17:11:48 +0100317 if (!desc)
318 goto error_free_cert;
David Howells46963b72014-09-16 17:36:13 +0100319 p = memcpy(desc, cert->subject, sulen);
320 p += sulen;
321 *p++ = ':';
322 *p++ = ' ';
323 p = bin2hex(p, q, srlen);
324 *p = 0;
325
326 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
327 if (!kids)
328 goto error_free_desc;
329 kids->id[0] = cert->id;
330 kids->id[1] = cert->skid;
David Howellsc26fd692012-09-24 17:11:48 +0100331
332 /* We're pinning the module by being linked against it */
333 __module_get(public_key_subtype.owner);
334 prep->type_data[0] = &public_key_subtype;
David Howells46963b72014-09-16 17:36:13 +0100335 prep->type_data[1] = kids;
David Howellsfc7c70e2014-07-18 18:56:34 +0100336 prep->payload[0] = cert->pub;
David Howellsc26fd692012-09-24 17:11:48 +0100337 prep->description = desc;
338 prep->quotalen = 100;
339
340 /* We've finished with the certificate */
341 cert->pub = NULL;
David Howells46963b72014-09-16 17:36:13 +0100342 cert->id = NULL;
343 cert->skid = NULL;
David Howellsc26fd692012-09-24 17:11:48 +0100344 desc = NULL;
345 ret = 0;
346
David Howells46963b72014-09-16 17:36:13 +0100347error_free_desc:
348 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +0100349error_free_cert:
350 x509_free_certificate(cert);
351 return ret;
352}
353
354static struct asymmetric_key_parser x509_key_parser = {
355 .owner = THIS_MODULE,
356 .name = "x509",
357 .parse = x509_key_preparse,
358};
359
360/*
361 * Module stuff
362 */
363static int __init x509_key_init(void)
364{
365 return register_asymmetric_key_parser(&x509_key_parser);
366}
367
368static void __exit x509_key_exit(void)
369{
370 unregister_asymmetric_key_parser(&x509_key_parser);
371}
372
373module_init(x509_key_init);
374module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400375
376MODULE_DESCRIPTION("X.509 certificate parser");
377MODULE_LICENSE("GPL");