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 			goto error_free_key;
190 
191 		if (pkey->key_is_private)
192 			ret = crypto_sig_set_privkey(sig, key, pkey->keylen);
193 		else
194 			ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
195 		if (ret < 0)
196 			goto error_free_tfm;
197 
198 		len = crypto_sig_maxsize(sig);
199 
200 		info->supported_ops = KEYCTL_SUPPORTS_VERIFY;
201 		if (pkey->key_is_private)
202 			info->supported_ops |= KEYCTL_SUPPORTS_SIGN;
203 
204 		if (strcmp(params->encoding, "pkcs1") == 0) {
205 			info->supported_ops |= KEYCTL_SUPPORTS_ENCRYPT;
206 			if (pkey->key_is_private)
207 				info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
208 		}
209 	} else {
210 		tfm = crypto_alloc_akcipher(alg_name, 0, 0);
211 		if (IS_ERR(tfm))
212 			goto error_free_key;
213 
214 		if (pkey->key_is_private)
215 			ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
216 		else
217 			ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
218 		if (ret < 0)
219 			goto error_free_tfm;
220 
221 		len = crypto_akcipher_maxsize(tfm);
222 
223 		info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT;
224 		if (pkey->key_is_private)
225 			info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
226 	}
227 
228 	info->key_size = len * 8;
229 
230 	if (strncmp(pkey->pkey_algo, "ecdsa", 5) == 0) {
231 		/*
232 		 * ECDSA key sizes are much smaller than RSA, and thus could
233 		 * operate on (hashed) inputs that are larger than key size.
234 		 * For example SHA384-hashed input used with secp256r1
235 		 * based keys.  Set max_data_size to be at least as large as
236 		 * the largest supported hash size (SHA512)
237 		 */
238 		info->max_data_size = 64;
239 
240 		/*
241 		 * Verify takes ECDSA-Sig (described in RFC 5480) as input,
242 		 * which is actually 2 'key_size'-bit integers encoded in
243 		 * ASN.1.  Account for the ASN.1 encoding overhead here.
244 		 */
245 		info->max_sig_size = 2 * (len + 3) + 2;
246 	} else {
247 		info->max_data_size = len;
248 		info->max_sig_size = len;
249 	}
250 
251 	info->max_enc_size = len;
252 	info->max_dec_size = len;
253 
254 	ret = 0;
255 
256 error_free_tfm:
257 	if (issig)
258 		crypto_free_sig(sig);
259 	else
260 		crypto_free_akcipher(tfm);
261 error_free_key:
262 	kfree(key);
263 	pr_devel("<==%s() = %d\n", __func__, ret);
264 	return ret;
265 }
266 
267 /*
268  * Do encryption, decryption and signing ops.
269  */
270 static int software_key_eds_op(struct kernel_pkey_params *params,
271 			       const void *in, void *out)
272 {
273 	const struct public_key *pkey = params->key->payload.data[asym_crypto];
274 	char alg_name[CRYPTO_MAX_ALG_NAME];
275 	struct crypto_akcipher *tfm;
276 	struct crypto_sig *sig;
277 	char *key, *ptr;
278 	bool issig;
279 	int ksz;
280 	int ret;
281 
282 	pr_devel("==>%s()\n", __func__);
283 
284 	ret = software_key_determine_akcipher(pkey, params->encoding,
285 					      params->hash_algo, alg_name,
286 					      &issig, params->op);
287 	if (ret < 0)
288 		return ret;
289 
290 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
291 		      GFP_KERNEL);
292 	if (!key)
293 		return -ENOMEM;
294 
295 	memcpy(key, pkey->key, pkey->keylen);
296 	ptr = key + pkey->keylen;
297 	ptr = pkey_pack_u32(ptr, pkey->algo);
298 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
299 	memcpy(ptr, pkey->params, pkey->paramlen);
300 
301 	if (issig) {
302 		sig = crypto_alloc_sig(alg_name, 0, 0);
303 		if (IS_ERR(sig))
304 			goto error_free_key;
305 
306 		if (pkey->key_is_private)
307 			ret = crypto_sig_set_privkey(sig, key, pkey->keylen);
308 		else
309 			ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
310 		if (ret)
311 			goto error_free_tfm;
312 
313 		ksz = crypto_sig_maxsize(sig);
314 	} else {
315 		tfm = crypto_alloc_akcipher(alg_name, 0, 0);
316 		if (IS_ERR(tfm))
317 			goto error_free_key;
318 
319 		if (pkey->key_is_private)
320 			ret = crypto_akcipher_set_priv_key(tfm, key, pkey->keylen);
321 		else
322 			ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
323 		if (ret)
324 			goto error_free_tfm;
325 
326 		ksz = crypto_akcipher_maxsize(tfm);
327 	}
328 
329 	ret = -EINVAL;
330 
331 	/* Perform the encryption calculation. */
332 	switch (params->op) {
333 	case kernel_pkey_encrypt:
334 		if (issig)
335 			break;
336 		ret = crypto_akcipher_sync_encrypt(tfm, in, params->in_len,
337 						   out, params->out_len);
338 		break;
339 	case kernel_pkey_decrypt:
340 		if (issig)
341 			break;
342 		ret = crypto_akcipher_sync_decrypt(tfm, in, params->in_len,
343 						   out, params->out_len);
344 		break;
345 	case kernel_pkey_sign:
346 		if (!issig)
347 			break;
348 		ret = crypto_sig_sign(sig, in, params->in_len,
349 				      out, params->out_len);
350 		break;
351 	default:
352 		BUG();
353 	}
354 
355 	if (ret == 0)
356 		ret = ksz;
357 
358 error_free_tfm:
359 	if (issig)
360 		crypto_free_sig(sig);
361 	else
362 		crypto_free_akcipher(tfm);
363 error_free_key:
364 	kfree(key);
365 	pr_devel("<==%s() = %d\n", __func__, ret);
366 	return ret;
367 }
368 
369 /*
370  * Verify a signature using a public key.
371  */
372 int public_key_verify_signature(const struct public_key *pkey,
373 				const struct public_key_signature *sig)
374 {
375 	char alg_name[CRYPTO_MAX_ALG_NAME];
376 	struct crypto_sig *tfm;
377 	char *key, *ptr;
378 	bool issig;
379 	int ret;
380 
381 	pr_devel("==>%s()\n", __func__);
382 
383 	BUG_ON(!pkey);
384 	BUG_ON(!sig);
385 	BUG_ON(!sig->s);
386 
387 	/*
388 	 * If the signature specifies a public key algorithm, it *must* match
389 	 * the key's actual public key algorithm.
390 	 *
391 	 * Small exception: ECDSA signatures don't specify the curve, but ECDSA
392 	 * keys do.  So the strings can mismatch slightly in that case:
393 	 * "ecdsa-nist-*" for the key, but "ecdsa" for the signature.
394 	 */
395 	if (sig->pkey_algo) {
396 		if (strcmp(pkey->pkey_algo, sig->pkey_algo) != 0 &&
397 		    (strncmp(pkey->pkey_algo, "ecdsa-", 6) != 0 ||
398 		     strcmp(sig->pkey_algo, "ecdsa") != 0))
399 			return -EKEYREJECTED;
400 	}
401 
402 	ret = software_key_determine_akcipher(pkey, sig->encoding,
403 					      sig->hash_algo, alg_name,
404 					      &issig, kernel_pkey_verify);
405 	if (ret < 0)
406 		return ret;
407 
408 	tfm = crypto_alloc_sig(alg_name, 0, 0);
409 	if (IS_ERR(tfm))
410 		return PTR_ERR(tfm);
411 
412 	key = kmalloc(pkey->keylen + sizeof(u32) * 2 + pkey->paramlen,
413 		      GFP_KERNEL);
414 	if (!key)
415 		goto error_free_tfm;
416 
417 	memcpy(key, pkey->key, pkey->keylen);
418 	ptr = key + pkey->keylen;
419 	ptr = pkey_pack_u32(ptr, pkey->algo);
420 	ptr = pkey_pack_u32(ptr, pkey->paramlen);
421 	memcpy(ptr, pkey->params, pkey->paramlen);
422 
423 	if (pkey->key_is_private)
424 		ret = crypto_sig_set_privkey(tfm, key, pkey->keylen);
425 	else
426 		ret = crypto_sig_set_pubkey(tfm, key, pkey->keylen);
427 	if (ret)
428 		goto error_free_key;
429 
430 	ret = crypto_sig_verify(tfm, sig->s, sig->s_size,
431 				sig->digest, sig->digest_size);
432 
433 error_free_key:
434 	kfree(key);
435 error_free_tfm:
436 	crypto_free_sig(tfm);
437 	pr_devel("<==%s() = %d\n", __func__, ret);
438 	if (WARN_ON_ONCE(ret > 0))
439 		ret = -EINVAL;
440 	return ret;
441 }
442 EXPORT_SYMBOL_GPL(public_key_verify_signature);
443 
444 static int public_key_verify_signature_2(const struct key *key,
445 					 const struct public_key_signature *sig)
446 {
447 	const struct public_key *pk = key->payload.data[asym_crypto];
448 	return public_key_verify_signature(pk, sig);
449 }
450 
451 /*
452  * Public key algorithm asymmetric key subtype
453  */
454 struct asymmetric_key_subtype public_key_subtype = {
455 	.owner			= THIS_MODULE,
456 	.name			= "public_key",
457 	.name_len		= sizeof("public_key") - 1,
458 	.describe		= public_key_describe,
459 	.destroy		= public_key_destroy,
460 	.query			= software_key_query,
461 	.eds_op			= software_key_eds_op,
462 	.verify_signature	= public_key_verify_signature_2,
463 };
464 EXPORT_SYMBOL_GPL(public_key_subtype);
465