2
0

keymgmt_meth.c 17 KB

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