pmeth_lib.c 25 KB

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