xref: /linux/security/keys/keyctl_pkey.c (revision 889dd44491b37c890b1b4ef40876cf19032ca897)
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 
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  */
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  */
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  */
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 	down_read(&params->key->sem);
133 
134 	if (!params->key->type->asym_query) {
135 		ret = -EOPNOTSUPP;
136 		goto out_up_key_sem;
137 	}
138 
139 	ret = params->key->type->asym_query(params, &info);
140 	if (ret < 0)
141 		goto out_up_key_sem;
142 
143 	switch (op) {
144 	case KEYCTL_PKEY_ENCRYPT:
145 		if (uparams.in_len  > info.max_dec_size ||
146 		    uparams.out_len > info.max_enc_size) {
147 			ret = -EINVAL;
148 			goto out_up_key_sem;
149 		}
150 
151 		params->out_len = info.max_enc_size;
152 		break;
153 	case KEYCTL_PKEY_DECRYPT:
154 		if (uparams.in_len  > info.max_enc_size ||
155 		    uparams.out_len > info.max_dec_size) {
156 			ret = -EINVAL;
157 			goto out_up_key_sem;
158 		}
159 
160 		params->out_len = info.max_dec_size;
161 		break;
162 	case KEYCTL_PKEY_SIGN:
163 		if (uparams.in_len  > info.max_data_size ||
164 		    uparams.out_len > info.max_sig_size) {
165 			ret = -EINVAL;
166 			goto out_up_key_sem;
167 		}
168 
169 		params->out_len = info.max_sig_size;
170 		break;
171 	case KEYCTL_PKEY_VERIFY:
172 		if (uparams.in_len  > info.max_data_size ||
173 		    uparams.in2_len > info.max_sig_size) {
174 			ret = -EINVAL;
175 			goto out_up_key_sem;
176 		}
177 
178 		params->out_len = info.max_sig_size;
179 		break;
180 	default:
181 		ret = -EOPNOTSUPP;
182 		goto out_up_key_sem;
183 	}
184 
185 	params->in_len  = uparams.in_len;
186 
187 	switch (op) {
188 	case KEYCTL_PKEY_VERIFY:
189 		if (!params->key->type->asym_verify_signature)
190 			ret = -EOPNOTSUPP;
191 		break;
192 	case KEYCTL_PKEY_ENCRYPT:
193 	case KEYCTL_PKEY_DECRYPT:
194 	case KEYCTL_PKEY_SIGN:
195 		if (!params->key->type->asym_eds_op)
196 			ret = -EOPNOTSUPP;
197 		break;
198 	}
199 
200 out_up_key_sem:
201 	up_read(&params->key->sem);
202 	return ret;
203 }
204 
205 /*
206  * Query information about an asymmetric key.
207  */
208 long keyctl_pkey_query(key_serial_t id,
209 		       const char __user *_info,
210 		       struct keyctl_pkey_query __user *_res)
211 {
212 	struct kernel_pkey_params params;
213 	struct kernel_pkey_query res;
214 	long ret;
215 
216 	ret = keyctl_pkey_params_get(id, _info, &params);
217 	if (ret < 0)
218 		goto error;
219 
220 	down_read(&params.key->sem);
221 	if (!params.key->type->asym_query) {
222 		ret = -EOPNOTSUPP;
223 		goto out_up_key_sem;
224 	}
225 
226 	ret = params.key->type->asym_query(&params, &res);
227 
228 out_up_key_sem:
229 	up_read(&params.key->sem);
230 	if (ret < 0)
231 		goto error;
232 
233 	ret = -EFAULT;
234 	if (copy_to_user(_res, &res, sizeof(res)) == 0 &&
235 	    clear_user(_res->__spare, sizeof(_res->__spare)) == 0)
236 		ret = 0;
237 
238 error:
239 	keyctl_pkey_params_free(&params);
240 	return ret;
241 }
242 
243 /*
244  * Encrypt/decrypt/sign
245  *
246  * Encrypt data, decrypt data or sign data using a public key.
247  *
248  * _info is a string of supplementary information in key=val format.  For
249  * instance, it might contain:
250  *
251  *	"enc=pkcs1 hash=sha256"
252  *
253  * where enc= specifies the encoding and hash= selects the OID to go in that
254  * particular encoding if required.  If enc= isn't supplied, it's assumed that
255  * the caller is supplying raw values.
256  *
257  * If successful, the amount of data written into the output buffer is
258  * returned.
259  */
260 long keyctl_pkey_e_d_s(int op,
261 		       const struct keyctl_pkey_params __user *_params,
262 		       const char __user *_info,
263 		       const void __user *_in,
264 		       void __user *_out)
265 {
266 	struct kernel_pkey_params params;
267 	void *in, *out;
268 	long ret;
269 
270 	ret = keyctl_pkey_params_get_2(_params, _info, op, &params);
271 	if (ret < 0)
272 		goto error_params;
273 
274 	switch (op) {
275 	case KEYCTL_PKEY_ENCRYPT:
276 		params.op = kernel_pkey_encrypt;
277 		break;
278 	case KEYCTL_PKEY_DECRYPT:
279 		params.op = kernel_pkey_decrypt;
280 		break;
281 	case KEYCTL_PKEY_SIGN:
282 		params.op = kernel_pkey_sign;
283 		break;
284 	default:
285 		ret = -EOPNOTSUPP;
286 		goto error_params;
287 	}
288 
289 	in = memdup_user(_in, params.in_len);
290 	if (IS_ERR(in)) {
291 		ret = PTR_ERR(in);
292 		goto error_params;
293 	}
294 
295 	ret = -ENOMEM;
296 	out = kmalloc(params.out_len, GFP_KERNEL);
297 	if (!out)
298 		goto error_in;
299 
300 	down_read(&params.key->sem);
301 	if (!params.key->type->asym_eds_op) {
302 		ret = -EOPNOTSUPP;
303 		goto out_up_key_sem;
304 	}
305 
306 	ret = params.key->type->asym_eds_op(&params, in, out);
307 
308 out_up_key_sem:
309 	up_read(&params.key->sem);
310 	if (ret < 0)
311 		goto error_out;
312 
313 	if (copy_to_user(_out, out, ret) != 0)
314 		ret = -EFAULT;
315 
316 error_out:
317 	kfree(out);
318 error_in:
319 	kfree(in);
320 error_params:
321 	keyctl_pkey_params_free(&params);
322 	return ret;
323 }
324 
325 /*
326  * Verify a signature.
327  *
328  * Verify a public key signature using the given key, or if not given, search
329  * for a matching key.
330  *
331  * _info is a string of supplementary information in key=val format.  For
332  * instance, it might contain:
333  *
334  *	"enc=pkcs1 hash=sha256"
335  *
336  * where enc= specifies the signature blob encoding and hash= selects the OID
337  * to go in that particular encoding.  If enc= isn't supplied, it's assumed
338  * that the caller is supplying raw values.
339  *
340  * If successful, 0 is returned.
341  */
342 long keyctl_pkey_verify(const struct keyctl_pkey_params __user *_params,
343 			const char __user *_info,
344 			const void __user *_in,
345 			const void __user *_in2)
346 {
347 	struct kernel_pkey_params params;
348 	void *in, *in2;
349 	long ret;
350 
351 	ret = keyctl_pkey_params_get_2(_params, _info, KEYCTL_PKEY_VERIFY,
352 				       &params);
353 	if (ret < 0)
354 		goto out_free_params;
355 
356 	in = memdup_user(_in, params.in_len);
357 	if (IS_ERR(in)) {
358 		ret = PTR_ERR(in);
359 		goto out_free_params;
360 	}
361 
362 	in2 = memdup_user(_in2, params.in2_len);
363 	if (IS_ERR(in2)) {
364 		ret = PTR_ERR(in2);
365 		goto out_free_in;
366 	}
367 
368 	params.op = kernel_pkey_verify;
369 	down_read(&params.key->sem);
370 	if (!params.key->type->asym_verify_signature) {
371 		ret = -EOPNOTSUPP;
372 		goto out_up_key_sem;
373 	}
374 
375 	ret = params.key->type->asym_verify_signature(&params, in, in2);
376 
377 out_up_key_sem:
378 	up_read(&params.key->sem);
379 	kfree(in2);
380 
381 out_free_in:
382 	kfree(in);
383 
384 out_free_params:
385 	keyctl_pkey_params_free(&params);
386 	return ret;
387 }
388