]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - certs/system_keyring.c
UBUNTU: Ubuntu-raspi2-4.10.0-1009.12
[mirror_ubuntu-zesty-kernel.git] / certs / system_keyring.c
CommitLineData
b56e5a17
DH
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>
091f6e26 19#include <crypto/pkcs7.h>
b56e5a17 20
d3bfe841
DH
21static struct key *builtin_trusted_keys;
22#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING
23static struct key *secondary_trusted_keys;
24#endif
177a18d1
JB
25#ifdef CONFIG_SYSTEM_BLACKLIST_KEYRING
26struct key *system_blacklist_keyring;
27#endif
b56e5a17
DH
28
29extern __initconst const u8 system_certificate_list[];
62226983 30extern __initconst const unsigned long system_certificate_list_size;
b56e5a17 31
16eb7765
JB
32/**
33 * get_system_keyring - Return a pointer to the system keyring
34 *
35 */
36struct key *get_system_keyring(void)
37{
38 struct key *system_keyring = NULL;
39
40 system_keyring = builtin_trusted_keys;
41 return system_keyring;
42}
43EXPORT_SYMBOL_GPL(get_system_keyring);
44
a511e1af 45/**
d3bfe841 46 * restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA
a511e1af
DH
47 *
48 * Restrict the addition of keys into a keyring based on the key-to-be-added
d3bfe841 49 * being vouched for by a key in the built in system keyring.
a511e1af
DH
50 */
51int restrict_link_by_builtin_trusted(struct key *keyring,
52 const struct key_type *type,
a511e1af
DH
53 const union key_payload *payload)
54{
d3bfe841 55 return restrict_link_by_signature(builtin_trusted_keys, type, payload);
a511e1af
DH
56}
57
d3bfe841
DH
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 */
67int 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
b56e5a17 85/*
d3bfe841 86 * Create the trusted keyrings
b56e5a17
DH
87 */
88static __init int system_trusted_keyring_init(void)
89{
d3bfe841 90 pr_notice("Initialise system trusted keyrings\n");
b56e5a17 91
d3bfe841
DH
92 builtin_trusted_keys =
93 keyring_alloc(".builtin_trusted_keys",
b56e5a17
DH
94 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
95 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
af34cb0c 96 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
5ac7eace 97 KEY_ALLOC_NOT_IN_QUOTA,
d3bfe841
DH
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
177a18d1
JB
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
d3bfe841 128
b56e5a17
DH
129 return 0;
130}
131
132/*
133 * Must be initialised before we try and load the keys into the keyring.
134 */
135device_initcall(system_trusted_keyring_init);
136
137/*
138 * Load the compiled-in list of X.509 certificates.
139 */
140static __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
b56e5a17 148 p = system_certificate_list;
62226983 149 end = p + system_certificate_list_size;
b56e5a17
DH
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
d3bfe841 164 key = key_create_or_update(make_key_ref(builtin_trusted_keys, 1),
b56e5a17
DH
165 "asymmetric",
166 NULL,
167 p,
168 plen,
af34cb0c
MZ
169 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
170 KEY_USR_VIEW | KEY_USR_READ),
008643b8 171 KEY_ALLOC_NOT_IN_QUOTA |
5ac7eace
DH
172 KEY_ALLOC_BUILT_IN |
173 KEY_ALLOC_BYPASS_RESTRICTION);
b56e5a17
DH
174 if (IS_ERR(key)) {
175 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
176 PTR_ERR(key));
ca6088d8 177 WARN_ON_ONCE(1);
b56e5a17
DH
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
188dodgy_cert:
189 pr_err("Problem parsing in-kernel X.509 certificate list\n");
190 return 0;
191}
192late_initcall(load_system_certificate_list);
091f6e26
DH
193
194#ifdef CONFIG_SYSTEM_DATA_VERIFICATION
195
196/**
e68503bd
DH
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).
091f6e26
DH
199 * @len: Size of @data.
200 * @raw_pkcs7: The PKCS#7 message that is the signature.
201 * @pkcs7_len: The size of @raw_pkcs7.
d3bfe841
DH
202 * @trusted_keys: Trusted keys to use (NULL for builtin trusted keys only,
203 * (void *)1UL for all trusted keys).
99db4435 204 * @usage: The use to which the key is being put.
e68503bd
DH
205 * @view_content: Callback to gain access to content.
206 * @ctx: Context for callback.
091f6e26 207 */
e68503bd
DH
208int verify_pkcs7_signature(const void *data, size_t len,
209 const void *raw_pkcs7, size_t pkcs7_len,
210 struct key *trusted_keys,
e68503bd
DH
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)
091f6e26
DH
216{
217 struct pkcs7_message *pkcs7;
091f6e26
DH
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. */
e68503bd 225 if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
091f6e26
DH
226 pr_err("PKCS#7 signature with non-detached data\n");
227 ret = -EBADMSG;
228 goto error;
229 }
230
99db4435 231 ret = pkcs7_verify(pkcs7, usage);
091f6e26
DH
232 if (ret < 0)
233 goto error;
234
d3bfe841
DH
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 }
177a18d1
JB
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
bda850cd
DH
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");
e68503bd
DH
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);
091f6e26
DH
271 }
272
273error:
274 pkcs7_free_message(pkcs7);
275 pr_devel("<==%s() = %d\n", __func__, ret);
276 return ret;
277}
e68503bd 278EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
091f6e26
DH
279
280#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */