eng_openssl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /* crypto/engine/eng_openssl.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. /* ====================================================================
  60. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  61. * ECDH support in OpenSSL originally developed by
  62. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  63. */
  64. #include <stdio.h>
  65. #include <openssl/crypto.h>
  66. #include "cryptlib.h"
  67. #include <openssl/engine.h>
  68. #include <openssl/dso.h>
  69. #include <openssl/pem.h>
  70. #include <openssl/evp.h>
  71. #include <openssl/rand.h>
  72. #ifndef OPENSSL_NO_RSA
  73. # include <openssl/rsa.h>
  74. #endif
  75. #ifndef OPENSSL_NO_DSA
  76. # include <openssl/dsa.h>
  77. #endif
  78. #ifndef OPENSSL_NO_DH
  79. # include <openssl/dh.h>
  80. #endif
  81. /*
  82. * This testing gunk is implemented (and explained) lower down. It also
  83. * assumes the application explicitly calls "ENGINE_load_openssl()" because
  84. * this is no longer automatic in ENGINE_load_builtin_engines().
  85. */
  86. #define TEST_ENG_OPENSSL_RC4
  87. #define TEST_ENG_OPENSSL_PKEY
  88. /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
  89. #define TEST_ENG_OPENSSL_RC4_P_INIT
  90. /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
  91. #define TEST_ENG_OPENSSL_SHA
  92. /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
  93. /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
  94. /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
  95. /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
  96. /* Now check what of those algorithms are actually enabled */
  97. #ifdef OPENSSL_NO_RC4
  98. # undef TEST_ENG_OPENSSL_RC4
  99. # undef TEST_ENG_OPENSSL_RC4_OTHERS
  100. # undef TEST_ENG_OPENSSL_RC4_P_INIT
  101. # undef TEST_ENG_OPENSSL_RC4_P_CIPHER
  102. #endif
  103. #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1)
  104. # undef TEST_ENG_OPENSSL_SHA
  105. # undef TEST_ENG_OPENSSL_SHA_OTHERS
  106. # undef TEST_ENG_OPENSSL_SHA_P_INIT
  107. # undef TEST_ENG_OPENSSL_SHA_P_UPDATE
  108. # undef TEST_ENG_OPENSSL_SHA_P_FINAL
  109. #endif
  110. #ifdef TEST_ENG_OPENSSL_RC4
  111. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  112. const int **nids, int nid);
  113. #endif
  114. #ifdef TEST_ENG_OPENSSL_SHA
  115. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  116. const int **nids, int nid);
  117. #endif
  118. #ifdef TEST_ENG_OPENSSL_PKEY
  119. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  120. UI_METHOD *ui_method,
  121. void *callback_data);
  122. #endif
  123. /* The constants used when creating the ENGINE */
  124. static const char *engine_openssl_id = "openssl";
  125. static const char *engine_openssl_name = "Software engine support";
  126. /*
  127. * This internal function is used by ENGINE_openssl() and possibly by the
  128. * "dynamic" ENGINE support too
  129. */
  130. static int bind_helper(ENGINE *e)
  131. {
  132. if (!ENGINE_set_id(e, engine_openssl_id)
  133. || !ENGINE_set_name(e, engine_openssl_name)
  134. #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
  135. # ifndef OPENSSL_NO_RSA
  136. || !ENGINE_set_RSA(e, RSA_get_default_method())
  137. # endif
  138. # ifndef OPENSSL_NO_DSA
  139. || !ENGINE_set_DSA(e, DSA_get_default_method())
  140. # endif
  141. # ifndef OPENSSL_NO_ECDH
  142. || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
  143. # endif
  144. # ifndef OPENSSL_NO_ECDSA
  145. || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())
  146. # endif
  147. # ifndef OPENSSL_NO_DH
  148. || !ENGINE_set_DH(e, DH_get_default_method())
  149. # endif
  150. || !ENGINE_set_RAND(e, RAND_SSLeay())
  151. # ifdef TEST_ENG_OPENSSL_RC4
  152. || !ENGINE_set_ciphers(e, openssl_ciphers)
  153. # endif
  154. # ifdef TEST_ENG_OPENSSL_SHA
  155. || !ENGINE_set_digests(e, openssl_digests)
  156. # endif
  157. #endif
  158. #ifdef TEST_ENG_OPENSSL_PKEY
  159. || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
  160. #endif
  161. )
  162. return 0;
  163. /*
  164. * If we add errors to this ENGINE, ensure the error handling is setup
  165. * here
  166. */
  167. /* openssl_load_error_strings(); */
  168. return 1;
  169. }
  170. static ENGINE *engine_openssl(void)
  171. {
  172. ENGINE *ret = ENGINE_new();
  173. if (!ret)
  174. return NULL;
  175. if (!bind_helper(ret)) {
  176. ENGINE_free(ret);
  177. return NULL;
  178. }
  179. return ret;
  180. }
  181. void ENGINE_load_openssl(void)
  182. {
  183. ENGINE *toadd = engine_openssl();
  184. if (!toadd)
  185. return;
  186. ENGINE_add(toadd);
  187. /*
  188. * If the "add" worked, it gets a structural reference. So either way, we
  189. * release our just-created reference.
  190. */
  191. ENGINE_free(toadd);
  192. ERR_clear_error();
  193. }
  194. /*
  195. * This stuff is needed if this ENGINE is being compiled into a
  196. * self-contained shared-library.
  197. */
  198. #ifdef ENGINE_DYNAMIC_SUPPORT
  199. static int bind_fn(ENGINE *e, const char *id)
  200. {
  201. if (id && (strcmp(id, engine_openssl_id) != 0))
  202. return 0;
  203. if (!bind_helper(e))
  204. return 0;
  205. return 1;
  206. }
  207. IMPLEMENT_DYNAMIC_CHECK_FN()
  208. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  209. #endif /* ENGINE_DYNAMIC_SUPPORT */
  210. #ifdef TEST_ENG_OPENSSL_RC4
  211. /*-
  212. * This section of code compiles an "alternative implementation" of two modes of
  213. * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
  214. * should under normal circumstances go via this support rather than the default
  215. * EVP support. There are other symbols to tweak the testing;
  216. * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
  217. * we're asked for a cipher we don't support (should not happen).
  218. * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
  219. * the "init_key" handler is called.
  220. * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
  221. */
  222. # include <openssl/rc4.h>
  223. # define TEST_RC4_KEY_SIZE 16
  224. static int test_cipher_nids[] = { NID_rc4, NID_rc4_40 };
  225. static int test_cipher_nids_number = 2;
  226. typedef struct {
  227. unsigned char key[TEST_RC4_KEY_SIZE];
  228. RC4_KEY ks;
  229. } TEST_RC4_KEY;
  230. # define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
  231. static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  232. const unsigned char *iv, int enc)
  233. {
  234. # ifdef TEST_ENG_OPENSSL_RC4_P_INIT
  235. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
  236. # endif
  237. memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx));
  238. RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
  239. test(ctx)->key);
  240. return 1;
  241. }
  242. static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  243. const unsigned char *in, size_t inl)
  244. {
  245. # ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
  246. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
  247. # endif
  248. RC4(&test(ctx)->ks, inl, in, out);
  249. return 1;
  250. }
  251. static const EVP_CIPHER test_r4_cipher = {
  252. NID_rc4,
  253. 1, TEST_RC4_KEY_SIZE, 0,
  254. EVP_CIPH_VARIABLE_LENGTH,
  255. test_rc4_init_key,
  256. test_rc4_cipher,
  257. NULL,
  258. sizeof(TEST_RC4_KEY),
  259. NULL,
  260. NULL,
  261. NULL,
  262. NULL
  263. };
  264. static const EVP_CIPHER test_r4_40_cipher = {
  265. NID_rc4_40,
  266. 1, 5 /* 40 bit */ , 0,
  267. EVP_CIPH_VARIABLE_LENGTH,
  268. test_rc4_init_key,
  269. test_rc4_cipher,
  270. NULL,
  271. sizeof(TEST_RC4_KEY),
  272. NULL,
  273. NULL,
  274. NULL,
  275. NULL
  276. };
  277. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  278. const int **nids, int nid)
  279. {
  280. if (!cipher) {
  281. /* We are returning a list of supported nids */
  282. *nids = test_cipher_nids;
  283. return test_cipher_nids_number;
  284. }
  285. /* We are being asked for a specific cipher */
  286. if (nid == NID_rc4)
  287. *cipher = &test_r4_cipher;
  288. else if (nid == NID_rc4_40)
  289. *cipher = &test_r4_40_cipher;
  290. else {
  291. # ifdef TEST_ENG_OPENSSL_RC4_OTHERS
  292. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
  293. "nid %d\n", nid);
  294. # endif
  295. *cipher = NULL;
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. #endif
  301. #ifdef TEST_ENG_OPENSSL_SHA
  302. /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
  303. # include <openssl/sha.h>
  304. static int test_digest_nids[] = { NID_sha1 };
  305. static int test_digest_nids_number = 1;
  306. static int test_sha1_init(EVP_MD_CTX *ctx)
  307. {
  308. # ifdef TEST_ENG_OPENSSL_SHA_P_INIT
  309. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
  310. # endif
  311. return SHA1_Init(ctx->md_data);
  312. }
  313. static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
  314. {
  315. # ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
  316. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
  317. # endif
  318. return SHA1_Update(ctx->md_data, data, count);
  319. }
  320. static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
  321. {
  322. # ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
  323. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
  324. # endif
  325. return SHA1_Final(md, ctx->md_data);
  326. }
  327. static const EVP_MD test_sha_md = {
  328. NID_sha1,
  329. NID_sha1WithRSAEncryption,
  330. SHA_DIGEST_LENGTH,
  331. 0,
  332. test_sha1_init,
  333. test_sha1_update,
  334. test_sha1_final,
  335. NULL,
  336. NULL,
  337. EVP_PKEY_RSA_method,
  338. SHA_CBLOCK,
  339. sizeof(EVP_MD *) + sizeof(SHA_CTX),
  340. };
  341. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  342. const int **nids, int nid)
  343. {
  344. if (!digest) {
  345. /* We are returning a list of supported nids */
  346. *nids = test_digest_nids;
  347. return test_digest_nids_number;
  348. }
  349. /* We are being asked for a specific digest */
  350. if (nid == NID_sha1)
  351. *digest = &test_sha_md;
  352. else {
  353. # ifdef TEST_ENG_OPENSSL_SHA_OTHERS
  354. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
  355. "nid %d\n", nid);
  356. # endif
  357. *digest = NULL;
  358. return 0;
  359. }
  360. return 1;
  361. }
  362. #endif
  363. #ifdef TEST_ENG_OPENSSL_PKEY
  364. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  365. UI_METHOD *ui_method,
  366. void *callback_data)
  367. {
  368. BIO *in;
  369. EVP_PKEY *key;
  370. fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
  371. key_id);
  372. in = BIO_new_file(key_id, "r");
  373. if (!in)
  374. return NULL;
  375. key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
  376. BIO_free(in);
  377. return key;
  378. }
  379. #endif