pmeth_lib.c 22 KB

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