os/ossrv/ssl/libcrypto/src/crypto/engine/eng_lib.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/engine/eng_lib.c */
sl@0
     2
/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
sl@0
     3
 * project 2000.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
#include "eng_int.h"
sl@0
    62
#include <openssl/rand.h>
sl@0
    63
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    64
#include "libcrypto_wsd_macros.h"
sl@0
    65
#include "libcrypto_wsd.h"
sl@0
    66
#endif
sl@0
    67
sl@0
    68
sl@0
    69
/* The "new"/"free" stuff first */
sl@0
    70
sl@0
    71
EXPORT_C ENGINE *ENGINE_new(void)
sl@0
    72
	{
sl@0
    73
	ENGINE *ret;
sl@0
    74
sl@0
    75
	ret = (ENGINE *)OPENSSL_malloc(sizeof(ENGINE));
sl@0
    76
	if(ret == NULL)
sl@0
    77
		{
sl@0
    78
		ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
sl@0
    79
		return NULL;
sl@0
    80
		}
sl@0
    81
	memset(ret, 0, sizeof(ENGINE));
sl@0
    82
	ret->struct_ref = 1;
sl@0
    83
	engine_ref_debug(ret, 0, 1)
sl@0
    84
	CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data);
sl@0
    85
	return ret;
sl@0
    86
	}
sl@0
    87
sl@0
    88
/* Placed here (close proximity to ENGINE_new) so that modifications to the
sl@0
    89
 * elements of the ENGINE structure are more likely to be caught and changed
sl@0
    90
 * here. */
sl@0
    91
EXPORT_C void engine_set_all_null(ENGINE *e)
sl@0
    92
	{
sl@0
    93
	e->id = NULL;
sl@0
    94
	e->name = NULL;
sl@0
    95
	e->rsa_meth = NULL;
sl@0
    96
	e->dsa_meth = NULL;
sl@0
    97
	e->dh_meth = NULL;
sl@0
    98
	e->rand_meth = NULL;
sl@0
    99
	e->store_meth = NULL;
sl@0
   100
	e->ciphers = NULL;
sl@0
   101
	e->digests = NULL;
sl@0
   102
	e->destroy = NULL;
sl@0
   103
	e->init = NULL;
sl@0
   104
	e->finish = NULL;
sl@0
   105
	e->ctrl = NULL;
sl@0
   106
	e->load_privkey = NULL;
sl@0
   107
	e->load_pubkey = NULL;
sl@0
   108
	e->cmd_defns = NULL;
sl@0
   109
	e->flags = 0;
sl@0
   110
	}
sl@0
   111
sl@0
   112
EXPORT_C int engine_free_util(ENGINE *e, int locked)
sl@0
   113
	{
sl@0
   114
	int i;
sl@0
   115
sl@0
   116
	if(e == NULL)
sl@0
   117
		{
sl@0
   118
		ENGINEerr(ENGINE_F_ENGINE_FREE_UTIL,
sl@0
   119
			ERR_R_PASSED_NULL_PARAMETER);
sl@0
   120
		return 0;
sl@0
   121
		}
sl@0
   122
	if(locked)
sl@0
   123
		i = CRYPTO_add(&e->struct_ref,-1,CRYPTO_LOCK_ENGINE);
sl@0
   124
	else
sl@0
   125
		i = --e->struct_ref;
sl@0
   126
	engine_ref_debug(e, 0, -1)
sl@0
   127
	if (i > 0) return 1;
sl@0
   128
#ifdef REF_CHECK
sl@0
   129
	if (i < 0)
sl@0
   130
		{
sl@0
   131
		fprintf(stderr,"ENGINE_free, bad structural reference count\n");
sl@0
   132
		abort();
sl@0
   133
		}
sl@0
   134
#endif
sl@0
   135
	/* Give the ENGINE a chance to do any structural cleanup corresponding
sl@0
   136
	 * to allocation it did in its constructor (eg. unload error strings) */
sl@0
   137
	if(e->destroy)
sl@0
   138
		e->destroy(e);
sl@0
   139
	CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
sl@0
   140
	OPENSSL_free(e);
sl@0
   141
	return 1;
sl@0
   142
	}
sl@0
   143
sl@0
   144
EXPORT_C int ENGINE_free(ENGINE *e)
sl@0
   145
	{
sl@0
   146
	return engine_free_util(e, 1);
sl@0
   147
	}
sl@0
   148
sl@0
   149
/* Cleanup stuff */
sl@0
   150
sl@0
   151
/* ENGINE_cleanup() is coded such that anything that does work that will need
sl@0
   152
 * cleanup can register a "cleanup" callback here. That way we don't get linker
sl@0
   153
 * bloat by referring to all *possible* cleanups, but any linker bloat into code
sl@0
   154
 * "X" will cause X's cleanup function to end up here. */
sl@0
   155
#ifndef EMULATOR 
sl@0
   156
static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
sl@0
   157
#else
sl@0
   158
GET_STATIC_VAR_FROM_TLS(cleanup_stack,eng_lib,STACK_OF(ENGINE_CLEANUP_ITEM) *)
sl@0
   159
#define cleanup_stack (*GET_WSD_VAR_NAME(cleanup_stack,eng_lib, s)())
sl@0
   160
#endif
sl@0
   161
static int int_cleanup_check(int create)
sl@0
   162
	{
sl@0
   163
	if(cleanup_stack) return 1;
sl@0
   164
	if(!create) return 0;
sl@0
   165
	cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
sl@0
   166
	return (cleanup_stack ? 1 : 0);
sl@0
   167
	}
sl@0
   168
static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
sl@0
   169
	{
sl@0
   170
	ENGINE_CLEANUP_ITEM *item = OPENSSL_malloc(sizeof(
sl@0
   171
					ENGINE_CLEANUP_ITEM));
sl@0
   172
	if(!item) return NULL;
sl@0
   173
	item->cb = cb;
sl@0
   174
	return item;
sl@0
   175
	}
sl@0
   176
EXPORT_C void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
sl@0
   177
	{
sl@0
   178
	ENGINE_CLEANUP_ITEM *item;
sl@0
   179
	if(!int_cleanup_check(1)) return;
sl@0
   180
	item = int_cleanup_item(cb);
sl@0
   181
	if(item)
sl@0
   182
		sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0);
sl@0
   183
	}
sl@0
   184
EXPORT_C void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
sl@0
   185
	{
sl@0
   186
	ENGINE_CLEANUP_ITEM *item;
sl@0
   187
	if(!int_cleanup_check(1)) return;
sl@0
   188
	item = int_cleanup_item(cb);
sl@0
   189
	if(item)
sl@0
   190
		sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item);
sl@0
   191
	}
sl@0
   192
/* The API function that performs all cleanup */
sl@0
   193
static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
sl@0
   194
	{
sl@0
   195
	(*(item->cb))();
sl@0
   196
	OPENSSL_free(item);
sl@0
   197
	}
sl@0
   198
EXPORT_C void ENGINE_cleanup(void)
sl@0
   199
	{
sl@0
   200
	if(int_cleanup_check(0))
sl@0
   201
		{
sl@0
   202
		sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
sl@0
   203
			engine_cleanup_cb_free);
sl@0
   204
		cleanup_stack = NULL;
sl@0
   205
		}
sl@0
   206
	/* FIXME: This should be handled (somehow) through RAND, eg. by it
sl@0
   207
	 * registering a cleanup callback. */
sl@0
   208
	RAND_set_rand_method(NULL);
sl@0
   209
	}
sl@0
   210
sl@0
   211
/* Now the "ex_data" support */
sl@0
   212
sl@0
   213
EXPORT_C int ENGINE_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
sl@0
   214
		CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
sl@0
   215
	{
sl@0
   216
	return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_ENGINE, argl, argp,
sl@0
   217
			new_func, dup_func, free_func);
sl@0
   218
	}
sl@0
   219
sl@0
   220
EXPORT_C int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
sl@0
   221
	{
sl@0
   222
	return(CRYPTO_set_ex_data(&e->ex_data, idx, arg));
sl@0
   223
	}
sl@0
   224
sl@0
   225
EXPORT_C void *ENGINE_get_ex_data(const ENGINE *e, int idx)
sl@0
   226
	{
sl@0
   227
	return(CRYPTO_get_ex_data(&e->ex_data, idx));
sl@0
   228
	}
sl@0
   229
sl@0
   230
/* Functions to get/set an ENGINE's elements - mainly to avoid exposing the
sl@0
   231
 * ENGINE structure itself. */
sl@0
   232
sl@0
   233
EXPORT_C int ENGINE_set_id(ENGINE *e, const char *id)
sl@0
   234
	{
sl@0
   235
	if(id == NULL)
sl@0
   236
		{
sl@0
   237
		ENGINEerr(ENGINE_F_ENGINE_SET_ID,
sl@0
   238
			ERR_R_PASSED_NULL_PARAMETER);
sl@0
   239
		return 0;
sl@0
   240
		}
sl@0
   241
	e->id = id;
sl@0
   242
	return 1;
sl@0
   243
	}
sl@0
   244
sl@0
   245
EXPORT_C int ENGINE_set_name(ENGINE *e, const char *name)
sl@0
   246
	{
sl@0
   247
	if(name == NULL)
sl@0
   248
		{
sl@0
   249
		ENGINEerr(ENGINE_F_ENGINE_SET_NAME,
sl@0
   250
			ERR_R_PASSED_NULL_PARAMETER);
sl@0
   251
		return 0;
sl@0
   252
		}
sl@0
   253
	e->name = name;
sl@0
   254
	return 1;
sl@0
   255
	}
sl@0
   256
sl@0
   257
EXPORT_C int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
sl@0
   258
	{
sl@0
   259
	e->destroy = destroy_f;
sl@0
   260
	return 1;
sl@0
   261
	}
sl@0
   262
sl@0
   263
EXPORT_C int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
sl@0
   264
	{
sl@0
   265
	e->init = init_f;
sl@0
   266
	return 1;
sl@0
   267
	}
sl@0
   268
sl@0
   269
EXPORT_C int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
sl@0
   270
	{
sl@0
   271
	e->finish = finish_f;
sl@0
   272
	return 1;
sl@0
   273
	}
sl@0
   274
sl@0
   275
EXPORT_C int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
sl@0
   276
	{
sl@0
   277
	e->ctrl = ctrl_f;
sl@0
   278
	return 1;
sl@0
   279
	}
sl@0
   280
sl@0
   281
EXPORT_C int ENGINE_set_flags(ENGINE *e, int flags)
sl@0
   282
	{
sl@0
   283
	e->flags = flags;
sl@0
   284
	return 1;
sl@0
   285
	}
sl@0
   286
sl@0
   287
EXPORT_C int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
sl@0
   288
	{
sl@0
   289
	e->cmd_defns = defns;
sl@0
   290
	return 1;
sl@0
   291
	}
sl@0
   292
sl@0
   293
EXPORT_C const char *ENGINE_get_id(const ENGINE *e)
sl@0
   294
	{
sl@0
   295
	return e->id;
sl@0
   296
	}
sl@0
   297
sl@0
   298
EXPORT_C const char *ENGINE_get_name(const ENGINE *e)
sl@0
   299
	{
sl@0
   300
	return e->name;
sl@0
   301
	}
sl@0
   302
sl@0
   303
EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
sl@0
   304
	{
sl@0
   305
	return e->destroy;
sl@0
   306
	}
sl@0
   307
sl@0
   308
EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
sl@0
   309
	{
sl@0
   310
	return e->init;
sl@0
   311
	}
sl@0
   312
sl@0
   313
EXPORT_C ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
sl@0
   314
	{
sl@0
   315
	return e->finish;
sl@0
   316
	}
sl@0
   317
sl@0
   318
EXPORT_C ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
sl@0
   319
	{
sl@0
   320
	return e->ctrl;
sl@0
   321
	}
sl@0
   322
sl@0
   323
EXPORT_C int ENGINE_get_flags(const ENGINE *e)
sl@0
   324
	{
sl@0
   325
	return e->flags;
sl@0
   326
	}
sl@0
   327
sl@0
   328
EXPORT_C const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
sl@0
   329
	{
sl@0
   330
	return e->cmd_defns;
sl@0
   331
	}
sl@0
   332
sl@0
   333
/* eng_lib.o is pretty much linked into anything that touches ENGINE already, so
sl@0
   334
 * put the "static_state" hack here. */
sl@0
   335
#ifndef EMULATOR
sl@0
   336
static int internal_static_hack = 0;
sl@0
   337
#else
sl@0
   338
GET_STATIC_VAR_FROM_TLS(internal_static_hack,eng_lib,int)
sl@0
   339
#define internal_static_hack (*GET_WSD_VAR_NAME(internal_static_hack,eng_lib, s)())
sl@0
   340
#endif
sl@0
   341
sl@0
   342
EXPORT_C void *ENGINE_get_static_state(void)
sl@0
   343
	{
sl@0
   344
	return &internal_static_hack;
sl@0
   345
	}