keymgmt_meth.c 16 KB

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