asymmetric_type.c 16 KB

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