blob: da1e5fc85346686807c90f275782b6920ab21957 [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;
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030028static char *ca_keyid;
29
30#ifndef MODULE
31static int __init ca_keys_setup(char *str)
32{
33 if (!str) /* default system keyring */
34 return 1;
35
36 if (strncmp(str, "id:", 3) == 0)
37 ca_keyid = str; /* owner key 'id:xxxxxx' */
Dmitry Kasatkin32c47412014-06-17 11:56:59 +030038 else if (strcmp(str, "builtin") == 0)
39 use_builtin_keys = true;
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +030040
41 return 1;
42}
43__setup("ca_keys=", ca_keys_setup);
44#endif
45
David Howells5ce43ad2014-07-28 14:11:32 +010046/**
47 * x509_request_asymmetric_key - Request a key by X.509 certificate params.
48 * @keyring: The keys to search.
49 * @subject: The name of the subject to whom the key belongs.
50 * @key_id: The subject key ID as a hex string.
51 *
52 * Find a key in the given keyring by subject name and key ID. These might,
53 * for instance, be the issuer name and the authority key ID of an X.509
54 * certificate that needs to be verified.
Mimi Zohar3be4bea2013-08-20 14:36:27 -040055 */
David Howells5ce43ad2014-07-28 14:11:32 +010056struct key *x509_request_asymmetric_key(struct key *keyring,
57 const char *subject,
58 const char *key_id)
Mimi Zohar3be4bea2013-08-20 14:36:27 -040059{
60 key_ref_t key;
David Howells5ce43ad2014-07-28 14:11:32 +010061 size_t subject_len = strlen(subject), key_id_len = strlen(key_id);
Mimi Zohar3be4bea2013-08-20 14:36:27 -040062 char *id;
63
David Howells5ce43ad2014-07-28 14:11:32 +010064 /* Construct an identifier "<subjname>:<keyid>". */
65 id = kmalloc(subject_len + 2 + key_id_len + 1, GFP_KERNEL);
Mimi Zohar3be4bea2013-08-20 14:36:27 -040066 if (!id)
67 return ERR_PTR(-ENOMEM);
68
David Howells5ce43ad2014-07-28 14:11:32 +010069 memcpy(id, subject, subject_len);
70 id[subject_len + 0] = ':';
71 id[subject_len + 1] = ' ';
72 memcpy(id + subject_len + 2, key_id, key_id_len);
73 id[subject_len + 2 + key_id_len] = 0;
Mimi Zohar3be4bea2013-08-20 14:36:27 -040074
75 pr_debug("Look up: \"%s\"\n", id);
76
77 key = keyring_search(make_key_ref(keyring, 1),
78 &key_type_asymmetric, id);
79 if (IS_ERR(key))
David Howells5ce43ad2014-07-28 14:11:32 +010080 pr_debug("Request for key '%s' err %ld\n", id, PTR_ERR(key));
Mimi Zohar3be4bea2013-08-20 14:36:27 -040081 kfree(id);
82
83 if (IS_ERR(key)) {
84 switch (PTR_ERR(key)) {
85 /* Hide some search errors */
86 case -EACCES:
87 case -ENOTDIR:
88 case -EAGAIN:
89 return ERR_PTR(-ENOKEY);
90 default:
91 return ERR_CAST(key);
92 }
93 }
94
95 pr_devel("<==%s() = 0 [%x]\n", __func__,
96 key_serial(key_ref_to_ptr(key)));
97 return key_ref_to_ptr(key);
98}
99
David Howellsc26fd692012-09-24 17:11:48 +0100100/*
David Howellsb426beb2013-08-30 16:18:02 +0100101 * Set up the signature parameters in an X.509 certificate. This involves
102 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +0100103 */
David Howellsb426beb2013-08-30 16:18:02 +0100104int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +0100105{
David Howellsc26fd692012-09-24 17:11:48 +0100106 struct crypto_shash *tfm;
107 struct shash_desc *desc;
108 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +0100109 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +0100110 int ret;
111
112 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +0100113
114 if (cert->sig.rsa.s)
115 return 0;
116
117 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
118 if (!cert->sig.rsa.s)
119 return -ENOMEM;
120 cert->sig.nr_mpi = 1;
121
David Howellsc26fd692012-09-24 17:11:48 +0100122 /* Allocate the hashing algorithm we're going to need and find out how
123 * big the hash operational data will be.
124 */
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300125 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howellsc26fd692012-09-24 17:11:48 +0100126 if (IS_ERR(tfm))
127 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
128
129 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
130 digest_size = crypto_shash_digestsize(tfm);
131
David Howellsb426beb2013-08-30 16:18:02 +0100132 /* We allocate the hash operational data storage on the end of the
133 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +0100134 */
135 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +0100136 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
137 if (!digest)
138 goto error;
David Howellsc26fd692012-09-24 17:11:48 +0100139
David Howellsb426beb2013-08-30 16:18:02 +0100140 cert->sig.digest = digest;
141 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +0100142
David Howellsb426beb2013-08-30 16:18:02 +0100143 desc = digest + digest_size;
144 desc->tfm = tfm;
145 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +0100146
147 ret = crypto_shash_init(desc);
148 if (ret < 0)
149 goto error;
David Howellsb426beb2013-08-30 16:18:02 +0100150 might_sleep();
151 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +0100152error:
David Howellsc26fd692012-09-24 17:11:48 +0100153 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100154 pr_devel("<==%s() = %d\n", __func__, ret);
155 return ret;
156}
David Howellsb426beb2013-08-30 16:18:02 +0100157EXPORT_SYMBOL_GPL(x509_get_sig_params);
158
159/*
160 * Check the signature on a certificate using the provided public key
161 */
162int x509_check_signature(const struct public_key *pub,
163 struct x509_certificate *cert)
164{
165 int ret;
166
167 pr_devel("==>%s()\n", __func__);
168
169 ret = x509_get_sig_params(cert);
170 if (ret < 0)
171 return ret;
172
173 ret = public_key_verify_signature(pub, &cert->sig);
174 pr_debug("Cert Verification: %d\n", ret);
175 return ret;
176}
177EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100178
179/*
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400180 * Check the new certificate against the ones in the trust keyring. If one of
181 * those is the signing key and validates the new certificate, then mark the
182 * new certificate as being trusted.
183 *
184 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
185 * find a matching parent certificate in the trusted list and an error if there
186 * is a matching certificate but the signature check fails.
187 */
188static int x509_validate_trust(struct x509_certificate *cert,
189 struct key *trust_keyring)
190{
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400191 struct key *key;
192 int ret = 1;
193
194 if (!trust_keyring)
195 return -EOPNOTSUPP;
196
Dmitry Kasatkinffb70f62014-06-17 11:56:58 +0300197 if (ca_keyid && !asymmetric_keyid_match(cert->authority, ca_keyid))
198 return -EPERM;
199
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400200 key = x509_request_asymmetric_key(trust_keyring,
David Howells185de092014-07-09 16:48:00 +0100201 cert->issuer, cert->authority);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400202 if (!IS_ERR(key)) {
Dmitry Kasatkin32c47412014-06-17 11:56:59 +0300203 if (!use_builtin_keys
204 || test_bit(KEY_FLAG_BUILTIN, &key->flags))
205 ret = x509_check_signature(key->payload.data, cert);
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400206 key_put(key);
207 }
208 return ret;
209}
210
211/*
David Howellsc26fd692012-09-24 17:11:48 +0100212 * Attempt to parse a data blob for a key as an X509 certificate.
213 */
214static int x509_key_preparse(struct key_preparsed_payload *prep)
215{
216 struct x509_certificate *cert;
David Howellsc26fd692012-09-24 17:11:48 +0100217 size_t srlen, sulen;
218 char *desc = NULL;
219 int ret;
220
221 cert = x509_cert_parse(prep->data, prep->datalen);
222 if (IS_ERR(cert))
223 return PTR_ERR(cert);
224
225 pr_devel("Cert Issuer: %s\n", cert->issuer);
226 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100227
228 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
229 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
230 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
231 !pkey_algo[cert->pub->pkey_algo] ||
232 !pkey_algo[cert->sig.pkey_algo] ||
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300233 !hash_algo_name[cert->sig.pkey_hash_algo]) {
David Howells2ecdb232013-08-30 16:18:15 +0100234 ret = -ENOPKG;
235 goto error_free_cert;
236 }
237
David Howells67f7d60b2013-08-30 16:15:24 +0100238 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100239 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100240 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
241 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
242 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100243 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100244 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
245 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
246 cert->valid_to.tm_min, cert->valid_to.tm_sec);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300247 pr_devel("Cert Signature: %s + %s\n",
248 pkey_algo_name[cert->sig.pkey_algo],
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300249 hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100250
David Howells17334ca2013-08-30 16:18:31 +0100251 if (!cert->fingerprint) {
252 pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
David Howellsc26fd692012-09-24 17:11:48 +0100253 cert->subject);
254 ret = -EKEYREJECTED;
255 goto error_free_cert;
256 }
257
David Howells67f7d60b2013-08-30 16:15:24 +0100258 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100259 cert->pub->id_type = PKEY_ID_X509;
260
David Howells17334ca2013-08-30 16:18:31 +0100261 /* Check the signature on the key if it appears to be self-signed */
262 if (!cert->authority ||
263 strcmp(cert->fingerprint, cert->authority) == 0) {
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400264 ret = x509_check_signature(cert->pub, cert); /* self-signed */
David Howellsc26fd692012-09-24 17:11:48 +0100265 if (ret < 0)
266 goto error_free_cert;
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400267 } else if (!prep->trusted) {
268 ret = x509_validate_trust(cert, get_system_trusted_keyring());
269 if (!ret)
270 prep->trusted = 1;
David Howellsc26fd692012-09-24 17:11:48 +0100271 }
272
273 /* Propose a description */
274 sulen = strlen(cert->subject);
275 srlen = strlen(cert->fingerprint);
276 ret = -ENOMEM;
277 desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
278 if (!desc)
279 goto error_free_cert;
280 memcpy(desc, cert->subject, sulen);
281 desc[sulen] = ':';
282 desc[sulen + 1] = ' ';
283 memcpy(desc + sulen + 2, cert->fingerprint, srlen);
284 desc[sulen + 2 + srlen] = 0;
285
286 /* We're pinning the module by being linked against it */
287 __module_get(public_key_subtype.owner);
288 prep->type_data[0] = &public_key_subtype;
289 prep->type_data[1] = cert->fingerprint;
David Howellsfc7c70e2014-07-18 18:56:34 +0100290 prep->payload[0] = cert->pub;
David Howellsc26fd692012-09-24 17:11:48 +0100291 prep->description = desc;
292 prep->quotalen = 100;
293
294 /* We've finished with the certificate */
295 cert->pub = NULL;
296 cert->fingerprint = NULL;
297 desc = NULL;
298 ret = 0;
299
300error_free_cert:
301 x509_free_certificate(cert);
302 return ret;
303}
304
305static struct asymmetric_key_parser x509_key_parser = {
306 .owner = THIS_MODULE,
307 .name = "x509",
308 .parse = x509_key_preparse,
309};
310
311/*
312 * Module stuff
313 */
314static int __init x509_key_init(void)
315{
316 return register_asymmetric_key_parser(&x509_key_parser);
317}
318
319static void __exit x509_key_exit(void)
320{
321 unregister_asymmetric_key_parser(&x509_key_parser);
322}
323
324module_init(x509_key_init);
325module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400326
327MODULE_DESCRIPTION("X.509 certificate parser");
328MODULE_LICENSE("GPL");