keymgmt_meth.c 15 KB

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