x509_public_key.c 6.5 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 23
#include <crypto/hash.h>
#include "asymmetric_keys.h"
#include "x509_parser.h"

/*
24 25
 * Set up the signature parameters in an X.509 certificate.  This involves
 * digesting the signed data and extracting the signature.
26
 */
27
int x509_get_sig_params(struct x509_certificate *cert)
28
{
29
	struct public_key_signature *sig = cert->sig;
30 31
	struct crypto_shash *tfm;
	struct shash_desc *desc;
32
	size_t desc_size;
33 34 35
	int ret;

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

37 38 39 40 41 42 43 44 45
	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;
46
		return 0;
47
	}
48

49 50
	sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
	if (!sig->s)
51
		return -ENOMEM;
52

53
	sig->s_size = cert->raw_sig_size;
54

55 56 57
	/* Allocate the hashing algorithm we're going to need and find out how
	 * big the hash operational data will be.
	 */
58
	tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
59 60
	if (IS_ERR(tfm)) {
		if (PTR_ERR(tfm) == -ENOENT) {
61 62
			cert->unsupported_sig = true;
			return 0;
63 64 65
		}
		return PTR_ERR(tfm);
	}
66 67

	desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
68
	sig->digest_size = crypto_shash_digestsize(tfm);
69 70

	ret = -ENOMEM;
71 72
	sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
	if (!sig->digest)
73
		goto error;
74

75 76 77
	desc = kzalloc(desc_size, GFP_KERNEL);
	if (!desc)
		goto error;
78

79 80
	desc->tfm = tfm;
	desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
81 82 83

	ret = crypto_shash_init(desc);
	if (ret < 0)
84
		goto error_2;
85
	might_sleep();
86
	ret = crypto_shash_finup(desc, cert->tbs, cert->tbs_size, sig->digest);
87 88 89 90 91 92 93 94 95 96
	if (ret < 0)
		goto error_2;

	ret = is_hash_blacklisted(sig->digest, sig->digest_size, "tbs");
	if (ret == -EKEYREJECTED) {
		pr_err("Cert %*phN is blacklisted\n",
		       sig->digest_size, sig->digest);
		cert->blacklisted = true;
		ret = 0;
	}
97 98 99

error_2:
	kfree(desc);
100 101 102 103 104
error:
	crypto_free_shash(tfm);
	pr_devel("<==%s() = %d\n", __func__, ret);
	return ret;
}
105

106
/*
107 108
 * Check for self-signedness in an X.509 cert and if found, check the signature
 * immediately if we can.
109
 */
110
int x509_check_for_self_signed(struct x509_certificate *cert)
111
{
112
	int ret = 0;
113

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

116 117 118 119 120
	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;

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	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;
	}

137 138 139 140
	ret = -EKEYREJECTED;
	if (cert->pub->pkey_algo != cert->sig->pkey_algo)
		goto out;

141 142 143 144 145 146 147 148 149 150 151
	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;
152

153 154
out:
	pr_devel("<==%s() = %d\n", __func__, ret);
155
	return ret;
156 157 158 159

not_self_signed:
	pr_devel("<==%s() = 0 [not]\n", __func__);
	return 0;
160 161 162 163 164 165 166
}

/*
 * Attempt to parse a data blob for a key as an X509 certificate.
 */
static int x509_key_preparse(struct key_preparsed_payload *prep)
{
167
	struct asymmetric_key_ids *kids;
168
	struct x509_certificate *cert;
169
	const char *q;
170
	size_t srlen, sulen;
171
	char *desc = NULL, *p;
172 173 174 175 176 177 178 179
	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);
180

181
	if (cert->unsupported_key) {
182 183 184 185
		ret = -ENOPKG;
		goto error_free_cert;
	}

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

189
	cert->pub->id_type = "X509";
190

191
	if (cert->unsupported_sig) {
192 193 194 195 196
		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);
197 198
	}

199 200 201 202 203
	/* Don't permit addition of blacklisted keys */
	ret = -EKEYREJECTED;
	if (cert->blacklisted)
		goto error_free_cert;

204 205
	/* Propose a description */
	sulen = strlen(cert->subject);
206 207 208 209 210 211 212
	if (cert->raw_skid) {
		srlen = cert->raw_skid_size;
		q = cert->raw_skid;
	} else {
		srlen = cert->raw_serial_size;
		q = cert->raw_serial;
	}
213

214
	ret = -ENOMEM;
215
	desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
216 217
	if (!desc)
		goto error_free_cert;
218 219 220 221 222 223 224 225 226 227 228 229
	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;
230 231 232

	/* We're pinning the module by being linked against it */
	__module_get(public_key_subtype.owner);
233 234 235
	prep->payload.data[asym_subtype] = &public_key_subtype;
	prep->payload.data[asym_key_ids] = kids;
	prep->payload.data[asym_crypto] = cert->pub;
236
	prep->payload.data[asym_auth] = cert->sig;
237 238 239 240 241
	prep->description = desc;
	prep->quotalen = 100;

	/* We've finished with the certificate */
	cert->pub = NULL;
242 243
	cert->id = NULL;
	cert->skid = NULL;
244
	cert->sig = NULL;
245 246 247
	desc = NULL;
	ret = 0;

248 249
error_free_desc:
	kfree(desc);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
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);
276 277 278

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