d2873a6aabfccacaa0ed54b4c458a449ac86c159
[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_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 */
80
81 #ifdef TEST_ENG_OPENSSL_RC4
82 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
83                                 const int **nids, int nid);
84 #endif
85 #ifdef TEST_ENG_OPENSSL_SHA
86 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
87                                 const int **nids, int nid);
88 #endif
89
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";
93
94 static ENGINE *engine_openssl(void)
95         {
96         ENGINE *ret = ENGINE_new();
97         if(!ret)
98                 return NULL;
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())
103 #endif
104 #ifndef OPENSSL_NO_DSA
105                         || !ENGINE_set_DSA(ret, DSA_get_default_method())
106 #endif
107 #ifndef OPENSSL_NO_DH
108                         || !ENGINE_set_DH(ret, DH_get_default_method())
109 #endif
110                         || !ENGINE_set_RAND(ret, RAND_SSLeay())
111 #ifdef TEST_ENG_OPENSSL_RC4
112                         || !ENGINE_set_ciphers(ret, openssl_ciphers)
113 #endif
114 #ifdef TEST_ENG_OPENSSL_SHA
115                         || !ENGINE_set_digests(ret, openssl_digests)
116 #endif
117                         )
118                 {
119                 ENGINE_free(ret);
120                 return NULL;
121                 }
122         return ret;
123         }
124
125 void ENGINE_load_openssl(void)
126         {
127         ENGINE *toadd = engine_openssl();
128         if(!toadd) return;
129         ENGINE_add(toadd);
130         /* If the "add" worked, it gets a structural reference. So either way,
131          * we release our just-created reference. */
132         ENGINE_free(toadd);
133         ERR_clear_error();
134         }
135
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.
150  */
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;
157 #else
158 static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
159 static int test_cipher_nids_number = 2;
160 #endif
161 typedef struct {
162         unsigned char key[TEST_RC4_KEY_SIZE];
163         RC4_KEY ks;
164         } TEST_RC4_KEY;
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)
168         {
169 #ifdef TEST_ENG_OPENSSL_RC4_P_INIT
170         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
171 #endif
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),
174                 test(ctx)->key);
175         return 1;
176         }
177 static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
178                       const unsigned char *in, unsigned int inl)
179         {
180 #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
181         fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
182 #endif
183         RC4(&test(ctx)->ks,inl,in,out);
184         return 1;
185         }
186 static const EVP_CIPHER test_r4_cipher=
187         {
188         NID_rc4,
189         1,TEST_RC4_KEY_SIZE,0,
190         EVP_CIPH_VARIABLE_LENGTH,
191         test_rc4_init_key,
192         test_rc4_cipher,
193         NULL,
194         sizeof(TEST_RC4_KEY),
195         NULL,
196         NULL,
197         NULL
198         };
199 static const EVP_CIPHER test_r4_40_cipher=
200         {
201         NID_rc4_40,
202         1,5 /* 40 bit */,0,
203         EVP_CIPH_VARIABLE_LENGTH,
204         test_rc4_init_key,
205         test_rc4_cipher,
206         NULL,
207         sizeof(TEST_RC4_KEY),
208         NULL, 
209         NULL,
210         NULL
211         };
212 static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
213                         const int **nids, int nid)
214         {
215         if(!cipher)
216                 {
217                 /* We are returning a list of supported nids */
218                 *nids = test_cipher_nids;
219                 return test_cipher_nids_number;
220                 }
221         /* We are being asked for a specific cipher */
222         if(nid == NID_rc4)
223                 *cipher = &test_r4_cipher;
224         else if(nid == NID_rc4_40)
225                 *cipher = &test_r4_40_cipher;
226         else
227                 {
228 #ifdef TEST_ENG_OPENSSL_RC4_OTHERS
229                 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
230                                 "nid %d\n", nid);
231 #endif
232                 *cipher = NULL;
233                 return 0;
234                 }
235         return 1;
236         }
237 #endif
238
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;
246 #else
247 static int test_digest_nids[] = {NID_sha1};
248 static int test_digest_nids_number = 1;
249 #endif
250 static int test_sha1_init(EVP_MD_CTX *ctx)
251         {
252 #ifdef TEST_ENG_OPENSSL_SHA_P_INIT
253         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
254 #endif
255         return SHA1_Init(ctx->md_data);
256         }
257 static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
258         {
259 #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
260         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
261 #endif
262         return SHA1_Update(ctx->md_data,data,count);
263         }
264 static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
265         {
266 #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
267         fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
268 #endif
269         return SHA1_Final(md,ctx->md_data);
270         }
271 static const EVP_MD test_sha_md=
272         {
273         NID_sha1,
274         NID_sha1WithRSAEncryption,
275         SHA_DIGEST_LENGTH,
276         0,
277         test_sha1_init,
278         test_sha1_update,
279         test_sha1_final,
280         NULL,
281         NULL,
282         EVP_PKEY_RSA_method,
283         SHA_CBLOCK,
284         sizeof(EVP_MD *)+sizeof(SHA_CTX),
285         };
286 static int openssl_digests(ENGINE *e, const EVP_MD **digest,
287                         const int **nids, int nid)
288         {
289         if(!digest)
290                 {
291                 /* We are returning a list of supported nids */
292                 *nids = test_digest_nids;
293                 return test_digest_nids_number;
294                 }
295         /* We are being asked for a specific digest */
296         if(nid == NID_sha1)
297                 *digest = &test_sha_md;
298         else
299                 {
300 #ifdef TEST_ENG_OPENSSL_SHA_OTHERS
301                 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
302                                 "nid %d\n", nid);
303 #endif
304                 *digest = NULL;
305                 return 0;
306                 }
307         return 1;
308         }
309 #endif