keymgmt_meth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/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. || (keymgmt->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  23. EVP_KEYMGMT_free(keymgmt);
  24. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  25. return NULL;
  26. }
  27. keymgmt->refcnt = 1;
  28. return keymgmt;
  29. }
  30. static void *keymgmt_from_algorithm(int name_id,
  31. const OSSL_ALGORITHM *algodef,
  32. OSSL_PROVIDER *prov)
  33. {
  34. const OSSL_DISPATCH *fns = algodef->implementation;
  35. EVP_KEYMGMT *keymgmt = NULL;
  36. int setparamfncnt = 0, getparamfncnt = 0;
  37. int setgenparamfncnt = 0;
  38. int importfncnt = 0, exportfncnt = 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. importfncnt++;
  148. keymgmt->import_types = OSSL_FUNC_keymgmt_import_types(fns);
  149. }
  150. break;
  151. case OSSL_FUNC_KEYMGMT_EXPORT:
  152. if (keymgmt->export == NULL) {
  153. exportfncnt++;
  154. keymgmt->export = OSSL_FUNC_keymgmt_export(fns);
  155. }
  156. break;
  157. case OSSL_FUNC_KEYMGMT_EXPORT_TYPES:
  158. if (keymgmt->export_types == NULL) {
  159. exportfncnt++;
  160. keymgmt->export_types = OSSL_FUNC_keymgmt_export_types(fns);
  161. }
  162. break;
  163. }
  164. }
  165. /*
  166. * Try to check that the method is sensible.
  167. * At least one constructor and the destructor are MANDATORY
  168. * The functions 'has' is MANDATORY
  169. * It makes no sense being able to free stuff if you can't create it.
  170. * It makes no sense providing OSSL_PARAM descriptors for import and
  171. * export if you can't import or export.
  172. */
  173. if (keymgmt->free == NULL
  174. || (keymgmt->new == NULL
  175. && keymgmt->gen == NULL
  176. && keymgmt->load == NULL)
  177. || keymgmt->has == NULL
  178. || (getparamfncnt != 0 && getparamfncnt != 2)
  179. || (setparamfncnt != 0 && setparamfncnt != 2)
  180. || (setgenparamfncnt != 0 && setgenparamfncnt != 2)
  181. || (importfncnt != 0 && importfncnt != 2)
  182. || (exportfncnt != 0 && exportfncnt != 2)
  183. || (keymgmt->gen != NULL
  184. && (keymgmt->gen_init == NULL
  185. || keymgmt->gen_cleanup == NULL))) {
  186. EVP_KEYMGMT_free(keymgmt);
  187. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
  188. return NULL;
  189. }
  190. keymgmt->prov = prov;
  191. if (prov != NULL)
  192. ossl_provider_up_ref(prov);
  193. return keymgmt;
  194. }
  195. EVP_KEYMGMT *evp_keymgmt_fetch_from_prov(OSSL_PROVIDER *prov,
  196. const char *name,
  197. const char *properties)
  198. {
  199. return evp_generic_fetch_from_prov(prov, OSSL_OP_KEYMGMT,
  200. name, properties,
  201. keymgmt_from_algorithm,
  202. (int (*)(void *))EVP_KEYMGMT_up_ref,
  203. (void (*)(void *))EVP_KEYMGMT_free);
  204. }
  205. EVP_KEYMGMT *EVP_KEYMGMT_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
  206. const char *properties)
  207. {
  208. return evp_generic_fetch(ctx, OSSL_OP_KEYMGMT, algorithm, properties,
  209. keymgmt_from_algorithm,
  210. (int (*)(void *))EVP_KEYMGMT_up_ref,
  211. (void (*)(void *))EVP_KEYMGMT_free);
  212. }
  213. int EVP_KEYMGMT_up_ref(EVP_KEYMGMT *keymgmt)
  214. {
  215. int ref = 0;
  216. CRYPTO_UP_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
  217. return 1;
  218. }
  219. void EVP_KEYMGMT_free(EVP_KEYMGMT *keymgmt)
  220. {
  221. int ref = 0;
  222. if (keymgmt == NULL)
  223. return;
  224. CRYPTO_DOWN_REF(&keymgmt->refcnt, &ref, keymgmt->lock);
  225. if (ref > 0)
  226. return;
  227. OPENSSL_free(keymgmt->type_name);
  228. ossl_provider_free(keymgmt->prov);
  229. CRYPTO_THREAD_lock_free(keymgmt->lock);
  230. OPENSSL_free(keymgmt);
  231. }
  232. const OSSL_PROVIDER *EVP_KEYMGMT_get0_provider(const EVP_KEYMGMT *keymgmt)
  233. {
  234. return keymgmt->prov;
  235. }
  236. int evp_keymgmt_get_number(const EVP_KEYMGMT *keymgmt)
  237. {
  238. return keymgmt->name_id;
  239. }
  240. const char *EVP_KEYMGMT_get0_description(const EVP_KEYMGMT *keymgmt)
  241. {
  242. return keymgmt->description;
  243. }
  244. const char *EVP_KEYMGMT_get0_name(const EVP_KEYMGMT *keymgmt)
  245. {
  246. return keymgmt->type_name;
  247. }
  248. int EVP_KEYMGMT_is_a(const EVP_KEYMGMT *keymgmt, const char *name)
  249. {
  250. return keymgmt != NULL
  251. && evp_is_a(keymgmt->prov, keymgmt->name_id, NULL, name);
  252. }
  253. void EVP_KEYMGMT_do_all_provided(OSSL_LIB_CTX *libctx,
  254. void (*fn)(EVP_KEYMGMT *keymgmt, void *arg),
  255. void *arg)
  256. {
  257. evp_generic_do_all(libctx, OSSL_OP_KEYMGMT,
  258. (void (*)(void *, void *))fn, arg,
  259. keymgmt_from_algorithm,
  260. (int (*)(void *))EVP_KEYMGMT_up_ref,
  261. (void (*)(void *))EVP_KEYMGMT_free);
  262. }
  263. int EVP_KEYMGMT_names_do_all(const EVP_KEYMGMT *keymgmt,
  264. void (*fn)(const char *name, void *data),
  265. void *data)
  266. {
  267. if (keymgmt->prov != NULL)
  268. return evp_names_do_all(keymgmt->prov, keymgmt->name_id, fn, data);
  269. return 1;
  270. }
  271. /*
  272. * Internal API that interfaces with the method function pointers
  273. */
  274. void *evp_keymgmt_newdata(const EVP_KEYMGMT *keymgmt)
  275. {
  276. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  277. /*
  278. * 'new' is currently mandatory on its own, but when new
  279. * constructors appear, it won't be quite as mandatory,
  280. * so we have a check for future cases.
  281. */
  282. if (keymgmt->new == NULL)
  283. return NULL;
  284. return keymgmt->new(provctx);
  285. }
  286. void evp_keymgmt_freedata(const EVP_KEYMGMT *keymgmt, void *keydata)
  287. {
  288. /* This is mandatory, no need to check for its presence */
  289. keymgmt->free(keydata);
  290. }
  291. void *evp_keymgmt_gen_init(const EVP_KEYMGMT *keymgmt, int selection,
  292. const OSSL_PARAM params[])
  293. {
  294. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  295. if (keymgmt->gen_init == NULL)
  296. return NULL;
  297. return keymgmt->gen_init(provctx, selection, params);
  298. }
  299. int evp_keymgmt_gen_set_template(const EVP_KEYMGMT *keymgmt, void *genctx,
  300. void *template)
  301. {
  302. /*
  303. * It's arguable if we actually should return success in this case, as
  304. * it allows the caller to set a template key, which is then ignored.
  305. * However, this is how the legacy methods (EVP_PKEY_METHOD) operate,
  306. * so we do this in the interest of backward compatibility.
  307. */
  308. if (keymgmt->gen_set_template == NULL)
  309. return 1;
  310. return keymgmt->gen_set_template(genctx, template);
  311. }
  312. int evp_keymgmt_gen_set_params(const EVP_KEYMGMT *keymgmt, void *genctx,
  313. const OSSL_PARAM params[])
  314. {
  315. if (keymgmt->gen_set_params == NULL)
  316. return 0;
  317. return keymgmt->gen_set_params(genctx, params);
  318. }
  319. const OSSL_PARAM *EVP_KEYMGMT_gen_settable_params(const EVP_KEYMGMT *keymgmt)
  320. {
  321. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  322. if (keymgmt->gen_settable_params == NULL)
  323. return NULL;
  324. return keymgmt->gen_settable_params(NULL, provctx);
  325. }
  326. void *evp_keymgmt_gen(const EVP_KEYMGMT *keymgmt, void *genctx,
  327. OSSL_CALLBACK *cb, void *cbarg)
  328. {
  329. if (keymgmt->gen == NULL)
  330. return NULL;
  331. return keymgmt->gen(genctx, cb, cbarg);
  332. }
  333. void evp_keymgmt_gen_cleanup(const EVP_KEYMGMT *keymgmt, void *genctx)
  334. {
  335. if (keymgmt->gen != NULL)
  336. keymgmt->gen_cleanup(genctx);
  337. }
  338. int evp_keymgmt_has_load(const EVP_KEYMGMT *keymgmt)
  339. {
  340. return keymgmt != NULL && keymgmt->load != NULL;
  341. }
  342. void *evp_keymgmt_load(const EVP_KEYMGMT *keymgmt,
  343. const void *objref, size_t objref_sz)
  344. {
  345. if (evp_keymgmt_has_load(keymgmt))
  346. return keymgmt->load(objref, objref_sz);
  347. return NULL;
  348. }
  349. int evp_keymgmt_get_params(const EVP_KEYMGMT *keymgmt, void *keydata,
  350. OSSL_PARAM params[])
  351. {
  352. if (keymgmt->get_params == NULL)
  353. return 1;
  354. return keymgmt->get_params(keydata, params);
  355. }
  356. const OSSL_PARAM *EVP_KEYMGMT_gettable_params(const EVP_KEYMGMT *keymgmt)
  357. {
  358. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  359. if (keymgmt->gettable_params == NULL)
  360. return NULL;
  361. return keymgmt->gettable_params(provctx);
  362. }
  363. int evp_keymgmt_set_params(const EVP_KEYMGMT *keymgmt, void *keydata,
  364. const OSSL_PARAM params[])
  365. {
  366. if (keymgmt->set_params == NULL)
  367. return 1;
  368. return keymgmt->set_params(keydata, params);
  369. }
  370. const OSSL_PARAM *EVP_KEYMGMT_settable_params(const EVP_KEYMGMT *keymgmt)
  371. {
  372. void *provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(keymgmt));
  373. if (keymgmt->settable_params == NULL)
  374. return NULL;
  375. return keymgmt->settable_params(provctx);
  376. }
  377. int evp_keymgmt_has(const EVP_KEYMGMT *keymgmt, void *keydata, int selection)
  378. {
  379. /* This is mandatory, no need to check for its presence */
  380. return keymgmt->has(keydata, selection);
  381. }
  382. int evp_keymgmt_validate(const EVP_KEYMGMT *keymgmt, void *keydata,
  383. int selection, int checktype)
  384. {
  385. /* We assume valid if the implementation doesn't have a function */
  386. if (keymgmt->validate == NULL)
  387. return 1;
  388. return keymgmt->validate(keydata, selection, checktype);
  389. }
  390. int evp_keymgmt_match(const EVP_KEYMGMT *keymgmt,
  391. const void *keydata1, const void *keydata2,
  392. int selection)
  393. {
  394. /* We assume no match if the implementation doesn't have a function */
  395. if (keymgmt->match == NULL)
  396. return 0;
  397. return keymgmt->match(keydata1, keydata2, selection);
  398. }
  399. int evp_keymgmt_import(const EVP_KEYMGMT *keymgmt, void *keydata,
  400. int selection, const OSSL_PARAM params[])
  401. {
  402. if (keymgmt->import == NULL)
  403. return 0;
  404. return keymgmt->import(keydata, selection, params);
  405. }
  406. const OSSL_PARAM *evp_keymgmt_import_types(const EVP_KEYMGMT *keymgmt,
  407. int selection)
  408. {
  409. if (keymgmt->import_types == NULL)
  410. return NULL;
  411. return keymgmt->import_types(selection);
  412. }
  413. int evp_keymgmt_export(const EVP_KEYMGMT *keymgmt, void *keydata,
  414. int selection, OSSL_CALLBACK *param_cb, void *cbarg)
  415. {
  416. if (keymgmt->export == NULL)
  417. return 0;
  418. return keymgmt->export(keydata, selection, param_cb, cbarg);
  419. }
  420. const OSSL_PARAM *evp_keymgmt_export_types(const EVP_KEYMGMT *keymgmt,
  421. int selection)
  422. {
  423. if (keymgmt->export_types == NULL)
  424. return NULL;
  425. return keymgmt->export_types(selection);
  426. }
  427. void *evp_keymgmt_dup(const EVP_KEYMGMT *keymgmt, const void *keydata_from,
  428. int selection)
  429. {
  430. /* We assume no dup if the implementation doesn't have a function */
  431. if (keymgmt->dup == NULL)
  432. return NULL;
  433. return keymgmt->dup(keydata_from, selection);
  434. }