forked from OpenSC/libp11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheng_back.c
More file actions
305 lines (264 loc) · 8.63 KB
/
Copy patheng_back.c
File metadata and controls
305 lines (264 loc) · 8.63 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright (c) 2001 Markus Friedl
* Copyright (c) 2002 Juha Yrjölä
* Copyright (c) 2002 Olaf Kirch
* Copyright (c) 2003 Kevin Stefanik
* Copyright (c) 2016-2025 Michał Trojnara <Michal.Trojnara@stunnel.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "engine.h"
#include "util.h"
#include <stdio.h>
#include <string.h>
struct engine_ctx_st {
/* UI */
int ui_method_provided;
UI_METHOD *ui_method;
void *ui_data;
/* Logging */
int debug_level; /* level of debug output */
void (*vlog)(int, const char *, va_list); /* for the logging callback */
/* Current operations */
UTIL_CTX *util_ctx;
};
#if defined(_WIN32) || defined(_WIN64)
#define strncasecmp _strnicmp
#endif
/******************************************************************************/
/* Utility functions */
/******************************************************************************/
void ENGINE_CTX_log(ENGINE_CTX *ctx, int level, const char *format, ...)
{
va_list args;
va_start(args, format);
if (!ctx) {
vfprintf(stderr, format, args);
} else if (ctx->vlog) {
/* Log messages through a custom logging function */
const char *prefix = "pkcs11: ";
char *vlog_format = OPENSSL_malloc(strlen(prefix) + strlen(format) + 1);
if (!vlog_format) {
va_end(args);
return;
}
/* Copy and concatenate strings */
strcpy(vlog_format, prefix);
strcat(vlog_format, format);
ctx->vlog(level, (const char *)vlog_format, args);
OPENSSL_free(vlog_format);
} else if (level <= ctx->debug_level) {
if (level <= 4) { /* LOG_WARNING */
vfprintf(stderr, format, args);
} else {
vprintf(format, args);
}
}
va_end(args);
}
/******************************************************************************/
/* Initialization and cleanup */
/******************************************************************************/
ENGINE_CTX *ENGINE_CTX_new()
{
ENGINE_CTX *ctx;
char *mod;
ctx = OPENSSL_malloc(sizeof(ENGINE_CTX));
if (!ctx)
return NULL;
memset(ctx, 0, sizeof(ENGINE_CTX));
ctx->util_ctx = UTIL_CTX_new();
if (!ctx->util_ctx) {
OPENSSL_free(ctx);
return NULL;
}
mod = getenv("PKCS11_MODULE_PATH");
if (mod) {
UTIL_CTX_set_module(ctx->util_ctx, mod);
} else {
#ifdef DEFAULT_PKCS11_MODULE
UTIL_CTX_set_module(ctx->util_ctx, DEFAULT_PKCS11_MODULE);
#else
UTIL_CTX_set_module(ctx->util_ctx, NULL);
#endif
}
/* UI */
ctx->ui_method_provided = 0;
ctx->ui_method = NULL;
ctx->ui_data = NULL;
/* Logging */
ctx->debug_level = LOG_NOTICE;
return ctx;
}
/* Destroy the context allocated with ENGINE_CTX_new() */
int ENGINE_CTX_destroy(ENGINE_CTX *ctx)
{
if (ctx) {
UTIL_CTX_free(ctx->util_ctx);
OPENSSL_free(ctx);
}
return 1;
}
static int ENGINE_CTX_enumerate_slots(ENGINE_CTX *ctx)
{
return UTIL_CTX_enumerate_slots(ctx->util_ctx);
}
/* Function called from ENGINE_init() */
int ENGINE_CTX_init(ENGINE_CTX *ctx)
{
/* OpenSC implicitly locks CRYPTO_LOCK_ENGINE during C_GetSlotList().
* OpenSSL also locks CRYPTO_LOCK_ENGINE in ENGINE_init().
* Double-locking a non-recursive rwlock causes the application to
* crash or hang, depending on the locking library implementation. */
(void)ctx; /* squash the unused parameter warning */
return 1;
}
/* Finish engine operations initialized with ENGINE_CTX_init() */
int ENGINE_CTX_finish(ENGINE_CTX *ctx)
{
if (ctx) {
UTIL_CTX_free_libp11(ctx->util_ctx);
}
return 1;
}
/******************************************************************************/
/* Engine load public/private key */
/******************************************************************************/
EVP_PKEY *ENGINE_CTX_load_pubkey(ENGINE_CTX *ctx, const char *uri,
UI_METHOD *ui_method, void *ui_data)
{
EVP_PKEY *evp_pkey;
evp_pkey = UTIL_CTX_get_pubkey_from_uri(ctx->util_ctx, uri,
ui_method, ui_data);
if (!evp_pkey) {
ENGINE_CTX_log(ctx, LOG_ERR, "PKCS11_get_public_key returned NULL\n");
if (!ERR_peek_last_error())
ENGerr(ENG_F_CTX_LOAD_PUBKEY, ENG_R_OBJECT_NOT_FOUND);
return NULL;
}
return evp_pkey;
}
EVP_PKEY *ENGINE_CTX_load_privkey(ENGINE_CTX *ctx, const char *uri,
UI_METHOD *ui_method, void *ui_data)
{
EVP_PKEY *evp_pkey;
if (!ctx->ui_method_provided) { /* Cache ui_method, but not ui_data */
ctx->ui_method = ui_method;
UTIL_CTX_set_ui_method(ctx->util_ctx, ctx->ui_method, ctx->ui_data);
}
evp_pkey = UTIL_CTX_get_privkey_from_uri(ctx->util_ctx, uri,
ui_method, ui_data);
if (!evp_pkey) {
ENGINE_CTX_log(ctx, LOG_ERR, "PKCS11_get_private_key returned NULL\n");
if (!ERR_peek_last_error())
ENGerr(ENG_F_CTX_LOAD_PRIVKEY, ENG_R_OBJECT_NOT_FOUND);
return NULL;
}
return evp_pkey;
}
/******************************************************************************/
/* Engine ctrl request handling */
/******************************************************************************/
static int ENGINE_CTX_ctrl_set_debug_level(ENGINE_CTX *ctx, int level)
{
ctx->debug_level = level;
UTIL_CTX_set_debug_level(ctx->util_ctx, level);
return 1;
}
static int ENGINE_CTX_ctrl_load_cert(ENGINE_CTX *ctx, void *p)
{
struct {
const char *s_slot_cert_id;
X509 *cert;
} *parms = p;
X509 *cert;
if (!parms) {
ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if (parms->cert) {
ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ENG_R_INVALID_PARAMETER);
return 0;
}
cert = UTIL_CTX_get_cert_from_uri(ctx->util_ctx, parms->s_slot_cert_id,
ctx->ui_method, ctx->ui_data);
if (!cert) {
if (!ERR_peek_last_error())
ENGerr(ENG_F_CTX_CTRL_LOAD_CERT, ENG_R_OBJECT_NOT_FOUND);
return 0;
}
parms->cert = cert;
return 1;
}
static int ENGINE_CTX_ctrl_set_vlog(ENGINE_CTX *ctx, void *cb)
{
struct {
PKCS11_VLOG_A_CB vlog;
} *vlog_callback = cb;
ctx->vlog = vlog_callback->vlog;
UTIL_CTX_set_vlog_a(ctx->util_ctx, vlog_callback->vlog);
return 1;
}
int ENGINE_CTX_ctrl(ENGINE_CTX *ctx, int cmd, long i, void *p, void (*f)(void))
{
(void)i; /* We don't currently take integer parameters */
(void)f; /* We don't currently take callback parameters */
/*int initialised = ((pkcs11_dso == NULL) ? 0 : 1); */
switch (cmd) {
case CMD_MODULE_PATH:
return UTIL_CTX_set_module(ctx->util_ctx, (const char *)p);
case CMD_PIN:
return UTIL_CTX_set_pin(ctx->util_ctx, (const char *)p);
case CMD_VERBOSE:
return ENGINE_CTX_ctrl_set_debug_level(ctx, 7);
case CMD_QUIET:
return ENGINE_CTX_ctrl_set_debug_level(ctx, 0);
case CMD_LOAD_CERT_CTRL:
return ENGINE_CTX_ctrl_load_cert(ctx, p);
case CMD_INIT_ARGS:
return UTIL_CTX_set_init_args(ctx->util_ctx, (const char *)p);
case ENGINE_CTRL_SET_USER_INTERFACE:
case CMD_SET_USER_INTERFACE:
ctx->ui_method_provided = 1;
ctx->ui_method = p;
return UTIL_CTX_set_ui_method(ctx->util_ctx, ctx->ui_method, ctx->ui_data);
case ENGINE_CTRL_SET_CALLBACK_DATA:
case CMD_SET_CALLBACK_DATA:
ctx->ui_data = p;
return UTIL_CTX_set_ui_method(ctx->util_ctx, ctx->ui_method, ctx->ui_data);
case CMD_FORCE_LOGIN:
UTIL_CTX_set_force_login(ctx->util_ctx, 1);
return 1;
case CMD_RE_ENUMERATE:
return ENGINE_CTX_enumerate_slots(ctx);
case CMD_VLOG_A:
return ENGINE_CTX_ctrl_set_vlog(ctx, p);
case CMD_DEBUG_LEVEL:
return ENGINE_CTX_ctrl_set_debug_level(ctx, (int)i);
case CMD_KEYGEN:
return UTIL_CTX_keygen(ctx->util_ctx, p);
default:
ENGerr(ENG_F_CTX_ENGINE_CTRL, ENG_R_UNKNOWN_COMMAND);
break;
}
return 0;
}
/* vim: set noexpandtab: */