pmeth_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. &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(EVP_PKEY_CTX));
  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(EVP_PKEY_METHOD));
  184. if (!pmeth)
  185. return NULL;
  186. memset(pmeth, 0, sizeof(EVP_PKEY_METHOD));
  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(EVP_PKEY_CTX));
  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. if (ctx->pkey)
  317. EVP_PKEY_free(ctx->pkey);
  318. if (ctx->peerkey)
  319. EVP_PKEY_free(ctx->peerkey);
  320. #ifndef OPENSSL_NO_ENGINE
  321. if (ctx->engine)
  322. /*
  323. * The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  324. * functional reference we held for this reason.
  325. */
  326. ENGINE_finish(ctx->engine);
  327. #endif
  328. OPENSSL_free(ctx);
  329. }
  330. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  331. int cmd, int p1, void *p2)
  332. {
  333. int ret;
  334. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  335. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  336. return -2;
  337. }
  338. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  339. return -1;
  340. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  341. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  342. return -1;
  343. }
  344. if ((optype != -1) && !(ctx->operation & optype)) {
  345. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  346. return -1;
  347. }
  348. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  349. if (ret == -2)
  350. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  351. return ret;
  352. }
  353. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  354. const char *name, const char *value)
  355. {
  356. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  357. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  358. return -2;
  359. }
  360. if (!strcmp(name, "digest")) {
  361. const EVP_MD *md;
  362. if (!value || !(md = EVP_get_digestbyname(value))) {
  363. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_INVALID_DIGEST);
  364. return 0;
  365. }
  366. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  367. }
  368. return ctx->pmeth->ctrl_str(ctx, name, value);
  369. }
  370. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  371. {
  372. return ctx->operation;
  373. }
  374. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  375. {
  376. ctx->keygen_info = dat;
  377. ctx->keygen_info_count = datlen;
  378. }
  379. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  380. {
  381. ctx->data = data;
  382. }
  383. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  384. {
  385. return ctx->data;
  386. }
  387. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  388. {
  389. return ctx->pkey;
  390. }
  391. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  392. {
  393. return ctx->peerkey;
  394. }
  395. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  396. {
  397. ctx->app_data = data;
  398. }
  399. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  400. {
  401. return ctx->app_data;
  402. }
  403. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  404. int (*init) (EVP_PKEY_CTX *ctx))
  405. {
  406. pmeth->init = init;
  407. }
  408. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  409. int (*copy) (EVP_PKEY_CTX *dst,
  410. EVP_PKEY_CTX *src))
  411. {
  412. pmeth->copy = copy;
  413. }
  414. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  415. void (*cleanup) (EVP_PKEY_CTX *ctx))
  416. {
  417. pmeth->cleanup = cleanup;
  418. }
  419. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  420. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  421. int (*paramgen) (EVP_PKEY_CTX *ctx,
  422. EVP_PKEY *pkey))
  423. {
  424. pmeth->paramgen_init = paramgen_init;
  425. pmeth->paramgen = paramgen;
  426. }
  427. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  428. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  429. int (*keygen) (EVP_PKEY_CTX *ctx,
  430. EVP_PKEY *pkey))
  431. {
  432. pmeth->keygen_init = keygen_init;
  433. pmeth->keygen = keygen;
  434. }
  435. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  436. int (*sign_init) (EVP_PKEY_CTX *ctx),
  437. int (*sign) (EVP_PKEY_CTX *ctx,
  438. unsigned char *sig, size_t *siglen,
  439. const unsigned char *tbs,
  440. size_t tbslen))
  441. {
  442. pmeth->sign_init = sign_init;
  443. pmeth->sign = sign;
  444. }
  445. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  446. int (*verify_init) (EVP_PKEY_CTX *ctx),
  447. int (*verify) (EVP_PKEY_CTX *ctx,
  448. const unsigned char *sig,
  449. size_t siglen,
  450. const unsigned char *tbs,
  451. size_t tbslen))
  452. {
  453. pmeth->verify_init = verify_init;
  454. pmeth->verify = verify;
  455. }
  456. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  457. int (*verify_recover_init) (EVP_PKEY_CTX
  458. *ctx),
  459. int (*verify_recover) (EVP_PKEY_CTX
  460. *ctx,
  461. unsigned char
  462. *sig,
  463. size_t *siglen,
  464. const unsigned
  465. char *tbs,
  466. size_t tbslen))
  467. {
  468. pmeth->verify_recover_init = verify_recover_init;
  469. pmeth->verify_recover = verify_recover;
  470. }
  471. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  472. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  473. EVP_MD_CTX *mctx),
  474. int (*signctx) (EVP_PKEY_CTX *ctx,
  475. unsigned char *sig,
  476. size_t *siglen,
  477. EVP_MD_CTX *mctx))
  478. {
  479. pmeth->signctx_init = signctx_init;
  480. pmeth->signctx = signctx;
  481. }
  482. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  483. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  484. EVP_MD_CTX *mctx),
  485. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  486. const unsigned char *sig,
  487. int siglen,
  488. EVP_MD_CTX *mctx))
  489. {
  490. pmeth->verifyctx_init = verifyctx_init;
  491. pmeth->verifyctx = verifyctx;
  492. }
  493. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  494. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  495. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  496. unsigned char *out,
  497. size_t *outlen,
  498. const unsigned char *in,
  499. size_t inlen))
  500. {
  501. pmeth->encrypt_init = encrypt_init;
  502. pmeth->encrypt = encryptfn;
  503. }
  504. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  505. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  506. int (*decrypt) (EVP_PKEY_CTX *ctx,
  507. unsigned char *out,
  508. size_t *outlen,
  509. const unsigned char *in,
  510. size_t inlen))
  511. {
  512. pmeth->decrypt_init = decrypt_init;
  513. pmeth->decrypt = decrypt;
  514. }
  515. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  516. int (*derive_init) (EVP_PKEY_CTX *ctx),
  517. int (*derive) (EVP_PKEY_CTX *ctx,
  518. unsigned char *key,
  519. size_t *keylen))
  520. {
  521. pmeth->derive_init = derive_init;
  522. pmeth->derive = derive;
  523. }
  524. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  525. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  526. void *p2),
  527. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  528. const char *type,
  529. const char *value))
  530. {
  531. pmeth->ctrl = ctrl;
  532. pmeth->ctrl_str = ctrl_str;
  533. }