Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

549 Zeilen
16 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/evp.h>
  57. #include <stdio.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/obj.h>
  61. #include "internal.h"
  62. extern const EVP_PKEY_METHOD rsa_pkey_meth;
  63. extern const EVP_PKEY_METHOD hmac_pkey_meth;
  64. extern const EVP_PKEY_METHOD ec_pkey_meth;
  65. static const EVP_PKEY_METHOD *evp_methods[] = {
  66. &rsa_pkey_meth,
  67. &hmac_pkey_meth,
  68. &ec_pkey_meth,
  69. };
  70. static const EVP_PKEY_METHOD *evp_pkey_meth_find(int type) {
  71. unsigned i;
  72. for (i = 0; i < sizeof(evp_methods)/sizeof(EVP_PKEY_METHOD*); i++) {
  73. if (evp_methods[i]->pkey_id == type) {
  74. return evp_methods[i];
  75. }
  76. }
  77. return NULL;
  78. }
  79. static EVP_PKEY_CTX *evp_pkey_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id) {
  80. EVP_PKEY_CTX *ret;
  81. const EVP_PKEY_METHOD *pmeth;
  82. if (id == -1) {
  83. if (!pkey || !pkey->ameth) {
  84. return NULL;
  85. }
  86. id = pkey->ameth->pkey_id;
  87. }
  88. pmeth = evp_pkey_meth_find(id);
  89. if (pmeth == NULL) {
  90. OPENSSL_PUT_ERROR(EVP, evp_pkey_ctx_new, EVP_R_UNSUPPORTED_ALGORITHM);
  91. const char *name = OBJ_nid2sn(id);
  92. ERR_add_error_dataf("algorithm %d (%s)", id, name);
  93. return NULL;
  94. }
  95. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  96. if (!ret) {
  97. OPENSSL_PUT_ERROR(EVP, evp_pkey_ctx_new, ERR_R_MALLOC_FAILURE);
  98. return NULL;
  99. }
  100. memset(ret, 0, sizeof(EVP_PKEY_CTX));
  101. ret->engine = e;
  102. ret->pmeth = pmeth;
  103. ret->operation = EVP_PKEY_OP_UNDEFINED;
  104. if (pkey) {
  105. ret->pkey = pkey;
  106. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  107. }
  108. if (pmeth->init) {
  109. if (pmeth->init(ret) <= 0) {
  110. EVP_PKEY_CTX_free(ret);
  111. return NULL;
  112. }
  113. }
  114. return ret;
  115. }
  116. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) {
  117. return evp_pkey_ctx_new(pkey, e, -1);
  118. }
  119. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) {
  120. return evp_pkey_ctx_new(NULL, e, id);
  121. }
  122. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) {
  123. if (ctx == NULL) {
  124. return;
  125. }
  126. if (ctx->pmeth && ctx->pmeth->cleanup) {
  127. ctx->pmeth->cleanup(ctx);
  128. }
  129. if (ctx->pkey) {
  130. EVP_PKEY_free(ctx->pkey);
  131. }
  132. if (ctx->peerkey) {
  133. EVP_PKEY_free(ctx->peerkey);
  134. }
  135. OPENSSL_free(ctx);
  136. }
  137. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx) {
  138. EVP_PKEY_CTX *rctx;
  139. if (!pctx->pmeth || !pctx->pmeth->copy) {
  140. return NULL;
  141. }
  142. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  143. if (!rctx) {
  144. return NULL;
  145. }
  146. memset(rctx, 0, sizeof(EVP_PKEY_CTX));
  147. rctx->pmeth = pctx->pmeth;
  148. rctx->engine = pctx->engine;
  149. rctx->operation = pctx->operation;
  150. if (pctx->pkey) {
  151. CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  152. }
  153. rctx->pkey = pctx->pkey;
  154. if (pctx->peerkey) {
  155. CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  156. }
  157. rctx->peerkey = pctx->peerkey;
  158. if (pctx->pmeth->copy(rctx, pctx) > 0) {
  159. return rctx;
  160. }
  161. EVP_PKEY_CTX_free(rctx);
  162. return NULL;
  163. }
  164. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) { return ctx->pkey; }
  165. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) {
  166. ctx->app_data = data;
  167. }
  168. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) { return ctx->app_data; }
  169. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, int cmd,
  170. int p1, void *p2) {
  171. int ret;
  172. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  173. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_ctrl, EVP_R_COMMAND_NOT_SUPPORTED);
  174. return -2;
  175. }
  176. if (keytype != -1 && ctx->pmeth->pkey_id != keytype) {
  177. return -1;
  178. }
  179. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  180. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_ctrl, EVP_R_NO_OPERATION_SET);
  181. return -1;
  182. }
  183. if (optype != -1 && !(ctx->operation & optype)) {
  184. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_ctrl, EVP_R_INVALID_OPERATION);
  185. return -1;
  186. }
  187. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  188. if (ret == -2) {
  189. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_CTX_ctrl, EVP_R_COMMAND_NOT_SUPPORTED);
  190. }
  191. return ret;
  192. }
  193. int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx) {
  194. int ret;
  195. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  196. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_sign_init,
  197. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  198. return 0;
  199. }
  200. ctx->operation = EVP_PKEY_OP_SIGN;
  201. if (!ctx->pmeth->sign_init) {
  202. return 1;
  203. }
  204. ret = ctx->pmeth->sign_init(ctx);
  205. /* TODO(fork): normalise the return value convention. */
  206. if (ret <= 0) {
  207. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  208. return 0;
  209. }
  210. return ret;
  211. }
  212. static int check_autoarg(const EVP_PKEY_CTX *ctx, const uint8_t *arg,
  213. size_t *arg_len) {
  214. size_t size;
  215. if (0 == (ctx->pmeth->flags & EVP_PKEY_FLAG_AUTOARGLEN)) {
  216. return 1;
  217. }
  218. size = EVP_PKEY_size(ctx->pkey);
  219. if (arg == NULL) {
  220. *arg_len = size;
  221. return 1;
  222. }
  223. if (*arg_len < size) {
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. int EVP_PKEY_sign(EVP_PKEY_CTX *ctx, uint8_t *sig, size_t *sig_len,
  229. const uint8_t *data, size_t data_len) {
  230. if (!ctx || !ctx->pmeth || !ctx->pmeth->sign) {
  231. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_sign,
  232. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  233. return 0;
  234. }
  235. if (ctx->operation != EVP_PKEY_OP_SIGN) {
  236. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_sign, EVP_R_OPERATON_NOT_INITIALIZED);
  237. return 0;
  238. }
  239. if (!check_autoarg(ctx, sig, sig_len)) {
  240. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_sign,
  241. EVP_R_BUFFER_TOO_SMALL);
  242. return 0;
  243. }
  244. return ctx->pmeth->sign(ctx, sig, sig_len, data, data_len);
  245. }
  246. int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx) {
  247. int ret;
  248. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  249. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_verify_init,
  250. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  251. return -2;
  252. }
  253. ctx->operation = EVP_PKEY_OP_VERIFY;
  254. if (!ctx->pmeth->verify_init) {
  255. return 1;
  256. }
  257. ret = ctx->pmeth->verify_init(ctx);
  258. if (ret <= 0) {
  259. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  260. }
  261. return ret;
  262. }
  263. int EVP_PKEY_verify(EVP_PKEY_CTX *ctx, const uint8_t *sig, size_t sig_len,
  264. const uint8_t *data, size_t data_len) {
  265. if (!ctx || !ctx->pmeth || !ctx->pmeth->verify) {
  266. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_verify,
  267. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  268. return -2;
  269. }
  270. if (ctx->operation != EVP_PKEY_OP_VERIFY) {
  271. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_verify, EVP_R_OPERATON_NOT_INITIALIZED);
  272. return -1;
  273. }
  274. return ctx->pmeth->verify(ctx, sig, sig_len, data, data_len);
  275. }
  276. int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) {
  277. int ret;
  278. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  279. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_encrypt_init,
  280. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  281. return -2;
  282. }
  283. ctx->operation = EVP_PKEY_OP_ENCRYPT;
  284. if (!ctx->pmeth->encrypt_init) {
  285. return 1;
  286. }
  287. ret = ctx->pmeth->encrypt_init(ctx);
  288. if (ret <= 0) {
  289. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  290. }
  291. return ret;
  292. }
  293. int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  294. const uint8_t *in, size_t inlen) {
  295. if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
  296. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_encrypt,
  297. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  298. return -2;
  299. }
  300. if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
  301. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_encrypt, EVP_R_OPERATON_NOT_INITIALIZED);
  302. return -1;
  303. }
  304. if (!check_autoarg(ctx, out, outlen)) {
  305. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_encrypt, EVP_R_BUFFER_TOO_SMALL);
  306. return 0;
  307. }
  308. return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
  309. }
  310. int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) {
  311. int ret;
  312. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  313. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_decrypt_init,
  314. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  315. return -2;
  316. }
  317. ctx->operation = EVP_PKEY_OP_DECRYPT;
  318. if (!ctx->pmeth->decrypt_init) {
  319. return 1;
  320. }
  321. ret = ctx->pmeth->decrypt_init(ctx);
  322. if (ret <= 0) {
  323. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  324. }
  325. return ret;
  326. }
  327. int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *outlen,
  328. const uint8_t *in, size_t inlen) {
  329. if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
  330. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_decrypt,
  331. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  332. return -2;
  333. }
  334. if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
  335. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_decrypt, EVP_R_OPERATON_NOT_INITIALIZED);
  336. return -1;
  337. }
  338. if (!check_autoarg(ctx, out, outlen)) {
  339. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_decrypt, EVP_R_BUFFER_TOO_SMALL);
  340. return 0;
  341. }
  342. return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);
  343. }
  344. int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx) {
  345. int ret;
  346. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  347. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_init,
  348. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  349. return -2;
  350. }
  351. ctx->operation = EVP_PKEY_OP_DERIVE;
  352. if (!ctx->pmeth->derive_init) {
  353. return 1;
  354. }
  355. ret = ctx->pmeth->derive_init(ctx);
  356. if (ret <= 0) {
  357. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  358. }
  359. return ret;
  360. }
  361. int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer) {
  362. int ret;
  363. if (!ctx || !ctx->pmeth ||
  364. !(ctx->pmeth->derive || ctx->pmeth->encrypt || ctx->pmeth->decrypt) ||
  365. !ctx->pmeth->ctrl) {
  366. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_set_peer,
  367. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  368. return -2;
  369. }
  370. if (ctx->operation != EVP_PKEY_OP_DERIVE &&
  371. ctx->operation != EVP_PKEY_OP_ENCRYPT &&
  372. ctx->operation != EVP_PKEY_OP_DECRYPT) {
  373. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_set_peer,
  374. EVP_R_OPERATON_NOT_INITIALIZED);
  375. return -1;
  376. }
  377. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 0, peer);
  378. if (ret <= 0) {
  379. return ret;
  380. }
  381. if (ret == 2) {
  382. return 1;
  383. }
  384. if (!ctx->pkey) {
  385. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_set_peer, EVP_R_NO_KEY_SET);
  386. return -1;
  387. }
  388. if (ctx->pkey->type != peer->type) {
  389. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_set_peer, EVP_R_DIFFERENT_KEY_TYPES);
  390. return -1;
  391. }
  392. /* ran@cryptocom.ru: For clarity. The error is if parameters in peer are
  393. * present (!missing) but don't match. EVP_PKEY_cmp_parameters may return
  394. * 1 (match), 0 (don't match) and -2 (comparison is not defined). -1
  395. * (different key types) is impossible here because it is checked earlier.
  396. * -2 is OK for us here, as well as 1, so we can check for 0 only. */
  397. if (!EVP_PKEY_missing_parameters(peer) &&
  398. !EVP_PKEY_cmp_parameters(ctx->pkey, peer)) {
  399. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive_set_peer,
  400. EVP_R_DIFFERENT_PARAMETERS);
  401. return -1;
  402. }
  403. if (ctx->peerkey) {
  404. EVP_PKEY_free(ctx->peerkey);
  405. }
  406. ctx->peerkey = peer;
  407. ret = ctx->pmeth->ctrl(ctx, EVP_PKEY_CTRL_PEER_KEY, 1, peer);
  408. if (ret <= 0) {
  409. ctx->peerkey = NULL;
  410. return ret;
  411. }
  412. CRYPTO_add(&peer->references, 1, CRYPTO_LOCK_EVP_PKEY);
  413. return 1;
  414. }
  415. int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, uint8_t *key, size_t *out_key_len) {
  416. if (!ctx || !ctx->pmeth || !ctx->pmeth->derive) {
  417. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive,
  418. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  419. return -2;
  420. }
  421. if (ctx->operation != EVP_PKEY_OP_DERIVE) {
  422. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive, EVP_R_OPERATON_NOT_INITIALIZED);
  423. return -1;
  424. }
  425. if (!check_autoarg(ctx, key, out_key_len)) {
  426. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_derive, EVP_R_BUFFER_TOO_SMALL);
  427. return 0;
  428. }
  429. return ctx->pmeth->derive(ctx, key, out_key_len);
  430. }
  431. int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) {
  432. int ret;
  433. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  434. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_keygen_init,
  435. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  436. return -2;
  437. }
  438. ctx->operation = EVP_PKEY_OP_KEYGEN;
  439. if (!ctx->pmeth->keygen_init) {
  440. return 1;
  441. }
  442. ret = ctx->pmeth->keygen_init(ctx);
  443. if (ret <= 0) {
  444. ctx->operation = EVP_PKEY_OP_UNDEFINED;
  445. }
  446. return ret;
  447. }
  448. int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) {
  449. int ret;
  450. if (!ctx || !ctx->pmeth || !ctx->pmeth->keygen) {
  451. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_keygen,
  452. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  453. return -2;
  454. }
  455. if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
  456. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_keygen, EVP_R_OPERATON_NOT_INITIALIZED);
  457. return -1;
  458. }
  459. if (!ppkey) {
  460. return -1;
  461. }
  462. if (!*ppkey) {
  463. *ppkey = EVP_PKEY_new();
  464. }
  465. ret = ctx->pmeth->keygen(ctx, *ppkey);
  466. if (ret <= 0) {
  467. EVP_PKEY_free(*ppkey);
  468. *ppkey = NULL;
  469. }
  470. return ret;
  471. }