blob: c60905c3f4d2c4842e59a56e24c63a0715b6bc1d [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
31static int __init ca_keys_setup(char *str)
32{
33 if (!str) /* default system keyring */
34 return 1;
35
David Howells46963b72014-09-16 17:36:13 +010036 if (strncmp(str, "id:", 3) == 0) {
37 struct asymmetric_key_id *p;
38 p = asymmetric_key_hex_to_key_id(str);
39 if (p == ERR_PTR(-EINVAL))
40 pr_err("Unparsable hex string in ca_keys\n");
41 else if (!IS_ERR(p))
42 ca_keyid = p; /* owner key 'id:xxxxxx' */
43 } else if (strcmp(str, "builtin") == 0) {
Dmitry Kasatkin32c47412014-06-17 11:56:59 +030044 use_builtin_keys = true;
David Howells46963b72014-09-16 17:36:13 +010045 }
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030046
47 return 1;
48}
49__setup("ca_keys=", ca_keys_setup);
50#endif
51
David Howells5ce43ad2014-07-28 14:11:32 +010052/**
53 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
54 * @keyring: The keys to search.
David Howells46963b72014-09-16 17:36:13 +010055 * @kid: The key ID.
David Howells5ce43ad2014-07-28 14:11:32 +010056 *
57 * Find a key in the given keyring by subject name and key ID. These might,
58 * for instance, be the issuer name and the authority key ID of an X.509
59 * certificate that needs to be verified.
Mimi Zohar3be4bea2013-08-20 14:36:27 -040060 */
David Howells5ce43ad2014-07-28 14:11:32 +010061struct key *x509_request_asymmetric_key(struct key *keyring,
David Howells46963b72014-09-16 17:36:13 +010062 const struct asymmetric_key_id *kid)
Mimi Zohar3be4bea2013-08-20 14:36:27 -040063{
64 key_ref_t key;
David Howells46963b72014-09-16 17:36:13 +010065 char *id, *p;
Mimi Zohar3be4bea2013-08-20 14:36:27 -040066
David Howells46963b72014-09-16 17:36:13 +010067 /* Construct an identifier "id:<keyid>". */
68 p = id = kmalloc(2 + 1 + kid->len * 2 + 1, GFP_KERNEL);
Mimi Zohar3be4bea2013-08-20 14:36:27 -040069 if (!id)
70 return ERR_PTR(-ENOMEM);
71
David Howells46963b72014-09-16 17:36:13 +010072 *p++ = 'i';
73 *p++ = 'd';
74 *p++ = ':';
75 p = bin2hex(p, kid->data, kid->len);
76 *p = 0;
Mimi Zohar3be4bea2013-08-20 14:36:27 -040077
78 pr_debug("Look up: \"%s\"\n", id);
79
80 key = keyring_search(make_key_ref(keyring, 1),
81 &key_type_asymmetric, id);
82 if (IS_ERR(key))
David Howells5ce43ad2014-07-28 14:11:32 +010083 pr_debug("Request for key '%s' err %ld\n", id, PTR_ERR(key));
Mimi Zohar3be4bea2013-08-20 14:36:27 -040084 kfree(id);
85
86 if (IS_ERR(key)) {
87 switch (PTR_ERR(key)) {
88 /* Hide some search errors */
89 case -EACCES:
90 case -ENOTDIR:
91 case -EAGAIN:
92 return ERR_PTR(-ENOKEY);
93 default:
94 return ERR_CAST(key);
95 }
96 }
97
98 pr_devel("<==%s() = 0 [%x]\n", __func__,
99 key_serial(key_ref_to_ptr(key)));
100 return key_ref_to_ptr(key);
101}
David Howellscf5b50f2014-08-03 12:54:48 +0100102EXPORT_SYMBOL_GPL(x509_request_asymmetric_key);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400103
David Howellsc26fd692012-09-24 17:11:48 +0100104/*
David Howellsb426beb2013-08-30 16:18:02 +0100105 * Set up the signature parameters in an X.509 certificate. This involves
106 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +0100107 */
David Howellsb426beb2013-08-30 16:18:02 +0100108int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +0100109{
David Howellsc26fd692012-09-24 17:11:48 +0100110 struct crypto_shash *tfm;
111 struct shash_desc *desc;
112 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +0100113 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +0100114 int ret;
115
116 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +0100117
118 if (cert->sig.rsa.s)
119 return 0;
120
121 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
122 if (!cert->sig.rsa.s)
123 return -ENOMEM;
124 cert->sig.nr_mpi = 1;
125
David Howellsc26fd692012-09-24 17:11:48 +0100126 /* Allocate the hashing algorithm we're going to need and find out how
127 * big the hash operational data will be.
128 */
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300129 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howellsc26fd692012-09-24 17:11:48 +0100130 if (IS_ERR(tfm))
131 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
132
133 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
134 digest_size = crypto_shash_digestsize(tfm);
135
David Howellsb426beb2013-08-30 16:18:02 +0100136 /* We allocate the hash operational data storage on the end of the
137 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +0100138 */
139 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +0100140 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
141 if (!digest)
142 goto error;
David Howellsc26fd692012-09-24 17:11:48 +0100143
David Howellsb426beb2013-08-30 16:18:02 +0100144 cert->sig.digest = digest;
145 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +0100146
David Howellsb426beb2013-08-30 16:18:02 +0100147 desc = digest + digest_size;
148 desc->tfm = tfm;
149 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +0100150
151 ret = crypto_shash_init(desc);
152 if (ret < 0)
153 goto error;
David Howellsb426beb2013-08-30 16:18:02 +0100154 might_sleep();
155 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +0100156error:
David Howellsc26fd692012-09-24 17:11:48 +0100157 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100158 pr_devel("<==%s() = %d\n", __func__, ret);
159 return ret;
160}
David Howellsb426beb2013-08-30 16:18:02 +0100161EXPORT_SYMBOL_GPL(x509_get_sig_params);
162
163/*
164 * Check the signature on a certificate using the provided public key
165 */
166int x509_check_signature(const struct public_key *pub,
167 struct x509_certificate *cert)
168{
169 int ret;
170
171 pr_devel("==>%s()\n", __func__);
172
173 ret = x509_get_sig_params(cert);
174 if (ret < 0)
175 return ret;
176
177 ret = public_key_verify_signature(pub, &cert->sig);
178 pr_debug("Cert Verification: %d\n", ret);
179 return ret;
180}
181EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100182
183/*
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400184 * Check the new certificate against the ones in the trust keyring. If one of
185 * those is the signing key and validates the new certificate, then mark the
186 * new certificate as being trusted.
187 *
188 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
189 * find a matching parent certificate in the trusted list and an error if there
190 * is a matching certificate but the signature check fails.
191 */
192static int x509_validate_trust(struct x509_certificate *cert,
193 struct key *trust_keyring)
194{
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400195 struct key *key;
196 int ret = 1;
197
198 if (!trust_keyring)
199 return -EOPNOTSUPP;
200
David Howells46963b72014-09-16 17:36:13 +0100201 if (ca_keyid && !asymmetric_key_id_same(cert->authority, ca_keyid))
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +0300202 return -EPERM;
203
David Howells46963b72014-09-16 17:36:13 +0100204 key = x509_request_asymmetric_key(trust_keyring, cert->authority);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400205 if (!IS_ERR(key)) {
Dmitry Kasatkin32c47412014-06-17 11:56:59 +0300206 if (!use_builtin_keys
207 || test_bit(KEY_FLAG_BUILTIN, &key->flags))
208 ret = x509_check_signature(key->payload.data, cert);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400209 key_put(key);
210 }
211 return ret;
212}
213
214/*
David Howellsc26fd692012-09-24 17:11:48 +0100215 * Attempt to parse a data blob for a key as an X509 certificate.
216 */
217static int x509_key_preparse(struct key_preparsed_payload *prep)
218{
David Howells46963b72014-09-16 17:36:13 +0100219 struct asymmetric_key_ids *kids;
David Howellsc26fd692012-09-24 17:11:48 +0100220 struct x509_certificate *cert;
David Howells46963b72014-09-16 17:36:13 +0100221 const char *q;
David Howellsc26fd692012-09-24 17:11:48 +0100222 size_t srlen, sulen;
David Howells46963b72014-09-16 17:36:13 +0100223 char *desc = NULL, *p;
David Howellsc26fd692012-09-24 17:11:48 +0100224 int ret;
225
226 cert = x509_cert_parse(prep->data, prep->datalen);
227 if (IS_ERR(cert))
228 return PTR_ERR(cert);
229
230 pr_devel("Cert Issuer: %s\n", cert->issuer);
231 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100232
233 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
234 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
235 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
236 !pkey_algo[cert->pub->pkey_algo] ||
237 !pkey_algo[cert->sig.pkey_algo] ||
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300238 !hash_algo_name[cert->sig.pkey_hash_algo]) {
David Howells2ecdb232013-08-30 16:18:15 +0100239 ret = -ENOPKG;
240 goto error_free_cert;
241 }
242
David Howells67f7d60b2013-08-30 16:15:24 +0100243 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100244 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100245 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
246 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
247 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100248 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100249 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
250 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
251 cert->valid_to.tm_min, cert->valid_to.tm_sec);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300252 pr_devel("Cert Signature: %s + %s\n",
253 pkey_algo_name[cert->sig.pkey_algo],
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300254 hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100255
David Howells67f7d60b2013-08-30 16:15:24 +0100256 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100257 cert->pub->id_type = PKEY_ID_X509;
258
David Howells17334ca2013-08-30 16:18:31 +0100259 /* Check the signature on the key if it appears to be self-signed */
260 if (!cert->authority ||
David Howells46963b72014-09-16 17:36:13 +0100261 asymmetric_key_id_same(cert->skid, cert->authority)) {
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400262 ret = x509_check_signature(cert->pub, cert); /* self-signed */
David Howellsc26fd692012-09-24 17:11:48 +0100263 if (ret < 0)
264 goto error_free_cert;
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400265 } else if (!prep->trusted) {
266 ret = x509_validate_trust(cert, get_system_trusted_keyring());
267 if (!ret)
268 prep->trusted = 1;
David Howellsc26fd692012-09-24 17:11:48 +0100269 }
270
271 /* Propose a description */
272 sulen = strlen(cert->subject);
David Howells46963b72014-09-16 17:36:13 +0100273 srlen = cert->raw_serial_size;
274 q = cert->raw_serial;
275 if (srlen > 1 && *q == 0) {
276 srlen--;
277 q++;
278 }
279
David Howellsc26fd692012-09-24 17:11:48 +0100280 ret = -ENOMEM;
David Howells46963b72014-09-16 17:36:13 +0100281 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
David Howellsc26fd692012-09-24 17:11:48 +0100282 if (!desc)
283 goto error_free_cert;
David Howells46963b72014-09-16 17:36:13 +0100284 p = memcpy(desc, cert->subject, sulen);
285 p += sulen;
286 *p++ = ':';
287 *p++ = ' ';
288 p = bin2hex(p, q, srlen);
289 *p = 0;
290
291 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
292 if (!kids)
293 goto error_free_desc;
294 kids->id[0] = cert->id;
295 kids->id[1] = cert->skid;
David Howellsc26fd692012-09-24 17:11:48 +0100296
297 /* We're pinning the module by being linked against it */
298 __module_get(public_key_subtype.owner);
299 prep->type_data[0] = &public_key_subtype;
David Howells46963b72014-09-16 17:36:13 +0100300 prep->type_data[1] = kids;
David Howellsfc7c70e2014-07-18 18:56:34 +0100301 prep->payload[0] = cert->pub;
David Howellsc26fd692012-09-24 17:11:48 +0100302 prep->description = desc;
303 prep->quotalen = 100;
304
305 /* We've finished with the certificate */
306 cert->pub = NULL;
David Howells46963b72014-09-16 17:36:13 +0100307 cert->id = NULL;
308 cert->skid = NULL;
David Howellsc26fd692012-09-24 17:11:48 +0100309 desc = NULL;
310 ret = 0;
311
David Howells46963b72014-09-16 17:36:13 +0100312error_free_desc:
313 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +0100314error_free_cert:
315 x509_free_certificate(cert);
316 return ret;
317}
318
319static struct asymmetric_key_parser x509_key_parser = {
320 .owner = THIS_MODULE,
321 .name = "x509",
322 .parse = x509_key_preparse,
323};
324
325/*
326 * Module stuff
327 */
328static int __init x509_key_init(void)
329{
330 return register_asymmetric_key_parser(&x509_key_parser);
331}
332
333static void __exit x509_key_exit(void)
334{
335 unregister_asymmetric_key_parser(&x509_key_parser);
336}
337
338module_init(x509_key_init);
339module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400340
341MODULE_DESCRIPTION("X.509 certificate parser");
342MODULE_LICENSE("GPL");