1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c26fd69fSDavid Howells /* Instantiate a public key crypto key from an X.509 Certificate
3c26fd69fSDavid Howells *
4c26fd69fSDavid Howells * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5c26fd69fSDavid Howells * Written by David Howells (dhowells@redhat.com)
6c26fd69fSDavid Howells */
7c26fd69fSDavid Howells
8c26fd69fSDavid Howells #define pr_fmt(fmt) "X.509: "fmt
9e5221fa6SHerbert Xu #include <crypto/hash.h>
10e5221fa6SHerbert Xu #include <crypto/sm2.h>
11e5221fa6SHerbert Xu #include <keys/asymmetric-parser.h>
12e5221fa6SHerbert Xu #include <keys/asymmetric-subtype.h>
13e5221fa6SHerbert Xu #include <keys/system_keyring.h>
14c26fd69fSDavid Howells #include <linux/module.h>
15c26fd69fSDavid Howells #include <linux/kernel.h>
16c26fd69fSDavid Howells #include <linux/slab.h>
17e5221fa6SHerbert Xu #include <linux/string.h>
18c26fd69fSDavid Howells #include "asymmetric_keys.h"
19c26fd69fSDavid Howells #include "x509_parser.h"
20c26fd69fSDavid Howells
21c26fd69fSDavid Howells /*
22b426beb6SDavid Howells * Set up the signature parameters in an X.509 certificate. This involves
23b426beb6SDavid Howells * digesting the signed data and extracting the signature.
24c26fd69fSDavid Howells */
x509_get_sig_params(struct x509_certificate * cert)25b426beb6SDavid Howells int x509_get_sig_params(struct x509_certificate *cert)
26c26fd69fSDavid Howells {
2777d0910dSDavid Howells struct public_key_signature *sig = cert->sig;
28c26fd69fSDavid Howells struct crypto_shash *tfm;
29c26fd69fSDavid Howells struct shash_desc *desc;
3077d0910dSDavid Howells size_t desc_size;
31c26fd69fSDavid Howells int ret;
32c26fd69fSDavid Howells
33c26fd69fSDavid Howells pr_devel("==>%s()\n", __func__);
34c26fd69fSDavid Howells
3577d0910dSDavid Howells sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
3677d0910dSDavid Howells if (!sig->s)
37b426beb6SDavid Howells return -ENOMEM;
38db6c43bdSTadeusz Struk
3977d0910dSDavid Howells sig->s_size = cert->raw_sig_size;
40b426beb6SDavid Howells
41c26fd69fSDavid Howells /* Allocate the hashing algorithm we're going to need and find out how
42c26fd69fSDavid Howells * big the hash operational data will be.
43c26fd69fSDavid Howells */
4477d0910dSDavid Howells tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
4541559420SDavid Howells if (IS_ERR(tfm)) {
4641559420SDavid Howells if (PTR_ERR(tfm) == -ENOENT) {
476c2dc5aeSDavid Howells cert->unsupported_sig = true;
486c2dc5aeSDavid Howells return 0;
4941559420SDavid Howells }
5041559420SDavid Howells return PTR_ERR(tfm);
5141559420SDavid Howells }
52c26fd69fSDavid Howells
53c26fd69fSDavid Howells desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
5477d0910dSDavid Howells sig->digest_size = crypto_shash_digestsize(tfm);
55c26fd69fSDavid Howells
56c26fd69fSDavid Howells ret = -ENOMEM;
5777d0910dSDavid Howells sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
5877d0910dSDavid Howells if (!sig->digest)
59b426beb6SDavid Howells goto error;
60c26fd69fSDavid Howells
6177d0910dSDavid Howells desc = kzalloc(desc_size, GFP_KERNEL);
6277d0910dSDavid Howells if (!desc)
6377d0910dSDavid Howells goto error;
64c26fd69fSDavid Howells
65c26fd69fSDavid Howells desc->tfm = tfm;
66c26fd69fSDavid Howells
67e5221fa6SHerbert Xu if (strcmp(cert->pub->pkey_algo, "sm2") == 0) {
68e5221fa6SHerbert Xu ret = strcmp(sig->hash_algo, "sm3") != 0 ? -EINVAL :
69e5221fa6SHerbert Xu crypto_shash_init(desc) ?:
70e5221fa6SHerbert Xu sm2_compute_z_digest(desc, cert->pub->key,
71e5221fa6SHerbert Xu cert->pub->keylen, sig->digest) ?:
72e5221fa6SHerbert Xu crypto_shash_init(desc) ?:
73e5221fa6SHerbert Xu crypto_shash_update(desc, sig->digest,
74e5221fa6SHerbert Xu sig->digest_size) ?:
75e5221fa6SHerbert Xu crypto_shash_finup(desc, cert->tbs, cert->tbs_size,
76e5221fa6SHerbert Xu sig->digest);
77e5221fa6SHerbert Xu } else {
78e5221fa6SHerbert Xu ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size,
79e5221fa6SHerbert Xu sig->digest);
80e5221fa6SHerbert Xu }
81e5221fa6SHerbert Xu
8243652956SDavid Howells if (ret < 0)
8343652956SDavid Howells goto error_2;
8443652956SDavid Howells
85141e5239SMickaël Salaün ret = is_hash_blacklisted(sig->digest, sig->digest_size,
86141e5239SMickaël Salaün BLACKLIST_HASH_X509_TBS);
8743652956SDavid Howells if (ret == -EKEYREJECTED) {
8843652956SDavid Howells pr_err("Cert %*phN is blacklisted\n",
8943652956SDavid Howells sig->digest_size, sig->digest);
9043652956SDavid Howells cert->blacklisted = true;
9143652956SDavid Howells ret = 0;
9243652956SDavid Howells }
9377d0910dSDavid Howells
9477d0910dSDavid Howells error_2:
9577d0910dSDavid Howells kfree(desc);
96c26fd69fSDavid Howells error:
97c26fd69fSDavid Howells crypto_free_shash(tfm);
98c26fd69fSDavid Howells pr_devel("<==%s() = %d\n", __func__, ret);
99c26fd69fSDavid Howells return ret;
100c26fd69fSDavid Howells }
101b426beb6SDavid Howells
102b426beb6SDavid Howells /*
1036c2dc5aeSDavid Howells * Check for self-signedness in an X.509 cert and if found, check the signature
1046c2dc5aeSDavid Howells * immediately if we can.
105b426beb6SDavid Howells */
x509_check_for_self_signed(struct x509_certificate * cert)1066c2dc5aeSDavid Howells int x509_check_for_self_signed(struct x509_certificate *cert)
107b426beb6SDavid Howells {
1086c2dc5aeSDavid Howells int ret = 0;
109b426beb6SDavid Howells
110b426beb6SDavid Howells pr_devel("==>%s()\n", __func__);
111b426beb6SDavid Howells
112ad3043fdSDavid Howells if (cert->raw_subject_size != cert->raw_issuer_size ||
113ad3043fdSDavid Howells memcmp(cert->raw_subject, cert->raw_issuer,
114ad3043fdSDavid Howells cert->raw_issuer_size) != 0)
115ad3043fdSDavid Howells goto not_self_signed;
116ad3043fdSDavid Howells
1176c2dc5aeSDavid Howells if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
1186c2dc5aeSDavid Howells /* If the AKID is present it may have one or two parts. If
1196c2dc5aeSDavid Howells * both are supplied, both must match.
1206c2dc5aeSDavid Howells */
1216c2dc5aeSDavid Howells bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
1226c2dc5aeSDavid Howells bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
1236c2dc5aeSDavid Howells
1246c2dc5aeSDavid Howells if (!a && !b)
1256c2dc5aeSDavid Howells goto not_self_signed;
1266c2dc5aeSDavid Howells
1276c2dc5aeSDavid Howells ret = -EKEYREJECTED;
1286c2dc5aeSDavid Howells if (((a && !b) || (b && !a)) &&
1296c2dc5aeSDavid Howells cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
1306c2dc5aeSDavid Howells goto out;
1316c2dc5aeSDavid Howells }
1326c2dc5aeSDavid Howells
133ef5b52a6SThore Sommer if (cert->unsupported_sig) {
134ef5b52a6SThore Sommer ret = 0;
135ef5b52a6SThore Sommer goto out;
136ef5b52a6SThore Sommer }
137ef5b52a6SThore Sommer
1386c2dc5aeSDavid Howells ret = public_key_verify_signature(cert->pub, cert->sig);
1396c2dc5aeSDavid Howells if (ret < 0) {
1406c2dc5aeSDavid Howells if (ret == -ENOPKG) {
1416c2dc5aeSDavid Howells cert->unsupported_sig = true;
1426c2dc5aeSDavid Howells ret = 0;
1436c2dc5aeSDavid Howells }
1446c2dc5aeSDavid Howells goto out;
1456c2dc5aeSDavid Howells }
1466c2dc5aeSDavid Howells
1476c2dc5aeSDavid Howells pr_devel("Cert Self-signature verified");
1486c2dc5aeSDavid Howells cert->self_signed = true;
1496c2dc5aeSDavid Howells
1506c2dc5aeSDavid Howells out:
1516c2dc5aeSDavid Howells pr_devel("<==%s() = %d\n", __func__, ret);
152b426beb6SDavid Howells return ret;
153b426beb6SDavid Howells
1546c2dc5aeSDavid Howells not_self_signed:
1556c2dc5aeSDavid Howells pr_devel("<==%s() = 0 [not]\n", __func__);
1566c2dc5aeSDavid Howells return 0;
157b426beb6SDavid Howells }
158c26fd69fSDavid Howells
159c26fd69fSDavid Howells /*
160c26fd69fSDavid Howells * Attempt to parse a data blob for a key as an X509 certificate.
161c26fd69fSDavid Howells */
x509_key_preparse(struct key_preparsed_payload * prep)162c26fd69fSDavid Howells static int x509_key_preparse(struct key_preparsed_payload *prep)
163c26fd69fSDavid Howells {
16446963b77SDavid Howells struct asymmetric_key_ids *kids;
165c26fd69fSDavid Howells struct x509_certificate *cert;
16646963b77SDavid Howells const char *q;
167c26fd69fSDavid Howells size_t srlen, sulen;
16846963b77SDavid Howells char *desc = NULL, *p;
169c26fd69fSDavid Howells int ret;
170c26fd69fSDavid Howells
171c26fd69fSDavid Howells cert = x509_cert_parse(prep->data, prep->datalen);
172c26fd69fSDavid Howells if (IS_ERR(cert))
173c26fd69fSDavid Howells return PTR_ERR(cert);
174c26fd69fSDavid Howells
175c26fd69fSDavid Howells pr_devel("Cert Issuer: %s\n", cert->issuer);
176c26fd69fSDavid Howells pr_devel("Cert Subject: %s\n", cert->subject);
1774e8ae72aSDavid Howells pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
178fd19a3d1SDavid Howells pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
179c26fd69fSDavid Howells
1804e8ae72aSDavid Howells cert->pub->id_type = "X509";
181c26fd69fSDavid Howells
182a511e1afSDavid Howells if (cert->unsupported_sig) {
1836c2dc5aeSDavid Howells public_key_signature_free(cert->sig);
1846c2dc5aeSDavid Howells cert->sig = NULL;
1856c2dc5aeSDavid Howells } else {
1866c2dc5aeSDavid Howells pr_devel("Cert Signature: %s + %s\n",
1876c2dc5aeSDavid Howells cert->sig->pkey_algo, cert->sig->hash_algo);
188c26fd69fSDavid Howells }
189c26fd69fSDavid Howells
19043652956SDavid Howells /* Don't permit addition of blacklisted keys */
19143652956SDavid Howells ret = -EKEYREJECTED;
19243652956SDavid Howells if (cert->blacklisted)
19343652956SDavid Howells goto error_free_cert;
19443652956SDavid Howells
195c26fd69fSDavid Howells /* Propose a description */
196c26fd69fSDavid Howells sulen = strlen(cert->subject);
197dd2f6c44SDavid Howells if (cert->raw_skid) {
198dd2f6c44SDavid Howells srlen = cert->raw_skid_size;
199dd2f6c44SDavid Howells q = cert->raw_skid;
200dd2f6c44SDavid Howells } else {
20146963b77SDavid Howells srlen = cert->raw_serial_size;
20246963b77SDavid Howells q = cert->raw_serial;
203dd2f6c44SDavid Howells }
20446963b77SDavid Howells
205c26fd69fSDavid Howells ret = -ENOMEM;
20646963b77SDavid Howells desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
207c26fd69fSDavid Howells if (!desc)
208c26fd69fSDavid Howells goto error_free_cert;
20946963b77SDavid Howells p = memcpy(desc, cert->subject, sulen);
21046963b77SDavid Howells p += sulen;
21146963b77SDavid Howells *p++ = ':';
21246963b77SDavid Howells *p++ = ' ';
21346963b77SDavid Howells p = bin2hex(p, q, srlen);
21446963b77SDavid Howells *p = 0;
21546963b77SDavid Howells
21646963b77SDavid Howells kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
21746963b77SDavid Howells if (!kids)
21846963b77SDavid Howells goto error_free_desc;
21946963b77SDavid Howells kids->id[0] = cert->id;
22046963b77SDavid Howells kids->id[1] = cert->skid;
2217d30198eSAndrew Zaborowski kids->id[2] = asymmetric_key_generate_id(cert->raw_subject,
2227d30198eSAndrew Zaborowski cert->raw_subject_size,
2237d30198eSAndrew Zaborowski "", 0);
2247d30198eSAndrew Zaborowski if (IS_ERR(kids->id[2])) {
2257d30198eSAndrew Zaborowski ret = PTR_ERR(kids->id[2]);
2267d30198eSAndrew Zaborowski goto error_free_kids;
2277d30198eSAndrew Zaborowski }
228c26fd69fSDavid Howells
229c26fd69fSDavid Howells /* We're pinning the module by being linked against it */
230c26fd69fSDavid Howells __module_get(public_key_subtype.owner);
231146aa8b1SDavid Howells prep->payload.data[asym_subtype] = &public_key_subtype;
232146aa8b1SDavid Howells prep->payload.data[asym_key_ids] = kids;
233146aa8b1SDavid Howells prep->payload.data[asym_crypto] = cert->pub;
23477d0910dSDavid Howells prep->payload.data[asym_auth] = cert->sig;
235c26fd69fSDavid Howells prep->description = desc;
236c26fd69fSDavid Howells prep->quotalen = 100;
237c26fd69fSDavid Howells
238c26fd69fSDavid Howells /* We've finished with the certificate */
239c26fd69fSDavid Howells cert->pub = NULL;
24046963b77SDavid Howells cert->id = NULL;
24146963b77SDavid Howells cert->skid = NULL;
24277d0910dSDavid Howells cert->sig = NULL;
243c26fd69fSDavid Howells desc = NULL;
2447d30198eSAndrew Zaborowski kids = NULL;
245c26fd69fSDavid Howells ret = 0;
246c26fd69fSDavid Howells
2477d30198eSAndrew Zaborowski error_free_kids:
2487d30198eSAndrew Zaborowski kfree(kids);
24946963b77SDavid Howells error_free_desc:
25046963b77SDavid Howells kfree(desc);
251c26fd69fSDavid Howells error_free_cert:
252c26fd69fSDavid Howells x509_free_certificate(cert);
253c26fd69fSDavid Howells return ret;
254c26fd69fSDavid Howells }
255c26fd69fSDavid Howells
256c26fd69fSDavid Howells static struct asymmetric_key_parser x509_key_parser = {
257c26fd69fSDavid Howells .owner = THIS_MODULE,
258c26fd69fSDavid Howells .name = "x509",
259c26fd69fSDavid Howells .parse = x509_key_preparse,
260c26fd69fSDavid Howells };
261c26fd69fSDavid Howells
262c26fd69fSDavid Howells /*
263c26fd69fSDavid Howells * Module stuff
264c26fd69fSDavid Howells */
x509_key_init(void)265c26fd69fSDavid Howells static int __init x509_key_init(void)
266c26fd69fSDavid Howells {
267*e71d8a3aSHerbert Xu return register_asymmetric_key_parser(&x509_key_parser);
268c26fd69fSDavid Howells }
269c26fd69fSDavid Howells
x509_key_exit(void)270c26fd69fSDavid Howells static void __exit x509_key_exit(void)
271c26fd69fSDavid Howells {
272c26fd69fSDavid Howells unregister_asymmetric_key_parser(&x509_key_parser);
273c26fd69fSDavid Howells }
274c26fd69fSDavid Howells
275c26fd69fSDavid Howells module_init(x509_key_init);
276c26fd69fSDavid Howells module_exit(x509_key_exit);
277e19aaa7dSKonstantin Khlebnikov
278e19aaa7dSKonstantin Khlebnikov MODULE_DESCRIPTION("X.509 certificate parser");
2791e684d38SDavid Howells MODULE_AUTHOR("Red Hat, Inc.");
280e19aaa7dSKonstantin Khlebnikov MODULE_LICENSE("GPL");
281