]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - certs/system_keyring.c
UBUNTU: Start new release
[mirror_ubuntu-zesty-kernel.git] / certs / system_keyring.c
1 /* System trusted keyring for trusted public keys
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
20
21 static struct key *builtin_trusted_keys;
22 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
23 static struct key *secondary_trusted_keys;
24 #endif
25 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
26 struct key *system_blacklist_keyring;
27 #endif
28
29 extern __initconst const u8 system_certificate_list[];
30 extern __initconst const unsigned long system_certificate_list_size;
31
32 /**
33 * get_system_keyring - Return a pointer to the system keyring
34 *
35 */
36 struct key *get_system_keyring(void)
37 {
38 struct key *system_keyring = NULL;
39
40 system_keyring = builtin_trusted_keys;
41 return system_keyring;
42 }
43 EXPORT_SYMBOL_GPL(get_system_keyring);
44
45 /**
46 * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
47 *
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.
50 */
51 int restrict_link_by_builtin_trusted(struct key *keyring,
52 const struct key_type *type,
53 const union key_payload *payload)
54 {
55 return restrict_link_by_signature(builtin_trusted_keys, type, payload);
56 }
57
58 #ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
59 /**
60 * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
61 * addition by both builtin and secondary keyrings
62 *
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
65 * keyrings.
66 */
67 int restrict_link_by_builtin_and_secondary_trusted(
68 struct key *keyring,
69 const struct key_type *type,
70 const union key_payload *payload)
71 {
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.
74 */
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 */
79 return 0;
80
81 return restrict_link_by_signature(secondary_trusted_keys, type, payload);
82 }
83 #endif
84
85 /*
86 * Create the trusted keyrings
87 */
88 static __init int system_trusted_keyring_init(void)
89 {
90 pr_notice("Initialise system trusted keyrings\n");
91
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,
98 NULL, NULL);
99 if (IS_ERR(builtin_trusted_keys))
100 panic("Can't allocate builtin trusted keyring\n");
101
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 |
108 KEY_USR_WRITE),
109 KEY_ALLOC_NOT_IN_QUOTA,
110 restrict_link_by_builtin_and_secondary_trusted,
111 NULL);
112 if (IS_ERR(secondary_trusted_keys))
113 panic("Can't allocate secondary trusted keyring\n");
114
115 if (key_link(secondary_trusted_keys, builtin_trusted_keys) < 0)
116 panic("Can't link trusted keyrings\n");
117 #endif
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,
124 NULL, NULL);
125 if (IS_ERR(system_blacklist_keyring))
126 panic("Can't allocate system blacklist keyring\n");
127 #endif
128
129 return 0;
130 }
131
132 /*
133 * Must be initialised before we try and load the keys into the keyring.
134 */
135 device_initcall(system_trusted_keyring_init);
136
137 /*
138 * Load the compiled-in list of X.509 certificates.
139 */
140 static __init int load_system_certificate_list(void)
141 {
142 key_ref_t key;
143 const u8 *p, *end;
144 size_t plen;
145
146 pr_notice("Loading compiled-in X.509 certificates\n");
147
148 p = system_certificate_list;
149 end = p + system_certificate_list_size;
150 while (p < end) {
151 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
152 * than 256 bytes in size.
153 */
154 if (end - p < 4)
155 goto dodgy_cert;
156 if (p[0] != 0x30 &&
157 p[1] != 0x82)
158 goto dodgy_cert;
159 plen = (p[2] << 8) | p[3];
160 plen += 4;
161 if (plen > end - p)
162 goto dodgy_cert;
163
164 key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
165 "asymmetric",
166 NULL,
167 p,
168 plen,
169 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
170 KEY_USR_VIEW | KEY_USR_READ),
171 KEY_ALLOC_NOT_IN_QUOTA |
172 KEY_ALLOC_BUILT_IN |
173 KEY_ALLOC_BYPASS_RESTRICTION);
174 if (IS_ERR(key)) {
175 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
176 PTR_ERR(key));
177 WARN_ON_ONCE(1);
178 } else {
179 pr_notice("Loaded X.509 cert '%s'\n",
180 key_ref_to_ptr(key)->description);
181 key_ref_put(key);
182 }
183 p += plen;
184 }
185
186 return 0;
187
188 dodgy_cert:
189 pr_err("Problem parsing in-kernel X.509 certificate list\n");
190 return 0;
191 }
192 late_initcall(load_system_certificate_list);
193
194 #ifdef CONFIG_SYSTEM_DATA_VERIFICATION
195
196 /**
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.
207 */
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,
214 size_t asn1hdrlen),
215 void *ctx)
216 {
217 struct pkcs7_message *pkcs7;
218 int ret;
219
220 pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
221 if (IS_ERR(pkcs7))
222 return PTR_ERR(pkcs7);
223
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");
227 ret = -EBADMSG;
228 goto error;
229 }
230
231 ret = pkcs7_verify(pkcs7, usage);
232 if (ret < 0)
233 goto error;
234
235 if (!trusted_keys) {
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;
240 #else
241 trusted_keys = builtin_trusted_keys;
242 #endif
243 }
244 #ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
245 ret = pkcs7_validate_trust(pkcs7, system_blacklist_keyring);
246 if (!ret) {
247 /* module is signed with a cert in the blacklist. reject */
248 pr_err("Module key is in the blacklist\n");
249 ret = -EKEYREJECTED;
250 goto error;
251 }
252 #endif
253 ret = pkcs7_validate_trust(pkcs7, trusted_keys);
254 if (ret < 0) {
255 if (ret == -ENOKEY)
256 pr_err("PKCS#7 signature not signed with a trusted key\n");
257 goto error;
258 }
259
260 if (view_content) {
261 size_t asn1hdrlen;
262
263 ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
264 if (ret < 0) {
265 if (ret == -ENODATA)
266 pr_devel("PKCS#7 message does not contain data\n");
267 goto error;
268 }
269
270 ret = view_content(ctx, data, len, asn1hdrlen);
271 }
272
273 error:
274 pkcs7_free_message(pkcs7);
275 pr_devel("<==%s() = %d\n", __func__, ret);
276 return ret;
277 }
278 EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
279
280 #endif /* CONFIG_SYSTEM_DATA_VERIFICATION */