This source file includes following definitions.
- ca_keys_setup
- restrict_link_by_signature
- match_either_id
- key_or_keyring_common
- restrict_link_by_key_or_keyring
- restrict_link_by_key_or_keyring_chain
1
2
3
4
5
6
7
8 #define pr_fmt(fmt) "ASYM: "fmt
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/err.h>
12 #include <crypto/public_key.h>
13 #include "asymmetric_keys.h"
14
15 static bool use_builtin_keys;
16 static struct asymmetric_key_id *ca_keyid;
17
18 #ifndef MODULE
19 static struct {
20 struct asymmetric_key_id id;
21 unsigned char data[10];
22 } cakey;
23
24 static int __init ca_keys_setup(char *str)
25 {
26 if (!str)
27 return 1;
28
29 if (strncmp(str, "id:", 3) == 0) {
30 struct asymmetric_key_id *p = &cakey.id;
31 size_t hexlen = (strlen(str) - 3) / 2;
32 int ret;
33
34 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
35 pr_err("Missing or invalid ca_keys id\n");
36 return 1;
37 }
38
39 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
40 if (ret < 0)
41 pr_err("Unparsable ca_keys id hex string\n");
42 else
43 ca_keyid = p;
44 } else if (strcmp(str, "builtin") == 0) {
45 use_builtin_keys = true;
46 }
47
48 return 1;
49 }
50 __setup("ca_keys=", ca_keys_setup);
51 #endif
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 int restrict_link_by_signature(struct key *dest_keyring,
71 const struct key_type *type,
72 const union key_payload *payload,
73 struct key *trust_keyring)
74 {
75 const struct public_key_signature *sig;
76 struct key *key;
77 int ret;
78
79 pr_devel("==>%s()\n", __func__);
80
81 if (!trust_keyring)
82 return -ENOKEY;
83
84 if (type != &key_type_asymmetric)
85 return -EOPNOTSUPP;
86
87 sig = payload->data[asym_auth];
88 if (!sig)
89 return -ENOPKG;
90 if (!sig->auth_ids[0] && !sig->auth_ids[1])
91 return -ENOKEY;
92
93 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
94 return -EPERM;
95
96
97 key = find_asymmetric_key(trust_keyring,
98 sig->auth_ids[0], sig->auth_ids[1],
99 false);
100 if (IS_ERR(key))
101 return -ENOKEY;
102
103 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
104 ret = -ENOKEY;
105 else
106 ret = verify_signature(key, sig);
107 key_put(key);
108 return ret;
109 }
110
111 static bool match_either_id(const struct asymmetric_key_ids *pair,
112 const struct asymmetric_key_id *single)
113 {
114 return (asymmetric_key_id_same(pair->id[0], single) ||
115 asymmetric_key_id_same(pair->id[1], single));
116 }
117
118 static int key_or_keyring_common(struct key *dest_keyring,
119 const struct key_type *type,
120 const union key_payload *payload,
121 struct key *trusted, bool check_dest)
122 {
123 const struct public_key_signature *sig;
124 struct key *key = NULL;
125 int ret;
126
127 pr_devel("==>%s()\n", __func__);
128
129 if (!dest_keyring)
130 return -ENOKEY;
131 else if (dest_keyring->type != &key_type_keyring)
132 return -EOPNOTSUPP;
133
134 if (!trusted && !check_dest)
135 return -ENOKEY;
136
137 if (type != &key_type_asymmetric)
138 return -EOPNOTSUPP;
139
140 sig = payload->data[asym_auth];
141 if (!sig)
142 return -ENOPKG;
143 if (!sig->auth_ids[0] && !sig->auth_ids[1])
144 return -ENOKEY;
145
146 if (trusted) {
147 if (trusted->type == &key_type_keyring) {
148
149 key = find_asymmetric_key(trusted, sig->auth_ids[0],
150 sig->auth_ids[1], false);
151 if (IS_ERR(key))
152 key = NULL;
153 } else if (trusted->type == &key_type_asymmetric) {
154 const struct asymmetric_key_ids *signer_ids;
155
156 signer_ids = asymmetric_key_ids(trusted);
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
176 const struct asymmetric_key_id *auth_id;
177
178 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
179 if (match_either_id(signer_ids, auth_id))
180 key = __key_get(trusted);
181
182 } else if (asymmetric_key_id_same(signer_ids->id[1],
183 sig->auth_ids[1]) &&
184 match_either_id(signer_ids,
185 sig->auth_ids[0])) {
186 key = __key_get(trusted);
187 }
188 } else {
189 return -EOPNOTSUPP;
190 }
191 }
192
193 if (check_dest && !key) {
194
195 key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
196 sig->auth_ids[1], false);
197 if (IS_ERR(key))
198 key = NULL;
199 }
200
201 if (!key)
202 return -ENOKEY;
203
204 ret = key_validate(key);
205 if (ret == 0)
206 ret = verify_signature(key, sig);
207
208 key_put(key);
209 return ret;
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 int restrict_link_by_key_or_keyring(struct key *dest_keyring,
231 const struct key_type *type,
232 const union key_payload *payload,
233 struct key *trusted)
234 {
235 return key_or_keyring_common(dest_keyring, type, payload, trusted,
236 false);
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
258 const struct key_type *type,
259 const union key_payload *payload,
260 struct key *trusted)
261 {
262 return key_or_keyring_common(dest_keyring, type, payload, trusted,
263 true);
264 }