public_key.c 12 KB

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