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