blob: eea71dc9686c29fd2c3fe07d2c9fc0b213bc99b7 [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>
David Howellsc26fd692012-09-24 17:11:48 +010016#include <keys/asymmetric-subtype.h>
17#include <keys/asymmetric-parser.h>
Mimi Zohar3be4bea2013-08-20 14:36:27 -040018#include <keys/system_keyring.h>
David Howellsc26fd692012-09-24 17:11:48 +010019#include <crypto/hash.h>
20#include "asymmetric_keys.h"
David Howellsc26fd692012-09-24 17:11:48 +010021#include "x509_parser.h"
22
David Howellsc26fd692012-09-24 17:11:48 +010023/*
David Howellsb426beb2013-08-30 16:18:02 +010024 * Set up the signature parameters in an X.509 certificate. This involves
25 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +010026 */
David Howellsb426beb2013-08-30 16:18:02 +010027int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +010028{
David Howells77d09102016-04-06 16:13:33 +010029 struct public_key_signature *sig = cert->sig;
David Howellsc26fd692012-09-24 17:11:48 +010030 struct crypto_shash *tfm;
31 struct shash_desc *desc;
David Howells77d09102016-04-06 16:13:33 +010032 size_t desc_size;
David Howellsc26fd692012-09-24 17:11:48 +010033 int ret;
34
35 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +010036
David Howells6c2dc5a2016-04-06 16:13:34 +010037 if (!cert->pub->pkey_algo)
38 cert->unsupported_key = true;
39
40 if (!sig->pkey_algo)
41 cert->unsupported_sig = true;
42
43 /* We check the hash if we can - even if we can't then verify it */
44 if (!sig->hash_algo) {
45 cert->unsupported_sig = true;
David Howellsb426beb2013-08-30 16:18:02 +010046 return 0;
David Howells6c2dc5a2016-04-06 16:13:34 +010047 }
David Howellsb426beb2013-08-30 16:18:02 +010048
David Howells77d09102016-04-06 16:13:33 +010049 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
50 if (!sig->s)
David Howellsb426beb2013-08-30 16:18:02 +010051 return -ENOMEM;
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080052
David Howells77d09102016-04-06 16:13:33 +010053 sig->s_size = cert->raw_sig_size;
David Howellsb426beb2013-08-30 16:18:02 +010054
David Howellsc26fd692012-09-24 17:11:48 +010055 /* Allocate the hashing algorithm we're going to need and find out how
56 * big the hash operational data will be.
57 */
David Howells77d09102016-04-06 16:13:33 +010058 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
David Howells41559422014-09-16 17:36:15 +010059 if (IS_ERR(tfm)) {
60 if (PTR_ERR(tfm) == -ENOENT) {
David Howells6c2dc5a2016-04-06 16:13:34 +010061 cert->unsupported_sig = true;
62 return 0;
David Howells41559422014-09-16 17:36:15 +010063 }
64 return PTR_ERR(tfm);
65 }
David Howellsc26fd692012-09-24 17:11:48 +010066
67 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
David Howells77d09102016-04-06 16:13:33 +010068 sig->digest_size = crypto_shash_digestsize(tfm);
David Howellsc26fd692012-09-24 17:11:48 +010069
David Howellsc26fd692012-09-24 17:11:48 +010070 ret = -ENOMEM;
David Howells77d09102016-04-06 16:13:33 +010071 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
72 if (!sig->digest)
David Howellsb426beb2013-08-30 16:18:02 +010073 goto error;
David Howellsc26fd692012-09-24 17:11:48 +010074
David Howells77d09102016-04-06 16:13:33 +010075 desc = kzalloc(desc_size, GFP_KERNEL);
76 if (!desc)
77 goto error;
David Howellsc26fd692012-09-24 17:11:48 +010078
David Howellsb426beb2013-08-30 16:18:02 +010079 desc->tfm = tfm;
80 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
David Howellsc26fd692012-09-24 17:11:48 +010081
82 ret = crypto_shash_init(desc);
83 if (ret < 0)
David Howells77d09102016-04-06 16:13:33 +010084 goto error_2;
David Howellsb426beb2013-08-30 16:18:02 +010085 might_sleep();
David Howells77d09102016-04-06 16:13:33 +010086 ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);
David Howells43652952017-04-03 16:07:25 +010087 if (ret < 0)
88 goto error_2;
89
90 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
91 if (ret == -EKEYREJECTED) {
92 pr_err("Cert %*phN is blacklisted\n",
93 sig->digest_size, sig->digest);
94 cert->blacklisted = true;
95 ret = 0;
96 }
David Howells77d09102016-04-06 16:13:33 +010097
98error_2:
99 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +0100100error:
David Howellsc26fd692012-09-24 17:11:48 +0100101 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +0100102 pr_devel("<==%s() = %d\n", __func__, ret);
103 return ret;
104}
David Howellsb426beb2013-08-30 16:18:02 +0100105
106/*
David Howells6c2dc5a2016-04-06 16:13:34 +0100107 * Check for self-signedness in an X.509 cert and if found, check the signature
108 * immediately if we can.
David Howellsb426beb2013-08-30 16:18:02 +0100109 */
David Howells6c2dc5a2016-04-06 16:13:34 +0100110int x509_check_for_self_signed(struct x509_certificate *cert)
David Howellsb426beb2013-08-30 16:18:02 +0100111{
David Howells6c2dc5a2016-04-06 16:13:34 +0100112 int ret = 0;
David Howellsb426beb2013-08-30 16:18:02 +0100113
114 pr_devel("==>%s()\n", __func__);
115
David Howellsad3043f2016-04-06 16:13:34 +0100116 if (cert->raw_subject_size != cert->raw_issuer_size ||
117 memcmp(cert->raw_subject, cert->raw_issuer,
118 cert->raw_issuer_size) != 0)
119 goto not_self_signed;
120
David Howells6c2dc5a2016-04-06 16:13:34 +0100121 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
122 /* If the AKID is present it may have one or two parts. If
123 * both are supplied, both must match.
124 */
125 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
126 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
David Howellsb426beb2013-08-30 16:18:02 +0100127
David Howells6c2dc5a2016-04-06 16:13:34 +0100128 if (!a && !b)
129 goto not_self_signed;
130
131 ret = -EKEYREJECTED;
132 if (((a && !b) || (b && !a)) &&
133 cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
134 goto out;
135 }
136
David Howellsad3043f2016-04-06 16:13:34 +0100137 ret = -EKEYREJECTED;
138 if (cert->pub->pkey_algo != cert->sig->pkey_algo)
139 goto out;
140
David Howells6c2dc5a2016-04-06 16:13:34 +0100141 ret = public_key_verify_signature(cert->pub, cert->sig);
142 if (ret < 0) {
143 if (ret == -ENOPKG) {
144 cert->unsupported_sig = true;
145 ret = 0;
146 }
147 goto out;
148 }
149
150 pr_devel("Cert Self-signature verified");
151 cert->self_signed = true;
152
153out:
154 pr_devel("<==%s() = %d\n", __func__, ret);
David Howellsb426beb2013-08-30 16:18:02 +0100155 return ret;
David Howells6c2dc5a2016-04-06 16:13:34 +0100156
157not_self_signed:
158 pr_devel("<==%s() = 0 [not]\n", __func__);
159 return 0;
David Howellsb426beb2013-08-30 16:18:02 +0100160}
David Howellsc26fd692012-09-24 17:11:48 +0100161
162/*
163 * Attempt to parse a data blob for a key as an X509 certificate.
164 */
165static int x509_key_preparse(struct key_preparsed_payload *prep)
166{
David Howells46963b72014-09-16 17:36:13 +0100167 struct asymmetric_key_ids *kids;
David Howellsc26fd692012-09-24 17:11:48 +0100168 struct x509_certificate *cert;
David Howells46963b72014-09-16 17:36:13 +0100169 const char *q;
David Howellsc26fd692012-09-24 17:11:48 +0100170 size_t srlen, sulen;
David Howells46963b72014-09-16 17:36:13 +0100171 char *desc = NULL, *p;
David Howellsc26fd692012-09-24 17:11:48 +0100172 int ret;
173
174 cert = x509_cert_parse(prep->data, prep->datalen);
175 if (IS_ERR(cert))
176 return PTR_ERR(cert);
177
178 pr_devel("Cert Issuer: %s\n", cert->issuer);
179 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100180
David Howells6c2dc5a2016-04-06 16:13:34 +0100181 if (cert->unsupported_key) {
David Howells2ecdb232013-08-30 16:18:15 +0100182 ret = -ENOPKG;
183 goto error_free_cert;
184 }
185
David Howells4e8ae722016-03-03 21:49:27 +0000186 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
David Howellsfd19a3d2015-07-29 16:58:32 +0100187 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
David Howellsc26fd692012-09-24 17:11:48 +0100188
David Howells4e8ae722016-03-03 21:49:27 +0000189 cert->pub->id_type = "X509";
David Howellsc26fd692012-09-24 17:11:48 +0100190
David Howellsa511e1a2016-04-06 16:14:26 +0100191 if (cert->unsupported_sig) {
David Howells6c2dc5a2016-04-06 16:13:34 +0100192 public_key_signature_free(cert->sig);
193 cert->sig = NULL;
194 } else {
195 pr_devel("Cert Signature: %s + %s\n",
196 cert->sig->pkey_algo, cert->sig->hash_algo);
David Howellsc26fd692012-09-24 17:11:48 +0100197 }
198
David Howells43652952017-04-03 16:07:25 +0100199 /* Don't permit addition of blacklisted keys */
200 ret = -EKEYREJECTED;
201 if (cert->blacklisted)
202 goto error_free_cert;
203
David Howellsc26fd692012-09-24 17:11:48 +0100204 /* Propose a description */
205 sulen = strlen(cert->subject);
David Howellsdd2f6c42014-10-03 16:17:02 +0100206 if (cert->raw_skid) {
207 srlen = cert->raw_skid_size;
208 q = cert->raw_skid;
209 } else {
210 srlen = cert->raw_serial_size;
211 q = cert->raw_serial;
212 }
David Howells46963b72014-09-16 17:36:13 +0100213
David Howellsc26fd692012-09-24 17:11:48 +0100214 ret = -ENOMEM;
David Howells46963b72014-09-16 17:36:13 +0100215 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
David Howellsc26fd692012-09-24 17:11:48 +0100216 if (!desc)
217 goto error_free_cert;
David Howells46963b72014-09-16 17:36:13 +0100218 p = memcpy(desc, cert->subject, sulen);
219 p += sulen;
220 *p++ = ':';
221 *p++ = ' ';
222 p = bin2hex(p, q, srlen);
223 *p = 0;
224
225 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
226 if (!kids)
227 goto error_free_desc;
228 kids->id[0] = cert->id;
229 kids->id[1] = cert->skid;
David Howellsc26fd692012-09-24 17:11:48 +0100230
231 /* We're pinning the module by being linked against it */
232 __module_get(public_key_subtype.owner);
David Howells146aa8b2015-10-21 14:04:48 +0100233 prep->payload.data[asym_subtype] = &public_key_subtype;
234 prep->payload.data[asym_key_ids] = kids;
235 prep->payload.data[asym_crypto] = cert->pub;
David Howells77d09102016-04-06 16:13:33 +0100236 prep->payload.data[asym_auth] = cert->sig;
David Howellsc26fd692012-09-24 17:11:48 +0100237 prep->description = desc;
238 prep->quotalen = 100;
239
240 /* We've finished with the certificate */
241 cert->pub = NULL;
David Howells46963b72014-09-16 17:36:13 +0100242 cert->id = NULL;
243 cert->skid = NULL;
David Howells77d09102016-04-06 16:13:33 +0100244 cert->sig = NULL;
David Howellsc26fd692012-09-24 17:11:48 +0100245 desc = NULL;
246 ret = 0;
247
David Howells46963b72014-09-16 17:36:13 +0100248error_free_desc:
249 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +0100250error_free_cert:
251 x509_free_certificate(cert);
252 return ret;
253}
254
255static struct asymmetric_key_parser x509_key_parser = {
256 .owner = THIS_MODULE,
257 .name = "x509",
258 .parse = x509_key_preparse,
259};
260
261/*
262 * Module stuff
263 */
264static int __init x509_key_init(void)
265{
266 return register_asymmetric_key_parser(&x509_key_parser);
267}
268
269static void __exit x509_key_exit(void)
270{
271 unregister_asymmetric_key_parser(&x509_key_parser);
272}
273
274module_init(x509_key_init);
275module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400276
277MODULE_DESCRIPTION("X.509 certificate parser");
278MODULE_LICENSE("GPL");