2
0

pmeth_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* pmeth_lib.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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. #include "asn1_locl.h"
  64. #include "evp_locl.h"
  65. typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
  66. STACK *app_pkey_methods = NULL;
  67. extern EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth, ec_pkey_meth;
  68. static const EVP_PKEY_METHOD *standard_methods[] =
  69. {
  70. &rsa_pkey_meth,
  71. &dh_pkey_meth,
  72. &dsa_pkey_meth,
  73. &ec_pkey_meth
  74. };
  75. static int pmeth_cmp(const EVP_PKEY_METHOD * const *a,
  76. const EVP_PKEY_METHOD * const *b)
  77. {
  78. return ((*a)->pkey_id - (*b)->pkey_id);
  79. }
  80. const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type, ENGINE *e)
  81. {
  82. EVP_PKEY_METHOD tmp, *t = &tmp, **ret;
  83. tmp.pkey_id = type;
  84. if (app_pkey_methods)
  85. {
  86. int idx;
  87. idx = sk_find(app_pkey_methods, (char *)&tmp);
  88. if (idx >= 0)
  89. return (EVP_PKEY_METHOD *)
  90. sk_value(app_pkey_methods, idx);
  91. }
  92. ret = (EVP_PKEY_METHOD **) OBJ_bsearch((char *)&t,
  93. (char *)standard_methods,
  94. sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *),
  95. sizeof(EVP_PKEY_METHOD *),
  96. (int (*)(const void *, const void *))pmeth_cmp);
  97. if (!ret || !*ret)
  98. return NULL;
  99. return *ret;
  100. }
  101. static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
  102. {
  103. EVP_PKEY_CTX *ret;
  104. const EVP_PKEY_METHOD *pmeth;
  105. if (id == -1)
  106. {
  107. if (!pkey || !pkey->ameth)
  108. return NULL;
  109. id = pkey->ameth->pkey_id;
  110. }
  111. pmeth = EVP_PKEY_meth_find(id, e);
  112. if (pmeth == NULL)
  113. return NULL;
  114. ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
  115. ret->pmeth = pmeth;
  116. ret->operation = EVP_PKEY_OP_UNDEFINED;
  117. ret->pkey = pkey;
  118. ret->peerkey = NULL;
  119. if (pkey)
  120. CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
  121. ret->data = NULL;
  122. if (pmeth->init)
  123. {
  124. if (pmeth->init(ret) <= 0)
  125. {
  126. EVP_PKEY_CTX_free(ret);
  127. return NULL;
  128. }
  129. }
  130. return ret;
  131. }
  132. EVP_PKEY_METHOD* EVP_PKEY_meth_new(int id, int flags)
  133. {
  134. EVP_PKEY_METHOD *pmeth;
  135. pmeth = OPENSSL_malloc(sizeof(EVP_PKEY_METHOD));
  136. if (!pmeth)
  137. return NULL;
  138. pmeth->pkey_id = id;
  139. pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC;
  140. pmeth->init = 0;
  141. pmeth->cleanup = 0;
  142. pmeth->paramgen_init = 0;
  143. pmeth->paramgen = 0;
  144. pmeth->keygen_init = 0;
  145. pmeth->keygen = 0;
  146. pmeth->sign_init = 0;
  147. pmeth->sign = 0;
  148. pmeth->verify_init = 0;
  149. pmeth->verify = 0;
  150. pmeth->verify_recover_init = 0;
  151. pmeth->verify_recover = 0;
  152. pmeth->signctx_init = 0;
  153. pmeth->signctx = 0;
  154. pmeth->verifyctx_init = 0;
  155. pmeth->verifyctx = 0;
  156. pmeth->encrypt_init = 0;
  157. pmeth->encrypt = 0;
  158. pmeth->decrypt_init = 0;
  159. pmeth->decrypt = 0;
  160. pmeth->derive_init = 0;
  161. pmeth->derive = 0;
  162. pmeth->ctrl = 0;
  163. pmeth->ctrl_str = 0;
  164. return pmeth;
  165. }
  166. void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth)
  167. {
  168. if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC))
  169. OPENSSL_free(pmeth);
  170. }
  171. EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e)
  172. {
  173. return int_ctx_new(pkey, e, -1);
  174. }
  175. EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e)
  176. {
  177. return int_ctx_new(NULL, e, id);
  178. }
  179. int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
  180. {
  181. if (app_pkey_methods == NULL)
  182. {
  183. app_pkey_methods = sk_new((sk_cmp_fn_type *)pmeth_cmp);
  184. if (!app_pkey_methods)
  185. return 0;
  186. }
  187. if (!sk_push(app_pkey_methods, (char *)pmeth))
  188. return 0;
  189. sk_sort(app_pkey_methods);
  190. return 1;
  191. }
  192. void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx)
  193. {
  194. if (ctx->pmeth && ctx->pmeth->cleanup)
  195. ctx->pmeth->cleanup(ctx);
  196. if (ctx->pkey)
  197. EVP_PKEY_free(ctx->pkey);
  198. if (ctx->peerkey)
  199. EVP_PKEY_free(ctx->peerkey);
  200. OPENSSL_free(ctx);
  201. }
  202. int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
  203. int cmd, int p1, void *p2)
  204. {
  205. int ret;
  206. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl)
  207. {
  208. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  209. return -2;
  210. }
  211. if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
  212. return -1;
  213. if (ctx->operation == EVP_PKEY_OP_UNDEFINED)
  214. {
  215. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
  216. return -1;
  217. }
  218. if ((optype != -1) && !(ctx->operation & optype))
  219. {
  220. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
  221. return -1;
  222. }
  223. ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
  224. if (ret == -2)
  225. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
  226. return ret;
  227. }
  228. int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
  229. const char *name, const char *value)
  230. {
  231. if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str)
  232. {
  233. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  234. EVP_R_COMMAND_NOT_SUPPORTED);
  235. return -2;
  236. }
  237. if (!strcmp(name, "digest"))
  238. {
  239. const EVP_MD *md;
  240. if (!value || !(md = EVP_get_digestbyname(value)))
  241. {
  242. EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR,
  243. EVP_R_INVALID_DIGEST);
  244. return 0;
  245. }
  246. return EVP_PKEY_CTX_set_signature_md(ctx, md);
  247. }
  248. return ctx->pmeth->ctrl_str(ctx, name, value);
  249. }
  250. void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
  251. {
  252. ctx->data = data;
  253. }
  254. void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX *ctx)
  255. {
  256. return ctx->data;
  257. }
  258. EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
  259. {
  260. return ctx->pkey;
  261. }
  262. void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
  263. {
  264. ctx->app_data = data;
  265. }
  266. void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
  267. {
  268. return ctx->app_data;
  269. }
  270. void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
  271. int (*init)(EVP_PKEY_CTX *ctx))
  272. {
  273. pmeth->init = init;
  274. }
  275. void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
  276. void (*cleanup)(EVP_PKEY_CTX *ctx))
  277. {
  278. pmeth->cleanup = cleanup;
  279. }
  280. void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
  281. int (*paramgen_init)(EVP_PKEY_CTX *ctx),
  282. int (*paramgen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  283. {
  284. pmeth->paramgen_init = paramgen_init;
  285. pmeth->paramgen = paramgen;
  286. }
  287. void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
  288. int (*keygen_init)(EVP_PKEY_CTX *ctx),
  289. int (*keygen)(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey))
  290. {
  291. pmeth->keygen_init = keygen_init;
  292. pmeth->keygen = keygen;
  293. }
  294. void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
  295. int (*sign_init)(EVP_PKEY_CTX *ctx),
  296. int (*sign)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
  297. const unsigned char *tbs, int tbslen))
  298. {
  299. pmeth->sign_init = sign_init;
  300. pmeth->sign = sign;
  301. }
  302. void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
  303. int (*verify_init)(EVP_PKEY_CTX *ctx),
  304. int (*verify)(EVP_PKEY_CTX *ctx, const unsigned char *sig, int siglen,
  305. const unsigned char *tbs, int tbslen))
  306. {
  307. pmeth->verify_init = verify_init;
  308. pmeth->verify = verify;
  309. }
  310. void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
  311. int (*verify_recover_init)(EVP_PKEY_CTX *ctx),
  312. int (*verify_recover)(EVP_PKEY_CTX *ctx,
  313. unsigned char *sig, int *siglen,
  314. const unsigned char *tbs, int tbslen))
  315. {
  316. pmeth->verify_recover_init = verify_recover_init;
  317. pmeth->verify_recover = verify_recover;
  318. }
  319. void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
  320. int (*signctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  321. int (*signctx)(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
  322. EVP_MD_CTX *mctx))
  323. {
  324. pmeth->signctx_init = signctx_init;
  325. pmeth->signctx = signctx;
  326. }
  327. void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
  328. int (*verifyctx_init)(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx),
  329. int (*verifyctx)(EVP_PKEY_CTX *ctx, const unsigned char *sig,int siglen,
  330. EVP_MD_CTX *mctx))
  331. {
  332. pmeth->verifyctx_init = verifyctx_init;
  333. pmeth->verifyctx = verifyctx;
  334. }
  335. void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
  336. int (*encrypt_init)(EVP_PKEY_CTX *ctx),
  337. int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
  338. const unsigned char *in, int inlen))
  339. {
  340. pmeth->encrypt_init = encrypt_init;
  341. pmeth->encrypt = encrypt;
  342. }
  343. void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
  344. int (*decrypt_init)(EVP_PKEY_CTX *ctx),
  345. int (*decrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, int *outlen,
  346. const unsigned char *in, int inlen))
  347. {
  348. pmeth->decrypt_init = decrypt_init;
  349. pmeth->decrypt = decrypt;
  350. }
  351. void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
  352. int (*derive_init)(EVP_PKEY_CTX *ctx),
  353. int (*derive)(EVP_PKEY_CTX *ctx, unsigned char *key, int *keylen))
  354. {
  355. pmeth->derive_init = derive_init;
  356. pmeth->derive = derive;
  357. }
  358. void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
  359. int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2),
  360. int (*ctrl_str)(EVP_PKEY_CTX *ctx, const char *type, const char *value))
  361. {
  362. pmeth->ctrl = ctrl;
  363. pmeth->ctrl_str = ctrl_str;
  364. }