2013-08-30 23:07:30 +08:00
|
|
|
/* System trusted keyring for trusted public keys
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/cred.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <keys/asymmetric-type.h>
|
|
|
|
#include <keys/system_keyring.h>
|
2015-07-21 04:16:28 +08:00
|
|
|
#include <crypto/pkcs7.h>
|
2013-08-30 23:07:30 +08:00
|
|
|
|
|
|
|
struct key *system_trusted_keyring;
|
|
|
|
EXPORT_SYMBOL_GPL(system_trusted_keyring);
|
|
|
|
|
|
|
|
extern __initconst const u8 system_certificate_list[];
|
2013-12-05 21:48:22 +08:00
|
|
|
extern __initconst const unsigned long system_certificate_list_size;
|
2013-08-30 23:07:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the compiled-in keys
|
|
|
|
*/
|
|
|
|
static __init int system_trusted_keyring_init(void)
|
|
|
|
{
|
|
|
|
pr_notice("Initialise system trusted keyring\n");
|
|
|
|
|
|
|
|
system_trusted_keyring =
|
|
|
|
keyring_alloc(".system_keyring",
|
|
|
|
KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
|
|
|
|
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
|
2013-08-21 02:36:26 +08:00
|
|
|
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
|
2013-08-30 23:07:30 +08:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA, NULL);
|
|
|
|
if (IS_ERR(system_trusted_keyring))
|
|
|
|
panic("Can't allocate system trusted keyring\n");
|
|
|
|
|
2013-08-30 23:07:37 +08:00
|
|
|
set_bit(KEY_FLAG_TRUSTED_ONLY, &system_trusted_keyring->flags);
|
2013-08-30 23:07:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be initialised before we try and load the keys into the keyring.
|
|
|
|
*/
|
|
|
|
device_initcall(system_trusted_keyring_init);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load the compiled-in list of X.509 certificates.
|
|
|
|
*/
|
|
|
|
static __init int load_system_certificate_list(void)
|
|
|
|
{
|
|
|
|
key_ref_t key;
|
|
|
|
const u8 *p, *end;
|
|
|
|
size_t plen;
|
|
|
|
|
|
|
|
pr_notice("Loading compiled-in X.509 certificates\n");
|
|
|
|
|
|
|
|
p = system_certificate_list;
|
2013-12-05 21:48:22 +08:00
|
|
|
end = p + system_certificate_list_size;
|
2013-08-30 23:07:30 +08:00
|
|
|
while (p < end) {
|
|
|
|
/* Each cert begins with an ASN.1 SEQUENCE tag and must be more
|
|
|
|
* than 256 bytes in size.
|
|
|
|
*/
|
|
|
|
if (end - p < 4)
|
|
|
|
goto dodgy_cert;
|
|
|
|
if (p[0] != 0x30 &&
|
|
|
|
p[1] != 0x82)
|
|
|
|
goto dodgy_cert;
|
|
|
|
plen = (p[2] << 8) | p[3];
|
|
|
|
plen += 4;
|
|
|
|
if (plen > end - p)
|
|
|
|
goto dodgy_cert;
|
|
|
|
|
|
|
|
key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
|
|
|
|
"asymmetric",
|
|
|
|
NULL,
|
|
|
|
p,
|
|
|
|
plen,
|
2013-08-21 02:36:26 +08:00
|
|
|
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
|
|
|
|
KEY_USR_VIEW | KEY_USR_READ),
|
2013-08-30 23:07:37 +08:00
|
|
|
KEY_ALLOC_NOT_IN_QUOTA |
|
|
|
|
KEY_ALLOC_TRUSTED);
|
2013-08-30 23:07:30 +08:00
|
|
|
if (IS_ERR(key)) {
|
|
|
|
pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
|
|
|
|
PTR_ERR(key));
|
|
|
|
} else {
|
2014-06-17 16:56:59 +08:00
|
|
|
set_bit(KEY_FLAG_BUILTIN, &key_ref_to_ptr(key)->flags);
|
2013-08-30 23:07:30 +08:00
|
|
|
pr_notice("Loaded X.509 cert '%s'\n",
|
|
|
|
key_ref_to_ptr(key)->description);
|
|
|
|
key_ref_put(key);
|
|
|
|
}
|
|
|
|
p += plen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
dodgy_cert:
|
|
|
|
pr_err("Problem parsing in-kernel X.509 certificate list\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
late_initcall(load_system_certificate_list);
|
2015-07-21 04:16:28 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_SYSTEM_DATA_VERIFICATION
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a PKCS#7-based signature on system data.
|
|
|
|
* @data: The data to be verified.
|
|
|
|
* @len: Size of @data.
|
|
|
|
* @raw_pkcs7: The PKCS#7 message that is the signature.
|
|
|
|
* @pkcs7_len: The size of @raw_pkcs7.
|
PKCS#7: Appropriately restrict authenticated attributes and content type
A PKCS#7 or CMS message can have per-signature authenticated attributes
that are digested as a lump and signed by the authorising key for that
signature. If such attributes exist, the content digest isn't itself
signed, but rather it is included in a special authattr which then
contributes to the signature.
Further, we already require the master message content type to be
pkcs7_signedData - but there's also a separate content type for the data
itself within the SignedData object and this must be repeated inside the
authattrs for each signer [RFC2315 9.2, RFC5652 11.1].
We should really validate the authattrs if they exist or forbid them
entirely as appropriate. To this end:
(1) Alter the PKCS#7 parser to reject any message that has more than one
signature where at least one signature has authattrs and at least one
that does not.
(2) Validate authattrs if they are present and strongly restrict them.
Only the following authattrs are permitted and all others are
rejected:
(a) contentType. This is checked to be an OID that matches the
content type in the SignedData object.
(b) messageDigest. This must match the crypto digest of the data.
(c) signingTime. If present, we check that this is a valid, parseable
UTCTime or GeneralTime and that the date it encodes fits within
the validity window of the matching X.509 cert.
(d) S/MIME capabilities. We don't check the contents.
(e) Authenticode SP Opus Info. We don't check the contents.
(f) Authenticode Statement Type. We don't check the contents.
The message is rejected if (a) or (b) are missing. If the message is
an Authenticode type, the message is rejected if (e) is missing; if
not Authenticode, the message is rejected if (d) - (f) are present.
The S/MIME capabilities authattr (d) unfortunately has to be allowed
to support kernels already signed by the pesign program. This only
affects kexec. sign-file suppresses them (CMS_NOSMIMECAP).
The message is also rejected if an authattr is given more than once or
if it contains more than one element in its set of values.
(3) Add a parameter to pkcs7_verify() to select one of the following
restrictions and pass in the appropriate option from the callers:
(*) VERIFYING_MODULE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
forbids authattrs. sign-file sets CMS_NOATTR. We could be more
flexible and permit authattrs optionally, but only permit minimal
content.
(*) VERIFYING_FIRMWARE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
requires authattrs. In future, this will require an attribute
holding the target firmware name in addition to the minimal set.
(*) VERIFYING_UNSPECIFIED_SIGNATURE
This requires that the SignedData content type be pkcs7-data but
allows either no authattrs or only permits the minimal set.
(*) VERIFYING_KEXEC_PE_SIGNATURE
This only supports the Authenticode SPC_INDIRECT_DATA content type
and requires at least an SpcSpOpusInfo authattr in addition to the
minimal set. It also permits an SPC_STATEMENT_TYPE authattr (and
an S/MIME capabilities authattr because the pesign program doesn't
remove these).
(*) VERIFYING_KEY_SIGNATURE
(*) VERIFYING_KEY_SELF_SIGNATURE
These are invalid in this context but are included for later use
when limiting the use of X.509 certs.
(4) The pkcs7_test key type is given a module parameter to select between
the above options for testing purposes. For example:
echo 1 >/sys/module/pkcs7_test_key/parameters/usage
keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7
will attempt to check the signature on stuff.pkcs7 as if it contains a
firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE).
Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: David Woodhouse <David.Woodhouse@intel.com>
2015-08-05 22:22:27 +08:00
|
|
|
* @usage: The use to which the key is being put.
|
2015-07-21 04:16:28 +08:00
|
|
|
*/
|
|
|
|
int system_verify_data(const void *data, unsigned long len,
|
PKCS#7: Appropriately restrict authenticated attributes and content type
A PKCS#7 or CMS message can have per-signature authenticated attributes
that are digested as a lump and signed by the authorising key for that
signature. If such attributes exist, the content digest isn't itself
signed, but rather it is included in a special authattr which then
contributes to the signature.
Further, we already require the master message content type to be
pkcs7_signedData - but there's also a separate content type for the data
itself within the SignedData object and this must be repeated inside the
authattrs for each signer [RFC2315 9.2, RFC5652 11.1].
We should really validate the authattrs if they exist or forbid them
entirely as appropriate. To this end:
(1) Alter the PKCS#7 parser to reject any message that has more than one
signature where at least one signature has authattrs and at least one
that does not.
(2) Validate authattrs if they are present and strongly restrict them.
Only the following authattrs are permitted and all others are
rejected:
(a) contentType. This is checked to be an OID that matches the
content type in the SignedData object.
(b) messageDigest. This must match the crypto digest of the data.
(c) signingTime. If present, we check that this is a valid, parseable
UTCTime or GeneralTime and that the date it encodes fits within
the validity window of the matching X.509 cert.
(d) S/MIME capabilities. We don't check the contents.
(e) Authenticode SP Opus Info. We don't check the contents.
(f) Authenticode Statement Type. We don't check the contents.
The message is rejected if (a) or (b) are missing. If the message is
an Authenticode type, the message is rejected if (e) is missing; if
not Authenticode, the message is rejected if (d) - (f) are present.
The S/MIME capabilities authattr (d) unfortunately has to be allowed
to support kernels already signed by the pesign program. This only
affects kexec. sign-file suppresses them (CMS_NOSMIMECAP).
The message is also rejected if an authattr is given more than once or
if it contains more than one element in its set of values.
(3) Add a parameter to pkcs7_verify() to select one of the following
restrictions and pass in the appropriate option from the callers:
(*) VERIFYING_MODULE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
forbids authattrs. sign-file sets CMS_NOATTR. We could be more
flexible and permit authattrs optionally, but only permit minimal
content.
(*) VERIFYING_FIRMWARE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
requires authattrs. In future, this will require an attribute
holding the target firmware name in addition to the minimal set.
(*) VERIFYING_UNSPECIFIED_SIGNATURE
This requires that the SignedData content type be pkcs7-data but
allows either no authattrs or only permits the minimal set.
(*) VERIFYING_KEXEC_PE_SIGNATURE
This only supports the Authenticode SPC_INDIRECT_DATA content type
and requires at least an SpcSpOpusInfo authattr in addition to the
minimal set. It also permits an SPC_STATEMENT_TYPE authattr (and
an S/MIME capabilities authattr because the pesign program doesn't
remove these).
(*) VERIFYING_KEY_SIGNATURE
(*) VERIFYING_KEY_SELF_SIGNATURE
These are invalid in this context but are included for later use
when limiting the use of X.509 certs.
(4) The pkcs7_test key type is given a module parameter to select between
the above options for testing purposes. For example:
echo 1 >/sys/module/pkcs7_test_key/parameters/usage
keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7
will attempt to check the signature on stuff.pkcs7 as if it contains a
firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE).
Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: David Woodhouse <David.Woodhouse@intel.com>
2015-08-05 22:22:27 +08:00
|
|
|
const void *raw_pkcs7, size_t pkcs7_len,
|
|
|
|
enum key_being_used_for usage)
|
2015-07-21 04:16:28 +08:00
|
|
|
{
|
|
|
|
struct pkcs7_message *pkcs7;
|
|
|
|
bool trusted;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
|
|
|
|
if (IS_ERR(pkcs7))
|
|
|
|
return PTR_ERR(pkcs7);
|
|
|
|
|
|
|
|
/* The data should be detached - so we need to supply it. */
|
|
|
|
if (pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
|
|
|
|
pr_err("PKCS#7 signature with non-detached data\n");
|
|
|
|
ret = -EBADMSG;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
PKCS#7: Appropriately restrict authenticated attributes and content type
A PKCS#7 or CMS message can have per-signature authenticated attributes
that are digested as a lump and signed by the authorising key for that
signature. If such attributes exist, the content digest isn't itself
signed, but rather it is included in a special authattr which then
contributes to the signature.
Further, we already require the master message content type to be
pkcs7_signedData - but there's also a separate content type for the data
itself within the SignedData object and this must be repeated inside the
authattrs for each signer [RFC2315 9.2, RFC5652 11.1].
We should really validate the authattrs if they exist or forbid them
entirely as appropriate. To this end:
(1) Alter the PKCS#7 parser to reject any message that has more than one
signature where at least one signature has authattrs and at least one
that does not.
(2) Validate authattrs if they are present and strongly restrict them.
Only the following authattrs are permitted and all others are
rejected:
(a) contentType. This is checked to be an OID that matches the
content type in the SignedData object.
(b) messageDigest. This must match the crypto digest of the data.
(c) signingTime. If present, we check that this is a valid, parseable
UTCTime or GeneralTime and that the date it encodes fits within
the validity window of the matching X.509 cert.
(d) S/MIME capabilities. We don't check the contents.
(e) Authenticode SP Opus Info. We don't check the contents.
(f) Authenticode Statement Type. We don't check the contents.
The message is rejected if (a) or (b) are missing. If the message is
an Authenticode type, the message is rejected if (e) is missing; if
not Authenticode, the message is rejected if (d) - (f) are present.
The S/MIME capabilities authattr (d) unfortunately has to be allowed
to support kernels already signed by the pesign program. This only
affects kexec. sign-file suppresses them (CMS_NOSMIMECAP).
The message is also rejected if an authattr is given more than once or
if it contains more than one element in its set of values.
(3) Add a parameter to pkcs7_verify() to select one of the following
restrictions and pass in the appropriate option from the callers:
(*) VERIFYING_MODULE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
forbids authattrs. sign-file sets CMS_NOATTR. We could be more
flexible and permit authattrs optionally, but only permit minimal
content.
(*) VERIFYING_FIRMWARE_SIGNATURE
This requires that the SignedData content type be pkcs7-data and
requires authattrs. In future, this will require an attribute
holding the target firmware name in addition to the minimal set.
(*) VERIFYING_UNSPECIFIED_SIGNATURE
This requires that the SignedData content type be pkcs7-data but
allows either no authattrs or only permits the minimal set.
(*) VERIFYING_KEXEC_PE_SIGNATURE
This only supports the Authenticode SPC_INDIRECT_DATA content type
and requires at least an SpcSpOpusInfo authattr in addition to the
minimal set. It also permits an SPC_STATEMENT_TYPE authattr (and
an S/MIME capabilities authattr because the pesign program doesn't
remove these).
(*) VERIFYING_KEY_SIGNATURE
(*) VERIFYING_KEY_SELF_SIGNATURE
These are invalid in this context but are included for later use
when limiting the use of X.509 certs.
(4) The pkcs7_test key type is given a module parameter to select between
the above options for testing purposes. For example:
echo 1 >/sys/module/pkcs7_test_key/parameters/usage
keyctl padd pkcs7_test foo @s </tmp/stuff.pkcs7
will attempt to check the signature on stuff.pkcs7 as if it contains a
firmware blob (1 being VERIFYING_FIRMWARE_SIGNATURE).
Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: David Woodhouse <David.Woodhouse@intel.com>
2015-08-05 22:22:27 +08:00
|
|
|
ret = pkcs7_verify(pkcs7, usage);
|
2015-07-21 04:16:28 +08:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
ret = pkcs7_validate_trust(pkcs7, system_trusted_keyring, &trusted);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!trusted) {
|
|
|
|
pr_err("PKCS#7 signature not signed with a trusted key\n");
|
|
|
|
ret = -ENOKEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
pkcs7_free_message(pkcs7);
|
|
|
|
pr_devel("<==%s() = %d\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(system_verify_data);
|
|
|
|
|
|
|
|
#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
|