blob: 436fbd8552fc4c06ffc8b1464bb1b3fa18e2d253 [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
David Howellsc26fd692012-09-24 17:11:48 +010027/*
Mimi Zohar3be4bea2013-08-20 14:36:27 -040028 * Find a key in the given keyring by issuer and authority.
29 */
30static struct key *x509_request_asymmetric_key(struct key *keyring,
31 const char *signer,
32 size_t signer_len,
33 const char *authority,
34 size_t auth_len)
35{
36 key_ref_t key;
37 char *id;
38
39 /* Construct an identifier. */
40 id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
41 if (!id)
42 return ERR_PTR(-ENOMEM);
43
44 memcpy(id, signer, signer_len);
45 id[signer_len + 0] = ':';
46 id[signer_len + 1] = ' ';
47 memcpy(id + signer_len + 2, authority, auth_len);
48 id[signer_len + 2 + auth_len] = 0;
49
50 pr_debug("Look up: \"%s\"\n", id);
51
52 key = keyring_search(make_key_ref(keyring, 1),
53 &key_type_asymmetric, id);
54 if (IS_ERR(key))
55 pr_debug("Request for module key '%s' err %ld\n",
56 id, PTR_ERR(key));
57 kfree(id);
58
59 if (IS_ERR(key)) {
60 switch (PTR_ERR(key)) {
61 /* Hide some search errors */
62 case -EACCES:
63 case -ENOTDIR:
64 case -EAGAIN:
65 return ERR_PTR(-ENOKEY);
66 default:
67 return ERR_CAST(key);
68 }
69 }
70
71 pr_devel("<==%s() = 0 [%x]\n", __func__,
72 key_serial(key_ref_to_ptr(key)));
73 return key_ref_to_ptr(key);
74}
75
76/*
David Howellsb426beb2013-08-30 16:18:02 +010077 * Set up the signature parameters in an X.509 certificate. This involves
78 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +010079 */
David Howellsb426beb2013-08-30 16:18:02 +010080int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +010081{
David Howellsc26fd692012-09-24 17:11:48 +010082 struct crypto_shash *tfm;
83 struct shash_desc *desc;
84 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +010085 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +010086 int ret;
87
88 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +010089
90 if (cert->sig.rsa.s)
91 return 0;
92
93 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
94 if (!cert->sig.rsa.s)
95 return -ENOMEM;
96 cert->sig.nr_mpi = 1;
97
David Howellsc26fd692012-09-24 17:11:48 +010098 /* Allocate the hashing algorithm we're going to need and find out how
99 * big the hash operational data will be.
100 */
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300101 tfm = crypto_alloc_shash(hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howellsc26fd692012-09-24 17:11:48 +0100102 if (IS_ERR(tfm))
103 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
104
105 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
106 digest_size = crypto_shash_digestsize(tfm);
107
David Howellsb426beb2013-08-30 16:18:02 +0100108 /* We allocate the hash operational data storage on the end of the
109 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +0100110 */
111 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +0100112 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
113 if (!digest)
114 goto error;
David Howellsc26fd692012-09-24 17:11:48 +0100115
David Howellsb426beb2013-08-30 16:18:02 +0100116 cert->sig.digest = digest;
117 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +0100118
David Howellsb426beb2013-08-30 16:18:02 +0100119 desc = digest + digest_size;
120 desc->tfm = tfm;
121 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +0100122
123 ret = crypto_shash_init(desc);
124 if (ret < 0)
125 goto error;
David Howellsb426beb2013-08-30 16:18:02 +0100126 might_sleep();
127 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +0100128error:
David Howellsc26fd692012-09-24 17:11:48 +0100129 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100130 pr_devel("<==%s() = %d\n", __func__, ret);
131 return ret;
132}
David Howellsb426beb2013-08-30 16:18:02 +0100133EXPORT_SYMBOL_GPL(x509_get_sig_params);
134
135/*
136 * Check the signature on a certificate using the provided public key
137 */
138int x509_check_signature(const struct public_key *pub,
139 struct x509_certificate *cert)
140{
141 int ret;
142
143 pr_devel("==>%s()\n", __func__);
144
145 ret = x509_get_sig_params(cert);
146 if (ret < 0)
147 return ret;
148
149 ret = public_key_verify_signature(pub, &cert->sig);
150 pr_debug("Cert Verification: %d\n", ret);
151 return ret;
152}
153EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100154
155/*
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400156 * Check the new certificate against the ones in the trust keyring. If one of
157 * those is the signing key and validates the new certificate, then mark the
158 * new certificate as being trusted.
159 *
160 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
161 * find a matching parent certificate in the trusted list and an error if there
162 * is a matching certificate but the signature check fails.
163 */
164static int x509_validate_trust(struct x509_certificate *cert,
165 struct key *trust_keyring)
166{
167 const struct public_key *pk;
168 struct key *key;
169 int ret = 1;
170
171 if (!trust_keyring)
172 return -EOPNOTSUPP;
173
174 key = x509_request_asymmetric_key(trust_keyring,
175 cert->issuer, strlen(cert->issuer),
176 cert->authority,
177 strlen(cert->authority));
178 if (!IS_ERR(key)) {
179 pk = key->payload.data;
180 ret = x509_check_signature(pk, cert);
181 key_put(key);
182 }
183 return ret;
184}
185
186/*
David Howellsc26fd692012-09-24 17:11:48 +0100187 * Attempt to parse a data blob for a key as an X509 certificate.
188 */
189static int x509_key_preparse(struct key_preparsed_payload *prep)
190{
191 struct x509_certificate *cert;
David Howellsc26fd692012-09-24 17:11:48 +0100192 size_t srlen, sulen;
193 char *desc = NULL;
194 int ret;
195
196 cert = x509_cert_parse(prep->data, prep->datalen);
197 if (IS_ERR(cert))
198 return PTR_ERR(cert);
199
200 pr_devel("Cert Issuer: %s\n", cert->issuer);
201 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100202
203 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
204 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
205 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
206 !pkey_algo[cert->pub->pkey_algo] ||
207 !pkey_algo[cert->sig.pkey_algo] ||
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300208 !hash_algo_name[cert->sig.pkey_hash_algo]) {
David Howells2ecdb232013-08-30 16:18:15 +0100209 ret = -ENOPKG;
210 goto error_free_cert;
211 }
212
David Howells67f7d60b2013-08-30 16:15:24 +0100213 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100214 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100215 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
216 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
217 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100218 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100219 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
220 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
221 cert->valid_to.tm_min, cert->valid_to.tm_sec);
Dmitry Kasatkinc7c8bb22013-04-25 10:43:56 +0300222 pr_devel("Cert Signature: %s + %s\n",
223 pkey_algo_name[cert->sig.pkey_algo],
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300224 hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100225
David Howells17334ca2013-08-30 16:18:31 +0100226 if (!cert->fingerprint) {
227 pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
David Howellsc26fd692012-09-24 17:11:48 +0100228 cert->subject);
229 ret = -EKEYREJECTED;
230 goto error_free_cert;
231 }
232
David Howells67f7d60b2013-08-30 16:15:24 +0100233 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100234 cert->pub->id_type = PKEY_ID_X509;
235
David Howells17334ca2013-08-30 16:18:31 +0100236 /* Check the signature on the key if it appears to be self-signed */
237 if (!cert->authority ||
238 strcmp(cert->fingerprint, cert->authority) == 0) {
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400239 ret = x509_check_signature(cert->pub, cert); /* self-signed */
David Howellsc26fd692012-09-24 17:11:48 +0100240 if (ret < 0)
241 goto error_free_cert;
Mimi Zohar3be4bea2013-08-20 14:36:27 -0400242 } else if (!prep->trusted) {
243 ret = x509_validate_trust(cert, get_system_trusted_keyring());
244 if (!ret)
245 prep->trusted = 1;
David Howellsc26fd692012-09-24 17:11:48 +0100246 }
247
248 /* Propose a description */
249 sulen = strlen(cert->subject);
250 srlen = strlen(cert->fingerprint);
251 ret = -ENOMEM;
252 desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
253 if (!desc)
254 goto error_free_cert;
255 memcpy(desc, cert->subject, sulen);
256 desc[sulen] = ':';
257 desc[sulen + 1] = ' ';
258 memcpy(desc + sulen + 2, cert->fingerprint, srlen);
259 desc[sulen + 2 + srlen] = 0;
260
261 /* We're pinning the module by being linked against it */
262 __module_get(public_key_subtype.owner);
263 prep->type_data[0] = &public_key_subtype;
264 prep->type_data[1] = cert->fingerprint;
265 prep->payload = cert->pub;
266 prep->description = desc;
267 prep->quotalen = 100;
268
269 /* We've finished with the certificate */
270 cert->pub = NULL;
271 cert->fingerprint = NULL;
272 desc = NULL;
273 ret = 0;
274
275error_free_cert:
276 x509_free_certificate(cert);
277 return ret;
278}
279
280static struct asymmetric_key_parser x509_key_parser = {
281 .owner = THIS_MODULE,
282 .name = "x509",
283 .parse = x509_key_preparse,
284};
285
286/*
287 * Module stuff
288 */
289static int __init x509_key_init(void)
290{
291 return register_asymmetric_key_parser(&x509_key_parser);
292}
293
294static void __exit x509_key_exit(void)
295{
296 unregister_asymmetric_key_parser(&x509_key_parser);
297}
298
299module_init(x509_key_init);
300module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400301
302MODULE_DESCRIPTION("X.509 certificate parser");
303MODULE_LICENSE("GPL");