Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /* System trusted keyring for trusted public keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/verification.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <keys/asymmetric-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <keys/system_keyring.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <crypto/pkcs7.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static struct key *builtin_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static struct key *secondary_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) static struct key *platform_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) extern __initconst const u8 system_certificate_list[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) extern __initconst const unsigned long system_certificate_list_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Restrict the addition of keys into a keyring based on the key-to-be-added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * being vouched for by a key in the built in system keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) int restrict_link_by_builtin_trusted(struct key *dest_keyring,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 				     const struct key_type *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 				     const union key_payload *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				     struct key *restriction_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return restrict_link_by_signature(dest_keyring, type, payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 					  builtin_trusted_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *   addition by both builtin and secondary keyrings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * Restrict the addition of keys into a keyring based on the key-to-be-added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * being vouched for by a key in either the built-in or the secondary system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * keyrings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) int restrict_link_by_builtin_and_secondary_trusted(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct key *dest_keyring,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	const struct key_type *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	const union key_payload *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	struct key *restrict_key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* If we have a secondary trusted keyring, then that contains a link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * through to the builtin keyring and the search will follow that link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (type == &key_type_keyring &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	    dest_keyring == secondary_trusted_keys &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	    payload == &builtin_trusted_keys->payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		/* Allow the builtin keyring to be added to the secondary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return restrict_link_by_signature(dest_keyring, type, payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 					  secondary_trusted_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * Allocate a struct key_restriction for the "builtin and secondary trust"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * keyring. Only for use in system_trusted_keyring_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	struct key_restriction *restriction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (!restriction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		panic("Can't allocate secondary trusted keyring restriction\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	restriction->check = restrict_link_by_builtin_and_secondary_trusted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return restriction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  * Create the trusted keyrings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static __init int system_trusted_keyring_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	pr_notice("Initialise system trusted keyrings\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	builtin_trusted_keys =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		keyring_alloc(".builtin_trusted_keys",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 			      KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 			      KEY_ALLOC_NOT_IN_QUOTA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			      NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (IS_ERR(builtin_trusted_keys))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		panic("Can't allocate builtin trusted keyring\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	secondary_trusted_keys =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		keyring_alloc(".secondary_trusted_keys",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			      KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			      ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			       KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			       KEY_USR_WRITE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			      KEY_ALLOC_NOT_IN_QUOTA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			      get_builtin_and_secondary_restriction(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 			      NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (IS_ERR(secondary_trusted_keys))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		panic("Can't allocate secondary trusted keyring\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		panic("Can't link trusted keyrings\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  * Must be initialised before we try and load the keys into the keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) device_initcall(system_trusted_keyring_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * Load the compiled-in list of X.509 certificates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static __init int load_system_certificate_list(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	pr_notice("Loading compiled-in X.509 certificates\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return load_certificate_list(system_certificate_list, system_certificate_list_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 				     builtin_trusted_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) late_initcall(load_system_certificate_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * @data: The data to be verified (NULL if expecting internal data).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * @len: Size of @data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * @pkcs7: The PKCS#7 message that is the signature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  *					(void *)1UL for all trusted keys).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * @usage: The use to which the key is being put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * @view_content: Callback to gain access to content.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * @ctx: Context for callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) int verify_pkcs7_message_sig(const void *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			     struct pkcs7_message *pkcs7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			     struct key *trusted_keys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			     enum key_being_used_for usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			     int (*view_content)(void *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 						 const void *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 						 size_t asn1hdrlen),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			     void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	/* The data should be detached - so we need to supply it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		pr_err("PKCS#7 signature with non-detached data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		ret = -EBADMSG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	ret = pkcs7_verify(pkcs7, usage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (!trusted_keys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		trusted_keys = builtin_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	} else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		trusted_keys = secondary_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		trusted_keys = builtin_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	} else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		trusted_keys = platform_trusted_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		trusted_keys = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		if (!trusted_keys) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 			pr_devel("PKCS#7 platform keyring is not available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		ret = is_key_on_revocation_list(pkcs7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		if (ret != -ENOKEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			pr_devel("PKCS#7 platform key is on revocation list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	ret = pkcs7_validate_trust(pkcs7, trusted_keys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		if (ret == -ENOKEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			pr_devel("PKCS#7 signature not signed with a trusted key\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (view_content) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		size_t asn1hdrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			if (ret == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				pr_devel("PKCS#7 message does not contain data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		ret = view_content(ctx, data, len, asn1hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	pr_devel("<==%s() = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * @data: The data to be verified (NULL if expecting internal data).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * @len: Size of @data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  * @raw_pkcs7: The PKCS#7 message that is the signature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * @pkcs7_len: The size of @raw_pkcs7.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  *					(void *)1UL for all trusted keys).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  * @usage: The use to which the key is being put.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * @view_content: Callback to gain access to content.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * @ctx: Context for callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int verify_pkcs7_signature(const void *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			   const void *raw_pkcs7, size_t pkcs7_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 			   struct key *trusted_keys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 			   enum key_being_used_for usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			   int (*view_content)(void *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 					       const void *data, size_t len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 					       size_t asn1hdrlen),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			   void *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	struct pkcs7_message *pkcs7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	if (IS_ERR(pkcs7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return PTR_ERR(pkcs7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				       view_content, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	pkcs7_free_message(pkcs7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	pr_devel("<==%s() = %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) void __init set_platform_trusted_keys(struct key *keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	platform_trusted_keys = keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #endif