-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpkicore_pkcs11.c
278 lines (232 loc) · 5.92 KB
/
pkicore_pkcs11.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
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
/*
* PKCS#11 Functions
*
* Copyright (c) 2011 Nick Kossifidis <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include "pkicore.h"
#if defined(PKICORE_PKCS11) && defined(PKICORE_OPENSSL) && !defined(OPENSSL_NO_ENGINE)
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/engine.h>
#include "pkicore_pkcs11.h"
/**
* pki_ossl_get_crt_from_pkcs11 - Get a certificate from a PKCS#11 resource
*
* Tries to retrieve a certificate using the given URL and config.
*
* @char* url - The PKCS#11 URL
* @struct pki_config* p11_conf - Global config
*
* returns an X509 pointer or NULL
*/
X509*
pki_ossl_get_crt_from_pkcs11(char* url, struct pki_config* p11_conf)
{
char* tmp = NULL;
char* colon = NULL;
char* uscore = NULL;
char* cert_id = NULL;
int len = 0;
ENGINE *e = NULL;
X509* cert = NULL;
int ret = PKI_OK;
struct {
const char* cert_id;
X509* cert;
} params;
params.cert_id = NULL;
params.cert = NULL;
/* Copy string to tmp
* for later use */
tmp = malloc(strlen(url) + 1);
memset(tmp, 0, strlen(url) + 1);
strncpy(tmp, url, strlen(url) + 1);
/* Get pkcs11 engine by id */
e = ENGINE_by_id("pkcs11");
if (!e) {
pki_msg(0,"PKCS11",
"Couldn't find pkcs11 engine\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Set module path for the pkcs11 provider library */
ret = ENGINE_ctrl_cmd_string(e, "MODULE_PATH",
p11_conf->pkcs11_provider_lib, 0);
if (!ret) {
pki_msg(0,"PKCS11",
"ENGINE_ctrl_cmt_string failed\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* Initialize the engine */
ret = ENGINE_init(e);
if (!ret) {
pki_msg(0,"PKCS11",
"ENGINE_init failed\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
/* ENGINE_init() returned a functional
* reference, so free the structural
* reference from ENGINE_by_id(). */
ENGINE_free(e);
/* Prepare slot_id in the form
* <slot>:<id> */
colon = strchr(tmp, ':');
if(!colon)
goto cleanup;
cert_id = colon + 1;
uscore = strchr(cert_id, '_');
if(uscore) {
uscore[0] = ':';
}
/* Send the command to ask for the certificate */
params.cert_id = cert_id;
ret = ENGINE_ctrl_cmd(e, "LOAD_CERT_CTRL", 0, ¶ms, NULL, 0);
if (!ret) {
pki_msg(0,"PKCS11",
"ENGINE_ctrl_cmd failed\n");
ret = PKI_OPENSSL_ERR;
goto cleanup;
}
if (!params.cert) {
pki_msg(0,"PKCS11",
"No cert found\n");
ret = PKI_NOTFOUND_ERR;
goto cleanup;
}
cert = params.cert;
X509_print_fp(stdout, cert);
pki_msg(2,"PKCS11",
"Got certificate from %s\n",
cert_id);
/* Make sure we don't free e again on cleanup, ENGINE_cleanup()
* will handle that */
e = NULL;
cleanup:
if(e)
ENGINE_free(e);
free(tmp);
ENGINE_cleanup();
ERR_print_errors_fp(stderr);
if(ret)
pki_set_ret_code(ret);
return cert;
}
EVP_PKEY*
pki_ossl_get_pkey_from_pkcs11(char* url, struct pki_config* p11_conf)
{
char* tmp = NULL;
char* colon = NULL;
char* uscore = NULL;
char* cert_id = NULL;
int len = 0;
int ret = 0;
BIO *debugbio = NULL;
ENGINE *e = NULL;
EVP_PKEY *pkey = NULL;
struct {
const void *password;
const char *prompt_info;
} cb_data;
cb_data.password = p11_conf->privkey_pass;
cb_data.prompt_info = url;
/* Copy string to tmp
* for later use */
tmp = malloc(strlen(url) + 1);
strncpy(tmp, url, strlen(url) + 1);
/* Get pkcs11 engine by id */
e = ENGINE_by_id("pkcs11");
if (!e) {
pki_msg(0,"PKCS11", "Couldn't find pkcs11 engine\n");
goto cleanup;
}
/* Set module path for the pkcs11 provider library */
ret = ENGINE_ctrl_cmd_string(e, "MODULE_PATH",
p11_conf->pkcs11_provider_lib, 0);
if (!ret) {
pki_msg(0,"PKCS11", "ENGINE_ctrl_cmt_string failed\n");
goto cleanup;
}
/* Initialize the engine */
ret = ENGINE_init(e);
if (!ret) {
pki_msg(0,"PKCS11", "ENGINE_init failed\n");
goto cleanup;
}
/* ENGINE_init() returned a functional
* reference, so free the structural
* reference from ENGINE_by_id(). */
ENGINE_free(e);
/* Prepare slot_id in the form
* <slot>:<id> */
colon = strchr(tmp, ':');
if(!colon)
goto cleanup;
cert_id = colon + 1;
uscore = strchr(cert_id, '_');
if(uscore) {
uscore[0] = ':';
}
/* Try to load the private key from the token */
pkey = ENGINE_load_private_key(e, cert_id, NULL, NULL);
if (!pkey) {
pki_msg(0,"PKCS11", "ENGINE_load_private_key failed\n");
goto cleanup;
}
debugbio = BIO_new_fp (stderr, BIO_NOCLOSE);
EVP_PKEY_print_private(debugbio, pkey, 0, NULL);
pki_msg(1,"PKCS11", "got private key from %s\n", cert_id);
/* Make sure we don't free e again on cleanup, ENGINE_cleanup()
* will handle that */
e = NULL;
cleanup:
if(debugbio)
BIO_free_all(debugbio);
if(e)
ENGINE_free(e);
free(tmp);
ENGINE_cleanup();
ERR_print_errors_fp(stderr);
return pkey;
}
X509*
pki_ossl_get_crt_from_pkcs11_with_login(char* url, struct pki_config* p11_conf)
{
int ret = 0;
EVP_PKEY *pkey = NULL;
X509* cert = NULL;
cert = pki_ossl_get_crt_from_pkcs11(url, p11_conf);
if(!cert)
goto cleanup;
pkey = pki_ossl_get_pkey_from_pkcs11(url, p11_conf);
if(!pkey)
goto cleanup;
ret = X509_check_private_key(cert, pkey);
if(!ret) {
X509_free(cert);
pki_msg(0, "PKCS11",
"private key doesn't match the certificate! %s", url);
return NULL;
}
cleanup:
if(pkey)
EVP_PKEY_free(pkey);
return cert;
}
#endif