blob: 87612642f1b1ed4f19cdcc73e3d17957f1297160 [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 Zohar09fbc472013-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 Zohar09fbc472013-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(
31 struct key *keyring,
32 const char *signer, size_t signer_len,
33 const char *authority, size_t auth_len)
34{
35 key_ref_t key;
36 char *id;
37
38 /* Construct an identifier. */
39 id = kmalloc(signer_len + 2 + auth_len + 1, GFP_KERNEL);
40 if (!id)
41 return ERR_PTR(-ENOMEM);
42
43 memcpy(id, signer, signer_len);
44 id[signer_len + 0] = ':';
45 id[signer_len + 1] = ' ';
46 memcpy(id + signer_len + 2, authority, auth_len);
47 id[signer_len + 2 + auth_len] = 0;
48
49 pr_debug("Look up: \"%s\"\n", id);
50
51 key = keyring_search(make_key_ref(keyring, 1),
52 &key_type_asymmetric, id);
53 if (IS_ERR(key))
54 pr_debug("Request for module key '%s' err %ld\n",
55 id, PTR_ERR(key));
56 kfree(id);
57
58 if (IS_ERR(key)) {
59 switch (PTR_ERR(key)) {
60 /* Hide some search errors */
61 case -EACCES:
62 case -ENOTDIR:
63 case -EAGAIN:
64 return ERR_PTR(-ENOKEY);
65 default:
66 return ERR_CAST(key);
67 }
68 }
69
70 pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key_ref_to_ptr(key)));
71 return key_ref_to_ptr(key);
72}
73
74/*
David Howellsb426beb2013-08-30 16:18:02 +010075 * Set up the signature parameters in an X.509 certificate. This involves
76 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +010077 */
David Howellsb426beb2013-08-30 16:18:02 +010078int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +010079{
David Howellsc26fd692012-09-24 17:11:48 +010080 struct crypto_shash *tfm;
81 struct shash_desc *desc;
82 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +010083 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +010084 int ret;
85
86 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +010087
88 if (cert->sig.rsa.s)
89 return 0;
90
91 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
92 if (!cert->sig.rsa.s)
93 return -ENOMEM;
94 cert->sig.nr_mpi = 1;
95
David Howellsc26fd692012-09-24 17:11:48 +010096 /* Allocate the hashing algorithm we're going to need and find out how
97 * big the hash operational data will be.
98 */
David Howellsb426beb2013-08-30 16:18:02 +010099 tfm = crypto_alloc_shash(pkey_hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howellsc26fd692012-09-24 17:11:48 +0100100 if (IS_ERR(tfm))
101 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
102
103 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
104 digest_size = crypto_shash_digestsize(tfm);
105
David Howellsb426beb2013-08-30 16:18:02 +0100106 /* We allocate the hash operational data storage on the end of the
107 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +0100108 */
109 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +0100110 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
111 if (!digest)
112 goto error;
David Howellsc26fd692012-09-24 17:11:48 +0100113
David Howellsb426beb2013-08-30 16:18:02 +0100114 cert->sig.digest = digest;
115 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +0100116
David Howellsb426beb2013-08-30 16:18:02 +0100117 desc = digest + digest_size;
118 desc->tfm = tfm;
119 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +0100120
121 ret = crypto_shash_init(desc);
122 if (ret < 0)
123 goto error;
David Howellsb426beb2013-08-30 16:18:02 +0100124 might_sleep();
125 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +0100126error:
David Howellsc26fd692012-09-24 17:11:48 +0100127 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100128 pr_devel("<==%s() = %d\n", __func__, ret);
129 return ret;
130}
David Howellsb426beb2013-08-30 16:18:02 +0100131EXPORT_SYMBOL_GPL(x509_get_sig_params);
132
133/*
134 * Check the signature on a certificate using the provided public key
135 */
136int x509_check_signature(const struct public_key *pub,
137 struct x509_certificate *cert)
138{
139 int ret;
140
141 pr_devel("==>%s()\n", __func__);
142
143 ret = x509_get_sig_params(cert);
144 if (ret < 0)
145 return ret;
146
147 ret = public_key_verify_signature(pub, &cert->sig);
148 pr_debug("Cert Verification: %d\n", ret);
149 return ret;
150}
151EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100152
153/*
Mimi Zohar09fbc472013-08-20 14:36:27 -0400154 * Check the new certificate against the ones in the trust keyring. If one of
155 * those is the signing key and validates the new certificate, then mark the
156 * new certificate as being trusted.
157 *
158 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
159 * find a matching parent certificate in the trusted list and an error if there
160 * is a matching certificate but the signature check fails.
161 */
162static int x509_validate_trust(struct x509_certificate *cert,
163 struct key *trust_keyring)
164{
165 const struct public_key *pk;
166 struct key *key;
167 int ret = 1;
168
169 key = x509_request_asymmetric_key(trust_keyring,
170 cert->issuer, strlen(cert->issuer),
171 cert->authority,
172 strlen(cert->authority));
173 if (!IS_ERR(key)) {
174 pk = key->payload.data;
175 ret = x509_check_signature(pk, cert);
176 }
177 return ret;
178}
179
180/*
David Howellsc26fd692012-09-24 17:11:48 +0100181 * Attempt to parse a data blob for a key as an X509 certificate.
182 */
183static int x509_key_preparse(struct key_preparsed_payload *prep)
184{
185 struct x509_certificate *cert;
David Howellsc26fd692012-09-24 17:11:48 +0100186 size_t srlen, sulen;
187 char *desc = NULL;
188 int ret;
189
190 cert = x509_cert_parse(prep->data, prep->datalen);
191 if (IS_ERR(cert))
192 return PTR_ERR(cert);
193
194 pr_devel("Cert Issuer: %s\n", cert->issuer);
195 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100196
197 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
198 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
199 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
200 !pkey_algo[cert->pub->pkey_algo] ||
201 !pkey_algo[cert->sig.pkey_algo] ||
202 !pkey_hash_algo_name[cert->sig.pkey_hash_algo]) {
203 ret = -ENOPKG;
204 goto error_free_cert;
205 }
206
David Howells67f7d60b2013-08-30 16:15:24 +0100207 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100208 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100209 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
210 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
211 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100212 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100213 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
214 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
215 cert->valid_to.tm_min, cert->valid_to.tm_sec);
David Howellsc26fd692012-09-24 17:11:48 +0100216 pr_devel("Cert Signature: %s + %s\n",
David Howellsb426beb2013-08-30 16:18:02 +0100217 pkey_algo_name[cert->sig.pkey_algo],
218 pkey_hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100219
David Howells17334ca2013-08-30 16:18:31 +0100220 if (!cert->fingerprint) {
221 pr_warn("Cert for '%s' must have a SubjKeyId extension\n",
David Howellsc26fd692012-09-24 17:11:48 +0100222 cert->subject);
223 ret = -EKEYREJECTED;
224 goto error_free_cert;
225 }
226
David Howells67f7d60b2013-08-30 16:15:24 +0100227 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100228 cert->pub->id_type = PKEY_ID_X509;
229
David Howells17334ca2013-08-30 16:18:31 +0100230 /* Check the signature on the key if it appears to be self-signed */
231 if (!cert->authority ||
232 strcmp(cert->fingerprint, cert->authority) == 0) {
Mimi Zohar09fbc472013-08-20 14:36:27 -0400233 ret = x509_check_signature(cert->pub, cert); /* self-signed */
David Howellsc26fd692012-09-24 17:11:48 +0100234 if (ret < 0)
235 goto error_free_cert;
Mimi Zohar09fbc472013-08-20 14:36:27 -0400236 } else {
237 ret = x509_validate_trust(cert, system_trusted_keyring);
238 if (!ret)
239 prep->trusted = 1;
David Howellsc26fd692012-09-24 17:11:48 +0100240 }
241
242 /* Propose a description */
243 sulen = strlen(cert->subject);
244 srlen = strlen(cert->fingerprint);
245 ret = -ENOMEM;
246 desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
247 if (!desc)
248 goto error_free_cert;
249 memcpy(desc, cert->subject, sulen);
250 desc[sulen] = ':';
251 desc[sulen + 1] = ' ';
252 memcpy(desc + sulen + 2, cert->fingerprint, srlen);
253 desc[sulen + 2 + srlen] = 0;
254
255 /* We're pinning the module by being linked against it */
256 __module_get(public_key_subtype.owner);
257 prep->type_data[0] = &public_key_subtype;
258 prep->type_data[1] = cert->fingerprint;
259 prep->payload = cert->pub;
260 prep->description = desc;
261 prep->quotalen = 100;
262
263 /* We've finished with the certificate */
264 cert->pub = NULL;
265 cert->fingerprint = NULL;
266 desc = NULL;
267 ret = 0;
268
269error_free_cert:
270 x509_free_certificate(cert);
271 return ret;
272}
273
274static struct asymmetric_key_parser x509_key_parser = {
275 .owner = THIS_MODULE,
276 .name = "x509",
277 .parse = x509_key_preparse,
278};
279
280/*
281 * Module stuff
282 */
283static int __init x509_key_init(void)
284{
285 return register_asymmetric_key_parser(&x509_key_parser);
286}
287
288static void __exit x509_key_exit(void)
289{
290 unregister_asymmetric_key_parser(&x509_key_parser);
291}
292
293module_init(x509_key_init);
294module_exit(x509_key_exit);