1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Public-key operation keyctls
3 *
4 * Copyright (C) 2016 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8 #include <linux/slab.h>
9 #include <linux/err.h>
10 #include <linux/key.h>
11 #include <linux/keyctl.h>
12 #include <linux/parser.h>
13 #include <linux/uaccess.h>
14 #include <keys/user-type.h>
15 #include "internal.h"
16
keyctl_pkey_params_free(struct kernel_pkey_params * params)17 static void keyctl_pkey_params_free(struct kernel_pkey_params *params)
18 {
19 kfree(params->info);
20 key_put(params->key);
21 }
22
23 enum {
24 Opt_err,
25 Opt_enc, /* "enc=<encoding>" eg. "enc=oaep" */
26 Opt_hash, /* "hash=<digest-name>" eg. "hash=sha1" */
27 };
28
29 static const match_table_t param_keys = {
30 { Opt_enc, "enc=%s" },
31 { Opt_hash, "hash=%s" },
32 { Opt_err, NULL }
33 };
34
35 /*
36 * Parse the information string which consists of key=val pairs.
37 */
keyctl_pkey_params_parse(struct kernel_pkey_params * params)38 static int keyctl_pkey_params_parse(struct kernel_pkey_params *params)
39 {
40 unsigned long token_mask = 0;
41 substring_t args[MAX_OPT_ARGS];
42 char *c = params->info, *p, *q;
43 int token;
44
45 while ((p = strsep(&c, " \t"))) {
46 if (*p == '\0' || *p == ' ' || *p == '\t')
47 continue;
48 token = match_token(p, param_keys, args);
49 if (token == Opt_err)
50 return -EINVAL;
51 if (__test_and_set_bit(token, &token_mask))
52 return -EINVAL;
53 q = args[0].from;
54 if (!q[0])
55 return -EINVAL;
56
57 switch (token) {
58 case Opt_enc:
59 params->encoding = q;
60 break;
61
62 case Opt_hash:
63 params->hash_algo = q;
64 break;
65
66 default:
67 return -EINVAL;
68 }
69 }
70
71 return 0;
72 }
73
74 /*
75 * Interpret parameters. Callers must always call the free function
76 * on params, even if an error is returned.
77 */
keyctl_pkey_params_get(key_serial_t id,const char __user * _info,struct kernel_pkey_params * params)78 static int keyctl_pkey_params_get(key_serial_t id,
79 const char __user *_info,
80 struct kernel_pkey_params *params)
81 {
82 key_ref_t key_ref;
83 void *p;
84 int ret;
85
86 memset(params, 0, sizeof(*params));
87 params->encoding = "raw";
88
89 p = strndup_user(_info, PAGE_SIZE);
90 if (IS_ERR(p))
91 return PTR_ERR(p);
92 params->info = p;
93
94 ret = keyctl_pkey_params_parse(params);
95 if (ret < 0)
96 return ret;
97
98 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
99 if (IS_ERR(key_ref))
100 return PTR_ERR(key_ref);
101 params->key = key_ref_to_ptr(key_ref);
102
103 if (!params->key->type->asym_query)
104 return -EOPNOTSUPP;
105
106 return 0;
107 }
108
109 /*
110 * Get parameters from userspace. Callers must always call the free function
111 * on params, even if an error is returned.
112 */
keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user * _params,const char __user * _info,int op,struct kernel_pkey_params * params)113 static int keyctl_pkey_params_get_2(const struct keyctl_pkey_params __user *_params,
114 const char __user *_info,
115 int op,
116 struct kernel_pkey_params *params)
117 {
118 struct keyctl_pkey_params uparams;
119 struct kernel_pkey_query info;
120 int ret;
121
122 memset(params, 0, sizeof(*params));
123 params->encoding = "raw";
124
125 if (copy_from_user(&uparams, _params, sizeof(uparams)) != 0)
126 return -EFAULT;
127
128 ret = keyctl_pkey_params_get(uparams.key_id, _info, params);
129 if (ret < 0)
130 return ret;
131
132 ret = params->key->type->asym_query(params, &info);
133 if (ret < 0)
134 return ret;
135
136 switch (op) {
137 case KEYCTL_PKEY_ENCRYPT:
138 if (uparams.in_len > info.max_dec_size ||
139 uparams.out_len > info.max_enc_size)
140 return -EINVAL;
141 break;
142 case KEYCTL_PKEY_DECRYPT:
143 if (uparams.in_len > info.max_enc_size ||
144 uparams.out_len > info.max_dec_size)
145 return -EINVAL;
146 break;
147 case KEYCTL_PKEY_SIGN:
148 if (uparams.in_len > info.max_data_size ||
149 uparams.out_len > info.max_sig_size)
150 return -EINVAL;
151 break;
152 case KEYCTL_PKEY_VERIFY:
153 if (uparams.in_len > info.max_data_size ||
154 uparams.in2_len > info.max_sig_size)
155 return -EINVAL;
156 break;
157 default:
158 BUG();
159 }
160
161 params->in_len = uparams.in_len;
162 params->out_len = uparams.out_len; /* Note: same as in2_len */
163 return 0;
164 }
165
166 /*
167 * Query information about an asymmetric key.
168 */
keyctl_pkey_query(key_serial_t id,const char __user * _info,struct keyctl_pkey_query __user * _res)169 long keyctl_pkey_query(key_serial_t id,
170 const char __user *_info,
171 struct keyctl_pkey_query __user *_res)
172 {
173 struct kernel_pkey_params params;
174 struct kernel_pkey_query res;
175 long ret;
176
177 memset(¶ms, 0, sizeof(params));
178
179 ret = keyctl_pkey_params_get(id, _info, ¶ms);
180 if (ret < 0)
181 goto error;
182
183 ret = params.key->type->asym_query(¶ms, &res);
184 if (ret < 0)
185 goto error;
186
187 ret = -EFAULT;
188 if (copy_to_user(_res, &res, sizeof(res)) == 0 &&
189 clear_user(_res->__spare, sizeof(_res->__spare)) == 0)
190 ret = 0;
191
192 error:
193 keyctl_pkey_params_free(¶ms);
194 return ret;
195 }
196
197 /*
198 * Encrypt/decrypt/sign
199 *
200 * Encrypt data, decrypt data or sign data using a public key.
201 *
202 * _info is a string of supplementary information in key=val format. For
203 * instance, it might contain:
204 *
205 * "enc=pkcs1 hash=sha256"
206 *
207 * where enc= specifies the encoding and hash= selects the OID to go in that
208 * particular encoding if required. If enc= isn't supplied, it's assumed that
209 * the caller is supplying raw values.
210 *
211 * If successful, the amount of data written into the output buffer is
212 * returned.
213 */
keyctl_pkey_e_d_s(int op,const struct keyctl_pkey_params __user * _params,const char __user * _info,const void __user * _in,void __user * _out)214 long keyctl_pkey_e_d_s(int op,
215 const struct keyctl_pkey_params __user *_params,
216 const char __user *_info,
217 const void __user *_in,
218 void __user *_out)
219 {
220 struct kernel_pkey_params params;
221 void *in, *out;
222 long ret;
223
224 ret = keyctl_pkey_params_get_2(_params, _info, op, ¶ms);
225 if (ret < 0)
226 goto error_params;
227
228 ret = -EOPNOTSUPP;
229 if (!params.key->type->asym_eds_op)
230 goto error_params;
231
232 switch (op) {
233 case KEYCTL_PKEY_ENCRYPT:
234 params.op = kernel_pkey_encrypt;
235 break;
236 case KEYCTL_PKEY_DECRYPT:
237 params.op = kernel_pkey_decrypt;
238 break;
239 case KEYCTL_PKEY_SIGN:
240 params.op = kernel_pkey_sign;
241 break;
242 default:
243 BUG();
244 }
245
246 in = memdup_user(_in, params.in_len);
247 if (IS_ERR(in)) {
248 ret = PTR_ERR(in);
249 goto error_params;
250 }
251
252 ret = -ENOMEM;
253 out = kmalloc(params.out_len, GFP_KERNEL);
254 if (!out)
255 goto error_in;
256
257 ret = params.key->type->asym_eds_op(¶ms, in, out);
258 if (ret < 0)
259 goto error_out;
260
261 if (copy_to_user(_out, out, ret) != 0)
262 ret = -EFAULT;
263
264 error_out:
265 kfree(out);
266 error_in:
267 kfree(in);
268 error_params:
269 keyctl_pkey_params_free(¶ms);
270 return ret;
271 }
272
273 /*
274 * Verify a signature.
275 *
276 * Verify a public key signature using the given key, or if not given, search
277 * for a matching key.
278 *
279 * _info is a string of supplementary information in key=val format. For
280 * instance, it might contain:
281 *
282 * "enc=pkcs1 hash=sha256"
283 *
284 * where enc= specifies the signature blob encoding and hash= selects the OID
285 * to go in that particular encoding. If enc= isn't supplied, it's assumed
286 * that the caller is supplying raw values.
287 *
288 * If successful, 0 is returned.
289 */
keyctl_pkey_verify(const struct keyctl_pkey_params __user * _params,const char __user * _info,const void __user * _in,const void __user * _in2)290 long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
291 const char __user *_info,
292 const void __user *_in,
293 const void __user *_in2)
294 {
295 struct kernel_pkey_params params;
296 void *in, *in2;
297 long ret;
298
299 ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
300 ¶ms);
301 if (ret < 0)
302 goto error_params;
303
304 ret = -EOPNOTSUPP;
305 if (!params.key->type->asym_verify_signature)
306 goto error_params;
307
308 in = memdup_user(_in, params.in_len);
309 if (IS_ERR(in)) {
310 ret = PTR_ERR(in);
311 goto error_params;
312 }
313
314 in2 = memdup_user(_in2, params.in2_len);
315 if (IS_ERR(in2)) {
316 ret = PTR_ERR(in2);
317 goto error_in;
318 }
319
320 params.op = kernel_pkey_verify;
321 ret = params.key->type->asym_verify_signature(¶ms, in, in2);
322
323 kfree(in2);
324 error_in:
325 kfree(in);
326 error_params:
327 keyctl_pkey_params_free(¶ms);
328 return ret;
329 }
330