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