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