1 /* System trusted keyring for trusted public keys
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/cred.h>
16 #include <linux/err.h>
17 #include <keys/asymmetric-type.h>
18 #include <keys/system_keyring.h>
19 #include <crypto/pkcs7.h>
21 static struct key
*builtin_trusted_keys
;
22 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
23 static struct key
*secondary_trusted_keys
;
25 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
26 struct key
*system_blacklist_keyring
;
29 extern __initconst
const u8 system_certificate_list
[];
30 extern __initconst
const unsigned long system_certificate_list_size
;
33 * get_system_keyring - Return a pointer to the system keyring
36 struct key
*get_system_keyring(void)
38 struct key
*system_keyring
= NULL
;
40 system_keyring
= builtin_trusted_keys
;
41 return system_keyring
;
43 EXPORT_SYMBOL_GPL(get_system_keyring
);
46 * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
48 * Restrict the addition of keys into a keyring based on the key-to-be-added
49 * being vouched for by a key in the built in system keyring.
51 int restrict_link_by_builtin_trusted(struct key
*keyring
,
52 const struct key_type
*type
,
53 const union key_payload
*payload
)
55 return restrict_link_by_signature(builtin_trusted_keys
, type
, payload
);
58 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
60 * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
61 * addition by both builtin and secondary keyrings
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(
69 const struct key_type
*type
,
70 const union key_payload
*payload
)
72 /* If we have a secondary trusted keyring, then that contains a link
73 * through to the builtin keyring and the search will follow that link.
75 if (type
== &key_type_keyring
&&
76 keyring
== secondary_trusted_keys
&&
77 payload
== &builtin_trusted_keys
->payload
)
78 /* Allow the builtin keyring to be added to the secondary */
81 return restrict_link_by_signature(secondary_trusted_keys
, type
, payload
);
86 * Create the trusted keyrings
88 static __init
int system_trusted_keyring_init(void)
90 pr_notice("Initialise system trusted keyrings\n");
92 builtin_trusted_keys
=
93 keyring_alloc(".builtin_trusted_keys",
94 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
95 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
96 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
),
97 KEY_ALLOC_NOT_IN_QUOTA
,
99 if (IS_ERR(builtin_trusted_keys
))
100 panic("Can't allocate builtin trusted keyring\n");
102 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
103 secondary_trusted_keys
=
104 keyring_alloc(".secondary_trusted_keys",
105 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
106 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
107 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
|
109 KEY_ALLOC_NOT_IN_QUOTA
,
110 restrict_link_by_builtin_and_secondary_trusted
,
112 if (IS_ERR(secondary_trusted_keys
))
113 panic("Can't allocate secondary trusted keyring\n");
115 if (key_link(secondary_trusted_keys
, builtin_trusted_keys
) < 0)
116 panic("Can't link trusted keyrings\n");
118 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
119 system_blacklist_keyring
= keyring_alloc(".system_blacklist_keyring",
120 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
121 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
122 KEY_USR_VIEW
| KEY_USR_READ
| KEY_USR_SEARCH
),
123 KEY_ALLOC_NOT_IN_QUOTA
,
125 if (IS_ERR(system_blacklist_keyring
))
126 panic("Can't allocate system blacklist keyring\n");
133 * Must be initialised before we try and load the keys into the keyring.
135 device_initcall(system_trusted_keyring_init
);
138 * Load the compiled-in list of X.509 certificates.
140 static __init
int load_system_certificate_list(void)
146 pr_notice("Loading compiled-in X.509 certificates\n");
148 p
= system_certificate_list
;
149 end
= p
+ system_certificate_list_size
;
151 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
152 * than 256 bytes in size.
159 plen
= (p
[2] << 8) | p
[3];
164 key
= key_create_or_update(make_key_ref(builtin_trusted_keys
, 1),
169 ((KEY_POS_ALL
& ~KEY_POS_SETATTR
) |
170 KEY_USR_VIEW
| KEY_USR_READ
),
171 KEY_ALLOC_NOT_IN_QUOTA
|
173 KEY_ALLOC_BYPASS_RESTRICTION
);
175 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
179 pr_notice("Loaded X.509 cert '%s'\n",
180 key_ref_to_ptr(key
)->description
);
189 pr_err("Problem parsing in-kernel X.509 certificate list\n");
192 late_initcall(load_system_certificate_list
);
194 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
197 * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
198 * @data: The data to be verified (NULL if expecting internal data).
199 * @len: Size of @data.
200 * @raw_pkcs7: The PKCS#7 message that is the signature.
201 * @pkcs7_len: The size of @raw_pkcs7.
202 * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
203 * (void *)1UL for all trusted keys).
204 * @usage: The use to which the key is being put.
205 * @view_content: Callback to gain access to content.
206 * @ctx: Context for callback.
208 int verify_pkcs7_signature(const void *data
, size_t len
,
209 const void *raw_pkcs7
, size_t pkcs7_len
,
210 struct key
*trusted_keys
,
211 enum key_being_used_for usage
,
212 int (*view_content
)(void *ctx
,
213 const void *data
, size_t len
,
217 struct pkcs7_message
*pkcs7
;
220 pkcs7
= pkcs7_parse_message(raw_pkcs7
, pkcs7_len
);
222 return PTR_ERR(pkcs7
);
224 /* The data should be detached - so we need to supply it. */
225 if (data
&& pkcs7_supply_detached_data(pkcs7
, data
, len
) < 0) {
226 pr_err("PKCS#7 signature with non-detached data\n");
231 ret
= pkcs7_verify(pkcs7
, usage
);
236 trusted_keys
= builtin_trusted_keys
;
237 } else if (trusted_keys
== (void *)1UL) {
238 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
239 trusted_keys
= secondary_trusted_keys
;
241 trusted_keys
= builtin_trusted_keys
;
244 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
245 ret
= pkcs7_validate_trust(pkcs7
, system_blacklist_keyring
);
247 /* module is signed with a cert in the blacklist. reject */
248 pr_err("Module key is in the blacklist\n");
253 ret
= pkcs7_validate_trust(pkcs7
, trusted_keys
);
256 pr_err("PKCS#7 signature not signed with a trusted key\n");
263 ret
= pkcs7_get_content_data(pkcs7
, &data
, &len
, &asn1hdrlen
);
266 pr_devel("PKCS#7 message does not contain data\n");
270 ret
= view_content(ctx
, data
, len
, asn1hdrlen
);
274 pkcs7_free_message(pkcs7
);
275 pr_devel("<==%s() = %d\n", __func__
, ret
);
278 EXPORT_SYMBOL_GPL(verify_pkcs7_signature
);
280 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */