Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Pkcs11.cpp @ 5081:c673997507ea attach-custom-data
advanced storage cont
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 13 Sep 2022 11:02:43 +0200 |
parents | 859f3668c181 |
children | 0ea402b4d901 |
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 |
5073
859f3668c181
replaced macro "#warning" by "#pragma message" for Visual Studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4892
diff
changeset
|
46 # if defined(_MSC_VER) |
859f3668c181
replaced macro "#warning" by "#pragma message" for Visual Studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4892
diff
changeset
|
47 # pragma message("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.") |
859f3668c181
replaced macro "#warning" by "#pragma message" for Visual Studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4892
diff
changeset
|
48 # else |
859f3668c181
replaced macro "#warning" by "#pragma message" for Visual Studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4892
diff
changeset
|
49 # 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. |
859f3668c181
replaced macro "#warning" by "#pragma message" for Visual Studio
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4892
diff
changeset
|
50 # endif |
4741
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
51 #endif |
a6b7c29f5118
compiler warning about openssl license
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
52 |
2025 | 53 |
54 namespace Orthanc | |
55 { | |
56 namespace Pkcs11 | |
57 { | |
58 static const char* PKCS11_ENGINE_ID = "pkcs11"; | |
59 static const char* PKCS11_ENGINE_NAME = "PKCS#11 for Orthanc"; | |
60 static const ENGINE_CMD_DEFN PKCS11_ENGINE_COMMANDS[] = | |
61 { | |
62 { | |
63 CMD_MODULE_PATH, | |
64 "MODULE_PATH", | |
65 "Specifies the path to the PKCS#11 module shared library", | |
66 ENGINE_CMD_FLAG_STRING | |
67 }, | |
68 { | |
69 CMD_PIN, | |
70 "PIN", | |
71 "Specifies the pin code", | |
72 ENGINE_CMD_FLAG_STRING | |
73 }, | |
74 { | |
75 CMD_VERBOSE, | |
76 "VERBOSE", | |
77 "Print additional details", | |
78 ENGINE_CMD_FLAG_NO_INPUT | |
79 }, | |
80 { | |
81 CMD_LOAD_CERT_CTRL, | |
82 "LOAD_CERT_CTRL", | |
83 "Get the certificate from card", | |
84 ENGINE_CMD_FLAG_INTERNAL | |
85 }, | |
86 { | |
87 0, | |
88 NULL, | |
89 NULL, | |
90 0 | |
91 } | |
92 }; | |
93 | |
94 | |
95 static bool pkcs11Initialized_ = false; | |
96 static ENGINE_CTX *context_ = NULL; | |
97 | |
98 static int EngineInitialize(ENGINE* engine) | |
99 { | |
100 if (context_ == NULL) | |
101 { | |
102 return 0; | |
103 } | |
104 else | |
105 { | |
106 return pkcs11_init(context_); | |
107 } | |
108 } | |
109 | |
110 | |
111 static int EngineFinalize(ENGINE* engine) | |
112 { | |
113 if (context_ == NULL) | |
114 { | |
115 return 0; | |
116 } | |
117 else | |
118 { | |
119 return pkcs11_finish(context_); | |
120 } | |
121 } | |
122 | |
123 | |
124 static int EngineDestroy(ENGINE* engine) | |
125 { | |
126 return (context_ == NULL ? 0 : 1); | |
127 } | |
128 | |
129 | |
130 static int EngineControl(ENGINE *engine, | |
131 int command, | |
132 long i, | |
133 void *p, | |
134 void (*f) ()) | |
135 { | |
136 if (context_ == NULL) | |
137 { | |
138 return 0; | |
139 } | |
140 else | |
141 { | |
142 return pkcs11_engine_ctrl(context_, command, i, p, f); | |
143 } | |
144 } | |
145 | |
146 | |
147 static EVP_PKEY *EngineLoadPublicKey(ENGINE *engine, | |
148 const char *s_key_id, | |
149 UI_METHOD *ui_method, | |
150 void *callback_data) | |
151 { | |
152 if (context_ == NULL) | |
153 { | |
154 return 0; | |
155 } | |
156 else | |
157 { | |
158 return pkcs11_load_public_key(context_, s_key_id, ui_method, callback_data); | |
159 } | |
160 } | |
161 | |
162 | |
163 static EVP_PKEY *EngineLoadPrivateKey(ENGINE *engine, | |
164 const char *s_key_id, | |
165 UI_METHOD *ui_method, | |
166 void *callback_data) | |
167 { | |
168 if (context_ == NULL) | |
169 { | |
170 return 0; | |
171 } | |
172 else | |
173 { | |
174 return pkcs11_load_private_key(context_, s_key_id, ui_method, callback_data); | |
175 } | |
176 } | |
177 | |
178 | |
179 static ENGINE* LoadEngine() | |
180 { | |
181 // This function creates an engine for PKCS#11 and inspired by | |
182 // the "ENGINE_load_dynamic" function from OpenSSL, in file | |
183 // "crypto/engine/eng_dyn.c" | |
184 | |
185 ENGINE* engine = ENGINE_new(); | |
186 if (!engine) | |
187 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
188 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
189 "Cannot create an OpenSSL engine for PKCS#11"); |
2025 | 190 } |
191 | |
192 // Create a PKCS#11 context using libp11 | |
193 context_ = pkcs11_new(); | |
194 if (!context_) | |
195 { | |
196 ENGINE_free(engine); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
197 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
198 "Cannot create a libp11 context for PKCS#11"); |
2025 | 199 } |
200 | |
201 if (!ENGINE_set_id(engine, PKCS11_ENGINE_ID) || | |
202 !ENGINE_set_name(engine, PKCS11_ENGINE_NAME) || | |
203 !ENGINE_set_cmd_defns(engine, PKCS11_ENGINE_COMMANDS) || | |
204 | |
205 // Register the callback functions | |
206 !ENGINE_set_init_function(engine, EngineInitialize) || | |
207 !ENGINE_set_finish_function(engine, EngineFinalize) || | |
208 !ENGINE_set_destroy_function(engine, EngineDestroy) || | |
209 !ENGINE_set_ctrl_function(engine, EngineControl) || | |
210 !ENGINE_set_load_pubkey_function(engine, EngineLoadPublicKey) || | |
211 !ENGINE_set_load_privkey_function(engine, EngineLoadPrivateKey) || | |
212 | |
213 !ENGINE_set_RSA(engine, PKCS11_get_rsa_method()) || | |
3723
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
214 |
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
215 #if OPENSSL_VERSION_NUMBER < 0x10100000L // OpenSSL 1.0.2 |
2025 | 216 !ENGINE_set_ECDSA(engine, PKCS11_get_ecdsa_method()) || |
217 !ENGINE_set_ECDH(engine, PKCS11_get_ecdh_method()) || | |
3723
cc6d4edfe8fe
fix pkcs11 compilation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
218 #else |
2025 | 219 !ENGINE_set_EC(engine, PKCS11_get_ec_key_method()) || |
220 #endif | |
221 | |
222 // Make OpenSSL know about our PKCS#11 engine | |
223 !ENGINE_add(engine)) | |
224 { | |
225 pkcs11_finish(context_); | |
226 ENGINE_free(engine); | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
227 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
228 "Cannot initialize the OpenSSL engine for PKCS#11"); |
2025 | 229 } |
230 | |
231 // If the "ENGINE_add" worked, it gets a structural | |
232 // reference. We release our just-created reference. | |
233 ENGINE_free(engine); | |
234 | |
235 return ENGINE_by_id(PKCS11_ENGINE_ID); | |
236 } | |
237 | |
238 | |
239 bool IsInitialized() | |
240 { | |
241 return pkcs11Initialized_; | |
242 } | |
243 | |
244 const char* GetEngineIdentifier() | |
245 { | |
246 return PKCS11_ENGINE_ID; | |
247 } | |
248 | |
249 void Initialize(const std::string& module, | |
250 const std::string& pin, | |
251 bool verbose) | |
252 { | |
253 if (pkcs11Initialized_) | |
254 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
255 throw OrthancException(ErrorCode_BadSequenceOfCalls, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
256 "The PKCS#11 engine has already been initialized"); |
2025 | 257 } |
258 | |
259 if (module.empty() || | |
2145 | 260 !SystemToolbox::IsRegularFile(module)) |
2025 | 261 { |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
262 throw OrthancException( |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
263 ErrorCode_InexistentFile, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
264 "The PKCS#11 module must be a path to one shared library (DLL or .so)"); |
2025 | 265 } |
266 | |
267 ENGINE* engine = LoadEngine(); | |
268 if (!engine) | |
269 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
270 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
271 "Cannot create an OpenSSL engine for PKCS#11"); |
2025 | 272 } |
273 | |
274 if (!ENGINE_ctrl_cmd_string(engine, "MODULE_PATH", module.c_str(), 0)) | |
275 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
276 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
277 "Cannot configure the OpenSSL dynamic engine for PKCS#11"); |
2025 | 278 } |
279 | |
280 if (verbose) | |
281 { | |
282 ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0); | |
283 } | |
284 | |
285 if (!pin.empty() && | |
286 !ENGINE_ctrl_cmd_string(engine, "PIN", pin.c_str(), 0)) | |
287 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
288 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
289 "Cannot set the PIN code for PKCS#11"); |
2025 | 290 } |
291 | |
292 if (!ENGINE_init(engine)) | |
293 { | |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
294 throw OrthancException(ErrorCode_InternalError, |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
295 "Cannot initialize the OpenSSL dynamic engine for PKCS#11"); |
2025 | 296 } |
297 | |
298 LOG(WARNING) << "The PKCS#11 engine has been successfully initialized"; | |
299 pkcs11Initialized_ = true; | |
300 } | |
301 | |
302 | |
303 void Finalize() | |
304 { | |
305 // Nothing to do, the unregistration of the engine is | |
306 // automatically done by OpenSSL | |
307 } | |
308 } | |
309 } |