KEYS: Remove KEY_FLAG_TRUSTED and KEY_ALLOC_TRUSTED
[deliverable/linux.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
a511e1af 21static struct key *system_trusted_keyring;
b56e5a17
DH
22
23extern __initconst const u8 system_certificate_list[];
62226983 24extern __initconst const unsigned long system_certificate_list_size;
b56e5a17 25
a511e1af
DH
26/**
27 * restrict_link_by_builtin_trusted - Restrict keyring addition by system CA
28 *
29 * Restrict the addition of keys into a keyring based on the key-to-be-added
30 * being vouched for by a key in the system keyring.
31 */
32int restrict_link_by_builtin_trusted(struct key *keyring,
33 const struct key_type *type,
a511e1af
DH
34 const union key_payload *payload)
35{
36 return restrict_link_by_signature(system_trusted_keyring,
37 type, payload);
38}
39
b56e5a17
DH
40/*
41 * Load the compiled-in keys
42 */
43static __init int system_trusted_keyring_init(void)
44{
45 pr_notice("Initialise system trusted keyring\n");
46
47 system_trusted_keyring =
48 keyring_alloc(".system_keyring",
49 KUIDT_INIT(0), KGIDT_INIT(0), current_cred(),
50 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
af34cb0c 51 KEY_USR_VIEW | KEY_USR_READ | KEY_USR_SEARCH),
5ac7eace 52 KEY_ALLOC_NOT_IN_QUOTA,
a511e1af 53 restrict_link_by_builtin_trusted, NULL);
b56e5a17
DH
54 if (IS_ERR(system_trusted_keyring))
55 panic("Can't allocate system trusted keyring\n");
b56e5a17
DH
56 return 0;
57}
58
59/*
60 * Must be initialised before we try and load the keys into the keyring.
61 */
62device_initcall(system_trusted_keyring_init);
63
64/*
65 * Load the compiled-in list of X.509 certificates.
66 */
67static __init int load_system_certificate_list(void)
68{
69 key_ref_t key;
70 const u8 *p, *end;
71 size_t plen;
72
73 pr_notice("Loading compiled-in X.509 certificates\n");
74
b56e5a17 75 p = system_certificate_list;
62226983 76 end = p + system_certificate_list_size;
b56e5a17
DH
77 while (p < end) {
78 /* Each cert begins with an ASN.1 SEQUENCE tag and must be more
79 * than 256 bytes in size.
80 */
81 if (end - p < 4)
82 goto dodgy_cert;
83 if (p[0] != 0x30 &&
84 p[1] != 0x82)
85 goto dodgy_cert;
86 plen = (p[2] << 8) | p[3];
87 plen += 4;
88 if (plen > end - p)
89 goto dodgy_cert;
90
91 key = key_create_or_update(make_key_ref(system_trusted_keyring, 1),
92 "asymmetric",
93 NULL,
94 p,
95 plen,
af34cb0c
MZ
96 ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
97 KEY_USR_VIEW | KEY_USR_READ),
008643b8 98 KEY_ALLOC_NOT_IN_QUOTA |
5ac7eace
DH
99 KEY_ALLOC_BUILT_IN |
100 KEY_ALLOC_BYPASS_RESTRICTION);
b56e5a17
DH
101 if (IS_ERR(key)) {
102 pr_err("Problem loading in-kernel X.509 certificate (%ld)\n",
103 PTR_ERR(key));
104 } else {
105 pr_notice("Loaded X.509 cert '%s'\n",
106 key_ref_to_ptr(key)->description);
107 key_ref_put(key);
108 }
109 p += plen;
110 }
111
112 return 0;
113
114dodgy_cert:
115 pr_err("Problem parsing in-kernel X.509 certificate list\n");
116 return 0;
117}
118late_initcall(load_system_certificate_list);
091f6e26
DH
119
120#ifdef CONFIG_SYSTEM_DATA_VERIFICATION
121
122/**
e68503bd
DH
123 * verify_pkcs7_signature - Verify a PKCS#7-based signature on system data.
124 * @data: The data to be verified (NULL if expecting internal data).
091f6e26
DH
125 * @len: Size of @data.
126 * @raw_pkcs7: The PKCS#7 message that is the signature.
127 * @pkcs7_len: The size of @raw_pkcs7.
e68503bd 128 * @trusted_keys: Trusted keys to use (NULL for system_trusted_keyring).
99db4435 129 * @usage: The use to which the key is being put.
e68503bd
DH
130 * @view_content: Callback to gain access to content.
131 * @ctx: Context for callback.
091f6e26 132 */
e68503bd
DH
133int verify_pkcs7_signature(const void *data, size_t len,
134 const void *raw_pkcs7, size_t pkcs7_len,
135 struct key *trusted_keys,
e68503bd
DH
136 enum key_being_used_for usage,
137 int (*view_content)(void *ctx,
138 const void *data, size_t len,
139 size_t asn1hdrlen),
140 void *ctx)
091f6e26
DH
141{
142 struct pkcs7_message *pkcs7;
091f6e26
DH
143 int ret;
144
145 pkcs7 = pkcs7_parse_message(raw_pkcs7, pkcs7_len);
146 if (IS_ERR(pkcs7))
147 return PTR_ERR(pkcs7);
148
149 /* The data should be detached - so we need to supply it. */
e68503bd 150 if (data && pkcs7_supply_detached_data(pkcs7, data, len) < 0) {
091f6e26
DH
151 pr_err("PKCS#7 signature with non-detached data\n");
152 ret = -EBADMSG;
153 goto error;
154 }
155
99db4435 156 ret = pkcs7_verify(pkcs7, usage);
091f6e26
DH
157 if (ret < 0)
158 goto error;
159
e68503bd
DH
160 if (!trusted_keys)
161 trusted_keys = system_trusted_keyring;
bda850cd
DH
162 ret = pkcs7_validate_trust(pkcs7, trusted_keys);
163 if (ret < 0) {
164 if (ret == -ENOKEY)
165 pr_err("PKCS#7 signature not signed with a trusted key\n");
e68503bd
DH
166 goto error;
167 }
168
169 if (view_content) {
170 size_t asn1hdrlen;
171
172 ret = pkcs7_get_content_data(pkcs7, &data, &len, &asn1hdrlen);
173 if (ret < 0) {
174 if (ret == -ENODATA)
175 pr_devel("PKCS#7 message does not contain data\n");
176 goto error;
177 }
178
179 ret = view_content(ctx, data, len, asn1hdrlen);
091f6e26
DH
180 }
181
182error:
183 pkcs7_free_message(pkcs7);
184 pr_devel("<==%s() = %d\n", __func__, ret);
185 return ret;
186}
e68503bd 187EXPORT_SYMBOL_GPL(verify_pkcs7_signature);
091f6e26
DH
188
189#endif /* CONFIG_SYSTEM_DATA_VERIFICATION */
This page took 0.131804 seconds and 5 git commands to generate.