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