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