opensc/src/common/libpkcs11.c

106 lines
2.6 KiB
C
Raw Normal View History

/*
* Convenience pkcs11 library that can be linked into an application,
* and will bind to a specific pkcs11 module.
*
* Copyright (C) 2002 Olaf Kirch <okir@suse.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2015-04-22 21:55:33 +00:00
#if HAVE_CONFIG_H
#include "config.h"
2015-04-22 21:55:33 +00:00
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "pkcs11/pkcs11.h"
#include "common/libscdl.h"
#include "common/libpkcs11.h"
#define MAGIC 0xd00bed00
struct sc_pkcs11_module {
unsigned int _magic;
void *handle;
};
typedef struct sc_pkcs11_module sc_pkcs11_module_t;
/*
* Load a module - this will load the shared object, call
* C_Initialize, and get the list of function pointers
*/
void *
C_LoadModule(const char *mspec, CK_FUNCTION_LIST_PTR_PTR funcs)
{
sc_pkcs11_module_t *mod;
CK_RV rv, (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR);
Do not cast the return value of malloc(3) and calloc(3) From http://en.wikipedia.org/wiki/Malloc#Casting_and_type_safety " Casting and type safety malloc returns a void pointer (void *), which indicates that it is a pointer to a region of unknown data type. One may "cast" (see type conversion) this pointer to a specific type, as in int *ptr = (int*)malloc(10 * sizeof (int)); When using C, this is considered bad practice; it is redundant under the C standard. Moreover, putting in a cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found. In the absence of a prototype for malloc, the C compiler will assume that malloc returns an int, and will issue a warning in a context such as the above, provided the error is not masked by a cast. On certain architectures and data models (such as LP64 on 64 bit systems, where long and pointers are 64 bit and int is 32 bit), this error can actually result in undefined behavior, as the implicitly declared malloc returns a 32 bit value whereas the actually defined function returns a 64 bit value. Depending on calling conventions and memory layout, this may result in stack smashing. The returned pointer need not be explicitly cast to a more specific pointer type, since ANSI C defines an implicit conversion between the void pointer type and other pointers to objects. An explicit cast of malloc's return value is sometimes performed because malloc originally returned a char *, but this cast is unnecessary in standard C code.[4][5] Omitting the cast, however, creates an incompatibility with C++, which does require it. The lack of a specific pointer type returned from malloc is type-unsafe behaviour: malloc allocates based on byte count but not on type. This distinguishes it from the C++ new operator that returns a pointer whose type relies on the operand. (see C Type Safety). " See also http://www.opensc-project.org/pipermail/opensc-devel/2010-August/014586.html git-svn-id: https://www.opensc-project.org/svnp/opensc/trunk@4636 c6295689-39f2-0310-b995-f0e70906c6a9
2010-08-18 15:08:51 +00:00
mod = calloc(1, sizeof(*mod));
if (mod == NULL) {
return NULL;
}
mod->_magic = MAGIC;
if (mspec == NULL) {
free(mod);
return NULL;
}
mod->handle = sc_dlopen(mspec);
if (mod->handle == NULL) {
fprintf(stderr, "sc_dlopen failed: %s\n", sc_dlerror());
goto failed;
}
/* Get the list of function pointers */
c_get_function_list = (CK_RV (*)(CK_FUNCTION_LIST_PTR_PTR))
sc_dlsym(mod->handle, "C_GetFunctionList");
if (!c_get_function_list)
goto failed;
rv = c_get_function_list(funcs);
if (rv == CKR_OK)
return (void *) mod;
else {
fprintf(stderr, "C_GetFunctionList failed %lx", rv);
rv = C_UnloadModule((void *) mod);
if (rv == CKR_OK)
mod = NULL; /* already freed */
}
failed:
2016-10-10 20:21:46 +00:00
free(mod);
return NULL;
}
/*
* Unload a pkcs11 module.
* The calling application is responsible for cleaning up
* and calling C_Finalize
*/
CK_RV
C_UnloadModule(void *module)
{
sc_pkcs11_module_t *mod = (sc_pkcs11_module_t *) module;
if (!mod || mod->_magic != MAGIC)
return CKR_ARGUMENTS_BAD;
if (mod->handle != NULL && sc_dlclose(mod->handle) < 0)
return CKR_FUNCTION_FAILED;
free(mod);
return CKR_OK;
}