keymgmt_lib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/core_names.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/nelem.h"
  12. #include "crypto/evp.h"
  13. #include "internal/core.h"
  14. #include "internal/provider.h"
  15. #include "evp_local.h"
  16. /*
  17. * match_type() checks if two EVP_KEYMGMT are matching key types. This
  18. * function assumes that the caller has made all the necessary NULL checks.
  19. */
  20. static int match_type(const EVP_KEYMGMT *keymgmt1, const EVP_KEYMGMT *keymgmt2)
  21. {
  22. const char *name2 = EVP_KEYMGMT_get0_name(keymgmt2);
  23. return EVP_KEYMGMT_is_a(keymgmt1, name2);
  24. }
  25. int evp_keymgmt_util_try_import(const OSSL_PARAM params[], void *arg)
  26. {
  27. struct evp_keymgmt_util_try_import_data_st *data = arg;
  28. int delete_on_error = 0;
  29. /* Just in time creation of keydata */
  30. if (data->keydata == NULL) {
  31. if ((data->keydata = evp_keymgmt_newdata(data->keymgmt)) == NULL) {
  32. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  33. return 0;
  34. }
  35. delete_on_error = 1;
  36. }
  37. /*
  38. * It's fine if there was no data to transfer, we just end up with an
  39. * empty destination key.
  40. */
  41. if (params[0].key == NULL)
  42. return 1;
  43. if (evp_keymgmt_import(data->keymgmt, data->keydata, data->selection,
  44. params))
  45. return 1;
  46. if (delete_on_error) {
  47. evp_keymgmt_freedata(data->keymgmt, data->keydata);
  48. data->keydata = NULL;
  49. }
  50. return 0;
  51. }
  52. int evp_keymgmt_util_assign_pkey(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt,
  53. void *keydata)
  54. {
  55. if (pkey == NULL || keymgmt == NULL || keydata == NULL
  56. || !EVP_PKEY_set_type_by_keymgmt(pkey, keymgmt)) {
  57. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  58. return 0;
  59. }
  60. pkey->keydata = keydata;
  61. evp_keymgmt_util_cache_keyinfo(pkey);
  62. return 1;
  63. }
  64. EVP_PKEY *evp_keymgmt_util_make_pkey(EVP_KEYMGMT *keymgmt, void *keydata)
  65. {
  66. EVP_PKEY *pkey = NULL;
  67. if (keymgmt == NULL
  68. || keydata == NULL
  69. || (pkey = EVP_PKEY_new()) == NULL
  70. || !evp_keymgmt_util_assign_pkey(pkey, keymgmt, keydata)) {
  71. EVP_PKEY_free(pkey);
  72. return NULL;
  73. }
  74. return pkey;
  75. }
  76. int evp_keymgmt_util_export(const EVP_PKEY *pk, int selection,
  77. OSSL_CALLBACK *export_cb, void *export_cbarg)
  78. {
  79. if (pk == NULL || export_cb == NULL)
  80. return 0;
  81. return evp_keymgmt_export(pk->keymgmt, pk->keydata, selection,
  82. export_cb, export_cbarg);
  83. }
  84. void *evp_keymgmt_util_export_to_provider(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
  85. int selection)
  86. {
  87. struct evp_keymgmt_util_try_import_data_st import_data;
  88. OP_CACHE_ELEM *op;
  89. /* Export to where? */
  90. if (keymgmt == NULL)
  91. return NULL;
  92. /* If we have an unassigned key, give up */
  93. if (pk->keydata == NULL)
  94. return NULL;
  95. /*
  96. * If |keymgmt| matches the "origin" |keymgmt|, there is no more to do.
  97. * The "origin" is determined by the |keymgmt| pointers being identical
  98. * or when the provider and the name ID match. The latter case handles the
  99. * situation where the fetch cache is flushed and a "new" key manager is
  100. * created.
  101. */
  102. if (pk->keymgmt == keymgmt
  103. || (pk->keymgmt->name_id == keymgmt->name_id
  104. && pk->keymgmt->prov == keymgmt->prov))
  105. return pk->keydata;
  106. if (!CRYPTO_THREAD_read_lock(pk->lock))
  107. return NULL;
  108. /*
  109. * If the provider native "origin" hasn't changed since last time, we
  110. * try to find our keymgmt in the operation cache. If it has changed
  111. * and our keymgmt isn't found, we will clear the cache further down.
  112. */
  113. if (pk->dirty_cnt == pk->dirty_cnt_copy) {
  114. /* If this key is already exported to |keymgmt|, no more to do */
  115. op = evp_keymgmt_util_find_operation_cache(pk, keymgmt, selection);
  116. if (op != NULL && op->keymgmt != NULL) {
  117. void *ret = op->keydata;
  118. CRYPTO_THREAD_unlock(pk->lock);
  119. return ret;
  120. }
  121. }
  122. CRYPTO_THREAD_unlock(pk->lock);
  123. /* If the "origin" |keymgmt| doesn't support exporting, give up */
  124. if (pk->keymgmt->export == NULL)
  125. return NULL;
  126. /*
  127. * Make sure that the type of the keymgmt to export to matches the type
  128. * of the "origin"
  129. */
  130. if (!ossl_assert(match_type(pk->keymgmt, keymgmt)))
  131. return NULL;
  132. /*
  133. * We look at the already cached provider keys, and import from the
  134. * first that supports it (i.e. use its export function), and export
  135. * the imported data to the new provider.
  136. */
  137. /* Setup for the export callback */
  138. import_data.keydata = NULL; /* evp_keymgmt_util_try_import will create it */
  139. import_data.keymgmt = keymgmt;
  140. import_data.selection = selection;
  141. /*
  142. * The export function calls the callback (evp_keymgmt_util_try_import),
  143. * which does the import for us. If successful, we're done.
  144. */
  145. if (!evp_keymgmt_util_export(pk, selection,
  146. &evp_keymgmt_util_try_import, &import_data))
  147. /* If there was an error, bail out */
  148. return NULL;
  149. if (!CRYPTO_THREAD_write_lock(pk->lock)) {
  150. evp_keymgmt_freedata(keymgmt, import_data.keydata);
  151. return NULL;
  152. }
  153. /* Check to make sure some other thread didn't get there first */
  154. op = evp_keymgmt_util_find_operation_cache(pk, keymgmt, selection);
  155. if (op != NULL && op->keydata != NULL) {
  156. void *ret = op->keydata;
  157. CRYPTO_THREAD_unlock(pk->lock);
  158. /*
  159. * Another thread seemms to have already exported this so we abandon
  160. * all the work we just did.
  161. */
  162. evp_keymgmt_freedata(keymgmt, import_data.keydata);
  163. return ret;
  164. }
  165. /*
  166. * If the dirty counter changed since last time, then clear the
  167. * operation cache. In that case, we know that |i| is zero.
  168. */
  169. if (pk->dirty_cnt != pk->dirty_cnt_copy)
  170. evp_keymgmt_util_clear_operation_cache(pk);
  171. /* Add the new export to the operation cache */
  172. if (!evp_keymgmt_util_cache_keydata(pk, keymgmt, import_data.keydata,
  173. selection)) {
  174. CRYPTO_THREAD_unlock(pk->lock);
  175. evp_keymgmt_freedata(keymgmt, import_data.keydata);
  176. return NULL;
  177. }
  178. /* Synchronize the dirty count */
  179. pk->dirty_cnt_copy = pk->dirty_cnt;
  180. CRYPTO_THREAD_unlock(pk->lock);
  181. return import_data.keydata;
  182. }
  183. static void op_cache_free(OP_CACHE_ELEM *e)
  184. {
  185. evp_keymgmt_freedata(e->keymgmt, e->keydata);
  186. EVP_KEYMGMT_free(e->keymgmt);
  187. OPENSSL_free(e);
  188. }
  189. int evp_keymgmt_util_clear_operation_cache(EVP_PKEY *pk)
  190. {
  191. if (pk != NULL) {
  192. sk_OP_CACHE_ELEM_pop_free(pk->operation_cache, op_cache_free);
  193. pk->operation_cache = NULL;
  194. }
  195. return 1;
  196. }
  197. OP_CACHE_ELEM *evp_keymgmt_util_find_operation_cache(EVP_PKEY *pk,
  198. EVP_KEYMGMT *keymgmt,
  199. int selection)
  200. {
  201. int i, end = sk_OP_CACHE_ELEM_num(pk->operation_cache);
  202. OP_CACHE_ELEM *p;
  203. /*
  204. * A comparison and sk_P_CACHE_ELEM_find() are avoided to not cause
  205. * problems when we've only a read lock.
  206. * A keymgmt is a match if the |keymgmt| pointers are identical or if the
  207. * provider and the name ID match
  208. */
  209. for (i = 0; i < end; i++) {
  210. p = sk_OP_CACHE_ELEM_value(pk->operation_cache, i);
  211. if ((p->selection & selection) == selection
  212. && (keymgmt == p->keymgmt
  213. || (keymgmt->name_id == p->keymgmt->name_id
  214. && keymgmt->prov == p->keymgmt->prov)))
  215. return p;
  216. }
  217. return NULL;
  218. }
  219. int evp_keymgmt_util_cache_keydata(EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
  220. void *keydata, int selection)
  221. {
  222. OP_CACHE_ELEM *p = NULL;
  223. if (keydata != NULL) {
  224. if (pk->operation_cache == NULL) {
  225. pk->operation_cache = sk_OP_CACHE_ELEM_new_null();
  226. if (pk->operation_cache == NULL)
  227. return 0;
  228. }
  229. p = OPENSSL_malloc(sizeof(*p));
  230. if (p == NULL)
  231. return 0;
  232. p->keydata = keydata;
  233. p->keymgmt = keymgmt;
  234. p->selection = selection;
  235. if (!EVP_KEYMGMT_up_ref(keymgmt)) {
  236. OPENSSL_free(p);
  237. return 0;
  238. }
  239. if (!sk_OP_CACHE_ELEM_push(pk->operation_cache, p)) {
  240. EVP_KEYMGMT_free(keymgmt);
  241. OPENSSL_free(p);
  242. return 0;
  243. }
  244. }
  245. return 1;
  246. }
  247. void evp_keymgmt_util_cache_keyinfo(EVP_PKEY *pk)
  248. {
  249. /*
  250. * Cache information about the provider "origin" key.
  251. *
  252. * This services functions like EVP_PKEY_get_size, EVP_PKEY_get_bits, etc
  253. */
  254. if (pk->keydata != NULL) {
  255. int bits = 0;
  256. int security_bits = 0;
  257. int size = 0;
  258. OSSL_PARAM params[4];
  259. params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_BITS, &bits);
  260. params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_SECURITY_BITS,
  261. &security_bits);
  262. params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_MAX_SIZE, &size);
  263. params[3] = OSSL_PARAM_construct_end();
  264. if (evp_keymgmt_get_params(pk->keymgmt, pk->keydata, params)) {
  265. pk->cache.size = size;
  266. pk->cache.bits = bits;
  267. pk->cache.security_bits = security_bits;
  268. }
  269. }
  270. }
  271. void *evp_keymgmt_util_fromdata(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
  272. int selection, const OSSL_PARAM params[])
  273. {
  274. void *keydata = NULL;
  275. if ((keydata = evp_keymgmt_newdata(keymgmt)) == NULL
  276. || !evp_keymgmt_import(keymgmt, keydata, selection, params)
  277. || !evp_keymgmt_util_assign_pkey(target, keymgmt, keydata)) {
  278. evp_keymgmt_freedata(keymgmt, keydata);
  279. keydata = NULL;
  280. }
  281. return keydata;
  282. }
  283. int evp_keymgmt_util_has(EVP_PKEY *pk, int selection)
  284. {
  285. /* Check if key is even assigned */
  286. if (pk->keymgmt == NULL)
  287. return 0;
  288. return evp_keymgmt_has(pk->keymgmt, pk->keydata, selection);
  289. }
  290. /*
  291. * evp_keymgmt_util_match() doesn't just look at the provider side "origin",
  292. * but also in the operation cache to see if there's any common keymgmt that
  293. * supplies OP_keymgmt_match.
  294. *
  295. * evp_keymgmt_util_match() adheres to the return values that EVP_PKEY_eq()
  296. * and EVP_PKEY_parameters_eq() return, i.e.:
  297. *
  298. * 1 same key
  299. * 0 not same key
  300. * -1 not same key type
  301. * -2 unsupported operation
  302. */
  303. int evp_keymgmt_util_match(EVP_PKEY *pk1, EVP_PKEY *pk2, int selection)
  304. {
  305. EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
  306. void *keydata1 = NULL, *keydata2 = NULL;
  307. if (pk1 == NULL || pk2 == NULL) {
  308. if (pk1 == NULL && pk2 == NULL)
  309. return 1;
  310. return 0;
  311. }
  312. keymgmt1 = pk1->keymgmt;
  313. keydata1 = pk1->keydata;
  314. keymgmt2 = pk2->keymgmt;
  315. keydata2 = pk2->keydata;
  316. if (keymgmt1 != keymgmt2) {
  317. /*
  318. * The condition for a successful cross export is that the
  319. * keydata to be exported is NULL (typed, but otherwise empty
  320. * EVP_PKEY), or that it was possible to export it with
  321. * evp_keymgmt_util_export_to_provider().
  322. *
  323. * We use |ok| to determine if it's ok to cross export one way,
  324. * but also to determine if we should attempt a cross export
  325. * the other way. There's no point doing it both ways.
  326. */
  327. int ok = 0;
  328. /* Complex case, where the keymgmt differ */
  329. if (keymgmt1 != NULL
  330. && keymgmt2 != NULL
  331. && !match_type(keymgmt1, keymgmt2)) {
  332. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
  333. return -1; /* Not the same type */
  334. }
  335. /*
  336. * The key types are determined to match, so we try cross export,
  337. * but only to keymgmt's that supply a matching function.
  338. */
  339. if (keymgmt2 != NULL
  340. && keymgmt2->match != NULL) {
  341. void *tmp_keydata = NULL;
  342. ok = 1;
  343. if (keydata1 != NULL) {
  344. tmp_keydata =
  345. evp_keymgmt_util_export_to_provider(pk1, keymgmt2,
  346. selection);
  347. ok = (tmp_keydata != NULL);
  348. }
  349. if (ok) {
  350. keymgmt1 = keymgmt2;
  351. keydata1 = tmp_keydata;
  352. }
  353. }
  354. /*
  355. * If we've successfully cross exported one way, there's no point
  356. * doing it the other way, hence the |!ok| check.
  357. */
  358. if (!ok
  359. && keymgmt1 != NULL
  360. && keymgmt1->match != NULL) {
  361. void *tmp_keydata = NULL;
  362. ok = 1;
  363. if (keydata2 != NULL) {
  364. tmp_keydata =
  365. evp_keymgmt_util_export_to_provider(pk2, keymgmt1,
  366. selection);
  367. ok = (tmp_keydata != NULL);
  368. }
  369. if (ok) {
  370. keymgmt2 = keymgmt1;
  371. keydata2 = tmp_keydata;
  372. }
  373. }
  374. }
  375. /* If we still don't have matching keymgmt implementations, we give up */
  376. if (keymgmt1 != keymgmt2)
  377. return -2;
  378. /* If both keydata are NULL, then they're the same key */
  379. if (keydata1 == NULL && keydata2 == NULL)
  380. return 1;
  381. /* If only one of the keydata is NULL, then they're different keys */
  382. if (keydata1 == NULL || keydata2 == NULL)
  383. return 0;
  384. /* If both keydata are non-NULL, we let the backend decide */
  385. return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
  386. }
  387. int evp_keymgmt_util_copy(EVP_PKEY *to, EVP_PKEY *from, int selection)
  388. {
  389. /* Save copies of pointers we want to play with without affecting |to| */
  390. EVP_KEYMGMT *to_keymgmt = to->keymgmt;
  391. void *to_keydata = to->keydata, *alloc_keydata = NULL;
  392. /* An unassigned key can't be copied */
  393. if (from == NULL || from->keydata == NULL)
  394. return 0;
  395. /*
  396. * If |to| is unassigned, ensure it gets the same KEYMGMT as |from|,
  397. * Note that the final setting of KEYMGMT is done further down, with
  398. * EVP_PKEY_set_type_by_keymgmt(); we don't want to do that prematurely.
  399. */
  400. if (to_keymgmt == NULL)
  401. to_keymgmt = from->keymgmt;
  402. if (to_keymgmt == from->keymgmt && to_keymgmt->dup != NULL
  403. && to_keydata == NULL) {
  404. to_keydata = alloc_keydata = evp_keymgmt_dup(to_keymgmt,
  405. from->keydata,
  406. selection);
  407. if (to_keydata == NULL)
  408. return 0;
  409. } else if (match_type(to_keymgmt, from->keymgmt)) {
  410. struct evp_keymgmt_util_try_import_data_st import_data;
  411. import_data.keymgmt = to_keymgmt;
  412. import_data.keydata = to_keydata;
  413. import_data.selection = selection;
  414. if (!evp_keymgmt_util_export(from, selection,
  415. &evp_keymgmt_util_try_import,
  416. &import_data))
  417. return 0;
  418. /*
  419. * In case to_keydata was previously unallocated,
  420. * evp_keymgmt_util_try_import() may have created it for us.
  421. */
  422. if (to_keydata == NULL)
  423. to_keydata = alloc_keydata = import_data.keydata;
  424. } else {
  425. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
  426. return 0;
  427. }
  428. /*
  429. * We only need to set the |to| type when its |keymgmt| isn't set.
  430. * We can then just set its |keydata| to what we have, which might
  431. * be exactly what it had when entering this function.
  432. * This is a bit different from using evp_keymgmt_util_assign_pkey(),
  433. * which isn't as careful with |to|'s original |keymgmt|, since it's
  434. * meant to forcibly reassign an EVP_PKEY no matter what, which is
  435. * why we don't use that one here.
  436. */
  437. if (to->keymgmt == NULL
  438. && !EVP_PKEY_set_type_by_keymgmt(to, to_keymgmt)) {
  439. evp_keymgmt_freedata(to_keymgmt, alloc_keydata);
  440. return 0;
  441. }
  442. to->keydata = to_keydata;
  443. evp_keymgmt_util_cache_keyinfo(to);
  444. return 1;
  445. }
  446. void *evp_keymgmt_util_gen(EVP_PKEY *target, EVP_KEYMGMT *keymgmt,
  447. void *genctx, OSSL_CALLBACK *cb, void *cbarg)
  448. {
  449. void *keydata = NULL;
  450. if ((keydata = evp_keymgmt_gen(keymgmt, genctx, cb, cbarg)) == NULL
  451. || !evp_keymgmt_util_assign_pkey(target, keymgmt, keydata)) {
  452. evp_keymgmt_freedata(keymgmt, keydata);
  453. keydata = NULL;
  454. }
  455. return keydata;
  456. }
  457. /*
  458. * Returns the same numbers as EVP_PKEY_get_default_digest_name()
  459. * When the string from the EVP_KEYMGMT implementation is "", we use
  460. * SN_undef, since that corresponds to what EVP_PKEY_get_default_nid()
  461. * returns for no digest.
  462. */
  463. int evp_keymgmt_util_get_deflt_digest_name(EVP_KEYMGMT *keymgmt,
  464. void *keydata,
  465. char *mdname, size_t mdname_sz)
  466. {
  467. OSSL_PARAM params[3];
  468. char mddefault[100] = "";
  469. char mdmandatory[100] = "";
  470. char *result = NULL;
  471. int rv = -2;
  472. params[0] =
  473. OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
  474. mddefault, sizeof(mddefault));
  475. params[1] =
  476. OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
  477. mdmandatory,
  478. sizeof(mdmandatory));
  479. params[2] = OSSL_PARAM_construct_end();
  480. if (!evp_keymgmt_get_params(keymgmt, keydata, params))
  481. return 0;
  482. if (OSSL_PARAM_modified(params + 1)) {
  483. if (params[1].return_size <= 1) /* Only a NUL byte */
  484. result = SN_undef;
  485. else
  486. result = mdmandatory;
  487. rv = 2;
  488. } else if (OSSL_PARAM_modified(params)) {
  489. if (params[0].return_size <= 1) /* Only a NUL byte */
  490. result = SN_undef;
  491. else
  492. result = mddefault;
  493. rv = 1;
  494. }
  495. if (rv > 0)
  496. OPENSSL_strlcpy(mdname, result, mdname_sz);
  497. return rv;
  498. }
  499. /*
  500. * If |keymgmt| has the method function |query_operation_name|, use it to get
  501. * the name of a supported operation identity. Otherwise, return the keytype,
  502. * assuming that it works as a default operation name.
  503. */
  504. const char *evp_keymgmt_util_query_operation_name(EVP_KEYMGMT *keymgmt,
  505. int op_id)
  506. {
  507. const char *name = NULL;
  508. if (keymgmt != NULL) {
  509. if (keymgmt->query_operation_name != NULL)
  510. name = keymgmt->query_operation_name(op_id);
  511. if (name == NULL)
  512. name = EVP_KEYMGMT_get0_name(keymgmt);
  513. }
  514. return name;
  515. }