Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* 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)
  6 */
  7
  8#define pr_fmt(fmt) "X.509: "fmt
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/slab.h>
 12#include <keys/asymmetric-subtype.h>
 13#include <keys/asymmetric-parser.h>
 14#include <keys/system_keyring.h>
 15#include <crypto/hash.h>
 16#include "asymmetric_keys.h"
 17#include "x509_parser.h"
 18
 19/*
 20 * Set up the signature parameters in an X.509 certificate.  This involves
 21 * digesting the signed data and extracting the signature.
 22 */
 23int x509_get_sig_params(struct x509_certificate *cert)
 24{
 25	struct public_key_signature *sig = cert->sig;
 26	struct crypto_shash *tfm;
 27	struct shash_desc *desc;
 28	size_t desc_size;
 29	int ret;
 30
 31	pr_devel("==>%s()\n", __func__);
 32
 33	sig->data = cert->tbs;
 34	sig->data_size = cert->tbs_size;
 35
 
 
 
 
 
 
 
 
 
 
 
 
 36	sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
 37	if (!sig->s)
 38		return -ENOMEM;
 39
 40	sig->s_size = cert->raw_sig_size;
 41
 42	/* Allocate the hashing algorithm we're going to need and find out how
 43	 * big the hash operational data will be.
 44	 */
 45	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
 46	if (IS_ERR(tfm)) {
 47		if (PTR_ERR(tfm) == -ENOENT) {
 48			cert->unsupported_sig = true;
 49			return 0;
 50		}
 51		return PTR_ERR(tfm);
 52	}
 53
 54	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
 55	sig->digest_size = crypto_shash_digestsize(tfm);
 56
 57	ret = -ENOMEM;
 58	sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
 59	if (!sig->digest)
 60		goto error;
 61
 62	desc = kzalloc(desc_size, GFP_KERNEL);
 63	if (!desc)
 64		goto error;
 65
 66	desc->tfm = tfm;
 67
 68	ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
 69	if (ret < 0)
 70		goto error_2;
 71
 72	ret = is_hash_blacklisted(sig->digest, sig->digest_size,
 73				  BLACKLIST_HASH_X509_TBS);
 74	if (ret == -EKEYREJECTED) {
 75		pr_err("Cert %*phN is blacklisted\n",
 76		       sig->digest_size, sig->digest);
 77		cert->blacklisted = true;
 78		ret = 0;
 79	}
 80
 81error_2:
 82	kfree(desc);
 83error:
 84	crypto_free_shash(tfm);
 85	pr_devel("<==%s() = %d\n", __func__, ret);
 86	return ret;
 87}
 88
 89/*
 90 * Check for self-signedness in an X.509 cert and if found, check the signature
 91 * immediately if we can.
 92 */
 93int x509_check_for_self_signed(struct x509_certificate *cert)
 94{
 95	int ret = 0;
 96
 97	pr_devel("==>%s()\n", __func__);
 98
 99	if (cert->raw_subject_size != cert->raw_issuer_size ||
100	    memcmp(cert->raw_subject, cert->raw_issuer,
101		   cert->raw_issuer_size) != 0)
102		goto not_self_signed;
103
104	if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
105		/* If the AKID is present it may have one or two parts.  If
106		 * both are supplied, both must match.
107		 */
108		bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
109		bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);
110
111		if (!a && !b)
112			goto not_self_signed;
113
114		ret = -EKEYREJECTED;
115		if (((a && !b) || (b && !a)) &&
116		    cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
117			goto out;
118	}
119
 
 
 
 
 
 
120	ret = public_key_verify_signature(cert->pub, cert->sig);
121	if (ret < 0) {
122		if (ret == -ENOPKG) {
123			cert->unsupported_sig = true;
124			ret = 0;
125		}
126		goto out;
127	}
128
129	pr_devel("Cert Self-signature verified");
130	cert->self_signed = true;
131
132out:
133	pr_devel("<==%s() = %d\n", __func__, ret);
134	return ret;
135
136not_self_signed:
137	pr_devel("<==%s() = 0 [not]\n", __func__);
138	return 0;
139}
140
141/*
142 * Attempt to parse a data blob for a key as an X509 certificate.
143 */
144static int x509_key_preparse(struct key_preparsed_payload *prep)
145{
146	struct asymmetric_key_ids *kids;
147	struct x509_certificate *cert;
148	const char *q;
149	size_t srlen, sulen;
150	char *desc = NULL, *p;
151	int ret;
152
153	cert = x509_cert_parse(prep->data, prep->datalen);
154	if (IS_ERR(cert))
155		return PTR_ERR(cert);
156
157	pr_devel("Cert Issuer: %s\n", cert->issuer);
158	pr_devel("Cert Subject: %s\n", cert->subject);
 
 
 
 
 
 
159	pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
160	pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
161
162	cert->pub->id_type = "X509";
163
164	if (cert->unsupported_sig) {
165		public_key_signature_free(cert->sig);
166		cert->sig = NULL;
167	} else {
168		pr_devel("Cert Signature: %s + %s\n",
169			 cert->sig->pkey_algo, cert->sig->hash_algo);
170	}
171
172	/* Don't permit addition of blacklisted keys */
173	ret = -EKEYREJECTED;
174	if (cert->blacklisted)
175		goto error_free_cert;
176
177	/* Propose a description */
178	sulen = strlen(cert->subject);
179	if (cert->raw_skid) {
180		srlen = cert->raw_skid_size;
181		q = cert->raw_skid;
182	} else {
183		srlen = cert->raw_serial_size;
184		q = cert->raw_serial;
185	}
186
187	ret = -ENOMEM;
188	desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
189	if (!desc)
190		goto error_free_cert;
191	p = memcpy(desc, cert->subject, sulen);
192	p += sulen;
193	*p++ = ':';
194	*p++ = ' ';
195	p = bin2hex(p, q, srlen);
196	*p = 0;
197
198	kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
199	if (!kids)
200		goto error_free_desc;
201	kids->id[0] = cert->id;
202	kids->id[1] = cert->skid;
203	kids->id[2] = asymmetric_key_generate_id(cert->raw_subject,
204						 cert->raw_subject_size,
205						 "", 0);
206	if (IS_ERR(kids->id[2])) {
207		ret = PTR_ERR(kids->id[2]);
208		goto error_free_kids;
209	}
210
211	/* We're pinning the module by being linked against it */
212	__module_get(public_key_subtype.owner);
213	prep->payload.data[asym_subtype] = &public_key_subtype;
214	prep->payload.data[asym_key_ids] = kids;
215	prep->payload.data[asym_crypto] = cert->pub;
216	prep->payload.data[asym_auth] = cert->sig;
217	prep->description = desc;
218	prep->quotalen = 100;
219
220	/* We've finished with the certificate */
221	cert->pub = NULL;
222	cert->id = NULL;
223	cert->skid = NULL;
224	cert->sig = NULL;
225	desc = NULL;
226	kids = NULL;
227	ret = 0;
228
229error_free_kids:
230	kfree(kids);
231error_free_desc:
232	kfree(desc);
233error_free_cert:
234	x509_free_certificate(cert);
235	return ret;
236}
237
238static struct asymmetric_key_parser x509_key_parser = {
239	.owner	= THIS_MODULE,
240	.name	= "x509",
241	.parse	= x509_key_preparse,
242};
243
244/*
245 * Module stuff
246 */
247extern int __init certs_selftest(void);
248static int __init x509_key_init(void)
249{
250	int ret;
251
252	ret = register_asymmetric_key_parser(&x509_key_parser);
253	if (ret < 0)
254		return ret;
255	return fips_signature_selftest();
256}
257
258static void __exit x509_key_exit(void)
259{
260	unregister_asymmetric_key_parser(&x509_key_parser);
261}
262
263module_init(x509_key_init);
264module_exit(x509_key_exit);
265
266MODULE_DESCRIPTION("X.509 certificate parser");
267MODULE_AUTHOR("Red Hat, Inc.");
268MODULE_LICENSE("GPL");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* 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)
  6 */
  7
  8#define pr_fmt(fmt) "X.509: "fmt
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/slab.h>
 12#include <keys/asymmetric-subtype.h>
 13#include <keys/asymmetric-parser.h>
 14#include <keys/system_keyring.h>
 15#include <crypto/hash.h>
 16#include "asymmetric_keys.h"
 17#include "x509_parser.h"
 18
 19/*
 20 * Set up the signature parameters in an X.509 certificate.  This involves
 21 * digesting the signed data and extracting the signature.
 22 */
 23int x509_get_sig_params(struct x509_certificate *cert)
 24{
 25	struct public_key_signature *sig = cert->sig;
 26	struct crypto_shash *tfm;
 27	struct shash_desc *desc;
 28	size_t desc_size;
 29	int ret;
 30
 31	pr_devel("==>%s()\n", __func__);
 32
 33	sig->data = cert->tbs;
 34	sig->data_size = cert->tbs_size;
 35
 36	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;
 45		return 0;
 46	}
 47
 48	sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
 49	if (!sig->s)
 50		return -ENOMEM;
 51
 52	sig->s_size = cert->raw_sig_size;
 53
 54	/* Allocate the hashing algorithm we're going to need and find out how
 55	 * big the hash operational data will be.
 56	 */
 57	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
 58	if (IS_ERR(tfm)) {
 59		if (PTR_ERR(tfm) == -ENOENT) {
 60			cert->unsupported_sig = true;
 61			return 0;
 62		}
 63		return PTR_ERR(tfm);
 64	}
 65
 66	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
 67	sig->digest_size = crypto_shash_digestsize(tfm);
 68
 69	ret = -ENOMEM;
 70	sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
 71	if (!sig->digest)
 72		goto error;
 73
 74	desc = kzalloc(desc_size, GFP_KERNEL);
 75	if (!desc)
 76		goto error;
 77
 78	desc->tfm = tfm;
 79
 80	ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
 81	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	}
 91
 92error_2:
 93	kfree(desc);
 94error:
 95	crypto_free_shash(tfm);
 96	pr_devel("<==%s() = %d\n", __func__, ret);
 97	return ret;
 98}
 99
100/*
101 * Check for self-signedness in an X.509 cert and if found, check the signature
102 * immediately if we can.
103 */
104int x509_check_for_self_signed(struct x509_certificate *cert)
105{
106	int ret = 0;
107
108	pr_devel("==>%s()\n", __func__);
109
110	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
115	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]);
121
122		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
131	ret = -EKEYREJECTED;
132	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))
135		goto out;
136
137	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);
151	return ret;
152
153not_self_signed:
154	pr_devel("<==%s() = 0 [not]\n", __func__);
155	return 0;
156}
157
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{
163	struct asymmetric_key_ids *kids;
164	struct x509_certificate *cert;
165	const char *q;
166	size_t srlen, sulen;
167	char *desc = NULL, *p;
168	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);
176
177	if (cert->unsupported_key) {
178		ret = -ENOPKG;
179		goto error_free_cert;
180	}
181
182	pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
183	pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
184
185	cert->pub->id_type = "X509";
186
187	if (cert->unsupported_sig) {
188		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);
193	}
194
195	/* Don't permit addition of blacklisted keys */
196	ret = -EKEYREJECTED;
197	if (cert->blacklisted)
198		goto error_free_cert;
199
200	/* Propose a description */
201	sulen = strlen(cert->subject);
202	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	}
209
210	ret = -ENOMEM;
211	desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
212	if (!desc)
213		goto error_free_cert;
214	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;
 
 
 
 
 
 
 
226
227	/* We're pinning the module by being linked against it */
228	__module_get(public_key_subtype.owner);
229	prep->payload.data[asym_subtype] = &public_key_subtype;
230	prep->payload.data[asym_key_ids] = kids;
231	prep->payload.data[asym_crypto] = cert->pub;
232	prep->payload.data[asym_auth] = cert->sig;
233	prep->description = desc;
234	prep->quotalen = 100;
235
236	/* We've finished with the certificate */
237	cert->pub = NULL;
238	cert->id = NULL;
239	cert->skid = NULL;
240	cert->sig = NULL;
241	desc = NULL;
 
242	ret = 0;
243
 
 
244error_free_desc:
245	kfree(desc);
246error_free_cert:
247	x509_free_certificate(cert);
248	return ret;
249}
250
251static struct asymmetric_key_parser x509_key_parser = {
252	.owner	= THIS_MODULE,
253	.name	= "x509",
254	.parse	= x509_key_preparse,
255};
256
257/*
258 * Module stuff
259 */
 
260static int __init x509_key_init(void)
261{
262	return register_asymmetric_key_parser(&x509_key_parser);
 
 
 
 
 
263}
264
265static void __exit x509_key_exit(void)
266{
267	unregister_asymmetric_key_parser(&x509_key_parser);
268}
269
270module_init(x509_key_init);
271module_exit(x509_key_exit);
272
273MODULE_DESCRIPTION("X.509 certificate parser");
274MODULE_AUTHOR("Red Hat, Inc.");
275MODULE_LICENSE("GPL");