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