]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/engine/eng_openssl.c
'flags' should only be set inside DSO_load() if constructing a new DSO
[thirdparty/openssl.git] / crypto / engine / eng_openssl.c
CommitLineData
2b671586 1/* crypto/engine/eng_openssl.c */
5270e702
RL
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
2b671586 6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
5270e702
RL
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"
5270e702
RL
63#include <openssl/engine.h>
64#include <openssl/dso.h>
5270e702 65
3b04cdd7
GT
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
3b04cdd7
GT
70/* #define TEST_ENG_OPENSSL_RC4_OTHERS */
71#define TEST_ENG_OPENSSL_RC4_P_INIT
72/* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */
73#define TEST_ENG_OPENSSL_SHA
3b04cdd7
GT
74/* #define TEST_ENG_OPENSSL_SHA_OTHERS */
75/* #define TEST_ENG_OPENSSL_SHA_P_INIT */
76/* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */
77/* #define TEST_ENG_OPENSSL_SHA_P_FINAL */
78
79#ifdef TEST_ENG_OPENSSL_RC4
80static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
81 const int **nids, int nid);
82#endif
83#ifdef TEST_ENG_OPENSSL_SHA
84static int openssl_digests(ENGINE *e, const EVP_MD **digest,
85 const int **nids, int nid);
86#endif
87
59bc3126
GT
88/* The constants used when creating the ENGINE */
89static const char *engine_openssl_id = "openssl";
b6d1e52d 90static const char *engine_openssl_name = "Software engine support";
5270e702 91
3b04cdd7 92static ENGINE *engine_openssl(void)
5270e702 93 {
59bc3126
GT
94 ENGINE *ret = ENGINE_new();
95 if(!ret)
96 return NULL;
3b04cdd7
GT
97 if(!ENGINE_set_id(ret, engine_openssl_id)
98 || !ENGINE_set_name(ret, engine_openssl_name)
9e78e6c3 99#ifndef OPENSSL_NO_RSA
3b04cdd7 100 || !ENGINE_set_RSA(ret, RSA_get_default_method())
9e78e6c3
RL
101#endif
102#ifndef OPENSSL_NO_DSA
3b04cdd7 103 || !ENGINE_set_DSA(ret, DSA_get_default_method())
9e78e6c3
RL
104#endif
105#ifndef OPENSSL_NO_DH
3b04cdd7
GT
106 || !ENGINE_set_DH(ret, DH_get_default_method())
107#endif
108 || !ENGINE_set_RAND(ret, RAND_SSLeay())
109#ifdef TEST_ENG_OPENSSL_RC4
110 || !ENGINE_set_ciphers(ret, openssl_ciphers)
111#endif
112#ifdef TEST_ENG_OPENSSL_SHA
113 || !ENGINE_set_digests(ret, openssl_digests)
9e78e6c3 114#endif
3b04cdd7 115 )
59bc3126
GT
116 {
117 ENGINE_free(ret);
118 return NULL;
119 }
120 return ret;
5270e702 121 }
3b04cdd7
GT
122
123void ENGINE_load_openssl(void)
124 {
125 ENGINE *toadd = engine_openssl();
126 if(!toadd) return;
127 ENGINE_add(toadd);
128 /* If the "add" worked, it gets a structural reference. So either way,
129 * we release our just-created reference. */
130 ENGINE_free(toadd);
131 ERR_clear_error();
132 }
133
134#ifdef TEST_ENG_OPENSSL_RC4
135/* This section of code compiles an "alternative implementation" of two modes of
136 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4"
137 * should under normal circumstances go via this support rather than the default
138 * EVP support. There are other symbols to tweak the testing;
3b04cdd7 139 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time
004aa803 140 * we're asked for a cipher we don't support (should not happen).
3b04cdd7
GT
141 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time
142 * the "init_key" handler is called.
143 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler.
144 */
145#include <openssl/evp.h>
146#include <openssl/rc4.h>
147#define TEST_RC4_KEY_SIZE 16
3b04cdd7
GT
148static int test_cipher_nids[] = {NID_rc4,NID_rc4_40};
149static int test_cipher_nids_number = 2;
3b04cdd7
GT
150typedef struct {
151 unsigned char key[TEST_RC4_KEY_SIZE];
152 RC4_KEY ks;
153 } TEST_RC4_KEY;
154#define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
155static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
156 const unsigned char *iv, int enc)
157 {
158#ifdef TEST_ENG_OPENSSL_RC4_P_INIT
159 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
160#endif
161 memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx));
162 RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
163 test(ctx)->key);
164 return 1;
165 }
166static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
167 const unsigned char *in, unsigned int inl)
168 {
169#ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
170 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
171#endif
172 RC4(&test(ctx)->ks,inl,in,out);
173 return 1;
174 }
175static const EVP_CIPHER test_r4_cipher=
176 {
177 NID_rc4,
178 1,TEST_RC4_KEY_SIZE,0,
179 EVP_CIPH_VARIABLE_LENGTH,
180 test_rc4_init_key,
181 test_rc4_cipher,
182 NULL,
183 sizeof(TEST_RC4_KEY),
184 NULL,
185 NULL,
186 NULL
187 };
188static const EVP_CIPHER test_r4_40_cipher=
189 {
190 NID_rc4_40,
191 1,5 /* 40 bit */,0,
192 EVP_CIPH_VARIABLE_LENGTH,
193 test_rc4_init_key,
194 test_rc4_cipher,
195 NULL,
196 sizeof(TEST_RC4_KEY),
197 NULL,
198 NULL,
199 NULL
200 };
201static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
202 const int **nids, int nid)
203 {
204 if(!cipher)
205 {
206 /* We are returning a list of supported nids */
207 *nids = test_cipher_nids;
208 return test_cipher_nids_number;
209 }
210 /* We are being asked for a specific cipher */
211 if(nid == NID_rc4)
212 *cipher = &test_r4_cipher;
213 else if(nid == NID_rc4_40)
214 *cipher = &test_r4_40_cipher;
215 else
216 {
217#ifdef TEST_ENG_OPENSSL_RC4_OTHERS
218 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
219 "nid %d\n", nid);
220#endif
221 *cipher = NULL;
222 return 0;
223 }
224 return 1;
225 }
226#endif
227
228#ifdef TEST_ENG_OPENSSL_SHA
229/* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */
230#include <openssl/evp.h>
231#include <openssl/sha.h>
3b04cdd7
GT
232static int test_digest_nids[] = {NID_sha1};
233static int test_digest_nids_number = 1;
3b04cdd7
GT
234static int test_sha1_init(EVP_MD_CTX *ctx)
235 {
236#ifdef TEST_ENG_OPENSSL_SHA_P_INIT
237 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
238#endif
239 return SHA1_Init(ctx->md_data);
240 }
241static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,unsigned long count)
242 {
243#ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
244 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
245#endif
246 return SHA1_Update(ctx->md_data,data,count);
247 }
248static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md)
249 {
250#ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
251 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
252#endif
253 return SHA1_Final(md,ctx->md_data);
254 }
255static const EVP_MD test_sha_md=
256 {
257 NID_sha1,
258 NID_sha1WithRSAEncryption,
259 SHA_DIGEST_LENGTH,
260 0,
261 test_sha1_init,
262 test_sha1_update,
263 test_sha1_final,
264 NULL,
265 NULL,
266 EVP_PKEY_RSA_method,
267 SHA_CBLOCK,
268 sizeof(EVP_MD *)+sizeof(SHA_CTX),
269 };
270static int openssl_digests(ENGINE *e, const EVP_MD **digest,
271 const int **nids, int nid)
272 {
273 if(!digest)
274 {
275 /* We are returning a list of supported nids */
276 *nids = test_digest_nids;
277 return test_digest_nids_number;
278 }
279 /* We are being asked for a specific digest */
280 if(nid == NID_sha1)
281 *digest = &test_sha_md;
282 else
283 {
284#ifdef TEST_ENG_OPENSSL_SHA_OTHERS
285 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
286 "nid %d\n", nid);
287#endif
288 *digest = NULL;
289 return 0;
290 }
291 return 1;
292 }
293#endif