1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* System trusted keyring for trusted public keys
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/export.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/cred.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/uidgid.h>
15 #include <linux/verification.h>
16 #include <keys/asymmetric-type.h>
17 #include <keys/system_keyring.h>
18 #include <crypto/pkcs7.h>
20 static struct key *builtin_trusted_keys;
21 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
22 static struct key *secondary_trusted_keys;
24 #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
25 static struct key *machine_trusted_keys;
27 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
28 static struct key *platform_trusted_keys;
31 extern __initconst const u8 system_certificate_list[];
32 extern __initconst const unsigned long system_certificate_list_size;
33 extern __initconst const unsigned long module_cert_size;
36 * restrict_link_by_builtin_trusted - Restrict keyring addition by built-in CA
37 * @dest_keyring: Keyring being linked to.
38 * @type: The type of key being added.
39 * @payload: The payload of the new key.
40 * @restriction_key: A ring of keys that can be used to vouch for the new cert.
42 * Restrict the addition of keys into a keyring based on the key-to-be-added
43 * being vouched for by a key in the built in system keyring.
45 int restrict_link_by_builtin_trusted(struct key *dest_keyring,
46 const struct key_type *type,
47 const union key_payload *payload,
48 struct key *restriction_key)
50 return restrict_link_by_signature(dest_keyring, type, payload,
51 builtin_trusted_keys);
54 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
56 * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
57 * addition by both built-in and secondary keyrings.
58 * @dest_keyring: Keyring being linked to.
59 * @type: The type of key being added.
60 * @payload: The payload of the new key.
61 * @restrict_key: A ring of keys that can be used to vouch for the new cert.
63 * Restrict the addition of keys into a keyring based on the key-to-be-added
64 * being vouched for by a key in either the built-in or the secondary system
67 int restrict_link_by_builtin_and_secondary_trusted(
68 struct key *dest_keyring,
69 const struct key_type *type,
70 const union key_payload *payload,
71 struct key *restrict_key)
73 /* If we have a secondary trusted keyring, then that contains a link
74 * through to the builtin keyring and the search will follow that link.
76 if (type == &key_type_keyring &&
77 dest_keyring == secondary_trusted_keys &&
78 payload == &builtin_trusted_keys->payload)
79 /* Allow the builtin keyring to be added to the secondary */
82 return restrict_link_by_signature(dest_keyring, type, payload,
83 secondary_trusted_keys);
87 * Allocate a struct key_restriction for the "builtin and secondary trust"
88 * keyring. Only for use in system_trusted_keyring_init().
90 static __init struct key_restriction *get_builtin_and_secondary_restriction(void)
92 struct key_restriction *restriction;
94 restriction = kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
97 panic("Can't allocate secondary trusted keyring restriction\n");
99 if (IS_ENABLED(CONFIG_INTEGRITY_MACHINE_KEYRING))
100 restriction->check = restrict_link_by_builtin_secondary_and_machine;
102 restriction->check = restrict_link_by_builtin_and_secondary_trusted;
107 #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
108 void __init set_machine_trusted_keys(struct key *keyring)
110 machine_trusted_keys = keyring;
112 if (key_link(secondary_trusted_keys, machine_trusted_keys) < 0)
113 panic("Can't link (machine) trusted keyrings\n");
117 * restrict_link_by_builtin_secondary_and_machine - Restrict keyring addition.
118 * @dest_keyring: Keyring being linked to.
119 * @type: The type of key being added.
120 * @payload: The payload of the new key.
121 * @restrict_key: A ring of keys that can be used to vouch for the new cert.
123 * Restrict the addition of keys into a keyring based on the key-to-be-added
124 * being vouched for by a key in either the built-in, the secondary, or
125 * the machine keyrings.
127 int restrict_link_by_builtin_secondary_and_machine(
128 struct key *dest_keyring,
129 const struct key_type *type,
130 const union key_payload *payload,
131 struct key *restrict_key)
133 if (machine_trusted_keys && type == &key_type_keyring &&
134 dest_keyring == secondary_trusted_keys &&
135 payload == &machine_trusted_keys->payload)
136 /* Allow the machine keyring to be added to the secondary */
139 return restrict_link_by_builtin_and_secondary_trusted(dest_keyring, type,
140 payload, restrict_key);
145 * Create the trusted keyrings
147 static __init int system_trusted_keyring_init(void)
149 pr_notice("Initialise system trusted keyrings\n");
151 builtin_trusted_keys =
152 keyring_alloc(".builtin_trusted_keys",
153 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
154 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
155 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
156 KEY_ALLOC_NOT_IN_QUOTA,
158 if (IS_ERR(builtin_trusted_keys))
159 panic("Can't allocate builtin trusted keyring\n");
161 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
162 secondary_trusted_keys =
163 keyring_alloc(".secondary_trusted_keys",
164 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
165 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
166 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH |
168 KEY_ALLOC_NOT_IN_QUOTA,
169 get_builtin_and_secondary_restriction(),
171 if (IS_ERR(secondary_trusted_keys))
172 panic("Can't allocate secondary trusted keyring\n");
174 if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
175 panic("Can't link trusted keyrings\n");
182 * Must be initialised before we try and load the keys into the keyring.
184 device_initcall(system_trusted_keyring_init);
186 __init int load_module_cert(struct key *keyring)
188 if (!IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG))
191 pr_notice("Loading compiled-in module X.509 certificates\n");
193 return x509_load_certificate_list(system_certificate_list,
194 module_cert_size, keyring);
198 * Load the compiled-in list of X.509 certificates.
200 static __init int load_system_certificate_list(void)
205 pr_notice("Loading compiled-in X.509 certificates\n");
207 #ifdef CONFIG_MODULE_SIG
208 p = system_certificate_list;
209 size = system_certificate_list_size;
211 p = system_certificate_list + module_cert_size;
212 size = system_certificate_list_size - module_cert_size;
215 return x509_load_certificate_list(p, size, builtin_trusted_keys);
217 late_initcall(load_system_certificate_list);
219 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
222 * verify_pkcs7_message_sig - Verify a PKCS#7-based signature on system data.
223 * @data: The data to be verified (NULL if expecting internal data).
224 * @len: Size of @data.
225 * @pkcs7: The PKCS#7 message that is the signature.
226 * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
227 * (void *)1UL for all trusted keys).
228 * @usage: The use to which the key is being put.
229 * @view_content: Callback to gain access to content.
230 * @ctx: Context for callback.
232 int verify_pkcs7_message_sig(const void *data, size_t len,
233 struct pkcs7_message *pkcs7,
234 struct key *trusted_keys,
235 enum key_being_used_for usage,
236 int (*view_content)(void *ctx,
237 const void *data, size_t len,
243 /* The data should be detached - so we need to supply it. */
244 if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
245 pr_err("PKCS#7 signature with non-detached data\n");
250 ret = pkcs7_verify(pkcs7, usage);
255 trusted_keys = builtin_trusted_keys;
256 } else if (trusted_keys == VERIFY_USE_SECONDARY_KEYRING) {
257 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
258 trusted_keys = secondary_trusted_keys;
260 trusted_keys = builtin_trusted_keys;
262 } else if (trusted_keys == VERIFY_USE_PLATFORM_KEYRING) {
263 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
264 trusted_keys = platform_trusted_keys;
270 pr_devel("PKCS#7 platform keyring is not available\n");
274 ret = is_key_on_revocation_list(pkcs7);
275 if (ret != -ENOKEY) {
276 pr_devel("PKCS#7 platform key is on revocation list\n");
280 ret = pkcs7_validate_trust(pkcs7, trusted_keys);
283 pr_devel("PKCS#7 signature not signed with a trusted key\n");
290 ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
293 pr_devel("PKCS#7 message does not contain data\n");
297 ret = view_content(ctx, data, len, asn1hdrlen);
301 pr_devel("<==%s() = %d\n", __func__, ret);
306 * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
307 * @data: The data to be verified (NULL if expecting internal data).
308 * @len: Size of @data.
309 * @raw_pkcs7: The PKCS#7 message that is the signature.
310 * @pkcs7_len: The size of @raw_pkcs7.
311 * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
312 * (void *)1UL for all trusted keys).
313 * @usage: The use to which the key is being put.
314 * @view_content: Callback to gain access to content.
315 * @ctx: Context for callback.
317 int verify_pkcs7_signature(const void *data, size_t len,
318 const void *raw_pkcs7, size_t pkcs7_len,
319 struct key *trusted_keys,
320 enum key_being_used_for usage,
321 int (*view_content)(void *ctx,
322 const void *data, size_t len,
326 struct pkcs7_message *pkcs7;
329 pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
331 return PTR_ERR(pkcs7);
333 ret = verify_pkcs7_message_sig(data, len, pkcs7, trusted_keys, usage,
336 pkcs7_free_message(pkcs7);
337 pr_devel("<==%s() = %d\n", __func__, ret);
340 EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
342 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
344 #ifdef CONFIG_INTEGRITY_PLATFORM_KEYRING
345 void __init set_platform_trusted_keys(struct key *keyring)
347 platform_trusted_keys = keyring;