pmeth_lib.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <openssl/engine.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/x509v3.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/dh.h>
  16. #include "internal/cryptlib.h"
  17. #include "crypto/asn1.h"
  18. #include "crypto/evp.h"
  19. #include "internal/numbers.h"
  20. #include "internal/provider.h"
  21. #include "evp_local.h"
  22. typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void);
  23. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  24. static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
  25. /* This array needs to be in order of NIDs */
  26. static pmeth_fn standard_methods[] = {
  27. #ifndef OPENSSL_NO_RSA
  28. rsa_pkey_method,
  29. #endif
  30. #ifndef OPENSSL_NO_DH
  31. dh_pkey_method,
  32. #endif
  33. #ifndef OPENSSL_NO_DSA
  34. dsa_pkey_method,
  35. #endif
  36. #ifndef OPENSSL_NO_EC
  37. ec_pkey_method,
  38. #endif
  39. hmac_pkey_method,
  40. #ifndef OPENSSL_NO_CMAC
  41. cmac_pkey_method,
  42. #endif
  43. #ifndef OPENSSL_NO_RSA
  44. rsa_pss_pkey_method,
  45. #endif
  46. #ifndef OPENSSL_NO_DH
  47. dhx_pkey_method,
  48. #endif
  49. #ifndef OPENSSL_NO_SCRYPT
  50. scrypt_pkey_method,
  51. #endif
  52. tls1_prf_pkey_method,
  53. #ifndef OPENSSL_NO_EC
  54. ecx25519_pkey_method,
  55. ecx448_pkey_method,
  56. #endif
  57. hkdf_pkey_method,
  58. #ifndef OPENSSL_NO_POLY1305
  59. poly1305_pkey_method,
  60. #endif
  61. #ifndef OPENSSL_NO_SIPHASH
  62. siphash_pkey_method,
  63. #endif
  64. #ifndef OPENSSL_NO_EC
  65. ed25519_pkey_method,
  66. ed448_pkey_method,
  67. #endif
  68. #ifndef OPENSSL_NO_SM2
  69. sm2_pkey_method,
  70. #endif
  71. };
  72. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
  73. static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b)
  74. {
  75. return ((*a)->pkey_id - ((**b)())->pkey_id);
  76. }
  77. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func);
  78. static int pmeth_cmp(const EVP_PKEY_METHOD *const *a,
  79. const EVP_PKEY_METHOD *const *b)
  80. {
  81. return ((*a)->pkey_id - (*b)->pkey_id);
  82. }
  83. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  84. {
  85. pmeth_fn *ret;
  86. EVP_PKEY_METHOD tmp;
  87. const EVP_PKEY_METHOD *t = &tmp;
  88. tmp.pkey_id = type;
  89. if (app_pkey_methods) {
  90. int idx;
  91. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  92. if (idx >= 0)
  93. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  94. }
  95. ret = OBJ_bsearch_pmeth_func(&t, standard_methods,
  96. sizeof(standard_methods) /
  97. sizeof(pmeth_fn));
  98. if (ret == NULL || *ret == NULL)
  99. return NULL;
  100. return (**ret)();
  101. }
  102. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e,
  103. const char *name, const char *propquery,
  104. int id)
  105. {
  106. EVP_PKEY_CTX *ret;
  107. const EVP_PKEY_METHOD *pmeth = NULL;
  108. /*
  109. * When using providers, the context is bound to the algo implementation
  110. * later.
  111. */
  112. if (pkey == NULL && e == NULL && id == -1)
  113. goto common;
  114. /* TODO(3.0) Legacy code should be removed when all is provider based */
  115. /* BEGIN legacy */
  116. if (id == -1) {
  117. if (pkey == NULL)
  118. return 0;
  119. id = pkey->type;
  120. }
  121. name = OBJ_nid2sn(id);
  122. propquery = NULL;
  123. #ifndef OPENSSL_NO_ENGINE
  124. if (e == NULL && pkey != NULL)
  125. e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine;
  126. /* Try to find an ENGINE which implements this method */
  127. if (e) {
  128. if (!ENGINE_init(e)) {
  129. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB);
  130. return NULL;
  131. }
  132. } else {
  133. e = ENGINE_get_pkey_meth_engine(id);
  134. }
  135. /*
  136. * If an ENGINE handled this method look it up. Otherwise use internal
  137. * tables.
  138. */
  139. if (e)
  140. pmeth = ENGINE_get_pkey_meth(e, id);
  141. else
  142. #endif
  143. pmeth = EVP_PKEY_meth_find(id);
  144. if (pmeth == NULL) {
  145. #ifndef OPENSSL_NO_ENGINE
  146. ENGINE_finish(e);
  147. #endif
  148. EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM);
  149. return NULL;
  150. }
  151. /* END legacy */
  152. common:
  153. ret = OPENSSL_zalloc(sizeof(*ret));
  154. if (ret == NULL) {
  155. #ifndef OPENSSL_NO_ENGINE
  156. ENGINE_finish(e);
  157. #endif
  158. EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE);
  159. return NULL;
  160. }
  161. ret->algorithm = name;
  162. ret->propquery = propquery;
  163. ret->engine = e;
  164. ret->pmeth = pmeth;
  165. ret->operation = EVP_PKEY_OP_UNDEFINED;
  166. ret->pkey = pkey;
  167. if (pkey != NULL)
  168. EVP_PKEY_up_ref(pkey);
  169. if (pmeth != NULL && pmeth->init != NULL) {
  170. if (pmeth->init(ret) <= 0) {
  171. ret->pmeth = NULL;
  172. EVP_PKEY_CTX_free(ret);
  173. return NULL;
  174. }
  175. }
  176. return ret;
  177. }
  178. void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx)
  179. {
  180. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  181. if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL)
  182. ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx);
  183. EVP_KEYEXCH_free(ctx->op.kex.exchange);
  184. } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  185. if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL)
  186. ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx);
  187. EVP_SIGNATURE_free(ctx->op.sig.signature);
  188. }
  189. }
  190. EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags)
  191. {
  192. EVP_PKEY_METHOD *pmeth;
  193. pmeth = OPENSSL_zalloc(sizeof(*pmeth));
  194. if (pmeth == NULL) {
  195. EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE);
  196. return NULL;
  197. }
  198. pmeth->pkey_id = id;
  199. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  200. return pmeth;
  201. }
  202. void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags,
  203. const EVP_PKEY_METHOD *meth)
  204. {
  205. if (ppkey_id)
  206. *ppkey_id = meth->pkey_id;
  207. if (pflags)
  208. *pflags = meth->flags;
  209. }
  210. void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src)
  211. {
  212. dst->init = src->init;
  213. dst->copy = src->copy;
  214. dst->cleanup = src->cleanup;
  215. dst->paramgen_init = src->paramgen_init;
  216. dst->paramgen = src->paramgen;
  217. dst->keygen_init = src->keygen_init;
  218. dst->keygen = src->keygen;
  219. dst->sign_init = src->sign_init;
  220. dst->sign = src->sign;
  221. dst->verify_init = src->verify_init;
  222. dst->verify = src->verify;
  223. dst->verify_recover_init = src->verify_recover_init;
  224. dst->verify_recover = src->verify_recover;
  225. dst->signctx_init = src->signctx_init;
  226. dst->signctx = src->signctx;
  227. dst->verifyctx_init = src->verifyctx_init;
  228. dst->verifyctx = src->verifyctx;
  229. dst->encrypt_init = src->encrypt_init;
  230. dst->encrypt = src->encrypt;
  231. dst->decrypt_init = src->decrypt_init;
  232. dst->decrypt = src->decrypt;
  233. dst->derive_init = src->derive_init;
  234. dst->derive = src->derive;
  235. dst->ctrl = src->ctrl;
  236. dst->ctrl_str = src->ctrl_str;
  237. dst->check = src->check;
  238. }
  239. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  240. {
  241. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  242. OPENSSL_free(pmeth);
  243. }
  244. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  245. {
  246. return int_ctx_new(pkey, e, NULL, NULL, -1);
  247. }
  248. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  249. {
  250. return int_ctx_new(NULL, e, NULL, NULL, id);
  251. }
  252. EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(const char *name,
  253. const char *propquery)
  254. {
  255. return int_ctx_new(NULL, NULL, name, propquery, -1);
  256. }
  257. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
  258. {
  259. EVP_PKEY_CTX *rctx;
  260. if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
  261. && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
  262. && pctx->op.kex.exchprovctx == NULL)
  263. || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
  264. && pctx->op.sig.sigprovctx == NULL)))
  265. return NULL;
  266. #ifndef OPENSSL_NO_ENGINE
  267. /* Make sure it's safe to copy a pkey context using an ENGINE */
  268. if (pctx->engine && !ENGINE_init(pctx->engine)) {
  269. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
  270. return 0;
  271. }
  272. #endif
  273. rctx = OPENSSL_zalloc(sizeof(*rctx));
  274. if (rctx == NULL) {
  275. EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
  276. return NULL;
  277. }
  278. if (pctx->pkey != NULL)
  279. EVP_PKEY_up_ref(pctx->pkey);
  280. rctx->pkey = pctx->pkey;
  281. rctx->operation = pctx->operation;
  282. rctx->algorithm = pctx->algorithm;
  283. rctx->propquery = pctx->propquery;
  284. if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
  285. if (pctx->op.kex.exchange != NULL) {
  286. rctx->op.kex.exchange = pctx->op.kex.exchange;
  287. if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
  288. OPENSSL_free(rctx);
  289. return NULL;
  290. }
  291. }
  292. if (pctx->op.kex.exchprovctx != NULL) {
  293. if (!ossl_assert(pctx->op.kex.exchange != NULL))
  294. return NULL;
  295. rctx->op.kex.exchprovctx
  296. = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
  297. if (rctx->op.kex.exchprovctx == NULL) {
  298. EVP_KEYEXCH_free(rctx->op.kex.exchange);
  299. OPENSSL_free(rctx);
  300. return NULL;
  301. }
  302. return rctx;
  303. }
  304. } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
  305. if (pctx->op.sig.signature != NULL) {
  306. rctx->op.sig.signature = pctx->op.sig.signature;
  307. if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
  308. OPENSSL_free(rctx);
  309. return NULL;
  310. }
  311. }
  312. if (pctx->op.sig.sigprovctx != NULL) {
  313. if (!ossl_assert(pctx->op.sig.signature != NULL))
  314. return NULL;
  315. rctx->op.sig.sigprovctx
  316. = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
  317. if (rctx->op.sig.sigprovctx == NULL) {
  318. EVP_SIGNATURE_free(rctx->op.sig.signature);
  319. OPENSSL_free(rctx);
  320. return NULL;
  321. }
  322. return rctx;
  323. }
  324. }
  325. rctx->pmeth = pctx->pmeth;
  326. #ifndef OPENSSL_NO_ENGINE
  327. rctx->engine = pctx->engine;
  328. #endif
  329. if (pctx->peerkey)
  330. EVP_PKEY_up_ref(pctx->peerkey);
  331. rctx->peerkey = pctx->peerkey;
  332. if (pctx->pmeth->copy(rctx, pctx) > 0)
  333. return rctx;
  334. rctx->pmeth = NULL;
  335. EVP_PKEY_CTX_free(rctx);
  336. return NULL;
  337. }
  338. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  339. {
  340. if (app_pkey_methods == NULL) {
  341. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  342. if (app_pkey_methods == NULL){
  343. EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
  344. return 0;
  345. }
  346. }
  347. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
  348. EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
  349. return 0;
  350. }
  351. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  352. return 1;
  353. }
  354. void evp_app_cleanup_int(void)
  355. {
  356. if (app_pkey_methods != NULL)
  357. sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
  358. }
  359. int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
  360. {
  361. const EVP_PKEY_METHOD *ret;
  362. ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
  363. return ret == NULL ? 0 : 1;
  364. }
  365. size_t EVP_PKEY_meth_get_count(void)
  366. {
  367. size_t rv = OSSL_NELEM(standard_methods);
  368. if (app_pkey_methods)
  369. rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
  370. return rv;
  371. }
  372. const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
  373. {
  374. if (idx < OSSL_NELEM(standard_methods))
  375. return (standard_methods[idx])();
  376. if (app_pkey_methods == NULL)
  377. return NULL;
  378. idx -= OSSL_NELEM(standard_methods);
  379. if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
  380. return NULL;
  381. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  382. }
  383. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  384. {
  385. if (ctx == NULL)
  386. return;
  387. if (ctx->pmeth && ctx->pmeth->cleanup)
  388. ctx->pmeth->cleanup(ctx);
  389. evp_pkey_ctx_free_old_ops(ctx);
  390. EVP_PKEY_free(ctx->pkey);
  391. EVP_PKEY_free(ctx->peerkey);
  392. #ifndef OPENSSL_NO_ENGINE
  393. ENGINE_finish(ctx->engine);
  394. #endif
  395. OPENSSL_free(ctx);
  396. }
  397. int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  398. {
  399. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  400. && ctx->op.sig.sigprovctx != NULL
  401. && ctx->op.sig.signature != NULL
  402. && ctx->op.sig.signature->get_ctx_params != NULL)
  403. return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx,
  404. params);
  405. return 0;
  406. }
  407. const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx)
  408. {
  409. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  410. && ctx->op.sig.signature != NULL
  411. && ctx->op.sig.signature->gettable_ctx_params != NULL)
  412. return ctx->op.sig.signature->gettable_ctx_params();
  413. return NULL;
  414. }
  415. int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  416. {
  417. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  418. && ctx->op.kex.exchprovctx != NULL
  419. && ctx->op.kex.exchange != NULL
  420. && ctx->op.kex.exchange->set_ctx_params != NULL)
  421. return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
  422. params);
  423. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  424. && ctx->op.sig.sigprovctx != NULL
  425. && ctx->op.sig.signature != NULL
  426. && ctx->op.sig.signature->set_ctx_params != NULL)
  427. return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
  428. params);
  429. return 0;
  430. }
  431. const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
  432. {
  433. if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  434. && ctx->op.kex.exchange != NULL
  435. && ctx->op.kex.exchange->settable_ctx_params != NULL)
  436. return ctx->op.kex.exchange->settable_ctx_params();
  437. if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  438. && ctx->op.sig.signature != NULL
  439. && ctx->op.sig.signature->settable_ctx_params != NULL)
  440. return ctx->op.sig.signature->settable_ctx_params();
  441. return NULL;
  442. }
  443. #ifndef OPENSSL_NO_DH
  444. int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
  445. {
  446. OSSL_PARAM dh_pad_params[2];
  447. unsigned int upad = pad;
  448. /* We use EVP_PKEY_CTX_ctrl return values */
  449. if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
  450. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  451. return -2;
  452. }
  453. /* TODO(3.0): Remove this eventually when no more legacy */
  454. if (ctx->op.kex.exchprovctx == NULL)
  455. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
  456. EVP_PKEY_CTRL_DH_PAD, pad, NULL);
  457. dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
  458. dh_pad_params[1] = OSSL_PARAM_construct_end();
  459. return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
  460. }
  461. #endif
  462. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  463. {
  464. OSSL_PARAM sig_md_params[3], *p = sig_md_params;
  465. /* 80 should be big enough */
  466. char name[80] = "";
  467. const EVP_MD *tmp;
  468. if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  469. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  470. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  471. return -2;
  472. }
  473. /* TODO(3.0): Remove this eventually when no more legacy */
  474. if (ctx->op.sig.sigprovctx == NULL)
  475. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
  476. EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
  477. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  478. name,
  479. sizeof(name));
  480. *p++ = OSSL_PARAM_construct_end();
  481. if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
  482. return 0;
  483. tmp = EVP_get_digestbyname(name);
  484. if (tmp == NULL)
  485. return 0;
  486. *md = tmp;
  487. return 1;
  488. }
  489. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  490. {
  491. OSSL_PARAM sig_md_params[3], *p = sig_md_params;
  492. size_t mdsize;
  493. const char *name;
  494. if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
  495. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  496. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  497. return -2;
  498. }
  499. /* TODO(3.0): Remove this eventually when no more legacy */
  500. if (ctx->op.sig.sigprovctx == NULL)
  501. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
  502. EVP_PKEY_CTRL_MD, 0, (void *)(md));
  503. if (md == NULL) {
  504. name = "";
  505. mdsize = 0;
  506. } else {
  507. mdsize = EVP_MD_size(md);
  508. name = EVP_MD_name(md);
  509. }
  510. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
  511. /*
  512. * Cast away the const. This is read
  513. * only so should be safe
  514. */
  515. (char *)name,
  516. strlen(name) + 1);
  517. *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE,
  518. &mdsize);
  519. *p++ = OSSL_PARAM_construct_end();
  520. return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
  521. }
  522. static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
  523. int cmd, int p1, void *p2)
  524. {
  525. switch (cmd) {
  526. #ifndef OPENSSL_NO_DH
  527. case EVP_PKEY_CTRL_DH_PAD:
  528. return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
  529. #endif
  530. case EVP_PKEY_CTRL_MD:
  531. return EVP_PKEY_CTX_set_signature_md(ctx, p2);
  532. case EVP_PKEY_CTRL_GET_MD:
  533. return EVP_PKEY_CTX_get_signature_md(ctx, p2);
  534. }
  535. return 0;
  536. }
  537. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  538. int cmd, int p1, void *p2)
  539. {
  540. int ret;
  541. if (ctx == NULL) {
  542. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  543. return -2;
  544. }
  545. if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
  546. || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
  547. && ctx->op.sig.sigprovctx != NULL))
  548. return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
  549. if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
  550. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  551. return -2;
  552. }
  553. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  554. return -1;
  555. /* Skip the operation checks since this is called in a very early stage */
  556. if (ctx->pmeth->digest_custom != NULL)
  557. goto doit;
  558. if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
  559. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  560. return -1;
  561. }
  562. if ((optype != -1) && !(ctx->operation & optype)) {
  563. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  564. return -1;
  565. }
  566. doit:
  567. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  568. if (ret == -2)
  569. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  570. return ret;
  571. }
  572. int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
  573. int cmd, uint64_t value)
  574. {
  575. return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
  576. }
  577. static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
  578. const char *value)
  579. {
  580. #ifndef OPENSSL_NO_DH
  581. if (strcmp(name, "dh_pad") == 0) {
  582. int pad;
  583. pad = atoi(value);
  584. return EVP_PKEY_CTX_set_dh_pad(ctx, pad);
  585. }
  586. #endif
  587. if (strcmp(name, "digest") == 0) {
  588. int ret;
  589. EVP_MD *md;
  590. if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL)
  591. return 0;
  592. md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov),
  593. value, NULL);
  594. if (md == NULL)
  595. return 0;
  596. ret = EVP_PKEY_CTX_set_signature_md(ctx, md);
  597. EVP_MD_meth_free(md);
  598. return ret;
  599. }
  600. return 0;
  601. }
  602. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  603. const char *name, const char *value)
  604. {
  605. if (ctx == NULL) {
  606. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  607. return -2;
  608. }
  609. if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
  610. || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  611. && ctx->op.sig.sigprovctx != NULL))
  612. return legacy_ctrl_str_to_param(ctx, name, value);
  613. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
  614. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
  615. return -2;
  616. }
  617. if (strcmp(name, "digest") == 0)
  618. return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
  619. value);
  620. return ctx->pmeth->ctrl_str(ctx, name, value);
  621. }
  622. /* Utility functions to send a string of hex string to a ctrl */
  623. int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
  624. {
  625. size_t len;
  626. len = strlen(str);
  627. if (len > INT_MAX)
  628. return -1;
  629. return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
  630. }
  631. int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
  632. {
  633. unsigned char *bin;
  634. long binlen;
  635. int rv = -1;
  636. bin = OPENSSL_hexstr2buf(hex, &binlen);
  637. if (bin == NULL)
  638. return 0;
  639. if (binlen <= INT_MAX)
  640. rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
  641. OPENSSL_free(bin);
  642. return rv;
  643. }
  644. /* Pass a message digest to a ctrl */
  645. int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
  646. {
  647. const EVP_MD *m;
  648. if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
  649. EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
  650. return 0;
  651. }
  652. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
  653. }
  654. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  655. {
  656. return ctx->operation;
  657. }
  658. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  659. {
  660. ctx->keygen_info = dat;
  661. ctx->keygen_info_count = datlen;
  662. }
  663. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  664. {
  665. ctx->data = data;
  666. }
  667. void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
  668. {
  669. return ctx->data;
  670. }
  671. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  672. {
  673. return ctx->pkey;
  674. }
  675. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  676. {
  677. return ctx->peerkey;
  678. }
  679. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  680. {
  681. ctx->app_data = data;
  682. }
  683. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  684. {
  685. return ctx->app_data;
  686. }
  687. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  688. int (*init) (EVP_PKEY_CTX *ctx))
  689. {
  690. pmeth->init = init;
  691. }
  692. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  693. int (*copy) (EVP_PKEY_CTX *dst,
  694. const EVP_PKEY_CTX *src))
  695. {
  696. pmeth->copy = copy;
  697. }
  698. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  699. void (*cleanup) (EVP_PKEY_CTX *ctx))
  700. {
  701. pmeth->cleanup = cleanup;
  702. }
  703. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  704. int (*paramgen_init) (EVP_PKEY_CTX *ctx),
  705. int (*paramgen) (EVP_PKEY_CTX *ctx,
  706. EVP_PKEY *pkey))
  707. {
  708. pmeth->paramgen_init = paramgen_init;
  709. pmeth->paramgen = paramgen;
  710. }
  711. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  712. int (*keygen_init) (EVP_PKEY_CTX *ctx),
  713. int (*keygen) (EVP_PKEY_CTX *ctx,
  714. EVP_PKEY *pkey))
  715. {
  716. pmeth->keygen_init = keygen_init;
  717. pmeth->keygen = keygen;
  718. }
  719. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  720. int (*sign_init) (EVP_PKEY_CTX *ctx),
  721. int (*sign) (EVP_PKEY_CTX *ctx,
  722. unsigned char *sig, size_t *siglen,
  723. const unsigned char *tbs,
  724. size_t tbslen))
  725. {
  726. pmeth->sign_init = sign_init;
  727. pmeth->sign = sign;
  728. }
  729. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  730. int (*verify_init) (EVP_PKEY_CTX *ctx),
  731. int (*verify) (EVP_PKEY_CTX *ctx,
  732. const unsigned char *sig,
  733. size_t siglen,
  734. const unsigned char *tbs,
  735. size_t tbslen))
  736. {
  737. pmeth->verify_init = verify_init;
  738. pmeth->verify = verify;
  739. }
  740. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  741. int (*verify_recover_init) (EVP_PKEY_CTX
  742. *ctx),
  743. int (*verify_recover) (EVP_PKEY_CTX
  744. *ctx,
  745. unsigned char
  746. *sig,
  747. size_t *siglen,
  748. const unsigned
  749. char *tbs,
  750. size_t tbslen))
  751. {
  752. pmeth->verify_recover_init = verify_recover_init;
  753. pmeth->verify_recover = verify_recover;
  754. }
  755. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  756. int (*signctx_init) (EVP_PKEY_CTX *ctx,
  757. EVP_MD_CTX *mctx),
  758. int (*signctx) (EVP_PKEY_CTX *ctx,
  759. unsigned char *sig,
  760. size_t *siglen,
  761. EVP_MD_CTX *mctx))
  762. {
  763. pmeth->signctx_init = signctx_init;
  764. pmeth->signctx = signctx;
  765. }
  766. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  767. int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
  768. EVP_MD_CTX *mctx),
  769. int (*verifyctx) (EVP_PKEY_CTX *ctx,
  770. const unsigned char *sig,
  771. int siglen,
  772. EVP_MD_CTX *mctx))
  773. {
  774. pmeth->verifyctx_init = verifyctx_init;
  775. pmeth->verifyctx = verifyctx;
  776. }
  777. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  778. int (*encrypt_init) (EVP_PKEY_CTX *ctx),
  779. int (*encryptfn) (EVP_PKEY_CTX *ctx,
  780. unsigned char *out,
  781. size_t *outlen,
  782. const unsigned char *in,
  783. size_t inlen))
  784. {
  785. pmeth->encrypt_init = encrypt_init;
  786. pmeth->encrypt = encryptfn;
  787. }
  788. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  789. int (*decrypt_init) (EVP_PKEY_CTX *ctx),
  790. int (*decrypt) (EVP_PKEY_CTX *ctx,
  791. unsigned char *out,
  792. size_t *outlen,
  793. const unsigned char *in,
  794. size_t inlen))
  795. {
  796. pmeth->decrypt_init = decrypt_init;
  797. pmeth->decrypt = decrypt;
  798. }
  799. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  800. int (*derive_init) (EVP_PKEY_CTX *ctx),
  801. int (*derive) (EVP_PKEY_CTX *ctx,
  802. unsigned char *key,
  803. size_t *keylen))
  804. {
  805. pmeth->derive_init = derive_init;
  806. pmeth->derive = derive;
  807. }
  808. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  809. int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  810. void *p2),
  811. int (*ctrl_str) (EVP_PKEY_CTX *ctx,
  812. const char *type,
  813. const char *value))
  814. {
  815. pmeth->ctrl = ctrl;
  816. pmeth->ctrl_str = ctrl_str;
  817. }
  818. void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
  819. int (*check) (EVP_PKEY *pkey))
  820. {
  821. pmeth->check = check;
  822. }
  823. void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
  824. int (*check) (EVP_PKEY *pkey))
  825. {
  826. pmeth->public_check = check;
  827. }
  828. void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
  829. int (*check) (EVP_PKEY *pkey))
  830. {
  831. pmeth->param_check = check;
  832. }
  833. void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
  834. int (*digest_custom) (EVP_PKEY_CTX *ctx,
  835. EVP_MD_CTX *mctx))
  836. {
  837. pmeth->digest_custom = digest_custom;
  838. }
  839. void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
  840. int (**pinit) (EVP_PKEY_CTX *ctx))
  841. {
  842. *pinit = pmeth->init;
  843. }
  844. void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
  845. int (**pcopy) (EVP_PKEY_CTX *dst,
  846. const EVP_PKEY_CTX *src))
  847. {
  848. *pcopy = pmeth->copy;
  849. }
  850. void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
  851. void (**pcleanup) (EVP_PKEY_CTX *ctx))
  852. {
  853. *pcleanup = pmeth->cleanup;
  854. }
  855. void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
  856. int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
  857. int (**pparamgen) (EVP_PKEY_CTX *ctx,
  858. EVP_PKEY *pkey))
  859. {
  860. if (pparamgen_init)
  861. *pparamgen_init = pmeth->paramgen_init;
  862. if (pparamgen)
  863. *pparamgen = pmeth->paramgen;
  864. }
  865. void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
  866. int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
  867. int (**pkeygen) (EVP_PKEY_CTX *ctx,
  868. EVP_PKEY *pkey))
  869. {
  870. if (pkeygen_init)
  871. *pkeygen_init = pmeth->keygen_init;
  872. if (pkeygen)
  873. *pkeygen = pmeth->keygen;
  874. }
  875. void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
  876. int (**psign_init) (EVP_PKEY_CTX *ctx),
  877. int (**psign) (EVP_PKEY_CTX *ctx,
  878. unsigned char *sig, size_t *siglen,
  879. const unsigned char *tbs,
  880. size_t tbslen))
  881. {
  882. if (psign_init)
  883. *psign_init = pmeth->sign_init;
  884. if (psign)
  885. *psign = pmeth->sign;
  886. }
  887. void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
  888. int (**pverify_init) (EVP_PKEY_CTX *ctx),
  889. int (**pverify) (EVP_PKEY_CTX *ctx,
  890. const unsigned char *sig,
  891. size_t siglen,
  892. const unsigned char *tbs,
  893. size_t tbslen))
  894. {
  895. if (pverify_init)
  896. *pverify_init = pmeth->verify_init;
  897. if (pverify)
  898. *pverify = pmeth->verify;
  899. }
  900. void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
  901. int (**pverify_recover_init) (EVP_PKEY_CTX
  902. *ctx),
  903. int (**pverify_recover) (EVP_PKEY_CTX
  904. *ctx,
  905. unsigned char
  906. *sig,
  907. size_t *siglen,
  908. const unsigned
  909. char *tbs,
  910. size_t tbslen))
  911. {
  912. if (pverify_recover_init)
  913. *pverify_recover_init = pmeth->verify_recover_init;
  914. if (pverify_recover)
  915. *pverify_recover = pmeth->verify_recover;
  916. }
  917. void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
  918. int (**psignctx_init) (EVP_PKEY_CTX *ctx,
  919. EVP_MD_CTX *mctx),
  920. int (**psignctx) (EVP_PKEY_CTX *ctx,
  921. unsigned char *sig,
  922. size_t *siglen,
  923. EVP_MD_CTX *mctx))
  924. {
  925. if (psignctx_init)
  926. *psignctx_init = pmeth->signctx_init;
  927. if (psignctx)
  928. *psignctx = pmeth->signctx;
  929. }
  930. void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
  931. int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
  932. EVP_MD_CTX *mctx),
  933. int (**pverifyctx) (EVP_PKEY_CTX *ctx,
  934. const unsigned char *sig,
  935. int siglen,
  936. EVP_MD_CTX *mctx))
  937. {
  938. if (pverifyctx_init)
  939. *pverifyctx_init = pmeth->verifyctx_init;
  940. if (pverifyctx)
  941. *pverifyctx = pmeth->verifyctx;
  942. }
  943. void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
  944. int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
  945. int (**pencryptfn) (EVP_PKEY_CTX *ctx,
  946. unsigned char *out,
  947. size_t *outlen,
  948. const unsigned char *in,
  949. size_t inlen))
  950. {
  951. if (pencrypt_init)
  952. *pencrypt_init = pmeth->encrypt_init;
  953. if (pencryptfn)
  954. *pencryptfn = pmeth->encrypt;
  955. }
  956. void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
  957. int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
  958. int (**pdecrypt) (EVP_PKEY_CTX *ctx,
  959. unsigned char *out,
  960. size_t *outlen,
  961. const unsigned char *in,
  962. size_t inlen))
  963. {
  964. if (pdecrypt_init)
  965. *pdecrypt_init = pmeth->decrypt_init;
  966. if (pdecrypt)
  967. *pdecrypt = pmeth->decrypt;
  968. }
  969. void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
  970. int (**pderive_init) (EVP_PKEY_CTX *ctx),
  971. int (**pderive) (EVP_PKEY_CTX *ctx,
  972. unsigned char *key,
  973. size_t *keylen))
  974. {
  975. if (pderive_init)
  976. *pderive_init = pmeth->derive_init;
  977. if (pderive)
  978. *pderive = pmeth->derive;
  979. }
  980. void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
  981. int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
  982. void *p2),
  983. int (**pctrl_str) (EVP_PKEY_CTX *ctx,
  984. const char *type,
  985. const char *value))
  986. {
  987. if (pctrl)
  988. *pctrl = pmeth->ctrl;
  989. if (pctrl_str)
  990. *pctrl_str = pmeth->ctrl_str;
  991. }
  992. void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
  993. int (**pcheck) (EVP_PKEY *pkey))
  994. {
  995. if (pcheck != NULL)
  996. *pcheck = pmeth->check;
  997. }
  998. void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
  999. int (**pcheck) (EVP_PKEY *pkey))
  1000. {
  1001. if (pcheck != NULL)
  1002. *pcheck = pmeth->public_check;
  1003. }
  1004. void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
  1005. int (**pcheck) (EVP_PKEY *pkey))
  1006. {
  1007. if (pcheck != NULL)
  1008. *pcheck = pmeth->param_check;
  1009. }
  1010. void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
  1011. int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
  1012. EVP_MD_CTX *mctx))
  1013. {
  1014. if (pdigest_custom != NULL)
  1015. *pdigest_custom = pmeth->digest_custom;
  1016. }