pmeth_lib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 "crypto/asn1.h"
  16. #include "crypto/evp.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_digestsign(EVP_PKEY_METHOD *pmeth,
  551. int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  552. const unsigned char *tbs, size_t tbslen))
  553. {
  554. pmeth->digestsign = digestsign;
  555. }
  556. void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
  557. int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  558. size_t siglen, const unsigned char *tbs,
  559. size_t tbslen))
  560. {
  561. pmeth->digestverify = digestverify;
  562. }
  563. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  564. int (*check) (EVP_PKEY *pkey))
  565. {
  566. pmeth->check = check;
  567. }
  568. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  569. int (*check) (EVP_PKEY *pkey))
  570. {
  571. pmeth->public_check = check;
  572. }
  573. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  574. int (*check) (EVP_PKEY *pkey))
  575. {
  576. pmeth->param_check = check;
  577. }
  578. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  579. int (*digest_custom) (EVP_PKEY_CTX *ctx,
  580. EVP_MD_CTX *mctx))
  581. {
  582. pmeth->digest_custom = digest_custom;
  583. }
  584. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  585. int (**pinit) (EVP_PKEY_CTX *ctx))
  586. {
  587. *pinit = pmeth->init;
  588. }
  589. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  590. int (**pcopy) (EVP_PKEY_CTX *dst,
  591. EVP_PKEY_CTX *src))
  592. {
  593. *pcopy = pmeth->copy;
  594. }
  595. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  596. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  597. {
  598. *pcleanup = pmeth->cleanup;
  599. }
  600. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  601. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  602. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  603. EVP_PKEY *pkey))
  604. {
  605. if (pparamgen_init)
  606. *pparamgen_init = pmeth->paramgen_init;
  607. if (pparamgen)
  608. *pparamgen = pmeth->paramgen;
  609. }
  610. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  611. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  612. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  613. EVP_PKEY *pkey))
  614. {
  615. if (pkeygen_init)
  616. *pkeygen_init = pmeth->keygen_init;
  617. if (pkeygen)
  618. *pkeygen = pmeth->keygen;
  619. }
  620. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  621. int (**psign_init) (EVP_PKEY_CTX *ctx),
  622. int (**psign) (EVP_PKEY_CTX *ctx,
  623. unsigned char *sig, size_t *siglen,
  624. const unsigned char *tbs,
  625. size_t tbslen))
  626. {
  627. if (psign_init)
  628. *psign_init = pmeth->sign_init;
  629. if (psign)
  630. *psign = pmeth->sign;
  631. }
  632. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  633. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  634. int (**pverify) (EVP_PKEY_CTX *ctx,
  635. const unsigned char *sig,
  636. size_t siglen,
  637. const unsigned char *tbs,
  638. size_t tbslen))
  639. {
  640. if (pverify_init)
  641. *pverify_init = pmeth->verify_init;
  642. if (pverify)
  643. *pverify = pmeth->verify;
  644. }
  645. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  646. int (**pverify_recover_init) (EVP_PKEY_CTX
  647. *ctx),
  648. int (**pverify_recover) (EVP_PKEY_CTX
  649. *ctx,
  650. unsigned char
  651. *sig,
  652. size_t *siglen,
  653. const unsigned
  654. char *tbs,
  655. size_t tbslen))
  656. {
  657. if (pverify_recover_init)
  658. *pverify_recover_init = pmeth->verify_recover_init;
  659. if (pverify_recover)
  660. *pverify_recover = pmeth->verify_recover;
  661. }
  662. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  663. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  664. EVP_MD_CTX *mctx),
  665. int (**psignctx) (EVP_PKEY_CTX *ctx,
  666. unsigned char *sig,
  667. size_t *siglen,
  668. EVP_MD_CTX *mctx))
  669. {
  670. if (psignctx_init)
  671. *psignctx_init = pmeth->signctx_init;
  672. if (psignctx)
  673. *psignctx = pmeth->signctx;
  674. }
  675. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  676. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  677. EVP_MD_CTX *mctx),
  678. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  679. const unsigned char *sig,
  680. int siglen,
  681. EVP_MD_CTX *mctx))
  682. {
  683. if (pverifyctx_init)
  684. *pverifyctx_init = pmeth->verifyctx_init;
  685. if (pverifyctx)
  686. *pverifyctx = pmeth->verifyctx;
  687. }
  688. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  689. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  690. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  691. unsigned char *out,
  692. size_t *outlen,
  693. const unsigned char *in,
  694. size_t inlen))
  695. {
  696. if (pencrypt_init)
  697. *pencrypt_init = pmeth->encrypt_init;
  698. if (pencryptfn)
  699. *pencryptfn = pmeth->encrypt;
  700. }
  701. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  702. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  703. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  704. unsigned char *out,
  705. size_t *outlen,
  706. const unsigned char *in,
  707. size_t inlen))
  708. {
  709. if (pdecrypt_init)
  710. *pdecrypt_init = pmeth->decrypt_init;
  711. if (pdecrypt)
  712. *pdecrypt = pmeth->decrypt;
  713. }
  714. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  715. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  716. int (**pderive) (EVP_PKEY_CTX *ctx,
  717. unsigned char *key,
  718. size_t *keylen))
  719. {
  720. if (pderive_init)
  721. *pderive_init = pmeth->derive_init;
  722. if (pderive)
  723. *pderive = pmeth->derive;
  724. }
  725. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  726. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  727. void *p2),
  728. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  729. const char *type,
  730. const char *value))
  731. {
  732. if (pctrl)
  733. *pctrl = pmeth->ctrl;
  734. if (pctrl_str)
  735. *pctrl_str = pmeth->ctrl_str;
  736. }
  737. void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
  738. int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
  739. const unsigned char *tbs, size_t tbslen))
  740. {
  741. if (digestsign)
  742. *digestsign = pmeth->digestsign;
  743. }
  744. void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
  745. int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
  746. size_t siglen, const unsigned char *tbs,
  747. size_t tbslen))
  748. {
  749. if (digestverify)
  750. *digestverify = pmeth->digestverify;
  751. }
  752. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  753. int (**pcheck) (EVP_PKEY *pkey))
  754. {
  755. if (pcheck != NULL)
  756. *pcheck = pmeth->check;
  757. }
  758. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  759. int (**pcheck) (EVP_PKEY *pkey))
  760. {
  761. if (pcheck != NULL)
  762. *pcheck = pmeth->public_check;
  763. }
  764. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  765. int (**pcheck) (EVP_PKEY *pkey))
  766. {
  767. if (pcheck != NULL)
  768. *pcheck = pmeth->param_check;
  769. }
  770. void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
  771. int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
  772. EVP_MD_CTX *mctx))
  773. {
  774. if (pdigest_custom != NULL)
  775. *pdigest_custom = pmeth->digest_custom;
  776. }