2
0

pmeth_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* pmeth_lib.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 2006.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include "cryptlib.h"
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.h>
  64. #ifndef OPENSSL_NO_ENGINE
  65. # include <openssl/engine.h>
  66. #endif
  67. #include "asn1_locl.h"
  68. #include "evp_locl.h"
  69. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  70. DECLARE_STACK_OF(EVP_PKEY_METHOD)
  71. STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
  72. extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
  73. extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth, cmac_pkey_meth;
  74. extern const EVP_PKEY_METHOD dhx_pkey_meth;
  75. static const EVP_PKEY_METHOD *standard_methods[] = {
  76. #ifndef OPENSSL_NO_RSA
  77. &rsa_pkey_meth,
  78. #endif
  79. #ifndef OPENSSL_NO_DH
  80. &dh_pkey_meth,
  81. #endif
  82. #ifndef OPENSSL_NO_DSA
  83. &dsa_pkey_meth,
  84. #endif
  85. #ifndef OPENSSL_NO_EC
  86. &ec_pkey_meth,
  87. #endif
  88. &hmac_pkey_meth,
  89. #ifndef OPENSSL_NO_CMAC
  90. &cmac_pkey_meth,
  91. #endif
  92. #ifndef OPENSSL_NO_DH
  93. &dhx_pkey_meth
  94. #endif
  95. };
  96. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  97. pmeth);
  98. static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
  99. const EVP_PKEY_METHOD *const *b)
  100. {
  101. return ((*a)->pkey_id - (*b)->pkey_id);
  102. }
  103. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  104. pmeth);
  105. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  106. {
  107. EVP_PKEY_METHOD tmp;
  108. const EVP_PKEY_METHOD *t = &tmp, **ret;
  109. tmp.pkey_id = type;
  110. if (app_pkey_methods) {
  111. int idx;
  112. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  113. if (idx >= 0)
  114. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  115. }
  116. ret = OBJ_bsearch_pmeth(&t, standard_methods,
  117. sizeof(standard_methods) /
  118. sizeof(EVP_PKEY_METHOD *));
  119. if (!ret || !*ret)
  120. return NULL;
  121. return *ret;
  122. }
  123. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
  124. {
  125. EVP_PKEY_CTX *ret;
  126. const EVP_PKEY_METHOD *pmeth;
  127. if (id == -1) {
  128. if (!pkey || !pkey->ameth)
  129. return NULL;
  130. id = pkey->ameth->pkey_id;
  131. }
  132. #ifndef OPENSSL_NO_ENGINE
  133. if (pkey && pkey->engine)
  134. e = pkey->engine;
  135. /* Try to find an ENGINE which implements this method */
  136. if (e) {
  137. if (!ENGINE_init(e)) {
  138. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
  139. return NULL;
  140. }
  141. } else
  142. e = ENGINE_get_pkey_meth_engine(id);
  143. /*
  144. * If an ENGINE handled this method look it up. Othewise use internal
  145. * tables.
  146. */
  147. if (e)
  148. pmeth = ENGINE_get_pkey_meth(e, id);
  149. else
  150. #endif
  151. pmeth = EVP_PKEY_meth_find(id);
  152. if (pmeth == NULL) {
  153. EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
  154. return NULL;
  155. }
  156. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  157. if (!ret) {
  158. #ifndef OPENSSL_NO_ENGINE
  159. if (e)
  160. ENGINE_finish(e);
  161. #endif
  162. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
  163. return NULL;
  164. }
  165. ret->engine = e;
  166. ret->pmeth = pmeth;
  167. ret->operation = EVP_PKEY_OP_UNDEFINED;
  168. ret->pkey = pkey;
  169. ret->peerkey = NULL;
  170. ret->pkey_gencb = 0;
  171. if (pkey)
  172. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  173. ret->data = NULL;
  174. if (pmeth->init) {
  175. if (pmeth->init(ret) <= 0) {
  176. ret->pmeth = NULL;
  177. EVP_PKEY_CTX_free(ret);
  178. return NULL;
  179. }
  180. }
  181. return ret;
  182. }
  183. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  184. {
  185. EVP_PKEY_METHOD *pmeth;
  186. pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
  187. if (!pmeth)
  188. return NULL;
  189. memset(pmeth, 0, sizeof(EVP_PKEY_METHOD));
  190. pmeth->pkey_id = id;
  191. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  192. return pmeth;
  193. }
  194. void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
  195. const EVP_PKEY_METHOD *meth)
  196. {
  197. if (ppkey_id)
  198. *ppkey_id = meth->pkey_id;
  199. if (pflags)
  200. *pflags = meth->flags;
  201. }
  202. void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
  203. {
  204. dst->init = src->init;
  205. dst->copy = src->copy;
  206. dst->cleanup = src->cleanup;
  207. dst->paramgen_init = src->paramgen_init;
  208. dst->paramgen = src->paramgen;
  209. dst->keygen_init = src->keygen_init;
  210. dst->keygen = src->keygen;
  211. dst->sign_init = src->sign_init;
  212. dst->sign = src->sign;
  213. dst->verify_init = src->verify_init;
  214. dst->verify = src->verify;
  215. dst->verify_recover_init = src->verify_recover_init;
  216. dst->verify_recover = src->verify_recover;
  217. dst->signctx_init = src->signctx_init;
  218. dst->signctx = src->signctx;
  219. dst->verifyctx_init = src->verifyctx_init;
  220. dst->verifyctx = src->verifyctx;
  221. dst->encrypt_init = src->encrypt_init;
  222. dst->encrypt = src->encrypt;
  223. dst->decrypt_init = src->decrypt_init;
  224. dst->decrypt = src->decrypt;
  225. dst->derive_init = src->derive_init;
  226. dst->derive = src->derive;
  227. dst->ctrl = src->ctrl;
  228. dst->ctrl_str = src->ctrl_str;
  229. }
  230. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  231. {
  232. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  233. OPENSSL_free(pmeth);
  234. }
  235. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  236. {
  237. return int_ctx_new(pkey, e, -1);
  238. }
  239. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  240. {
  241. return int_ctx_new(NULL, e, id);
  242. }
  243. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
  244. {
  245. EVP_PKEY_CTX *rctx;
  246. if (!pctx->pmeth || !pctx->pmeth->copy)
  247. return NULL;
  248. #ifndef OPENSSL_NO_ENGINE
  249. /* Make sure it's safe to copy a pkey context using an ENGINE */
  250. if (pctx->engine && !ENGINE_init(pctx->engine)) {
  251. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
  252. return 0;
  253. }
  254. #endif
  255. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  256. if (!rctx)
  257. return NULL;
  258. rctx->pmeth = pctx->pmeth;
  259. #ifndef OPENSSL_NO_ENGINE
  260. rctx->engine = pctx->engine;
  261. #endif
  262. if (pctx->pkey)
  263. CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  264. rctx->pkey = pctx->pkey;
  265. if (pctx->peerkey)
  266. CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  267. rctx->peerkey = pctx->peerkey;
  268. rctx->data = NULL;
  269. rctx->app_data = NULL;
  270. rctx->operation = pctx->operation;
  271. if (pctx->pmeth->copy(rctx, pctx) > 0)
  272. return rctx;
  273. rctx->pmeth = NULL;
  274. EVP_PKEY_CTX_free(rctx);
  275. return NULL;
  276. }
  277. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  278. {
  279. if (app_pkey_methods == NULL) {
  280. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  281. if (!app_pkey_methods)
  282. return 0;
  283. }
  284. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  285. return 0;
  286. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  287. return 1;
  288. }
  289. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  290. {
  291. if (ctx == NULL)
  292. return;
  293. if (ctx->pmeth && ctx->pmeth->cleanup)
  294. ctx->pmeth->cleanup(ctx);
  295. if (ctx->pkey)
  296. EVP_PKEY_free(ctx->pkey);
  297. if (ctx->peerkey)
  298. EVP_PKEY_free(ctx->peerkey);
  299. #ifndef OPENSSL_NO_ENGINE
  300. if (ctx->engine)
  301. /*
  302. * The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  303. * functional reference we held for this reason.
  304. */
  305. ENGINE_finish(ctx->engine);
  306. #endif
  307. OPENSSL_free(ctx);
  308. }
  309. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  310. int cmd, int p1, void *p2)
  311. {
  312. int ret;
  313. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  314. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  315. return -2;
  316. }
  317. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  318. return -1;
  319. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  320. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  321. return -1;
  322. }
  323. if ((optype != -1) && !(ctx->operation & optype)) {
  324. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  325. return -1;
  326. }
  327. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  328. if (ret == -2)
  329. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  330. return ret;
  331. }
  332. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  333. const char *name, const char *value)
  334. {
  335. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  336. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  337. return -2;
  338. }
  339. if (!strcmp(name, "digest")) {
  340. const EVP_MD *md;
  341. if (!value || !(md = EVP_get_digestbyname(value))) {
  342. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
  343. return 0;
  344. }
  345. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  346. }
  347. return ctx->pmeth->ctrl_str(ctx, name, value);
  348. }
  349. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  350. {
  351. return ctx->operation;
  352. }
  353. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  354. {
  355. ctx->keygen_info = dat;
  356. ctx->keygen_info_count = datlen;
  357. }
  358. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  359. {
  360. ctx->data = data;
  361. }
  362. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  363. {
  364. return ctx->data;
  365. }
  366. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  367. {
  368. return ctx->pkey;
  369. }
  370. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  371. {
  372. return ctx->peerkey;
  373. }
  374. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  375. {
  376. ctx->app_data = data;
  377. }
  378. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  379. {
  380. return ctx->app_data;
  381. }
  382. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  383. int (*init) (EVP_PKEY_CTX *ctx))
  384. {
  385. pmeth->init = init;
  386. }
  387. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  388. int (*copy) (EVP_PKEY_CTX *dst,
  389. EVP_PKEY_CTX *src))
  390. {
  391. pmeth->copy = copy;
  392. }
  393. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  394. void (*cleanup) (EVP_PKEY_CTX *ctx))
  395. {
  396. pmeth->cleanup = cleanup;
  397. }
  398. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  399. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  400. int (*paramgen) (EVP_PKEY_CTX *ctx,
  401. EVP_PKEY *pkey))
  402. {
  403. pmeth->paramgen_init = paramgen_init;
  404. pmeth->paramgen = paramgen;
  405. }
  406. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  407. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  408. int (*keygen) (EVP_PKEY_CTX *ctx,
  409. EVP_PKEY *pkey))
  410. {
  411. pmeth->keygen_init = keygen_init;
  412. pmeth->keygen = keygen;
  413. }
  414. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  415. int (*sign_init) (EVP_PKEY_CTX *ctx),
  416. int (*sign) (EVP_PKEY_CTX *ctx,
  417. unsigned char *sig, size_t *siglen,
  418. const unsigned char *tbs,
  419. size_t tbslen))
  420. {
  421. pmeth->sign_init = sign_init;
  422. pmeth->sign = sign;
  423. }
  424. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  425. int (*verify_init) (EVP_PKEY_CTX *ctx),
  426. int (*verify) (EVP_PKEY_CTX *ctx,
  427. const unsigned char *sig,
  428. size_t siglen,
  429. const unsigned char *tbs,
  430. size_t tbslen))
  431. {
  432. pmeth->verify_init = verify_init;
  433. pmeth->verify = verify;
  434. }
  435. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  436. int (*verify_recover_init) (EVP_PKEY_CTX
  437. *ctx),
  438. int (*verify_recover) (EVP_PKEY_CTX
  439. *ctx,
  440. unsigned char
  441. *sig,
  442. size_t *siglen,
  443. const unsigned
  444. char *tbs,
  445. size_t tbslen))
  446. {
  447. pmeth->verify_recover_init = verify_recover_init;
  448. pmeth->verify_recover = verify_recover;
  449. }
  450. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  451. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  452. EVP_MD_CTX *mctx),
  453. int (*signctx) (EVP_PKEY_CTX *ctx,
  454. unsigned char *sig,
  455. size_t *siglen,
  456. EVP_MD_CTX *mctx))
  457. {
  458. pmeth->signctx_init = signctx_init;
  459. pmeth->signctx = signctx;
  460. }
  461. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  462. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  463. EVP_MD_CTX *mctx),
  464. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  465. const unsigned char *sig,
  466. int siglen,
  467. EVP_MD_CTX *mctx))
  468. {
  469. pmeth->verifyctx_init = verifyctx_init;
  470. pmeth->verifyctx = verifyctx;
  471. }
  472. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  473. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  474. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  475. unsigned char *out,
  476. size_t *outlen,
  477. const unsigned char *in,
  478. size_t inlen))
  479. {
  480. pmeth->encrypt_init = encrypt_init;
  481. pmeth->encrypt = encryptfn;
  482. }
  483. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  484. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  485. int (*decrypt) (EVP_PKEY_CTX *ctx,
  486. unsigned char *out,
  487. size_t *outlen,
  488. const unsigned char *in,
  489. size_t inlen))
  490. {
  491. pmeth->decrypt_init = decrypt_init;
  492. pmeth->decrypt = decrypt;
  493. }
  494. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  495. int (*derive_init) (EVP_PKEY_CTX *ctx),
  496. int (*derive) (EVP_PKEY_CTX *ctx,
  497. unsigned char *key,
  498. size_t *keylen))
  499. {
  500. pmeth->derive_init = derive_init;
  501. pmeth->derive = derive;
  502. }
  503. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  504. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  505. void *p2),
  506. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  507. const char *type,
  508. const char *value))
  509. {
  510. pmeth->ctrl = ctrl;
  511. pmeth->ctrl_str = ctrl_str;
  512. }