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