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