keymgmt_meth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/crypto.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/err.h>
  13. #include "internal/provider.h"
  14. #include "internal/refcount.h"
  15. #include "internal/core.h"
  16. #include "crypto/evp.h"
  17. #include "evp_local.h"
  18. static void *keymgmt_new(void)
  19. {
  20. EVP_KEYMGMT *keymgmt = NULL;
  21. if ((keymgmt = OPENSSL_zalloc(sizeof(*keymgmt))) == NULL)
  22. return NULL;
  23. if (!CRYPTO_NEW_REF(&keymgmt->refcnt, 1)) {
  24. EVP_KEYMGMT_free(keymgmt);
  25. return NULL;
  26. }
  27. return keymgmt;
  28. }
  29. #ifndef FIPS_MODULE
  30. static void help_get_legacy_alg_type_from_keymgmt(const char *keytype,
  31. void *arg)
  32. {
  33. int *type = arg;
  34. if (*type == NID_undef)
  35. *type = evp_pkey_name2type(keytype);
  36. }
  37. static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt)
  38. {
  39. int type = NID_undef;
  40. EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt,
  41. &type);
  42. return type;
  43. }
  44. #endif
  45. static void *keymgmt_from_algorithm(int name_id,
  46. const OSSL_ALGORITHM *algodef,
  47. OSSL_PROVIDER *prov)
  48. {
  49. const OSSL_DISPATCH *fns = algodef->implementation;
  50. EVP_KEYMGMT *keymgmt = NULL;
  51. int setparamfncnt = 0, getparamfncnt = 0;
  52. int setgenparamfncnt = 0;
  53. int importfncnt = 0, exportfncnt = 0;
  54. int importtypesfncnt = 0, exporttypesfncnt = 0;
  55. if ((keymgmt = keymgmt_new()) == NULL)
  56. return NULL;
  57. keymgmt->name_id = name_id;
  58. if ((keymgmt->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  59. EVP_KEYMGMT_free(keymgmt);
  60. return NULL;
  61. }
  62. keymgmt->description = algodef->algorithm_description;
  63. for (; fns->function_id != 0; fns++) {
  64. switch (fns->function_id) {
  65. case OSSL_FUNC_KEYMGMT_NEW:
  66. if (keymgmt->new == NULL)
  67. keymgmt->new = OSSL_FUNC_keymgmt_new(fns);
  68. break;
  69. case OSSL_FUNC_KEYMGMT_GEN_INIT:
  70. if (keymgmt->gen_init == NULL)
  71. keymgmt->gen_init = OSSL_FUNC_keymgmt_gen_init(fns);
  72. break;
  73. case OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE:
  74. if (keymgmt->gen_set_template == NULL)
  75. keymgmt->gen_set_template =
  76. OSSL_FUNC_keymgmt_gen_set_template(fns);
  77. break;
  78. case OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS:
  79. if (keymgmt->gen_set_params == NULL) {
  80. setgenparamfncnt++;
  81. keymgmt->gen_set_params =
  82. OSSL_FUNC_keymgmt_gen_set_params(fns);
  83. }
  84. break;
  85. case OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS:
  86. if (keymgmt->gen_settable_params == NULL) {
  87. setgenparamfncnt++;
  88. keymgmt->gen_settable_params =
  89. OSSL_FUNC_keymgmt_gen_settable_params(fns);
  90. }
  91. break;
  92. case OSSL_FUNC_KEYMGMT_GEN:
  93. if (keymgmt->gen == NULL)
  94. keymgmt->gen = OSSL_FUNC_keymgmt_gen(fns);
  95. break;
  96. case OSSL_FUNC_KEYMGMT_GEN_CLEANUP:
  97. if (keymgmt->gen_cleanup == NULL)
  98. keymgmt->gen_cleanup = OSSL_FUNC_keymgmt_gen_cleanup(fns);
  99. break;
  100. case OSSL_FUNC_KEYMGMT_FREE:
  101. if (keymgmt->free == NULL)
  102. keymgmt->free = OSSL_FUNC_keymgmt_free(fns);
  103. break;
  104. case OSSL_FUNC_KEYMGMT_LOAD:
  105. if (keymgmt->load == NULL)
  106. keymgmt->load = OSSL_FUNC_keymgmt_load(fns);
  107. break;
  108. case OSSL_FUNC_KEYMGMT_GET_PARAMS:
  109. if (keymgmt->get_params == NULL) {
  110. getparamfncnt++;
  111. keymgmt->get_params = OSSL_FUNC_keymgmt_get_params(fns);
  112. }
  113. break;
  114. case OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS:
  115. if (keymgmt->gettable_params == NULL) {
  116. getparamfncnt++;
  117. keymgmt->gettable_params =
  118. OSSL_FUNC_keymgmt_gettable_params(fns);
  119. }
  120. break;
  121. case OSSL_FUNC_KEYMGMT_SET_PARAMS:
  122. if (keymgmt->set_params == NULL) {
  123. setparamfncnt++;
  124. keymgmt->set_params = OSSL_FUNC_keymgmt_set_params(fns);
  125. }
  126. break;
  127. case OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS:
  128. if (keymgmt->settable_params == NULL) {
  129. setparamfncnt++;
  130. keymgmt->settable_params =
  131. OSSL_FUNC_keymgmt_settable_params(fns);
  132. }
  133. break;
  134. case OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME:
  135. if (keymgmt->query_operation_name == NULL)
  136. keymgmt->query_operation_name =
  137. OSSL_FUNC_keymgmt_query_operation_name(fns);
  138. break;
  139. case OSSL_FUNC_KEYMGMT_HAS:
  140. if (keymgmt->has == NULL)
  141. keymgmt->has = OSSL_FUNC_keymgmt_has(fns);
  142. break;
  143. case OSSL_FUNC_KEYMGMT_DUP:
  144. if (keymgmt->dup == NULL)
  145. keymgmt->dup = OSSL_FUNC_keymgmt_dup(fns);
  146. break;
  147. case OSSL_FUNC_KEYMGMT_VALIDATE:
  148. if (keymgmt->validate == NULL)
  149. keymgmt->validate = OSSL_FUNC_keymgmt_validate(fns);
  150. break;
  151. case OSSL_FUNC_KEYMGMT_MATCH:
  152. if (keymgmt->match == NULL)
  153. keymgmt->match = OSSL_FUNC_keymgmt_match(fns);
  154. break;
  155. case OSSL_FUNC_KEYMGMT_IMPORT:
  156. if (keymgmt->import == NULL) {
  157. importfncnt++;
  158. keymgmt->import = OSSL_FUNC_keymgmt_import(fns);
  159. }
  160. break;
  161. case OSSL_FUNC_KEYMGMT_IMPORT_TYPES:
  162. if (keymgmt->import_types == NULL) {
  163. if (importtypesfncnt == 0)
  164. importfncnt++;
  165. importtypesfncnt++;
  166. keymgmt->import_types = OSSL_FUNC_keymgmt_import_types(fns);
  167. }
  168. break;
  169. case OSSL_FUNC_KEYMGMT_IMPORT_TYPES_EX:
  170. if (keymgmt->import_types_ex == NULL) {
  171. if (importtypesfncnt == 0)
  172. importfncnt++;
  173. importtypesfncnt++;
  174. keymgmt->import_types_ex = OSSL_FUNC_keymgmt_import_types_ex(fns);
  175. }
  176. break;
  177. case OSSL_FUNC_KEYMGMT_EXPORT:
  178. if (keymgmt->export == NULL) {
  179. exportfncnt++;
  180. keymgmt->export = OSSL_FUNC_keymgmt_export(fns);
  181. }
  182. break;
  183. case OSSL_FUNC_KEYMGMT_EXPORT_TYPES:
  184. if (keymgmt->export_types == NULL) {
  185. if (exporttypesfncnt == 0)
  186. exportfncnt++;
  187. exporttypesfncnt++;
  188. keymgmt->export_types = OSSL_FUNC_keymgmt_export_types(fns);
  189. }
  190. break;
  191. case OSSL_FUNC_KEYMGMT_EXPORT_TYPES_EX:
  192. if (keymgmt->export_types_ex == NULL) {
  193. if (exporttypesfncnt == 0)
  194. exportfncnt++;
  195. exporttypesfncnt++;
  196. keymgmt->export_types_ex = OSSL_FUNC_keymgmt_export_types_ex(fns);
  197. }
  198. break;
  199. }
  200. }
  201. /*
  202. * Try to check that the method is sensible.
  203. * At least one constructor and the destructor are MANDATORY
  204. * The functions 'has' is MANDATORY
  205. * It makes no sense being able to free stuff if you can't create it.
  206. * It makes no sense providing OSSL_PARAM descriptors for import and
  207. * export if you can't import or export.
  208. */
  209. if (keymgmt->free == NULL
  210. || (keymgmt->new == NULL
  211. && keymgmt->gen == NULL
  212. && keymgmt->load == NULL)
  213. || keymgmt->has == NULL
  214. || (getparamfncnt != 0 && getparamfncnt != 2)
  215. || (setparamfncnt != 0 && setparamfncnt != 2)
  216. || (setgenparamfncnt != 0 && setgenparamfncnt != 2)
  217. || (importfncnt != 0 && importfncnt != 2)
  218. || (exportfncnt != 0 && exportfncnt != 2)
  219. || (keymgmt->gen != NULL
  220. && (keymgmt->gen_init == NULL
  221. || keymgmt->gen_cleanup == NULL))) {
  222. EVP_KEYMGMT_free(keymgmt);
  223. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  224. return NULL;
  225. }
  226. keymgmt->prov = prov;
  227. if (prov != NULL)
  228. ossl_provider_up_ref(prov);
  229. #ifndef FIPS_MODULE
  230. keymgmt->legacy_alg = get_legacy_alg_type_from_keymgmt(keymgmt);
  231. #endif
  232. return keymgmt;
  233. }
  234. EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
  235. const char *name,
  236. const char *properties)
  237. {
  238. return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYMGMT,
  239. name, properties,
  240. keymgmt_from_algorithm,
  241. (int (*)(void *))EVP_KEYMGMT_up_ref,
  242. (void (*)(void *))EVP_KEYMGMT_free);
  243. }
  244. EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  245. const char *properties)
  246. {
  247. return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
  248. keymgmt_from_algorithm,
  249. (int (*)(void *))EVP_KEYMGMT_up_ref,
  250. (void (*)(void *))EVP_KEYMGMT_free);
  251. }
  252. int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
  253. {
  254. int ref = 0;
  255. CRYPTO_UP_REF(&keymgmt->refcnt, &ref);
  256. return 1;
  257. }
  258. void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
  259. {
  260. int ref = 0;
  261. if (keymgmt == NULL)
  262. return;
  263. CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref);
  264. if (ref > 0)
  265. return;
  266. OPENSSL_free(keymgmt->type_name);
  267. ossl_provider_free(keymgmt->prov);
  268. CRYPTO_FREE_REF(&keymgmt->refcnt);
  269. OPENSSL_free(keymgmt);
  270. }
  271. const OSSL_PROVIDER *EVP_KEYMGMT_get0_provider(const EVP_KEYMGMT *keymgmt)
  272. {
  273. return keymgmt->prov;
  274. }
  275. int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt)
  276. {
  277. return keymgmt->name_id;
  278. }
  279. int evp_keymgmt_get_legacy_alg(const EVP_KEYMGMT *keymgmt)
  280. {
  281. return keymgmt->legacy_alg;
  282. }
  283. const char *EVP_KEYMGMT_get0_description(const EVP_KEYMGMT *keymgmt)
  284. {
  285. return keymgmt->description;
  286. }
  287. const char *EVP_KEYMGMT_get0_name(const EVP_KEYMGMT *keymgmt)
  288. {
  289. return keymgmt->type_name;
  290. }
  291. int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
  292. {
  293. return keymgmt != NULL
  294. && evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
  295. }
  296. void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX *libctx,
  297. void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
  298. void *arg)
  299. {
  300. evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
  301. (void (*)(void *, void *))fn, arg,
  302. keymgmt_from_algorithm,
  303. (int (*)(void *))EVP_KEYMGMT_up_ref,
  304. (void (*)(void *))EVP_KEYMGMT_free);
  305. }
  306. int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
  307. void (*fn)(const char *name, void *data),
  308. void *data)
  309. {
  310. if (keymgmt->prov != NULL)
  311. return evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
  312. return 1;
  313. }
  314. /*
  315. * Internal API that interfaces with the method function pointers
  316. */
  317. void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt)
  318. {
  319. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  320. /*
  321. * 'new' is currently mandatory on its own, but when new
  322. * constructors appear, it won't be quite as mandatory,
  323. * so we have a check for future cases.
  324. */
  325. if (keymgmt->new == NULL)
  326. return NULL;
  327. return keymgmt->new(provctx);
  328. }
  329. void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
  330. {
  331. /* This is mandatory, no need to check for its presence */
  332. keymgmt->free(keydata);
  333. }
  334. void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
  335. const OSSL_PARAM params[])
  336. {
  337. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  338. if (keymgmt->gen_init == NULL)
  339. return NULL;
  340. return keymgmt->gen_init(provctx, selection, params);
  341. }
  342. int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
  343. void *templ)
  344. {
  345. /*
  346. * It's arguable if we actually should return success in this case, as
  347. * it allows the caller to set a template key, which is then ignored.
  348. * However, this is how the legacy methods (EVP_PKEY_METHOD) operate,
  349. * so we do this in the interest of backward compatibility.
  350. */
  351. if (keymgmt->gen_set_template == NULL)
  352. return 1;
  353. return keymgmt->gen_set_template(genctx, templ);
  354. }
  355. int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
  356. const OSSL_PARAM params[])
  357. {
  358. if (keymgmt->gen_set_params == NULL)
  359. return 0;
  360. return keymgmt->gen_set_params(genctx, params);
  361. }
  362. const OSSL_PARAM *EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT *keymgmt)
  363. {
  364. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  365. if (keymgmt->gen_settable_params == NULL)
  366. return NULL;
  367. return keymgmt->gen_settable_params(NULL, provctx);
  368. }
  369. void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
  370. OSSL_CALLBACK *cb, void *cbarg)
  371. {
  372. if (keymgmt->gen == NULL)
  373. return NULL;
  374. return keymgmt->gen(genctx, cb, cbarg);
  375. }
  376. void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
  377. {
  378. if (keymgmt->gen_cleanup != NULL)
  379. keymgmt->gen_cleanup(genctx);
  380. }
  381. int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt)
  382. {
  383. return keymgmt != NULL && keymgmt->load != NULL;
  384. }
  385. void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
  386. const void *objref, size_t objref_sz)
  387. {
  388. if (evp_keymgmt_has_load(keymgmt))
  389. return keymgmt->load(objref, objref_sz);
  390. return NULL;
  391. }
  392. int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
  393. OSSL_PARAM params[])
  394. {
  395. if (keymgmt->get_params == NULL)
  396. return 1;
  397. return keymgmt->get_params(keydata, params);
  398. }
  399. const OSSL_PARAM *EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT *keymgmt)
  400. {
  401. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  402. if (keymgmt->gettable_params == NULL)
  403. return NULL;
  404. return keymgmt->gettable_params(provctx);
  405. }
  406. int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata,
  407. const OSSL_PARAM params[])
  408. {
  409. if (keymgmt->set_params == NULL)
  410. return 1;
  411. return keymgmt->set_params(keydata, params);
  412. }
  413. const OSSL_PARAM *EVP_KEYMGMT_settable_params(const EVP_KEYMGMT *keymgmt)
  414. {
  415. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  416. if (keymgmt->settable_params == NULL)
  417. return NULL;
  418. return keymgmt->settable_params(provctx);
  419. }
  420. int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keydata, int selection)
  421. {
  422. /* This is mandatory, no need to check for its presence */
  423. return keymgmt->has(keydata, selection);
  424. }
  425. int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
  426. int selection, int checktype)
  427. {
  428. /* We assume valid if the implementation doesn't have a function */
  429. if (keymgmt->validate == NULL)
  430. return 1;
  431. return keymgmt->validate(keydata, selection, checktype);
  432. }
  433. int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
  434. const void *keydata1, const void *keydata2,
  435. int selection)
  436. {
  437. /* We assume no match if the implementation doesn't have a function */
  438. if (keymgmt->match == NULL)
  439. return 0;
  440. return keymgmt->match(keydata1, keydata2, selection);
  441. }
  442. int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
  443. int selection, const OSSL_PARAM params[])
  444. {
  445. if (keymgmt->import == NULL)
  446. return 0;
  447. return keymgmt->import(keydata, selection, params);
  448. }
  449. const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
  450. int selection)
  451. {
  452. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  453. if (keymgmt->import_types_ex != NULL)
  454. return keymgmt->import_types_ex(provctx, selection);
  455. if (keymgmt->import_types == NULL)
  456. return NULL;
  457. return keymgmt->import_types(selection);
  458. }
  459. int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
  460. int selection, OSSL_CALLBACK *param_cb, void *cbarg)
  461. {
  462. if (keymgmt->export == NULL)
  463. return 0;
  464. return keymgmt->export(keydata, selection, param_cb, cbarg);
  465. }
  466. const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
  467. int selection)
  468. {
  469. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  470. if (keymgmt->export_types_ex != NULL)
  471. return keymgmt->export_types_ex(provctx, selection);
  472. if (keymgmt->export_types == NULL)
  473. return NULL;
  474. return keymgmt->export_types(selection);
  475. }
  476. void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, const void *keydata_from,
  477. int selection)
  478. {
  479. /* We assume no dup if the implementation doesn't have a function */
  480. if (keymgmt->dup == NULL)
  481. return NULL;
  482. return keymgmt->dup(keydata_from, selection);
  483. }