2
0

pmeth_gn.c 12 KB

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