summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/engine/eng_openssl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/engine/eng_openssl.c')
-rw-r--r--src/lib/libcrypto/engine/eng_openssl.c228
1 files changed, 123 insertions, 105 deletions
diff --git a/src/lib/libcrypto/engine/eng_openssl.c b/src/lib/libcrypto/engine/eng_openssl.c
index f7cd8df622..9ba61dd842 100644
--- a/src/lib/libcrypto/engine/eng_openssl.c
+++ b/src/lib/libcrypto/engine/eng_openssl.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: eng_openssl.c,v 1.5 2014/06/12 15:49:29 deraadt Exp $ */ 1/* $OpenBSD: eng_openssl.c,v 1.6 2014/06/22 12:05:09 jsing Exp $ */
2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL 2/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -57,7 +57,7 @@
57 */ 57 */
58/* ==================================================================== 58/* ====================================================================
59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60 * ECDH support in OpenSSL originally developed by 60 * ECDH support in OpenSSL originally developed by
61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 61 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62 */ 62 */
63 63
@@ -106,21 +106,21 @@
106#undef TEST_ENG_OPENSSL_SHA_OTHERS 106#undef TEST_ENG_OPENSSL_SHA_OTHERS
107#undef TEST_ENG_OPENSSL_SHA_P_INIT 107#undef TEST_ENG_OPENSSL_SHA_P_INIT
108#undef TEST_ENG_OPENSSL_SHA_P_UPDATE 108#undef TEST_ENG_OPENSSL_SHA_P_UPDATE
109#undef TEST_ENG_OPENSSL_SHA_P_FINAL 109#undef TEST_ENG_OPENSSL_SHA_P_FINAL
110#endif 110#endif
111 111
112#ifdef TEST_ENG_OPENSSL_RC4 112#ifdef TEST_ENG_OPENSSL_RC4
113static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 113static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
114 const int **nids, int nid); 114 const int **nids, int nid);
115#endif 115#endif
116#ifdef TEST_ENG_OPENSSL_SHA 116#ifdef TEST_ENG_OPENSSL_SHA
117static int openssl_digests(ENGINE *e, const EVP_MD **digest, 117static int openssl_digests(ENGINE *e, const EVP_MD **digest,
118 const int **nids, int nid); 118 const int **nids, int nid);
119#endif 119#endif
120 120
121#ifdef TEST_ENG_OPENSSL_PKEY 121#ifdef TEST_ENG_OPENSSL_PKEY
122static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, 122static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id,
123 UI_METHOD *ui_method, void *callback_data); 123 UI_METHOD *ui_method, void *callback_data);
124#endif 124#endif
125 125
126/* The constants used when creating the ENGINE */ 126/* The constants used when creating the ENGINE */
@@ -129,79 +129,85 @@ static const char *engine_openssl_name = "Software engine support";
129 129
130/* This internal function is used by ENGINE_openssl() and possibly by the 130/* This internal function is used by ENGINE_openssl() and possibly by the
131 * "dynamic" ENGINE support too */ 131 * "dynamic" ENGINE support too */
132static int bind_helper(ENGINE *e) 132static int
133 { 133bind_helper(ENGINE *e)
134 if(!ENGINE_set_id(e, engine_openssl_id) 134{
135 || !ENGINE_set_name(e, engine_openssl_name) 135 if (!ENGINE_set_id(e, engine_openssl_id) ||
136 !ENGINE_set_name(e, engine_openssl_name)
136#ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS 137#ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS
137#ifndef OPENSSL_NO_RSA 138#ifndef OPENSSL_NO_RSA
138 || !ENGINE_set_RSA(e, RSA_get_default_method()) 139 || !ENGINE_set_RSA(e, RSA_get_default_method())
139#endif 140#endif
140#ifndef OPENSSL_NO_DSA 141#ifndef OPENSSL_NO_DSA
141 || !ENGINE_set_DSA(e, DSA_get_default_method()) 142 || !ENGINE_set_DSA(e, DSA_get_default_method())
142#endif 143#endif
143#ifndef OPENSSL_NO_ECDH 144#ifndef OPENSSL_NO_ECDH
144 || !ENGINE_set_ECDH(e, ECDH_OpenSSL()) 145 || !ENGINE_set_ECDH(e, ECDH_OpenSSL())
145#endif 146#endif
146#ifndef OPENSSL_NO_ECDSA 147#ifndef OPENSSL_NO_ECDSA
147 || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL()) 148 || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL())
148#endif 149#endif
149#ifndef OPENSSL_NO_DH 150#ifndef OPENSSL_NO_DH
150 || !ENGINE_set_DH(e, DH_get_default_method()) 151 || !ENGINE_set_DH(e, DH_get_default_method())
151#endif 152#endif
152 || !ENGINE_set_RAND(e, RAND_SSLeay()) 153 || !ENGINE_set_RAND(e, RAND_SSLeay())
153#ifdef TEST_ENG_OPENSSL_RC4 154#ifdef TEST_ENG_OPENSSL_RC4
154 || !ENGINE_set_ciphers(e, openssl_ciphers) 155 || !ENGINE_set_ciphers(e, openssl_ciphers)
155#endif 156#endif
156#ifdef TEST_ENG_OPENSSL_SHA 157#ifdef TEST_ENG_OPENSSL_SHA
157 || !ENGINE_set_digests(e, openssl_digests) 158 || !ENGINE_set_digests(e, openssl_digests)
158#endif 159#endif
159#endif 160#endif
160#ifdef TEST_ENG_OPENSSL_PKEY 161#ifdef TEST_ENG_OPENSSL_PKEY
161 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey) 162 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey)
162#endif 163#endif
163 ) 164 )
164 return 0; 165 return 0;
165 /* If we add errors to this ENGINE, ensure the error handling is setup here */ 166 /* If we add errors to this ENGINE, ensure the error handling is setup here */
166 /* openssl_load_error_strings(); */ 167 /* openssl_load_error_strings(); */
167 return 1; 168 return 1;
168 } 169}
169 170
170static ENGINE *engine_openssl(void) 171static ENGINE *
171 { 172engine_openssl(void)
173{
172 ENGINE *ret = ENGINE_new(); 174 ENGINE *ret = ENGINE_new();
173 if(!ret) 175
176 if (!ret)
174 return NULL; 177 return NULL;
175 if(!bind_helper(ret)) 178 if (!bind_helper(ret)) {
176 {
177 ENGINE_free(ret); 179 ENGINE_free(ret);
178 return NULL; 180 return NULL;
179 }
180 return ret;
181 } 181 }
182 return ret;
183}
182 184
183void ENGINE_load_openssl(void) 185void
184 { 186ENGINE_load_openssl(void)
187{
185 ENGINE *toadd = engine_openssl(); 188 ENGINE *toadd = engine_openssl();
186 if(!toadd) return; 189
190 if (!toadd)
191 return;
187 ENGINE_add(toadd); 192 ENGINE_add(toadd);
188 /* If the "add" worked, it gets a structural reference. So either way, 193 /* If the "add" worked, it gets a structural reference. So either way,
189 * we release our just-created reference. */ 194 * we release our just-created reference. */
190 ENGINE_free(toadd); 195 ENGINE_free(toadd);
191 ERR_clear_error(); 196 ERR_clear_error();
192 } 197}
193 198
194/* This stuff is needed if this ENGINE is being compiled into a self-contained 199/* This stuff is needed if this ENGINE is being compiled into a self-contained
195 * shared-library. */ 200 * shared-library. */
196#ifdef ENGINE_DYNAMIC_SUPPORT 201#ifdef ENGINE_DYNAMIC_SUPPORT
197static int bind_fn(ENGINE *e, const char *id) 202static int
198 { 203bind_fn(ENGINE *e, const char *id)
199 if(id && (strcmp(id, engine_openssl_id) != 0)) 204{
205 if (id && (strcmp(id, engine_openssl_id) != 0))
200 return 0; 206 return 0;
201 if(!bind_helper(e)) 207 if (!bind_helper(e))
202 return 0; 208 return 0;
203 return 1; 209 return 1;
204 } 210}
205IMPLEMENT_DYNAMIC_CHECK_FN() 211IMPLEMENT_DYNAMIC_CHECK_FN()
206IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 212IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
207#endif /* ENGINE_DYNAMIC_SUPPORT */ 213#endif /* ENGINE_DYNAMIC_SUPPORT */
@@ -219,37 +225,42 @@ IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
219 */ 225 */
220#include <openssl/rc4.h> 226#include <openssl/rc4.h>
221#define TEST_RC4_KEY_SIZE 16 227#define TEST_RC4_KEY_SIZE 16
222static int test_cipher_nids[] = {NID_rc4,NID_rc4_40}; 228static int test_cipher_nids[] = {NID_rc4, NID_rc4_40};
223static int test_cipher_nids_number = 2; 229static int test_cipher_nids_number = 2;
230
224typedef struct { 231typedef struct {
225 unsigned char key[TEST_RC4_KEY_SIZE]; 232 unsigned char key[TEST_RC4_KEY_SIZE];
226 RC4_KEY ks; 233 RC4_KEY ks;
227 } TEST_RC4_KEY; 234} TEST_RC4_KEY;
235
228#define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data) 236#define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data)
229static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 237static int
230 const unsigned char *iv, int enc) 238test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
231 { 239 const unsigned char *iv, int enc)
240{
232#ifdef TEST_ENG_OPENSSL_RC4_P_INIT 241#ifdef TEST_ENG_OPENSSL_RC4_P_INIT
233 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); 242 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n");
234#endif 243#endif
235 memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx)); 244 memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx));
236 RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), 245 RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
237 test(ctx)->key); 246 test(ctx)->key);
238 return 1; 247 return 1;
239 } 248}
240static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 249
241 const unsigned char *in, size_t inl) 250static int
242 { 251test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
252 const unsigned char *in, size_t inl)
253{
243#ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER 254#ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER
244 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); 255 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n");
245#endif 256#endif
246 RC4(&test(ctx)->ks,inl,in,out); 257 RC4(&test(ctx)->ks, inl, in, out);
247 return 1; 258 return 1;
248 } 259}
249static const EVP_CIPHER test_r4_cipher= 260
250 { 261static const EVP_CIPHER test_r4_cipher = {
251 NID_rc4, 262 NID_rc4,
252 1,TEST_RC4_KEY_SIZE,0, 263 1, TEST_RC4_KEY_SIZE, 0,
253 EVP_CIPH_VARIABLE_LENGTH, 264 EVP_CIPH_VARIABLE_LENGTH,
254 test_rc4_init_key, 265 test_rc4_init_key,
255 test_rc4_cipher, 266 test_rc4_cipher,
@@ -259,9 +270,9 @@ static const EVP_CIPHER test_r4_cipher=
259 NULL, 270 NULL,
260 NULL, 271 NULL,
261 NULL 272 NULL
262 }; 273};
263static const EVP_CIPHER test_r4_40_cipher= 274
264 { 275static const EVP_CIPHER test_r4_40_cipher = {
265 NID_rc4_40, 276 NID_rc4_40,
266 1,5 /* 40 bit */,0, 277 1,5 /* 40 bit */,0,
267 EVP_CIPH_VARIABLE_LENGTH, 278 EVP_CIPH_VARIABLE_LENGTH,
@@ -269,36 +280,35 @@ static const EVP_CIPHER test_r4_40_cipher=
269 test_rc4_cipher, 280 test_rc4_cipher,
270 NULL, 281 NULL,
271 sizeof(TEST_RC4_KEY), 282 sizeof(TEST_RC4_KEY),
272 NULL, 283 NULL,
273 NULL, 284 NULL,
274 NULL, 285 NULL,
275 NULL 286 NULL
276 }; 287};
277static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 288
278 const int **nids, int nid) 289static int
279 { 290openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, const int **nids, int nid)
280 if(!cipher) 291{
281 { 292 if (!cipher) {
282 /* We are returning a list of supported nids */ 293 /* We are returning a list of supported nids */
283 *nids = test_cipher_nids; 294 *nids = test_cipher_nids;
284 return test_cipher_nids_number; 295 return test_cipher_nids_number;
285 } 296 }
286 /* We are being asked for a specific cipher */ 297 /* We are being asked for a specific cipher */
287 if(nid == NID_rc4) 298 if (nid == NID_rc4)
288 *cipher = &test_r4_cipher; 299 *cipher = &test_r4_cipher;
289 else if(nid == NID_rc4_40) 300 else if (nid == NID_rc4_40)
290 *cipher = &test_r4_40_cipher; 301 *cipher = &test_r4_40_cipher;
291 else 302 else {
292 {
293#ifdef TEST_ENG_OPENSSL_RC4_OTHERS 303#ifdef TEST_ENG_OPENSSL_RC4_OTHERS
294 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " 304 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for "
295 "nid %d\n", nid); 305 "nid %d\n", nid);
296#endif 306#endif
297 *cipher = NULL; 307 *cipher = NULL;
298 return 0; 308 return 0;
299 }
300 return 1;
301 } 309 }
310 return 1;
311}
302#endif 312#endif
303 313
304#ifdef TEST_ENG_OPENSSL_SHA 314#ifdef TEST_ENG_OPENSSL_SHA
@@ -306,29 +316,35 @@ static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher,
306#include <openssl/sha.h> 316#include <openssl/sha.h>
307static int test_digest_nids[] = {NID_sha1}; 317static int test_digest_nids[] = {NID_sha1};
308static int test_digest_nids_number = 1; 318static int test_digest_nids_number = 1;
309static int test_sha1_init(EVP_MD_CTX *ctx) 319
310 { 320static int
321test_sha1_init(EVP_MD_CTX *ctx)
322{
311#ifdef TEST_ENG_OPENSSL_SHA_P_INIT 323#ifdef TEST_ENG_OPENSSL_SHA_P_INIT
312 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); 324 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n");
313#endif 325#endif
314 return SHA1_Init(ctx->md_data); 326 return SHA1_Init(ctx->md_data);
315 } 327}
316static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,size_t count) 328
317 { 329static int
330test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count)
331{
318#ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE 332#ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE
319 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); 333 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n");
320#endif 334#endif
321 return SHA1_Update(ctx->md_data,data,count); 335 return SHA1_Update(ctx->md_data, data, count);
322 } 336}
323static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md) 337
324 { 338static int
339test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md)
340{
325#ifdef TEST_ENG_OPENSSL_SHA_P_FINAL 341#ifdef TEST_ENG_OPENSSL_SHA_P_FINAL
326 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); 342 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n");
327#endif 343#endif
328 return SHA1_Final(md,ctx->md_data); 344 return SHA1_Final(md, ctx->md_data);
329 } 345}
330static const EVP_MD test_sha_md= 346
331 { 347static const EVP_MD test_sha_md = {
332 NID_sha1, 348 NID_sha1,
333 NID_sha1WithRSAEncryption, 349 NID_sha1WithRSAEncryption,
334 SHA_DIGEST_LENGTH, 350 SHA_DIGEST_LENGTH,
@@ -340,45 +356,47 @@ static const EVP_MD test_sha_md=
340 NULL, 356 NULL,
341 EVP_PKEY_RSA_method, 357 EVP_PKEY_RSA_method,
342 SHA_CBLOCK, 358 SHA_CBLOCK,
343 sizeof(EVP_MD *)+sizeof(SHA_CTX), 359 sizeof(EVP_MD *) + sizeof(SHA_CTX),
344 }; 360};
345static int openssl_digests(ENGINE *e, const EVP_MD **digest, 361
346 const int **nids, int nid) 362static int
347 { 363openssl_digests(ENGINE *e, const EVP_MD **digest, const int **nids, int nid)
348 if(!digest) 364{
349 { 365 if (!digest) {
350 /* We are returning a list of supported nids */ 366 /* We are returning a list of supported nids */
351 *nids = test_digest_nids; 367 *nids = test_digest_nids;
352 return test_digest_nids_number; 368 return test_digest_nids_number;
353 } 369 }
354 /* We are being asked for a specific digest */ 370 /* We are being asked for a specific digest */
355 if(nid == NID_sha1) 371 if (nid == NID_sha1)
356 *digest = &test_sha_md; 372 *digest = &test_sha_md;
357 else 373 else {
358 {
359#ifdef TEST_ENG_OPENSSL_SHA_OTHERS 374#ifdef TEST_ENG_OPENSSL_SHA_OTHERS
360 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " 375 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for "
361 "nid %d\n", nid); 376 "nid %d\n", nid);
362#endif 377#endif
363 *digest = NULL; 378 *digest = NULL;
364 return 0; 379 return 0;
365 }
366 return 1;
367 } 380 }
381 return 1;
382}
368#endif 383#endif
369 384
370#ifdef TEST_ENG_OPENSSL_PKEY 385#ifdef TEST_ENG_OPENSSL_PKEY
371static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, 386static EVP_PKEY *
372 UI_METHOD *ui_method, void *callback_data) 387openssl_load_privkey(ENGINE *eng, const char *key_id, UI_METHOD *ui_method,
373 { 388 void *callback_data)
389{
374 BIO *in; 390 BIO *in;
375 EVP_PKEY *key; 391 EVP_PKEY *key;
376 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id); 392
393 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n",
394 key_id);
377 in = BIO_new_file(key_id, "r"); 395 in = BIO_new_file(key_id, "r");
378 if (!in) 396 if (!in)
379 return NULL; 397 return NULL;
380 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); 398 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
381 BIO_free(in); 399 BIO_free(in);
382 return key; 400 return key;
383 } 401}
384#endif 402#endif