Implement failover for ubsec. Submitted by Subramanian Ramamoorthy
[openssl.git] / crypto / engine / eng_openssl.c
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
60 #include <stdio.h>
61 #include <openssl/crypto.h>
62 #include "cryptlib.h"
63 #include <openssl/engine.h>
64 #include <openssl/dso.h>
65
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_ENG_OPENSSL_RC4_OTHERS */
71 #define TEST_ENG_OPENSSL_RC4_P_INIT
72 /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
73 #define TEST_ENG_OPENSSL_SHA
74 /* #define TEST_ENG_OPENSSL_SHA_OTHERS */
75 /* #define TEST_ENG_OPENSSL_SHA_P_INIT */
76 /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
77 /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
78
79 #ifdef TEST_ENG_OPENSSL_RC4
80 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
81                                 const int **nids, int nid);
82 #endif
83 #ifdef TEST_ENG_OPENSSL_SHA
84 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
85                                 const int **nids, int nid);
86 #endif
87
88 /* The constants used when creating the ENGINE */
89 static const char *engine_openssl_id = "openssl";
90 static const char *engine_openssl_name = "Software engine support";
91
92 /* This internal function is used by ENGINE_openssl() and possibly by the
93  * "dynamic" ENGINE support too */
94 static int bind_helper(ENGINE *e)
95         {
96         if(!ENGINE_set_id(e, engine_openssl_id)
97                         || !ENGINE_set_name(e, engine_openssl_name)
98 #ifndef OPENSSL_NO_RSA
99                         || !ENGINE_set_RSA(e, RSA_get_default_method())
100 #endif
101 #ifndef OPENSSL_NO_DSA
102                         || !ENGINE_set_DSA(e, DSA_get_default_method())
103 #endif
104 #ifndef OPENSSL_NO_DH
105                         || !ENGINE_set_DH(e, DH_get_default_method())
106 #endif
107                         || !ENGINE_set_RAND(e, RAND_SSLeay())
108 #ifdef TEST_ENG_OPENSSL_RC4
109                         || !ENGINE_set_ciphers(e, openssl_ciphers)
110 #endif
111 #ifdef TEST_ENG_OPENSSL_SHA
112                         || !ENGINE_set_digests(e, openssl_digests)
113 #endif
114                         )
115                 return 0;
116         /* If we add errors to this ENGINE, ensure the error handling is setup here */
117         /* openssl_load_error_strings(); */
118         return 1;
119         }
120
121 static ENGINE *engine_openssl(void)
122         {
123         ENGINE *ret = ENGINE_new();
124         if(!ret)
125                 return NULL;
126         if(!bind_helper(ret))
127                 {
128                 ENGINE_free(ret);
129                 return NULL;
130                 }
131         return ret;
132         }
133
134 void ENGINE_load_openssl(void)
135         {
136         ENGINE *toadd = engine_openssl();
137         if(!toadd) return;
138         ENGINE_add(toadd);
139         /* If the "add" worked, it gets a structural reference. So either way,
140          * we release our just-created reference. */
141         ENGINE_free(toadd);
142         ERR_clear_error();
143         }
144
145 /* This stuff is needed if this ENGINE is being compiled into a self-contained
146  * shared-library. */
147 #ifdef ENGINE_DYNAMIC_SUPPORT
148 static int bind_fn(ENGINE *e, const char *id)
149         {
150         if(id && (strcmp(id, engine_openssl_id) != 0))
151                 return 0;
152         if(!bind_helper(e))
153                 return 0;
154         return 1;
155         }
156 IMPLEMENT_DYNAMIC_CHECK_FN()
157 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
158 #endif /* ENGINE_DYNAMIC_SUPPORT */
159
160 #ifdef TEST_ENG_OPENSSL_RC4
161 /* This section of code compiles an "alternative implementation" of two modes of
162  * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
163  * should under normal circumstances go via this support rather than the default
164  * EVP support. There are other symbols to tweak the testing;
165  *    TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
166  *        we're asked for a cipher we don't support (should not happen).
167  *    TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
168  *        the "init_key" handler is called.
169  *    TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
170  */
171 #include <openssl/evp.h>
172 #include <openssl/rc4.h>
173 #define TEST_RC4_KEY_SIZE               16
174 static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
175 static int test_cipher_nids_number = 2;
176 typedef struct {
177         unsigned char key[TEST_RC4_KEY_SIZE];
178         RC4_KEY ks;
179         } TEST_RC4_KEY;
180 #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
181 static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
182                         const unsigned char *iv, int enc)
183         {
184 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
185         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
186 #endif
187         memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
188         RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
189                 test(ctx)->key);
190         return 1;
191         }
192 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
193                       const unsigned char *in, unsigned int inl)
194         {
195 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
196         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
197 #endif
198         RC4(&test(ctx)->ks,inl,in,out);
199         return 1;
200         }
201 static const EVP_CIPHER test_r4_cipher=
202         {
203         NID_rc4,
204         1,TEST_RC4_KEY_SIZE,0,
205         EVP_CIPH_VARIABLE_LENGTH,
206         test_rc4_init_key,
207         test_rc4_cipher,
208         NULL,
209         sizeof(TEST_RC4_KEY),
210         NULL,
211         NULL,
212         NULL
213         };
214 static const EVP_CIPHER test_r4_40_cipher=
215         {
216         NID_rc4_40,
217         1,5 /* 40 bit */,0,
218         EVP_CIPH_VARIABLE_LENGTH,
219         test_rc4_init_key,
220         test_rc4_cipher,
221         NULL,
222         sizeof(TEST_RC4_KEY),
223         NULL, 
224         NULL,
225         NULL
226         };
227 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
228                         const int **nids, int nid)
229         {
230         if(!cipher)
231                 {
232                 /* We are returning a list of supported nids */
233                 *nids = test_cipher_nids;
234                 return test_cipher_nids_number;
235                 }
236         /* We are being asked for a specific cipher */
237         if(nid == NID_rc4)
238                 *cipher = &test_r4_cipher;
239         else if(nid == NID_rc4_40)
240                 *cipher = &test_r4_40_cipher;
241         else
242                 {
243 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
244                 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
245                                 "nid %d\n", nid);
246 #endif
247                 *cipher = NULL;
248                 return 0;
249                 }
250         return 1;
251         }
252 #endif
253
254 #ifdef TEST_ENG_OPENSSL_SHA
255 /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
256 #include <openssl/evp.h>
257 #include <openssl/sha.h>
258 static int test_digest_nids[] = {NID_sha1};
259 static int test_digest_nids_number = 1;
260 static int test_sha1_init(EVP_MD_CTX *ctx)
261         {
262 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
263         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
264 #endif
265         return SHA1_Init(ctx->md_data);
266         }
267 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
268         {
269 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
270         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
271 #endif
272         return SHA1_Update(ctx->md_data,data,count);
273         }
274 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
275         {
276 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
277         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
278 #endif
279         return SHA1_Final(md,ctx->md_data);
280         }
281 static const EVP_MD test_sha_md=
282         {
283         NID_sha1,
284         NID_sha1WithRSAEncryption,
285         SHA_DIGEST_LENGTH,
286         0,
287         test_sha1_init,
288         test_sha1_update,
289         test_sha1_final,
290         NULL,
291         NULL,
292         EVP_PKEY_RSA_method,
293         SHA_CBLOCK,
294         sizeof(EVP_MD *)+sizeof(SHA_CTX),
295         };
296 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
297                         const int **nids, int nid)
298         {
299         if(!digest)
300                 {
301                 /* We are returning a list of supported nids */
302                 *nids = test_digest_nids;
303                 return test_digest_nids_number;
304                 }
305         /* We are being asked for a specific digest */
306         if(nid == NID_sha1)
307                 *digest = &test_sha_md;
308         else
309                 {
310 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
311                 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
312                                 "nid %d\n", nid);
313 #endif
314                 *digest = NULL;
315                 return 0;
316                 }
317         return 1;
318         }
319 #endif