Beispiel #1
0
int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
		ENGINE *e, const int *nids, int num_nids, int setdefault)
	{
	int ret = 0, added = 0;
	ENGINE_PILE tmplate, *fnd;
	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
	if(!(*table))
		added = 1;
	if(!int_table_check(table, 1))
		goto end;
	if(added)
		/* The cleanup callback needs to be added */
		engine_cleanup_add_first(cleanup);
	while(num_nids--)
		{
		tmplate.nid = *nids;
		fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
		if(!fnd)
			{
			fnd = OPENSSL_malloc(sizeof(ENGINE_PILE));
			if(!fnd) goto end;
			fnd->uptodate = 1;
			fnd->nid = *nids;
			fnd->sk = sk_ENGINE_new_null();
			if(!fnd->sk)
				{
				OPENSSL_free(fnd);
				goto end;
				}
			fnd->funct = NULL;
			(void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
			}
		/* A registration shouldn't add duplciate entries */
		(void)sk_ENGINE_delete_ptr(fnd->sk, e);
		/* if 'setdefault', this ENGINE goes to the head of the list */
		if(!sk_ENGINE_push(fnd->sk, e))
			goto end;
		/* "touch" this ENGINE_PILE */
		fnd->uptodate = 0;
		if(setdefault)
			{
			if(!engine_unlocked_init(e))
				{
				ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
						ENGINE_R_INIT_FAILED);
				goto end;
				}
			if(fnd->funct)
				engine_unlocked_finish(fnd->funct, 0);
			fnd->funct = e;
			fnd->uptodate = 1;
			}
		nids++;
		}
	ret = 1;
end:
	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
	return ret;
	}
Beispiel #2
0
ENGINE *
engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l)
#endif
{
	ENGINE *ret = NULL;
	ENGINE_PILE tmplate, *fnd = NULL;
	int initres, loop = 0;

	if (!(*table)) {
#ifdef ENGINE_TABLE_DEBUG
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
		    "registered!\n", f, l, nid);
#endif
		return NULL;
	}
	ERR_set_mark();
	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
	/* Check again inside the lock otherwise we could race against cleanup
	 * operations. But don't worry about a fprintf(stderr). */
	if (!int_table_check(table, 0))
		goto end;
	tmplate.nid = nid;
	fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
	if (!fnd)
		goto end;
	if (fnd->funct && engine_unlocked_init(fnd->funct)) {
#ifdef ENGINE_TABLE_DEBUG
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
		    "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
#endif
		ret = fnd->funct;
		goto end;
	}
	if (fnd->uptodate) {
		ret = fnd->funct;
		goto end;
	}
trynext:
	ret = sk_ENGINE_value(fnd->sk, loop++);
	if (!ret) {
#ifdef ENGINE_TABLE_DEBUG
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
		    "registered implementations would initialise\n", f, l, nid);
#endif
		goto end;
	}
	/* Try to initialise the ENGINE? */
	if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
		initres = engine_unlocked_init(ret);
	else
		initres = 0;
	if (initres) {
		/* Update 'funct' */
		if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
			/* If there was a previous default we release it. */
			if (fnd->funct)
				engine_unlocked_finish(fnd->funct, 0);
			fnd->funct = ret;
#ifdef ENGINE_TABLE_DEBUG
			fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
			    "setting default to '%s'\n", f, l, nid, ret->id);
#endif
		}
#ifdef ENGINE_TABLE_DEBUG
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
		    "newly initialised '%s'\n", f, l, nid, ret->id);
#endif
		goto end;
	}
	goto trynext;
end:
	/* If it failed, it is unlikely to succeed again until some future
	 * registrations have taken place. In all cases, we cache. */
	if (fnd)
		fnd->uptodate = 1;
#ifdef ENGINE_TABLE_DEBUG
	if (ret)
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
		    "ENGINE '%s'\n", f, l, nid, ret->id);
	else
		fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
		    "'no matching ENGINE'\n", f, l, nid);
#endif
	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
	/* Whatever happened, any failed init()s are not failures in this
	 * context, so clear our error state. */
	ERR_pop_to_mark();
	return ret;
}