1/* crypto/engine/eng_openssl.c */ 2/* 3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project 4 * 2000. 5 */ 6/* ==================================================================== 7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgment: 23 * "This product includes software developed by the OpenSSL Project 24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25 * 26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27 * endorse or promote products derived from this software without 28 * prior written permission. For written permission, please contact 29 * licensing@OpenSSL.org. 30 * 31 * 5. Products derived from this software may not be called "OpenSSL" 32 * nor may "OpenSSL" appear in their names without prior written 33 * permission of the OpenSSL Project. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51 * OF THE POSSIBILITY OF SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This product includes cryptographic software written by Eric Young 55 * (eay@cryptsoft.com). This product includes software written by Tim 56 * Hudson (tjh@cryptsoft.com). 57 * 58 */ 59/* ==================================================================== 60 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 61 * ECDH support in OpenSSL originally developed by 62 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. 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#ifndef OPENSSL_NO_RSA 74# include <openssl/rsa.h> 75#endif 76#ifndef OPENSSL_NO_DSA 77# include <openssl/dsa.h> 78#endif 79#ifndef OPENSSL_NO_DH 80# include <openssl/dh.h> 81#endif 82 83/* 84 * This testing gunk is implemented (and explained) lower down. It also 85 * assumes the application explicitly calls "ENGINE_load_openssl()" because 86 * this is no longer automatic in ENGINE_load_builtin_engines(). 87 */ 88#define TEST_ENG_OPENSSL_RC4 89#define TEST_ENG_OPENSSL_PKEY 90/* #define TEST_ENG_OPENSSL_RC4_OTHERS */ 91#define TEST_ENG_OPENSSL_RC4_P_INIT 92/* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */ 93#define TEST_ENG_OPENSSL_SHA 94/* #define TEST_ENG_OPENSSL_SHA_OTHERS */ 95/* #define TEST_ENG_OPENSSL_SHA_P_INIT */ 96/* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */ 97/* #define TEST_ENG_OPENSSL_SHA_P_FINAL */ 98 99/* Now check what of those algorithms are actually enabled */ 100#ifdef OPENSSL_NO_RC4 101# undef TEST_ENG_OPENSSL_RC4 102# undef TEST_ENG_OPENSSL_RC4_OTHERS 103# undef TEST_ENG_OPENSSL_RC4_P_INIT 104# undef TEST_ENG_OPENSSL_RC4_P_CIPHER 105#endif 106#if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1) 107# undef TEST_ENG_OPENSSL_SHA 108# undef TEST_ENG_OPENSSL_SHA_OTHERS 109# undef TEST_ENG_OPENSSL_SHA_P_INIT 110# undef TEST_ENG_OPENSSL_SHA_P_UPDATE 111# undef TEST_ENG_OPENSSL_SHA_P_FINAL 112#endif 113 114#ifdef TEST_ENG_OPENSSL_RC4 115static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 116 const int **nids, int nid); 117#endif 118#ifdef TEST_ENG_OPENSSL_SHA 119static int openssl_digests(ENGINE *e, const EVP_MD **digest, 120 const int **nids, int nid); 121#endif 122 123#ifdef TEST_ENG_OPENSSL_PKEY 124static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, 125 UI_METHOD *ui_method, 126 void *callback_data); 127#endif 128 129/* The constants used when creating the ENGINE */ 130static const char *engine_openssl_id = "openssl"; 131static const char *engine_openssl_name = "Software engine support"; 132 133/* 134 * This internal function is used by ENGINE_openssl() and possibly by the 135 * "dynamic" ENGINE support too 136 */ 137static int bind_helper(ENGINE *e) 138{ 139 if (!ENGINE_set_id(e, engine_openssl_id) 140 || !ENGINE_set_name(e, engine_openssl_name) 141#ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS 142# ifndef OPENSSL_NO_RSA 143 || !ENGINE_set_RSA(e, RSA_get_default_method()) 144# endif 145# ifndef OPENSSL_NO_DSA 146 || !ENGINE_set_DSA(e, DSA_get_default_method()) 147# endif 148# ifndef OPENSSL_NO_ECDH 149 || !ENGINE_set_ECDH(e, ECDH_OpenSSL()) 150# endif 151# ifndef OPENSSL_NO_ECDSA 152 || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL()) 153# endif 154# ifndef OPENSSL_NO_DH 155 || !ENGINE_set_DH(e, DH_get_default_method()) 156# endif 157 || !ENGINE_set_RAND(e, RAND_SSLeay()) 158# ifdef TEST_ENG_OPENSSL_RC4 159 || !ENGINE_set_ciphers(e, openssl_ciphers) 160# endif 161# ifdef TEST_ENG_OPENSSL_SHA 162 || !ENGINE_set_digests(e, openssl_digests) 163# endif 164#endif 165#ifdef TEST_ENG_OPENSSL_PKEY 166 || !ENGINE_set_load_privkey_function(e, openssl_load_privkey) 167#endif 168 ) 169 return 0; 170 /* 171 * If we add errors to this ENGINE, ensure the error handling is setup 172 * here 173 */ 174 /* openssl_load_error_strings(); */ 175 return 1; 176} 177 178static ENGINE *engine_openssl(void) 179{ 180 ENGINE *ret = ENGINE_new(); 181 if (!ret) 182 return NULL; 183 if (!bind_helper(ret)) { 184 ENGINE_free(ret); 185 return NULL; 186 } 187 return ret; 188} 189 190void ENGINE_load_openssl(void) 191{ 192 ENGINE *toadd = engine_openssl(); 193 if (!toadd) 194 return; 195 ENGINE_add(toadd); 196 /* 197 * If the "add" worked, it gets a structural reference. So either way, we 198 * release our just-created reference. 199 */ 200 ENGINE_free(toadd); 201 ERR_clear_error(); 202} 203 204/* 205 * This stuff is needed if this ENGINE is being compiled into a 206 * self-contained shared-library. 207 */ 208#ifdef ENGINE_DYNAMIC_SUPPORT 209static int bind_fn(ENGINE *e, const char *id) 210{ 211 if (id && (strcmp(id, engine_openssl_id) != 0)) 212 return 0; 213 if (!bind_helper(e)) 214 return 0; 215 return 1; 216} 217 218IMPLEMENT_DYNAMIC_CHECK_FN() 219 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) 220#endif /* ENGINE_DYNAMIC_SUPPORT */ 221#ifdef TEST_ENG_OPENSSL_RC4 222/*- 223 * This section of code compiles an "alternative implementation" of two modes of 224 * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4" 225 * should under normal circumstances go via this support rather than the default 226 * EVP support. There are other symbols to tweak the testing; 227 * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time 228 * we're asked for a cipher we don't support (should not happen). 229 * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time 230 * the "init_key" handler is called. 231 * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler. 232 */ 233# include <openssl/rc4.h> 234# define TEST_RC4_KEY_SIZE 16 235static int test_cipher_nids[] = { NID_rc4, NID_rc4_40 }; 236 237static int test_cipher_nids_number = 2; 238typedef struct { 239 unsigned char key[TEST_RC4_KEY_SIZE]; 240 RC4_KEY ks; 241} TEST_RC4_KEY; 242# define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data) 243static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, 244 const unsigned char *iv, int enc) 245{ 246# ifdef TEST_ENG_OPENSSL_RC4_P_INIT 247 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); 248# endif 249 memcpy(&test(ctx)->key[0], key, EVP_CIPHER_CTX_key_length(ctx)); 250 RC4_set_key(&test(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), 251 test(ctx)->key); 252 return 1; 253} 254 255static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 256 const unsigned char *in, unsigned int inl) 257{ 258# ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER 259 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); 260# endif 261 RC4(&test(ctx)->ks, inl, in, out); 262 return 1; 263} 264 265static const EVP_CIPHER test_r4_cipher = { 266 NID_rc4, 267 1, TEST_RC4_KEY_SIZE, 0, 268 EVP_CIPH_VARIABLE_LENGTH, 269 test_rc4_init_key, 270 test_rc4_cipher, 271 NULL, 272 sizeof(TEST_RC4_KEY), 273 NULL, 274 NULL, 275 NULL, 276 NULL 277}; 278 279static const EVP_CIPHER test_r4_40_cipher = { 280 NID_rc4_40, 281 1, 5 /* 40 bit */ , 0, 282 EVP_CIPH_VARIABLE_LENGTH, 283 test_rc4_init_key, 284 test_rc4_cipher, 285 NULL, 286 sizeof(TEST_RC4_KEY), 287 NULL, 288 NULL, 289 NULL, 290 NULL 291}; 292 293static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, 294 const int **nids, int nid) 295{ 296 if (!cipher) { 297 /* We are returning a list of supported nids */ 298 *nids = test_cipher_nids; 299 return test_cipher_nids_number; 300 } 301 /* We are being asked for a specific cipher */ 302 if (nid == NID_rc4) 303 *cipher = &test_r4_cipher; 304 else if (nid == NID_rc4_40) 305 *cipher = &test_r4_40_cipher; 306 else { 307# ifdef TEST_ENG_OPENSSL_RC4_OTHERS 308 fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " 309 "nid %d\n", nid); 310# endif 311 *cipher = NULL; 312 return 0; 313 } 314 return 1; 315} 316#endif 317 318#ifdef TEST_ENG_OPENSSL_SHA 319/* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */ 320# include <openssl/sha.h> 321static int test_digest_nids[] = { NID_sha1 }; 322 323static int test_digest_nids_number = 1; 324static int test_sha1_init(EVP_MD_CTX *ctx) 325{ 326# ifdef TEST_ENG_OPENSSL_SHA_P_INIT 327 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); 328# endif 329 return SHA1_Init(ctx->md_data); 330} 331 332static int test_sha1_update(EVP_MD_CTX *ctx, const void *data, size_t count) 333{ 334# ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE 335 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); 336# endif 337 return SHA1_Update(ctx->md_data, data, count); 338} 339 340static int test_sha1_final(EVP_MD_CTX *ctx, unsigned char *md) 341{ 342# ifdef TEST_ENG_OPENSSL_SHA_P_FINAL 343 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); 344# endif 345 return SHA1_Final(md, ctx->md_data); 346} 347 348static const EVP_MD test_sha_md = { 349 NID_sha1, 350 NID_sha1WithRSAEncryption, 351 SHA_DIGEST_LENGTH, 352 0, 353 test_sha1_init, 354 test_sha1_update, 355 test_sha1_final, 356 NULL, 357 NULL, 358 EVP_PKEY_RSA_method, 359 SHA_CBLOCK, 360 sizeof(EVP_MD *) + sizeof(SHA_CTX), 361}; 362 363static int openssl_digests(ENGINE *e, const EVP_MD **digest, 364 const int **nids, int nid) 365{ 366 if (!digest) { 367 /* We are returning a list of supported nids */ 368 *nids = test_digest_nids; 369 return test_digest_nids_number; 370 } 371 /* We are being asked for a specific digest */ 372 if (nid == NID_sha1) 373 *digest = &test_sha_md; 374 else { 375# ifdef TEST_ENG_OPENSSL_SHA_OTHERS 376 fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " 377 "nid %d\n", nid); 378# endif 379 *digest = NULL; 380 return 0; 381 } 382 return 1; 383} 384#endif 385 386#ifdef TEST_ENG_OPENSSL_PKEY 387static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, 388 UI_METHOD *ui_method, 389 void *callback_data) 390{ 391 BIO *in; 392 EVP_PKEY *key; 393 fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", 394 key_id); 395 in = BIO_new_file(key_id, "r"); 396 if (!in) 397 return NULL; 398 key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); 399 BIO_free(in); 400 return key; 401} 402#endif 403