1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* In-software asymmetric public-key crypto subtype
3  *
4  * See Documentation/crypto/asymmetric-keys.rst
5  *
6  * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
7  * Written by David Howells (dhowells@redhat.com)
8  */
9 
10 #define pr_fmt(fmt) "PKEY: "fmt
11 #include <crypto/akcipher.h>
12 #include <crypto/public_key.h>
13 #include <crypto/sig.h>
14 #include <keys/asymmetric-subtype.h>
15 #include <linux/asn1.h>
16 #include <linux/err.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 
23 MODULE_DESCRIPTION("In-software asymmetric public-key subtype");
24 MODULE_AUTHOR("Red Hat, Inc.");
25 MODULE_LICENSE("GPL");
26 
27 /*
28  * Provide a part of a description of the key for /proc/keys.
29  */
30 static void public_key_describe(const struct key *asymmetric_key,
31 				struct seq_file *m)
32 {
33 	struct public_key *key = asymmetric_key->payload.data[asym_crypto];
34 
35 	if (key)
36 		seq_printf(m, "%s.%s", key->id_type, key->pkey_algo);
37 }
38 
39 /*
40  * Destroy a public key algorithm key.
41  */
42 void public_key_free(struct public_key *key)
43 {
44 	if (key) {
45 		kfree(key->key);
46 		kfree(key->params);
47 		kfree(key);
48 	}
49 }
50 EXPORT_SYMBOL_GPL(public_key_free);
51 
52 /*
53  * Destroy a public key algorithm key.
54  */
55 static void public_key_destroy(void *payload0, void *payload3)
56 {
57 	public_key_free(payload0);
58 	public_key_signature_free(payload3);
59 }
60 
61 /*
62  * Given a public_key, and an encoding and hash_algo to be used for signing
63  * and/or verification with that key, determine the name of the corresponding
64  * akcipher algorithm.  Also check that encoding and hash_algo are allowed.
65  */
66 static int
67 software_key_determine_akcipher(const struct public_key *pkey,
68 				const char *encoding, const char *hash_algo,
69 				char alg_name[CRYPTO_MAX_ALG_NAME], bool *sig,
70 				enum kernel_pkey_operation op)
71 {
72 	int n;
73 
74 	*sig = true;
75 
76 	if (!encoding)
77 		return -EINVAL;
78 
79 	if (strcmp(pkey->pkey_algo, "rsa") == 0) {
80 		/*
81 		 * RSA signatures usually use EMSA-PKCS1-1_5 [RFC3447 sec 8.2].
82 		 */
83 		if (strcmp(encoding, "pkcs1") == 0) {
84 			if (!hash_algo) {
85 				*sig = false;
86 				n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
87 					     "pkcs1pad(%s)",
88 					     pkey->pkey_algo);
89 			} else {
90 				*sig = op == kernel_pkey_sign ||
91 				       op == kernel_pkey_verify;
92 				n = snprintf(alg_name, CRYPTO_MAX_ALG_NAME,
93 					     "pkcs1pad(%s,%s)",
94 					     pkey->pkey_algo, hash_algo);
95 			}
96 			return n >= CRYPTO_MAX_ALG_NAME ? -EINVAL : 0;
97 		}
98 		if (strcmp(encoding, "raw") != 0)
99 			return -EINVAL;
100 		/*
101 		 * Raw RSA cannot differentiate between different hash
102 		 * algorithms.
103 		 */
104 		if (hash_algo)
105 			return -EINVAL;
106 		*sig = false;
107 	} else if (strncmp(pkey->pkey_algo, "ecdsa", 5) == 0) {
108 		if (strcmp(encoding, "x962") != 0)
109 			return -EINVAL;
110 		/*
111 		 * ECDSA signatures are taken over a raw hash, so they don't
112 		 * differentiate between different hash algorithms.  That means
113 		 * that the verifier should hard-code a specific hash algorithm.
114 		 * Unfortunately, in practice ECDSA is used with multiple SHAs,
115 		 * so we have to allow all of them and not just one.
116 		 */
117 		if (!hash_algo)
118 			return -EINVAL;
119 		if (strcmp(hash_algo, "sha1") != 0 &&
120 		    strcmp(hash_algo, "sha224") != 0 &&
121 		    strcmp(hash_algo, "sha256") != 0 &&
122 		    strcmp(hash_algo, "sha384") != 0 &&
123 		    strcmp(hash_algo, "sha512") != 0)
124 			return -EINVAL;
125 	} else if (strcmp(pkey->pkey_algo, "sm2") == 0) {
126 		if (strcmp(encoding, "raw") != 0)
127 			return -EINVAL;
128 		if (!hash_algo)
129 			return -EINVAL;
130 		if (strcmp(hash_algo, "sm3") != 0)
131 			return -EINVAL;
132 	} else if (strcmp(pkey->pkey_algo, "ecrdsa") == 0) {
133 		if (strcmp(encoding, "raw") != 0)
134 			return -EINVAL;
135 		if (!hash_algo)
136 			return -EINVAL;
137 		if (strcmp(hash_algo, "streebog256") != 0 &&
138 		    strcmp(hash_algo, "streebog512") != 0)
139 			return -EINVAL;
140 	} else {
141 		/* Unknown public key algorithm */
142 		return -ENOPKG;
143 	}
144 	if (strscpy(alg_name, pkey->pkey_algo, CRYPTO_MAX_ALG_NAME) < 0)
145 		return -EINVAL;
146 	return 0;
147 }
148 
149 static u8 *pkey_pack_u32(u8 *dst, u32 val)
150 {
151 	memcpy(dst, &val, sizeof(val));
152 	return dst + sizeof(val);
153 }
154 
155 /*
156  * Query information about a key.
157  */
158 static int software_key_query(const struct kernel_pkey_params *params,
159 			      struct kernel_pkey_query *info)
160 {
161 	struct crypto_akcipher *tfm;
162 	struct public_key *pkey = params->key->payload.data[asym_crypto];
163 	char alg_name[CRYPTO_MAX_ALG_NAME];
164 	struct crypto_sig *sig;
165 	u8 *key, *ptr;
166 	int ret, len;
167 	bool issig;
168 
169 	ret = software_key_determine_akcipher(pkey, params->encoding,
170 					      params->hash_algo, alg_name,
171 					      &issig, kernel_pkey_sign);
172 	if (ret < 0)
173 		return ret;
174 
175 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
176 		      GFP_KERNEL);
177 	if (!key)
178 		return -ENOMEM;
179 
180 	memcpy(key, pkey->key, pkey->keylen);
181 	ptr = key + pkey->keylen;
182 	ptr = pkey_pack_u32(ptr, pkey->algo);
183 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
184 	memcpy(ptr, pkey->params, pkey->paramlen);
185 
186 	if (issig) {
187 		sig = crypto_alloc_sig(alg_name, 0, 0);
188 		if (IS_ERR(sig)) {
189 			ret = PTR_ERR(sig);
190 			goto error_free_key;
191 		}
192 
193 		if (pkey->key_is_private)
194 			ret = crypto_sig_set_privkey(sig, key, pkey->keylen);
195 		else
196 			ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
197 		if (ret < 0)
198 			goto error_free_tfm;
199 
200 		len = crypto_sig_maxsize(sig);
201 
202 		info->supported_ops = KEYCTL_SUPPORTS_VERIFY;
203 		if (pkey->key_is_private)
204 			info->supported_ops |= KEYCTL_SUPPORTS_SIGN;
205 
206 		if (strcmp(params->encoding, "pkcs1") == 0) {
207 			info->supported_ops |= KEYCTL_SUPPORTS_ENCRYPT;
208 			if (pkey->key_is_private)
209 				info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
210 		}
211 	} else {
212 		tfm = crypto_alloc_akcipher(alg_name, 0, 0);
213 		if (IS_ERR(tfm)) {
214 			ret = PTR_ERR(tfm);
215 			goto error_free_key;
216 		}
217 
218 		if (pkey->key_is_private)
219 			ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
220 		else
221 			ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
222 		if (ret < 0)
223 			goto error_free_tfm;
224 
225 		len = crypto_akcipher_maxsize(tfm);
226 
227 		info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT;
228 		if (pkey->key_is_private)
229 			info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
230 	}
231 
232 	info->key_size = len * 8;
233 
234 	if (strncmp(pkey->pkey_algo, "ecdsa", 5) == 0) {
235 		/*
236 		 * ECDSA key sizes are much smaller than RSA, and thus could
237 		 * operate on (hashed) inputs that are larger than key size.
238 		 * For example SHA384-hashed input used with secp256r1
239 		 * based keys.  Set max_data_size to be at least as large as
240 		 * the largest supported hash size (SHA512)
241 		 */
242 		info->max_data_size = 64;
243 
244 		/*
245 		 * Verify takes ECDSA-Sig (described in RFC 5480) as input,
246 		 * which is actually 2 'key_size'-bit integers encoded in
247 		 * ASN.1.  Account for the ASN.1 encoding overhead here.
248 		 */
249 		info->max_sig_size = 2 * (len + 3) + 2;
250 	} else {
251 		info->max_data_size = len;
252 		info->max_sig_size = len;
253 	}
254 
255 	info->max_enc_size = len;
256 	info->max_dec_size = len;
257 
258 	ret = 0;
259 
260 error_free_tfm:
261 	if (issig)
262 		crypto_free_sig(sig);
263 	else
264 		crypto_free_akcipher(tfm);
265 error_free_key:
266 	kfree(key);
267 	pr_devel("<==%s() = %d\n", __func__, ret);
268 	return ret;
269 }
270 
271 /*
272  * Do encryption, decryption and signing ops.
273  */
274 static int software_key_eds_op(struct kernel_pkey_params *params,
275 			       const void *in, void *out)
276 {
277 	const struct public_key *pkey = params->key->payload.data[asym_crypto];
278 	char alg_name[CRYPTO_MAX_ALG_NAME];
279 	struct crypto_akcipher *tfm;
280 	struct crypto_sig *sig;
281 	char *key, *ptr;
282 	bool issig;
283 	int ksz;
284 	int ret;
285 
286 	pr_devel("==>%s()\n", __func__);
287 
288 	ret = software_key_determine_akcipher(pkey, params->encoding,
289 					      params->hash_algo, alg_name,
290 					      &issig, params->op);
291 	if (ret < 0)
292 		return ret;
293 
294 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
295 		      GFP_KERNEL);
296 	if (!key)
297 		return -ENOMEM;
298 
299 	memcpy(key, pkey->key, pkey->keylen);
300 	ptr = key + pkey->keylen;
301 	ptr = pkey_pack_u32(ptr, pkey->algo);
302 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
303 	memcpy(ptr, pkey->params, pkey->paramlen);
304 
305 	if (issig) {
306 		sig = crypto_alloc_sig(alg_name, 0, 0);
307 		if (IS_ERR(sig)) {
308 			ret = PTR_ERR(sig);
309 			goto error_free_key;
310 		}
311 
312 		if (pkey->key_is_private)
313 			ret = crypto_sig_set_privkey(sig, key, pkey->keylen);
314 		else
315 			ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
316 		if (ret)
317 			goto error_free_tfm;
318 
319 		ksz = crypto_sig_maxsize(sig);
320 	} else {
321 		tfm = crypto_alloc_akcipher(alg_name, 0, 0);
322 		if (IS_ERR(tfm)) {
323 			ret = PTR_ERR(tfm);
324 			goto error_free_key;
325 		}
326 
327 		if (pkey->key_is_private)
328 			ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
329 		else
330 			ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
331 		if (ret)
332 			goto error_free_tfm;
333 
334 		ksz = crypto_akcipher_maxsize(tfm);
335 	}
336 
337 	ret = -EINVAL;
338 
339 	/* Perform the encryption calculation. */
340 	switch (params->op) {
341 	case kernel_pkey_encrypt:
342 		if (issig)
343 			break;
344 		ret = crypto_akcipher_sync_encrypt(tfm, in, params->in_len,
345 						   out, params->out_len);
346 		break;
347 	case kernel_pkey_decrypt:
348 		if (issig)
349 			break;
350 		ret = crypto_akcipher_sync_decrypt(tfm, in, params->in_len,
351 						   out, params->out_len);
352 		break;
353 	case kernel_pkey_sign:
354 		if (!issig)
355 			break;
356 		ret = crypto_sig_sign(sig, in, params->in_len,
357 				      out, params->out_len);
358 		break;
359 	default:
360 		BUG();
361 	}
362 
363 	if (ret == 0)
364 		ret = ksz;
365 
366 error_free_tfm:
367 	if (issig)
368 		crypto_free_sig(sig);
369 	else
370 		crypto_free_akcipher(tfm);
371 error_free_key:
372 	kfree(key);
373 	pr_devel("<==%s() = %d\n", __func__, ret);
374 	return ret;
375 }
376 
377 /*
378  * Verify a signature using a public key.
379  */
380 int public_key_verify_signature(const struct public_key *pkey,
381 				const struct public_key_signature *sig)
382 {
383 	char alg_name[CRYPTO_MAX_ALG_NAME];
384 	struct crypto_sig *tfm;
385 	char *key, *ptr;
386 	bool issig;
387 	int ret;
388 
389 	pr_devel("==>%s()\n", __func__);
390 
391 	BUG_ON(!pkey);
392 	BUG_ON(!sig);
393 	BUG_ON(!sig->s);
394 
395 	/*
396 	 * If the signature specifies a public key algorithm, it *must* match
397 	 * the key's actual public key algorithm.
398 	 *
399 	 * Small exception: ECDSA signatures don't specify the curve, but ECDSA
400 	 * keys do.  So the strings can mismatch slightly in that case:
401 	 * "ecdsa-nist-*" for the key, but "ecdsa" for the signature.
402 	 */
403 	if (sig->pkey_algo) {
404 		if (strcmp(pkey->pkey_algo, sig->pkey_algo) != 0 &&
405 		    (strncmp(pkey->pkey_algo, "ecdsa-", 6) != 0 ||
406 		     strcmp(sig->pkey_algo, "ecdsa") != 0))
407 			return -EKEYREJECTED;
408 	}
409 
410 	ret = software_key_determine_akcipher(pkey, sig->encoding,
411 					      sig->hash_algo, alg_name,
412 					      &issig, kernel_pkey_verify);
413 	if (ret < 0)
414 		return ret;
415 
416 	tfm = crypto_alloc_sig(alg_name, 0, 0);
417 	if (IS_ERR(tfm))
418 		return PTR_ERR(tfm);
419 
420 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
421 		      GFP_KERNEL);
422 	if (!key) {
423 		ret = -ENOMEM;
424 		goto error_free_tfm;
425 	}
426 
427 	memcpy(key, pkey->key, pkey->keylen);
428 	ptr = key + pkey->keylen;
429 	ptr = pkey_pack_u32(ptr, pkey->algo);
430 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
431 	memcpy(ptr, pkey->params, pkey->paramlen);
432 
433 	if (pkey->key_is_private)
434 		ret = crypto_sig_set_privkey(tfm, key, pkey->keylen);
435 	else
436 		ret = crypto_sig_set_pubkey(tfm, key, pkey->keylen);
437 	if (ret)
438 		goto error_free_key;
439 
440 	ret = crypto_sig_verify(tfm, sig->s, sig->s_size,
441 				sig->digest, sig->digest_size);
442 
443 error_free_key:
444 	kfree(key);
445 error_free_tfm:
446 	crypto_free_sig(tfm);
447 	pr_devel("<==%s() = %d\n", __func__, ret);
448 	if (WARN_ON_ONCE(ret > 0))
449 		ret = -EINVAL;
450 	return ret;
451 }
452 EXPORT_SYMBOL_GPL(public_key_verify_signature);
453 
454 static int public_key_verify_signature_2(const struct key *key,
455 					 const struct public_key_signature *sig)
456 {
457 	const struct public_key *pk = key->payload.data[asym_crypto];
458 	return public_key_verify_signature(pk, sig);
459 }
460 
461 /*
462  * Public key algorithm asymmetric key subtype
463  */
464 struct asymmetric_key_subtype public_key_subtype = {
465 	.owner			= THIS_MODULE,
466 	.name			= "public_key",
467 	.name_len		= sizeof("public_key") - 1,
468 	.describe		= public_key_describe,
469 	.destroy		= public_key_destroy,
470 	.query			= software_key_query,
471 	.eds_op			= software_key_eds_op,
472 	.verify_signature	= public_key_verify_signature_2,
473 };
474 EXPORT_SYMBOL_GPL(public_key_subtype);
475