pmeth_gn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * Copyright 2006-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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <openssl/core.h>
  12. #include <openssl/core_names.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/core.h"
  15. #include <openssl/objects.h>
  16. #include <openssl/evp.h>
  17. #include "crypto/bn.h"
  18. #include "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #include "evp_local.h"
  21. static int gen_init(EVP_PKEY_CTX *ctx, int operation)
  22. {
  23. int ret = 0;
  24. if (ctx == NULL)
  25. goto not_supported;
  26. evp_pkey_ctx_free_old_ops(ctx);
  27. ctx->operation = operation;
  28. if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
  29. goto legacy;
  30. switch (operation) {
  31. case EVP_PKEY_OP_PARAMGEN:
  32. ctx->op.keymgmt.genctx =
  33. evp_keymgmt_gen_init(ctx->keymgmt,
  34. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, NULL);
  35. break;
  36. case EVP_PKEY_OP_KEYGEN:
  37. ctx->op.keymgmt.genctx =
  38. evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR,
  39. NULL);
  40. break;
  41. }
  42. if (ctx->op.keymgmt.genctx == NULL)
  43. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  44. else
  45. ret = 1;
  46. goto end;
  47. legacy:
  48. #ifdef FIPS_MODULE
  49. goto not_supported;
  50. #else
  51. if (ctx->pmeth == NULL
  52. || (operation == EVP_PKEY_OP_PARAMGEN
  53. && ctx->pmeth->paramgen == NULL)
  54. || (operation == EVP_PKEY_OP_KEYGEN
  55. && ctx->pmeth->keygen == NULL))
  56. goto not_supported;
  57. ret = 1;
  58. switch (operation) {
  59. case EVP_PKEY_OP_PARAMGEN:
  60. if (ctx->pmeth->paramgen_init != NULL)
  61. ret = ctx->pmeth->paramgen_init(ctx);
  62. break;
  63. case EVP_PKEY_OP_KEYGEN:
  64. if (ctx->pmeth->keygen_init != NULL)
  65. ret = ctx->pmeth->keygen_init(ctx);
  66. break;
  67. }
  68. #endif
  69. end:
  70. if (ret <= 0 && ctx != NULL) {
  71. evp_pkey_ctx_free_old_ops(ctx);
  72. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  73. }
  74. return ret;
  75. not_supported:
  76. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  77. ret = -2;
  78. goto end;
  79. }
  80. int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
  81. {
  82. return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
  83. }
  84. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
  85. {
  86. return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
  87. }
  88. static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
  89. {
  90. EVP_PKEY_CTX *ctx = arg;
  91. const OSSL_PARAM *param = NULL;
  92. int p = -1, n = -1;
  93. if (ctx->pkey_gencb == NULL)
  94. return 1; /* No callback? That's fine */
  95. if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
  96. == NULL
  97. || !OSSL_PARAM_get_int(param, &p))
  98. return 0;
  99. if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
  100. == NULL
  101. || !OSSL_PARAM_get_int(param, &n))
  102. return 0;
  103. ctx->keygen_info[0] = p;
  104. ctx->keygen_info[1] = n;
  105. return ctx->pkey_gencb(ctx);
  106. }
  107. int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  108. {
  109. int ret = 0;
  110. OSSL_CALLBACK cb;
  111. EVP_PKEY *allocated_pkey = NULL;
  112. /* Legacy compatible keygen callback info, only used with provider impls */
  113. int gentmp[2];
  114. if (ppkey == NULL)
  115. return -1;
  116. if (ctx == NULL)
  117. goto not_supported;
  118. if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
  119. goto not_initialized;
  120. if (*ppkey == NULL)
  121. *ppkey = allocated_pkey = EVP_PKEY_new();
  122. if (*ppkey == NULL) {
  123. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  124. return -1;
  125. }
  126. if (ctx->op.keymgmt.genctx == NULL)
  127. goto legacy;
  128. /*
  129. * Asssigning gentmp to ctx->keygen_info is something our legacy
  130. * implementations do. Because the provider implementations aren't
  131. * allowed to reach into our EVP_PKEY_CTX, we need to provide similar
  132. * space for backward compatibility. It's ok that we attach a local
  133. * variable, as it should only be useful in the calls down from here.
  134. * This is cleared as soon as it isn't useful any more, i.e. directly
  135. * after the evp_keymgmt_util_gen() call.
  136. */
  137. ctx->keygen_info = gentmp;
  138. ctx->keygen_info_count = 2;
  139. ret = 1;
  140. if (ctx->pkey != NULL) {
  141. EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
  142. void *keydata =
  143. evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
  144. &tmp_keymgmt, ctx->propquery);
  145. if (tmp_keymgmt == NULL)
  146. goto not_supported;
  147. /*
  148. * It's ok if keydata is NULL here. The backend is expected to deal
  149. * with that as it sees fit.
  150. */
  151. ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
  152. ctx->op.keymgmt.genctx, keydata);
  153. }
  154. /*
  155. * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
  156. * so we do not need to save it, just check it.
  157. */
  158. ret = ret
  159. && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
  160. ossl_callback_to_pkey_gencb, ctx)
  161. != NULL);
  162. ctx->keygen_info = NULL;
  163. #ifndef FIPS_MODULE
  164. /* In case |*ppkey| was originally a legacy key */
  165. if (ret)
  166. evp_pkey_free_legacy(*ppkey);
  167. #endif
  168. /*
  169. * Because we still have legacy keys
  170. */
  171. (*ppkey)->type = ctx->legacy_keytype;
  172. goto end;
  173. legacy:
  174. #ifdef FIPS_MODULE
  175. goto not_supported;
  176. #else
  177. /*
  178. * If we get here then we're using legacy paramgen/keygen. In that case
  179. * the pkey in ctx (if there is one) had better not be provided (because the
  180. * legacy methods may not know how to handle it). However we can only get
  181. * here if ctx->op.keymgmt.genctx == NULL, but that should never be the case
  182. * if ctx->pkey is provided because we don't allow this when we initialise
  183. * the ctx.
  184. */
  185. if (ctx->pkey != NULL && !ossl_assert(!evp_pkey_is_provided(ctx->pkey)))
  186. goto not_accessible;
  187. switch (ctx->operation) {
  188. case EVP_PKEY_OP_PARAMGEN:
  189. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  190. break;
  191. case EVP_PKEY_OP_KEYGEN:
  192. ret = ctx->pmeth->keygen(ctx, *ppkey);
  193. break;
  194. default:
  195. goto not_supported;
  196. }
  197. #endif
  198. end:
  199. if (ret <= 0) {
  200. if (allocated_pkey != NULL)
  201. *ppkey = NULL;
  202. EVP_PKEY_free(allocated_pkey);
  203. }
  204. return ret;
  205. not_supported:
  206. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  207. ret = -2;
  208. goto end;
  209. not_initialized:
  210. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  211. ret = -1;
  212. goto end;
  213. #ifndef FIPS_MODULE
  214. not_accessible:
  215. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
  216. ret = -1;
  217. goto end;
  218. #endif
  219. }
  220. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  221. {
  222. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  223. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  224. return -1;
  225. }
  226. return EVP_PKEY_generate(ctx, ppkey);
  227. }
  228. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  229. {
  230. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  231. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  232. return -1;
  233. }
  234. return EVP_PKEY_generate(ctx, ppkey);
  235. }
  236. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  237. {
  238. ctx->pkey_gencb = cb;
  239. }
  240. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  241. {
  242. return ctx->pkey_gencb;
  243. }
  244. /*
  245. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  246. * callbacks.
  247. */
  248. static int trans_cb(int a, int b, BN_GENCB *gcb)
  249. {
  250. EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
  251. ctx->keygen_info[0] = a;
  252. ctx->keygen_info[1] = b;
  253. return ctx->pkey_gencb(ctx);
  254. }
  255. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  256. {
  257. BN_GENCB_set(cb, trans_cb, ctx);
  258. }
  259. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  260. {
  261. if (idx == -1)
  262. return ctx->keygen_info_count;
  263. if (idx < 0 || idx > ctx->keygen_info_count)
  264. return 0;
  265. return ctx->keygen_info[idx];
  266. }
  267. #ifndef FIPS_MODULE
  268. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  269. const unsigned char *key, int keylen)
  270. {
  271. EVP_PKEY_CTX *mac_ctx = NULL;
  272. EVP_PKEY *mac_key = NULL;
  273. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  274. if (!mac_ctx)
  275. return NULL;
  276. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  277. goto merr;
  278. if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
  279. goto merr;
  280. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  281. goto merr;
  282. merr:
  283. EVP_PKEY_CTX_free(mac_ctx);
  284. return mac_key;
  285. }
  286. #endif /* FIPS_MODULE */
  287. /*- All methods below can also be used in FIPS_MODULE */
  288. static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
  289. {
  290. if (ctx == NULL || ctx->keytype == NULL)
  291. goto not_supported;
  292. evp_pkey_ctx_free_old_ops(ctx);
  293. if (ctx->keymgmt == NULL)
  294. goto not_supported;
  295. ctx->operation = operation;
  296. return 1;
  297. not_supported:
  298. if (ctx != NULL)
  299. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  300. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  301. return -2;
  302. }
  303. int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx)
  304. {
  305. return fromdata_init(ctx, EVP_PKEY_OP_FROMDATA);
  306. }
  307. int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
  308. OSSL_PARAM params[])
  309. {
  310. void *keydata = NULL;
  311. if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) {
  312. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  313. return -2;
  314. }
  315. if (ppkey == NULL)
  316. return -1;
  317. if (*ppkey == NULL)
  318. *ppkey = EVP_PKEY_new();
  319. if (*ppkey == NULL) {
  320. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  321. return -1;
  322. }
  323. keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params);
  324. if (keydata == NULL)
  325. return 0;
  326. /* keydata is cached in *ppkey, so we need not bother with it further */
  327. return 1;
  328. }
  329. const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection)
  330. {
  331. /* We call fromdata_init to get ctx->keymgmt populated */
  332. if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED) == 1)
  333. return evp_keymgmt_import_types(ctx->keymgmt, selection);
  334. return NULL;
  335. }
  336. static OSSL_CALLBACK ossl_pkey_todata_cb;
  337. static int ossl_pkey_todata_cb(const OSSL_PARAM params[], void *arg)
  338. {
  339. OSSL_PARAM **ret = arg;
  340. *ret = OSSL_PARAM_dup(params);
  341. return 1;
  342. }
  343. int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params)
  344. {
  345. if (params == NULL)
  346. return 0;
  347. return EVP_PKEY_export(pkey, selection, ossl_pkey_todata_cb, params);
  348. }
  349. #ifndef FIPS_MODULE
  350. struct fake_import_data_st {
  351. OSSL_CALLBACK *export_cb;
  352. void *export_cbarg;
  353. };
  354. static OSSL_FUNC_keymgmt_import_fn pkey_fake_import;
  355. static int pkey_fake_import(void *fake_keydata, int ignored_selection,
  356. const OSSL_PARAM params[])
  357. {
  358. struct fake_import_data_st *data = fake_keydata;
  359. return data->export_cb(params, data->export_cbarg);
  360. }
  361. #endif
  362. int EVP_PKEY_export(const EVP_PKEY *pkey, int selection,
  363. OSSL_CALLBACK *export_cb, void *export_cbarg)
  364. {
  365. if (pkey == NULL) {
  366. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  367. return 0;
  368. }
  369. #ifndef FIPS_MODULE
  370. if (evp_pkey_is_legacy(pkey)) {
  371. struct fake_import_data_st data;
  372. data.export_cb = export_cb;
  373. data.export_cbarg = export_cbarg;
  374. /*
  375. * We don't need to care about libctx or propq here, as we're only
  376. * interested in the resulting OSSL_PARAM array.
  377. */
  378. return pkey->ameth->export_to(pkey, &data, pkey_fake_import,
  379. NULL, NULL);
  380. }
  381. #endif
  382. return evp_keymgmt_util_export(pkey, selection, export_cb, export_cbarg);
  383. }