comparison Core/Pkcs11.cpp @ 2954:d924f9bb61cc

taking advantage of details in OrthancException
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Dec 2018 14:35:34 +0100
parents 878b59270859
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2953:210d5afd8f2b 2954:d924f9bb61cc
185 // "crypto/engine/eng_dyn.c" 185 // "crypto/engine/eng_dyn.c"
186 186
187 ENGINE* engine = ENGINE_new(); 187 ENGINE* engine = ENGINE_new();
188 if (!engine) 188 if (!engine)
189 { 189 {
190 LOG(ERROR) << "Cannot create an OpenSSL engine for PKCS#11"; 190 throw OrthancException(ErrorCode_InternalError,
191 throw OrthancException(ErrorCode_InternalError); 191 "Cannot create an OpenSSL engine for PKCS#11");
192 } 192 }
193 193
194 // Create a PKCS#11 context using libp11 194 // Create a PKCS#11 context using libp11
195 context_ = pkcs11_new(); 195 context_ = pkcs11_new();
196 if (!context_) 196 if (!context_)
197 { 197 {
198 LOG(ERROR) << "Cannot create a libp11 context for PKCS#11";
199 ENGINE_free(engine); 198 ENGINE_free(engine);
200 throw OrthancException(ErrorCode_InternalError); 199 throw OrthancException(ErrorCode_InternalError,
200 "Cannot create a libp11 context for PKCS#11");
201 } 201 }
202 202
203 if (!ENGINE_set_id(engine, PKCS11_ENGINE_ID) || 203 if (!ENGINE_set_id(engine, PKCS11_ENGINE_ID) ||
204 !ENGINE_set_name(engine, PKCS11_ENGINE_NAME) || 204 !ENGINE_set_name(engine, PKCS11_ENGINE_NAME) ||
205 !ENGINE_set_cmd_defns(engine, PKCS11_ENGINE_COMMANDS) || 205 !ENGINE_set_cmd_defns(engine, PKCS11_ENGINE_COMMANDS) ||
221 #endif 221 #endif
222 222
223 // Make OpenSSL know about our PKCS#11 engine 223 // Make OpenSSL know about our PKCS#11 engine
224 !ENGINE_add(engine)) 224 !ENGINE_add(engine))
225 { 225 {
226 LOG(ERROR) << "Cannot initialize the OpenSSL engine for PKCS#11";
227 pkcs11_finish(context_); 226 pkcs11_finish(context_);
228 ENGINE_free(engine); 227 ENGINE_free(engine);
229 throw OrthancException(ErrorCode_InternalError); 228 throw OrthancException(ErrorCode_InternalError,
229 "Cannot initialize the OpenSSL engine for PKCS#11");
230 } 230 }
231 231
232 // If the "ENGINE_add" worked, it gets a structural 232 // If the "ENGINE_add" worked, it gets a structural
233 // reference. We release our just-created reference. 233 // reference. We release our just-created reference.
234 ENGINE_free(engine); 234 ENGINE_free(engine);
251 const std::string& pin, 251 const std::string& pin,
252 bool verbose) 252 bool verbose)
253 { 253 {
254 if (pkcs11Initialized_) 254 if (pkcs11Initialized_)
255 { 255 {
256 LOG(ERROR) << "The PKCS#11 engine has already been initialized"; 256 throw OrthancException(ErrorCode_BadSequenceOfCalls,
257 throw OrthancException(ErrorCode_BadSequenceOfCalls); 257 "The PKCS#11 engine has already been initialized");
258 } 258 }
259 259
260 if (module.empty() || 260 if (module.empty() ||
261 !SystemToolbox::IsRegularFile(module)) 261 !SystemToolbox::IsRegularFile(module))
262 { 262 {
263 LOG(ERROR) << "The PKCS#11 module must be a path to one shared library (DLL or .so)"; 263 throw OrthancException(
264 throw OrthancException(ErrorCode_InexistentFile); 264 ErrorCode_InexistentFile,
265 "The PKCS#11 module must be a path to one shared library (DLL or .so)");
265 } 266 }
266 267
267 ENGINE* engine = LoadEngine(); 268 ENGINE* engine = LoadEngine();
268 if (!engine) 269 if (!engine)
269 { 270 {
270 LOG(ERROR) << "Cannot create an OpenSSL engine for PKCS#11"; 271 throw OrthancException(ErrorCode_InternalError,
271 throw OrthancException(ErrorCode_InternalError); 272 "Cannot create an OpenSSL engine for PKCS#11");
272 } 273 }
273 274
274 if (!ENGINE_ctrl_cmd_string(engine, "MODULE_PATH", module.c_str(), 0)) 275 if (!ENGINE_ctrl_cmd_string(engine, "MODULE_PATH", module.c_str(), 0))
275 { 276 {
276 LOG(ERROR) << "Cannot configure the OpenSSL dynamic engine for PKCS#11"; 277 throw OrthancException(ErrorCode_InternalError,
277 throw OrthancException(ErrorCode_InternalError); 278 "Cannot configure the OpenSSL dynamic engine for PKCS#11");
278 } 279 }
279 280
280 if (verbose) 281 if (verbose)
281 { 282 {
282 ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0); 283 ENGINE_ctrl_cmd_string(engine, "VERBOSE", NULL, 0);
283 } 284 }
284 285
285 if (!pin.empty() && 286 if (!pin.empty() &&
286 !ENGINE_ctrl_cmd_string(engine, "PIN", pin.c_str(), 0)) 287 !ENGINE_ctrl_cmd_string(engine, "PIN", pin.c_str(), 0))
287 { 288 {
288 LOG(ERROR) << "Cannot set the PIN code for PKCS#11"; 289 throw OrthancException(ErrorCode_InternalError,
289 throw OrthancException(ErrorCode_InternalError); 290 "Cannot set the PIN code for PKCS#11");
290 } 291 }
291 292
292 if (!ENGINE_init(engine)) 293 if (!ENGINE_init(engine))
293 { 294 {
294 LOG(ERROR) << "Cannot initialize the OpenSSL dynamic engine for PKCS#11"; 295 throw OrthancException(ErrorCode_InternalError,
295 throw OrthancException(ErrorCode_InternalError); 296 "Cannot initialize the OpenSSL dynamic engine for PKCS#11");
296 } 297 }
297 298
298 LOG(WARNING) << "The PKCS#11 engine has been successfully initialized"; 299 LOG(WARNING) << "The PKCS#11 engine has been successfully initialized";
299 pkcs11Initialized_ = true; 300 pkcs11Initialized_ = true;
300 } 301 }