forked from OpenSC/libp11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibpkcs11.c
132 lines (114 loc) · 2.92 KB
/
libpkcs11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* libp11, a simple layer on to of PKCS#11 API
* Copyright (C) 2005 Olaf Kirch <[email protected]>
*
* 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
*/
/*
* Convenience pkcs11 library that can be linked into an application,
* and will bind to a specific pkcs11 module.
*
* Copyright (C) 2002 Olaf Kirch <[email protected]>
*/
#include "libp11-int.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#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 (*c_get_function_list)(CK_FUNCTION_LIST_PTR_PTR);
int rv;
if (!mspec)
return NULL;
mod = OPENSSL_malloc(sizeof(sc_pkcs11_module_t));
if (!mod)
return NULL;
memset(mod, 0, sizeof(sc_pkcs11_module_t));
mod->_magic = MAGIC;
#ifdef WIN32
mod->handle = LoadLibraryA(mspec);
#else
mod->handle = dlopen(mspec, RTLD_LAZY | RTLD_LOCAL);
#endif
if (!mod->handle) {
#ifndef WIN32
fprintf(stderr, "%s\n", dlerror());
#endif
goto failed;
}
#ifdef WIN32
c_get_function_list = (CK_C_GetFunctionList)
GetProcAddress(mod->handle, "C_GetFunctionList");
#else
{
/*
* Make compiler happy!
*/
void *p = dlsym(mod->handle, "C_GetFunctionList");
memmove(&c_get_function_list, &p, sizeof(void *));
}
#endif
if (!c_get_function_list) {
#ifndef WIN32
fprintf(stderr, "%s\n", dlerror());
#endif
goto failed;
}
rv = c_get_function_list(funcs);
if (rv == CKR_OK)
return mod;
failed:
C_UnloadModule((void *) 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) {
#ifdef WIN32
FreeLibrary(mod->handle);
#else
dlclose(mod->handle);
#endif
}
memset(mod, 0, sizeof(sc_pkcs11_module_t));
OPENSSL_free(mod);
return CKR_OK;
}
/* vim: set noexpandtab: */