pmeth_lib.c 15 KB

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