Blame src/eng_back.c

Packit 6b81fa
/*
Packit 6b81fa
 * Copyright (c) 2001 Markus Friedl
Packit 6b81fa
 * Copyright (c) 2002 Juha Yrjölä
Packit 6b81fa
 * Copyright (c) 2002 Olaf Kirch
Packit 6b81fa
 * Copyright (c) 2003 Kevin Stefanik
Packit 6b81fa
 * Copyright (c) 2016-2018 Michał Trojnara <Michal.Trojnara@stunnel.org>
Packit 6b81fa
 *
Packit 6b81fa
 * Redistribution and use in source and binary forms, with or without
Packit 6b81fa
 * modification, are permitted provided that the following conditions
Packit 6b81fa
 * are met:
Packit 6b81fa
 * 1. Redistributions of source code must retain the above copyright
Packit 6b81fa
 *    notice, this list of conditions and the following disclaimer.
Packit 6b81fa
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 6b81fa
 *    notice, this list of conditions and the following disclaimer in the
Packit 6b81fa
 *    documentation and/or other materials provided with the distribution.
Packit 6b81fa
 *
Packit 6b81fa
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit 6b81fa
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit 6b81fa
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit 6b81fa
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 6b81fa
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit 6b81fa
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 6b81fa
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 6b81fa
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 6b81fa
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit 6b81fa
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 6b81fa
 */
Packit 6b81fa
Packit 6b81fa
#include "engine.h"
Packit 6b81fa
#include <stdio.h>
Packit 6b81fa
#include <string.h>
Packit 6b81fa
Packit 6b81fa
#if defined(_WIN32) || defined(_WIN64)
Packit 6b81fa
#define strncasecmp _strnicmp
Packit 6b81fa
#endif
Packit 6b81fa
Packit 6b81fa
/* The maximum length of an internally-allocated PIN */
Packit 6b81fa
#define MAX_PIN_LENGTH   32
Packit 6b81fa
#define MAX_VALUE_LEN	200
Packit 6b81fa
Packit 6b81fa
struct st_engine_ctx {
Packit 6b81fa
	/* Engine configuration */
Packit 6b81fa
	/*
Packit 6b81fa
	 * The PIN used for login. Cache for the ctx_get_pin function.
Packit 6b81fa
	 * The memory for this PIN is always owned internally,
Packit 6b81fa
	 * and may be freed as necessary. Before freeing, the PIN
Packit 6b81fa
	 * must be whitened, to prevent security holes.
Packit 6b81fa
	 */
Packit 6b81fa
	char *pin;
Packit 6b81fa
	size_t pin_length;
Packit 6b81fa
	int verbose;
Packit 6b81fa
	char *module;
Packit 6b81fa
	char *init_args;
Packit 6b81fa
	UI_METHOD *ui_method;
Packit 6b81fa
	void *callback_data;
Packit 6b81fa
	int force_login;
Packit 6b81fa
Packit 6b81fa
	/* Engine initialization mutex */
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
	CRYPTO_RWLOCK *rwlock;
Packit 6b81fa
#else
Packit 6b81fa
	int rwlock;
Packit 6b81fa
#endif
Packit 6b81fa
Packit 6b81fa
	/* Current operations */
Packit 6b81fa
	PKCS11_CTX *pkcs11_ctx;
Packit 6b81fa
	PKCS11_SLOT *slot_list;
Packit 6b81fa
	unsigned int slot_count;
Packit 6b81fa
};
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* Utility functions                                                          */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
void ctx_log(ENGINE_CTX *ctx, int level, const char *format, ...)
Packit 6b81fa
{
Packit 6b81fa
	va_list ap;
Packit 6b81fa
Packit 6b81fa
	if (level > ctx->verbose)
Packit 6b81fa
			return;
Packit 6b81fa
	va_start(ap, format);
Packit 6b81fa
	vfprintf(stderr, format, ap);
Packit 6b81fa
	va_end(ap);
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static void dump_hex(ENGINE_CTX *ctx, int level,
Packit 6b81fa
		const unsigned char *val, const size_t len)
Packit 6b81fa
{
Packit 6b81fa
	size_t n;
Packit 6b81fa
Packit 6b81fa
	for (n = 0; n < len; n++)
Packit 6b81fa
		ctx_log(ctx, level, "%02x", val[n]);
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* PIN handling                                                               */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
/* Free PIN storage in secure way. */
Packit 6b81fa
static void ctx_destroy_pin(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	if (ctx->pin != NULL) {
Packit 6b81fa
		OPENSSL_cleanse(ctx->pin, ctx->pin_length);
Packit 6b81fa
		OPENSSL_free(ctx->pin);
Packit 6b81fa
		ctx->pin = NULL;
Packit 6b81fa
		ctx->pin_length = 0;
Packit 6b81fa
	}
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Get the PIN via asking user interface. The supplied call-back data are
Packit 6b81fa
 * passed to the user interface implemented by an application. Only the
Packit 6b81fa
 * application knows how to interpret the call-back data.
Packit 6b81fa
 * A (strdup'ed) copy of the PIN code will be stored in the pin variable. */
Packit 6b81fa
static int ctx_get_pin(ENGINE_CTX *ctx, const char* token_label, UI_METHOD *ui_method, void *callback_data)
Packit 6b81fa
{
Packit 6b81fa
	UI *ui;
Packit 6b81fa
	char* prompt;
Packit 6b81fa
Packit 6b81fa
	/* call ui to ask for a pin */
Packit 6b81fa
	ui = UI_new_method(ui_method);
Packit 6b81fa
	if (ui == NULL) {
Packit 6b81fa
		ctx_log(ctx, 0, "UI_new failed\n");
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	if (callback_data != NULL)
Packit 6b81fa
		UI_add_user_data(ui, callback_data);
Packit 6b81fa
Packit 6b81fa
	ctx_destroy_pin(ctx);
Packit 6b81fa
	ctx->pin = OPENSSL_malloc(MAX_PIN_LENGTH+1);
Packit 6b81fa
	if (ctx->pin == NULL)
Packit 6b81fa
		return 0;
Packit 6b81fa
	memset(ctx->pin, 0, MAX_PIN_LENGTH+1);
Packit 6b81fa
	ctx->pin_length = MAX_PIN_LENGTH;
Packit 6b81fa
	prompt = UI_construct_prompt(ui, "PKCS#11 token PIN", token_label);
Packit 6b81fa
	if (!prompt) {
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	if (!UI_dup_input_string(ui, prompt,
Packit 6b81fa
			UI_INPUT_FLAG_DEFAULT_PWD, ctx->pin, 4, MAX_PIN_LENGTH)) {
Packit 6b81fa
		ctx_log(ctx, 0, "UI_dup_input_string failed\n");
Packit 6b81fa
		UI_free(ui);
Packit 6b81fa
		OPENSSL_free(prompt);
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	OPENSSL_free(prompt);
Packit 6b81fa
Packit 6b81fa
	if (UI_process(ui)) {
Packit 6b81fa
		ctx_log(ctx, 0, "UI_process failed\n");
Packit 6b81fa
		UI_free(ui);
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	UI_free(ui);
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Return 1 if the user has already logged in */
Packit 6b81fa
static int slot_logged_in(ENGINE_CTX *ctx, PKCS11_SLOT *slot) {
Packit 6b81fa
	int logged_in = 0;
Packit 6b81fa
Packit 6b81fa
	/* Check if already logged in to avoid resetting state */
Packit 6b81fa
	if (PKCS11_is_logged_in(slot, 0, &logged_in) != 0) {
Packit 6b81fa
		ctx_log(ctx, 0, "Unable to check if already logged in\n");
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	return logged_in;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/*
Packit 6b81fa
 * Log-into the token if necessary.
Packit 6b81fa
 *
Packit 6b81fa
 * @slot is PKCS11 slot to log in
Packit 6b81fa
 * @tok is PKCS11 token to log in (??? could be derived as @slot->token)
Packit 6b81fa
 * @ui_method is OpenSSL user interface which is used to ask for a password
Packit 6b81fa
 * @callback_data are application data to the user interface
Packit 6b81fa
 * @return 1 on success, 0 on error.
Packit 6b81fa
 */
Packit 6b81fa
static int ctx_login(ENGINE_CTX *ctx, PKCS11_SLOT *slot, PKCS11_TOKEN *tok,
Packit 6b81fa
		UI_METHOD *ui_method, void *callback_data)
Packit 6b81fa
{
Packit 6b81fa
	if (!(ctx->force_login || tok->loginRequired) || slot_logged_in(ctx, slot))
Packit 6b81fa
		return 1;
Packit 6b81fa
Packit 6b81fa
	/* If the token has a secure login (i.e., an external keypad),
Packit 6b81fa
	 * then use a NULL PIN. Otherwise, obtain a new PIN if needed. */
Packit 6b81fa
	if (tok->secureLogin) {
Packit 6b81fa
		/* Free the PIN if it has already been
Packit 6b81fa
		 * assigned (i.e, cached by ctx_get_pin) */
Packit 6b81fa
		ctx_destroy_pin(ctx);
Packit 6b81fa
	} else if (ctx->pin == NULL) {
Packit 6b81fa
		ctx->pin = OPENSSL_malloc(MAX_PIN_LENGTH+1);
Packit 6b81fa
		ctx->pin_length = MAX_PIN_LENGTH;
Packit 6b81fa
		if (ctx->pin == NULL) {
Packit 6b81fa
			ctx_log(ctx, 0, "Could not allocate memory for PIN\n");
Packit 6b81fa
			return 0;
Packit 6b81fa
		}
Packit 6b81fa
		memset(ctx->pin, 0, MAX_PIN_LENGTH+1);
Packit 6b81fa
		if (!ctx_get_pin(ctx, tok->label, ui_method, callback_data)) {
Packit 6b81fa
			ctx_destroy_pin(ctx);
Packit 6b81fa
			ctx_log(ctx, 0, "No PIN code was entered\n");
Packit 6b81fa
			return 0;
Packit 6b81fa
		}
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	/* Now login in with the (possibly NULL) PIN */
Packit 6b81fa
	if (PKCS11_login(slot, 0, ctx->pin)) {
Packit 6b81fa
		/* Login failed, so free the PIN if present */
Packit 6b81fa
		ctx_destroy_pin(ctx);
Packit 6b81fa
		ctx_log(ctx, 0, "Login failed\n");
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* Initialization and cleanup                                                 */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
ENGINE_CTX *ctx_new()
Packit 6b81fa
{
Packit 6b81fa
	ENGINE_CTX *ctx;
Packit 6b81fa
	char *mod;
Packit 6b81fa
Packit 6b81fa
	ctx = OPENSSL_malloc(sizeof(ENGINE_CTX));
Packit 6b81fa
	if (ctx == NULL)
Packit 6b81fa
		return NULL;
Packit 6b81fa
	memset(ctx, 0, sizeof(ENGINE_CTX));
Packit 6b81fa
Packit 6b81fa
	mod = getenv("PKCS11_MODULE_PATH");
Packit 6b81fa
	if (mod) {
Packit 6b81fa
		ctx->module = OPENSSL_strdup(mod);
Packit 6b81fa
	} else {
Packit 6b81fa
#ifdef DEFAULT_PKCS11_MODULE
Packit 6b81fa
		ctx->module = OPENSSL_strdup(DEFAULT_PKCS11_MODULE);
Packit 6b81fa
#else
Packit 6b81fa
		ctx->module = NULL;
Packit 6b81fa
#endif
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
	ctx->rwlock = CRYPTO_THREAD_lock_new();
Packit 6b81fa
#else
Packit 6b81fa
	ctx->rwlock = CRYPTO_get_dynlock_create_callback() ?
Packit 6b81fa
		CRYPTO_get_new_dynlockid() : 0;
Packit 6b81fa
#endif
Packit 6b81fa
Packit 6b81fa
	return ctx;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Destroy the context allocated with ctx_new() */
Packit 6b81fa
int ctx_destroy(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	if (ctx) {
Packit 6b81fa
		ctx_destroy_pin(ctx);
Packit 6b81fa
		OPENSSL_free(ctx->module);
Packit 6b81fa
		OPENSSL_free(ctx->init_args);
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
		CRYPTO_THREAD_lock_free(ctx->rwlock);
Packit 6b81fa
#else
Packit 6b81fa
		if (ctx->rwlock)
Packit 6b81fa
			CRYPTO_destroy_dynlockid(ctx->rwlock);
Packit 6b81fa
#endif
Packit 6b81fa
		OPENSSL_free(ctx);
Packit 6b81fa
	}
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Initialize libp11 data: ctx->pkcs11_ctx and ctx->slot_list */
Packit 6b81fa
static void ctx_init_libp11_unlocked(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	PKCS11_CTX *pkcs11_ctx;
Packit 6b81fa
	PKCS11_SLOT *slot_list = NULL;
Packit 6b81fa
	unsigned int slot_count = 0;
Packit 6b81fa
Packit 6b81fa
	ctx_log(ctx, 1, "PKCS#11: Initializing the engine\n");
Packit 6b81fa
Packit 6b81fa
	pkcs11_ctx = PKCS11_CTX_new();
Packit 6b81fa
	PKCS11_CTX_init_args(pkcs11_ctx, ctx->init_args);
Packit 6b81fa
	PKCS11_set_ui_method(pkcs11_ctx, ctx->ui_method, ctx->callback_data);
Packit 6b81fa
Packit 6b81fa
	/* PKCS11_CTX_load() uses C_GetSlotList() via p11-kit */
Packit 6b81fa
	if (PKCS11_CTX_load(pkcs11_ctx, ctx->module) < 0) {
Packit 6b81fa
		ctx_log(ctx, 0, "Unable to load module %s\n", ctx->module);
Packit 6b81fa
		PKCS11_CTX_free(pkcs11_ctx);
Packit 6b81fa
		return;
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	/* PKCS11_enumerate_slots() uses C_GetSlotList() via libp11 */
Packit 6b81fa
	if (PKCS11_enumerate_slots(pkcs11_ctx, &slot_list, &slot_count) < 0) {
Packit 6b81fa
		ctx_log(ctx, 0, "Failed to enumerate slots\n");
Packit 6b81fa
		PKCS11_CTX_unload(pkcs11_ctx);
Packit 6b81fa
		PKCS11_CTX_free(pkcs11_ctx);
Packit 6b81fa
		return;
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	ctx_log(ctx, 1, "Found %u slot%s\n", slot_count,
Packit 6b81fa
		slot_count <= 1 ? "" : "s");
Packit 6b81fa
Packit 6b81fa
	ctx->pkcs11_ctx = pkcs11_ctx;
Packit 6b81fa
	ctx->slot_list = slot_list;
Packit 6b81fa
	ctx->slot_count = slot_count;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_init_libp11(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
	CRYPTO_THREAD_write_lock(ctx->rwlock);
Packit 6b81fa
#else
Packit 6b81fa
	if (ctx->rwlock)
Packit 6b81fa
		CRYPTO_w_lock(ctx->rwlock);
Packit 6b81fa
#endif
Packit 6b81fa
	if (ctx->pkcs11_ctx == NULL || ctx->slot_list == NULL)
Packit 6b81fa
		ctx_init_libp11_unlocked(ctx);
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
	CRYPTO_THREAD_unlock(ctx->rwlock);
Packit 6b81fa
#else
Packit 6b81fa
	if (ctx->rwlock)
Packit 6b81fa
		CRYPTO_w_unlock(ctx->rwlock);
Packit 6b81fa
#endif
Packit 6b81fa
	return ctx->pkcs11_ctx && ctx->slot_list ? 0 : -1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Function called from ENGINE_init() */
Packit 6b81fa
int ctx_init(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	/* OpenSC implicitly locks CRYPTO_LOCK_ENGINE during C_GetSlotList().
Packit 6b81fa
	 * OpenSSL also locks CRYPTO_LOCK_ENGINE in ENGINE_init().
Packit 6b81fa
	 * Double-locking a non-recursive rwlock causes the application to
Packit 6b81fa
	 * crash or hang, depending on the locking library implementation. */
Packit 6b81fa
Packit 6b81fa
	/* Only attempt initialization when dynamic locks are unavailable.
Packit 6b81fa
	 * This likely also indicates a single-threaded application,
Packit 6b81fa
	 * so temporarily unlocking CRYPTO_LOCK_ENGINE should be safe. */
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER < 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
	if (CRYPTO_get_dynlock_create_callback() == NULL ||
Packit 6b81fa
			CRYPTO_get_dynlock_lock_callback() == NULL ||
Packit 6b81fa
			CRYPTO_get_dynlock_destroy_callback() == NULL) {
Packit 6b81fa
		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
Packit 6b81fa
		ctx_init_libp11_unlocked(ctx);
Packit 6b81fa
		CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
Packit 6b81fa
		return ctx->pkcs11_ctx && ctx->slot_list ? 1 : 0;
Packit 6b81fa
	}
Packit 6b81fa
#else
Packit 6b81fa
	(void)ctx; /* squash the unused parameter warning */
Packit 6b81fa
#endif
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* Finish engine operations initialized with ctx_init() */
Packit 6b81fa
int ctx_finish(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	if (ctx) {
Packit 6b81fa
		if (ctx->slot_list) {
Packit 6b81fa
			PKCS11_release_all_slots(ctx->pkcs11_ctx,
Packit 6b81fa
				ctx->slot_list, ctx->slot_count);
Packit 6b81fa
			ctx->slot_list = NULL;
Packit 6b81fa
			ctx->slot_count = 0;
Packit 6b81fa
		}
Packit 6b81fa
		if (ctx->pkcs11_ctx) {
Packit 6b81fa
			PKCS11_CTX_unload(ctx->pkcs11_ctx);
Packit 6b81fa
			PKCS11_CTX_free(ctx->pkcs11_ctx);
Packit 6b81fa
			ctx->pkcs11_ctx = NULL;
Packit 6b81fa
		}
Packit 6b81fa
	}
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* Certificate handling                                                       */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
/* prototype for OpenSSL ENGINE_load_cert */
Packit 6b81fa
/* used by load_cert_ctrl via ENGINE_ctrl for now */
Packit 6b81fa
Packit 6b81fa
static X509 *ctx_load_cert(ENGINE_CTX *ctx, const char *s_slot_cert_id,
Packit 6b81fa
		const int login)
Packit 6b81fa
{
Packit 6b81fa
	PKCS11_SLOT *slot;
Packit Service 7f6fc8
	PKCS11_SLOT *found_slot = NULL, **matched_slots = NULL;
Packit 6b81fa
	PKCS11_TOKEN *tok, *match_tok = NULL;
Packit 6b81fa
	PKCS11_CERT *certs, *selected_cert = NULL;
Packit Service 7f6fc8
	X509 *x509 = NULL;
Packit 6b81fa
	unsigned int cert_count, n, m;
Packit 6b81fa
	unsigned char cert_id[MAX_VALUE_LEN / 2];
Packit 6b81fa
	size_t cert_id_len = sizeof(cert_id);
Packit 6b81fa
	char *cert_label = NULL;
Packit 6b81fa
	char tmp_pin[MAX_PIN_LENGTH+1];
Packit 6b81fa
	size_t tmp_pin_len = MAX_PIN_LENGTH;
Packit 6b81fa
	int slot_nr = -1;
Packit 6b81fa
	char flags[64];
Packit Service 7f6fc8
	size_t matched_count = 0;
Packit 6b81fa
Packit 6b81fa
	if (ctx_init_libp11(ctx)) /* Delayed libp11 initialization */
Packit 6b81fa
		return NULL;
Packit 6b81fa
Packit 6b81fa
	if (s_slot_cert_id && *s_slot_cert_id) {
Packit 6b81fa
		if (!strncasecmp(s_slot_cert_id, "pkcs11:", 7)) {
Packit 6b81fa
			n = parse_pkcs11_uri(ctx, s_slot_cert_id, &match_tok,
Packit 6b81fa
				cert_id, &cert_id_len,
Packit 6b81fa
				tmp_pin, &tmp_pin_len, &cert_label);
Packit 6b81fa
			if (!n) {
Packit 6b81fa
				ctx_log(ctx, 0,
Packit 6b81fa
					"The certificate ID is not a valid PKCS#11 URI\n"
Packit 6b81fa
					"The PKCS#11 URI format is defined by RFC7512\n");
Packit 6b81fa
				ENGerr(ENG_F_CTX_LOAD_CERT, ENG_R_INVALID_ID);
Packit Service 7f6fc8
				goto error;
Packit 6b81fa
			}
Packit 6b81fa
			if (tmp_pin_len > 0 && tmp_pin[0] != 0) {
Packit 6b81fa
				ctx_destroy_pin(ctx);
Packit 6b81fa
				ctx->pin = OPENSSL_malloc(MAX_PIN_LENGTH+1);
Packit 6b81fa
				if (ctx->pin != NULL) {
Packit 6b81fa
					memset(ctx->pin, 0, MAX_PIN_LENGTH+1);
Packit 6b81fa
					memcpy(ctx->pin, tmp_pin, tmp_pin_len);
Packit 6b81fa
					ctx->pin_length = tmp_pin_len;
Packit 6b81fa
				}
Packit 6b81fa
			}
Packit 6b81fa
		} else {
Packit 6b81fa
			n = parse_slot_id_string(ctx, s_slot_cert_id, &slot_nr,
Packit 6b81fa
				cert_id, &cert_id_len, &cert_label);
Packit 6b81fa
			if (!n) {
Packit 6b81fa
				ctx_log(ctx, 0,
Packit 6b81fa
					"The certificate ID is not a valid PKCS#11 URI\n"
Packit 6b81fa
					"The PKCS#11 URI format is defined by RFC7512\n"
Packit 6b81fa
					"The legacy ENGINE_pkcs11 ID format is also "
Packit 6b81fa
					"still accepted for now\n");
Packit 6b81fa
				ENGerr(ENG_F_CTX_LOAD_CERT, ENG_R_INVALID_ID);
Packit Service 7f6fc8
				goto error;
Packit 6b81fa
			}
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "Looking in slot %d for certificate: ",
Packit 6b81fa
			slot_nr);
Packit 6b81fa
		if (cert_id_len != 0) {
Packit 6b81fa
			ctx_log(ctx, 1, "id=");
Packit 6b81fa
			dump_hex(ctx, 1, cert_id, cert_id_len);
Packit 6b81fa
		}
Packit 6b81fa
		if (cert_id_len != 0 && cert_label != NULL)
Packit 6b81fa
			ctx_log(ctx, 1, " ");
Packit 6b81fa
		if (cert_label != NULL)
Packit 6b81fa
			ctx_log(ctx, 1, "label=%s", cert_label);
Packit 6b81fa
		ctx_log(ctx, 1, "\n");
Packit 6b81fa
	}
Packit 6b81fa
Packit Service 7f6fc8
	matched_slots = (PKCS11_SLOT **)calloc(ctx->slot_count,
Packit Service 7f6fc8
		sizeof(PKCS11_SLOT *));
Packit Service 7f6fc8
	if (matched_slots == NULL) {
Packit Service 7f6fc8
		ctx_log(ctx, 0, "Could not allocate memory for matched slots\n");
Packit Service 7f6fc8
		goto error;
Packit Service 7f6fc8
	}
Packit Service 7f6fc8
Packit 6b81fa
	for (n = 0; n < ctx->slot_count; n++) {
Packit 6b81fa
		slot = ctx->slot_list + n;
Packit 6b81fa
		flags[0] = '\0';
Packit 6b81fa
		if (slot->token) {
Packit 6b81fa
			if (!slot->token->initialized)
Packit 6b81fa
				strcat(flags, "uninitialized, ");
Packit 6b81fa
			else if (!slot->token->userPinSet)
Packit 6b81fa
				strcat(flags, "no pin, ");
Packit 6b81fa
			if (slot->token->loginRequired)
Packit 6b81fa
				strcat(flags, "login, ");
Packit 6b81fa
			if (slot->token->readOnly)
Packit 6b81fa
				strcat(flags, "ro, ");
Packit 6b81fa
		} else {
Packit 6b81fa
			strcpy(flags, "no token");
Packit 6b81fa
		}
Packit 6b81fa
		if ((m = strlen(flags)) != 0) {
Packit 6b81fa
			flags[m - 2] = '\0';
Packit 6b81fa
		}
Packit 6b81fa
Packit 6b81fa
		if (slot_nr != -1 &&
Packit 6b81fa
			slot_nr == (int)PKCS11_get_slotid_from_slot(slot)) {
Packit 6b81fa
			found_slot = slot;
Packit 6b81fa
		}
Packit Service 7f6fc8
Packit 6b81fa
		if (match_tok && slot->token &&
Packit 6b81fa
				(match_tok->label == NULL ||
Packit 6b81fa
					!strcmp(match_tok->label, slot->token->label)) &&
Packit 6b81fa
				(match_tok->manufacturer == NULL ||
Packit 6b81fa
					!strcmp(match_tok->manufacturer, slot->token->manufacturer)) &&
Packit 6b81fa
				(match_tok->serialnr == NULL ||
Packit 6b81fa
					!strcmp(match_tok->serialnr, slot->token->serialnr)) &&
Packit 6b81fa
				(match_tok->model == NULL ||
Packit 6b81fa
					!strcmp(match_tok->model, slot->token->model))) {
Packit 6b81fa
			found_slot = slot;
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "[%lu] %-25.25s  %-16s",
Packit 6b81fa
			PKCS11_get_slotid_from_slot(slot),
Packit 6b81fa
			slot->description, flags);
Packit 6b81fa
		if (slot->token) {
Packit 6b81fa
			ctx_log(ctx, 1, "  (%s)",
Packit 6b81fa
				slot->token->label[0] ?
Packit 6b81fa
				slot->token->label : "no label");
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "\n");
Packit 6b81fa
Packit Service 7f6fc8
		if (found_slot && found_slot->token && !found_slot->token->initialized)
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Found uninitialized token\n");
Packit 6b81fa
Packit Service 7f6fc8
		/* Ignore slots without tokens or with uninitialized token */
Packit Service 7f6fc8
		if (found_slot && found_slot->token && found_slot->token->initialized) {
Packit Service 7f6fc8
			matched_slots[matched_count] = found_slot;
Packit Service 7f6fc8
			matched_count++;
Packit Service 7f6fc8
		}
Packit Service 7f6fc8
		found_slot = NULL;
Packit Service 975729
	}
Packit 6b81fa
Packit Service 7f6fc8
	if (matched_count == 0) {
Packit Service 7f6fc8
		if (match_tok) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Specified object not found\n");
Packit Service 7f6fc8
			goto error;
Packit Service 7f6fc8
		}
Packit Service 975729
Packit Service 7f6fc8
		/* If the legacy slot ID format was used */
Packit Service 7f6fc8
		if (slot_nr != -1) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Invalid slot number: %d\n", slot_nr);
Packit Service 7f6fc8
			goto error;
Packit Service 7f6fc8
		} else {
Packit Service 7f6fc8
			found_slot = PKCS11_find_token(ctx->pkcs11_ctx,
Packit Service 7f6fc8
								ctx->slot_list, ctx->slot_count);
Packit Service 7f6fc8
			/* Ignore if the the token is not initialized */
Packit Service 7f6fc8
			if (found_slot && found_slot->token &&
Packit Service 7f6fc8
					found_slot->token->initialized) {
Packit Service 7f6fc8
				matched_slots[matched_count] = found_slot;
Packit Service 7f6fc8
				matched_count++;
Packit Service 7f6fc8
			} else {
Packit Service 7f6fc8
				ctx_log(ctx, 0, "No tokens found\n");
Packit Service 7f6fc8
				goto error;
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		}
Packit 6b81fa
	}
Packit 6b81fa
Packit Service 7f6fc8
	for (n = 0; n < matched_count; n++) {
Packit Service 7f6fc8
		slot = matched_slots[n];
Packit Service 7f6fc8
		tok = slot->token;
Packit Service 7f6fc8
		if (tok == NULL) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Empty token found\n");
Packit Service 7f6fc8
			break;
Packit Service 7f6fc8
		}
Packit 6b81fa
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found slot:  %s\n", slot->description);
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found token: %s\n", slot->token->label);
Packit Service 7f6fc8
Packit Service 7f6fc8
		/* In several tokens certificates are marked as private */
Packit Service 7f6fc8
		if (login) {
Packit Service 7f6fc8
			/* Only try to login if login is required */
Packit Service 7f6fc8
			if (tok->loginRequired) {
Packit Service 7f6fc8
				/* Only try to login if a single slot matched to avoiding trying
Packit Service 7f6fc8
				 * the PIN against all matching slots */
Packit Service 7f6fc8
				if (matched_count == 1) {
Packit Service 7f6fc8
					if (!ctx_login(ctx, slot, tok,
Packit Service 7f6fc8
							ctx->ui_method, ctx->callback_data)) {
Packit Service 7f6fc8
						ctx_log(ctx, 0, "Login to token failed, returning NULL...\n");
Packit Service 7f6fc8
						goto error;
Packit Service 7f6fc8
					}
Packit Service 7f6fc8
				} else {
Packit Service 7f6fc8
					ctx_log(ctx, 0, "Multiple matching slots (%lu); will not try to"
Packit Service 7f6fc8
						" login\n", matched_count);
Packit Service 7f6fc8
					for (m = 0; m < matched_count; m++){
Packit Service 7f6fc8
						slot = matched_slots[m];
Packit Service 7f6fc8
						ctx_log(ctx, 0, "[%u] %s: %s\n", m + 1,
Packit Service 7f6fc8
								slot->description? slot->description:
Packit Service 7f6fc8
								"(no description)",
Packit Service 7f6fc8
								(slot->token && slot->token->label)?
Packit Service 7f6fc8
								slot->token->label: "no label");
Packit Service 7f6fc8
					}
Packit Service 7f6fc8
					goto error;
Packit Service 7f6fc8
				}
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		}
Packit 6b81fa
Packit Service 7f6fc8
		if (PKCS11_enumerate_certs(tok, &certs, &cert_count)) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Unable to enumerate certificates\n");
Packit Service 7f6fc8
			continue;
Packit 6b81fa
		}
Packit Service 7f6fc8
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found %u cert%s:\n", cert_count,
Packit Service 7f6fc8
				(cert_count <= 1) ? "" : "s");
Packit Service 7f6fc8
		if ((s_slot_cert_id && *s_slot_cert_id) &&
Packit Service 7f6fc8
				(cert_id_len != 0 || cert_label != NULL)) {
Packit Service 7f6fc8
			for (m = 0; m < cert_count; m++) {
Packit Service 7f6fc8
				PKCS11_CERT *k = certs + m;
Packit Service 7f6fc8
Packit Service 7f6fc8
				if (cert_label != NULL && strcmp(k->label, cert_label) == 0)
Packit Service 7f6fc8
					selected_cert = k;
Packit Service 7f6fc8
				if (cert_id_len != 0 && k->id_len == cert_id_len &&
Packit Service 7f6fc8
						memcmp(k->id, cert_id, cert_id_len) == 0)
Packit Service 7f6fc8
					selected_cert = k;
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		} else {
Packit Service 7f6fc8
			for (m = 0; m < cert_count; m++) {
Packit Service 7f6fc8
				PKCS11_CERT *k = certs + m;
Packit Service 7f6fc8
				if (k->id && *(k->id)) {
Packit Service 7f6fc8
					selected_cert = k; /* Use the first certificate with nonempty id */
Packit Service 7f6fc8
					break;
Packit Service 7f6fc8
				}
Packit 6b81fa
			}
Packit Service 7f6fc8
			if (!selected_cert)
Packit Service 7f6fc8
				selected_cert = certs; /* Use the first certificate */
Packit Service 7f6fc8
		}
Packit Service 7f6fc8
Packit Service 7f6fc8
		if (selected_cert) {
Packit Service 7f6fc8
			break;
Packit 6b81fa
		}
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	if (selected_cert != NULL) {
Packit 6b81fa
		x509 = X509_dup(selected_cert->x509);
Packit 6b81fa
	} else {
Packit 6b81fa
		if (login) /* Only print the error on the second attempt */
Packit 6b81fa
			ctx_log(ctx, 0, "Certificate not found.\n");
Packit 6b81fa
		x509 = NULL;
Packit 6b81fa
	}
Packit Service 7f6fc8
error:
Packit Service 7f6fc8
	/* Free the searched token data */
Packit Service 7f6fc8
	if (match_tok) {
Packit Service 7f6fc8
		OPENSSL_free(match_tok->model);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->manufacturer);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->serialnr);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->label);
Packit Service 7f6fc8
		OPENSSL_free(match_tok);
Packit Service 7f6fc8
	}
Packit Service 7f6fc8
Packit 6b81fa
	if (cert_label != NULL)
Packit 6b81fa
		OPENSSL_free(cert_label);
Packit Service 7f6fc8
	if (matched_slots != NULL)
Packit Service 7f6fc8
		free(matched_slots);
Packit 6b81fa
	return x509;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_load_cert(ENGINE_CTX *ctx, void *p)
Packit 6b81fa
{
Packit 6b81fa
	struct {
Packit 6b81fa
		const char *s_slot_cert_id;
Packit 6b81fa
		X509 *cert;
Packit 6b81fa
	} *parms = p;
Packit 6b81fa
Packit 6b81fa
	if (parms == NULL) {
Packit 6b81fa
		ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ERR_R_PASSED_NULL_PARAMETER);
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	if (parms->cert != NULL) {
Packit 6b81fa
		ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ENG_R_INVALID_PARAMETER);
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	ERR_clear_error();
Packit 6b81fa
	if (!ctx->force_login)
Packit 6b81fa
		parms->cert = ctx_load_cert(ctx, parms->s_slot_cert_id, 0);
Packit 6b81fa
	if (parms->cert == NULL) { /* Try again with login */
Packit 6b81fa
		ERR_clear_error();
Packit 6b81fa
		parms->cert = ctx_load_cert(ctx, parms->s_slot_cert_id, 1);
Packit 6b81fa
	}
Packit 6b81fa
	if (parms->cert == NULL) {
Packit 6b81fa
		if (!ERR_peek_last_error())
Packit 6b81fa
			ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ENG_R_OBJECT_NOT_FOUND);
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* Private and public key handling                                            */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
static EVP_PKEY *ctx_load_key(ENGINE_CTX *ctx, const char *s_slot_key_id,
Packit 6b81fa
		UI_METHOD *ui_method, void *callback_data,
Packit 6b81fa
		const int isPrivate, const int login)
Packit 6b81fa
{
Packit 6b81fa
	PKCS11_SLOT *slot;
Packit Service 7f6fc8
	PKCS11_SLOT *found_slot = NULL, **matched_slots = NULL;
Packit 6b81fa
	PKCS11_TOKEN *tok, *match_tok = NULL;
Packit 6b81fa
	PKCS11_KEY *keys, *selected_key = NULL;
Packit 6b81fa
	EVP_PKEY *pk = NULL;
Packit 6b81fa
	unsigned int key_count, n, m;
Packit 6b81fa
	unsigned char key_id[MAX_VALUE_LEN / 2];
Packit 6b81fa
	size_t key_id_len = sizeof(key_id);
Packit 6b81fa
	char *key_label = NULL;
Packit 6b81fa
	int slot_nr = -1;
Packit 6b81fa
	char tmp_pin[MAX_PIN_LENGTH+1];
Packit 6b81fa
	size_t tmp_pin_len = MAX_PIN_LENGTH;
Packit 6b81fa
	char flags[64];
Packit Service 7f6fc8
	size_t matched_count = 0;
Packit 6b81fa
Packit 6b81fa
	if (ctx_init_libp11(ctx)) /* Delayed libp11 initialization */
Packit 6b81fa
		goto error;
Packit 6b81fa
Packit 6b81fa
	ctx_log(ctx, 1, "Loading %s key \"%s\"\n",
Packit 6b81fa
		(char *)(isPrivate ? "private" : "public"),
Packit 6b81fa
		s_slot_key_id);
Packit 6b81fa
	if (s_slot_key_id && *s_slot_key_id) {
Packit 6b81fa
		if (!strncasecmp(s_slot_key_id, "pkcs11:", 7)) {
Packit 6b81fa
			n = parse_pkcs11_uri(ctx, s_slot_key_id, &match_tok,
Packit 6b81fa
				key_id, &key_id_len,
Packit 6b81fa
				tmp_pin, &tmp_pin_len, &key_label);
Packit 6b81fa
			if (!n) {
Packit 6b81fa
				ctx_log(ctx, 0,
Packit 6b81fa
					"The key ID is not a valid PKCS#11 URI\n"
Packit 6b81fa
					"The PKCS#11 URI format is defined by RFC7512\n");
Packit 6b81fa
				ENGerr(ENG_F_CTX_LOAD_KEY, ENG_R_INVALID_ID);
Packit 6b81fa
				goto error;
Packit 6b81fa
			}
Packit 6b81fa
			if (tmp_pin_len > 0 && tmp_pin[0] != 0) {
Packit Service 7f6fc8
				/* If the searched key is public, try without login once even
Packit Service 7f6fc8
				 * when the PIN is provided */
Packit Service 7f6fc8
				if (!login && isPrivate)
Packit 6b81fa
					goto error; /* Process on second attempt */
Packit 6b81fa
				ctx_destroy_pin(ctx);
Packit 6b81fa
				ctx->pin = OPENSSL_malloc(MAX_PIN_LENGTH+1);
Packit 6b81fa
				if (ctx->pin != NULL) {
Packit 6b81fa
					memset(ctx->pin, 0, MAX_PIN_LENGTH+1);
Packit 6b81fa
					memcpy(ctx->pin, tmp_pin, tmp_pin_len);
Packit 6b81fa
					ctx->pin_length = tmp_pin_len;
Packit 6b81fa
				}
Packit 6b81fa
			}
Packit 6b81fa
		} else {
Packit 6b81fa
			n = parse_slot_id_string(ctx, s_slot_key_id, &slot_nr,
Packit 6b81fa
				key_id, &key_id_len, &key_label);
Packit 6b81fa
			if (!n) {
Packit 6b81fa
				ctx_log(ctx, 0,
Packit 6b81fa
					"The key ID is not a valid PKCS#11 URI\n"
Packit 6b81fa
					"The PKCS#11 URI format is defined by RFC7512\n"
Packit 6b81fa
					"The legacy ENGINE_pkcs11 ID format is also "
Packit 6b81fa
					"still accepted for now\n");
Packit 6b81fa
				ENGerr(ENG_F_CTX_LOAD_KEY, ENG_R_INVALID_ID);
Packit 6b81fa
				goto error;
Packit 6b81fa
			}
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "Looking in slot %d for key: ",
Packit 6b81fa
			slot_nr);
Packit 6b81fa
		if (key_id_len != 0) {
Packit 6b81fa
			ctx_log(ctx, 1, "id=");
Packit 6b81fa
			dump_hex(ctx, 1, key_id, key_id_len);
Packit 6b81fa
		}
Packit 6b81fa
		if (key_id_len != 0 && key_label != NULL)
Packit 6b81fa
			ctx_log(ctx, 1, " ");
Packit 6b81fa
		if (key_label != NULL)
Packit 6b81fa
			ctx_log(ctx, 1, "label=%s", key_label);
Packit 6b81fa
		ctx_log(ctx, 1, "\n");
Packit 6b81fa
	}
Packit 6b81fa
Packit Service 7f6fc8
	matched_slots = (PKCS11_SLOT **)calloc(ctx->slot_count,
Packit Service 7f6fc8
		sizeof(PKCS11_SLOT *));
Packit Service 7f6fc8
	if (matched_slots == NULL) {
Packit Service 7f6fc8
		ctx_log(ctx, 0, "Could not allocate memory for matched slots\n");
Packit Service 7f6fc8
		goto error;
Packit Service 7f6fc8
	}
Packit Service 7f6fc8
Packit 6b81fa
	for (n = 0; n < ctx->slot_count; n++) {
Packit 6b81fa
		slot = ctx->slot_list + n;
Packit 6b81fa
		flags[0] = '\0';
Packit 6b81fa
		if (slot->token) {
Packit 6b81fa
			if (!slot->token->initialized)
Packit 6b81fa
				strcat(flags, "uninitialized, ");
Packit 6b81fa
			else if (!slot->token->userPinSet)
Packit 6b81fa
				strcat(flags, "no pin, ");
Packit 6b81fa
			if (slot->token->loginRequired)
Packit 6b81fa
				strcat(flags, "login, ");
Packit 6b81fa
			if (slot->token->readOnly)
Packit 6b81fa
				strcat(flags, "ro, ");
Packit 6b81fa
		} else {
Packit 6b81fa
			strcpy(flags, "no token");
Packit 6b81fa
		}
Packit 6b81fa
		if ((m = strlen(flags)) != 0) {
Packit 6b81fa
			flags[m - 2] = '\0';
Packit 6b81fa
		}
Packit 6b81fa
Packit 6b81fa
		if (slot_nr != -1 &&
Packit 6b81fa
			slot_nr == (int)PKCS11_get_slotid_from_slot(slot)) {
Packit 6b81fa
			found_slot = slot;
Packit 6b81fa
		}
Packit Service 7f6fc8
Packit 6b81fa
		if (match_tok && slot->token &&
Packit 6b81fa
				(match_tok->label == NULL ||
Packit 6b81fa
					!strcmp(match_tok->label, slot->token->label)) &&
Packit 6b81fa
				(match_tok->manufacturer == NULL ||
Packit 6b81fa
					!strcmp(match_tok->manufacturer, slot->token->manufacturer)) &&
Packit 6b81fa
				(match_tok->serialnr == NULL ||
Packit 6b81fa
					!strcmp(match_tok->serialnr, slot->token->serialnr)) &&
Packit 6b81fa
				(match_tok->model == NULL ||
Packit 6b81fa
					!strcmp(match_tok->model, slot->token->model))) {
Packit 6b81fa
			found_slot = slot;
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "[%lu] %-25.25s  %-16s",
Packit 6b81fa
			PKCS11_get_slotid_from_slot(slot),
Packit 6b81fa
			slot->description, flags);
Packit 6b81fa
		if (slot->token) {
Packit 6b81fa
			ctx_log(ctx, 1, "  (%s)",
Packit 6b81fa
				slot->token->label[0] ?
Packit 6b81fa
				slot->token->label : "no label");
Packit 6b81fa
		}
Packit 6b81fa
		ctx_log(ctx, 1, "\n");
Packit a90071
Packit Service 7f6fc8
		if (found_slot && found_slot->token && !found_slot->token->initialized)
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Found uninitialized token\n");
Packit Service 7f6fc8
Packit Service 7f6fc8
		/* Ignore slots without tokens or with uninitialized token */
Packit Service 7f6fc8
		if (found_slot && found_slot->token && found_slot->token->initialized) {
Packit Service 7f6fc8
			matched_slots[matched_count] = found_slot;
Packit Service 7f6fc8
			matched_count++;
Packit Service 7f6fc8
		}
Packit Service 7f6fc8
		found_slot = NULL;
Packit Service 975729
	}
Packit Service 7f6fc8
Packit Service 7f6fc8
	if (matched_count == 0) {
Packit Service 7f6fc8
		if (match_tok) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Specified object not found\n");
Packit 6b81fa
			goto error;
Packit 6b81fa
		}
Packit 6b81fa
Packit Service 7f6fc8
		/* If the legacy slot ID format was used */
Packit Service 7f6fc8
		if (slot_nr != -1) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Invalid slot number: %d\n", slot_nr);
Packit Service 7f6fc8
			goto error;
Packit Service 7f6fc8
		} else {
Packit Service 7f6fc8
			found_slot = PKCS11_find_token(ctx->pkcs11_ctx,
Packit Service 7f6fc8
								ctx->slot_list, ctx->slot_count);
Packit Service 7f6fc8
			/* Ignore if the the token is not initialized */
Packit Service 7f6fc8
			if (found_slot && found_slot->token &&
Packit Service 7f6fc8
					found_slot->token->initialized) {
Packit Service 7f6fc8
				matched_slots[matched_count] = found_slot;
Packit Service 7f6fc8
				matched_count++;
Packit Service 7f6fc8
			} else {
Packit Service 7f6fc8
				ctx_log(ctx, 0, "No tokens found\n");
Packit Service 7f6fc8
				goto error;
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		}
Packit 6b81fa
	}
Packit 6b81fa
Packit Service 7f6fc8
	for (n = 0; n < matched_count; n++) {
Packit Service 7f6fc8
		slot = matched_slots[n];
Packit Service 7f6fc8
		tok = slot->token;
Packit Service 7f6fc8
		if (tok == NULL) {
Packit Service 7f6fc8
			ctx_log(ctx, 0, "Found empty token\n");
Packit Service 7f6fc8
			break;
Packit Service 7f6fc8
		}
Packit 6b81fa
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found slot:  %s\n", slot->description);
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found token: %s\n", slot->token->label);
Packit Service 7f6fc8
Packit Service 7f6fc8
		/* Both private and public keys can have the CKA_PRIVATE attribute
Packit Service 7f6fc8
		 * set and thus require login (even to retrieve attributes!) */
Packit Service 7f6fc8
		if (login) {
Packit Service 7f6fc8
			/* Try to login only if login is required */
Packit Service 7f6fc8
			if (tok->loginRequired) {
Packit Service 7f6fc8
				/* Try to login only if a single slot matched to avoiding trying
Packit Service 7f6fc8
				 * the PIN against all matching slots */
Packit Service 7f6fc8
				if (matched_count == 1) {
Packit Service 7f6fc8
					if (!ctx_login(ctx, slot, tok, ui_method, callback_data)) {
Packit Service 7f6fc8
						ctx_log(ctx, 0, "Login to token failed, returning NULL...\n");
Packit Service 7f6fc8
						goto error;
Packit Service 7f6fc8
					}
Packit Service 7f6fc8
				} else {
Packit Service 7f6fc8
					ctx_log(ctx, 0, "Multiple matching slots (%lu); will not try to"
Packit Service 7f6fc8
						" login\n", matched_count);
Packit Service 7f6fc8
					for (m = 0; m < matched_count; m++){
Packit Service 7f6fc8
						slot = matched_slots[m];
Packit Service 7f6fc8
						ctx_log(ctx, 1, "[%u] %s: %s\n", m + 1,
Packit Service 7f6fc8
								slot->description? slot->description:
Packit Service 7f6fc8
								"(no description)",
Packit Service 7f6fc8
								(slot->token && slot->token->label)?
Packit Service 7f6fc8
								slot->token->label: "no label");
Packit Service 7f6fc8
					}
Packit Service 7f6fc8
					goto error;
Packit Service 7f6fc8
				}
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		}
Packit 6b81fa
Packit Service 7f6fc8
		if (isPrivate) {
Packit Service 7f6fc8
			/* Make sure there is at least one private key on the token */
Packit Service 7f6fc8
			if (PKCS11_enumerate_keys(tok, &keys, &key_count)) {
Packit Service 7f6fc8
				ctx_log(ctx, 0, "Unable to enumerate private keys\n");
Packit Service 7f6fc8
				continue;
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		} else {
Packit Service 7f6fc8
			/* Make sure there is at least one public key on the token */
Packit Service 7f6fc8
			if (PKCS11_enumerate_public_keys(tok, &keys, &key_count)) {
Packit Service 7f6fc8
				ctx_log(ctx, 0, "Unable to enumerate public keys\n");
Packit Service 7f6fc8
				continue;
Packit Service 7f6fc8
			}
Packit 6b81fa
		}
Packit Service 7f6fc8
		if (key_count == 0) {
Packit Service 7f6fc8
			if (login) /* Only print the error on the second attempt */
Packit Service 7f6fc8
				ctx_log(ctx, 0, "No %s keys found.\n",
Packit Service 7f6fc8
						(char *)(isPrivate ? "private" : "public"));
Packit Service 7f6fc8
			continue;
Packit a90071
		}
Packit Service 7f6fc8
		ctx_log(ctx, 1, "Found %u %s key%s:\n", key_count,
Packit Service 7f6fc8
				(char *)(isPrivate ? "private" : "public"),
Packit Service 7f6fc8
				(key_count == 1) ? "" : "s");
Packit Service 7f6fc8
Packit Service 7f6fc8
		if (s_slot_key_id && *s_slot_key_id &&
Packit Service 7f6fc8
				(key_id_len != 0 || key_label != NULL)) {
Packit Service 7f6fc8
			for (m = 0; m < key_count; m++) {
Packit Service 7f6fc8
				PKCS11_KEY *k = keys + m;
Packit Service 7f6fc8
Packit Service 7f6fc8
				ctx_log(ctx, 1, "  %2u %c%c id=", m + 1,
Packit Service 7f6fc8
						k->isPrivate ? 'P' : ' ',
Packit Service 7f6fc8
						k->needLogin ? 'L' : ' ');
Packit Service 7f6fc8
				dump_hex(ctx, 1, k->id, k->id_len);
Packit Service 7f6fc8
				ctx_log(ctx, 1, " label=%s\n", k->label);
Packit Service 7f6fc8
				if (key_label != NULL && strcmp(k->label, key_label) == 0)
Packit Service 7f6fc8
					selected_key = k;
Packit Service 7f6fc8
				if (key_id_len != 0 && k->id_len == key_id_len
Packit Service 7f6fc8
						&& memcmp(k->id, key_id, key_id_len) == 0)
Packit Service 7f6fc8
					selected_key = k;
Packit Service 7f6fc8
			}
Packit Service 7f6fc8
		} else {
Packit Service 7f6fc8
			selected_key = keys; /* Use the first key */
Packit Service 7f6fc8
		}
Packit Service 7f6fc8
Packit Service 7f6fc8
		if (selected_key) {
Packit Service 7f6fc8
			break;
Packit 6b81fa
		}
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	if (selected_key != NULL) {
Packit 6b81fa
		pk = isPrivate ?
Packit 6b81fa
			PKCS11_get_private_key(selected_key) :
Packit 6b81fa
			PKCS11_get_public_key(selected_key);
Packit 6b81fa
	} else {
Packit 6b81fa
		if (login) /* Only print the error on the second attempt */
Packit 6b81fa
			ctx_log(ctx, 0, "Key not found.\n");
Packit 6b81fa
		pk = NULL;
Packit 6b81fa
	}
Packit Service 7f6fc8
Packit 6b81fa
error:
Packit Service 7f6fc8
	/* Free the searched token data */
Packit Service 7f6fc8
	if (match_tok) {
Packit Service 7f6fc8
		OPENSSL_free(match_tok->model);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->manufacturer);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->serialnr);
Packit Service 7f6fc8
		OPENSSL_free(match_tok->label);
Packit Service 7f6fc8
		OPENSSL_free(match_tok);
Packit Service 7f6fc8
	}
Packit 6b81fa
	if (key_label != NULL)
Packit 6b81fa
		OPENSSL_free(key_label);
Packit Service 7f6fc8
	if (matched_slots != NULL)
Packit Service 7f6fc8
		free(matched_slots);
Packit 6b81fa
	return pk;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
EVP_PKEY *ctx_load_pubkey(ENGINE_CTX *ctx, const char *s_key_id,
Packit 6b81fa
		UI_METHOD *ui_method, void *callback_data)
Packit 6b81fa
{
Packit 6b81fa
	EVP_PKEY *pk = NULL;
Packit 6b81fa
Packit 6b81fa
	ERR_clear_error();
Packit 6b81fa
	if (!ctx->force_login)
Packit 6b81fa
		pk = ctx_load_key(ctx, s_key_id, ui_method, callback_data, 0, 0);
Packit 6b81fa
	if (pk == NULL) { /* Try again with login */
Packit 6b81fa
		ERR_clear_error();
Packit 6b81fa
		pk = ctx_load_key(ctx, s_key_id, ui_method, callback_data, 0, 1);
Packit 6b81fa
	}
Packit 6b81fa
	if (pk == NULL) {
Packit 6b81fa
		ctx_log(ctx, 0, "PKCS11_load_public_key returned NULL\n");
Packit 6b81fa
		if (!ERR_peek_last_error())
Packit 6b81fa
			ENGerr(ENG_F_CTX_LOAD_PUBKEY, ENG_R_OBJECT_NOT_FOUND);
Packit 6b81fa
		return NULL;
Packit 6b81fa
	}
Packit 6b81fa
	return pk;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
EVP_PKEY *ctx_load_privkey(ENGINE_CTX *ctx, const char *s_key_id,
Packit 6b81fa
		UI_METHOD *ui_method, void *callback_data)
Packit 6b81fa
{
Packit 6b81fa
	EVP_PKEY *pk = NULL;
Packit 6b81fa
Packit 6b81fa
	ERR_clear_error();
Packit 6b81fa
	if (!ctx->force_login)
Packit 6b81fa
		pk = ctx_load_key(ctx, s_key_id, ui_method, callback_data, 1, 0);
Packit 6b81fa
	if (pk == NULL) { /* Try again with login */
Packit 6b81fa
		ERR_clear_error();
Packit 6b81fa
		pk = ctx_load_key(ctx, s_key_id, ui_method, callback_data, 1, 1);
Packit 6b81fa
	}
Packit 6b81fa
	if (pk == NULL) {
Packit 6b81fa
		ctx_log(ctx, 0, "PKCS11_get_private_key returned NULL\n");
Packit 6b81fa
		if (!ERR_peek_last_error())
Packit 6b81fa
			ENGerr(ENG_F_CTX_LOAD_PRIVKEY, ENG_R_OBJECT_NOT_FOUND);
Packit 6b81fa
		return NULL;
Packit 6b81fa
	}
Packit 6b81fa
	return pk;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
/* Engine ctrl request handling                                               */
Packit 6b81fa
/******************************************************************************/
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_set_module(ENGINE_CTX *ctx, const char *modulename)
Packit 6b81fa
{
Packit 6b81fa
	OPENSSL_free(ctx->module);
Packit 6b81fa
	ctx->module = modulename ? OPENSSL_strdup(modulename) : NULL;
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/**
Packit 6b81fa
 * Set the PIN used for login. A copy of the PIN shall be made.
Packit 6b81fa
 *
Packit 6b81fa
 * If the PIN cannot be assigned, the value 0 shall be returned
Packit 6b81fa
 * and errno shall be set as follows:
Packit 6b81fa
 *
Packit 6b81fa
 *   EINVAL - a NULL PIN was supplied
Packit 6b81fa
 *   ENOMEM - insufficient memory to copy the PIN
Packit 6b81fa
 *
Packit 6b81fa
 * @param pin the pin to use for login. Must not be NULL.
Packit 6b81fa
 *
Packit 6b81fa
 * @return 1 on success, 0 on failure.
Packit 6b81fa
 */
Packit 6b81fa
static int ctx_ctrl_set_pin(ENGINE_CTX *ctx, const char *pin)
Packit 6b81fa
{
Packit 6b81fa
	/* Pre-condition check */
Packit 6b81fa
	if (pin == NULL) {
Packit 6b81fa
		ENGerr(ENG_F_CTX_CTRL_SET_PIN, ERR_R_PASSED_NULL_PARAMETER);
Packit 6b81fa
		errno = EINVAL;
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
Packit 6b81fa
	/* Copy the PIN. If the string cannot be copied, NULL
Packit 6b81fa
	 * shall be returned and errno shall be set. */
Packit 6b81fa
	ctx_destroy_pin(ctx);
Packit 6b81fa
	ctx->pin = OPENSSL_strdup(pin);
Packit 6b81fa
	if (ctx->pin == NULL) {
Packit 6b81fa
		ENGerr(ENG_F_CTX_CTRL_SET_PIN, ERR_R_MALLOC_FAILURE);
Packit 6b81fa
		errno = ENOMEM;
Packit 6b81fa
		return 0;
Packit 6b81fa
	}
Packit 6b81fa
	ctx->pin_length = strlen(ctx->pin);
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_inc_verbose(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	ctx->verbose++;
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_set_quiet(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	ctx->verbose = -1;
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_set_init_args(ENGINE_CTX *ctx, const char *init_args_orig)
Packit 6b81fa
{
Packit 6b81fa
	OPENSSL_free(ctx->init_args);
Packit 6b81fa
	ctx->init_args = init_args_orig ? OPENSSL_strdup(init_args_orig) : NULL;
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_set_user_interface(ENGINE_CTX *ctx, UI_METHOD *ui_method)
Packit 6b81fa
{
Packit 6b81fa
	ctx->ui_method = ui_method;
Packit 6b81fa
	if (ctx->pkcs11_ctx != NULL) /* libp11 is already initialized */
Packit 6b81fa
		PKCS11_set_ui_method(ctx->pkcs11_ctx,
Packit 6b81fa
			ctx->ui_method, ctx->callback_data);
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_set_callback_data(ENGINE_CTX *ctx, void *callback_data)
Packit 6b81fa
{
Packit 6b81fa
	ctx->callback_data = callback_data;
Packit 6b81fa
	if (ctx->pkcs11_ctx != NULL) /* libp11 is already initialized */
Packit 6b81fa
		PKCS11_set_ui_method(ctx->pkcs11_ctx,
Packit 6b81fa
			ctx->ui_method, ctx->callback_data);
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
static int ctx_ctrl_force_login(ENGINE_CTX *ctx)
Packit 6b81fa
{
Packit 6b81fa
	ctx->force_login = 1;
Packit 6b81fa
	return 1;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
int ctx_engine_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)())
Packit 6b81fa
{
Packit 6b81fa
	(void)i; /* We don't currently take integer parameters */
Packit 6b81fa
	(void)f; /* We don't currently take callback parameters */
Packit 6b81fa
	/*int initialised = ((pkcs11_dso == NULL) ? 0 : 1); */
Packit 6b81fa
	switch (cmd) {
Packit 6b81fa
	case CMD_MODULE_PATH:
Packit 6b81fa
		return ctx_ctrl_set_module(ctx, (const char *)p);
Packit 6b81fa
	case CMD_PIN:
Packit 6b81fa
		return ctx_ctrl_set_pin(ctx, (const char *)p);
Packit 6b81fa
	case CMD_VERBOSE:
Packit 6b81fa
		return ctx_ctrl_inc_verbose(ctx);
Packit 6b81fa
	case CMD_QUIET:
Packit 6b81fa
		return ctx_ctrl_set_quiet(ctx);
Packit 6b81fa
	case CMD_LOAD_CERT_CTRL:
Packit 6b81fa
		return ctx_ctrl_load_cert(ctx, p);
Packit 6b81fa
	case CMD_INIT_ARGS:
Packit 6b81fa
		return ctx_ctrl_set_init_args(ctx, (const char *)p);
Packit 6b81fa
	case ENGINE_CTRL_SET_USER_INTERFACE:
Packit 6b81fa
	case CMD_SET_USER_INTERFACE:
Packit 6b81fa
		return ctx_ctrl_set_user_interface(ctx, (UI_METHOD *)p);
Packit 6b81fa
	case ENGINE_CTRL_SET_CALLBACK_DATA:
Packit 6b81fa
	case CMD_SET_CALLBACK_DATA:
Packit 6b81fa
		return ctx_ctrl_set_callback_data(ctx, p);
Packit 6b81fa
	case CMD_FORCE_LOGIN:
Packit 6b81fa
		return ctx_ctrl_force_login(ctx);
Packit 6b81fa
	default:
Packit 6b81fa
		ENGerr(ENG_F_CTX_ENGINE_CTRL, ENG_R_UNKNOWN_COMMAND);
Packit 6b81fa
		break;
Packit 6b81fa
	}
Packit 6b81fa
	return 0;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/* vim: set noexpandtab: */