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