eng_openssl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /* crypto/engine/eng_openssl.c */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2001 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. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * ECDH support in OpenSSL originally developed by
  61. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  62. */
  63. #include <stdio.h>
  64. #include <openssl/crypto.h>
  65. #include "cryptlib.h"
  66. #include <openssl/engine.h>
  67. #include <openssl/dso.h>
  68. #include <openssl/pem.h>
  69. #include <openssl/evp.h>
  70. #include <openssl/rand.h>
  71. #ifndef OPENSSL_NO_RSA
  72. #include <openssl/rsa.h>
  73. #endif
  74. #ifndef OPENSSL_NO_DSA
  75. #include <openssl/dsa.h>
  76. #endif
  77. #ifndef OPENSSL_NO_DH
  78. #include <openssl/dh.h>
  79. #endif
  80. /* This testing gunk is implemented (and explained) lower down. It also assumes
  81. * the application explicitly calls "ENGINE_load_openssl()" because this is no
  82. * longer automatic in ENGINE_load_builtin_engines(). */
  83. #define TEST_ENG_OPENSSL_RC4
  84. #define TEST_ENG_OPENSSL_PKEY
  85. /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
  86. #define TEST_ENG_OPENSSL_RC4_P_INIT
  87. /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
  88. #define TEST_ENG_OPENSSL_SHA
  89. /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
  90. /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
  91. /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
  92. /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
  93. /* Now check what of those algorithms are actually enabled */
  94. #ifdef OPENSSL_NO_RC4
  95. #undef TEST_ENG_OPENSSL_RC4
  96. #undef TEST_ENG_OPENSSL_RC4_OTHERS
  97. #undef TEST_ENG_OPENSSL_RC4_P_INIT
  98. #undef TEST_ENG_OPENSSL_RC4_P_CIPHER
  99. #endif
  100. #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1)
  101. #undef TEST_ENG_OPENSSL_SHA
  102. #undef TEST_ENG_OPENSSL_SHA_OTHERS
  103. #undef TEST_ENG_OPENSSL_SHA_P_INIT
  104. #undef TEST_ENG_OPENSSL_SHA_P_UPDATE
  105. #undef TEST_ENG_OPENSSL_SHA_P_FINAL
  106. #endif
  107. #ifdef TEST_ENG_OPENSSL_RC4
  108. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  109. const int **nids, int nid);
  110. #endif
  111. #ifdef TEST_ENG_OPENSSL_SHA
  112. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  113. const int **nids, int nid);
  114. #endif
  115. #ifdef TEST_ENG_OPENSSL_PKEY
  116. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  117. UI_METHOD *ui_method, void *callback_data);
  118. #endif
  119. /* The constants used when creating the ENGINE */
  120. static const char *engine_openssl_id = "openssl";
  121. static const char *engine_openssl_name = "Software engine support";
  122. /* This internal function is used by ENGINE_openssl() and possibly by the
  123. * "dynamic" ENGINE support too */
  124. static int bind_helper(ENGINE *e)
  125. {
  126. if(!ENGINE_set_id(e, engine_openssl_id)
  127. || !ENGINE_set_name(e, engine_openssl_name)
  128. #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
  129. #ifndef OPENSSL_NO_RSA
  130. || !ENGINE_set_RSA(e, RSA_get_default_method())
  131. #endif
  132. #ifndef OPENSSL_NO_DSA
  133. || !ENGINE_set_DSA(e, DSA_get_default_method())
  134. #endif
  135. #ifndef OPENSSL_NO_ECDH
  136. || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
  137. #endif
  138. #ifndef OPENSSL_NO_ECDSA
  139. || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())
  140. #endif
  141. #ifndef OPENSSL_NO_DH
  142. || !ENGINE_set_DH(e, DH_get_default_method())
  143. #endif
  144. || !ENGINE_set_RAND(e, RAND_SSLeay())
  145. #ifdef TEST_ENG_OPENSSL_RC4
  146. || !ENGINE_set_ciphers(e, openssl_ciphers)
  147. #endif
  148. #ifdef TEST_ENG_OPENSSL_SHA
  149. || !ENGINE_set_digests(e, openssl_digests)
  150. #endif
  151. #endif
  152. #ifdef TEST_ENG_OPENSSL_PKEY
  153. || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
  154. #endif
  155. )
  156. return 0;
  157. /* If we add errors to this ENGINE, ensure the error handling is setup here */
  158. /* openssl_load_error_strings(); */
  159. return 1;
  160. }
  161. static ENGINE *engine_openssl(void)
  162. {
  163. ENGINE *ret = ENGINE_new();
  164. if(!ret)
  165. return NULL;
  166. if(!bind_helper(ret))
  167. {
  168. ENGINE_free(ret);
  169. return NULL;
  170. }
  171. return ret;
  172. }
  173. void ENGINE_load_openssl(void)
  174. {
  175. ENGINE *toadd = engine_openssl();
  176. if(!toadd) return;
  177. ENGINE_add(toadd);
  178. /* If the "add" worked, it gets a structural reference. So either way,
  179. * we release our just-created reference. */
  180. ENGINE_free(toadd);
  181. ERR_clear_error();
  182. }
  183. /* This stuff is needed if this ENGINE is being compiled into a self-contained
  184. * shared-library. */
  185. #ifdef ENGINE_DYNAMIC_SUPPORT
  186. static int bind_fn(ENGINE *e, const char *id)
  187. {
  188. if(id && (strcmp(id, engine_openssl_id) != 0))
  189. return 0;
  190. if(!bind_helper(e))
  191. return 0;
  192. return 1;
  193. }
  194. IMPLEMENT_DYNAMIC_CHECK_FN()
  195. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  196. #endif /* ENGINE_DYNAMIC_SUPPORT */
  197. #ifdef TEST_ENG_OPENSSL_RC4
  198. /* This section of code compiles an "alternative implementation" of two modes of
  199. * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
  200. * should under normal circumstances go via this support rather than the default
  201. * EVP support. There are other symbols to tweak the testing;
  202. * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
  203. * we're asked for a cipher we don't support (should not happen).
  204. * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
  205. * the "init_key" handler is called.
  206. * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
  207. */
  208. #include <openssl/rc4.h>
  209. #define TEST_RC4_KEY_SIZE 16
  210. static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
  211. static int test_cipher_nids_number = 2;
  212. typedef struct {
  213. unsigned char key[TEST_RC4_KEY_SIZE];
  214. RC4_KEY ks;
  215. } TEST_RC4_KEY;
  216. #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
  217. static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  218. const unsigned char *iv, int enc)
  219. {
  220. #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
  221. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
  222. #endif
  223. memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
  224. RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
  225. test(ctx)->key);
  226. return 1;
  227. }
  228. static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  229. const unsigned char *in, unsigned int inl)
  230. {
  231. #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
  232. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
  233. #endif
  234. RC4(&test(ctx)->ks,inl,in,out);
  235. return 1;
  236. }
  237. static const EVP_CIPHER test_r4_cipher=
  238. {
  239. NID_rc4,
  240. 1,TEST_RC4_KEY_SIZE,0,
  241. EVP_CIPH_VARIABLE_LENGTH,
  242. test_rc4_init_key,
  243. test_rc4_cipher,
  244. NULL,
  245. sizeof(TEST_RC4_KEY),
  246. NULL,
  247. NULL,
  248. NULL,
  249. NULL
  250. };
  251. static const EVP_CIPHER test_r4_40_cipher=
  252. {
  253. NID_rc4_40,
  254. 1,5 /* 40 bit */,0,
  255. EVP_CIPH_VARIABLE_LENGTH,
  256. test_rc4_init_key,
  257. test_rc4_cipher,
  258. NULL,
  259. sizeof(TEST_RC4_KEY),
  260. NULL,
  261. NULL,
  262. NULL,
  263. NULL
  264. };
  265. static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
  266. const int **nids, int nid)
  267. {
  268. if(!cipher)
  269. {
  270. /* We are returning a list of supported nids */
  271. *nids = test_cipher_nids;
  272. return test_cipher_nids_number;
  273. }
  274. /* We are being asked for a specific cipher */
  275. if(nid == NID_rc4)
  276. *cipher = &test_r4_cipher;
  277. else if(nid == NID_rc4_40)
  278. *cipher = &test_r4_40_cipher;
  279. else
  280. {
  281. #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
  282. fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
  283. "nid %d\n", nid);
  284. #endif
  285. *cipher = NULL;
  286. return 0;
  287. }
  288. return 1;
  289. }
  290. #endif
  291. #ifdef TEST_ENG_OPENSSL_SHA
  292. /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
  293. #include <openssl/sha.h>
  294. static int test_digest_nids[] = {NID_sha1};
  295. static int test_digest_nids_number = 1;
  296. static int test_sha1_init(EVP_MD_CTX *ctx)
  297. {
  298. #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
  299. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
  300. #endif
  301. return SHA1_Init(ctx->md_data);
  302. }
  303. static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,size_t count)
  304. {
  305. #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
  306. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
  307. #endif
  308. return SHA1_Update(ctx->md_data,data,count);
  309. }
  310. static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
  311. {
  312. #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
  313. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
  314. #endif
  315. return SHA1_Final(md,ctx->md_data);
  316. }
  317. static const EVP_MD test_sha_md=
  318. {
  319. NID_sha1,
  320. NID_sha1WithRSAEncryption,
  321. SHA_DIGEST_LENGTH,
  322. 0,
  323. test_sha1_init,
  324. test_sha1_update,
  325. test_sha1_final,
  326. NULL,
  327. NULL,
  328. EVP_PKEY_RSA_method,
  329. SHA_CBLOCK,
  330. sizeof(EVP_MD *)+sizeof(SHA_CTX),
  331. };
  332. static int openssl_digests(ENGINE *e, const EVP_MD **digest,
  333. const int **nids, int nid)
  334. {
  335. if(!digest)
  336. {
  337. /* We are returning a list of supported nids */
  338. *nids = test_digest_nids;
  339. return test_digest_nids_number;
  340. }
  341. /* We are being asked for a specific digest */
  342. if(nid == NID_sha1)
  343. *digest = &test_sha_md;
  344. else
  345. {
  346. #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
  347. fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
  348. "nid %d\n", nid);
  349. #endif
  350. *digest = NULL;
  351. return 0;
  352. }
  353. return 1;
  354. }
  355. #endif
  356. #ifdef TEST_ENG_OPENSSL_PKEY
  357. static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
  358. UI_METHOD *ui_method, void *callback_data)
  359. {
  360. BIO *in;
  361. EVP_PKEY *key;
  362. fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id);
  363. in = BIO_new_file(key_id, "r");
  364. if (!in)
  365. return NULL;
  366. key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
  367. BIO_free(in);
  368. return key;
  369. }
  370. #endif