2019-05-20 17:08:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-08-30 15:07:30 +00:00
|
|
|
/* System trusted keyring for trusted public keys
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
|
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/export.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/cred.h>
|
|
|
|
#include <linux/err.h>
|
2016-08-31 23:05:43 +00:00
|
|
|
#include <linux/slab.h>
|
2020-11-20 18:04:25 +00:00
|
|
|
#include <linux/uidgid.h>
|
2018-08-16 13:05:10 +00:00
|
|
|
#include <linux/verification.h>
|
2013-08-30 15:07:30 +00:00
|
|
|
#include <keys/asymmetric-type.h>
|
|
|
|
#include <keys/system_keyring.h>
|
2015-07-20 20:16:28 +00:00
|
|
|
#include <crypto/pkcs7.h>
|
2013-08-30 15:07:30 +00:00
|
|
|
|
2016-04-06 15:14:27 +00:00
|
|
|
static struct key *builtin_trusted_keys;
|
|
|
|
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
|
|
|
|
static struct key *secondary_trusted_keys;
|
|
|
|
#endif
|
2022-01-26 02:58:30 +00:00
|
|
|
#ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
|
|
|
|
static struct key *machine_trusted_keys;
|
|
|
|
#endif
|
2019-01-21 09:59:28 +00:00
|
|
|
#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
|
|
|
|
static struct key *platform_trusted_keys;
|
|
|
|
#endif
|
2013-08-30 15:07:30 +00:00
|
|
|
|
|
|
|
extern __initconst const u8 system_certificate_list[];
|
2013-12-05 13:48:22 +00:00
|
|
|
extern __initconst const unsigned long system_certificate_list_size;
|
2021-04-09 14:35:07 +00:00
|
|
|
extern __initconst const unsigned long module_cert_size;
|
2013-08-30 15:07:30 +00: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 15:14:26 +00:00
|
|
|
/**
|
2016-04-06 15:14:27 +00:00
|
|
|
* restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
|
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 15:14:26 +00:00
|
|
|
*
|
|
|
|
* Restrict the addition of keys into a keyring based on the key-to-be-added
|
2016-04-06 15:14:27 +00:00
|
|
|
* being vouched for by a key in the built in system keyring.
|
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 15:14:26 +00:00
|
|
|
*/
|
2016-08-30 18:33:13 +00:00
|
|
|
int restrict_link_by_builtin_trusted(struct key *dest_keyring,
|
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 15:14:26 +00:00
|
|
|
const struct key_type *type,
|
2016-08-30 18:33:13 +00:00
|
|
|
const union key_payload *payload,
|
|
|
|
struct key *restriction_key)
|
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 15:14:26 +00:00
|
|
|
{
|
2016-08-30 18:33:13 +00:00
|
|
|
return restrict_link_by_signature(dest_keyring, type, payload,
|
|
|
|
builtin_trusted_keys);
|
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 15:14:26 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 15:14:27 +00:00
|
|
|
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
|
|
|
|
/**
|
|
|
|
* restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
|
|
|
|
* addition by both builtin and secondary keyrings
|
|
|
|
*
|
|
|
|
* Restrict the addition of keys into a keyring based on the key-to-be-added
|
|
|
|
* being vouched for by a key in either the built-in or the secondary system
|
|
|
|
* keyrings.
|
|
|
|
*/
|
|
|
|
int restrict_link_by_builtin_and_secondary_trusted(
|
2016-08-30 18:33:13 +00:00
|
|
|
struct key *dest_keyring,
|
2016-04-06 15:14:27 +00:00
|
|
|
const struct key_type *type,
|
2016-08-30 18:33:13 +00:00
|
|
|
const union key_payload *payload,
|
|
|
|
struct key *restrict_key)
|
2016-04-06 15:14:27 +00:00
|
|
|
{
|
|
|
|
/* If we have a secondary trusted keyring, then that contains a link
|
|
|
|
* through to the builtin keyring and the search will follow that link.
|
|
|
|
*/
|
|
|
|
if (type == &key_type_keyring &&
|
2016-08-30 18:33:13 +00:00
|
|
|
dest_keyring == secondary_trusted_keys &&
|
2016-04-06 15:14:27 +00:00
|
|
|
payload == &builtin_trusted_keys->payload)
|
|
|
|
/* Allow the builtin keyring to be added to the secondary */
|
|
|
|
return 0;
|
|
|
|
|
2016-08-30 18:33:13 +00:00
|
|
|
return restrict_link_by_signature(dest_keyring, type, payload,
|
|
|
|
secondary_trusted_keys);
|
2016-04-06 15:14:27 +00:00
|
|
|
}
|
2016-08-31 23:05:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a struct key_restriction for the "builtin and secondary trust"
|
|
|
|
* keyring. Only for use in system_trusted_keyring_init().
|
|
|
|
*/
|
|
|
|
static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
|
|
|
|
{
|
|
|
|
struct key_restriction *restriction;
|
|
|
|
|
|
|
|
restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!restriction)
|
|
|
|
panic("Can't allocate secondary trusted keyring restriction\n");
|
|
|
|
|
2022-01-26 02:58:31 +00:00
|
|
|
if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING))
|
|
|
|
restriction->check = restrict_link_by_builtin_secondary_and_machine;
|
|
|
|
else
|
|
|
|
restriction->check = restrict_link_by_builtin_and_secondary_trusted;
|
2016-08-31 23:05:43 +00:00
|
|
|
|
|
|
|
return restriction;
|
|
|
|
}
|
2016-04-06 15:14:27 +00:00
|
|
|
#endif
|
2022-01-26 02:58:30 +00:00
|
|
|
#ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
|
|
|
|
void __init set_machine_trusted_keys(struct key *keyring)
|
|
|
|
{
|
|
|
|
machine_trusted_keys = keyring;
|
2022-01-26 02:58:31 +00:00
|
|
|
|
|
|
|
if (key_link(secondary_trusted_keys, machine_trusted_keys) < 0)
|
|
|
|
panic("Can't link (machine) trusted keyrings\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* restrict_link_by_builtin_secondary_and_machine - Restrict keyring addition.
|
|
|
|
* @dest_keyring: Keyring being linked to.
|
|
|
|
* @type: The type of key being added.
|
|
|
|
* @payload: The payload of the new key.
|
|
|
|
* @restrict_key: A ring of keys that can be used to vouch for the new cert.
|
|
|
|
*
|
|
|
|
* Restrict the addition of keys into a keyring based on the key-to-be-added
|
|
|
|
* being vouched for by a key in either the built-in, the secondary, or
|
|
|
|
* the machine keyrings.
|
|
|
|
*/
|
|
|
|
int restrict_link_by_builtin_secondary_and_machine(
|
|
|
|
struct key *dest_keyring,
|
|
|
|
const struct key_type *type,
|
|
|
|
const union key_payload *payload,
|
|
|
|
struct key *restrict_key)
|
|
|
|
{
|
|
|
|
if (machine_trusted_keys && type == &key_type_keyring &&
|
|
|
|
dest_keyring == secondary_trusted_keys &&
|
|
|
|
payload == &machine_trusted_keys->payload)
|
|
|
|
/* Allow the machine keyring to be added to the secondary */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return restrict_link_by_builtin_and_secondary_trusted(dest_keyring, type,
|
|
|
|
payload, restrict_key);
|
2022-01-26 02:58:30 +00:00
|
|
|
}
|
|
|
|
#endif
|
2016-04-06 15:14:27 +00:00
|
|
|
|
2013-08-30 15:07:30 +00:00
|
|
|
/*
|
2016-04-06 15:14:27 +00:00
|
|
|
* Create the trusted keyrings
|
2013-08-30 15:07:30 +00:00
|
|
|
*/
|
|
|
|
static __init int system_trusted_keyring_init(void)
|
|
|
|
{
|
2016-04-06 15:14:27 +00:00
|
|
|
pr_notice("Initialise system trusted keyrings\n");
|
2013-08-30 15:07:30 +00:00
|
|
|
|
2016-04-06 15:14:27 +00:00
|
|
|
builtin_trusted_keys =
|
|
|
|
keyring_alloc(".builtin_trusted_keys",
|
2020-11-20 18:04:25 +00:00
|
|
|
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
|
2019-07-11 01:43:43 +00:00
|
|
|
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
|
|
|
|
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
|
|
|
|
KEY_ALLOC_NOT_IN_QUOTA,
|
2016-04-06 15:14:27 +00:00
|
|
|
NULL, NULL);
|
|
|
|
if (IS_ERR(builtin_trusted_keys))
|
|
|
|
panic("Can't allocate builtin trusted keyring\n");
|
|
|
|
|
|
|
|
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
|
|
|
|
secondary_trusted_keys =
|
|
|
|
keyring_alloc(".secondary_trusted_keys",
|
2020-11-20 18:04:25 +00:00
|
|
|
GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
|
2019-07-11 01:43:43 +00:00
|
|
|
((KEY_POS_ALL & ~KEY_POS_SETATTR) |
|
|
|
|
KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
|
|
|
|
KEY_USR_WRITE),
|
|
|
|
KEY_ALLOC_NOT_IN_QUOTA,
|
2016-08-31 23:05:43 +00:00
|
|
|
get_builtin_and_secondary_restriction(),
|
2016-04-06 15:14:27 +00:00
|
|
|
NULL);
|
|
|
|
if (IS_ERR(secondary_trusted_keys))
|
|
|
|
panic("Can't allocate secondary trusted keyring\n");
|
|
|
|
|
|
|
|
if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
|
|
|
|
panic("Can't link trusted keyrings\n");
|
|
|
|
#endif
|
|
|
|
|
2013-08-30 15:07:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Must be initialised before we try and load the keys into the keyring.
|
|
|
|
*/
|
|
|
|
device_initcall(system_trusted_keyring_init);
|
|
|
|
|
2021-04-09 14:35:07 +00:00
|
|
|
__init int load_module_cert(struct key *keyring)
|
|
|
|
{
|
|
|
|
if (!IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pr_notice("Loading compiled-in module X.509 certificates\n");
|
|
|
|
|
2022-05-18 22:48:09 +00:00
|
|
|
return x509_load_certificate_list(system_certificate_list,
|
|
|
|
module_cert_size, keyring);
|
2021-04-09 14:35:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-30 15:07:30 +00:00
|
|
|
/*
|
|
|
|
* Load the compiled-in list of X.509 certificates.
|
|
|
|
*/
|
|
|
|
static __init int load_system_certificate_list(void)
|
|
|
|
{
|
2021-05-01 22:32:18 +00:00
|
|
|
const u8 *p;
|
2021-04-09 14:35:07 +00:00
|
|
|
unsigned long size;
|
|
|
|
|
2013-08-30 15:07:30 +00:00
|
|
|
pr_notice("Loading compiled-in X.509 certificates\n");
|
|
|
|
|
2021-04-09 14:35:07 +00:00
|
|
|
#ifdef CONFIG_MODULE_SIG
|
|
|
|
p = system_certificate_list;
|
|
|
|
size = system_certificate_list_size;
|
|
|
|
#else
|
|
|
|
p = system_certificate_list + module_cert_size;
|
|
|
|
size = system_certificate_list_size - module_cert_size;
|
|
|
|
#endif
|
|
|
|
|
2022-05-18 22:48:09 +00:00
|
|
|
return x509_load_certificate_list(p, size, builtin_trusted_keys);
|
2013-08-30 15:07:30 +00:00
|
|
|
}
|
|
|
|
late_initcall(load_system_certificate_list);
|
2015-07-20 20:16:28 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_SYSTEM_DATA_VERIFICATION
|
|
|
|
|
|
|
|
/**
|
2019-06-28 02:19:25 +00:00
|
|
|
* verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
|
2016-04-06 15:14:24 +00:00
|
|
|
* @data: The data to be verified (NULL if expecting internal data).
|
2015-07-20 20:16:28 +00:00
|
|
|
* @len: Size of @data.
|
2019-06-28 02:19:25 +00:00
|
|
|
* @pkcs7: The PKCS#7 message that is the signature.
|
2016-04-06 15:14:27 +00:00
|
|
|
* @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
|
|
|
|
* (void *)1UL for all trusted keys).
|
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 14:22:27 +00:00
|
|
|
* @usage: The use to which the key is being put.
|
2016-04-06 15:14:24 +00:00
|
|
|
* @view_content: Callback to gain access to content.
|
|
|
|
* @ctx: Context for callback.
|
2015-07-20 20:16:28 +00:00
|
|
|
*/
|
2019-06-28 02:19:25 +00:00
|
|
|
int verify_pkcs7_message_sig(const void *data, size_t len,
|
|
|
|
struct pkcs7_message *pkcs7,
|
|
|
|
struct key *trusted_keys,
|
|
|
|
enum key_being_used_for usage,
|
|
|
|
int (*view_content)(void *ctx,
|
|
|
|
const void *data, size_t len,
|
|
|
|
size_t asn1hdrlen),
|
|
|
|
void *ctx)
|
2015-07-20 20:16:28 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* The data should be detached - so we need to supply it. */
|
2016-04-06 15:14:24 +00:00
|
|
|
if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
|
2015-07-20 20:16:28 +00:00
|
|
|
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 14:22:27 +00:00
|
|
|
ret = pkcs7_verify(pkcs7, usage);
|
2015-07-20 20:16:28 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2016-04-06 15:14:27 +00:00
|
|
|
if (!trusted_keys) {
|
|
|
|
trusted_keys = builtin_trusted_keys;
|
2018-08-16 13:05:10 +00:00
|
|
|
} else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
|
2016-04-06 15:14:27 +00:00
|
|
|
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
|
|
|
|
trusted_keys = secondary_trusted_keys;
|
|
|
|
#else
|
|
|
|
trusted_keys = builtin_trusted_keys;
|
|
|
|
#endif
|
2019-01-21 09:59:29 +00:00
|
|
|
} else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
|
|
|
|
#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
|
|
|
|
trusted_keys = platform_trusted_keys;
|
|
|
|
#else
|
|
|
|
trusted_keys = NULL;
|
|
|
|
#endif
|
|
|
|
if (!trusted_keys) {
|
|
|
|
ret = -ENOKEY;
|
|
|
|
pr_devel("PKCS#7 platform keyring is not available\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2021-01-22 18:10:51 +00:00
|
|
|
|
|
|
|
ret = is_key_on_revocation_list(pkcs7);
|
|
|
|
if (ret != -ENOKEY) {
|
|
|
|
pr_devel("PKCS#7 platform key is on revocation list\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2016-04-06 15:14:27 +00:00
|
|
|
}
|
2016-04-06 15:14:24 +00:00
|
|
|
ret = pkcs7_validate_trust(pkcs7, trusted_keys);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == -ENOKEY)
|
2019-01-21 09:59:29 +00:00
|
|
|
pr_devel("PKCS#7 signature not signed with a trusted key\n");
|
2016-04-06 15:14:24 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (view_content) {
|
|
|
|
size_t asn1hdrlen;
|
|
|
|
|
|
|
|
ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret == -ENODATA)
|
|
|
|
pr_devel("PKCS#7 message does not contain data\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = view_content(ctx, data, len, asn1hdrlen);
|
2015-07-20 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
2019-06-28 02:19:25 +00:00
|
|
|
pr_devel("<==%s() = %d\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
|
|
|
|
* @data: The data to be verified (NULL if expecting internal data).
|
|
|
|
* @len: Size of @data.
|
|
|
|
* @raw_pkcs7: The PKCS#7 message that is the signature.
|
|
|
|
* @pkcs7_len: The size of @raw_pkcs7.
|
|
|
|
* @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
|
|
|
|
* (void *)1UL for all trusted keys).
|
|
|
|
* @usage: The use to which the key is being put.
|
|
|
|
* @view_content: Callback to gain access to content.
|
|
|
|
* @ctx: Context for callback.
|
|
|
|
*/
|
|
|
|
int verify_pkcs7_signature(const void *data, size_t len,
|
|
|
|
const void *raw_pkcs7, size_t pkcs7_len,
|
|
|
|
struct key *trusted_keys,
|
|
|
|
enum key_being_used_for usage,
|
|
|
|
int (*view_content)(void *ctx,
|
|
|
|
const void *data, size_t len,
|
|
|
|
size_t asn1hdrlen),
|
|
|
|
void *ctx)
|
|
|
|
{
|
|
|
|
struct pkcs7_message *pkcs7;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
|
|
|
|
if (IS_ERR(pkcs7))
|
|
|
|
return PTR_ERR(pkcs7);
|
|
|
|
|
|
|
|
ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
|
|
|
|
view_content, ctx);
|
|
|
|
|
2015-07-20 20:16:28 +00:00
|
|
|
pkcs7_free_message(pkcs7);
|
|
|
|
pr_devel("<==%s() = %d\n", __func__, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-04-06 15:14:24 +00:00
|
|
|
EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
|
2015-07-20 20:16:28 +00:00
|
|
|
|
|
|
|
#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
|
2019-01-21 09:59:28 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
|
|
|
|
void __init set_platform_trusted_keys(struct key *keyring)
|
|
|
|
{
|
|
|
|
platform_trusted_keys = keyring;
|
|
|
|
}
|
|
|
|
#endif
|