// SPDX-License-Identifier: GPL-2.0-or-later /* System trusted keyring for trusted public keys * * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com)
*/
/** * restrict_link_by_builtin_trusted - Restrict keyring addition by built-in CA * @dest_keyring: Keyring being linked to. * @type: The type of key being added. * @payload: The payload of the new key. * @restriction_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 the built in system keyring.
*/ int restrict_link_by_builtin_trusted(struct key *dest_keyring, conststruct key_type *type, constunion key_payload *payload, struct key *restriction_key)
{ return restrict_link_by_signature(dest_keyring, type, payload,
builtin_trusted_keys);
}
/** * restrict_link_by_digsig_builtin - Restrict digitalSignature key additions by the built-in keyring * @dest_keyring: Keyring being linked to. * @type: The type of key being added. * @payload: The payload of the new key. * @restriction_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 the built in system keyring. The new key * must have the digitalSignature usage field set.
*/ int restrict_link_by_digsig_builtin(struct key *dest_keyring, conststruct key_type *type, constunion key_payload *payload, struct key *restriction_key)
{ return restrict_link_by_digsig(dest_keyring, type, payload,
builtin_trusted_keys);
}
#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING /** * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring * addition by both built-in and secondary keyrings. * @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 or the secondary system * keyrings.
*/ int restrict_link_by_builtin_and_secondary_trusted( struct key *dest_keyring, conststruct key_type *type, constunion key_payload *payload, struct key *restrict_key)
{ /* 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 &&
dest_keyring == secondary_trusted_keys &&
payload == &builtin_trusted_keys->payload) /* Allow the builtin keyring to be added to the secondary */ return 0;
/** * restrict_link_by_digsig_builtin_and_secondary - Restrict by digitalSignature. * @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 or the secondary system * keyrings. The new key must have the digitalSignature usage field set.
*/ int restrict_link_by_digsig_builtin_and_secondary(struct key *dest_keyring, conststruct key_type *type, constunion key_payload *payload, struct key *restrict_key)
{ /* 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 &&
dest_keyring == secondary_trusted_keys &&
payload == &builtin_trusted_keys->payload) /* Allow the builtin keyring to be added to the secondary */ return 0;
/* * 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;
if (!restriction)
panic("Can't allocate secondary trusted keyring restriction\n");
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;
return restriction;
}
/** * add_to_secondary_keyring - Add to secondary keyring. * @source: Source of key * @data: The blob holding the key * @len: The length of the data blob * * Add a key to the secondary keyring. The key must be vouched for by a key in the builtin, * machine or secondary keyring itself.
*/ void __init add_to_secondary_keyring(constchar *source, constvoid *data, size_t len)
{
key_ref_t key;
key_perm_t perm;
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, conststruct key_type *type, constunion 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;
/** * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data. * @data: The data to be verified (NULL if expecting internal data). * @len: Size of @data. * @pkcs7: The PKCS#7 message that is the signature. * @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_message_sig(constvoid *data, size_t len, struct pkcs7_message *pkcs7, struct key *trusted_keys, enum key_being_used_for usage, int (*view_content)(void *ctx, constvoid *data, size_t len,
size_t asn1hdrlen), void *ctx)
{ int ret;
/* The data should be detached - so we need to supply it. */ if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
pr_err("PKCS#7 signature with non-detached data\n");
ret = -EBADMSG; goto error;
}
ret = pkcs7_verify(pkcs7, usage); if (ret < 0) goto error;
ret = is_key_on_revocation_list(pkcs7); if (ret != -ENOKEY) {
pr_devel("PKCS#7 key is on revocation list\n"); goto error;
}
if (!trusted_keys) {
trusted_keys = builtin_trusted_keys;
} elseif (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) { #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
trusted_keys = secondary_trusted_keys; #else
trusted_keys = builtin_trusted_keys; #endif
} elseif (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;
}
}
ret = pkcs7_validate_trust(pkcs7, trusted_keys); if (ret < 0) { if (ret == -ENOKEY)
pr_devel("PKCS#7 signature not signed with a trusted key\n"); 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;
}
/** * 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(constvoid *data, size_t len, constvoid *raw_pkcs7, size_t pkcs7_len, struct key *trusted_keys, enum key_being_used_for usage, int (*view_content)(void *ctx, constvoid *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);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.