keymgmt_meth.c 17 KB

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