pmeth_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 "internal/asn1_int.h"
  68. #include "internal/evp_int.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. &cmac_pkey_meth,
  90. #ifndef OPENSSL_NO_DH
  91. &dhx_pkey_meth
  92. #endif
  93. };
  94. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  95. pmeth);
  96. static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
  97. const EVP_PKEY_METHOD *const *b)
  98. {
  99. return ((*a)->pkey_id - (*b)->pkey_id);
  100. }
  101. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  102. pmeth);
  103. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  104. {
  105. EVP_PKEY_METHOD tmp;
  106. const EVP_PKEY_METHOD *t = &tmp, **ret;
  107. tmp.pkey_id = type;
  108. if (app_pkey_methods) {
  109. int idx;
  110. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  111. if (idx >= 0)
  112. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  113. }
  114. ret = OBJ_bsearch_pmeth(&t, standard_methods,
  115. sizeof(standard_methods) /
  116. sizeof(EVP_PKEY_METHOD *));
  117. if (!ret || !*ret)
  118. return NULL;
  119. return *ret;
  120. }
  121. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
  122. {
  123. EVP_PKEY_CTX *ret;
  124. const EVP_PKEY_METHOD *pmeth;
  125. if (id == -1) {
  126. if (!pkey || !pkey->ameth)
  127. return NULL;
  128. id = pkey->ameth->pkey_id;
  129. }
  130. #ifndef OPENSSL_NO_ENGINE
  131. if (pkey && pkey->engine)
  132. e = pkey->engine;
  133. /* Try to find an ENGINE which implements this method */
  134. if (e) {
  135. if (!ENGINE_init(e)) {
  136. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
  137. return NULL;
  138. }
  139. } else
  140. e = ENGINE_get_pkey_meth_engine(id);
  141. /*
  142. * If an ENGINE handled this method look it up. Othewise use internal
  143. * tables.
  144. */
  145. if (e)
  146. pmeth = ENGINE_get_pkey_meth(e, id);
  147. else
  148. #endif
  149. pmeth = EVP_PKEY_meth_find(id);
  150. if (pmeth == NULL) {
  151. EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
  152. return NULL;
  153. }
  154. ret = OPENSSL_malloc(sizeof(*ret));
  155. if (!ret) {
  156. #ifndef OPENSSL_NO_ENGINE
  157. if (e)
  158. ENGINE_finish(e);
  159. #endif
  160. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
  161. return NULL;
  162. }
  163. ret->engine = e;
  164. ret->pmeth = pmeth;
  165. ret->operation = EVP_PKEY_OP_UNDEFINED;
  166. ret->pkey = pkey;
  167. ret->peerkey = NULL;
  168. ret->pkey_gencb = 0;
  169. if (pkey)
  170. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  171. ret->data = NULL;
  172. if (pmeth->init) {
  173. if (pmeth->init(ret) <= 0) {
  174. EVP_PKEY_CTX_free(ret);
  175. return NULL;
  176. }
  177. }
  178. return ret;
  179. }
  180. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  181. {
  182. EVP_PKEY_METHOD *pmeth;
  183. pmeth = OPENSSL_malloc(sizeof(*pmeth));
  184. if (!pmeth)
  185. return NULL;
  186. memset(pmeth, 0, sizeof(*pmeth));
  187. pmeth->pkey_id = id;
  188. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  189. pmeth->init = 0;
  190. pmeth->copy = 0;
  191. pmeth->cleanup = 0;
  192. pmeth->paramgen_init = 0;
  193. pmeth->paramgen = 0;
  194. pmeth->keygen_init = 0;
  195. pmeth->keygen = 0;
  196. pmeth->sign_init = 0;
  197. pmeth->sign = 0;
  198. pmeth->verify_init = 0;
  199. pmeth->verify = 0;
  200. pmeth->verify_recover_init = 0;
  201. pmeth->verify_recover = 0;
  202. pmeth->signctx_init = 0;
  203. pmeth->signctx = 0;
  204. pmeth->verifyctx_init = 0;
  205. pmeth->verifyctx = 0;
  206. pmeth->encrypt_init = 0;
  207. pmeth->encrypt = 0;
  208. pmeth->decrypt_init = 0;
  209. pmeth->decrypt = 0;
  210. pmeth->derive_init = 0;
  211. pmeth->derive = 0;
  212. pmeth->ctrl = 0;
  213. pmeth->ctrl_str = 0;
  214. return pmeth;
  215. }
  216. void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
  217. const EVP_PKEY_METHOD *meth)
  218. {
  219. if (ppkey_id)
  220. *ppkey_id = meth->pkey_id;
  221. if (pflags)
  222. *pflags = meth->flags;
  223. }
  224. void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
  225. {
  226. dst->init = src->init;
  227. dst->copy = src->copy;
  228. dst->cleanup = src->cleanup;
  229. dst->paramgen_init = src->paramgen_init;
  230. dst->paramgen = src->paramgen;
  231. dst->keygen_init = src->keygen_init;
  232. dst->keygen = src->keygen;
  233. dst->sign_init = src->sign_init;
  234. dst->sign = src->sign;
  235. dst->verify_init = src->verify_init;
  236. dst->verify = src->verify;
  237. dst->verify_recover_init = src->verify_recover_init;
  238. dst->verify_recover = src->verify_recover;
  239. dst->signctx_init = src->signctx_init;
  240. dst->signctx = src->signctx;
  241. dst->verifyctx_init = src->verifyctx_init;
  242. dst->verifyctx = src->verifyctx;
  243. dst->encrypt_init = src->encrypt_init;
  244. dst->encrypt = src->encrypt;
  245. dst->decrypt_init = src->decrypt_init;
  246. dst->decrypt = src->decrypt;
  247. dst->derive_init = src->derive_init;
  248. dst->derive = src->derive;
  249. dst->ctrl = src->ctrl;
  250. dst->ctrl_str = src->ctrl_str;
  251. }
  252. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  253. {
  254. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  255. OPENSSL_free(pmeth);
  256. }
  257. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  258. {
  259. return int_ctx_new(pkey, e, -1);
  260. }
  261. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  262. {
  263. return int_ctx_new(NULL, e, id);
  264. }
  265. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
  266. {
  267. EVP_PKEY_CTX *rctx;
  268. if (!pctx->pmeth || !pctx->pmeth->copy)
  269. return NULL;
  270. #ifndef OPENSSL_NO_ENGINE
  271. /* Make sure it's safe to copy a pkey context using an ENGINE */
  272. if (pctx->engine && !ENGINE_init(pctx->engine)) {
  273. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
  274. return 0;
  275. }
  276. #endif
  277. rctx = OPENSSL_malloc(sizeof(*rctx));
  278. if (!rctx)
  279. return NULL;
  280. rctx->pmeth = pctx->pmeth;
  281. #ifndef OPENSSL_NO_ENGINE
  282. rctx->engine = pctx->engine;
  283. #endif
  284. if (pctx->pkey)
  285. CRYPTO_add(&pctx->pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  286. rctx->pkey = pctx->pkey;
  287. if (pctx->peerkey)
  288. CRYPTO_add(&pctx->peerkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  289. rctx->peerkey = pctx->peerkey;
  290. rctx->data = NULL;
  291. rctx->app_data = NULL;
  292. rctx->operation = pctx->operation;
  293. if (pctx->pmeth->copy(rctx, pctx) > 0)
  294. return rctx;
  295. EVP_PKEY_CTX_free(rctx);
  296. return NULL;
  297. }
  298. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  299. {
  300. if (app_pkey_methods == NULL) {
  301. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  302. if (!app_pkey_methods)
  303. return 0;
  304. }
  305. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  306. return 0;
  307. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  308. return 1;
  309. }
  310. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  311. {
  312. if (ctx == NULL)
  313. return;
  314. if (ctx->pmeth && ctx->pmeth->cleanup)
  315. ctx->pmeth->cleanup(ctx);
  316. EVP_PKEY_free(ctx->pkey);
  317. EVP_PKEY_free(ctx->peerkey);
  318. #ifndef OPENSSL_NO_ENGINE
  319. if (ctx->engine)
  320. /*
  321. * The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  322. * functional reference we held for this reason.
  323. */
  324. ENGINE_finish(ctx->engine);
  325. #endif
  326. OPENSSL_free(ctx);
  327. }
  328. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  329. int cmd, int p1, void *p2)
  330. {
  331. int ret;
  332. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  333. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  334. return -2;
  335. }
  336. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  337. return -1;
  338. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  339. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  340. return -1;
  341. }
  342. if ((optype != -1) && !(ctx->operation & optype)) {
  343. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  344. return -1;
  345. }
  346. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  347. if (ret == -2)
  348. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  349. return ret;
  350. }
  351. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  352. const char *name, const char *value)
  353. {
  354. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  355. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  356. return -2;
  357. }
  358. if (strcmp(name, "digest") == 0) {
  359. const EVP_MD *md;
  360. if (!value || !(md = EVP_get_digestbyname(value))) {
  361. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
  362. return 0;
  363. }
  364. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  365. }
  366. return ctx->pmeth->ctrl_str(ctx, name, value);
  367. }
  368. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  369. {
  370. return ctx->operation;
  371. }
  372. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  373. {
  374. ctx->keygen_info = dat;
  375. ctx->keygen_info_count = datlen;
  376. }
  377. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  378. {
  379. ctx->data = data;
  380. }
  381. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  382. {
  383. return ctx->data;
  384. }
  385. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  386. {
  387. return ctx->pkey;
  388. }
  389. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  390. {
  391. return ctx->peerkey;
  392. }
  393. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  394. {
  395. ctx->app_data = data;
  396. }
  397. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  398. {
  399. return ctx->app_data;
  400. }
  401. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  402. int (*init) (EVP_PKEY_CTX *ctx))
  403. {
  404. pmeth->init = init;
  405. }
  406. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  407. int (*copy) (EVP_PKEY_CTX *dst,
  408. EVP_PKEY_CTX *src))
  409. {
  410. pmeth->copy = copy;
  411. }
  412. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  413. void (*cleanup) (EVP_PKEY_CTX *ctx))
  414. {
  415. pmeth->cleanup = cleanup;
  416. }
  417. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  418. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  419. int (*paramgen) (EVP_PKEY_CTX *ctx,
  420. EVP_PKEY *pkey))
  421. {
  422. pmeth->paramgen_init = paramgen_init;
  423. pmeth->paramgen = paramgen;
  424. }
  425. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  426. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  427. int (*keygen) (EVP_PKEY_CTX *ctx,
  428. EVP_PKEY *pkey))
  429. {
  430. pmeth->keygen_init = keygen_init;
  431. pmeth->keygen = keygen;
  432. }
  433. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  434. int (*sign_init) (EVP_PKEY_CTX *ctx),
  435. int (*sign) (EVP_PKEY_CTX *ctx,
  436. unsigned char *sig, size_t *siglen,
  437. const unsigned char *tbs,
  438. size_t tbslen))
  439. {
  440. pmeth->sign_init = sign_init;
  441. pmeth->sign = sign;
  442. }
  443. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  444. int (*verify_init) (EVP_PKEY_CTX *ctx),
  445. int (*verify) (EVP_PKEY_CTX *ctx,
  446. const unsigned char *sig,
  447. size_t siglen,
  448. const unsigned char *tbs,
  449. size_t tbslen))
  450. {
  451. pmeth->verify_init = verify_init;
  452. pmeth->verify = verify;
  453. }
  454. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  455. int (*verify_recover_init) (EVP_PKEY_CTX
  456. *ctx),
  457. int (*verify_recover) (EVP_PKEY_CTX
  458. *ctx,
  459. unsigned char
  460. *sig,
  461. size_t *siglen,
  462. const unsigned
  463. char *tbs,
  464. size_t tbslen))
  465. {
  466. pmeth->verify_recover_init = verify_recover_init;
  467. pmeth->verify_recover = verify_recover;
  468. }
  469. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  470. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  471. EVP_MD_CTX *mctx),
  472. int (*signctx) (EVP_PKEY_CTX *ctx,
  473. unsigned char *sig,
  474. size_t *siglen,
  475. EVP_MD_CTX *mctx))
  476. {
  477. pmeth->signctx_init = signctx_init;
  478. pmeth->signctx = signctx;
  479. }
  480. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  481. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  482. EVP_MD_CTX *mctx),
  483. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  484. const unsigned char *sig,
  485. int siglen,
  486. EVP_MD_CTX *mctx))
  487. {
  488. pmeth->verifyctx_init = verifyctx_init;
  489. pmeth->verifyctx = verifyctx;
  490. }
  491. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  492. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  493. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  494. unsigned char *out,
  495. size_t *outlen,
  496. const unsigned char *in,
  497. size_t inlen))
  498. {
  499. pmeth->encrypt_init = encrypt_init;
  500. pmeth->encrypt = encryptfn;
  501. }
  502. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  503. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  504. int (*decrypt) (EVP_PKEY_CTX *ctx,
  505. unsigned char *out,
  506. size_t *outlen,
  507. const unsigned char *in,
  508. size_t inlen))
  509. {
  510. pmeth->decrypt_init = decrypt_init;
  511. pmeth->decrypt = decrypt;
  512. }
  513. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  514. int (*derive_init) (EVP_PKEY_CTX *ctx),
  515. int (*derive) (EVP_PKEY_CTX *ctx,
  516. unsigned char *key,
  517. size_t *keylen))
  518. {
  519. pmeth->derive_init = derive_init;
  520. pmeth->derive = derive;
  521. }
  522. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  523. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  524. void *p2),
  525. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  526. const char *type,
  527. const char *value))
  528. {
  529. pmeth->ctrl = ctrl;
  530. pmeth->ctrl_str = ctrl_str;
  531. }