Linux Audio

Check our new training course

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