keymgmt_meth.c 15 KB

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