keymgmt_lib.c 18 KB

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