| 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 |
static bool ossl_shutdown; |
| 81 |
|
| 82 |
// Mutex callback |
| 83 |
void lock_callback(int mode, int n, const char* file, int line) |
| 84 |
{ |
| 85 |
if ((unsigned) n >= nlocks) |
| 86 |
{ |
| 87 |
ERROR_MSG("out of range [0..%u[ lock %d at %s:%d", |
| 88 |
nlocks, n, file, line); |
| 89 |
|
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
Mutex* mtx = locks[(unsigned) n]; |
| 94 |
|
| 95 |
if (mode & CRYPTO_LOCK) |
| 96 |
{ |
| 97 |
mtx->lock(); |
| 98 |
} |
| 99 |
else |
| 100 |
{ |
| 101 |
mtx->unlock(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#if OPENSSL_VERSION_NUMBER >= 0x10100000L |
| 106 |
void ossl_factory_shutdown(void) |
| 107 |
{ |
| 108 |
/* |
| 109 |
* As of 1.1.0, OpenSSL registers its own atexit() handler |
| 110 |
* to call OPENSSL_cleanup(). If our own atexit() handler |
| 111 |
* subsequently tries to, for example, unreference an |
| 112 |
* ENGINE, then it'll crash or deadlock with a use-after-free. |
| 113 |
* |
| 114 |
* This hook into the OpenSSL_atexit() handlers will get called |
| 115 |
* when OPENSSL_cleanup() is called, and sets a flag which |
| 116 |
* prevents any further touching of OpenSSL objects — which |
| 117 |
* would otherwise happen fairly much immediately thereafter |
| 118 |
* when our own OSSLCryptoFactory destructor gets called by |
| 119 |
* the C++ runtime's own atexit() handler. |
| 120 |
*/ |
| 121 |
ossl_shutdown = true; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
// Constructor |
| 126 |
OSSLCryptoFactory::OSSLCryptoFactory() |
| 127 |
{ |
| 128 |
// Multi-thread support |
| 129 |
nlocks = CRYPTO_num_locks(); |
| 130 |
locks = new Mutex*[nlocks]; |
| 131 |
for (unsigned i = 0; i < nlocks; i++) |
| 132 |
{ |
| 133 |
locks[i] = MutexFactory::i()->getMutex(); |
| 134 |
} |
| 135 |
|
| 136 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 137 |
setLockingCallback = false; |
| 138 |
if (CRYPTO_get_locking_callback() == NULL) |
| 139 |
{ |
| 140 |
CRYPTO_set_locking_callback(lock_callback); |
| 141 |
setLockingCallback = true; |
| 142 |
} |
| 143 |
#else |
| 144 |
// Mustn't dereference engines after OpenSSL itself has shut down |
| 145 |
OPENSSL_atexit(ossl_factory_shutdown); |
| 146 |
#endif |
| 147 |
|
| 148 |
#ifdef WITH_FIPS |
| 149 |
// Already in FIPS mode on reenter (avoiding selftests) |
| 150 |
if (!FIPS_mode()) |
| 151 |
{ |
| 152 |
FipsSelfTestStatus = false; |
| 153 |
if (!FIPS_mode_set(1)) |
| 154 |
{ |
| 155 |
ERROR_MSG("can't enter into FIPS mode"); |
| 156 |
return; |
| 157 |
} |
| 158 |
} else { |
| 159 |
// Undo RAND_cleanup() |
| 160 |
RAND_init_fips(); |
| 161 |
} |
| 162 |
FipsSelfTestStatus = true; |
| 163 |
#endif |
| 164 |
|
| 165 |
// Initialise OpenSSL |
| 166 |
OpenSSL_add_all_algorithms(); |
| 167 |
|
| 168 |
#if !( OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) ) |
| 169 |
// Make sure RDRAND is loaded first |
| 170 |
ENGINE_load_rdrand(); |
| 171 |
#endif |
| 172 |
// Locate the engine |
| 173 |
rdrand_engine = ENGINE_by_id("rdrand"); |
| 174 |
// Use RDRAND if available |
| 175 |
if (rdrand_engine != NULL) |
| 176 |
{ |
| 177 |
// Initialize RDRAND engine |
| 178 |
if (!ENGINE_init(rdrand_engine)) |
| 179 |
{ |
| 180 |
WARNING_MSG("ENGINE_init returned %lu\n", ERR_get_error()); |
| 181 |
} |
| 182 |
// Set RDRAND engine as the default for RAND_ methods |
| 183 |
else if (!ENGINE_set_default(rdrand_engine, ENGINE_METHOD_RAND)) |
| 184 |
{ |
| 185 |
WARNING_MSG("ENGINE_set_default returned %lu\n", ERR_get_error()); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// Initialise the one-and-only RNG |
| 190 |
rng = new OSSLRNG(); |
| 191 |
|
| 192 |
#ifdef WITH_GOST |
| 193 |
// Load engines |
| 194 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 195 |
ENGINE_load_builtin_engines(); |
| 196 |
#else |
| 197 |
OPENSSL_init_crypto(OPENSSL_INIT_ENGINE_ALL_BUILTIN | |
| 198 |
OPENSSL_INIT_ENGINE_RDRAND | |
| 199 |
OPENSSL_INIT_LOAD_CRYPTO_STRINGS | |
| 200 |
OPENSSL_INIT_ADD_ALL_CIPHERS | |
| 201 |
OPENSSL_INIT_ADD_ALL_DIGESTS | |
| 202 |
OPENSSL_INIT_LOAD_CONFIG, NULL); |
| 203 |
#endif |
| 204 |
|
| 205 |
// Initialise the GOST engine |
| 206 |
eg = ENGINE_by_id("gost"); |
| 207 |
if (eg == NULL) |
| 208 |
{ |
| 209 |
ERROR_MSG("can't get the GOST engine"); |
| 210 |
return; |
| 211 |
} |
| 212 |
if (ENGINE_init(eg) <= 0) |
| 213 |
{ |
| 214 |
ENGINE_free(eg); |
| 215 |
eg = NULL; |
| 216 |
ERROR_MSG("can't initialize the GOST engine"); |
| 217 |
return; |
| 218 |
} |
| 219 |
// better than digest_gost |
| 220 |
EVP_GOST_34_11 = ENGINE_get_digest(eg, NID_id_GostR3411_94); |
| 221 |
if (EVP_GOST_34_11 == NULL) |
| 222 |
{ |
| 223 |
ERROR_MSG("can't get the GOST digest"); |
| 224 |
goto err; |
| 225 |
} |
| 226 |
// from the openssl.cnf |
| 227 |
if (ENGINE_register_pkey_asn1_meths(eg) <= 0) |
| 228 |
{ |
| 229 |
ERROR_MSG("can't register ASN.1 for the GOST engine"); |
| 230 |
goto err; |
| 231 |
} |
| 232 |
if (ENGINE_ctrl_cmd_string(eg, |
| 233 |
"CRYPT_PARAMS", |
| 234 |
"id-Gost28147-89-CryptoPro-A-ParamSet", |
| 235 |
0) <= 0) |
| 236 |
{ |
| 237 |
ERROR_MSG("can't set params of the GOST engine"); |
| 238 |
goto err; |
| 239 |
} |
| 240 |
return; |
| 241 |
|
| 242 |
err: |
| 243 |
ENGINE_finish(eg); |
| 244 |
ENGINE_free(eg); |
| 245 |
eg = NULL; |
| 246 |
return; |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
// Destructor |
| 251 |
OSSLCryptoFactory::~OSSLCryptoFactory() |
| 252 |
{ |
| 253 |
// Don't do this if OPENSSL_cleanup() has already happened |
| 254 |
if (!ossl_shutdown) |
| 255 |
{ |
| 256 |
#ifdef WITH_GOST |
| 257 |
// Finish the GOST engine |
| 258 |
if (eg != NULL) |
| 259 |
{ |
| 260 |
ENGINE_finish(eg); |
| 261 |
ENGINE_free(eg); |
| 262 |
eg = NULL; |
| 263 |
} |
| 264 |
#endif |
| 265 |
|
| 266 |
// Finish the rd_rand engine |
| 267 |
ENGINE_finish(rdrand_engine); |
| 268 |
ENGINE_free(rdrand_engine); |
| 269 |
rdrand_engine = NULL; |
| 270 |
|
| 271 |
// Recycle locks |
| 272 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER) |
| 273 |
if (setLockingCallback) |
| 274 |
{ |
| 275 |
CRYPTO_set_locking_callback(NULL); |
| 276 |
} |
| 277 |
#endif |
| 278 |
} |
| 279 |
// Destroy the one-and-only RNG |
| 280 |
delete rng; |
| 281 |
|
| 282 |
for (unsigned i = 0; i < nlocks; i++) |
| 283 |
{ |
| 284 |
MutexFactory::i()->recycleMutex(locks[i]); |
| 285 |
} |
| 286 |
delete[] locks; |
| 287 |
} |
| 288 |
|
| 289 |
// Return the one-and-only instance |
| 290 |
OSSLCryptoFactory* OSSLCryptoFactory::i() |
| 291 |
{ |
| 292 |
if (!instance.get()) |
| 293 |
{ |
| 294 |
instance.reset(new OSSLCryptoFactory()); |
| 295 |
} |
| 296 |
|
| 297 |
return instance.get(); |
| 298 |
} |
| 299 |
|
| 300 |
// This will destroy the one-and-only instance. |
| 301 |
void OSSLCryptoFactory::reset() |
| 302 |
{ |
| 303 |
instance.reset(); |
| 304 |
} |
| 305 |
|
| 306 |
#ifdef WITH_FIPS |
| 307 |
bool OSSLCryptoFactory::getFipsSelfTestStatus() const |
| 308 |
{ |
| 309 |
return FipsSelfTestStatus; |
| 310 |
} |
| 311 |
#endif |
| 312 |
|
| 313 |
// Create a concrete instance of a symmetric algorithm |
| 314 |
SymmetricAlgorithm* OSSLCryptoFactory::getSymmetricAlgorithm(SymAlgo::Type algorithm) |
| 315 |
{ |
| 316 |
switch (algorithm) |
| 317 |
{ |
| 318 |
case SymAlgo::AES: |
| 319 |
return new OSSLAES(); |
| 320 |
case SymAlgo::DES: |
| 321 |
case SymAlgo::DES3: |
| 322 |
return new OSSLDES(); |
| 323 |
default: |
| 324 |
// No algorithm implementation is available |
| 325 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 326 |
|
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
// No algorithm implementation is available |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
|
| 334 |
// Create a concrete instance of an asymmetric algorithm |
| 335 |
AsymmetricAlgorithm* OSSLCryptoFactory::getAsymmetricAlgorithm(AsymAlgo::Type algorithm) |
| 336 |
{ |
| 337 |
switch (algorithm) |
| 338 |
{ |
| 339 |
case AsymAlgo::RSA: |
| 340 |
return new OSSLRSA(); |
| 341 |
case AsymAlgo::DSA: |
| 342 |
return new OSSLDSA(); |
| 343 |
case AsymAlgo::DH: |
| 344 |
return new OSSLDH(); |
| 345 |
#ifdef WITH_ECC |
| 346 |
case AsymAlgo::ECDH: |
| 347 |
return new OSSLECDH(); |
| 348 |
case AsymAlgo::ECDSA: |
| 349 |
return new OSSLECDSA(); |
| 350 |
#endif |
| 351 |
#ifdef WITH_GOST |
| 352 |
case AsymAlgo::GOST: |
| 353 |
return new OSSLGOST(); |
| 354 |
#endif |
| 355 |
#ifdef WITH_EDDSA |
| 356 |
case AsymAlgo::EDDSA: |
| 357 |
return new OSSLEDDSA(); |
| 358 |
#endif |
| 359 |
default: |
| 360 |
// No algorithm implementation is available |
| 361 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 362 |
|
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
// No algorithm implementation is available |
| 367 |
return NULL; |
| 368 |
} |
| 369 |
|
| 370 |
// Create a concrete instance of a hash algorithm |
| 371 |
HashAlgorithm* OSSLCryptoFactory::getHashAlgorithm(HashAlgo::Type algorithm) |
| 372 |
{ |
| 373 |
switch (algorithm) |
| 374 |
{ |
| 375 |
case HashAlgo::MD5: |
| 376 |
return new OSSLMD5(); |
| 377 |
case HashAlgo::SHA1: |
| 378 |
return new OSSLSHA1(); |
| 379 |
case HashAlgo::SHA224: |
| 380 |
return new OSSLSHA224(); |
| 381 |
case HashAlgo::SHA256: |
| 382 |
return new OSSLSHA256(); |
| 383 |
case HashAlgo::SHA384: |
| 384 |
return new OSSLSHA384(); |
| 385 |
case HashAlgo::SHA512: |
| 386 |
return new OSSLSHA512(); |
| 387 |
#ifdef WITH_GOST |
| 388 |
case HashAlgo::GOST: |
| 389 |
return new OSSLGOSTR3411(); |
| 390 |
#endif |
| 391 |
default: |
| 392 |
// No algorithm implementation is available |
| 393 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 394 |
|
| 395 |
return NULL; |
| 396 |
} |
| 397 |
|
| 398 |
// No algorithm implementation is available |
| 399 |
return NULL; |
| 400 |
} |
| 401 |
|
| 402 |
// Create a concrete instance of a MAC algorithm |
| 403 |
MacAlgorithm* OSSLCryptoFactory::getMacAlgorithm(MacAlgo::Type algorithm) |
| 404 |
{ |
| 405 |
switch (algorithm) |
| 406 |
{ |
| 407 |
case MacAlgo::HMAC_MD5: |
| 408 |
return new OSSLHMACMD5(); |
| 409 |
case MacAlgo::HMAC_SHA1: |
| 410 |
return new OSSLHMACSHA1(); |
| 411 |
case MacAlgo::HMAC_SHA224: |
| 412 |
return new OSSLHMACSHA224(); |
| 413 |
case MacAlgo::HMAC_SHA256: |
| 414 |
return new OSSLHMACSHA256(); |
| 415 |
case MacAlgo::HMAC_SHA384: |
| 416 |
return new OSSLHMACSHA384(); |
| 417 |
case MacAlgo::HMAC_SHA512: |
| 418 |
return new OSSLHMACSHA512(); |
| 419 |
#ifdef WITH_GOST |
| 420 |
case MacAlgo::HMAC_GOST: |
| 421 |
return new OSSLHMACGOSTR3411(); |
| 422 |
#endif |
| 423 |
case MacAlgo::CMAC_DES: |
| 424 |
return new OSSLCMACDES(); |
| 425 |
case MacAlgo::CMAC_AES: |
| 426 |
return new OSSLCMACAES(); |
| 427 |
default: |
| 428 |
// No algorithm implementation is available |
| 429 |
ERROR_MSG("Unknown algorithm '%i'", algorithm); |
| 430 |
|
| 431 |
return NULL; |
| 432 |
} |
| 433 |
|
| 434 |
// No algorithm implementation is available |
| 435 |
return NULL; |
| 436 |
} |
| 437 |
|
| 438 |
// Get the global RNG (may be an unique RNG per thread) |
| 439 |
RNG* OSSLCryptoFactory::getRNG(RNGImpl::Type name /* = RNGImpl::Default */) |
| 440 |
{ |
| 441 |
if (name == RNGImpl::Default) |
| 442 |
{ |
| 443 |
return rng; |
| 444 |
} |
| 445 |
else |
| 446 |
{ |
| 447 |
// No RNG implementation is available |
| 448 |
ERROR_MSG("Unknown RNG '%i'", name); |
| 449 |
|
| 450 |
return NULL; |
| 451 |
} |
| 452 |
} |
| 453 |
|