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>
66 /* This testing gunk is implemented (and explained) lower down. It also assumes
67 * the application explicitly calls "ENGINE_load_openssl()" because this is no
68 * longer automatic in ENGINE_load_builtin_engines(). */
69 #define TEST_ENG_OPENSSL_RC4
70 /* #define TEST_ENC_OPENSSL_RC4_FALLBACK */
71 /* #define TEST_ENG_OPENSSL_RC4_OTHERS */
72 #define TEST_ENG_OPENSSL_RC4_P_INIT
73 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
74 #define TEST_ENG_OPENSSL_SHA
75 /* #define TEST_ENG_OPENSSL_SHA_FALLBACK */
76 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
77 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
78 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
79 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
81 #ifdef TEST_ENG_OPENSSL_RC4
82 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
83 const int **nids, int nid);
85 #ifdef TEST_ENG_OPENSSL_SHA
86 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
87 const int **nids, int nid);
90 /* The constants used when creating the ENGINE */
91 static const char *engine_openssl_id = "openssl";
92 static const char *engine_openssl_name = "Software engine support";
94 static ENGINE *engine_openssl(void)
96 ENGINE *ret = ENGINE_new();
99 if(!ENGINE_set_id(ret, engine_openssl_id)
100 || !ENGINE_set_name(ret, engine_openssl_name)
101 #ifndef OPENSSL_NO_RSA
102 || !ENGINE_set_RSA(ret, RSA_get_default_method())
104 #ifndef OPENSSL_NO_DSA
105 || !ENGINE_set_DSA(ret, DSA_get_default_method())
107 #ifndef OPENSSL_NO_DH
108 || !ENGINE_set_DH(ret, DH_get_default_method())
110 || !ENGINE_set_RAND(ret, RAND_SSLeay())
111 #ifdef TEST_ENG_OPENSSL_RC4
112 || !ENGINE_set_ciphers(ret, openssl_ciphers)
114 #ifdef TEST_ENG_OPENSSL_SHA
115 || !ENGINE_set_digests(ret, openssl_digests)
125 void ENGINE_load_openssl(void)
127 ENGINE *toadd = engine_openssl();
130 /* If the "add" worked, it gets a structural reference. So either way,
131 * we release our just-created reference. */
136 #ifdef TEST_ENG_OPENSSL_RC4
137 /* This section of code compiles an "alternative implementation" of two modes of
138 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
139 * should under normal circumstances go via this support rather than the default
140 * EVP support. There are other symbols to tweak the testing;
141 * TEST_ENC_OPENSSL_RC4_FALLBACK - declare support for "-1" so that all
142 * uncached cipher lookups check with this ENGINE (ie. it'll get asked
143 * about other ciphers, but hopefully not more than once for each nid).
144 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
145 * we're asked for a cipher we don't support (should only happen in
146 * combination with the "FALLBACK" case).
147 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
148 * the "init_key" handler is called.
149 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
151 #include <openssl/evp.h>
152 #include <openssl/rc4.h>
153 #define TEST_RC4_KEY_SIZE 16
154 #ifdef TEST_ENC_OPENSSL_RC4_FALLBACK
155 static int test_cipher_nids[] = {-1};
156 static int test_cipher_nids_number = 1;
158 static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
159 static int test_cipher_nids_number = 2;
162 unsigned char key[TEST_RC4_KEY_SIZE];
165 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
166 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
167 const unsigned char *iv, int enc)
169 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
170 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
172 memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
173 RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
177 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
178 const unsigned char *in, unsigned int inl)
180 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
181 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
183 RC4(&test(ctx)->ks,inl,in,out);
186 static const EVP_CIPHER test_r4_cipher=
189 1,TEST_RC4_KEY_SIZE,0,
190 EVP_CIPH_VARIABLE_LENGTH,
194 sizeof(TEST_RC4_KEY),
199 static const EVP_CIPHER test_r4_40_cipher=
203 EVP_CIPH_VARIABLE_LENGTH,
207 sizeof(TEST_RC4_KEY),
212 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
213 const int **nids, int nid)
217 /* We are returning a list of supported nids */
218 *nids = test_cipher_nids;
219 return test_cipher_nids_number;
221 /* We are being asked for a specific cipher */
223 *cipher = &test_r4_cipher;
224 else if(nid == NID_rc4_40)
225 *cipher = &test_r4_40_cipher;
228 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
229 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
239 #ifdef TEST_ENG_OPENSSL_SHA
240 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
241 #include <openssl/evp.h>
242 #include <openssl/sha.h>
243 #ifdef TEST_ENC_OPENSSL_SHA_FALLBACK
244 static int test_digest_nids[] = {-1};
245 static int test_digest_nids_number = 1;
247 static int test_digest_nids[] = {NID_sha1};
248 static int test_digest_nids_number = 1;
250 static int test_sha1_init(EVP_MD_CTX *ctx)
252 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
253 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
255 return SHA1_Init(ctx->md_data);
257 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
259 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
260 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
262 return SHA1_Update(ctx->md_data,data,count);
264 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
266 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
267 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
269 return SHA1_Final(md,ctx->md_data);
271 static const EVP_MD test_sha_md=
274 NID_sha1WithRSAEncryption,
284 sizeof(EVP_MD *)+sizeof(SHA_CTX),
286 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
287 const int **nids, int nid)
291 /* We are returning a list of supported nids */
292 *nids = test_digest_nids;
293 return test_digest_nids_number;
295 /* We are being asked for a specific digest */
297 *digest = &test_sha_md;
300 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
301 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "