Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Pkcs11.cpp @ 5022:3d0a084e1ace
fix to non-static variable
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 17 Jun 2022 07:20:19 +0200 |
parents | 6eff25f70121 |
children | 859f3668c181 |
rev | line source |
---|---|
2025 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
2025 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
2025 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
2025 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
2025 | 21 **/ |
22 | |
23 | |
24 #include "PrecompiledHeaders.h" | |
25 #include "Pkcs11.h" | |
26 | |
27 | |
28 #if defined(OPENSSL_NO_RSA) || defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_ECDSA) || defined(OPENSSL_NO_ECDH) | |
29 # error OpenSSL was compiled without support for RSA, EC, ECDSA or ECDH | |
30 #endif | |
31 | |
32 | |
33 #include "Logging.h" | |
34 #include "OrthancException.h" | |
2145 | 35 #include "SystemToolbox.h" |
2025 | 36 |
37 extern "C" | |
38 { | |
2145 | 39 # include <libp11/engine.h> // This is P11's "engine.h" |
40 # include <libp11/libp11.h> | |
2025 | 41 } |
42 | |
43 #include <openssl/engine.h> | |
44 | |
4741
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
45 #if OPENSSL_VERSION_NUMBER < 0x30000000L |
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
46 # warning You are linking Orthanc against OpenSSL 1.x, whose license is incompatible with the GPLv3+ used by Orthanc. Please update to OpenSSL 3.x, that uses the Apache 2 license. |
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
47 #endif |
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
48 |
2025 | 49 |
50 namespace Orthanc | |
51 { | |
52 namespace Pkcs11 | |
53 { | |
54 static const char* PKCS11_ENGINE_ID = "pkcs11"; | |
55 static const char* PKCS11_ENGINE_NAME = "PKCS#11 for Orthanc"; | |
56 static const ENGINE_CMD_DEFN PKCS11_ENGINE_COMMANDS[] = | |
57 { | |
58 { | |
59 CMD_MODULE_PATH, | |
60 "MODULE_PATH", | |
61 "Specifies the path to the PKCS#11 module shared library", | |
62 ENGINE_CMD_FLAG_STRING | |
63 }, | |
64 { | |
65 CMD_PIN, | |
66 "PIN", | |
67 "Specifies the pin code", | |
68 ENGINE_CMD_FLAG_STRING | |
69 }, | |
70 { | |
71 CMD_VERBOSE, | |
72 "VERBOSE", | |
73 "Print additional details", | |
74 ENGINE_CMD_FLAG_NO_INPUT | |
75 }, | |
76 { | |
77 CMD_LOAD_CERT_CTRL, | |
78 "LOAD_CERT_CTRL", | |
79 "Get the certificate from card", | |
80 ENGINE_CMD_FLAG_INTERNAL | |
81 }, | |
82 { | |
83 0, | |
84 NULL, | |
85 NULL, | |
86 0 | |
87 } | |
88 }; | |
89 | |
90 | |
91 static bool pkcs11Initialized_ = false; | |
92 static ENGINE_CTX *context_ = NULL; | |
93 | |
94 static int EngineInitialize(ENGINE* engine) | |
95 { | |
96 if (context_ == NULL) | |
97 { | |
98 return 0; | |
99 } | |
100 else | |
101 { | |
102 return pkcs11_init(context_); | |
103 } | |
104 } | |
105 | |
106 | |
107 static int EngineFinalize(ENGINE* engine) | |
108 { | |
109 if (context_ == NULL) | |
110 { | |
111 return 0; | |
112 } | |
113 else | |
114 { | |
115 return pkcs11_finish(context_); | |
116 } | |
117 } | |
118 | |
119 | |
120 static int EngineDestroy(ENGINE* engine) | |
121 { | |
122 return (context_ == NULL ? 0 : 1); | |
123 } | |
124 | |
125 | |
126 static int EngineControl(ENGINE *engine, | |
127 int command, | |
128 long i, | |
129 void *p, | |
130 void (*f) ()) | |
131 { | |
132 if (context_ == NULL) | |
133 { | |
134 return 0; | |
135 } | |
136 else | |
137 { | |
138 return pkcs11_engine_ctrl(context_, command, i, p, f); | |
139 } | |
140 } | |
141 | |
142 | |
143 static EVP_PKEY *EngineLoadPublicKey(ENGINE *engine, | |
144 const char *s_key_id, | |
145 UI_METHOD *ui_method, | |
146 void *callback_data) | |
147 { | |
148 if (context_ == NULL) | |
149 { | |
150 return 0; | |
151 } | |
152 else | |
153 { | |
154 return pkcs11_load_public_key(context_, s_key_id, ui_method, callback_data); | |
155 } | |
156 } | |
157 | |
158 | |
159 static EVP_PKEY *EngineLoadPrivateKey(ENGINE *engine, | |
160 const char *s_key_id, | |
161 UI_METHOD *ui_method, | |
162 void *callback_data) | |
163 { | |
164 if (context_ == NULL) | |
165 { | |
166 return 0; | |
167 } | |
168 else | |
169 { | |
170 return pkcs11_load_private_key(context_, s_key_id, ui_method, callback_data); | |
171 } | |
172 } | |
173 | |
174 | |
175 static ENGINE* LoadEngine() | |
176 { | |
177 // This function creates an engine for PKCS#11 and inspired by | |
178 // the "ENGINE_load_dynamic" function from OpenSSL, in file | |
179 // "crypto/engine/eng_dyn.c" | |
180 | |
181 ENGINE* engine = ENGINE_new(); | |
182 if (!engine) | |
183 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
184 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
185 "Cannot create an OpenSSL engine for PKCS#11"); |
2025 | 186 } |
187 | |
188 // Create a PKCS#11 context using libp11 | |
189 context_ = pkcs11_new(); | |
190 if (!context_) | |
191 { | |
192 ENGINE_free(engine); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
193 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
194 "Cannot create a libp11 context for PKCS#11"); |
2025 | 195 } |
196 | |
197 if (!ENGINE_set_id(engine, PKCS11_ENGINE_ID) || | |
198 !ENGINE_set_name(engine, PKCS11_ENGINE_NAME) || | |
199 !ENGINE_set_cmd_defns(engine, PKCS11_ENGINE_COMMANDS) || | |
200 | |
201 // Register the callback functions | |
202 !ENGINE_set_init_function(engine, EngineInitialize) || | |
203 !ENGINE_set_finish_function(engine, EngineFinalize) || | |
204 !ENGINE_set_destroy_function(engine, EngineDestroy) || | |
205 !ENGINE_set_ctrl_function(engine, EngineControl) || | |
206 !ENGINE_set_load_pubkey_function(engine, EngineLoadPublicKey) || | |
207 !ENGINE_set_load_privkey_function(engine, EngineLoadPrivateKey) || | |
208 | |
209 !ENGINE_set_RSA(engine, PKCS11_get_rsa_method()) || | |
3723
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
210 |
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
211 #if OPENSSL_VERSION_NUMBER < 0x10100000L // OpenSSL 1.0.2 |
2025 | 212 !ENGINE_set_ECDSA(engine, PKCS11_get_ecdsa_method()) || |
213 !ENGINE_set_ECDH(engine, PKCS11_get_ecdh_method()) || | |
3723
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
214 #else |
2025 | 215 !ENGINE_set_EC(engine, PKCS11_get_ec_key_method()) || |
216 #endif | |
217 | |
218 // Make OpenSSL know about our PKCS#11 engine | |
219 !ENGINE_add(engine)) | |
220 { | |
221 pkcs11_finish(context_); | |
222 ENGINE_free(engine); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
223 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
224 "Cannot initialize the OpenSSL engine for PKCS#11"); |
2025 | 225 } |
226 | |
227 // If the "ENGINE_add" worked, it gets a structural | |
228 // reference. We release our just-created reference. | |
229 ENGINE_free(engine); | |
230 | |
231 return ENGINE_by_id(PKCS11_ENGINE_ID); | |
232 } | |
233 | |
234 | |
235 bool IsInitialized() | |
236 { | |
237 return pkcs11Initialized_; | |
238 } | |
239 | |
240 const char* GetEngineIdentifier() | |
241 { | |
242 return PKCS11_ENGINE_ID; | |
243 } | |
244 | |
245 void Initialize(const std::string& module, | |
246 const std::string& pin, | |
247 bool verbose) | |
248 { | |
249 if (pkcs11Initialized_) | |
250 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
251 throw OrthancException(ErrorCode_BadSequenceOfCalls, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
252 "The PKCS#11 engine has already been initialized"); |
2025 | 253 } |
254 | |
255 if (module.empty() || | |
2145 | 256 !SystemToolbox::IsRegularFile(module)) |
2025 | 257 { |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
258 throw OrthancException( |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
259 ErrorCode_InexistentFile, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
260 "The PKCS#11 module must be a path to one shared library (DLL or .so)"); |
2025 | 261 } |
262 | |
263 ENGINE* engine = LoadEngine(); | |
264 if (!engine) | |
265 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
266 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
267 "Cannot create an OpenSSL engine for PKCS#11"); |
2025 | 268 } |
269 | |
270 if (!ENGINE_ctrl_cmd_string(engine, "MODULE_PATH", module.c_str(), 0)) | |
271 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
272 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
273 "Cannot configure the OpenSSL dynamic engine for PKCS#11"); |
2025 | 274 } |
275 | |
276 if (verbose) | |
277 { | |
278 ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0); | |
279 } | |
280 | |
281 if (!pin.empty() && | |
282 !ENGINE_ctrl_cmd_string(engine, "PIN", pin.c_str(), 0)) | |
283 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
284 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
285 "Cannot set the PIN code for PKCS#11"); |
2025 | 286 } |
287 | |
288 if (!ENGINE_init(engine)) | |
289 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
290 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
291 "Cannot initialize the OpenSSL dynamic engine for PKCS#11"); |
2025 | 292 } |
293 | |
294 LOG(WARNING) << "The PKCS#11 engine has been successfully initialized"; | |
295 pkcs11Initialized_ = true; | |
296 } | |
297 | |
298 | |
299 void Finalize() | |
300 { | |
301 // Nothing to do, the unregistration of the engine is | |
302 // automatically done by OpenSSL | |
303 } | |
304 } | |
305 } |