blob: eb368d4c632c1e4ca29ec4d1a04d074e907e32b1 [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>
21#include <crypto/hash.h>
22#include "asymmetric_keys.h"
23#include "public_key.h"
24#include "x509_parser.h"
25
David Howellsc26fd692012-09-24 17:11:48 +010026/*
David Howellsb426beb2013-08-30 16:18:02 +010027 * Set up the signature parameters in an X.509 certificate. This involves
28 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +010029 */
David Howellsb426beb2013-08-30 16:18:02 +010030int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +010031{
David Howellsc26fd692012-09-24 17:11:48 +010032 struct crypto_shash *tfm;
33 struct shash_desc *desc;
34 size_t digest_size, desc_size;
David Howellsb426beb2013-08-30 16:18:02 +010035 void *digest;
David Howellsc26fd692012-09-24 17:11:48 +010036 int ret;
37
38 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +010039
40 if (cert->sig.rsa.s)
41 return 0;
42
43 cert->sig.rsa.s = mpi_read_raw_data(cert->raw_sig, cert->raw_sig_size);
44 if (!cert->sig.rsa.s)
45 return -ENOMEM;
46 cert->sig.nr_mpi = 1;
47
David Howellsc26fd692012-09-24 17:11:48 +010048 /* Allocate the hashing algorithm we're going to need and find out how
49 * big the hash operational data will be.
50 */
David Howellsb426beb2013-08-30 16:18:02 +010051 tfm = crypto_alloc_shash(pkey_hash_algo_name[cert->sig.pkey_hash_algo], 0, 0);
David Howellsc26fd692012-09-24 17:11:48 +010052 if (IS_ERR(tfm))
53 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
54
55 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
56 digest_size = crypto_shash_digestsize(tfm);
57
David Howellsb426beb2013-08-30 16:18:02 +010058 /* We allocate the hash operational data storage on the end of the
59 * digest storage space.
David Howellsc26fd692012-09-24 17:11:48 +010060 */
61 ret = -ENOMEM;
David Howellsb426beb2013-08-30 16:18:02 +010062 digest = kzalloc(digest_size + desc_size, GFP_KERNEL);
63 if (!digest)
64 goto error;
David Howellsc26fd692012-09-24 17:11:48 +010065
David Howellsb426beb2013-08-30 16:18:02 +010066 cert->sig.digest = digest;
67 cert->sig.digest_size = digest_size;
David Howellsc26fd692012-09-24 17:11:48 +010068
David Howellsb426beb2013-08-30 16:18:02 +010069 desc = digest + digest_size;
70 desc->tfm = tfm;
71 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +010072
73 ret = crypto_shash_init(desc);
74 if (ret < 0)
75 goto error;
David Howellsb426beb2013-08-30 16:18:02 +010076 might_sleep();
77 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, digest);
David Howellsc26fd692012-09-24 17:11:48 +010078error:
David Howellsc26fd692012-09-24 17:11:48 +010079 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +010080 pr_devel("<==%s() = %d\n", __func__, ret);
81 return ret;
82}
David Howellsb426beb2013-08-30 16:18:02 +010083EXPORT_SYMBOL_GPL(x509_get_sig_params);
84
85/*
86 * Check the signature on a certificate using the provided public key
87 */
88int x509_check_signature(const struct public_key *pub,
89 struct x509_certificate *cert)
90{
91 int ret;
92
93 pr_devel("==>%s()\n", __func__);
94
95 ret = x509_get_sig_params(cert);
96 if (ret < 0)
97 return ret;
98
99 ret = public_key_verify_signature(pub, &cert->sig);
100 pr_debug("Cert Verification: %d\n", ret);
101 return ret;
102}
103EXPORT_SYMBOL_GPL(x509_check_signature);
David Howellsc26fd692012-09-24 17:11:48 +0100104
105/*
106 * Attempt to parse a data blob for a key as an X509 certificate.
107 */
108static int x509_key_preparse(struct key_preparsed_payload *prep)
109{
110 struct x509_certificate *cert;
David Howellsa5752d12012-10-02 14:36:16 +0100111 struct tm now;
David Howellsc26fd692012-09-24 17:11:48 +0100112 size_t srlen, sulen;
113 char *desc = NULL;
114 int ret;
115
116 cert = x509_cert_parse(prep->data, prep->datalen);
117 if (IS_ERR(cert))
118 return PTR_ERR(cert);
119
120 pr_devel("Cert Issuer: %s\n", cert->issuer);
121 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100122
123 if (cert->pub->pkey_algo >= PKEY_ALGO__LAST ||
124 cert->sig.pkey_algo >= PKEY_ALGO__LAST ||
125 cert->sig.pkey_hash_algo >= PKEY_HASH__LAST ||
126 !pkey_algo[cert->pub->pkey_algo] ||
127 !pkey_algo[cert->sig.pkey_algo] ||
128 !pkey_hash_algo_name[cert->sig.pkey_hash_algo]) {
129 ret = -ENOPKG;
130 goto error_free_cert;
131 }
132
David Howells67f7d60b2013-08-30 16:15:24 +0100133 pr_devel("Cert Key Algo: %s\n", pkey_algo_name[cert->pub->pkey_algo]);
David Howells2f1c4fe2012-10-04 14:21:23 +0100134 pr_devel("Cert Valid From: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100135 cert->valid_from.tm_year + 1900, cert->valid_from.tm_mon + 1,
136 cert->valid_from.tm_mday, cert->valid_from.tm_hour,
137 cert->valid_from.tm_min, cert->valid_from.tm_sec);
David Howells2f1c4fe2012-10-04 14:21:23 +0100138 pr_devel("Cert Valid To: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100139 cert->valid_to.tm_year + 1900, cert->valid_to.tm_mon + 1,
140 cert->valid_to.tm_mday, cert->valid_to.tm_hour,
141 cert->valid_to.tm_min, cert->valid_to.tm_sec);
David Howellsc26fd692012-09-24 17:11:48 +0100142 pr_devel("Cert Signature: %s + %s\n",
David Howellsb426beb2013-08-30 16:18:02 +0100143 pkey_algo_name[cert->sig.pkey_algo],
144 pkey_hash_algo_name[cert->sig.pkey_hash_algo]);
David Howellsc26fd692012-09-24 17:11:48 +0100145
146 if (!cert->fingerprint || !cert->authority) {
147 pr_warn("Cert for '%s' must have SubjKeyId and AuthKeyId extensions\n",
148 cert->subject);
149 ret = -EKEYREJECTED;
150 goto error_free_cert;
151 }
152
David Howellsa5752d12012-10-02 14:36:16 +0100153 time_to_tm(CURRENT_TIME.tv_sec, 0, &now);
David Howells2f1c4fe2012-10-04 14:21:23 +0100154 pr_devel("Now: %04ld-%02d-%02d %02d:%02d:%02d\n",
David Howellsa5752d12012-10-02 14:36:16 +0100155 now.tm_year + 1900, now.tm_mon + 1, now.tm_mday,
156 now.tm_hour, now.tm_min, now.tm_sec);
157 if (now.tm_year < cert->valid_from.tm_year ||
158 (now.tm_year == cert->valid_from.tm_year &&
159 (now.tm_mon < cert->valid_from.tm_mon ||
160 (now.tm_mon == cert->valid_from.tm_mon &&
161 (now.tm_mday < cert->valid_from.tm_mday ||
162 (now.tm_mday == cert->valid_from.tm_mday &&
163 (now.tm_hour < cert->valid_from.tm_hour ||
164 (now.tm_hour == cert->valid_from.tm_hour &&
165 (now.tm_min < cert->valid_from.tm_min ||
166 (now.tm_min == cert->valid_from.tm_min &&
167 (now.tm_sec < cert->valid_from.tm_sec
168 ))))))))))) {
David Howellsc26fd692012-09-24 17:11:48 +0100169 pr_warn("Cert %s is not yet valid\n", cert->fingerprint);
170 ret = -EKEYREJECTED;
171 goto error_free_cert;
172 }
David Howellsa5752d12012-10-02 14:36:16 +0100173 if (now.tm_year > cert->valid_to.tm_year ||
174 (now.tm_year == cert->valid_to.tm_year &&
175 (now.tm_mon > cert->valid_to.tm_mon ||
176 (now.tm_mon == cert->valid_to.tm_mon &&
177 (now.tm_mday > cert->valid_to.tm_mday ||
178 (now.tm_mday == cert->valid_to.tm_mday &&
179 (now.tm_hour > cert->valid_to.tm_hour ||
180 (now.tm_hour == cert->valid_to.tm_hour &&
181 (now.tm_min > cert->valid_to.tm_min ||
182 (now.tm_min == cert->valid_to.tm_min &&
183 (now.tm_sec > cert->valid_to.tm_sec
184 ))))))))))) {
David Howellsc26fd692012-09-24 17:11:48 +0100185 pr_warn("Cert %s has expired\n", cert->fingerprint);
186 ret = -EKEYEXPIRED;
187 goto error_free_cert;
188 }
189
David Howells67f7d60b2013-08-30 16:15:24 +0100190 cert->pub->algo = pkey_algo[cert->pub->pkey_algo];
David Howellsc26fd692012-09-24 17:11:48 +0100191 cert->pub->id_type = PKEY_ID_X509;
192
193 /* Check the signature on the key */
194 if (strcmp(cert->fingerprint, cert->authority) == 0) {
195 ret = x509_check_signature(cert->pub, cert);
196 if (ret < 0)
197 goto error_free_cert;
198 }
199
200 /* Propose a description */
201 sulen = strlen(cert->subject);
202 srlen = strlen(cert->fingerprint);
203 ret = -ENOMEM;
204 desc = kmalloc(sulen + 2 + srlen + 1, GFP_KERNEL);
205 if (!desc)
206 goto error_free_cert;
207 memcpy(desc, cert->subject, sulen);
208 desc[sulen] = ':';
209 desc[sulen + 1] = ' ';
210 memcpy(desc + sulen + 2, cert->fingerprint, srlen);
211 desc[sulen + 2 + srlen] = 0;
212
213 /* We're pinning the module by being linked against it */
214 __module_get(public_key_subtype.owner);
215 prep->type_data[0] = &public_key_subtype;
216 prep->type_data[1] = cert->fingerprint;
217 prep->payload = cert->pub;
218 prep->description = desc;
219 prep->quotalen = 100;
220
221 /* We've finished with the certificate */
222 cert->pub = NULL;
223 cert->fingerprint = NULL;
224 desc = NULL;
225 ret = 0;
226
227error_free_cert:
228 x509_free_certificate(cert);
229 return ret;
230}
231
232static struct asymmetric_key_parser x509_key_parser = {
233 .owner = THIS_MODULE,
234 .name = "x509",
235 .parse = x509_key_preparse,
236};
237
238/*
239 * Module stuff
240 */
241static int __init x509_key_init(void)
242{
243 return register_asymmetric_key_parser(&x509_key_parser);
244}
245
246static void __exit x509_key_exit(void)
247{
248 unregister_asymmetric_key_parser(&x509_key_parser);
249}
250
251module_init(x509_key_init);
252module_exit(x509_key_exit);