pmeth_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /* pmeth_lib.c */
  2. /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
  3. * project 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * [email protected].
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include "cryptlib.h"
  61. #include <openssl/objects.h>
  62. #include <openssl/evp.h>
  63. #ifndef OPENSSL_NO_ENGINE
  64. #include <openssl/engine.h>
  65. #endif
  66. #include "asn1_locl.h"
  67. #include "evp_locl.h"
  68. typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
  69. DECLARE_STACK_OF(EVP_PKEY_METHOD)
  70. STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
  71. extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
  72. extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth;
  73. static const EVP_PKEY_METHOD *standard_methods[] =
  74. {
  75. #ifndef OPENSSL_NO_RSA
  76. &rsa_pkey_meth,
  77. #endif
  78. #ifndef OPENSSL_NO_DH
  79. &dh_pkey_meth,
  80. #endif
  81. #ifndef OPENSSL_NO_DSA
  82. &dsa_pkey_meth,
  83. #endif
  84. #ifndef OPENSSL_NO_EC
  85. &ec_pkey_meth,
  86. #endif
  87. &hmac_pkey_meth,
  88. };
  89. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  90. pmeth);
  91. static int pmeth_cmp(const EVP_PKEY_METHOD * const *a,
  92. const EVP_PKEY_METHOD * const *b)
  93. {
  94. return ((*a)->pkey_id - (*b)->pkey_id);
  95. }
  96. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
  97. pmeth);
  98. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
  99. {
  100. EVP_PKEY_METHOD tmp;
  101. const EVP_PKEY_METHOD *t = &tmp, **ret;
  102. tmp.pkey_id = type;
  103. if (app_pkey_methods)
  104. {
  105. int idx;
  106. idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
  107. if (idx >= 0)
  108. return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
  109. }
  110. ret = OBJ_bsearch_pmeth(&t, standard_methods,
  111. sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *));
  112. if (!ret || !*ret)
  113. return NULL;
  114. return *ret;
  115. }
  116. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
  117. {
  118. EVP_PKEY_CTX *ret;
  119. const EVP_PKEY_METHOD *pmeth;
  120. if (id == -1)
  121. {
  122. if (!pkey || !pkey->ameth)
  123. return NULL;
  124. id = pkey->ameth->pkey_id;
  125. }
  126. #ifndef OPENSSL_NO_ENGINE
  127. if (pkey && pkey->engine)
  128. e = pkey->engine;
  129. /* Try to find an ENGINE which implements this method */
  130. if (e)
  131. {
  132. if (!ENGINE_init(e))
  133. {
  134. EVPerr(EVP_F_INT_CTX_NEW,ERR_R_ENGINE_LIB);
  135. return NULL;
  136. }
  137. }
  138. else
  139. e = ENGINE_get_pkey_meth_engine(id);
  140. /* If an ENGINE handled this method look it up. Othewise
  141. * use internal tables.
  142. */
  143. if (e)
  144. pmeth = ENGINE_get_pkey_meth(e, id);
  145. else
  146. #endif
  147. pmeth = EVP_PKEY_meth_find(id);
  148. if (pmeth == NULL)
  149. {
  150. EVPerr(EVP_F_INT_CTX_NEW,EVP_R_UNSUPPORTED_ALGORITHM);
  151. return NULL;
  152. }
  153. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  154. if (!ret)
  155. {
  156. #ifndef OPENSSL_NO_ENGINE
  157. if (e)
  158. ENGINE_finish(e);
  159. #endif
  160. EVPerr(EVP_F_INT_CTX_NEW,ERR_R_MALLOC_FAILURE);
  161. return NULL;
  162. }
  163. ret->engine = e;
  164. ret->pmeth = pmeth;
  165. ret->operation = EVP_PKEY_OP_UNDEFINED;
  166. ret->pkey = pkey;
  167. ret->peerkey = NULL;
  168. ret->pkey_gencb = 0;
  169. if (pkey)
  170. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  171. ret->data = NULL;
  172. if (pmeth->init)
  173. {
  174. if (pmeth->init(ret) <= 0)
  175. {
  176. EVP_PKEY_CTX_free(ret);
  177. return NULL;
  178. }
  179. }
  180. return ret;
  181. }
  182. EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags)
  183. {
  184. EVP_PKEY_METHOD *pmeth;
  185. pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
  186. if (!pmeth)
  187. return NULL;
  188. pmeth->pkey_id = id;
  189. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  190. pmeth->init = 0;
  191. pmeth->copy = 0;
  192. pmeth->cleanup = 0;
  193. pmeth->paramgen_init = 0;
  194. pmeth->paramgen = 0;
  195. pmeth->keygen_init = 0;
  196. pmeth->keygen = 0;
  197. pmeth->sign_init = 0;
  198. pmeth->sign = 0;
  199. pmeth->verify_init = 0;
  200. pmeth->verify = 0;
  201. pmeth->verify_recover_init = 0;
  202. pmeth->verify_recover = 0;
  203. pmeth->signctx_init = 0;
  204. pmeth->signctx = 0;
  205. pmeth->verifyctx_init = 0;
  206. pmeth->verifyctx = 0;
  207. pmeth->encrypt_init = 0;
  208. pmeth->encrypt = 0;
  209. pmeth->decrypt_init = 0;
  210. pmeth->decrypt = 0;
  211. pmeth->derive_init = 0;
  212. pmeth->derive = 0;
  213. pmeth->ctrl = 0;
  214. pmeth->ctrl_str = 0;
  215. return pmeth;
  216. }
  217. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  218. {
  219. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  220. OPENSSL_free(pmeth);
  221. }
  222. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  223. {
  224. return int_ctx_new(pkey, e, -1);
  225. }
  226. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  227. {
  228. return int_ctx_new(NULL, e, id);
  229. }
  230. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
  231. {
  232. EVP_PKEY_CTX *rctx;
  233. if (!pctx->pmeth || !pctx->pmeth->copy)
  234. return NULL;
  235. #ifndef OPENSSL_NO_ENGINE
  236. /* Make sure it's safe to copy a pkey context using an ENGINE */
  237. if (pctx->engine && !ENGINE_init(pctx->engine))
  238. {
  239. EVPerr(EVP_F_EVP_PKEY_CTX_DUP,ERR_R_ENGINE_LIB);
  240. return 0;
  241. }
  242. #endif
  243. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  244. if (!rctx)
  245. return NULL;
  246. rctx->pmeth = pctx->pmeth;
  247. #ifndef OPENSSL_NO_ENGINE
  248. rctx->engine = pctx->engine;
  249. #endif
  250. if (pctx->pkey)
  251. CRYPTO_add(&pctx->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  252. rctx->pkey = pctx->pkey;
  253. if (pctx->peerkey)
  254. CRYPTO_add(&pctx->peerkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  255. rctx->peerkey = pctx->peerkey;
  256. rctx->data = NULL;
  257. rctx->app_data = NULL;
  258. rctx->operation = pctx->operation;
  259. if (pctx->pmeth->copy(rctx, pctx) > 0)
  260. return rctx;
  261. EVP_PKEY_CTX_free(rctx);
  262. return NULL;
  263. }
  264. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  265. {
  266. if (app_pkey_methods == NULL)
  267. {
  268. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  269. if (!app_pkey_methods)
  270. return 0;
  271. }
  272. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  273. return 0;
  274. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  275. return 1;
  276. }
  277. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  278. {
  279. if (ctx == NULL)
  280. return;
  281. if (ctx->pmeth && ctx->pmeth->cleanup)
  282. ctx->pmeth->cleanup(ctx);
  283. if (ctx->pkey)
  284. EVP_PKEY_free(ctx->pkey);
  285. if (ctx->peerkey)
  286. EVP_PKEY_free(ctx->peerkey);
  287. #ifndef OPENSSL_NO_ENGINE
  288. if(ctx->engine)
  289. /* The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  290. * functional reference we held for this reason. */
  291. ENGINE_finish(ctx->engine);
  292. #endif
  293. OPENSSL_free(ctx);
  294. }
  295. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  296. int cmd, int p1, void *p2)
  297. {
  298. int ret;
  299. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl)
  300. {
  301. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  302. return -2;
  303. }
  304. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  305. return -1;
  306. if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
  307. {
  308. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  309. return -1;
  310. }
  311. if ((optype != -1) && !(ctx->operation & optype))
  312. {
  313. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  314. return -1;
  315. }
  316. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  317. if (ret == -2)
  318. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  319. return ret;
  320. }
  321. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  322. const char *name, const char *value)
  323. {
  324. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str)
  325. {
  326. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  327. EVP_R_COMMAND_NOT_SUPPORTED);
  328. return -2;
  329. }
  330. if (!strcmp(name, "digest"))
  331. {
  332. const EVP_MD *md;
  333. if (!value || !(md = EVP_get_digestbyname(value)))
  334. {
  335. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  336. EVP_R_INVALID_DIGEST);
  337. return 0;
  338. }
  339. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  340. }
  341. return ctx->pmeth->ctrl_str(ctx, name, value);
  342. }
  343. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  344. {
  345. return ctx->operation;
  346. }
  347. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  348. {
  349. ctx->keygen_info = dat;
  350. ctx->keygen_info_count = datlen;
  351. }
  352. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  353. {
  354. ctx->data = data;
  355. }
  356. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  357. {
  358. return ctx->data;
  359. }
  360. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  361. {
  362. return ctx->pkey;
  363. }
  364. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  365. {
  366. return ctx->peerkey;
  367. }
  368. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  369. {
  370. ctx->app_data = data;
  371. }
  372. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  373. {
  374. return ctx->app_data;
  375. }
  376. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  377. int (*init)(EVP_PKEY_CTX *ctx))
  378. {
  379. pmeth->init = init;
  380. }
  381. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  382. int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
  383. {
  384. pmeth->copy = copy;
  385. }
  386. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  387. void (*cleanup)(EVP_PKEY_CTX *ctx))
  388. {
  389. pmeth->cleanup = cleanup;
  390. }
  391. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  392. int (*paramgen_init)(EVP_PKEY_CTX *ctx),
  393. int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  394. {
  395. pmeth->paramgen_init = paramgen_init;
  396. pmeth->paramgen = paramgen;
  397. }
  398. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  399. int (*keygen_init)(EVP_PKEY_CTX *ctx),
  400. int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  401. {
  402. pmeth->keygen_init = keygen_init;
  403. pmeth->keygen = keygen;
  404. }
  405. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  406. int (*sign_init)(EVP_PKEY_CTX *ctx),
  407. int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  408. const unsigned char *tbs, size_t tbslen))
  409. {
  410. pmeth->sign_init = sign_init;
  411. pmeth->sign = sign;
  412. }
  413. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  414. int (*verify_init)(EVP_PKEY_CTX *ctx),
  415. int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
  416. const unsigned char *tbs, size_t tbslen))
  417. {
  418. pmeth->verify_init = verify_init;
  419. pmeth->verify = verify;
  420. }
  421. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  422. int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
  423. int (*verify_recover)(EVP_PKEY_CTX *ctx,
  424. unsigned char *sig, size_t *siglen,
  425. const unsigned char *tbs, size_t tbslen))
  426. {
  427. pmeth->verify_recover_init = verify_recover_init;
  428. pmeth->verify_recover = verify_recover;
  429. }
  430. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  431. int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  432. int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  433. EVP_MD_CTX *mctx))
  434. {
  435. pmeth->signctx_init = signctx_init;
  436. pmeth->signctx = signctx;
  437. }
  438. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  439. int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  440. int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig,int siglen,
  441. EVP_MD_CTX *mctx))
  442. {
  443. pmeth->verifyctx_init = verifyctx_init;
  444. pmeth->verifyctx = verifyctx;
  445. }
  446. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  447. int (*encrypt_init)(EVP_PKEY_CTX *ctx),
  448. int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  449. const unsigned char *in, size_t inlen))
  450. {
  451. pmeth->encrypt_init = encrypt_init;
  452. pmeth->encrypt = encryptfn;
  453. }
  454. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  455. int (*decrypt_init)(EVP_PKEY_CTX *ctx),
  456. int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  457. const unsigned char *in, size_t inlen))
  458. {
  459. pmeth->decrypt_init = decrypt_init;
  460. pmeth->decrypt = decrypt;
  461. }
  462. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  463. int (*derive_init)(EVP_PKEY_CTX *ctx),
  464. int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
  465. {
  466. pmeth->derive_init = derive_init;
  467. pmeth->derive = derive;
  468. }
  469. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  470. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
  471. int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
  472. {
  473. pmeth->ctrl = ctrl;
  474. pmeth->ctrl_str = ctrl_str;
  475. }