1 /* crypto/engine/eng_openssl.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <openssl/crypto.h>
63 #include <openssl/engine.h>
64 #include <openssl/dso.h>
65 #include <openssl/pem.h>
66 #include <openssl/evp.h>
68 /* This testing gunk is implemented (and explained) lower down. It also assumes
69 * the application explicitly calls "ENGINE_load_openssl()" because this is no
70 * longer automatic in ENGINE_load_builtin_engines(). */
71 #define TEST_ENG_OPENSSL_RC4
72 #define TEST_ENG_OPENSSL_PKEY
73 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
74 #define TEST_ENG_OPENSSL_RC4_P_INIT
75 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
76 #define TEST_ENG_OPENSSL_SHA
77 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
78 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
79 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
80 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
82 /* Now check what of those algorithms are actually enabled */
84 #undef TEST_ENG_OPENSSL_RC4
85 #undef TEST_ENG_OPENSSL_RC4_OTHERS
86 #undef TEST_ENG_OPENSSL_RC4_P_INIT
87 #undef TEST_ENG_OPENSSL_RC4_P_CIPHER
89 #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1)
90 #undef TEST_ENG_OPENSSL_SHA
91 #undef TEST_ENG_OPENSSL_SHA_OTHERS
92 #undef TEST_ENG_OPENSSL_SHA_P_INIT
93 #undef TEST_ENG_OPENSSL_SHA_P_UPDATE
94 #undef TEST_ENG_OPENSSL_SHA_P_FINAL
97 #ifdef TEST_ENG_OPENSSL_RC4
98 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
99 const int **nids, int nid);
101 #ifdef TEST_ENG_OPENSSL_SHA
102 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
103 const int **nids, int nid);
106 #ifdef TEST_ENG_OPENSSL_PKEY
107 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
108 UI_METHOD *ui_method, void *callback_data);
111 /* The constants used when creating the ENGINE */
112 static const char *engine_openssl_id = "openssl";
113 static const char *engine_openssl_name = "Software engine support";
115 /* This internal function is used by ENGINE_openssl() and possibly by the
116 * "dynamic" ENGINE support too */
117 static int bind_helper(ENGINE *e)
119 if(!ENGINE_set_id(e, engine_openssl_id)
120 || !ENGINE_set_name(e, engine_openssl_name)
121 #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
122 #ifndef OPENSSL_NO_RSA
123 || !ENGINE_set_RSA(e, RSA_get_default_method())
125 #ifndef OPENSSL_NO_DSA
126 || !ENGINE_set_DSA(e, DSA_get_default_method())
128 #ifndef OPENSSL_NO_DH
129 || !ENGINE_set_DH(e, DH_get_default_method())
131 || !ENGINE_set_RAND(e, RAND_SSLeay())
132 #ifdef TEST_ENG_OPENSSL_RC4
133 || !ENGINE_set_ciphers(e, openssl_ciphers)
135 #ifdef TEST_ENG_OPENSSL_SHA
136 || !ENGINE_set_digests(e, openssl_digests)
139 #ifdef TEST_ENG_OPENSSL_PKEY
140 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
144 /* If we add errors to this ENGINE, ensure the error handling is setup here */
145 /* openssl_load_error_strings(); */
149 static ENGINE *engine_openssl(void)
151 ENGINE *ret = ENGINE_new();
154 if(!bind_helper(ret))
162 void ENGINE_load_openssl(void)
164 ENGINE *toadd = engine_openssl();
167 /* If the "add" worked, it gets a structural reference. So either way,
168 * we release our just-created reference. */
173 /* This stuff is needed if this ENGINE is being compiled into a self-contained
175 #ifdef ENGINE_DYNAMIC_SUPPORT
176 static int bind_fn(ENGINE *e, const char *id)
178 if(id && (strcmp(id, engine_openssl_id) != 0))
184 IMPLEMENT_DYNAMIC_CHECK_FN()
185 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
186 #endif /* ENGINE_DYNAMIC_SUPPORT */
188 #ifdef TEST_ENG_OPENSSL_RC4
189 /* This section of code compiles an "alternative implementation" of two modes of
190 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
191 * should under normal circumstances go via this support rather than the default
192 * EVP support. There are other symbols to tweak the testing;
193 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
194 * we're asked for a cipher we don't support (should not happen).
195 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
196 * the "init_key" handler is called.
197 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
199 #include <openssl/rc4.h>
200 #define TEST_RC4_KEY_SIZE 16
201 static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
202 static int test_cipher_nids_number = 2;
204 unsigned char key[TEST_RC4_KEY_SIZE];
207 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
208 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
209 const unsigned char *iv, int enc)
211 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
212 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
214 memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
215 RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
219 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
220 const unsigned char *in, unsigned int inl)
222 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
223 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
225 RC4(&test(ctx)->ks,inl,in,out);
228 static const EVP_CIPHER test_r4_cipher=
231 1,TEST_RC4_KEY_SIZE,0,
232 EVP_CIPH_VARIABLE_LENGTH,
236 sizeof(TEST_RC4_KEY),
241 static const EVP_CIPHER test_r4_40_cipher=
245 EVP_CIPH_VARIABLE_LENGTH,
249 sizeof(TEST_RC4_KEY),
254 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
255 const int **nids, int nid)
259 /* We are returning a list of supported nids */
260 *nids = test_cipher_nids;
261 return test_cipher_nids_number;
263 /* We are being asked for a specific cipher */
265 *cipher = &test_r4_cipher;
266 else if(nid == NID_rc4_40)
267 *cipher = &test_r4_40_cipher;
270 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
271 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
281 #ifdef TEST_ENG_OPENSSL_SHA
282 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
283 #include <openssl/sha.h>
284 static int test_digest_nids[] = {NID_sha1};
285 static int test_digest_nids_number = 1;
286 static int test_sha1_init(EVP_MD_CTX *ctx)
288 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
289 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
291 return SHA1_Init(ctx->md_data);
293 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
295 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
296 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
298 return SHA1_Update(ctx->md_data,data,count);
300 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
302 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
303 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
305 return SHA1_Final(md,ctx->md_data);
307 static const EVP_MD test_sha_md=
310 NID_sha1WithRSAEncryption,
320 sizeof(EVP_MD *)+sizeof(SHA_CTX),
322 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
323 const int **nids, int nid)
327 /* We are returning a list of supported nids */
328 *nids = test_digest_nids;
329 return test_digest_nids_number;
331 /* We are being asked for a specific digest */
333 *digest = &test_sha_md;
336 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
337 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
347 #ifdef TEST_ENG_OPENSSL_PKEY
348 static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
349 UI_METHOD *ui_method, void *callback_data)
353 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id);
354 in = BIO_new_file(key_id, "r");
357 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);