2012-09-24 20:11:48 +04:00
|
|
|
/* 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>
|
2013-08-20 22:36:27 +04:00
|
|
|
#include <keys/system_keyring.h>
|
2012-09-24 20:11:48 +04:00
|
|
|
#include <crypto/hash.h>
|
|
|
|
#include "asymmetric_keys.h"
|
|
|
|
#include "x509_parser.h"
|
|
|
|
|
|
|
|
/*
|
2013-08-30 19:18:02 +04:00
|
|
|
* Set up the signature parameters in an X.509 certificate. This involves
|
|
|
|
* digesting the signed data and extracting the signature.
|
2012-09-24 20:11:48 +04:00
|
|
|
*/
|
2013-08-30 19:18:02 +04:00
|
|
|
int x509_get_sig_params(struct x509_certificate *cert)
|
2012-09-24 20:11:48 +04:00
|
|
|
{
|
2016-04-06 18:13:33 +03:00
|
|
|
struct public_key_signature *sig = cert->sig;
|
2012-09-24 20:11:48 +04:00
|
|
|
struct crypto_shash *tfm;
|
|
|
|
struct shash_desc *desc;
|
2016-04-06 18:13:33 +03:00
|
|
|
size_t desc_size;
|
2012-09-24 20:11:48 +04:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
pr_devel("==>%s()\n", __func__);
|
2013-08-30 19:18:02 +04:00
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
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;
|
2013-08-30 19:18:02 +04:00
|
|
|
return 0;
|
2016-04-06 18:13:34 +03:00
|
|
|
}
|
2013-08-30 19:18:02 +04:00
|
|
|
|
2016-04-06 18:13:33 +03:00
|
|
|
sig->s = kmemdup(cert->raw_sig, cert->raw_sig_size, GFP_KERNEL);
|
|
|
|
if (!sig->s)
|
2013-08-30 19:18:02 +04:00
|
|
|
return -ENOMEM;
|
2016-02-02 21:08:53 +03:00
|
|
|
|
2016-04-06 18:13:33 +03:00
|
|
|
sig->s_size = cert->raw_sig_size;
|
2013-08-30 19:18:02 +04:00
|
|
|
|
2012-09-24 20:11:48 +04:00
|
|
|
/* Allocate the hashing algorithm we're going to need and find out how
|
|
|
|
* big the hash operational data will be.
|
|
|
|
*/
|
2016-04-06 18:13:33 +03:00
|
|
|
tfm = crypto_alloc_shash(sig->hash_algo, 0, 0);
|
2014-09-16 20:36:15 +04:00
|
|
|
if (IS_ERR(tfm)) {
|
|
|
|
if (PTR_ERR(tfm) == -ENOENT) {
|
2016-04-06 18:13:34 +03:00
|
|
|
cert->unsupported_sig = true;
|
|
|
|
return 0;
|
2014-09-16 20:36:15 +04:00
|
|
|
}
|
|
|
|
return PTR_ERR(tfm);
|
|
|
|
}
|
2012-09-24 20:11:48 +04:00
|
|
|
|
|
|
|
desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
|
2016-04-06 18:13:33 +03:00
|
|
|
sig->digest_size = crypto_shash_digestsize(tfm);
|
2012-09-24 20:11:48 +04:00
|
|
|
|
|
|
|
ret = -ENOMEM;
|
2016-04-06 18:13:33 +03:00
|
|
|
sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
|
|
|
|
if (!sig->digest)
|
2013-08-30 19:18:02 +04:00
|
|
|
goto error;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2016-04-06 18:13:33 +03:00
|
|
|
desc = kzalloc(desc_size, GFP_KERNEL);
|
|
|
|
if (!desc)
|
|
|
|
goto error;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2013-08-30 19:18:02 +04:00
|
|
|
desc->tfm = tfm;
|
|
|
|
desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2017-12-08 18:13:29 +03:00
|
|
|
ret = crypto_shash_digest(desc, cert->tbs, cert->tbs_size, sig->digest);
|
2017-04-03 18:07:25 +03:00
|
|
|
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;
|
|
|
|
}
|
2016-04-06 18:13:33 +03:00
|
|
|
|
|
|
|
error_2:
|
|
|
|
kfree(desc);
|
2013-08-30 19:18:02 +04:00
|
|
|
error:
|
|
|
|
crypto_free_shash(tfm);
|
|
|
|
pr_devel("<==%s() = %d\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2013-08-30 19:18:02 +04:00
|
|
|
/*
|
2016-04-06 18:13:34 +03:00
|
|
|
* Check for self-signedness in an X.509 cert and if found, check the signature
|
|
|
|
* immediately if we can.
|
2013-08-30 19:18:02 +04:00
|
|
|
*/
|
2016-04-06 18:13:34 +03:00
|
|
|
int x509_check_for_self_signed(struct x509_certificate *cert)
|
2013-08-30 19:18:02 +04:00
|
|
|
{
|
2016-04-06 18:13:34 +03:00
|
|
|
int ret = 0;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2013-08-30 19:18:02 +04:00
|
|
|
pr_devel("==>%s()\n", __func__);
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
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;
|
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
ret = -EKEYREJECTED;
|
2017-12-08 18:13:29 +03:00
|
|
|
if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)
|
2016-04-06 18:13:34 +03:00
|
|
|
goto out;
|
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
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;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
out:
|
|
|
|
pr_devel("<==%s() = %d\n", __func__, ret);
|
2012-09-24 20:11:48 +04:00
|
|
|
return ret;
|
2016-04-06 18:13:34 +03:00
|
|
|
|
|
|
|
not_self_signed:
|
|
|
|
pr_devel("<==%s() = 0 [not]\n", __func__);
|
|
|
|
return 0;
|
2012-09-24 20:11:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attempt to parse a data blob for a key as an X509 certificate.
|
|
|
|
*/
|
|
|
|
static int x509_key_preparse(struct key_preparsed_payload *prep)
|
|
|
|
{
|
2014-09-16 20:36:13 +04:00
|
|
|
struct asymmetric_key_ids *kids;
|
2012-09-24 20:11:48 +04:00
|
|
|
struct x509_certificate *cert;
|
2014-09-16 20:36:13 +04:00
|
|
|
const char *q;
|
2012-09-24 20:11:48 +04:00
|
|
|
size_t srlen, sulen;
|
2014-09-16 20:36:13 +04:00
|
|
|
char *desc = NULL, *p;
|
2012-09-24 20:11:48 +04:00
|
|
|
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);
|
2013-08-30 19:18:15 +04:00
|
|
|
|
2016-04-06 18:13:34 +03:00
|
|
|
if (cert->unsupported_key) {
|
2013-08-30 19:18:15 +04:00
|
|
|
ret = -ENOPKG;
|
|
|
|
goto error_free_cert;
|
|
|
|
}
|
|
|
|
|
2016-03-04 00:49:27 +03:00
|
|
|
pr_devel("Cert Key Algo: %s\n", cert->pub->pkey_algo);
|
2015-07-29 18:58:32 +03:00
|
|
|
pr_devel("Cert Valid period: %lld-%lld\n", cert->valid_from, cert->valid_to);
|
2012-09-24 20:11:48 +04:00
|
|
|
|
2016-03-04 00:49:27 +03:00
|
|
|
cert->pub->id_type = "X509";
|
2012-09-24 20:11:48 +04:00
|
|
|
|
KEYS: Move the point of trust determination to __key_link()
Move the point at which a key is determined to be trustworthy to
__key_link() so that we use the contents of the keyring being linked in to
to determine whether the key being linked in is trusted or not.
What is 'trusted' then becomes a matter of what's in the keyring.
Currently, the test is done when the key is parsed, but given that at that
point we can only sensibly refer to the contents of the system trusted
keyring, we can only use that as the basis for working out the
trustworthiness of a new key.
With this change, a trusted keyring is a set of keys that once the
trusted-only flag is set cannot be added to except by verification through
one of the contained keys.
Further, adding a key into a trusted keyring, whilst it might grant
trustworthiness in the context of that keyring, does not automatically
grant trustworthiness in the context of a second keyring to which it could
be secondarily linked.
To accomplish this, the authentication data associated with the key source
must now be retained. For an X.509 cert, this means the contents of the
AuthorityKeyIdentifier and the signature data.
If system keyrings are disabled then restrict_link_by_builtin_trusted()
resolves to restrict_link_reject(). The integrity digital signature code
still works correctly with this as it was previously using
KEY_FLAG_TRUSTED_ONLY, which doesn't permit anything to be added if there
is no system keyring against which trust can be determined.
Signed-off-by: David Howells <dhowells@redhat.com>
2016-04-06 18:14:26 +03:00
|
|
|
if (cert->unsupported_sig) {
|
2016-04-06 18:13:34 +03:00
|
|
|
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);
|
2012-09-24 20:11:48 +04:00
|
|
|
}
|
|
|
|
|
2017-04-03 18:07:25 +03:00
|
|
|
/* Don't permit addition of blacklisted keys */
|
|
|
|
ret = -EKEYREJECTED;
|
|
|
|
if (cert->blacklisted)
|
|
|
|
goto error_free_cert;
|
|
|
|
|
2012-09-24 20:11:48 +04:00
|
|
|
/* Propose a description */
|
|
|
|
sulen = strlen(cert->subject);
|
2014-10-03 19:17:02 +04:00
|
|
|
if (cert->raw_skid) {
|
|
|
|
srlen = cert->raw_skid_size;
|
|
|
|
q = cert->raw_skid;
|
|
|
|
} else {
|
|
|
|
srlen = cert->raw_serial_size;
|
|
|
|
q = cert->raw_serial;
|
|
|
|
}
|
2014-09-16 20:36:13 +04:00
|
|
|
|
2012-09-24 20:11:48 +04:00
|
|
|
ret = -ENOMEM;
|
2014-09-16 20:36:13 +04:00
|
|
|
desc = kmalloc(sulen + 2 + srlen * 2 + 1, GFP_KERNEL);
|
2012-09-24 20:11:48 +04:00
|
|
|
if (!desc)
|
|
|
|
goto error_free_cert;
|
2014-09-16 20:36:13 +04:00
|
|
|
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;
|
2012-09-24 20:11:48 +04:00
|
|
|
|
|
|
|
/* We're pinning the module by being linked against it */
|
|
|
|
__module_get(public_key_subtype.owner);
|
2015-10-21 16:04:48 +03:00
|
|
|
prep->payload.data[asym_subtype] = &public_key_subtype;
|
|
|
|
prep->payload.data[asym_key_ids] = kids;
|
|
|
|
prep->payload.data[asym_crypto] = cert->pub;
|
2016-04-06 18:13:33 +03:00
|
|
|
prep->payload.data[asym_auth] = cert->sig;
|
2012-09-24 20:11:48 +04:00
|
|
|
prep->description = desc;
|
|
|
|
prep->quotalen = 100;
|
|
|
|
|
|
|
|
/* We've finished with the certificate */
|
|
|
|
cert->pub = NULL;
|
2014-09-16 20:36:13 +04:00
|
|
|
cert->id = NULL;
|
|
|
|
cert->skid = NULL;
|
2016-04-06 18:13:33 +03:00
|
|
|
cert->sig = NULL;
|
2012-09-24 20:11:48 +04:00
|
|
|
desc = NULL;
|
|
|
|
ret = 0;
|
|
|
|
|
2014-09-16 20:36:13 +04:00
|
|
|
error_free_desc:
|
|
|
|
kfree(desc);
|
2012-09-24 20:11:48 +04:00
|
|
|
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);
|
2013-09-17 15:14:55 +04:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("X.509 certificate parser");
|
2017-11-15 19:38:45 +03:00
|
|
|
MODULE_AUTHOR("Red Hat, Inc.");
|
2013-09-17 15:14:55 +04:00
|
|
|
MODULE_LICENSE("GPL");
|