asymmetric_type.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Asymmetric public-key cryptography key type
  3. *
  4. * See Documentation/crypto/asymmetric-keys.txt
  5. *
  6. * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
  7. * Written by David Howells (dhowells@redhat.com)
  8. */
  9. #ifndef __UBOOT__
  10. #include <keys/asymmetric-subtype.h>
  11. #include <keys/asymmetric-parser.h>
  12. #endif
  13. #include <crypto/public_key.h>
  14. #ifdef __UBOOT__
  15. #include <linux/compat.h>
  16. #include <linux/ctype.h>
  17. #include <linux/string.h>
  18. #else
  19. #include <linux/seq_file.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/ctype.h>
  23. #endif
  24. #ifdef __UBOOT__
  25. #include <keys/asymmetric-type.h>
  26. #else
  27. #include <keys/system_keyring.h>
  28. #include <keys/user-type.h>
  29. #include "asymmetric_keys.h"
  30. #endif
  31. MODULE_LICENSE("GPL");
  32. #ifndef __UBOOT__
  33. const char *const key_being_used_for[NR__KEY_BEING_USED_FOR] = {
  34. [VERIFYING_MODULE_SIGNATURE] = "mod sig",
  35. [VERIFYING_FIRMWARE_SIGNATURE] = "firmware sig",
  36. [VERIFYING_KEXEC_PE_SIGNATURE] = "kexec PE sig",
  37. [VERIFYING_KEY_SIGNATURE] = "key sig",
  38. [VERIFYING_KEY_SELF_SIGNATURE] = "key self sig",
  39. [VERIFYING_UNSPECIFIED_SIGNATURE] = "unspec sig",
  40. };
  41. EXPORT_SYMBOL_GPL(key_being_used_for);
  42. static LIST_HEAD(asymmetric_key_parsers);
  43. static DECLARE_RWSEM(asymmetric_key_parsers_sem);
  44. /**
  45. * find_asymmetric_key - Find a key by ID.
  46. * @keyring: The keys to search.
  47. * @id_0: The first ID to look for or NULL.
  48. * @id_1: The second ID to look for or NULL.
  49. * @partial: Use partial match if true, exact if false.
  50. *
  51. * Find a key in the given keyring by identifier. The preferred identifier is
  52. * the id_0 and the fallback identifier is the id_1. If both are given, the
  53. * lookup is by the former, but the latter must also match.
  54. */
  55. struct key *find_asymmetric_key(struct key *keyring,
  56. const struct asymmetric_key_id *id_0,
  57. const struct asymmetric_key_id *id_1,
  58. bool partial)
  59. {
  60. struct key *key;
  61. key_ref_t ref;
  62. const char *lookup;
  63. char *req, *p;
  64. int len;
  65. BUG_ON(!id_0 && !id_1);
  66. if (id_0) {
  67. lookup = id_0->data;
  68. len = id_0->len;
  69. } else {
  70. lookup = id_1->data;
  71. len = id_1->len;
  72. }
  73. /* Construct an identifier "id:<keyid>". */
  74. p = req = kmalloc(2 + 1 + len * 2 + 1, GFP_KERNEL);
  75. if (!req)
  76. return ERR_PTR(-ENOMEM);
  77. if (partial) {
  78. *p++ = 'i';
  79. *p++ = 'd';
  80. } else {
  81. *p++ = 'e';
  82. *p++ = 'x';
  83. }
  84. *p++ = ':';
  85. p = bin2hex(p, lookup, len);
  86. *p = 0;
  87. pr_debug("Look up: \"%s\"\n", req);
  88. ref = keyring_search(make_key_ref(keyring, 1),
  89. &key_type_asymmetric, req, true);
  90. if (IS_ERR(ref))
  91. pr_debug("Request for key '%s' err %ld\n", req, PTR_ERR(ref));
  92. kfree(req);
  93. if (IS_ERR(ref)) {
  94. switch (PTR_ERR(ref)) {
  95. /* Hide some search errors */
  96. case -EACCES:
  97. case -ENOTDIR:
  98. case -EAGAIN:
  99. return ERR_PTR(-ENOKEY);
  100. default:
  101. return ERR_CAST(ref);
  102. }
  103. }
  104. key = key_ref_to_ptr(ref);
  105. if (id_0 && id_1) {
  106. const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
  107. if (!kids->id[1]) {
  108. pr_debug("First ID matches, but second is missing\n");
  109. goto reject;
  110. }
  111. if (!asymmetric_key_id_same(id_1, kids->id[1])) {
  112. pr_debug("First ID matches, but second does not\n");
  113. goto reject;
  114. }
  115. }
  116. pr_devel("<==%s() = 0 [%x]\n", __func__, key_serial(key));
  117. return key;
  118. reject:
  119. key_put(key);
  120. return ERR_PTR(-EKEYREJECTED);
  121. }
  122. EXPORT_SYMBOL_GPL(find_asymmetric_key);
  123. #endif /* !__UBOOT__ */
  124. /**
  125. * asymmetric_key_generate_id: Construct an asymmetric key ID
  126. * @val_1: First binary blob
  127. * @len_1: Length of first binary blob
  128. * @val_2: Second binary blob
  129. * @len_2: Length of second binary blob
  130. *
  131. * Construct an asymmetric key ID from a pair of binary blobs.
  132. */
  133. struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
  134. size_t len_1,
  135. const void *val_2,
  136. size_t len_2)
  137. {
  138. struct asymmetric_key_id *kid;
  139. kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
  140. GFP_KERNEL);
  141. if (!kid)
  142. return ERR_PTR(-ENOMEM);
  143. kid->len = len_1 + len_2;
  144. memcpy(kid->data, val_1, len_1);
  145. memcpy(kid->data + len_1, val_2, len_2);
  146. return kid;
  147. }
  148. EXPORT_SYMBOL_GPL(asymmetric_key_generate_id);
  149. /**
  150. * asymmetric_key_id_same - Return true if two asymmetric keys IDs are the same.
  151. * @kid_1, @kid_2: The key IDs to compare
  152. */
  153. bool asymmetric_key_id_same(const struct asymmetric_key_id *kid1,
  154. const struct asymmetric_key_id *kid2)
  155. {
  156. if (!kid1 || !kid2)
  157. return false;
  158. if (kid1->len != kid2->len)
  159. return false;
  160. return memcmp(kid1->data, kid2->data, kid1->len) == 0;
  161. }
  162. EXPORT_SYMBOL_GPL(asymmetric_key_id_same);
  163. /**
  164. * asymmetric_key_id_partial - Return true if two asymmetric keys IDs
  165. * partially match
  166. * @kid_1, @kid_2: The key IDs to compare
  167. */
  168. bool asymmetric_key_id_partial(const struct asymmetric_key_id *kid1,
  169. const struct asymmetric_key_id *kid2)
  170. {
  171. if (!kid1 || !kid2)
  172. return false;
  173. if (kid1->len < kid2->len)
  174. return false;
  175. return memcmp(kid1->data + (kid1->len - kid2->len),
  176. kid2->data, kid2->len) == 0;
  177. }
  178. EXPORT_SYMBOL_GPL(asymmetric_key_id_partial);
  179. #ifndef __UBOOT__
  180. /**
  181. * asymmetric_match_key_ids - Search asymmetric key IDs
  182. * @kids: The list of key IDs to check
  183. * @match_id: The key ID we're looking for
  184. * @match: The match function to use
  185. */
  186. static bool asymmetric_match_key_ids(
  187. const struct asymmetric_key_ids *kids,
  188. const struct asymmetric_key_id *match_id,
  189. bool (*match)(const struct asymmetric_key_id *kid1,
  190. const struct asymmetric_key_id *kid2))
  191. {
  192. int i;
  193. if (!kids || !match_id)
  194. return false;
  195. for (i = 0; i < ARRAY_SIZE(kids->id); i++)
  196. if (match(kids->id[i], match_id))
  197. return true;
  198. return false;
  199. }
  200. /* helper function can be called directly with pre-allocated memory */
  201. inline int __asymmetric_key_hex_to_key_id(const char *id,
  202. struct asymmetric_key_id *match_id,
  203. size_t hexlen)
  204. {
  205. match_id->len = hexlen;
  206. return hex2bin(match_id->data, id, hexlen);
  207. }
  208. /**
  209. * asymmetric_key_hex_to_key_id - Convert a hex string into a key ID.
  210. * @id: The ID as a hex string.
  211. */
  212. struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
  213. {
  214. struct asymmetric_key_id *match_id;
  215. size_t asciihexlen;
  216. int ret;
  217. if (!*id)
  218. return ERR_PTR(-EINVAL);
  219. asciihexlen = strlen(id);
  220. if (asciihexlen & 1)
  221. return ERR_PTR(-EINVAL);
  222. match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
  223. GFP_KERNEL);
  224. if (!match_id)
  225. return ERR_PTR(-ENOMEM);
  226. ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
  227. if (ret < 0) {
  228. kfree(match_id);
  229. return ERR_PTR(-EINVAL);
  230. }
  231. return match_id;
  232. }
  233. /*
  234. * Match asymmetric keys by an exact match on an ID.
  235. */
  236. static bool asymmetric_key_cmp(const struct key *key,
  237. const struct key_match_data *match_data)
  238. {
  239. const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
  240. const struct asymmetric_key_id *match_id = match_data->preparsed;
  241. return asymmetric_match_key_ids(kids, match_id,
  242. asymmetric_key_id_same);
  243. }
  244. /*
  245. * Match asymmetric keys by a partial match on an IDs.
  246. */
  247. static bool asymmetric_key_cmp_partial(const struct key *key,
  248. const struct key_match_data *match_data)
  249. {
  250. const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
  251. const struct asymmetric_key_id *match_id = match_data->preparsed;
  252. return asymmetric_match_key_ids(kids, match_id,
  253. asymmetric_key_id_partial);
  254. }
  255. /*
  256. * Preparse the match criterion. If we don't set lookup_type and cmp,
  257. * the default will be an exact match on the key description.
  258. *
  259. * There are some specifiers for matching key IDs rather than by the key
  260. * description:
  261. *
  262. * "id:<id>" - find a key by partial match on any available ID
  263. * "ex:<id>" - find a key by exact match on any available ID
  264. *
  265. * These have to be searched by iteration rather than by direct lookup because
  266. * the key is hashed according to its description.
  267. */
  268. static int asymmetric_key_match_preparse(struct key_match_data *match_data)
  269. {
  270. struct asymmetric_key_id *match_id;
  271. const char *spec = match_data->raw_data;
  272. const char *id;
  273. bool (*cmp)(const struct key *, const struct key_match_data *) =
  274. asymmetric_key_cmp;
  275. if (!spec || !*spec)
  276. return -EINVAL;
  277. if (spec[0] == 'i' &&
  278. spec[1] == 'd' &&
  279. spec[2] == ':') {
  280. id = spec + 3;
  281. cmp = asymmetric_key_cmp_partial;
  282. } else if (spec[0] == 'e' &&
  283. spec[1] == 'x' &&
  284. spec[2] == ':') {
  285. id = spec + 3;
  286. } else {
  287. goto default_match;
  288. }
  289. match_id = asymmetric_key_hex_to_key_id(id);
  290. if (IS_ERR(match_id))
  291. return PTR_ERR(match_id);
  292. match_data->preparsed = match_id;
  293. match_data->cmp = cmp;
  294. match_data->lookup_type = KEYRING_SEARCH_LOOKUP_ITERATE;
  295. return 0;
  296. default_match:
  297. return 0;
  298. }
  299. /*
  300. * Free the preparsed the match criterion.
  301. */
  302. static void asymmetric_key_match_free(struct key_match_data *match_data)
  303. {
  304. kfree(match_data->preparsed);
  305. }
  306. /*
  307. * Describe the asymmetric key
  308. */
  309. static void asymmetric_key_describe(const struct key *key, struct seq_file *m)
  310. {
  311. const struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
  312. const struct asymmetric_key_ids *kids = asymmetric_key_ids(key);
  313. const struct asymmetric_key_id *kid;
  314. const unsigned char *p;
  315. int n;
  316. seq_puts(m, key->description);
  317. if (subtype) {
  318. seq_puts(m, ": ");
  319. subtype->describe(key, m);
  320. if (kids && kids->id[1]) {
  321. kid = kids->id[1];
  322. seq_putc(m, ' ');
  323. n = kid->len;
  324. p = kid->data;
  325. if (n > 4) {
  326. p += n - 4;
  327. n = 4;
  328. }
  329. seq_printf(m, "%*phN", n, p);
  330. }
  331. seq_puts(m, " [");
  332. /* put something here to indicate the key's capabilities */
  333. seq_putc(m, ']');
  334. }
  335. }
  336. /*
  337. * Preparse a asymmetric payload to get format the contents appropriately for the
  338. * internal payload to cut down on the number of scans of the data performed.
  339. *
  340. * We also generate a proposed description from the contents of the key that
  341. * can be used to name the key if the user doesn't want to provide one.
  342. */
  343. static int asymmetric_key_preparse(struct key_preparsed_payload *prep)
  344. {
  345. struct asymmetric_key_parser *parser;
  346. int ret;
  347. pr_devel("==>%s()\n", __func__);
  348. if (prep->datalen == 0)
  349. return -EINVAL;
  350. down_read(&asymmetric_key_parsers_sem);
  351. ret = -EBADMSG;
  352. list_for_each_entry(parser, &asymmetric_key_parsers, link) {
  353. pr_debug("Trying parser '%s'\n", parser->name);
  354. ret = parser->parse(prep);
  355. if (ret != -EBADMSG) {
  356. pr_debug("Parser recognised the format (ret %d)\n",
  357. ret);
  358. break;
  359. }
  360. }
  361. up_read(&asymmetric_key_parsers_sem);
  362. pr_devel("<==%s() = %d\n", __func__, ret);
  363. return ret;
  364. }
  365. /*
  366. * Clean up the key ID list
  367. */
  368. static void asymmetric_key_free_kids(struct asymmetric_key_ids *kids)
  369. {
  370. int i;
  371. if (kids) {
  372. for (i = 0; i < ARRAY_SIZE(kids->id); i++)
  373. kfree(kids->id[i]);
  374. kfree(kids);
  375. }
  376. }
  377. /*
  378. * Clean up the preparse data
  379. */
  380. static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
  381. {
  382. struct asymmetric_key_subtype *subtype = prep->payload.data[asym_subtype];
  383. struct asymmetric_key_ids *kids = prep->payload.data[asym_key_ids];
  384. pr_devel("==>%s()\n", __func__);
  385. if (subtype) {
  386. subtype->destroy(prep->payload.data[asym_crypto],
  387. prep->payload.data[asym_auth]);
  388. module_put(subtype->owner);
  389. }
  390. asymmetric_key_free_kids(kids);
  391. kfree(prep->description);
  392. }
  393. /*
  394. * dispose of the data dangling from the corpse of a asymmetric key
  395. */
  396. static void asymmetric_key_destroy(struct key *key)
  397. {
  398. struct asymmetric_key_subtype *subtype = asymmetric_key_subtype(key);
  399. struct asymmetric_key_ids *kids = key->payload.data[asym_key_ids];
  400. void *data = key->payload.data[asym_crypto];
  401. void *auth = key->payload.data[asym_auth];
  402. key->payload.data[asym_crypto] = NULL;
  403. key->payload.data[asym_subtype] = NULL;
  404. key->payload.data[asym_key_ids] = NULL;
  405. key->payload.data[asym_auth] = NULL;
  406. if (subtype) {
  407. subtype->destroy(data, auth);
  408. module_put(subtype->owner);
  409. }
  410. asymmetric_key_free_kids(kids);
  411. }
  412. static struct key_restriction *asymmetric_restriction_alloc(
  413. key_restrict_link_func_t check,
  414. struct key *key)
  415. {
  416. struct key_restriction *keyres =
  417. kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
  418. if (!keyres)
  419. return ERR_PTR(-ENOMEM);
  420. keyres->check = check;
  421. keyres->key = key;
  422. keyres->keytype = &key_type_asymmetric;
  423. return keyres;
  424. }
  425. /*
  426. * look up keyring restrict functions for asymmetric keys
  427. */
  428. static struct key_restriction *asymmetric_lookup_restriction(
  429. const char *restriction)
  430. {
  431. char *restrict_method;
  432. char *parse_buf;
  433. char *next;
  434. struct key_restriction *ret = ERR_PTR(-EINVAL);
  435. if (strcmp("builtin_trusted", restriction) == 0)
  436. return asymmetric_restriction_alloc(
  437. restrict_link_by_builtin_trusted, NULL);
  438. if (strcmp("builtin_and_secondary_trusted", restriction) == 0)
  439. return asymmetric_restriction_alloc(
  440. restrict_link_by_builtin_and_secondary_trusted, NULL);
  441. parse_buf = kstrndup(restriction, PAGE_SIZE, GFP_KERNEL);
  442. if (!parse_buf)
  443. return ERR_PTR(-ENOMEM);
  444. next = parse_buf;
  445. restrict_method = strsep(&next, ":");
  446. if ((strcmp(restrict_method, "key_or_keyring") == 0) && next) {
  447. char *key_text;
  448. key_serial_t serial;
  449. struct key *key;
  450. key_restrict_link_func_t link_fn =
  451. restrict_link_by_key_or_keyring;
  452. bool allow_null_key = false;
  453. key_text = strsep(&next, ":");
  454. if (next) {
  455. if (strcmp(next, "chain") != 0)
  456. goto out;
  457. link_fn = restrict_link_by_key_or_keyring_chain;
  458. allow_null_key = true;
  459. }
  460. if (kstrtos32(key_text, 0, &serial) < 0)
  461. goto out;
  462. if ((serial == 0) && allow_null_key) {
  463. key = NULL;
  464. } else {
  465. key = key_lookup(serial);
  466. if (IS_ERR(key)) {
  467. ret = ERR_CAST(key);
  468. goto out;
  469. }
  470. }
  471. ret = asymmetric_restriction_alloc(link_fn, key);
  472. if (IS_ERR(ret))
  473. key_put(key);
  474. }
  475. out:
  476. kfree(parse_buf);
  477. return ret;
  478. }
  479. int asymmetric_key_eds_op(struct kernel_pkey_params *params,
  480. const void *in, void *out)
  481. {
  482. const struct asymmetric_key_subtype *subtype;
  483. struct key *key = params->key;
  484. int ret;
  485. pr_devel("==>%s()\n", __func__);
  486. if (key->type != &key_type_asymmetric)
  487. return -EINVAL;
  488. subtype = asymmetric_key_subtype(key);
  489. if (!subtype ||
  490. !key->payload.data[0])
  491. return -EINVAL;
  492. if (!subtype->eds_op)
  493. return -ENOTSUPP;
  494. ret = subtype->eds_op(params, in, out);
  495. pr_devel("<==%s() = %d\n", __func__, ret);
  496. return ret;
  497. }
  498. static int asymmetric_key_verify_signature(struct kernel_pkey_params *params,
  499. const void *in, const void *in2)
  500. {
  501. struct public_key_signature sig = {
  502. .s_size = params->in2_len,
  503. .digest_size = params->in_len,
  504. .encoding = params->encoding,
  505. .hash_algo = params->hash_algo,
  506. .digest = (void *)in,
  507. .s = (void *)in2,
  508. };
  509. return verify_signature(params->key, &sig);
  510. }
  511. struct key_type key_type_asymmetric = {
  512. .name = "asymmetric",
  513. .preparse = asymmetric_key_preparse,
  514. .free_preparse = asymmetric_key_free_preparse,
  515. .instantiate = generic_key_instantiate,
  516. .match_preparse = asymmetric_key_match_preparse,
  517. .match_free = asymmetric_key_match_free,
  518. .destroy = asymmetric_key_destroy,
  519. .describe = asymmetric_key_describe,
  520. .lookup_restriction = asymmetric_lookup_restriction,
  521. .asym_query = query_asymmetric_key,
  522. .asym_eds_op = asymmetric_key_eds_op,
  523. .asym_verify_signature = asymmetric_key_verify_signature,
  524. };
  525. EXPORT_SYMBOL_GPL(key_type_asymmetric);
  526. /**
  527. * register_asymmetric_key_parser - Register a asymmetric key blob parser
  528. * @parser: The parser to register
  529. */
  530. int register_asymmetric_key_parser(struct asymmetric_key_parser *parser)
  531. {
  532. struct asymmetric_key_parser *cursor;
  533. int ret;
  534. down_write(&asymmetric_key_parsers_sem);
  535. list_for_each_entry(cursor, &asymmetric_key_parsers, link) {
  536. if (strcmp(cursor->name, parser->name) == 0) {
  537. pr_err("Asymmetric key parser '%s' already registered\n",
  538. parser->name);
  539. ret = -EEXIST;
  540. goto out;
  541. }
  542. }
  543. list_add_tail(&parser->link, &asymmetric_key_parsers);
  544. pr_notice("Asymmetric key parser '%s' registered\n", parser->name);
  545. ret = 0;
  546. out:
  547. up_write(&asymmetric_key_parsers_sem);
  548. return ret;
  549. }
  550. EXPORT_SYMBOL_GPL(register_asymmetric_key_parser);
  551. /**
  552. * unregister_asymmetric_key_parser - Unregister a asymmetric key blob parser
  553. * @parser: The parser to unregister
  554. */
  555. void unregister_asymmetric_key_parser(struct asymmetric_key_parser *parser)
  556. {
  557. down_write(&asymmetric_key_parsers_sem);
  558. list_del(&parser->link);
  559. up_write(&asymmetric_key_parsers_sem);
  560. pr_notice("Asymmetric key parser '%s' unregistered\n", parser->name);
  561. }
  562. EXPORT_SYMBOL_GPL(unregister_asymmetric_key_parser);
  563. /*
  564. * Module stuff
  565. */
  566. static int __init asymmetric_key_init(void)
  567. {
  568. return register_key_type(&key_type_asymmetric);
  569. }
  570. static void __exit asymmetric_key_cleanup(void)
  571. {
  572. unregister_key_type(&key_type_asymmetric);
  573. }
  574. module_init(asymmetric_key_init);
  575. module_exit(asymmetric_key_cleanup);
  576. #endif /* !__UBOOT__ */