pmeth_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /* pmeth_lib.c */
  2. /*
  3. * Written by Dr Stephen N Henson ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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. EVP_PKEY_CTX_free(ret);
  177. return NULL;
  178. }
  179. }
  180. return ret;
  181. }
  182. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  183. {
  184. EVP_PKEY_METHOD *pmeth;
  185. pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
  186. if (!pmeth)
  187. return NULL;
  188. memset(pmeth, 0, sizeof(EVP_PKEY_METHOD));
  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. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
  276. return 0;
  277. }
  278. #endif
  279. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  280. if (!rctx)
  281. return NULL;
  282. rctx->pmeth = pctx->pmeth;
  283. #ifndef OPENSSL_NO_ENGINE
  284. rctx->engine = pctx->engine;
  285. #endif
  286. if (pctx->pkey)
  287. CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  288. rctx->pkey = pctx->pkey;
  289. if (pctx->peerkey)
  290. CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  291. rctx->peerkey = pctx->peerkey;
  292. rctx->data = NULL;
  293. rctx->app_data = NULL;
  294. rctx->operation = pctx->operation;
  295. if (pctx->pmeth->copy(rctx, pctx) > 0)
  296. return rctx;
  297. EVP_PKEY_CTX_free(rctx);
  298. return NULL;
  299. }
  300. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  301. {
  302. if (app_pkey_methods == NULL) {
  303. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  304. if (!app_pkey_methods)
  305. return 0;
  306. }
  307. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  308. return 0;
  309. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  310. return 1;
  311. }
  312. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  313. {
  314. if (ctx == NULL)
  315. return;
  316. if (ctx->pmeth && ctx->pmeth->cleanup)
  317. ctx->pmeth->cleanup(ctx);
  318. if (ctx->pkey)
  319. EVP_PKEY_free(ctx->pkey);
  320. if (ctx->peerkey)
  321. EVP_PKEY_free(ctx->peerkey);
  322. #ifndef OPENSSL_NO_ENGINE
  323. if (ctx->engine)
  324. /*
  325. * The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  326. * functional reference we held for this reason.
  327. */
  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. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  338. return -2;
  339. }
  340. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  341. return -1;
  342. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  343. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  344. return -1;
  345. }
  346. if ((optype != -1) && !(ctx->operation & optype)) {
  347. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  348. return -1;
  349. }
  350. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  351. if (ret == -2)
  352. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  353. return ret;
  354. }
  355. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  356. const char *name, const char *value)
  357. {
  358. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  359. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  360. return -2;
  361. }
  362. if (!strcmp(name, "digest")) {
  363. const EVP_MD *md;
  364. if (!value || !(md = EVP_get_digestbyname(value))) {
  365. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
  366. return 0;
  367. }
  368. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  369. }
  370. return ctx->pmeth->ctrl_str(ctx, name, value);
  371. }
  372. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  373. {
  374. return ctx->operation;
  375. }
  376. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  377. {
  378. ctx->keygen_info = dat;
  379. ctx->keygen_info_count = datlen;
  380. }
  381. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  382. {
  383. ctx->data = data;
  384. }
  385. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  386. {
  387. return ctx->data;
  388. }
  389. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  390. {
  391. return ctx->pkey;
  392. }
  393. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  394. {
  395. return ctx->peerkey;
  396. }
  397. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  398. {
  399. ctx->app_data = data;
  400. }
  401. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  402. {
  403. return ctx->app_data;
  404. }
  405. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  406. int (*init) (EVP_PKEY_CTX *ctx))
  407. {
  408. pmeth->init = init;
  409. }
  410. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  411. int (*copy) (EVP_PKEY_CTX *dst,
  412. EVP_PKEY_CTX *src))
  413. {
  414. pmeth->copy = copy;
  415. }
  416. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  417. void (*cleanup) (EVP_PKEY_CTX *ctx))
  418. {
  419. pmeth->cleanup = cleanup;
  420. }
  421. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  422. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  423. int (*paramgen) (EVP_PKEY_CTX *ctx,
  424. EVP_PKEY *pkey))
  425. {
  426. pmeth->paramgen_init = paramgen_init;
  427. pmeth->paramgen = paramgen;
  428. }
  429. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  430. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  431. int (*keygen) (EVP_PKEY_CTX *ctx,
  432. EVP_PKEY *pkey))
  433. {
  434. pmeth->keygen_init = keygen_init;
  435. pmeth->keygen = keygen;
  436. }
  437. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  438. int (*sign_init) (EVP_PKEY_CTX *ctx),
  439. int (*sign) (EVP_PKEY_CTX *ctx,
  440. unsigned char *sig, size_t *siglen,
  441. const unsigned char *tbs,
  442. size_t tbslen))
  443. {
  444. pmeth->sign_init = sign_init;
  445. pmeth->sign = sign;
  446. }
  447. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  448. int (*verify_init) (EVP_PKEY_CTX *ctx),
  449. int (*verify) (EVP_PKEY_CTX *ctx,
  450. const unsigned char *sig,
  451. size_t siglen,
  452. const unsigned char *tbs,
  453. 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
  460. *ctx),
  461. int (*verify_recover) (EVP_PKEY_CTX
  462. *ctx,
  463. unsigned char
  464. *sig,
  465. size_t *siglen,
  466. const unsigned
  467. char *tbs,
  468. size_t tbslen))
  469. {
  470. pmeth->verify_recover_init = verify_recover_init;
  471. pmeth->verify_recover = verify_recover;
  472. }
  473. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  474. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  475. EVP_MD_CTX *mctx),
  476. int (*signctx) (EVP_PKEY_CTX *ctx,
  477. unsigned char *sig,
  478. size_t *siglen,
  479. EVP_MD_CTX *mctx))
  480. {
  481. pmeth->signctx_init = signctx_init;
  482. pmeth->signctx = signctx;
  483. }
  484. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  485. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  486. EVP_MD_CTX *mctx),
  487. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  488. const unsigned char *sig,
  489. int siglen,
  490. EVP_MD_CTX *mctx))
  491. {
  492. pmeth->verifyctx_init = verifyctx_init;
  493. pmeth->verifyctx = verifyctx;
  494. }
  495. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  496. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  497. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  498. unsigned char *out,
  499. size_t *outlen,
  500. const unsigned char *in,
  501. size_t inlen))
  502. {
  503. pmeth->encrypt_init = encrypt_init;
  504. pmeth->encrypt = encryptfn;
  505. }
  506. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  507. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  508. int (*decrypt) (EVP_PKEY_CTX *ctx,
  509. unsigned char *out,
  510. size_t *outlen,
  511. const unsigned char *in,
  512. size_t inlen))
  513. {
  514. pmeth->decrypt_init = decrypt_init;
  515. pmeth->decrypt = decrypt;
  516. }
  517. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  518. int (*derive_init) (EVP_PKEY_CTX *ctx),
  519. int (*derive) (EVP_PKEY_CTX *ctx,
  520. unsigned char *key,
  521. size_t *keylen))
  522. {
  523. pmeth->derive_init = derive_init;
  524. pmeth->derive = derive;
  525. }
  526. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  527. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  528. void *p2),
  529. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  530. const char *type,
  531. const char *value))
  532. {
  533. pmeth->ctrl = ctrl;
  534. pmeth->ctrl_str = ctrl_str;
  535. }