pmeth_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* pmeth_lib.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  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. /* Try to find an ENGINE which implements this method */
  128. if (e)
  129. {
  130. if (!ENGINE_init(e))
  131. {
  132. EVPerr(EVP_F_INT_CTX_NEW,ERR_R_ENGINE_LIB);
  133. return NULL;
  134. }
  135. }
  136. else
  137. e = ENGINE_get_pkey_meth_engine(id);
  138. /* If an ENGINE handled this method look it up. Othewise
  139. * use internal tables.
  140. */
  141. if (e)
  142. pmeth = ENGINE_get_pkey_meth(e, id);
  143. else
  144. #endif
  145. pmeth = EVP_PKEY_meth_find(id);
  146. if (pmeth == NULL)
  147. {
  148. EVPerr(EVP_F_INT_CTX_NEW,EVP_R_UNSUPPORTED_ALGORITHM);
  149. return NULL;
  150. }
  151. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  152. if (!ret)
  153. {
  154. #ifndef OPENSSL_NO_ENGINE
  155. if (e)
  156. ENGINE_finish(e);
  157. #endif
  158. EVPerr(EVP_F_INT_CTX_NEW,ERR_R_MALLOC_FAILURE);
  159. return NULL;
  160. }
  161. ret->engine = e;
  162. ret->pmeth = pmeth;
  163. ret->operation = EVP_PKEY_OP_UNDEFINED;
  164. ret->pkey = pkey;
  165. ret->peerkey = NULL;
  166. if (pkey)
  167. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  168. ret->data = NULL;
  169. if (pmeth->init)
  170. {
  171. if (pmeth->init(ret) <= 0)
  172. {
  173. EVP_PKEY_CTX_free(ret);
  174. return NULL;
  175. }
  176. }
  177. return ret;
  178. }
  179. EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags)
  180. {
  181. EVP_PKEY_METHOD *pmeth;
  182. pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
  183. if (!pmeth)
  184. return NULL;
  185. pmeth->pkey_id = id;
  186. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  187. pmeth->init = 0;
  188. pmeth->copy = 0;
  189. pmeth->cleanup = 0;
  190. pmeth->paramgen_init = 0;
  191. pmeth->paramgen = 0;
  192. pmeth->keygen_init = 0;
  193. pmeth->keygen = 0;
  194. pmeth->sign_init = 0;
  195. pmeth->sign = 0;
  196. pmeth->verify_init = 0;
  197. pmeth->verify = 0;
  198. pmeth->verify_recover_init = 0;
  199. pmeth->verify_recover = 0;
  200. pmeth->signctx_init = 0;
  201. pmeth->signctx = 0;
  202. pmeth->verifyctx_init = 0;
  203. pmeth->verifyctx = 0;
  204. pmeth->encrypt_init = 0;
  205. pmeth->encrypt = 0;
  206. pmeth->decrypt_init = 0;
  207. pmeth->decrypt = 0;
  208. pmeth->derive_init = 0;
  209. pmeth->derive = 0;
  210. pmeth->ctrl = 0;
  211. pmeth->ctrl_str = 0;
  212. return pmeth;
  213. }
  214. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  215. {
  216. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  217. OPENSSL_free(pmeth);
  218. }
  219. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  220. {
  221. return int_ctx_new(pkey, e, -1);
  222. }
  223. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  224. {
  225. return int_ctx_new(NULL, e, id);
  226. }
  227. EVP_PKEY_CTX *EVP_PKEY_CTX_dup(EVP_PKEY_CTX *pctx)
  228. {
  229. EVP_PKEY_CTX *rctx;
  230. if (!pctx->pmeth || !pctx->pmeth->copy)
  231. return NULL;
  232. #ifndef OPENSSL_NO_ENGINE
  233. /* Make sure it's safe to copy a pkey context using an ENGINE */
  234. if (pctx->engine && !ENGINE_init(pctx->engine))
  235. {
  236. EVPerr(EVP_F_EVP_PKEY_CTX_DUP,ERR_R_ENGINE_LIB);
  237. return 0;
  238. }
  239. #endif
  240. rctx = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  241. if (!rctx)
  242. return NULL;
  243. rctx->pmeth = pctx->pmeth;
  244. #ifndef OPENSSL_NO_ENGINE
  245. rctx->engine = pctx->engine;
  246. #endif
  247. if (pctx->pkey)
  248. CRYPTO_add(&pctx->pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  249. rctx->pkey = pctx->pkey;
  250. if (pctx->peerkey)
  251. CRYPTO_add(&pctx->peerkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  252. rctx->peerkey = pctx->peerkey;
  253. rctx->data = NULL;
  254. rctx->app_data = NULL;
  255. rctx->operation = pctx->operation;
  256. if (pctx->pmeth->copy(rctx, pctx) > 0)
  257. return rctx;
  258. EVP_PKEY_CTX_free(rctx);
  259. return NULL;
  260. }
  261. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  262. {
  263. if (app_pkey_methods == NULL)
  264. {
  265. app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
  266. if (!app_pkey_methods)
  267. return 0;
  268. }
  269. if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
  270. return 0;
  271. sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
  272. return 1;
  273. }
  274. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  275. {
  276. if (ctx == NULL)
  277. return;
  278. if (ctx->pmeth && ctx->pmeth->cleanup)
  279. ctx->pmeth->cleanup(ctx);
  280. if (ctx->pkey)
  281. EVP_PKEY_free(ctx->pkey);
  282. if (ctx->peerkey)
  283. EVP_PKEY_free(ctx->peerkey);
  284. #ifndef OPENSSL_NO_ENGINE
  285. if(ctx->engine)
  286. /* The EVP_PKEY_CTX we used belongs to an ENGINE, release the
  287. * functional reference we held for this reason. */
  288. ENGINE_finish(ctx->engine);
  289. #endif
  290. OPENSSL_free(ctx);
  291. }
  292. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  293. int cmd, int p1, void *p2)
  294. {
  295. int ret;
  296. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl)
  297. {
  298. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  299. return -2;
  300. }
  301. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  302. return -1;
  303. if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
  304. {
  305. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  306. return -1;
  307. }
  308. if ((optype != -1) && !(ctx->operation & optype))
  309. {
  310. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  311. return -1;
  312. }
  313. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  314. if (ret == -2)
  315. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  316. return ret;
  317. }
  318. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  319. const char *name, const char *value)
  320. {
  321. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str)
  322. {
  323. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  324. EVP_R_COMMAND_NOT_SUPPORTED);
  325. return -2;
  326. }
  327. if (!strcmp(name, "digest"))
  328. {
  329. const EVP_MD *md;
  330. if (!value || !(md = EVP_get_digestbyname(value)))
  331. {
  332. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  333. EVP_R_INVALID_DIGEST);
  334. return 0;
  335. }
  336. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  337. }
  338. return ctx->pmeth->ctrl_str(ctx, name, value);
  339. }
  340. int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
  341. {
  342. return ctx->operation;
  343. }
  344. void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
  345. {
  346. ctx->keygen_info = dat;
  347. ctx->keygen_info_count = datlen;
  348. }
  349. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  350. {
  351. ctx->data = data;
  352. }
  353. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  354. {
  355. return ctx->data;
  356. }
  357. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  358. {
  359. return ctx->pkey;
  360. }
  361. EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
  362. {
  363. return ctx->peerkey;
  364. }
  365. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  366. {
  367. ctx->app_data = data;
  368. }
  369. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  370. {
  371. return ctx->app_data;
  372. }
  373. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  374. int (*init)(EVP_PKEY_CTX *ctx))
  375. {
  376. pmeth->init = init;
  377. }
  378. void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
  379. int (*copy)(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src))
  380. {
  381. pmeth->copy = copy;
  382. }
  383. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  384. void (*cleanup)(EVP_PKEY_CTX *ctx))
  385. {
  386. pmeth->cleanup = cleanup;
  387. }
  388. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  389. int (*paramgen_init)(EVP_PKEY_CTX *ctx),
  390. int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  391. {
  392. pmeth->paramgen_init = paramgen_init;
  393. pmeth->paramgen = paramgen;
  394. }
  395. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  396. int (*keygen_init)(EVP_PKEY_CTX *ctx),
  397. int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  398. {
  399. pmeth->keygen_init = keygen_init;
  400. pmeth->keygen = keygen;
  401. }
  402. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  403. int (*sign_init)(EVP_PKEY_CTX *ctx),
  404. int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  405. const unsigned char *tbs, size_t tbslen))
  406. {
  407. pmeth->sign_init = sign_init;
  408. pmeth->sign = sign;
  409. }
  410. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  411. int (*verify_init)(EVP_PKEY_CTX *ctx),
  412. int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
  413. const unsigned char *tbs, size_t tbslen))
  414. {
  415. pmeth->verify_init = verify_init;
  416. pmeth->verify = verify;
  417. }
  418. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  419. int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
  420. int (*verify_recover)(EVP_PKEY_CTX *ctx,
  421. unsigned char *sig, size_t *siglen,
  422. const unsigned char *tbs, size_t tbslen))
  423. {
  424. pmeth->verify_recover_init = verify_recover_init;
  425. pmeth->verify_recover = verify_recover;
  426. }
  427. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  428. int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  429. int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
  430. EVP_MD_CTX *mctx))
  431. {
  432. pmeth->signctx_init = signctx_init;
  433. pmeth->signctx = signctx;
  434. }
  435. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  436. int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  437. int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig,int siglen,
  438. EVP_MD_CTX *mctx))
  439. {
  440. pmeth->verifyctx_init = verifyctx_init;
  441. pmeth->verifyctx = verifyctx;
  442. }
  443. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  444. int (*encrypt_init)(EVP_PKEY_CTX *ctx),
  445. int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  446. const unsigned char *in, size_t inlen))
  447. {
  448. pmeth->encrypt_init = encrypt_init;
  449. pmeth->encrypt = encryptfn;
  450. }
  451. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  452. int (*decrypt_init)(EVP_PKEY_CTX *ctx),
  453. int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
  454. const unsigned char *in, size_t inlen))
  455. {
  456. pmeth->decrypt_init = decrypt_init;
  457. pmeth->decrypt = decrypt;
  458. }
  459. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  460. int (*derive_init)(EVP_PKEY_CTX *ctx),
  461. int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen))
  462. {
  463. pmeth->derive_init = derive_init;
  464. pmeth->derive = derive;
  465. }
  466. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  467. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
  468. int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
  469. {
  470. pmeth->ctrl = ctrl;
  471. pmeth->ctrl_str = ctrl_str;
  472. }