pmeth_gn.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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_gen(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. * TODO remove this #legacy internal keys are gone
  171. */
  172. (*ppkey)->type = ctx->legacy_keytype;
  173. goto end;
  174. legacy:
  175. #ifdef FIPS_MODULE
  176. goto not_supported;
  177. #else
  178. /*
  179. * If we get here then we're using legacy paramgen/keygen. In that case
  180. * the pkey in ctx (if there is one) had better not be provided (because the
  181. * legacy methods may not know how to handle it). However we can only get
  182. * here if ctx->op.keymgmt.genctx == NULL, but that should never be the case
  183. * if ctx->pkey is provided because we don't allow this when we initialise
  184. * the ctx.
  185. */
  186. if (ctx->pkey != NULL && !ossl_assert(!evp_pkey_is_provided(ctx->pkey)))
  187. goto not_accessible;
  188. switch (ctx->operation) {
  189. case EVP_PKEY_OP_PARAMGEN:
  190. ret = ctx->pmeth->paramgen(ctx, *ppkey);
  191. break;
  192. case EVP_PKEY_OP_KEYGEN:
  193. ret = ctx->pmeth->keygen(ctx, *ppkey);
  194. break;
  195. default:
  196. goto not_supported;
  197. }
  198. #endif
  199. end:
  200. if (ret <= 0) {
  201. if (allocated_pkey != NULL)
  202. *ppkey = NULL;
  203. EVP_PKEY_free(allocated_pkey);
  204. }
  205. return ret;
  206. not_supported:
  207. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  208. ret = -2;
  209. goto end;
  210. not_initialized:
  211. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  212. ret = -1;
  213. goto end;
  214. #ifndef FIPS_MODULE
  215. not_accessible:
  216. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
  217. ret = -1;
  218. goto end;
  219. #endif
  220. }
  221. int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  222. {
  223. if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
  224. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  225. return -1;
  226. }
  227. return EVP_PKEY_gen(ctx, ppkey);
  228. }
  229. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
  230. {
  231. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  232. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
  233. return -1;
  234. }
  235. return EVP_PKEY_gen(ctx, ppkey);
  236. }
  237. void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
  238. {
  239. ctx->pkey_gencb = cb;
  240. }
  241. EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
  242. {
  243. return ctx->pkey_gencb;
  244. }
  245. /*
  246. * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
  247. * callbacks.
  248. */
  249. static int trans_cb(int a, int b, BN_GENCB *gcb)
  250. {
  251. EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
  252. ctx->keygen_info[0] = a;
  253. ctx->keygen_info[1] = b;
  254. return ctx->pkey_gencb(ctx);
  255. }
  256. void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
  257. {
  258. BN_GENCB_set(cb, trans_cb, ctx);
  259. }
  260. int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
  261. {
  262. if (idx == -1)
  263. return ctx->keygen_info_count;
  264. if (idx < 0 || idx > ctx->keygen_info_count)
  265. return 0;
  266. return ctx->keygen_info[idx];
  267. }
  268. #ifndef FIPS_MODULE
  269. EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
  270. const unsigned char *key, int keylen)
  271. {
  272. EVP_PKEY_CTX *mac_ctx = NULL;
  273. EVP_PKEY *mac_key = NULL;
  274. mac_ctx = EVP_PKEY_CTX_new_id(type, e);
  275. if (!mac_ctx)
  276. return NULL;
  277. if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
  278. goto merr;
  279. if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
  280. goto merr;
  281. if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
  282. goto merr;
  283. merr:
  284. EVP_PKEY_CTX_free(mac_ctx);
  285. return mac_key;
  286. }
  287. #endif /* FIPS_MODULE */
  288. /*- All methods below can also be used in FIPS_MODULE */
  289. static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
  290. {
  291. if (ctx == NULL || ctx->keytype == NULL)
  292. goto not_supported;
  293. evp_pkey_ctx_free_old_ops(ctx);
  294. if (ctx->keymgmt == NULL)
  295. goto not_supported;
  296. ctx->operation = operation;
  297. return 1;
  298. not_supported:
  299. if (ctx != NULL)
  300. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  301. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  302. return -2;
  303. }
  304. int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx)
  305. {
  306. return fromdata_init(ctx, EVP_PKEY_OP_FROMDATA);
  307. }
  308. int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection,
  309. OSSL_PARAM params[])
  310. {
  311. void *keydata = NULL;
  312. if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) {
  313. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  314. return -2;
  315. }
  316. if (ppkey == NULL)
  317. return -1;
  318. if (*ppkey == NULL)
  319. *ppkey = EVP_PKEY_new();
  320. if (*ppkey == NULL) {
  321. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  322. return -1;
  323. }
  324. keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params);
  325. if (keydata == NULL)
  326. return 0;
  327. /* keydata is cached in *ppkey, so we need not bother with it further */
  328. return 1;
  329. }
  330. const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection)
  331. {
  332. /* We call fromdata_init to get ctx->keymgmt populated */
  333. if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED) == 1)
  334. return evp_keymgmt_import_types(ctx->keymgmt, selection);
  335. return NULL;
  336. }
  337. static OSSL_CALLBACK ossl_pkey_todata_cb;
  338. static int ossl_pkey_todata_cb(const OSSL_PARAM params[], void *arg)
  339. {
  340. OSSL_PARAM **ret = arg;
  341. *ret = OSSL_PARAM_dup(params);
  342. return 1;
  343. }
  344. int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params)
  345. {
  346. if (params == NULL)
  347. return 0;
  348. return EVP_PKEY_export(pkey, selection, ossl_pkey_todata_cb, params);
  349. }
  350. int EVP_PKEY_export(const EVP_PKEY *pkey, int selection,
  351. OSSL_CALLBACK *export_cb, void *export_cbarg)
  352. {
  353. return evp_keymgmt_util_export(pkey, selection, export_cb, export_cbarg);
  354. }