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