pmeth_lib.c 26 KB

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