blob: fe14cae115b51bd03853cfcea3ac2e14cf05d871 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsc26fd692012-09-24 17:11:48 +01002/* Instantiate a public key crypto key from an X.509 Certificate
3 *
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howellsc26fd692012-09-24 17:11:48 +01006 */
7
8#define pr_fmt(fmt) "X.509: "fmt
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/slab.h>
David Howellsc26fd692012-09-24 17:11:48 +010012#include <keys/asymmetric-subtype.h>
13#include <keys/asymmetric-parser.h>
Mimi Zohar3be4bea2013-08-20 14:36:27 -040014#include <keys/system_keyring.h>
David Howellsc26fd692012-09-24 17:11:48 +010015#include <crypto/hash.h>
16#include "asymmetric_keys.h"
David Howellsc26fd692012-09-24 17:11:48 +010017#include "x509_parser.h"
18
David Howellsc26fd692012-09-24 17:11:48 +010019/*
David Howellsb426beb2013-08-30 16:18:02 +010020 * Set up the signature parameters in an X.509 certificate. This involves
21 * digesting the signed data and extracting the signature.
David Howellsc26fd692012-09-24 17:11:48 +010022 */
David Howellsb426beb2013-08-30 16:18:02 +010023int x509_get_sig_params(struct x509_certificate *cert)
David Howellsc26fd692012-09-24 17:11:48 +010024{
David Howells77d09102016-04-06 16:13:33 +010025 struct public_key_signature *sig = cert->sig;
David Howellsc26fd692012-09-24 17:11:48 +010026 struct crypto_shash *tfm;
27 struct shash_desc *desc;
David Howells77d09102016-04-06 16:13:33 +010028 size_t desc_size;
David Howellsc26fd692012-09-24 17:11:48 +010029 int ret;
30
31 pr_devel("==>%s()\n", __func__);
David Howellsb426beb2013-08-30 16:18:02 +010032
Tianjia Zhang21552562020-09-21 00:21:02 +080033 sig->data = cert->tbs;
34 sig->data_size = cert->tbs_size;
35
David Howells6c2dc5a2016-04-06 16:13:34 +010036 if (!cert->pub->pkey_algo)
37 cert->unsupported_key = true;
38
39 if (!sig->pkey_algo)
40 cert->unsupported_sig = true;
41
42 /* We check the hash if we can - even if we can't then verify it */
43 if (!sig->hash_algo) {
44 cert->unsupported_sig = true;
David Howellsb426beb2013-08-30 16:18:02 +010045 return 0;
David Howells6c2dc5a2016-04-06 16:13:34 +010046 }
David Howellsb426beb2013-08-30 16:18:02 +010047
David Howells77d09102016-04-06 16:13:33 +010048 sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
49 if (!sig->s)
David Howellsb426beb2013-08-30 16:18:02 +010050 return -ENOMEM;
Tadeusz Strukdb6c43b2016-02-02 10:08:53 -080051
David Howells77d09102016-04-06 16:13:33 +010052 sig->s_size = cert->raw_sig_size;
David Howellsb426beb2013-08-30 16:18:02 +010053
David Howellsc26fd692012-09-24 17:11:48 +010054 /* Allocate the hashing algorithm we're going to need and find out how
55 * big the hash operational data will be.
56 */
David Howells77d09102016-04-06 16:13:33 +010057 tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
David Howells41559422014-09-16 17:36:15 +010058 if (IS_ERR(tfm)) {
59 if (PTR_ERR(tfm) == -ENOENT) {
David Howells6c2dc5a2016-04-06 16:13:34 +010060 cert->unsupported_sig = true;
61 return 0;
David Howells41559422014-09-16 17:36:15 +010062 }
63 return PTR_ERR(tfm);
64 }
David Howellsc26fd692012-09-24 17:11:48 +010065
66 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
David Howells77d09102016-04-06 16:13:33 +010067 sig->digest_size = crypto_shash_digestsize(tfm);
David Howellsc26fd692012-09-24 17:11:48 +010068
David Howellsc26fd692012-09-24 17:11:48 +010069 ret = -ENOMEM;
David Howells77d09102016-04-06 16:13:33 +010070 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
71 if (!sig->digest)
David Howellsb426beb2013-08-30 16:18:02 +010072 goto error;
David Howellsc26fd692012-09-24 17:11:48 +010073
David Howells77d09102016-04-06 16:13:33 +010074 desc = kzalloc(desc_size, GFP_KERNEL);
75 if (!desc)
76 goto error;
David Howellsc26fd692012-09-24 17:11:48 +010077
David Howellsb426beb2013-08-30 16:18:02 +010078 desc->tfm = tfm;
David Howellsc26fd692012-09-24 17:11:48 +010079
Eric Biggersaa330032017-12-08 15:13:29 +000080 ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
David Howells43652952017-04-03 16:07:25 +010081 if (ret < 0)
82 goto error_2;
83
84 ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
85 if (ret == -EKEYREJECTED) {
86 pr_err("Cert %*phN is blacklisted\n",
87 sig->digest_size, sig->digest);
88 cert->blacklisted = true;
89 ret = 0;
90 }
David Howells77d09102016-04-06 16:13:33 +010091
92error_2:
93 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +010094error:
David Howellsc26fd692012-09-24 17:11:48 +010095 crypto_free_shash(tfm);
David Howellsc26fd692012-09-24 17:11:48 +010096 pr_devel("<==%s() = %d\n", __func__, ret);
97 return ret;
98}
David Howellsb426beb2013-08-30 16:18:02 +010099
100/*
David Howells6c2dc5a2016-04-06 16:13:34 +0100101 * Check for self-signedness in an X.509 cert and if found, check the signature
102 * immediately if we can.
David Howellsb426beb2013-08-30 16:18:02 +0100103 */
David Howells6c2dc5a2016-04-06 16:13:34 +0100104int x509_check_for_self_signed(struct x509_certificate *cert)
David Howellsb426beb2013-08-30 16:18:02 +0100105{
David Howells6c2dc5a2016-04-06 16:13:34 +0100106 int ret = 0;
David Howellsb426beb2013-08-30 16:18:02 +0100107
108 pr_devel("==>%s()\n", __func__);
109
David Howellsad3043f2016-04-06 16:13:34 +0100110 if (cert->raw_subject_size != cert->raw_issuer_size ||
111 memcmp(cert->raw_subject, cert->raw_issuer,
112 cert->raw_issuer_size) != 0)
113 goto not_self_signed;
114
David Howells6c2dc5a2016-04-06 16:13:34 +0100115 if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
116 /* If the AKID is present it may have one or two parts. If
117 * both are supplied, both must match.
118 */
119 bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
120 bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
David Howellsb426beb2013-08-30 16:18:02 +0100121
David Howells6c2dc5a2016-04-06 16:13:34 +0100122 if (!a && !b)
123 goto not_self_signed;
124
125 ret = -EKEYREJECTED;
126 if (((a && !b) || (b && !a)) &&
127 cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
128 goto out;
129 }
130
David Howellsad3043f2016-04-06 16:13:34 +0100131 ret = -EKEYREJECTED;
Stefan Berger299f5612021-03-16 17:07:37 -0400132 if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0 &&
133 (strncmp(cert->pub->pkey_algo, "ecdsa-", 6) != 0 ||
134 strcmp(cert->sig->pkey_algo, "ecdsa") != 0))
David Howellsad3043f2016-04-06 16:13:34 +0100135 goto out;
136
David Howells6c2dc5a2016-04-06 16:13:34 +0100137 ret = public_key_verify_signature(cert->pub, cert->sig);
138 if (ret < 0) {
139 if (ret == -ENOPKG) {
140 cert->unsupported_sig = true;
141 ret = 0;
142 }
143 goto out;
144 }
145
146 pr_devel("Cert Self-signature verified");
147 cert->self_signed = true;
148
149out:
150 pr_devel("<==%s() = %d\n", __func__, ret);
David Howellsb426beb2013-08-30 16:18:02 +0100151 return ret;
David Howells6c2dc5a2016-04-06 16:13:34 +0100152
153not_self_signed:
154 pr_devel("<==%s() = 0 [not]\n", __func__);
155 return 0;
David Howellsb426beb2013-08-30 16:18:02 +0100156}
David Howellsc26fd692012-09-24 17:11:48 +0100157
158/*
159 * Attempt to parse a data blob for a key as an X509 certificate.
160 */
161static int x509_key_preparse(struct key_preparsed_payload *prep)
162{
David Howells46963b72014-09-16 17:36:13 +0100163 struct asymmetric_key_ids *kids;
David Howellsc26fd692012-09-24 17:11:48 +0100164 struct x509_certificate *cert;
David Howells46963b72014-09-16 17:36:13 +0100165 const char *q;
David Howellsc26fd692012-09-24 17:11:48 +0100166 size_t srlen, sulen;
David Howells46963b72014-09-16 17:36:13 +0100167 char *desc = NULL, *p;
David Howellsc26fd692012-09-24 17:11:48 +0100168 int ret;
169
170 cert = x509_cert_parse(prep->data, prep->datalen);
171 if (IS_ERR(cert))
172 return PTR_ERR(cert);
173
174 pr_devel("Cert Issuer: %s\n", cert->issuer);
175 pr_devel("Cert Subject: %s\n", cert->subject);
David Howells2ecdb232013-08-30 16:18:15 +0100176
David Howells6c2dc5a2016-04-06 16:13:34 +0100177 if (cert->unsupported_key) {
David Howells2ecdb232013-08-30 16:18:15 +0100178 ret = -ENOPKG;
179 goto error_free_cert;
180 }
181
David Howells4e8ae722016-03-03 21:49:27 +0000182 pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
David Howellsfd19a3d2015-07-29 16:58:32 +0100183 pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
David Howellsc26fd692012-09-24 17:11:48 +0100184
David Howells4e8ae722016-03-03 21:49:27 +0000185 cert->pub->id_type = "X509";
David Howellsc26fd692012-09-24 17:11:48 +0100186
David Howellsa511e1a2016-04-06 16:14:26 +0100187 if (cert->unsupported_sig) {
David Howells6c2dc5a2016-04-06 16:13:34 +0100188 public_key_signature_free(cert->sig);
189 cert->sig = NULL;
190 } else {
191 pr_devel("Cert Signature: %s + %s\n",
192 cert->sig->pkey_algo, cert->sig->hash_algo);
David Howellsc26fd692012-09-24 17:11:48 +0100193 }
194
David Howells43652952017-04-03 16:07:25 +0100195 /* Don't permit addition of blacklisted keys */
196 ret = -EKEYREJECTED;
197 if (cert->blacklisted)
198 goto error_free_cert;
199
David Howellsc26fd692012-09-24 17:11:48 +0100200 /* Propose a description */
201 sulen = strlen(cert->subject);
David Howellsdd2f6c42014-10-03 16:17:02 +0100202 if (cert->raw_skid) {
203 srlen = cert->raw_skid_size;
204 q = cert->raw_skid;
205 } else {
206 srlen = cert->raw_serial_size;
207 q = cert->raw_serial;
208 }
David Howells46963b72014-09-16 17:36:13 +0100209
David Howellsc26fd692012-09-24 17:11:48 +0100210 ret = -ENOMEM;
David Howells46963b72014-09-16 17:36:13 +0100211 desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
David Howellsc26fd692012-09-24 17:11:48 +0100212 if (!desc)
213 goto error_free_cert;
David Howells46963b72014-09-16 17:36:13 +0100214 p = memcpy(desc, cert->subject, sulen);
215 p += sulen;
216 *p++ = ':';
217 *p++ = ' ';
218 p = bin2hex(p, q, srlen);
219 *p = 0;
220
221 kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
222 if (!kids)
223 goto error_free_desc;
224 kids->id[0] = cert->id;
225 kids->id[1] = cert->skid;
Andrew Zaborowski7d301982021-11-09 16:16:49 +0100226 kids->id[2] = asymmetric_key_generate_id(cert->raw_subject,
227 cert->raw_subject_size,
228 "", 0);
229 if (IS_ERR(kids->id[2])) {
230 ret = PTR_ERR(kids->id[2]);
231 goto error_free_kids;
232 }
David Howellsc26fd692012-09-24 17:11:48 +0100233
234 /* We're pinning the module by being linked against it */
235 __module_get(public_key_subtype.owner);
David Howells146aa8b2015-10-21 14:04:48 +0100236 prep->payload.data[asym_subtype] = &public_key_subtype;
237 prep->payload.data[asym_key_ids] = kids;
238 prep->payload.data[asym_crypto] = cert->pub;
David Howells77d09102016-04-06 16:13:33 +0100239 prep->payload.data[asym_auth] = cert->sig;
David Howellsc26fd692012-09-24 17:11:48 +0100240 prep->description = desc;
241 prep->quotalen = 100;
242
243 /* We've finished with the certificate */
244 cert->pub = NULL;
David Howells46963b72014-09-16 17:36:13 +0100245 cert->id = NULL;
246 cert->skid = NULL;
David Howells77d09102016-04-06 16:13:33 +0100247 cert->sig = NULL;
David Howellsc26fd692012-09-24 17:11:48 +0100248 desc = NULL;
Andrew Zaborowski7d301982021-11-09 16:16:49 +0100249 kids = NULL;
David Howellsc26fd692012-09-24 17:11:48 +0100250 ret = 0;
251
Andrew Zaborowski7d301982021-11-09 16:16:49 +0100252error_free_kids:
253 kfree(kids);
David Howells46963b72014-09-16 17:36:13 +0100254error_free_desc:
255 kfree(desc);
David Howellsc26fd692012-09-24 17:11:48 +0100256error_free_cert:
257 x509_free_certificate(cert);
258 return ret;
259}
260
261static struct asymmetric_key_parser x509_key_parser = {
262 .owner = THIS_MODULE,
263 .name = "x509",
264 .parse = x509_key_preparse,
265};
266
267/*
268 * Module stuff
269 */
270static int __init x509_key_init(void)
271{
272 return register_asymmetric_key_parser(&x509_key_parser);
273}
274
275static void __exit x509_key_exit(void)
276{
277 unregister_asymmetric_key_parser(&x509_key_parser);
278}
279
280module_init(x509_key_init);
281module_exit(x509_key_exit);
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400282
283MODULE_DESCRIPTION("X.509 certificate parser");
David Howells1e684d32017-11-15 16:38:45 +0000284MODULE_AUTHOR("Red Hat, Inc.");
Konstantin Khlebnikove19aaa72013-09-17 15:14:55 +0400285MODULE_LICENSE("GPL");