sl@0: /* crypto/engine/eng_openssl.c */ sl@0: /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL sl@0: * project 2000. sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in sl@0: * the documentation and/or other materials provided with the sl@0: * distribution. sl@0: * sl@0: * 3. All advertising materials mentioning features or use of this sl@0: * software must display the following acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" sl@0: * sl@0: * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to sl@0: * endorse or promote products derived from this software without sl@0: * prior written permission. For written permission, please contact sl@0: * licensing@OpenSSL.org. sl@0: * sl@0: * 5. Products derived from this software may not be called "OpenSSL" sl@0: * nor may "OpenSSL" appear in their names without prior written sl@0: * permission of the OpenSSL Project. sl@0: * sl@0: * 6. Redistributions of any form whatsoever must retain the following sl@0: * acknowledgment: sl@0: * "This product includes software developed by the OpenSSL Project sl@0: * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY sl@0: * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR sl@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR sl@0: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, sl@0: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT sl@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; sl@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, sl@0: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) sl@0: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED sl@0: * OF THE POSSIBILITY OF SUCH DAMAGE. sl@0: * ==================================================================== sl@0: * sl@0: * This product includes cryptographic software written by Eric Young sl@0: * (eay@cryptsoft.com). This product includes software written by Tim sl@0: * Hudson (tjh@cryptsoft.com). sl@0: * sl@0: */ sl@0: /* ==================================================================== sl@0: * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. sl@0: * ECDH support in OpenSSL originally developed by sl@0: * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #endif sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: sl@0: /* This testing gunk is implemented (and explained) lower down. It also assumes sl@0: * the application explicitly calls "ENGINE_load_openssl()" because this is no sl@0: * longer automatic in ENGINE_load_builtin_engines(). */ sl@0: #define TEST_ENG_OPENSSL_RC4 sl@0: #define TEST_ENG_OPENSSL_PKEY sl@0: /* #define TEST_ENG_OPENSSL_RC4_OTHERS */ sl@0: #define TEST_ENG_OPENSSL_RC4_P_INIT sl@0: /* #define TEST_ENG_OPENSSL_RC4_P_CIPHER */ sl@0: #define TEST_ENG_OPENSSL_SHA sl@0: /* #define TEST_ENG_OPENSSL_SHA_OTHERS */ sl@0: /* #define TEST_ENG_OPENSSL_SHA_P_INIT */ sl@0: /* #define TEST_ENG_OPENSSL_SHA_P_UPDATE */ sl@0: /* #define TEST_ENG_OPENSSL_SHA_P_FINAL */ sl@0: sl@0: /* Now check what of those algorithms are actually enabled */ sl@0: #ifdef OPENSSL_NO_RC4 sl@0: #undef TEST_ENG_OPENSSL_RC4 sl@0: #undef TEST_ENG_OPENSSL_RC4_OTHERS sl@0: #undef TEST_ENG_OPENSSL_RC4_P_INIT sl@0: #undef TEST_ENG_OPENSSL_RC4_P_CIPHER sl@0: #endif sl@0: #if defined(OPENSSL_NO_SHA) || defined(OPENSSL_NO_SHA0) || defined(OPENSSL_NO_SHA1) sl@0: #undef TEST_ENG_OPENSSL_SHA sl@0: #undef TEST_ENG_OPENSSL_SHA_OTHERS sl@0: #undef TEST_ENG_OPENSSL_SHA_P_INIT sl@0: #undef TEST_ENG_OPENSSL_SHA_P_UPDATE sl@0: #undef TEST_ENG_OPENSSL_SHA_P_FINAL sl@0: #endif sl@0: sl@0: #ifdef TEST_ENG_OPENSSL_RC4 sl@0: static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, sl@0: const int **nids, int nid); sl@0: #endif sl@0: #ifdef TEST_ENG_OPENSSL_SHA sl@0: static int openssl_digests(ENGINE *e, const EVP_MD **digest, sl@0: const int **nids, int nid); sl@0: #endif sl@0: sl@0: #ifdef TEST_ENG_OPENSSL_PKEY sl@0: static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, sl@0: UI_METHOD *ui_method, void *callback_data); sl@0: #endif sl@0: sl@0: /* The constants used when creating the ENGINE */ sl@0: #ifndef EMULATOR sl@0: static const char *engine_openssl_id = "openssl"; sl@0: static const char *engine_openssl_name = "Software engine support"; sl@0: #else sl@0: static const char *const engine_openssl_id = "openssl"; sl@0: static const char *const engine_openssl_name = "Software engine support"; sl@0: #endif sl@0: sl@0: /* This internal function is used by ENGINE_openssl() and possibly by the sl@0: * "dynamic" ENGINE support too */ sl@0: static int bind_helper(ENGINE *e) sl@0: { sl@0: if(!ENGINE_set_id(e, engine_openssl_id) sl@0: || !ENGINE_set_name(e, engine_openssl_name) sl@0: #ifndef TEST_ENG_OPENSSL_NO_ALGORITHMS sl@0: #ifndef OPENSSL_NO_RSA sl@0: || !ENGINE_set_RSA(e, RSA_get_default_method()) sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: || !ENGINE_set_DSA(e, DSA_get_default_method()) sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDH sl@0: || !ENGINE_set_ECDH(e, ECDH_OpenSSL()) sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: || !ENGINE_set_ECDSA(e, ECDSA_OpenSSL()) sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: || !ENGINE_set_DH(e, DH_get_default_method()) sl@0: #endif sl@0: || !ENGINE_set_RAND(e, RAND_SSLeay()) sl@0: #ifdef TEST_ENG_OPENSSL_RC4 sl@0: || !ENGINE_set_ciphers(e, openssl_ciphers) sl@0: #endif sl@0: #ifdef TEST_ENG_OPENSSL_SHA sl@0: || !ENGINE_set_digests(e, openssl_digests) sl@0: #endif sl@0: #endif sl@0: #ifdef TEST_ENG_OPENSSL_PKEY sl@0: || !ENGINE_set_load_privkey_function(e, openssl_load_privkey) sl@0: #endif sl@0: ) sl@0: return 0; sl@0: /* If we add errors to this ENGINE, ensure the error handling is setup here */ sl@0: /* openssl_load_error_strings(); */ sl@0: return 1; sl@0: } sl@0: sl@0: static ENGINE *engine_openssl(void) sl@0: { sl@0: ENGINE *ret = ENGINE_new(); sl@0: if(!ret) sl@0: return NULL; sl@0: if(!bind_helper(ret)) sl@0: { sl@0: ENGINE_free(ret); sl@0: return NULL; sl@0: } sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void ENGINE_load_openssl(void) sl@0: { sl@0: ENGINE *toadd = engine_openssl(); sl@0: if(!toadd) return; sl@0: ENGINE_add(toadd); sl@0: /* If the "add" worked, it gets a structural reference. So either way, sl@0: * we release our just-created reference. */ sl@0: ENGINE_free(toadd); sl@0: ERR_clear_error(); sl@0: } sl@0: sl@0: /* This stuff is needed if this ENGINE is being compiled into a self-contained sl@0: * shared-library. */ sl@0: #ifdef ENGINE_DYNAMIC_SUPPORT sl@0: static int bind_fn(ENGINE *e, const char *id) sl@0: { sl@0: if(id && (strcmp(id, engine_openssl_id) != 0)) sl@0: return 0; sl@0: if(!bind_helper(e)) sl@0: return 0; sl@0: return 1; sl@0: } sl@0: IMPLEMENT_DYNAMIC_CHECK_FN() sl@0: IMPLEMENT_DYNAMIC_BIND_FN(bind_fn) sl@0: #endif /* ENGINE_DYNAMIC_SUPPORT */ sl@0: sl@0: #ifdef TEST_ENG_OPENSSL_RC4 sl@0: /* This section of code compiles an "alternative implementation" of two modes of sl@0: * RC4 into this ENGINE. The result is that EVP_CIPHER operation for "rc4" sl@0: * should under normal circumstances go via this support rather than the default sl@0: * EVP support. There are other symbols to tweak the testing; sl@0: * TEST_ENC_OPENSSL_RC4_OTHERS - print a one line message to stderr each time sl@0: * we're asked for a cipher we don't support (should not happen). sl@0: * TEST_ENG_OPENSSL_RC4_P_INIT - print a one line message to stderr each time sl@0: * the "init_key" handler is called. sl@0: * TEST_ENG_OPENSSL_RC4_P_CIPHER - ditto for the "cipher" handler. sl@0: */ sl@0: #include sl@0: #define TEST_RC4_KEY_SIZE 16 sl@0: #ifndef EMULATOR sl@0: static int test_cipher_nids[] = {NID_rc4,NID_rc4_40}; sl@0: static int test_cipher_nids_number = 2; sl@0: #else sl@0: static const int test_cipher_nids[] = {NID_rc4,NID_rc4_40}; sl@0: static const int test_cipher_nids_number = 2; sl@0: #endif sl@0: typedef struct { sl@0: unsigned char key[TEST_RC4_KEY_SIZE]; sl@0: RC4_KEY ks; sl@0: } TEST_RC4_KEY; sl@0: #define test(ctx) ((TEST_RC4_KEY *)(ctx)->cipher_data) sl@0: static int test_rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, sl@0: const unsigned char *iv, int enc) sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_RC4_P_INIT sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_init_key() called\n"); sl@0: #endif sl@0: memcpy(&test(ctx)->key[0],key,EVP_CIPHER_CTX_key_length(ctx)); sl@0: RC4_set_key(&test(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), sl@0: test(ctx)->key); sl@0: return 1; sl@0: } sl@0: static int test_rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, sl@0: const unsigned char *in, unsigned int inl) sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_RC4_P_CIPHER sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) test_cipher() called\n"); sl@0: #endif sl@0: RC4(&test(ctx)->ks,inl,in,out); sl@0: return 1; sl@0: } sl@0: static const EVP_CIPHER test_r4_cipher= sl@0: { sl@0: NID_rc4, sl@0: 1,TEST_RC4_KEY_SIZE,0, sl@0: EVP_CIPH_VARIABLE_LENGTH, sl@0: test_rc4_init_key, sl@0: test_rc4_cipher, sl@0: NULL, sl@0: sizeof(TEST_RC4_KEY), sl@0: NULL, sl@0: NULL, sl@0: NULL, sl@0: NULL sl@0: }; sl@0: static const EVP_CIPHER test_r4_40_cipher= sl@0: { sl@0: NID_rc4_40, sl@0: 1,5 /* 40 bit */,0, sl@0: EVP_CIPH_VARIABLE_LENGTH, sl@0: test_rc4_init_key, sl@0: test_rc4_cipher, sl@0: NULL, sl@0: sizeof(TEST_RC4_KEY), sl@0: NULL, sl@0: NULL, sl@0: NULL, sl@0: NULL sl@0: }; sl@0: static int openssl_ciphers(ENGINE *e, const EVP_CIPHER **cipher, sl@0: const int **nids, int nid) sl@0: { sl@0: if(!cipher) sl@0: { sl@0: /* We are returning a list of supported nids */ sl@0: *nids = test_cipher_nids; sl@0: return test_cipher_nids_number; sl@0: } sl@0: /* We are being asked for a specific cipher */ sl@0: if(nid == NID_rc4) sl@0: *cipher = &test_r4_cipher; sl@0: else if(nid == NID_rc4_40) sl@0: *cipher = &test_r4_40_cipher; sl@0: else sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_RC4_OTHERS sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_RC4) returning NULL for " sl@0: "nid %d\n", nid); sl@0: #endif sl@0: *cipher = NULL; sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: #endif sl@0: sl@0: #ifdef TEST_ENG_OPENSSL_SHA sl@0: /* Much the same sort of comment as for TEST_ENG_OPENSSL_RC4 */ sl@0: #include sl@0: #ifndef EMULATOR sl@0: static int test_digest_nids[] = {NID_sha1}; sl@0: static int test_digest_nids_number = 1; sl@0: #else sl@0: static const int test_digest_nids[] = {NID_sha1}; sl@0: static const int test_digest_nids_number = 1; sl@0: #endif sl@0: static int test_sha1_init(EVP_MD_CTX *ctx) sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_SHA_P_INIT sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_init() called\n"); sl@0: #endif sl@0: return SHA1_Init(ctx->md_data); sl@0: } sl@0: static int test_sha1_update(EVP_MD_CTX *ctx,const void *data,size_t count) sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_SHA_P_UPDATE sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_update() called\n"); sl@0: #endif sl@0: return SHA1_Update(ctx->md_data,data,count); sl@0: } sl@0: static int test_sha1_final(EVP_MD_CTX *ctx,unsigned char *md) sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_SHA_P_FINAL sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) test_sha1_final() called\n"); sl@0: #endif sl@0: return SHA1_Final(md,ctx->md_data); sl@0: } sl@0: static const EVP_MD test_sha_md= sl@0: { sl@0: NID_sha1, sl@0: NID_sha1WithRSAEncryption, sl@0: SHA_DIGEST_LENGTH, sl@0: 0, sl@0: test_sha1_init, sl@0: test_sha1_update, sl@0: test_sha1_final, sl@0: NULL, sl@0: NULL, sl@0: EVP_PKEY_RSA_method, sl@0: SHA_CBLOCK, sl@0: sizeof(EVP_MD *)+sizeof(SHA_CTX), sl@0: }; sl@0: static int openssl_digests(ENGINE *e, const EVP_MD **digest, sl@0: const int **nids, int nid) sl@0: { sl@0: if(!digest) sl@0: { sl@0: /* We are returning a list of supported nids */ sl@0: *nids = test_digest_nids; sl@0: return test_digest_nids_number; sl@0: } sl@0: /* We are being asked for a specific digest */ sl@0: if(nid == NID_sha1) sl@0: *digest = &test_sha_md; sl@0: else sl@0: { sl@0: #ifdef TEST_ENG_OPENSSL_SHA_OTHERS sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_SHA) returning NULL for " sl@0: "nid %d\n", nid); sl@0: #endif sl@0: *digest = NULL; sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: #endif sl@0: sl@0: #ifdef TEST_ENG_OPENSSL_PKEY sl@0: static EVP_PKEY *openssl_load_privkey(ENGINE *eng, const char *key_id, sl@0: UI_METHOD *ui_method, void *callback_data) sl@0: { sl@0: BIO *in; sl@0: EVP_PKEY *key; sl@0: fprintf(stderr, "(TEST_ENG_OPENSSL_PKEY)Loading Private key %s\n", key_id); sl@0: in = BIO_new_file(key_id, "r"); sl@0: if (!in) sl@0: return NULL; sl@0: key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL); sl@0: BIO_free(in); sl@0: return key; sl@0: } sl@0: #endif