x509_public_key.c 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Instantiate a public key crypto key from an X.509 Certificate
 *
 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */

#define pr_fmt(fmt) "X.509: "fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <keys/asymmetric-subtype.h>
#include <keys/asymmetric-parser.h>
18
#include <keys/system_keyring.h>
19 20 21 22
#include <crypto/hash.h>
#include "asymmetric_keys.h"
#include "x509_parser.h"

23
static bool use_builtin_keys;
24
static struct asymmetric_key_id *ca_keyid;
25 26

#ifndef MODULE
27 28 29 30 31
static struct {
	struct asymmetric_key_id id;
	unsigned char data[10];
} cakey;

32 33 34 35 36
static int __init ca_keys_setup(char *str)
{
	if (!str)		/* default system keyring */
		return 1;

37
	if (strncmp(str, "id:", 3) == 0) {
38 39 40 41 42 43 44 45 46 47 48 49 50
		struct asymmetric_key_id *p = &cakey.id;
		size_t hexlen = (strlen(str) - 3) / 2;
		int ret;

		if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
			pr_err("Missing or invalid ca_keys id\n");
			return 1;
		}

		ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
		if (ret < 0)
			pr_err("Unparsable ca_keys id hex string\n");
		else
51 52
			ca_keyid = p;	/* owner key 'id:xxxxxx' */
	} else if (strcmp(str, "builtin") == 0) {
53
		use_builtin_keys = true;
54
	}
55 56 57 58 59 60

	return 1;
}
__setup("ca_keys=", ca_keys_setup);
#endif

61
/*
62 63
 * Set up the signature parameters in an X.509 certificate.  This involves
 * digesting the signed data and extracting the signature.
64
 */
65
int x509_get_sig_params(struct x509_certificate *cert)
66
{
67
	struct public_key_signature *sig = cert->sig;
68 69
	struct crypto_shash *tfm;
	struct shash_desc *desc;
70
	size_t desc_size;
71 72 73
	int ret;

	pr_devel("==>%s()\n", __func__);
74

75 76 77 78 79 80 81 82 83
	if (!cert->pub->pkey_algo)
		cert->unsupported_key = true;

	if (!sig->pkey_algo)
		cert->unsupported_sig = true;

	/* We check the hash if we can - even if we can't then verify it */
	if (!sig->hash_algo) {
		cert->unsupported_sig = true;
84
		return 0;
85
	}
86

87 88
	sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
	if (!sig->s)
89
		return -ENOMEM;
90

91
	sig->s_size = cert->raw_sig_size;
92

93 94 95
	/* Allocate the hashing algorithm we're going to need and find out how
	 * big the hash operational data will be.
	 */
96
	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
97 98
	if (IS_ERR(tfm)) {
		if (PTR_ERR(tfm) == -ENOENT) {
99 100
			cert->unsupported_sig = true;
			return 0;
101 102 103
		}
		return PTR_ERR(tfm);
	}
104 105

	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
106
	sig->digest_size = crypto_shash_digestsize(tfm);
107 108

	ret = -ENOMEM;
109 110
	sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
	if (!sig->digest)
111
		goto error;
112

113 114 115
	desc = kzalloc(desc_size, GFP_KERNEL);
	if (!desc)
		goto error;
116

117 118
	desc->tfm = tfm;
	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
119 120 121

	ret = crypto_shash_init(desc);
	if (ret < 0)
122
		goto error_2;
123
	might_sleep();
124 125 126 127
	ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);

error_2:
	kfree(desc);
128 129 130 131 132
error:
	crypto_free_shash(tfm);
	pr_devel("<==%s() = %d\n", __func__, ret);
	return ret;
}
133

134
/*
135 136
 * Check for self-signedness in an X.509 cert and if found, check the signature
 * immediately if we can.
137
 */
138
int x509_check_for_self_signed(struct x509_certificate *cert)
139
{
140
	int ret = 0;
141

142
	pr_devel("==>%s()\n", __func__);
143

144 145 146 147 148
	if (cert->raw_subject_size != cert->raw_issuer_size ||
	    memcmp(cert->raw_subject, cert->raw_issuer,
		   cert->raw_issuer_size) != 0)
		goto not_self_signed;

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
		/* If the AKID is present it may have one or two parts.  If
		 * both are supplied, both must match.
		 */
		bool a = asymmetric_key_id_same(cert->skid, cert->sig->auth_ids[1]);
		bool b = asymmetric_key_id_same(cert->id, cert->sig->auth_ids[0]);

		if (!a && !b)
			goto not_self_signed;

		ret = -EKEYREJECTED;
		if (((a && !b) || (b && !a)) &&
		    cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
			goto out;
	}

165 166 167 168
	ret = -EKEYREJECTED;
	if (cert->pub->pkey_algo != cert->sig->pkey_algo)
		goto out;

169 170 171 172 173 174 175 176 177 178 179
	ret = public_key_verify_signature(cert->pub, cert->sig);
	if (ret < 0) {
		if (ret == -ENOPKG) {
			cert->unsupported_sig = true;
			ret = 0;
		}
		goto out;
	}

	pr_devel("Cert Self-signature verified");
	cert->self_signed = true;
180

181 182
out:
	pr_devel("<==%s() = %d\n", __func__, ret);
183
	return ret;
184 185 186 187

not_self_signed:
	pr_devel("<==%s() = 0 [not]\n", __func__);
	return 0;
188 189
}

190 191 192 193 194 195 196 197 198 199 200 201
/*
 * Check the new certificate against the ones in the trust keyring.  If one of
 * those is the signing key and validates the new certificate, then mark the
 * new certificate as being trusted.
 *
 * Return 0 if the new certificate was successfully validated, 1 if we couldn't
 * find a matching parent certificate in the trusted list and an error if there
 * is a matching certificate but the signature check fails.
 */
static int x509_validate_trust(struct x509_certificate *cert,
			       struct key *trust_keyring)
{
202
	struct public_key_signature *sig = cert->sig;
203 204 205
	struct key *key;
	int ret = 1;

206 207 208
	if (!sig->auth_ids[0] && !sig->auth_ids[1])
		return 1;

209 210
	if (!trust_keyring)
		return -EOPNOTSUPP;
211
	if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
212
		return -EPERM;
213 214
	if (cert->unsupported_sig)
		return -ENOPKG;
215

216 217
	key = find_asymmetric_key(trust_keyring,
				  sig->auth_ids[0], sig->auth_ids[1], false);
218 219 220 221 222 223 224 225 226
	if (IS_ERR(key))
		return PTR_ERR(key);

	if (!use_builtin_keys ||
	    test_bit(KEY_FLAG_BUILTIN, &key->flags)) {
		ret = public_key_verify_signature(
			key->payload.data[asym_crypto], cert->sig);
		if (ret == -ENOPKG)
			cert->unsupported_sig = true;
227
	}
228
	key_put(key);
229 230 231
	return ret;
}

232 233 234 235 236
/*
 * Attempt to parse a data blob for a key as an X509 certificate.
 */
static int x509_key_preparse(struct key_preparsed_payload *prep)
{
237
	struct asymmetric_key_ids *kids;
238
	struct x509_certificate *cert;
239
	const char *q;
240
	size_t srlen, sulen;
241
	char *desc = NULL, *p;
242 243 244 245 246 247 248 249
	int ret;

	cert = x509_cert_parse(prep->data, prep->datalen);
	if (IS_ERR(cert))
		return PTR_ERR(cert);

	pr_devel("Cert Issuer: %s\n", cert->issuer);
	pr_devel("Cert Subject: %s\n", cert->subject);
250

251
	if (cert->unsupported_key) {
252 253 254 255
		ret = -ENOPKG;
		goto error_free_cert;
	}

256
	pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
257
	pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
258

259
	cert->pub->id_type = "X509";
260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
	/* See if we can derive the trustability of this certificate.
	 *
	 * When it comes to self-signed certificates, we cannot evaluate
	 * trustedness except by the fact that we obtained it from a trusted
	 * location.  So we just rely on x509_validate_trust() failing in this
	 * case.
	 *
	 * Note that there's a possibility of a self-signed cert matching a
	 * cert that we have (most likely a duplicate that we already trust) -
	 * in which case it will be marked trusted.
	 */
	if (cert->unsupported_sig || cert->self_signed) {
		public_key_signature_free(cert->sig);
		cert->sig = NULL;
	} else {
		pr_devel("Cert Signature: %s + %s\n",
			 cert->sig->pkey_algo, cert->sig->hash_algo);

279
		ret = x509_validate_trust(cert, get_system_trusted_keyring());
280 281
		if (ret)
			ret = x509_validate_trust(cert, get_ima_mok_keyring());
282 283
		if (ret == -EKEYREJECTED)
			goto error_free_cert;
284
		if (!ret)
285
			prep->trusted = true;
286 287 288 289
	}

	/* Propose a description */
	sulen = strlen(cert->subject);
290 291 292 293 294 295 296
	if (cert->raw_skid) {
		srlen = cert->raw_skid_size;
		q = cert->raw_skid;
	} else {
		srlen = cert->raw_serial_size;
		q = cert->raw_serial;
	}
297

298
	ret = -ENOMEM;
299
	desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
300 301
	if (!desc)
		goto error_free_cert;
302 303 304 305 306 307 308 309 310 311 312 313
	p = memcpy(desc, cert->subject, sulen);
	p += sulen;
	*p++ = ':';
	*p++ = ' ';
	p = bin2hex(p, q, srlen);
	*p = 0;

	kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
	if (!kids)
		goto error_free_desc;
	kids->id[0] = cert->id;
	kids->id[1] = cert->skid;
314 315 316

	/* We're pinning the module by being linked against it */
	__module_get(public_key_subtype.owner);
317 318 319
	prep->payload.data[asym_subtype] = &public_key_subtype;
	prep->payload.data[asym_key_ids] = kids;
	prep->payload.data[asym_crypto] = cert->pub;
320
	prep->payload.data[asym_auth] = cert->sig;
321 322 323 324 325
	prep->description = desc;
	prep->quotalen = 100;

	/* We've finished with the certificate */
	cert->pub = NULL;
326 327
	cert->id = NULL;
	cert->skid = NULL;
328
	cert->sig = NULL;
329 330 331
	desc = NULL;
	ret = 0;

332 333
error_free_desc:
	kfree(desc);
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
error_free_cert:
	x509_free_certificate(cert);
	return ret;
}

static struct asymmetric_key_parser x509_key_parser = {
	.owner	= THIS_MODULE,
	.name	= "x509",
	.parse	= x509_key_preparse,
};

/*
 * Module stuff
 */
static int __init x509_key_init(void)
{
	return register_asymmetric_key_parser(&x509_key_parser);
}

static void __exit x509_key_exit(void)
{
	unregister_asymmetric_key_parser(&x509_key_parser);
}

module_init(x509_key_init);
module_exit(x509_key_exit);
360 361 362

MODULE_DESCRIPTION("X.509 certificate parser");
MODULE_LICENSE("GPL");