Blame src/p11_misc.c

Packit 6b81fa
/* libp11, a simple layer on to of PKCS#11 API
Packit 6b81fa
 * Copyright (C) 2005 Olaf Kirch <okir@lst.de>
Packit 6b81fa
 * Copyright (C) 2015 MichaƂ Trojnara <Michal.Trojnara@stunnel.org>
Packit 6b81fa
 *
Packit 6b81fa
 *  This library is free software; you can redistribute it and/or
Packit 6b81fa
 *  modify it under the terms of the GNU Lesser General Public
Packit 6b81fa
 *  License as published by the Free Software Foundation; either
Packit 6b81fa
 *  version 2.1 of the License, or (at your option) any later version.
Packit 6b81fa
 *
Packit 6b81fa
 *  This library is distributed in the hope that it will be useful,
Packit 6b81fa
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6b81fa
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6b81fa
 *  Lesser General Public License for more details.
Packit 6b81fa
 *
Packit 6b81fa
 *  You should have received a copy of the GNU Lesser General Public
Packit 6b81fa
 *  License along with this library; if not, write to the Free Software
Packit 6b81fa
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
Packit 6b81fa
 */
Packit 6b81fa
Packit 6b81fa
#include "libp11-int.h"
Packit 6b81fa
#include <string.h>
Packit 6b81fa
#include <openssl/crypto.h>
Packit 6b81fa
Packit 6b81fa
/* PKCS11 strings are fixed size blank padded,
Packit 6b81fa
 * so when strduping them we must make sure
Packit 6b81fa
 * we stop at the end of the buffer, and while we're
Packit 6b81fa
 * at it it's nice to remove the padding */
Packit 6b81fa
char *pkcs11_strdup(char *mem, size_t size)
Packit 6b81fa
{
Packit 6b81fa
	char *res;
Packit 6b81fa
Packit 6b81fa
	while (size && mem[size - 1] == ' ')
Packit 6b81fa
		size--;
Packit 6b81fa
	res = OPENSSL_malloc(size + 1);
Packit 6b81fa
	if (res == NULL)
Packit 6b81fa
		return NULL;
Packit 6b81fa
	memcpy(res, mem, size);
Packit 6b81fa
	res[size] = '\0';
Packit 6b81fa
	return res;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
/*
Packit 6b81fa
 * CRYPTO dynlock wrappers: 0 is an invalid dynamic lock ID
Packit 6b81fa
 */
Packit 6b81fa
Packit 6b81fa
#if OPENSSL_VERSION_NUMBER < 0x10100004L || defined(LIBRESSL_VERSION_NUMBER)
Packit 6b81fa
Packit 6b81fa
int CRYPTO_THREAD_lock_new()
Packit 6b81fa
{
Packit 6b81fa
	int i;
Packit 6b81fa
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
		return 0; /* Dynamic callbacks not set */
Packit 6b81fa
	i = CRYPTO_get_new_dynlockid();
Packit 6b81fa
	if (i == 0)
Packit 6b81fa
		ERR_clear_error(); /* Dynamic locks are optional -> ignore */
Packit 6b81fa
	return i;
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
void CRYPTO_THREAD_lock_free(int i)
Packit 6b81fa
{
Packit 6b81fa
	if (i)
Packit 6b81fa
		CRYPTO_destroy_dynlockid(i);
Packit 6b81fa
}
Packit 6b81fa
Packit 6b81fa
#endif
Packit 6b81fa
Packit 6b81fa
/* vim: set noexpandtab: */