pmeth_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/engine.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/x509v3.h>
  15. #include "internal/asn1_int.h"
  16. #include "internal/evp_int.h"
  17. #include "internal/numbers.h"
  18. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  19. static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
  20. static const EVP_PKEY_METHOD *standard_methods[] = {
  21. #ifndef OPENSSL_NO_RSA
  22. &rsa_pkey_meth,
  23. #endif
  24. #ifndef OPENSSL_NO_DH
  25. &dh_pkey_meth,
  26. #endif
  27. #ifndef OPENSSL_NO_DSA
  28. &dsa_pkey_meth,
  29. #endif
  30. #ifndef OPENSSL_NO_EC
  31. &ec_pkey_meth,
  32. #endif
  33. &hmac_pkey_meth,
  34. #ifndef OPENSSL_NO_CMAC
  35. &cmac_pkey_meth,
  36. #endif
  37. #ifndef OPENSSL_NO_RSA
  38. &rsa_pss_pkey_meth,
  39. #endif
  40. #ifndef OPENSSL_NO_DH
  41. &dhx_pkey_meth,
  42. #endif
  43. &tls1_prf_pkey_meth,
  44. #ifndef OPENSSL_NO_EC
  45. &ecx25519_pkey_meth,
  46. #endif
  47. &hkdf_pkey_meth,
  48. #ifndef OPENSSL_NO_POLY1305
  49. &poly1305_pkey_meth,
  50. #endif
  51. #ifndef OPENSSL_NO_SIPHASH
  52. &siphash_pkey_meth,
  53. #endif
  54. };
  55. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  56. pmeth);
  57. static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
  58. const EVP_PKEY_METHOD *const *b)
  59. {
  60. return ((*a)->pkey_id - (*b)->pkey_id);
  61. }
  62. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  63. pmeth);
  64. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  65. {
  66. EVP_PKEY_METHOD tmp;
  67. const EVP_PKEY_METHOD *t = &tmp, **ret;
  68. tmp.pkey_id = type;
  69. if (app_pkey_methods) {
  70. int idx;
  71. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  72. if (idx >= 0)
  73. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  74. }
  75. ret = OBJ_bsearch_pmeth(&t, standard_methods,
  76. sizeof(standard_methods) /
  77. sizeof(EVP_PKEY_METHOD *));
  78. if (!ret || !*ret)
  79. return NULL;
  80. return *ret;
  81. }
  82. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
  83. {
  84. EVP_PKEY_CTX *ret;
  85. const EVP_PKEY_METHOD *pmeth;
  86. if (id == -1) {
  87. if (!pkey || !pkey->ameth)
  88. return NULL;
  89. id = pkey->ameth->pkey_id;
  90. }
  91. #ifndef OPENSSL_NO_ENGINE
  92. if (pkey && pkey->engine)
  93. e = pkey->engine;
  94. /* Try to find an ENGINE which implements this method */
  95. if (e) {
  96. if (!ENGINE_init(e)) {
  97. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
  98. return NULL;
  99. }
  100. } else
  101. e = ENGINE_get_pkey_meth_engine(id);
  102. /*
  103. * If an ENGINE handled this method look it up. Otherwise use internal
  104. * tables.
  105. */
  106. if (e)
  107. pmeth = ENGINE_get_pkey_meth(e, id);
  108. else
  109. #endif
  110. pmeth = EVP_PKEY_meth_find(id);
  111. if (pmeth == NULL) {
  112. EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
  113. return NULL;
  114. }
  115. ret = OPENSSL_zalloc(sizeof(*ret));
  116. if (ret == NULL) {
  117. #ifndef OPENSSL_NO_ENGINE
  118. ENGINE_finish(e);
  119. #endif
  120. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
  121. return NULL;
  122. }
  123. ret->engine = e;
  124. ret->pmeth = pmeth;
  125. ret->operation = EVP_PKEY_OP_UNDEFINED;
  126. ret->pkey = pkey;
  127. if (pkey)
  128. EVP_PKEY_up_ref(pkey);
  129. if (pmeth->init) {
  130. if (pmeth->init(ret) <= 0) {
  131. ret->pmeth = NULL;
  132. EVP_PKEY_CTX_free(ret);
  133. return NULL;
  134. }
  135. }
  136. return ret;
  137. }
  138. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  139. {
  140. EVP_PKEY_METHOD *pmeth;
  141. pmeth = OPENSSL_zalloc(sizeof(*pmeth));
  142. if (pmeth == NULL)
  143. return NULL;
  144. pmeth->pkey_id = id;
  145. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  146. return pmeth;
  147. }
  148. void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
  149. const EVP_PKEY_METHOD *meth)
  150. {
  151. if (ppkey_id)
  152. *ppkey_id = meth->pkey_id;
  153. if (pflags)
  154. *pflags = meth->flags;
  155. }
  156. void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
  157. {
  158. dst->init = src->init;
  159. dst->copy = src->copy;
  160. dst->cleanup = src->cleanup;
  161. dst->paramgen_init = src->paramgen_init;
  162. dst->paramgen = src->paramgen;
  163. dst->keygen_init = src->keygen_init;
  164. dst->keygen = src->keygen;
  165. dst->sign_init = src->sign_init;
  166. dst->sign = src->sign;
  167. dst->verify_init = src->verify_init;
  168. dst->verify = src->verify;
  169. dst->verify_recover_init = src->verify_recover_init;
  170. dst->verify_recover = src->verify_recover;
  171. dst->signctx_init = src->signctx_init;
  172. dst->signctx = src->signctx;
  173. dst->verifyctx_init = src->verifyctx_init;
  174. dst->verifyctx = src->verifyctx;
  175. dst->encrypt_init = src->encrypt_init;
  176. dst->encrypt = src->encrypt;
  177. dst->decrypt_init = src->decrypt_init;
  178. dst->decrypt = src->decrypt;
  179. dst->derive_init = src->derive_init;
  180. dst->derive = src->derive;
  181. dst->ctrl = src->ctrl;
  182. dst->ctrl_str = src->ctrl_str;
  183. }
  184. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  185. {
  186. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  187. OPENSSL_free(pmeth);
  188. }
  189. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  190. {
  191. return int_ctx_new(pkey, e, -1);
  192. }
  193. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  194. {
  195. return int_ctx_new(NULL, e, id);
  196. }
  197. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
  198. {
  199. EVP_PKEY_CTX *rctx;
  200. if (!pctx->pmeth || !pctx->pmeth->copy)
  201. return NULL;
  202. #ifndef OPENSSL_NO_ENGINE
  203. /* Make sure it's safe to copy a pkey context using an ENGINE */
  204. if (pctx->engine && !ENGINE_init(pctx->engine)) {
  205. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
  206. return 0;
  207. }
  208. #endif
  209. rctx = OPENSSL_malloc(sizeof(*rctx));
  210. if (rctx == NULL)
  211. return NULL;
  212. rctx->pmeth = pctx->pmeth;
  213. #ifndef OPENSSL_NO_ENGINE
  214. rctx->engine = pctx->engine;
  215. #endif
  216. if (pctx->pkey)
  217. EVP_PKEY_up_ref(pctx->pkey);
  218. rctx->pkey = pctx->pkey;
  219. if (pctx->peerkey)
  220. EVP_PKEY_up_ref(pctx->peerkey);
  221. rctx->peerkey = pctx->peerkey;
  222. rctx->data = NULL;
  223. rctx->app_data = NULL;
  224. rctx->operation = pctx->operation;
  225. if (pctx->pmeth->copy(rctx, pctx) > 0)
  226. return rctx;
  227. rctx->pmeth = NULL;
  228. EVP_PKEY_CTX_free(rctx);
  229. return NULL;
  230. }
  231. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  232. {
  233. if (app_pkey_methods == NULL) {
  234. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  235. if (app_pkey_methods == NULL)
  236. return 0;
  237. }
  238. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  239. return 0;
  240. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  241. return 1;
  242. }
  243. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  244. {
  245. if (ctx == NULL)
  246. return;
  247. if (ctx->pmeth && ctx->pmeth->cleanup)
  248. ctx->pmeth->cleanup(ctx);
  249. EVP_PKEY_free(ctx->pkey);
  250. EVP_PKEY_free(ctx->peerkey);
  251. #ifndef OPENSSL_NO_ENGINE
  252. ENGINE_finish(ctx->engine);
  253. #endif
  254. OPENSSL_free(ctx);
  255. }
  256. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  257. int cmd, int p1, void *p2)
  258. {
  259. int ret;
  260. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl) {
  261. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  262. return -2;
  263. }
  264. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  265. return -1;
  266. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  267. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  268. return -1;
  269. }
  270. if ((optype != -1) && !(ctx->operation & optype)) {
  271. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  272. return -1;
  273. }
  274. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  275. if (ret == -2)
  276. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  277. return ret;
  278. }
  279. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  280. const char *name, const char *value)
  281. {
  282. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  283. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  284. return -2;
  285. }
  286. if (strcmp(name, "digest") == 0)
  287. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
  288. value);
  289. return ctx->pmeth->ctrl_str(ctx, name, value);
  290. }
  291. /* Utility functions to send a string of hex string to a ctrl */
  292. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  293. {
  294. size_t len;
  295. len = strlen(str);
  296. if (len > INT_MAX)
  297. return -1;
  298. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  299. }
  300. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  301. {
  302. unsigned char *bin;
  303. long binlen;
  304. int rv = -1;
  305. bin = OPENSSL_hexstr2buf(hex, &binlen);
  306. if (bin == NULL)
  307. return 0;
  308. if (binlen <= INT_MAX)
  309. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  310. OPENSSL_free(bin);
  311. return rv;
  312. }
  313. /* Pass a message digest to a ctrl */
  314. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  315. {
  316. const EVP_MD *m;
  317. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  318. EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
  319. return 0;
  320. }
  321. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  322. }
  323. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  324. {
  325. return ctx->operation;
  326. }
  327. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  328. {
  329. ctx->keygen_info = dat;
  330. ctx->keygen_info_count = datlen;
  331. }
  332. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  333. {
  334. ctx->data = data;
  335. }
  336. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  337. {
  338. return ctx->data;
  339. }
  340. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  341. {
  342. return ctx->pkey;
  343. }
  344. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  345. {
  346. return ctx->peerkey;
  347. }
  348. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  349. {
  350. ctx->app_data = data;
  351. }
  352. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  353. {
  354. return ctx->app_data;
  355. }
  356. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  357. int (*init) (EVP_PKEY_CTX *ctx))
  358. {
  359. pmeth->init = init;
  360. }
  361. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  362. int (*copy) (EVP_PKEY_CTX *dst,
  363. EVP_PKEY_CTX *src))
  364. {
  365. pmeth->copy = copy;
  366. }
  367. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  368. void (*cleanup) (EVP_PKEY_CTX *ctx))
  369. {
  370. pmeth->cleanup = cleanup;
  371. }
  372. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  373. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  374. int (*paramgen) (EVP_PKEY_CTX *ctx,
  375. EVP_PKEY *pkey))
  376. {
  377. pmeth->paramgen_init = paramgen_init;
  378. pmeth->paramgen = paramgen;
  379. }
  380. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  381. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  382. int (*keygen) (EVP_PKEY_CTX *ctx,
  383. EVP_PKEY *pkey))
  384. {
  385. pmeth->keygen_init = keygen_init;
  386. pmeth->keygen = keygen;
  387. }
  388. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  389. int (*sign_init) (EVP_PKEY_CTX *ctx),
  390. int (*sign) (EVP_PKEY_CTX *ctx,
  391. unsigned char *sig, size_t *siglen,
  392. const unsigned char *tbs,
  393. size_t tbslen))
  394. {
  395. pmeth->sign_init = sign_init;
  396. pmeth->sign = sign;
  397. }
  398. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  399. int (*verify_init) (EVP_PKEY_CTX *ctx),
  400. int (*verify) (EVP_PKEY_CTX *ctx,
  401. const unsigned char *sig,
  402. size_t siglen,
  403. const unsigned char *tbs,
  404. size_t tbslen))
  405. {
  406. pmeth->verify_init = verify_init;
  407. pmeth->verify = verify;
  408. }
  409. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  410. int (*verify_recover_init) (EVP_PKEY_CTX
  411. *ctx),
  412. int (*verify_recover) (EVP_PKEY_CTX
  413. *ctx,
  414. unsigned char
  415. *sig,
  416. size_t *siglen,
  417. const unsigned
  418. char *tbs,
  419. size_t tbslen))
  420. {
  421. pmeth->verify_recover_init = verify_recover_init;
  422. pmeth->verify_recover = verify_recover;
  423. }
  424. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  425. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  426. EVP_MD_CTX *mctx),
  427. int (*signctx) (EVP_PKEY_CTX *ctx,
  428. unsigned char *sig,
  429. size_t *siglen,
  430. EVP_MD_CTX *mctx))
  431. {
  432. pmeth->signctx_init = signctx_init;
  433. pmeth->signctx = signctx;
  434. }
  435. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  436. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  437. EVP_MD_CTX *mctx),
  438. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  439. const unsigned char *sig,
  440. int siglen,
  441. EVP_MD_CTX *mctx))
  442. {
  443. pmeth->verifyctx_init = verifyctx_init;
  444. pmeth->verifyctx = verifyctx;
  445. }
  446. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  447. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  448. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  449. unsigned char *out,
  450. size_t *outlen,
  451. const unsigned char *in,
  452. size_t inlen))
  453. {
  454. pmeth->encrypt_init = encrypt_init;
  455. pmeth->encrypt = encryptfn;
  456. }
  457. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  458. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  459. int (*decrypt) (EVP_PKEY_CTX *ctx,
  460. unsigned char *out,
  461. size_t *outlen,
  462. const unsigned char *in,
  463. size_t inlen))
  464. {
  465. pmeth->decrypt_init = decrypt_init;
  466. pmeth->decrypt = decrypt;
  467. }
  468. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  469. int (*derive_init) (EVP_PKEY_CTX *ctx),
  470. int (*derive) (EVP_PKEY_CTX *ctx,
  471. unsigned char *key,
  472. size_t *keylen))
  473. {
  474. pmeth->derive_init = derive_init;
  475. pmeth->derive = derive;
  476. }
  477. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  478. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  479. void *p2),
  480. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  481. const char *type,
  482. const char *value))
  483. {
  484. pmeth->ctrl = ctrl;
  485. pmeth->ctrl_str = ctrl_str;
  486. }
  487. void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD *pmeth,
  488. int (**pinit) (EVP_PKEY_CTX *ctx))
  489. {
  490. *pinit = pmeth->init;
  491. }
  492. void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD *pmeth,
  493. int (**pcopy) (EVP_PKEY_CTX *dst,
  494. EVP_PKEY_CTX *src))
  495. {
  496. *pcopy = pmeth->copy;
  497. }
  498. void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD *pmeth,
  499. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  500. {
  501. *pcleanup = pmeth->cleanup;
  502. }
  503. void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD *pmeth,
  504. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  505. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  506. EVP_PKEY *pkey))
  507. {
  508. if (pparamgen_init)
  509. *pparamgen_init = pmeth->paramgen_init;
  510. if (pparamgen)
  511. *pparamgen = pmeth->paramgen;
  512. }
  513. void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD *pmeth,
  514. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  515. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  516. EVP_PKEY *pkey))
  517. {
  518. if (pkeygen_init)
  519. *pkeygen_init = pmeth->keygen_init;
  520. if (pkeygen)
  521. *pkeygen = pmeth->keygen;
  522. }
  523. void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD *pmeth,
  524. int (**psign_init) (EVP_PKEY_CTX *ctx),
  525. int (**psign) (EVP_PKEY_CTX *ctx,
  526. unsigned char *sig, size_t *siglen,
  527. const unsigned char *tbs,
  528. size_t tbslen))
  529. {
  530. if (psign_init)
  531. *psign_init = pmeth->sign_init;
  532. if (psign)
  533. *psign = pmeth->sign;
  534. }
  535. void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD *pmeth,
  536. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  537. int (**pverify) (EVP_PKEY_CTX *ctx,
  538. const unsigned char *sig,
  539. size_t siglen,
  540. const unsigned char *tbs,
  541. size_t tbslen))
  542. {
  543. if (pverify_init)
  544. *pverify_init = pmeth->verify_init;
  545. if (pverify)
  546. *pverify = pmeth->verify;
  547. }
  548. void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD *pmeth,
  549. int (**pverify_recover_init) (EVP_PKEY_CTX
  550. *ctx),
  551. int (**pverify_recover) (EVP_PKEY_CTX
  552. *ctx,
  553. unsigned char
  554. *sig,
  555. size_t *siglen,
  556. const unsigned
  557. char *tbs,
  558. size_t tbslen))
  559. {
  560. if (pverify_recover_init)
  561. *pverify_recover_init = pmeth->verify_recover_init;
  562. if (pverify_recover)
  563. *pverify_recover = pmeth->verify_recover;
  564. }
  565. void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD *pmeth,
  566. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  567. EVP_MD_CTX *mctx),
  568. int (**psignctx) (EVP_PKEY_CTX *ctx,
  569. unsigned char *sig,
  570. size_t *siglen,
  571. EVP_MD_CTX *mctx))
  572. {
  573. if (psignctx_init)
  574. *psignctx_init = pmeth->signctx_init;
  575. if (psignctx)
  576. *psignctx = pmeth->signctx;
  577. }
  578. void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD *pmeth,
  579. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  580. EVP_MD_CTX *mctx),
  581. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  582. const unsigned char *sig,
  583. int siglen,
  584. EVP_MD_CTX *mctx))
  585. {
  586. if (pverifyctx_init)
  587. *pverifyctx_init = pmeth->verifyctx_init;
  588. if (pverifyctx)
  589. *pverifyctx = pmeth->verifyctx;
  590. }
  591. void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD *pmeth,
  592. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  593. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  594. unsigned char *out,
  595. size_t *outlen,
  596. const unsigned char *in,
  597. size_t inlen))
  598. {
  599. if (pencrypt_init)
  600. *pencrypt_init = pmeth->encrypt_init;
  601. if (pencryptfn)
  602. *pencryptfn = pmeth->encrypt;
  603. }
  604. void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD *pmeth,
  605. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  606. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  607. unsigned char *out,
  608. size_t *outlen,
  609. const unsigned char *in,
  610. size_t inlen))
  611. {
  612. if (pdecrypt_init)
  613. *pdecrypt_init = pmeth->decrypt_init;
  614. if (pdecrypt)
  615. *pdecrypt = pmeth->decrypt;
  616. }
  617. void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD *pmeth,
  618. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  619. int (**pderive) (EVP_PKEY_CTX *ctx,
  620. unsigned char *key,
  621. size_t *keylen))
  622. {
  623. if (pderive_init)
  624. *pderive_init = pmeth->derive_init;
  625. if (pderive)
  626. *pderive = pmeth->derive;
  627. }
  628. void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD *pmeth,
  629. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  630. void *p2),
  631. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  632. const char *type,
  633. const char *value))
  634. {
  635. if (pctrl)
  636. *pctrl = pmeth->ctrl;
  637. if (pctrl_str)
  638. *pctrl_str = pmeth->ctrl_str;
  639. }