| 1 |
/* |
| 2 |
* Copyright (c) 2010 SURFnet bv |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
OSSLCryptoFactory.cpp |
| 29 |
|
| 30 |
This is an OpenSSL based cryptographic algorithm factory |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "MutexFactory.h" |
| 35 |
#include "OSSLCryptoFactory.h" |
| 36 |
#include "OSSLRNG.h" |
| 37 |
#include "OSSLAES.h" |
| 38 |
#include "OSSLDES.h" |
| 39 |
#include "OSSLMD5.h" |
| 40 |
#include "OSSLSHA1.h" |
| 41 |
#include "OSSLSHA224.h" |
| 42 |
#include "OSSLSHA256.h" |
| 43 |
#include "OSSLSHA384.h" |
| 44 |
#include "OSSLSHA512.h" |
| 45 |
#include "OSSLCMAC.h" |
| 46 |
#include "OSSLHMAC.h" |
| 47 |
#include "OSSLRSA.h" |
| 48 |
#include "OSSLDSA.h" |
| 49 |
#include "OSSLDH.h" |
| 50 |
#ifdef WITH_ECC |
| 51 |
#include "OSSLECDH.h" |
| 52 |
#include "OSSLECDSA.h" |
| 53 |
#endif |
| 54 |
#ifdef WITH_GOST |
| 55 |
#include "OSSLGOSTR3411.h" |
| 56 |
#include "OSSLGOST.h" |
| 57 |
#endif |
| 58 |
#ifdef WITH_EDDSA |
| 59 |
#include "OSSLEDDSA.h" |
| 60 |
#endif |
| 61 |
|
| 62 |
#include <algorithm> |
| 63 |
#include <string.h> |
| 64 |
#include <openssl/opensslv.h> |
| 65 |
#include <openssl/ssl.h> |
| 66 |
#include <openssl/crypto.h> |
| 67 |
#include <openssl/err.h> |
| 68 |
#include <openssl/rand.h> |
| 69 |
#ifdef WITH_GOST |
| 70 |
#include <openssl/objects.h> |
| 71 |
#endif |
| 72 |
|
| 73 |
#ifdef WITH_FIPS |
| 74 |
// Initialise the FIPS 140-2 selftest status |
| 75 |
bool OSSLCryptoFactory::FipsSelfTestStatus = false; |
| 76 |
#endif |
| 77 |
|
| 78 |
static unsigned nlocks; |
| 79 |
static Mutex** locks; |
| •80 |
|
| 81 |
// Mutex callback |
| 82 |
void lock_callback(int mode, int n, const char* file, int line) |
| 83 |
{ |
| 84 |
if ((unsigned) n >= nlocks) |
| 85 |
{ |
| 86 |
ERROR_MSG("out of range [0..%u[ lock %d at %s:%d", |
| 87 |
nlocks, n, file, line); |
| 88 |
|
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
Mutex* mtx = locks[(unsigned) n]; |
| 93 |
|
| 94 |
if (mode & CRYPTO_LOCK) |
| 95 |
{ |
| 96 |
mtx->lock(); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
mtx->unlock(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| •104 |
// Constructor |
| 105 |
OSSLCryptoFactory::OSSLCryptoFactory() |
| 106 |
{ |
| 107 |
// Multi-thread support |
| 108 |
nlocks = CRYPTO_num_locks(); |
| 109 |
locks = new Mutex*[nlocks]; |
| 110 |
for (unsigned i = 0; i < nlocks; i++) |
| 111 |
{ |
| 112 |
locks[i] = MutexFactory::i()->getMutex(); |
| 113 |
} |
| 114 |
|
| 115 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 116 |
setLockingCallback = false; |
| 117 |
if (CRYPTO_get_locking_callback() == NULL) |
| 118 |
{ |
| 119 |
CRYPTO_set_locking_callback(lock_callback); |
| 120 |
setLockingCallback = true; |
| 121 |
} |
| •122 |
#endif |
| 123 |
|
| 124 |
#ifdef WITH_FIPS |
| 125 |
// Already in FIPS mode on reenter (avoiding selftests) |
| 126 |
if (!FIPS_mode()) |
| 127 |
{ |
| 128 |
FipsSelfTestStatus = false; |
| 129 |
if (!FIPS_mode_set(1)) |
| 130 |
{ |
| 131 |
ERROR_MSG("can't enter into FIPS mode"); |
| 132 |
return; |
| 133 |
} |
| 134 |
} else { |
| 135 |
// Undo RAND_cleanup() |
| 136 |
RAND_init_fips(); |
| 137 |
} |
| 138 |
FipsSelfTestStatus = true; |
| 139 |
#endif |
| 140 |
|
| 141 |
// Initialise OpenSSL |
| 142 |
OpenSSL_add_all_algorithms(); |
| 143 |
|
| 144 |
#if !( OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) ) |
| 145 |
// Make sure RDRAND is loaded first |
| 146 |
ENGINE_load_rdrand(); |
| 147 |
#endif |
| 148 |
// Locate the engine |
| 149 |
rdrand_engine = ENGINE_by_id("rdrand"); |
| 150 |
// Use RDRAND if available |
| 151 |
if (rdrand_engine != NULL) |
| 152 |
{ |
| 153 |
// Initialize RDRAND engine |
| 154 |
if (!ENGINE_init(rdrand_engine)) |
| 155 |
{ |
| 156 |
WARNING_MSG("ENGINE_init returned %lu\n", ERR_get_error()); |
| 157 |
} |
| 158 |
// Set RDRAND engine as the default for RAND_ methods |
| 159 |
else if (!ENGINE_set_default(rdrand_engine, ENGINE_METHOD_RAND)) |
| 160 |
{ |
| 161 |
WARNING_MSG("ENGINE_set_default returned %lu\n", ERR_get_error()); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Initialise the one-and-only RNG |
| 166 |
rng = new OSSLRNG(); |
| 167 |
|
| 168 |
#ifdef WITH_GOST |
| 169 |
// Load engines |
| 170 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 171 |
ENGINE_load_builtin_engines(); |
| 172 |
#else |
| 173 |
OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
| 174 |
OPENSSL_INIT_ENGINE_RDRAND | |
| 175 |
OPENSSL_INIT_LOAD_CRYPTO_STRINGS | |
| 176 |
OPENSSL_INIT_ADD_ALL_CIPHERS | |
| 177 |
OPENSSL_INIT_ADD_ALL_DIGESTS | |
| 178 |
OPENSSL_INIT_LOAD_CONFIG, NULL); |
| 179 |
#endif |
| 180 |
|
| 181 |
// Initialise the GOST engine |
| 182 |
eg = ENGINE_by_id("gost"); |
| 183 |
if (eg == NULL) |
| 184 |
{ |
| 185 |
ERROR_MSG("can't get the GOST engine"); |
| 186 |
return; |
| 187 |
} |
| 188 |
if (ENGINE_init(eg) <= 0) |
| 189 |
{ |
| 190 |
ENGINE_free(eg); |
| 191 |
eg = NULL; |
| 192 |
ERROR_MSG("can't initialize the GOST engine"); |
| 193 |
return; |
| 194 |
} |
| 195 |
// better than digest_gost |
| 196 |
EVP_GOST_34_11 = ENGINE_get_digest(eg, NID_id_GostR3411_94); |
| 197 |
if (EVP_GOST_34_11 == NULL) |
| 198 |
{ |
| 199 |
ERROR_MSG("can't get the GOST digest"); |
| 200 |
goto err; |
| 201 |
} |
| 202 |
// from the openssl.cnf |
| 203 |
if (ENGINE_register_pkey_asn1_meths(eg) <= 0) |
| 204 |
{ |
| 205 |
ERROR_MSG("can't register ASN.1 for the GOST engine"); |
| 206 |
goto err; |
| 207 |
} |
| 208 |
if (ENGINE_ctrl_cmd_string(eg, |
| 209 |
"CRYPT_PARAMS", |
| 210 |
"id-Gost28147-89-CryptoPro-A-ParamSet", |
| 211 |
0) <= 0) |
| 212 |
{ |
| 213 |
ERROR_MSG("can't set params of the GOST engine"); |
| 214 |
goto err; |
| 215 |
} |
| 216 |
return; |
| 217 |
|
| 218 |
err: |
| 219 |
ENGINE_finish(eg); |
| 220 |
ENGINE_free(eg); |
| 221 |
eg = NULL; |
| 222 |
return; |
| 223 |
#endif |
| 224 |
} |
| 225 |
|
| 226 |
// Destructor |
| 227 |
OSSLCryptoFactory::~OSSLCryptoFactory() |
| 228 |
{ |
| •229 |
bool ossl_shutdown = false; |
| 230 |
|
| 231 |
#if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER) |
| •232 |
// OpenSSL 1.1.0+ will register an atexit() handler to run |
| 233 |
// OPENSSL_cleanup(). If that has already happened we must |
| 234 |
// not attempt to free any ENGINEs because they'll already |
| 235 |
// have been destroyed and the use-after-free would cause |
| 236 |
// a deadlock or crash. |
| 237 |
// |
| 238 |
// Detect that situation because reinitialisation will fail |
| 239 |
// after OPENSSL_cleanup() has run. |
| 240 |
(void)ERR_set_mark(); |
| 241 |
ossl_shutdown = !OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_RDRAND, NULL); |
| 242 |
(void)ERR_pop_to_mark(); |
| 243 |
#endif |
| 244 |
if (!ossl_shutdown) |
| 245 |
{ |
| 246 |
#ifdef WITH_GOST |
| 247 |
// Finish the GOST engine |
| 248 |
if (eg != NULL) |
| 249 |
{ |
| 250 |
ENGINE_finish(eg); |
| 251 |
ENGINE_free(eg); |
| 252 |
eg = NULL; |
| 253 |
} |
| 254 |
#endif |
| 255 |
|
| •256 |
// Finish the rd_rand engine |
| 257 |
ENGINE_finish(rdrand_engine); |
| 258 |
ENGINE_free(rdrand_engine); |
| 259 |
rdrand_engine = NULL; |
| 260 |
|
| •261 |
// Recycle locks |
| 262 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 263 |
if (setLockingCallback) |
| 264 |
{ |
| 265 |
CRYPTO_set_locking_callback(NULL); |
| 266 |
} |
| 267 |
#endif |
| 268 |
} |
| 269 |
// Destroy the one-and-only RNG |
| 270 |
delete rng; |
| 271 |
|
| 272 |
for (unsigned i = 0; i < nlocks; i++) |
| 273 |
{ |
| 274 |
MutexFactory::i()->recycleMutex(locks[i]); |
| 275 |
} |
| 276 |
delete[] locks; |
| 277 |
} |
| 278 |
|
| 279 |
// Return the one-and-only instance |
| 280 |
OSSLCryptoFactory* OSSLCryptoFactory::i() |
| 281 |
{ |
| 282 |
if (!instance.get()) |
| 283 |
{ |
| 284 |
instance.reset(new OSSLCryptoFactory()); |
| 285 |
} |
| 286 |
|
| 287 |
return instance.get(); |
| 288 |
} |
| 289 |
|
| 290 |
// This will destroy the one-and-only instance. |
| 291 |
void OSSLCryptoFactory::reset() |
| 292 |
{ |
| 293 |
instance.reset(); |
| 294 |
} |
| 295 |
|
| 296 |
#ifdef WITH_FIPS |
| 297 |
bool OSSLCryptoFactory::getFipsSelfTestStatus() const |
| 298 |
{ |
| 299 |
return FipsSelfTestStatus; |
| 300 |
} |
| 301 |
#endif |
| 302 |
|
| 303 |
// Create a concrete instance of a symmetric algorithm |
| 304 |
SymmetricAlgorithm* OSSLCryptoFactory::getSymmetricAlgorithm(SymAlgo::Type algorithm) |
| 305 |
{ |
| 306 |
switch (algorithm) |
| 307 |
{ |
| 308 |
case SymAlgo::AES: |
| 309 |
return new OSSLAES(); |
| 310 |
case SymAlgo::DES: |
| 311 |
case SymAlgo::DES3: |
| 312 |
return new OSSLDES(); |
| 313 |
default: |
| 314 |
// No algorithm implementation is available |
| 315 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 316 |
|
| 317 |
return NULL; |
| 318 |
} |
| 319 |
|
| 320 |
// No algorithm implementation is available |
| 321 |
return NULL; |
| 322 |
} |
| 323 |
|
| 324 |
// Create a concrete instance of an asymmetric algorithm |
| 325 |
AsymmetricAlgorithm* OSSLCryptoFactory::getAsymmetricAlgorithm(AsymAlgo::Type algorithm) |
| 326 |
{ |
| 327 |
switch (algorithm) |
| 328 |
{ |
| 329 |
case AsymAlgo::RSA: |
| 330 |
return new OSSLRSA(); |
| 331 |
case AsymAlgo::DSA: |
| 332 |
return new OSSLDSA(); |
| 333 |
case AsymAlgo::DH: |
| 334 |
return new OSSLDH(); |
| 335 |
#ifdef WITH_ECC |
| 336 |
case AsymAlgo::ECDH: |
| 337 |
return new OSSLECDH(); |
| 338 |
case AsymAlgo::ECDSA: |
| 339 |
return new OSSLECDSA(); |
| 340 |
#endif |
| 341 |
#ifdef WITH_GOST |
| 342 |
case AsymAlgo::GOST: |
| 343 |
return new OSSLGOST(); |
| 344 |
#endif |
| 345 |
#ifdef WITH_EDDSA |
| 346 |
case AsymAlgo::EDDSA: |
| 347 |
return new OSSLEDDSA(); |
| 348 |
#endif |
| 349 |
default: |
| 350 |
// No algorithm implementation is available |
| 351 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 352 |
|
| 353 |
return NULL; |
| 354 |
} |
| 355 |
|
| 356 |
// No algorithm implementation is available |
| 357 |
return NULL; |
| 358 |
} |
| 359 |
|
| 360 |
// Create a concrete instance of a hash algorithm |
| 361 |
HashAlgorithm* OSSLCryptoFactory::getHashAlgorithm(HashAlgo::Type algorithm) |
| 362 |
{ |
| 363 |
switch (algorithm) |
| 364 |
{ |
| 365 |
case HashAlgo::MD5: |
| 366 |
return new OSSLMD5(); |
| 367 |
case HashAlgo::SHA1: |
| 368 |
return new OSSLSHA1(); |
| 369 |
case HashAlgo::SHA224: |
| 370 |
return new OSSLSHA224(); |
| 371 |
case HashAlgo::SHA256: |
| 372 |
return new OSSLSHA256(); |
| 373 |
case HashAlgo::SHA384: |
| 374 |
return new OSSLSHA384(); |
| 375 |
case HashAlgo::SHA512: |
| 376 |
return new OSSLSHA512(); |
| 377 |
#ifdef WITH_GOST |
| 378 |
case HashAlgo::GOST: |
| 379 |
return new OSSLGOSTR3411(); |
| 380 |
#endif |
| 381 |
default: |
| 382 |
// No algorithm implementation is available |
| 383 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 384 |
|
| 385 |
return NULL; |
| 386 |
} |
| 387 |
|
| 388 |
// No algorithm implementation is available |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
|
| 392 |
// Create a concrete instance of a MAC algorithm |
| 393 |
MacAlgorithm* OSSLCryptoFactory::getMacAlgorithm(MacAlgo::Type algorithm) |
| 394 |
{ |
| 395 |
switch (algorithm) |
| 396 |
{ |
| 397 |
case MacAlgo::HMAC_MD5: |
| 398 |
return new OSSLHMACMD5(); |
| 399 |
case MacAlgo::HMAC_SHA1: |
| 400 |
return new OSSLHMACSHA1(); |
| 401 |
case MacAlgo::HMAC_SHA224: |
| 402 |
return new OSSLHMACSHA224(); |
| 403 |
case MacAlgo::HMAC_SHA256: |
| 404 |
return new OSSLHMACSHA256(); |
| 405 |
case MacAlgo::HMAC_SHA384: |
| 406 |
return new OSSLHMACSHA384(); |
| 407 |
case MacAlgo::HMAC_SHA512: |
| 408 |
return new OSSLHMACSHA512(); |
| 409 |
#ifdef WITH_GOST |
| 410 |
case MacAlgo::HMAC_GOST: |
| 411 |
return new OSSLHMACGOSTR3411(); |
| 412 |
#endif |
| 413 |
case MacAlgo::CMAC_DES: |
| 414 |
return new OSSLCMACDES(); |
| 415 |
case MacAlgo::CMAC_AES: |
| 416 |
return new OSSLCMACAES(); |
| 417 |
default: |
| 418 |
// No algorithm implementation is available |
| 419 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 420 |
|
| 421 |
return NULL; |
| 422 |
} |
| 423 |
|
| 424 |
// No algorithm implementation is available |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
|
| 428 |
// Get the global RNG (may be an unique RNG per thread) |
| 429 |
RNG* OSSLCryptoFactory::getRNG(RNGImpl::Type name /* = RNGImpl::Default */) |
| 430 |
{ |
| 431 |
if (name == RNGImpl::Default) |
| 432 |
{ |
| 433 |
return rng; |
| 434 |
} |
| 435 |
else |
| 436 |
{ |
| 437 |
// No RNG implementation is available |
| 438 |
ERROR_MSG("Unknown RNG '%i'", name); |
| 439 |
|
| 440 |
return NULL; |
| 441 |
} |
| 442 |
} |
| 443 |
|