-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathp11_load.c
203 lines (176 loc) · 4.81 KB
/
p11_load.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* libp11, a simple layer on to of PKCS#11 API
* Copyright (C) 2005 Olaf Kirch <okir@lst.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
*/
#include "libp11-int.h"
#include <string.h>
/* Global number of active PKCS11_CTX objects */
static int pkcs11_global_data_refs = 0;
static void pkcs11_global_data_free(void);
/*
* Create a new context
*/
PKCS11_CTX *pkcs11_CTX_new(void)
{
PKCS11_CTX_private *cpriv = NULL;
PKCS11_CTX *ctx = NULL;
/* Load error strings */
ERR_load_PKCS11_strings();
cpriv = OPENSSL_malloc(sizeof(PKCS11_CTX_private));
if (!cpriv)
goto fail;
memset(cpriv, 0, sizeof(PKCS11_CTX_private));
ctx = OPENSSL_malloc(sizeof(PKCS11_CTX));
if (!ctx)
goto fail;
memset(ctx, 0, sizeof(PKCS11_CTX));
ctx->_private = cpriv;
cpriv->forkid = get_forkid();
pthread_mutex_init(&cpriv->fork_lock, 0);
pkcs11_global_data_refs++;
return ctx;
fail:
OPENSSL_free(cpriv);
OPENSSL_free(ctx);
return NULL;
}
/*
* Set private init args for module
*/
void pkcs11_CTX_init_args(PKCS11_CTX *ctx, const char *init_args)
{
PKCS11_CTX_private *cpriv = PRIVCTX(ctx);
/* Free previously duplicated string */
if (cpriv->init_args) {
OPENSSL_free(cpriv->init_args);
}
cpriv->init_args = init_args ? OPENSSL_strdup(init_args) : NULL;
}
/*
* Tell the PKCS11 to initialize itself
*/
static int pkcs11_initialize(PKCS11_CTX_private *cpriv)
{
CK_C_INITIALIZE_ARGS args;
int rv;
memset(&args, 0, sizeof(args));
/* Unconditionally say using OS locking primitives is OK */
args.flags |= CKF_OS_LOCKING_OK;
args.pReserved = cpriv->init_args;
rv = cpriv->method->C_Initialize(&args);
if (rv && rv != CKR_CRYPTOKI_ALREADY_INITIALIZED) {
CKRerr(P11_F_PKCS11_CTX_LOAD, rv);
return -1;
}
return 0;
}
/*
* Load the shared library, and initialize it.
*/
int pkcs11_CTX_load(PKCS11_CTX *ctx, const char *name)
{
PKCS11_CTX_private *cpriv = PRIVCTX(ctx);
CK_INFO ck_info;
int rv;
cpriv->handle = C_LoadModule(name, &cpriv->method);
if (!cpriv->handle) {
P11err(P11_F_PKCS11_CTX_LOAD, P11_R_LOAD_MODULE_ERROR);
return -1;
}
if (pkcs11_initialize(cpriv)) {
C_UnloadModule(cpriv->handle);
cpriv->handle = NULL;
return -1;
}
/* Get info on the library */
memset(&ck_info, 0, sizeof(ck_info));
rv = cpriv->method->C_GetInfo(&ck_info);
if (rv) {
cpriv->method->C_Finalize(NULL);
C_UnloadModule(cpriv->handle);
cpriv->handle = NULL;
CKRerr(P11_F_PKCS11_CTX_LOAD, rv);
return -1;
}
ctx->manufacturer = PKCS11_DUP(ck_info.manufacturerID);
ctx->description = PKCS11_DUP(ck_info.libraryDescription);
cpriv->cryptoki_version.major = ck_info.cryptokiVersion.major;
cpriv->cryptoki_version.minor = ck_info.cryptokiVersion.minor;
return 0;
}
/*
* Reinitialize (e.g., after a fork).
*/
int pkcs11_CTX_reload(PKCS11_CTX_private *cpriv)
{
if (!cpriv->method) /* Module not loaded */
return 0;
return pkcs11_initialize(cpriv);
}
/*
* Unload the shared library
*/
void pkcs11_CTX_unload(PKCS11_CTX *ctx)
{
PKCS11_CTX_private *cpriv = PRIVCTX(ctx);
if (!cpriv->method) /* Module not loaded */
return;
/* Tell the PKCS11 library to shut down */
if (cpriv->forkid == get_forkid())
cpriv->method->C_Finalize(NULL);
/* Unload the module */
C_UnloadModule(cpriv->handle);
cpriv->handle = NULL;
}
/*
* Free a context
*/
void pkcs11_CTX_free(PKCS11_CTX *ctx)
{
PKCS11_CTX_private *cpriv = PRIVCTX(ctx);
if (cpriv->init_args) {
OPENSSL_free(cpriv->init_args);
}
if (cpriv->handle) {
OPENSSL_free(cpriv->handle);
}
pthread_mutex_destroy(&cpriv->fork_lock);
OPENSSL_free(ctx->manufacturer);
OPENSSL_free(ctx->description);
OPENSSL_free(ctx->_private);
OPENSSL_free(ctx);
if (--pkcs11_global_data_refs == 0)
pkcs11_global_data_free();
}
static void pkcs11_global_data_free(void)
{
#ifndef OPENSSL_NO_RSA
pkcs11_rsa_method_free();
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10100002L
#ifndef OPENSSL_NO_EC
pkcs11_ec_key_method_free();
#endif /* OPENSSL_NO_EC */
#else /* OPENSSL_VERSION_NUMBER */
#ifndef OPENSSL_NO_ECDSA
pkcs11_ecdsa_method_free();
#endif /* OPENSSL_NO_ECDSA */
#ifndef OPENSSL_NO_ECDH
pkcs11_ecdh_method_free();
#endif /* OPENSSL_NO_ECDH */
#endif /* OPENSSL_VERSION_NUMBER */
}
/* vim: set noexpandtab: */