| 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 |
OSSLRSA.cpp |
| 29 |
|
| 30 |
OpenSSL RSA asymmetric algorithm implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "log.h" |
| 35 |
#include "OSSLRSA.h" |
| 36 |
#include "OSSLUtil.h" |
| 37 |
#include "CryptoFactory.h" |
| 38 |
#include "RSAParameters.h" |
| 39 |
#include "OSSLRSAKeyPair.h" |
| 40 |
#include <algorithm> |
| 41 |
#include <openssl/rsa.h> |
| 42 |
#include <openssl/pem.h> |
| 43 |
#include <openssl/err.h> |
| 44 |
|
| 45 |
// Constructor |
| 46 |
OSSLRSA::OSSLRSA() |
| 47 |
{ |
| 48 |
pCurrentHash = NULL; |
| 49 |
pSecondHash = NULL; |
| 50 |
sLen = 0; |
| 51 |
} |
| 52 |
|
| 53 |
// Destructor |
| 54 |
OSSLRSA::~OSSLRSA() |
| 55 |
{ |
| 56 |
if (pCurrentHash != NULL) |
| 57 |
{ |
| 58 |
delete pCurrentHash; |
| 59 |
} |
| 60 |
|
| 61 |
if (pSecondHash != NULL) |
| 62 |
{ |
| 63 |
delete pSecondHash; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// Signing functions |
| 68 |
bool OSSLRSA::sign(PrivateKey* privateKey, const ByteString& dataToSign, |
| 69 |
ByteString& signature, const AsymMech::Type mechanism, |
| 70 |
const void* param /* = NULL */, const size_t paramLen /* = 0 */) |
| 71 |
{ |
| 72 |
if (mechanism == AsymMech::RSA_PKCS) |
| 73 |
{ |
| 74 |
// Separate implementation for RSA PKCS #1 signing without hash computation |
| 75 |
|
| 76 |
// Check if the private key is the right type |
| 77 |
if (!privateKey->isOfType(OSSLRSAPrivateKey::type)) |
| 78 |
{ |
| 79 |
ERROR_MSG("Invalid key type supplied"); |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
// In case of PKCS #1 signing the length of the input data may not exceed 40% of the |
| 85 |
// modulus size |
| 86 |
OSSLRSAPrivateKey* osslKey = (OSSLRSAPrivateKey*) privateKey; |
| 87 |
|
| 88 |
size_t allowedLen = osslKey->getN().size() - 11; |
| 89 |
|
| 90 |
if (dataToSign.size() > allowedLen) |
| 91 |
{ |
| 92 |
ERROR_MSG("Data to sign exceeds maximum for PKCS #1 signature"); |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
// Perform the signature operation |
| 98 |
signature.resize(osslKey->getN().size()); |
| 99 |
|
| 100 |
RSA* rsa = osslKey->getOSSLKey(); |
| 101 |
|
| 102 |
if (!RSA_blinding_on(rsa, NULL)) |
| 103 |
{ |
| 104 |
ERROR_MSG("Failed to turn on blinding for OpenSSL RSA key"); |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
int sigLen = RSA_private_encrypt(dataToSign.size(), (unsigned char*) dataToSign.const_byte_str(), &signature[0], rsa, RSA_PKCS1_PADDING); |
| 110 |
|
| 111 |
RSA_blinding_off(rsa); |
| 112 |
|
| 113 |
if (sigLen == -1) |
| 114 |
{ |
| 115 |
ERROR_MSG("An error occurred while performing a PKCS #1 signature"); |
| 116 |
|
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
signature.resize(sigLen); |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
else if (mechanism == AsymMech::RSA_PKCS_PSS) |
| 125 |
{ |
| 126 |
const RSA_PKCS_PSS_PARAMS *pssParam = (RSA_PKCS_PSS_PARAMS*)param; |
| 127 |
|
| 128 |
// Separate implementation for RSA PKCS #1 signing without hash computation |
| 129 |
|
| 130 |
// Check if the private key is the right type |
| 131 |
if (!privateKey->isOfType(OSSLRSAPrivateKey::type)) |
| 132 |
{ |
| 133 |
ERROR_MSG("Invalid key type supplied"); |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
if (pssParam == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS)) |
| 139 |
{ |
| 140 |
ERROR_MSG("Invalid parameters supplied"); |
| 141 |
|
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
size_t allowedLen; |
| 146 |
const EVP_MD* hash = NULL; |
| 147 |
|
| 148 |
switch (pssParam->hashAlg) |
| 149 |
{ |
| 150 |
case HashAlgo::SHA1: |
| 151 |
hash = EVP_sha1(); |
| 152 |
allowedLen = 20; |
| 153 |
break; |
| 154 |
case HashAlgo::SHA224: |
| 155 |
hash = EVP_sha224(); |
| 156 |
allowedLen = 28; |
| 157 |
break; |
| 158 |
case HashAlgo::SHA256: |
| 159 |
hash = EVP_sha256(); |
| 160 |
allowedLen = 32; |
| 161 |
break; |
| 162 |
case HashAlgo::SHA384: |
| 163 |
hash = EVP_sha384(); |
| 164 |
allowedLen = 48; |
| 165 |
break; |
| 166 |
case HashAlgo::SHA512: |
| 167 |
hash = EVP_sha512(); |
| 168 |
allowedLen = 64; |
| 169 |
break; |
| 170 |
default: |
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
OSSLRSAPrivateKey* osslKey = (OSSLRSAPrivateKey*) privateKey; |
| 175 |
|
| 176 |
RSA* rsa = osslKey->getOSSLKey(); |
| 177 |
|
| 178 |
if (dataToSign.size() != allowedLen) |
| 179 |
{ |
| 180 |
ERROR_MSG("Data to sign does not match expected (%d) for RSA PSS", (int)allowedLen); |
| 181 |
|
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
size_t sLen = pssParam->sLen; |
| 186 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-allowedLen)) |
| 187 |
{ |
| 188 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 189 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 190 |
return false; |
| 191 |
} |
| 192 |
|
| 193 |
ByteString em; |
| 194 |
em.resize(osslKey->getN().size()); |
| 195 |
|
| 196 |
int status = RSA_padding_add_PKCS1_PSS_mgf1(rsa, &em[0], (unsigned char*) dataToSign.const_byte_str(), hash, hash, pssParam->sLen); |
| 197 |
if (!status) |
| 198 |
{ |
| 199 |
ERROR_MSG("Error in RSA PSS padding generation"); |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
if (!RSA_blinding_on(rsa, NULL)) |
| 206 |
{ |
| 207 |
ERROR_MSG("Failed to turn on blinding for OpenSSL RSA key"); |
| 208 |
|
| 209 |
return false; |
| 210 |
} |
| 211 |
|
| 212 |
// Perform the signature operation |
| 213 |
signature.resize(osslKey->getN().size()); |
| 214 |
|
| 215 |
int sigLen = RSA_private_encrypt(osslKey->getN().size(), &em[0], &signature[0], rsa, RSA_NO_PADDING); |
| 216 |
|
| 217 |
RSA_blinding_off(rsa); |
| 218 |
|
| 219 |
if (sigLen == -1) |
| 220 |
{ |
| 221 |
ERROR_MSG("An error occurred while performing the RSA-PSS signature"); |
| 222 |
|
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
signature.resize(sigLen); |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
else if (mechanism == AsymMech::RSA) |
| 231 |
{ |
| 232 |
// Separate implementation for raw RSA signing |
| 233 |
|
| 234 |
// Check if the private key is the right type |
| 235 |
if (!privateKey->isOfType(OSSLRSAPrivateKey::type)) |
| 236 |
{ |
| 237 |
ERROR_MSG("Invalid key type supplied"); |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
// In case of raw RSA, the length of the input data must match the length of the modulus |
| 243 |
OSSLRSAPrivateKey* osslKey = (OSSLRSAPrivateKey*) privateKey; |
| 244 |
|
| 245 |
if (dataToSign.size() != osslKey->getN().size()) |
| 246 |
{ |
| 247 |
ERROR_MSG("Size of data to sign does not match the modulus size"); |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
// Perform the signature operation |
| 253 |
signature.resize(osslKey->getN().size()); |
| 254 |
|
| 255 |
RSA* rsa = osslKey->getOSSLKey(); |
| 256 |
|
| 257 |
if (!RSA_blinding_on(rsa, NULL)) |
| 258 |
{ |
| 259 |
ERROR_MSG("Failed to turn on blinding for OpenSSL RSA key"); |
| 260 |
|
| 261 |
return false; |
| 262 |
} |
| 263 |
|
| 264 |
int sigLen = RSA_private_encrypt(dataToSign.size(), (unsigned char*) dataToSign.const_byte_str(), &signature[0], rsa, RSA_NO_PADDING); |
| 265 |
|
| 266 |
RSA_blinding_off(rsa); |
| 267 |
|
| 268 |
if (sigLen == -1) |
| 269 |
{ |
| 270 |
ERROR_MSG("An error occurred while performing a raw RSA signature"); |
| 271 |
|
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
signature.resize(sigLen); |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
else |
| 280 |
{ |
| 281 |
// Call default implementation |
| 282 |
return AsymmetricAlgorithm::sign(privateKey, dataToSign, signature, mechanism, param, paramLen); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
bool OSSLRSA::signInit(PrivateKey* privateKey, const AsymMech::Type mechanism, |
| 287 |
const void* param /* = NULL */, const size_t paramLen /* = 0 */) |
| 288 |
{ |
| 289 |
if (!AsymmetricAlgorithm::signInit(privateKey, mechanism, param, paramLen)) |
| 290 |
{ |
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
// Check if the private key is the right type |
| 295 |
if (!privateKey->isOfType(OSSLRSAPrivateKey::type)) |
| 296 |
{ |
| 297 |
ERROR_MSG("Invalid key type supplied"); |
| 298 |
|
| 299 |
ByteString dummy; |
| 300 |
AsymmetricAlgorithm::signFinal(dummy); |
| 301 |
|
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
HashAlgo::Type hash1 = HashAlgo::Unknown; |
| 306 |
HashAlgo::Type hash2 = HashAlgo::Unknown; |
| 307 |
|
| 308 |
switch (mechanism) |
| 309 |
{ |
| 310 |
case AsymMech::RSA_MD5_PKCS: |
| 311 |
hash1 = HashAlgo::MD5; |
| 312 |
break; |
| 313 |
case AsymMech::RSA_SHA1_PKCS: |
| 314 |
hash1 = HashAlgo::SHA1; |
| 315 |
break; |
| 316 |
case AsymMech::RSA_SHA224_PKCS: |
| 317 |
hash1 = HashAlgo::SHA224; |
| 318 |
break; |
| 319 |
case AsymMech::RSA_SHA256_PKCS: |
| 320 |
hash1 = HashAlgo::SHA256; |
| 321 |
break; |
| 322 |
case AsymMech::RSA_SHA384_PKCS: |
| 323 |
hash1 = HashAlgo::SHA384; |
| 324 |
break; |
| 325 |
case AsymMech::RSA_SHA512_PKCS: |
| 326 |
hash1 = HashAlgo::SHA512; |
| 327 |
break; |
| 328 |
case AsymMech::RSA_SHA1_PKCS_PSS: |
| 329 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 330 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA1 || |
| 331 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA1) |
| 332 |
{ |
| 333 |
ERROR_MSG("Invalid parameters"); |
| 334 |
ByteString dummy; |
| 335 |
AsymmetricAlgorithm::signFinal(dummy); |
| 336 |
return false; |
| 337 |
} |
| 338 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 339 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-20)) |
| 340 |
{ |
| 341 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 342 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 343 |
ByteString dummy; |
| 344 |
AsymmetricAlgorithm::signFinal(dummy); |
| 345 |
return false; |
| 346 |
} |
| 347 |
hash1 = HashAlgo::SHA1; |
| 348 |
break; |
| 349 |
case AsymMech::RSA_SHA224_PKCS_PSS: |
| 350 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 351 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA224 || |
| 352 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA224) |
| 353 |
{ |
| 354 |
ERROR_MSG("Invalid parameters"); |
| 355 |
ByteString dummy; |
| 356 |
AsymmetricAlgorithm::signFinal(dummy); |
| 357 |
return false; |
| 358 |
} |
| 359 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 360 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-28)) |
| 361 |
{ |
| 362 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 363 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 364 |
ByteString dummy; |
| 365 |
AsymmetricAlgorithm::signFinal(dummy); |
| 366 |
return false; |
| 367 |
} |
| 368 |
hash1 = HashAlgo::SHA224; |
| 369 |
break; |
| 370 |
case AsymMech::RSA_SHA256_PKCS_PSS: |
| 371 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 372 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA256 || |
| 373 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA256) |
| 374 |
{ |
| 375 |
ERROR_MSG("Invalid parameters"); |
| 376 |
ByteString dummy; |
| 377 |
AsymmetricAlgorithm::signFinal(dummy); |
| 378 |
return false; |
| 379 |
} |
| 380 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 381 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-32)) |
| 382 |
{ |
| 383 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 384 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 385 |
ByteString dummy; |
| 386 |
AsymmetricAlgorithm::signFinal(dummy); |
| 387 |
return false; |
| 388 |
} |
| 389 |
hash1 = HashAlgo::SHA256; |
| 390 |
break; |
| 391 |
case AsymMech::RSA_SHA384_PKCS_PSS: |
| 392 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 393 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA384 || |
| 394 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA384) |
| 395 |
{ |
| 396 |
ERROR_MSG("Invalid parameters"); |
| 397 |
ByteString dummy; |
| 398 |
AsymmetricAlgorithm::signFinal(dummy); |
| 399 |
return false; |
| 400 |
} |
| 401 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 402 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-48)) |
| 403 |
{ |
| 404 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 405 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 406 |
ByteString dummy; |
| 407 |
AsymmetricAlgorithm::signFinal(dummy); |
| 408 |
return false; |
| 409 |
} |
| 410 |
hash1 = HashAlgo::SHA384; |
| 411 |
break; |
| 412 |
case AsymMech::RSA_SHA512_PKCS_PSS: |
| 413 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 414 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA512 || |
| 415 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA512) |
| 416 |
{ |
| 417 |
ERROR_MSG("Invalid parameters"); |
| 418 |
ByteString dummy; |
| 419 |
AsymmetricAlgorithm::signFinal(dummy); |
| 420 |
return false; |
| 421 |
} |
| 422 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 423 |
if (sLen > ((privateKey->getBitLength()+6)/8-2-64)) |
| 424 |
{ |
| 425 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 426 |
(unsigned long)sLen, privateKey->getBitLength()); |
| 427 |
ByteString dummy; |
| 428 |
AsymmetricAlgorithm::signFinal(dummy); |
| 429 |
return false; |
| 430 |
} |
| 431 |
hash1 = HashAlgo::SHA512; |
| 432 |
break; |
| 433 |
case AsymMech::RSA_SSL: |
| 434 |
hash1 = HashAlgo::MD5; |
| 435 |
hash2 = HashAlgo::SHA1; |
| 436 |
break; |
| 437 |
default: |
| 438 |
ERROR_MSG("Invalid mechanism supplied (%i)", mechanism); |
| 439 |
|
| 440 |
ByteString dummy; |
| 441 |
AsymmetricAlgorithm::signFinal(dummy); |
| 442 |
|
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
pCurrentHash = CryptoFactory::i()->getHashAlgorithm(hash1); |
| 447 |
|
| 448 |
if (pCurrentHash == NULL || !pCurrentHash->hashInit()) |
| 449 |
{ |
| 450 |
if (pCurrentHash != NULL) |
| 451 |
{ |
| 452 |
delete pCurrentHash; |
| 453 |
pCurrentHash = NULL; |
| 454 |
} |
| 455 |
|
| 456 |
ByteString dummy; |
| 457 |
AsymmetricAlgorithm::signFinal(dummy); |
| 458 |
|
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
if (hash2 != HashAlgo::Unknown) |
| 463 |
{ |
| 464 |
pSecondHash = CryptoFactory::i()->getHashAlgorithm(hash2); |
| 465 |
|
| 466 |
if (pSecondHash == NULL || !pSecondHash->hashInit()) |
| 467 |
{ |
| 468 |
delete pCurrentHash; |
| 469 |
pCurrentHash = NULL; |
| 470 |
|
| 471 |
if (pSecondHash != NULL) |
| 472 |
{ |
| 473 |
delete pSecondHash; |
| 474 |
pSecondHash = NULL; |
| 475 |
} |
| 476 |
|
| 477 |
ByteString dummy; |
| 478 |
AsymmetricAlgorithm::signFinal(dummy); |
| 479 |
|
| 480 |
return false; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
return true; |
| 485 |
} |
| 486 |
|
| 487 |
bool OSSLRSA::signUpdate(const ByteString& dataToSign) |
| 488 |
{ |
| 489 |
if (!AsymmetricAlgorithm::signUpdate(dataToSign)) |
| 490 |
{ |
| 491 |
return false; |
| 492 |
} |
| 493 |
|
| 494 |
if (!pCurrentHash->hashUpdate(dataToSign)) |
| 495 |
{ |
| 496 |
delete pCurrentHash; |
| 497 |
pCurrentHash = NULL; |
| 498 |
|
| 499 |
ByteString dummy; |
| 500 |
AsymmetricAlgorithm::signFinal(dummy); |
| 501 |
|
| 502 |
return false; |
| 503 |
} |
| 504 |
|
| 505 |
if ((pSecondHash != NULL) && !pSecondHash->hashUpdate(dataToSign)) |
| 506 |
{ |
| 507 |
delete pCurrentHash; |
| 508 |
pCurrentHash = NULL; |
| 509 |
|
| 510 |
delete pSecondHash; |
| 511 |
pSecondHash = NULL; |
| 512 |
|
| 513 |
ByteString dummy; |
| 514 |
AsymmetricAlgorithm::signFinal(dummy); |
| 515 |
|
| 516 |
return false; |
| 517 |
} |
| 518 |
|
| 519 |
return true; |
| 520 |
} |
| 521 |
|
| 522 |
bool OSSLRSA::signFinal(ByteString& signature) |
| 523 |
{ |
| 524 |
// Save necessary state before calling super class signFinal |
| 525 |
OSSLRSAPrivateKey* pk = (OSSLRSAPrivateKey*) currentPrivateKey; |
| 526 |
AsymMech::Type mechanism = currentMechanism; |
| 527 |
|
| 528 |
if (!AsymmetricAlgorithm::signFinal(signature)) |
| 529 |
{ |
| 530 |
return false; |
| 531 |
} |
| 532 |
|
| 533 |
ByteString firstHash, secondHash; |
| 534 |
|
| 535 |
bool bFirstResult = pCurrentHash->hashFinal(firstHash); |
| 536 |
bool bSecondResult = (pSecondHash != NULL) ? pSecondHash->hashFinal(secondHash) : true; |
| 537 |
|
| 538 |
delete pCurrentHash; |
| 539 |
pCurrentHash = NULL; |
| 540 |
|
| 541 |
if (pSecondHash != NULL) |
| 542 |
{ |
| 543 |
delete pSecondHash; |
| 544 |
|
| 545 |
pSecondHash = NULL; |
| 546 |
} |
| 547 |
|
| 548 |
if (!bFirstResult || !bSecondResult) |
| 549 |
{ |
| 550 |
return false; |
| 551 |
} |
| 552 |
|
| 553 |
ByteString digest = firstHash + secondHash; |
| 554 |
|
| 555 |
// Resize the data block for the signature to the modulus size of the key |
| 556 |
signature.resize(pk->getN().size()); |
| 557 |
|
| 558 |
// Determine the signature NID type |
| 559 |
int type = 0; |
| 560 |
bool isPSS = false; |
| 561 |
const EVP_MD* hash = NULL; |
| 562 |
|
| 563 |
switch (mechanism) |
| 564 |
{ |
| 565 |
case AsymMech::RSA_MD5_PKCS: |
| 566 |
type = NID_md5; |
| 567 |
break; |
| 568 |
case AsymMech::RSA_SHA1_PKCS: |
| 569 |
type = NID_sha1; |
| 570 |
break; |
| 571 |
case AsymMech::RSA_SHA224_PKCS: |
| 572 |
type = NID_sha224; |
| 573 |
break; |
| 574 |
case AsymMech::RSA_SHA256_PKCS: |
| 575 |
type = NID_sha256; |
| 576 |
break; |
| 577 |
case AsymMech::RSA_SHA384_PKCS: |
| 578 |
type = NID_sha384; |
| 579 |
break; |
| 580 |
case AsymMech::RSA_SHA512_PKCS: |
| 581 |
type = NID_sha512; |
| 582 |
break; |
| 583 |
case AsymMech::RSA_SHA1_PKCS_PSS: |
| 584 |
isPSS = true; |
| 585 |
hash = EVP_sha1(); |
| 586 |
break; |
| 587 |
case AsymMech::RSA_SHA224_PKCS_PSS: |
| 588 |
isPSS = true; |
| 589 |
hash = EVP_sha224(); |
| 590 |
break; |
| 591 |
case AsymMech::RSA_SHA256_PKCS_PSS: |
| 592 |
isPSS = true; |
| 593 |
hash = EVP_sha256(); |
| 594 |
break; |
| 595 |
case AsymMech::RSA_SHA384_PKCS_PSS: |
| 596 |
isPSS = true; |
| 597 |
hash = EVP_sha384(); |
| 598 |
break; |
| 599 |
case AsymMech::RSA_SHA512_PKCS_PSS: |
| 600 |
isPSS = true; |
| 601 |
hash = EVP_sha512(); |
| 602 |
break; |
| 603 |
case AsymMech::RSA_SSL: |
| 604 |
type = NID_md5_sha1; |
| 605 |
break; |
| 606 |
default: |
| 607 |
break; |
| 608 |
} |
| 609 |
|
| 610 |
// Perform the signature operation |
| 611 |
unsigned int sigLen = signature.size(); |
| 612 |
|
| 613 |
RSA* rsa = pk->getOSSLKey(); |
| 614 |
|
| 615 |
if (!RSA_blinding_on(rsa, NULL)) |
| 616 |
{ |
| 617 |
ERROR_MSG("Failed to turn blinding on for OpenSSL RSA key"); |
| 618 |
|
| 619 |
return false; |
| 620 |
} |
| 621 |
|
| 622 |
bool rv; |
| 623 |
int result; |
| 624 |
|
| 625 |
if (isPSS) |
| 626 |
{ |
| 627 |
ByteString em; |
| 628 |
em.resize(pk->getN().size()); |
| 629 |
|
| 630 |
result = (RSA_padding_add_PKCS1_PSS(pk->getOSSLKey(), &em[0], &digest[0], |
| 631 |
hash, sLen) == 1); |
| 632 |
if (!result) |
| 633 |
{ |
| 634 |
ERROR_MSG("RSA PSS padding failed (0x%08X)", ERR_get_error()); |
| 635 |
rv = false; |
| 636 |
} |
| 637 |
else |
| 638 |
{ |
| 639 |
result = RSA_private_encrypt(em.size(), &em[0], &signature[0], |
| 640 |
pk->getOSSLKey(), RSA_NO_PADDING); |
| 641 |
if (result >= 0) |
| 642 |
{ |
| 643 |
sigLen = result; |
| 644 |
rv = true; |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
ERROR_MSG("RSA private encrypt failed (0x%08X)", ERR_get_error()); |
| 649 |
rv = false; |
| 650 |
} |
| 651 |
} |
| 652 |
} |
| 653 |
else |
| 654 |
{ |
| 655 |
result = RSA_sign(type, &digest[0], digest.size(), &signature[0], |
| 656 |
&sigLen, pk->getOSSLKey()); |
| 657 |
if (result > 0) |
| 658 |
{ |
| 659 |
rv = true; |
| 660 |
} |
| 661 |
else |
| 662 |
{ |
| 663 |
ERROR_MSG("RSA sign failed (0x%08X)", ERR_get_error()); |
| 664 |
rv = false; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
RSA_blinding_off(rsa); |
| 669 |
|
| 670 |
signature.resize(sigLen); |
| 671 |
|
| 672 |
return rv; |
| 673 |
} |
| 674 |
|
| 675 |
// Verification functions |
| 676 |
bool OSSLRSA::verify(PublicKey* publicKey, const ByteString& originalData, |
| 677 |
const ByteString& signature, const AsymMech::Type mechanism, |
| 678 |
const void* param /* = NULL */, const size_t paramLen /* = 0 */) |
| 679 |
{ |
| 680 |
if (mechanism == AsymMech::RSA_PKCS) |
| 681 |
{ |
| 682 |
// Specific implementation for PKCS #1 only verification; originalData is assumed to contain |
| 683 |
// a digestInfo structure and verification is performed by comparing originalData to the data |
| 684 |
// recovered from the signature |
| 685 |
|
| 686 |
// Check if the public key is the right type |
| 687 |
if (!publicKey->isOfType(OSSLRSAPublicKey::type)) |
| 688 |
{ |
| 689 |
ERROR_MSG("Invalid key type supplied"); |
| 690 |
|
| 691 |
return false; |
| 692 |
} |
| 693 |
|
| 694 |
// Perform the RSA public key operation |
| 695 |
OSSLRSAPublicKey* osslKey = (OSSLRSAPublicKey*) publicKey; |
| 696 |
|
| 697 |
ByteString recoveredData; |
| 698 |
|
| 699 |
recoveredData.resize(osslKey->getN().size()); |
| 700 |
|
| 701 |
RSA* rsa = osslKey->getOSSLKey(); |
| 702 |
|
| 703 |
int retLen = RSA_public_decrypt(signature.size(), (unsigned char*) signature.const_byte_str(), &recoveredData[0], rsa, RSA_PKCS1_PADDING); |
| 704 |
|
| 705 |
if (retLen == -1) |
| 706 |
{ |
| 707 |
ERROR_MSG("Public key operation failed"); |
| 708 |
|
| 709 |
return false; |
| 710 |
} |
| 711 |
|
| 712 |
recoveredData.resize(retLen); |
| 713 |
|
| 714 |
return (originalData == recoveredData); |
| 715 |
} |
| 716 |
else if (mechanism == AsymMech::RSA_PKCS_PSS) |
| 717 |
{ |
| 718 |
const RSA_PKCS_PSS_PARAMS *pssParam = (RSA_PKCS_PSS_PARAMS*)param; |
| 719 |
|
| 720 |
if (pssParam == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS)) |
| 721 |
{ |
| 722 |
ERROR_MSG("Invalid parameters supplied"); |
| 723 |
|
| 724 |
return false; |
| 725 |
} |
| 726 |
|
| 727 |
// Check if the public key is the right type |
| 728 |
if (!publicKey->isOfType(OSSLRSAPublicKey::type)) |
| 729 |
{ |
| 730 |
ERROR_MSG("Invalid key type supplied"); |
| 731 |
|
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
// Perform the RSA public key operation |
| 736 |
OSSLRSAPublicKey* osslKey = (OSSLRSAPublicKey*) publicKey; |
| 737 |
|
| 738 |
ByteString recoveredData; |
| 739 |
|
| 740 |
recoveredData.resize(osslKey->getN().size()); |
| 741 |
|
| 742 |
RSA* rsa = osslKey->getOSSLKey(); |
| 743 |
|
| 744 |
int retLen = RSA_public_decrypt(signature.size(), (unsigned char*) signature.const_byte_str(), &recoveredData[0], rsa, RSA_NO_PADDING); |
| 745 |
|
| 746 |
if (retLen == -1) |
| 747 |
{ |
| 748 |
ERROR_MSG("Public key operation failed"); |
| 749 |
|
| 750 |
return false; |
| 751 |
} |
| 752 |
|
| 753 |
recoveredData.resize(retLen); |
| 754 |
|
| 755 |
size_t allowedLen; |
| 756 |
const EVP_MD* hash = NULL; |
| 757 |
|
| 758 |
switch (pssParam->hashAlg) |
| 759 |
{ |
| 760 |
case HashAlgo::SHA1: |
| 761 |
hash = EVP_sha1(); |
| 762 |
allowedLen = 20; |
| 763 |
break; |
| 764 |
case HashAlgo::SHA224: |
| 765 |
hash = EVP_sha224(); |
| 766 |
allowedLen = 28; |
| 767 |
break; |
| 768 |
case HashAlgo::SHA256: |
| 769 |
hash = EVP_sha256(); |
| 770 |
allowedLen = 32; |
| 771 |
break; |
| 772 |
case HashAlgo::SHA384: |
| 773 |
hash = EVP_sha384(); |
| 774 |
allowedLen = 48; |
| 775 |
break; |
| 776 |
case HashAlgo::SHA512: |
| 777 |
hash = EVP_sha512(); |
| 778 |
allowedLen = 64; |
| 779 |
break; |
| 780 |
default: |
| 781 |
return false; |
| 782 |
} |
| 783 |
|
| 784 |
if (originalData.size() != allowedLen) { |
| 785 |
return false; |
| 786 |
} |
| 787 |
|
| 788 |
size_t sLen = pssParam->sLen; |
| 789 |
if (sLen > ((osslKey->getBitLength()+6)/8-2-allowedLen)) |
| 790 |
{ |
| 791 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 792 |
(unsigned long)sLen, osslKey->getBitLength()); |
| 793 |
return false; |
| 794 |
} |
| 795 |
|
| 796 |
int status = RSA_verify_PKCS1_PSS_mgf1(rsa, (unsigned char*)originalData.const_byte_str(), hash, hash, (unsigned char*) recoveredData.const_byte_str(), pssParam->sLen); |
| 797 |
|
| 798 |
return (status == 1); |
| 799 |
} |
| 800 |
else if (mechanism == AsymMech::RSA) |
| 801 |
{ |
| 802 |
// Specific implementation for raw RSA verifiction; originalData is assumed to contain the |
| 803 |
// full input data used to compute the signature and verification is performed by comparing |
| 804 |
// originalData to the data recovered from the signature |
| 805 |
|
| 806 |
// Check if the public key is the right type |
| 807 |
if (!publicKey->isOfType(OSSLRSAPublicKey::type)) |
| 808 |
{ |
| 809 |
ERROR_MSG("Invalid key type supplied"); |
| 810 |
|
| 811 |
return false; |
| 812 |
} |
| 813 |
|
| 814 |
// Perform the RSA public key operation |
| 815 |
OSSLRSAPublicKey* osslKey = (OSSLRSAPublicKey*) publicKey; |
| 816 |
|
| 817 |
ByteString recoveredData; |
| 818 |
|
| 819 |
recoveredData.resize(osslKey->getN().size()); |
| 820 |
|
| 821 |
RSA* rsa = osslKey->getOSSLKey(); |
| 822 |
|
| 823 |
int retLen = RSA_public_decrypt(signature.size(), (unsigned char*) signature.const_byte_str(), &recoveredData[0], rsa, RSA_NO_PADDING); |
| 824 |
|
| 825 |
if (retLen == -1) |
| 826 |
{ |
| 827 |
ERROR_MSG("Public key operation failed"); |
| 828 |
|
| 829 |
return false; |
| 830 |
} |
| 831 |
|
| 832 |
recoveredData.resize(retLen); |
| 833 |
|
| 834 |
return (originalData == recoveredData); |
| 835 |
} |
| 836 |
else |
| 837 |
{ |
| 838 |
// Call the generic function |
| 839 |
return AsymmetricAlgorithm::verify(publicKey, originalData, signature, mechanism, param, paramLen); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
bool OSSLRSA::verifyInit(PublicKey* publicKey, const AsymMech::Type mechanism, |
| 844 |
const void* param /* = NULL */, const size_t paramLen /* = 0 */) |
| 845 |
{ |
| 846 |
if (!AsymmetricAlgorithm::verifyInit(publicKey, mechanism, param, paramLen)) |
| 847 |
{ |
| 848 |
return false; |
| 849 |
} |
| 850 |
|
| 851 |
// Check if the public key is the right type |
| 852 |
if (!publicKey->isOfType(OSSLRSAPublicKey::type)) |
| 853 |
{ |
| 854 |
ERROR_MSG("Invalid key type supplied"); |
| 855 |
|
| 856 |
ByteString dummy; |
| 857 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 858 |
|
| 859 |
return false; |
| 860 |
} |
| 861 |
|
| 862 |
HashAlgo::Type hash1 = HashAlgo::Unknown; |
| 863 |
HashAlgo::Type hash2 = HashAlgo::Unknown; |
| 864 |
|
| 865 |
switch (mechanism) |
| 866 |
{ |
| 867 |
case AsymMech::RSA_MD5_PKCS: |
| 868 |
hash1 = HashAlgo::MD5; |
| 869 |
break; |
| 870 |
case AsymMech::RSA_SHA1_PKCS: |
| 871 |
hash1 = HashAlgo::SHA1; |
| 872 |
break; |
| 873 |
case AsymMech::RSA_SHA224_PKCS: |
| 874 |
hash1 = HashAlgo::SHA224; |
| 875 |
break; |
| 876 |
case AsymMech::RSA_SHA256_PKCS: |
| 877 |
hash1 = HashAlgo::SHA256; |
| 878 |
break; |
| 879 |
case AsymMech::RSA_SHA384_PKCS: |
| 880 |
hash1 = HashAlgo::SHA384; |
| 881 |
break; |
| 882 |
case AsymMech::RSA_SHA512_PKCS: |
| 883 |
hash1 = HashAlgo::SHA512; |
| 884 |
break; |
| 885 |
case AsymMech::RSA_SHA1_PKCS_PSS: |
| 886 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 887 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA1 || |
| 888 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA1) |
| 889 |
{ |
| 890 |
ERROR_MSG("Invalid parameters"); |
| 891 |
ByteString dummy; |
| 892 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 893 |
return false; |
| 894 |
} |
| 895 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 896 |
if (sLen > ((publicKey->getBitLength()+6)/8-2-20)) |
| 897 |
{ |
| 898 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 899 |
(unsigned long)sLen, publicKey->getBitLength()); |
| 900 |
ByteString dummy; |
| 901 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 902 |
return false; |
| 903 |
} |
| 904 |
hash1 = HashAlgo::SHA1; |
| 905 |
break; |
| 906 |
case AsymMech::RSA_SHA224_PKCS_PSS: |
| 907 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 908 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA224 || |
| 909 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA224) |
| 910 |
{ |
| 911 |
ERROR_MSG("Invalid parameters"); |
| 912 |
ByteString dummy; |
| 913 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 914 |
return false; |
| 915 |
} |
| 916 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 917 |
if (sLen > ((publicKey->getBitLength()+6)/8-2-28)) |
| 918 |
{ |
| 919 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 920 |
(unsigned long)sLen, publicKey->getBitLength()); |
| 921 |
ByteString dummy; |
| 922 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 923 |
return false; |
| 924 |
} |
| 925 |
hash1 = HashAlgo::SHA224; |
| 926 |
break; |
| 927 |
case AsymMech::RSA_SHA256_PKCS_PSS: |
| 928 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 929 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA256 || |
| 930 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA256) |
| 931 |
{ |
| 932 |
ERROR_MSG("Invalid parameters"); |
| 933 |
ByteString dummy; |
| 934 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 935 |
return false; |
| 936 |
} |
| 937 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 938 |
if (sLen > ((publicKey->getBitLength()+6)/8-2-32)) |
| 939 |
{ |
| 940 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 941 |
(unsigned long)sLen, publicKey->getBitLength()); |
| 942 |
ByteString dummy; |
| 943 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 944 |
return false; |
| 945 |
} |
| 946 |
hash1 = HashAlgo::SHA256; |
| 947 |
break; |
| 948 |
case AsymMech::RSA_SHA384_PKCS_PSS: |
| 949 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 950 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA384 || |
| 951 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA384) |
| 952 |
{ |
| 953 |
ERROR_MSG("Invalid parameters"); |
| 954 |
ByteString dummy; |
| 955 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 956 |
return false; |
| 957 |
} |
| 958 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 959 |
if (sLen > ((publicKey->getBitLength()+6)/8-2-48)) |
| 960 |
{ |
| 961 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 962 |
(unsigned long)sLen, publicKey->getBitLength()); |
| 963 |
ByteString dummy; |
| 964 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 965 |
return false; |
| 966 |
} |
| 967 |
hash1 = HashAlgo::SHA384; |
| 968 |
break; |
| 969 |
case AsymMech::RSA_SHA512_PKCS_PSS: |
| 970 |
if (param == NULL || paramLen != sizeof(RSA_PKCS_PSS_PARAMS) || |
| 971 |
((RSA_PKCS_PSS_PARAMS*) param)->hashAlg != HashAlgo::SHA512 || |
| 972 |
((RSA_PKCS_PSS_PARAMS*) param)->mgf != AsymRSAMGF::MGF1_SHA512) |
| 973 |
{ |
| 974 |
ERROR_MSG("Invalid parameters"); |
| 975 |
ByteString dummy; |
| 976 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 977 |
return false; |
| 978 |
} |
| 979 |
sLen = ((RSA_PKCS_PSS_PARAMS*) param)->sLen; |
| 980 |
if (sLen > ((publicKey->getBitLength()+6)/8-2-64)) |
| 981 |
{ |
| 982 |
ERROR_MSG("sLen (%lu) is too large for current key size (%lu)", |
| 983 |
(unsigned long)sLen, publicKey->getBitLength()); |
| 984 |
ByteString dummy; |
| 985 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 986 |
return false; |
| 987 |
} |
| 988 |
hash1 = HashAlgo::SHA512; |
| 989 |
break; |
| 990 |
case AsymMech::RSA_SSL: |
| 991 |
hash1 = HashAlgo::MD5; |
| 992 |
hash2 = HashAlgo::SHA1; |
| 993 |
break; |
| 994 |
default: |
| 995 |
ERROR_MSG("Invalid mechanism supplied (%i)", mechanism); |
| 996 |
|
| 997 |
ByteString dummy; |
| 998 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 999 |
|
| 1000 |
return false; |
| 1001 |
} |
| 1002 |
|
| 1003 |
pCurrentHash = CryptoFactory::i()->getHashAlgorithm(hash1); |
| 1004 |
|
| 1005 |
if (pCurrentHash == NULL || !pCurrentHash->hashInit()) |
| 1006 |
{ |
| 1007 |
if (pCurrentHash != NULL) |
| 1008 |
{ |
| 1009 |
delete pCurrentHash; |
| 1010 |
pCurrentHash = NULL; |
| 1011 |
} |
| 1012 |
|
| 1013 |
ByteString dummy; |
| 1014 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 1015 |
|
| 1016 |
return false; |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (hash2 != HashAlgo::Unknown) |
| 1020 |
{ |
| 1021 |
pSecondHash = CryptoFactory::i()->getHashAlgorithm(hash2); |
| 1022 |
|
| 1023 |
if (pSecondHash == NULL || !pSecondHash->hashInit()) |
| 1024 |
{ |
| 1025 |
delete pCurrentHash; |
| 1026 |
pCurrentHash = NULL; |
| 1027 |
|
| 1028 |
if (pSecondHash != NULL) |
| 1029 |
{ |
| 1030 |
delete pSecondHash; |
| 1031 |
pSecondHash = NULL; |
| 1032 |
} |
| 1033 |
|
| 1034 |
ByteString dummy; |
| 1035 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 1036 |
|
| 1037 |
return false; |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
return true; |
| 1042 |
} |
| 1043 |
|
| 1044 |
bool OSSLRSA::verifyUpdate(const ByteString& originalData) |
| 1045 |
{ |
| 1046 |
if (!AsymmetricAlgorithm::verifyUpdate(originalData)) |
| 1047 |
{ |
| 1048 |
return false; |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (!pCurrentHash->hashUpdate(originalData)) |
| 1052 |
{ |
| 1053 |
delete pCurrentHash; |
| 1054 |
pCurrentHash = NULL; |
| 1055 |
|
| 1056 |
ByteString dummy; |
| 1057 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 1058 |
|
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
|
| 1062 |
if ((pSecondHash != NULL) && !pSecondHash->hashUpdate(originalData)) |
| 1063 |
{ |
| 1064 |
delete pCurrentHash; |
| 1065 |
pCurrentHash = NULL; |
| 1066 |
|
| 1067 |
delete pSecondHash; |
| 1068 |
pSecondHash = NULL; |
| 1069 |
|
| 1070 |
ByteString dummy; |
| 1071 |
AsymmetricAlgorithm::verifyFinal(dummy); |
| 1072 |
|
| 1073 |
return false; |
| 1074 |
} |
| 1075 |
|
| 1076 |
return true; |
| 1077 |
} |
| 1078 |
|
| 1079 |
bool OSSLRSA::verifyFinal(const ByteString& signature) |
| 1080 |
{ |
| 1081 |
// Save necessary state before calling super class verifyFinal |
| 1082 |
OSSLRSAPublicKey* pk = (OSSLRSAPublicKey*) currentPublicKey; |
| 1083 |
AsymMech::Type mechanism = currentMechanism; |
| 1084 |
|
| 1085 |
if (!AsymmetricAlgorithm::verifyFinal(signature)) |
| 1086 |
{ |
| 1087 |
return false; |
| 1088 |
} |
| 1089 |
|
| 1090 |
ByteString firstHash, secondHash; |
| 1091 |
|
| 1092 |
bool bFirstResult = pCurrentHash->hashFinal(firstHash); |
| 1093 |
bool bSecondResult = (pSecondHash != NULL) ? pSecondHash->hashFinal(secondHash) : true; |
| 1094 |
|
| 1095 |
delete pCurrentHash; |
| 1096 |
pCurrentHash = NULL; |
| 1097 |
|
| 1098 |
if (pSecondHash != NULL) |
| 1099 |
{ |
| 1100 |
delete pSecondHash; |
| 1101 |
|
| 1102 |
pSecondHash = NULL; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (!bFirstResult || !bSecondResult) |
| 1106 |
{ |
| 1107 |
return false; |
| 1108 |
} |
| 1109 |
|
| 1110 |
ByteString digest = firstHash + secondHash; |
| 1111 |
|
| 1112 |
// Determine the signature NID type |
| 1113 |
int type = 0; |
| 1114 |
bool isPSS = false; |
| 1115 |
const EVP_MD* hash = NULL; |
| 1116 |
|
| 1117 |
switch (mechanism) |
| 1118 |
{ |
| 1119 |
case AsymMech::RSA_MD5_PKCS: |
| 1120 |
type = NID_md5; |
| 1121 |
break; |
| 1122 |
case AsymMech::RSA_SHA1_PKCS: |
| 1123 |
type = NID_sha1; |
| 1124 |
break; |
| 1125 |
case AsymMech::RSA_SHA224_PKCS: |
| 1126 |
type = NID_sha224; |
| 1127 |
break; |
| 1128 |
case AsymMech::RSA_SHA256_PKCS: |
| 1129 |
type = NID_sha256; |
| 1130 |
break; |
| 1131 |
case AsymMech::RSA_SHA384_PKCS: |
| 1132 |
type = NID_sha384; |
| 1133 |
break; |
| 1134 |
case AsymMech::RSA_SHA512_PKCS: |
| 1135 |
type = NID_sha512; |
| 1136 |
break; |
| 1137 |
case AsymMech::RSA_SHA1_PKCS_PSS: |
| 1138 |
isPSS = true; |
| 1139 |
hash = EVP_sha1(); |
| 1140 |
break; |
| 1141 |
case AsymMech::RSA_SHA224_PKCS_PSS: |
| 1142 |
isPSS = true; |
| 1143 |
hash = EVP_sha224(); |
| 1144 |
break; |
| 1145 |
case AsymMech::RSA_SHA256_PKCS_PSS: |
| 1146 |
isPSS = true; |
| 1147 |
hash = EVP_sha256(); |
| 1148 |
break; |
| 1149 |
case AsymMech::RSA_SHA384_PKCS_PSS: |
| 1150 |
isPSS = true; |
| 1151 |
hash = EVP_sha384(); |
| 1152 |
break; |
| 1153 |
case AsymMech::RSA_SHA512_PKCS_PSS: |
| 1154 |
isPSS = true; |
| 1155 |
hash = EVP_sha512(); |
| 1156 |
break; |
| 1157 |
case AsymMech::RSA_SSL: |
| 1158 |
type = NID_md5_sha1; |
| 1159 |
break; |
| 1160 |
default: |
| 1161 |
break; |
| 1162 |
} |
| 1163 |
|
| 1164 |
// Perform the verify operation |
| 1165 |
bool rv; |
| 1166 |
|
| 1167 |
if (isPSS) |
| 1168 |
{ |
| 1169 |
ByteString plain; |
| 1170 |
plain.resize(pk->getN().size()); |
| 1171 |
int result = RSA_public_decrypt(signature.size(), |
| 1172 |
(unsigned char*) signature.const_byte_str(), |
| 1173 |
&plain[0], |
| 1174 |
pk->getOSSLKey(), |
| 1175 |
RSA_NO_PADDING); |
| 1176 |
if (result < 0) |
| 1177 |
{ |
| 1178 |
rv = false; |
| 1179 |
ERROR_MSG("RSA public decrypt failed (0x%08X)", ERR_get_error()); |
| 1180 |
} |
| 1181 |
else |
| 1182 |
{ |
| 1183 |
plain.resize(result); |
| 1184 |
result = RSA_verify_PKCS1_PSS(pk->getOSSLKey(), &digest[0], |
| 1185 |
hash, &plain[0], sLen); |
| 1186 |
if (result == 1) |
| 1187 |
{ |
| 1188 |
rv = true; |
| 1189 |
} |
| 1190 |
else |
| 1191 |
{ |
| 1192 |
rv = false; |
| 1193 |
ERROR_MSG("RSA PSS verify failed (0x%08X)", ERR_get_error()); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
else |
| 1198 |
{ |
| 1199 |
rv = (RSA_verify(type, &digest[0], digest.size(), (unsigned char*) signature.const_byte_str(), signature.size(), pk->getOSSLKey()) == 1); |
| 1200 |
|
| 1201 |
if (!rv) ERROR_MSG("RSA verify failed (0x%08X)", ERR_get_error()); |
| 1202 |
} |
| 1203 |
|
| 1204 |
return rv; |
| 1205 |
} |
| 1206 |
|
| 1207 |
// Encryption functions |
| 1208 |
bool OSSLRSA::encrypt(PublicKey* publicKey, const ByteString& data, |
| 1209 |
ByteString& encryptedData, const AsymMech::Type padding) |
| 1210 |
{ |
| 1211 |
// Check if the public key is the right type |
| 1212 |
if (!publicKey->isOfType(OSSLRSAPublicKey::type)) |
| 1213 |
{ |
| 1214 |
ERROR_MSG("Invalid key type supplied"); |
| 1215 |
|
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
|
| 1219 |
// Retrieve the OpenSSL key object |
| 1220 |
RSA* rsa = ((OSSLRSAPublicKey*) publicKey)->getOSSLKey(); |
| 1221 |
|
| 1222 |
// Check the data and padding algorithm |
| 1223 |
int osslPadding = 0; |
| 1224 |
|
| 1225 |
if (padding == AsymMech::RSA_PKCS) |
| 1226 |
{ |
| 1227 |
// The size of the input data cannot be more than the modulus |
| 1228 |
// length of the key - 11 |
| 1229 |
if (data.size() > (size_t) (RSA_size(rsa) - 11)) |
| 1230 |
{ |
| 1231 |
ERROR_MSG("Too much data supplied for RSA PKCS #1 encryption"); |
| 1232 |
|
| 1233 |
return false; |
| 1234 |
} |
| 1235 |
|
| 1236 |
osslPadding = RSA_PKCS1_PADDING; |
| 1237 |
} |
| 1238 |
else if (padding == AsymMech::RSA_PKCS_OAEP) |
| 1239 |
{ |
| 1240 |
// The size of the input data cannot be more than the modulus |
| 1241 |
// length of the key - 41 |
| 1242 |
if (data.size() > (size_t) (RSA_size(rsa) - 41)) |
| 1243 |
{ |
| 1244 |
ERROR_MSG("Too much data supplied for RSA OAEP encryption"); |
| 1245 |
|
| 1246 |
return false; |
| 1247 |
} |
| 1248 |
|
| 1249 |
osslPadding = RSA_PKCS1_OAEP_PADDING; |
| 1250 |
} |
| 1251 |
else if (padding == AsymMech::RSA) |
| 1252 |
{ |
| 1253 |
// The size of the input data should be exactly equal to the modulus length |
| 1254 |
if (data.size() != (size_t) RSA_size(rsa)) |
| 1255 |
{ |
| 1256 |
ERROR_MSG("Incorrect amount of input data supplied for raw RSA encryption"); |
| 1257 |
|
| 1258 |
return false; |
| 1259 |
} |
| 1260 |
|
| 1261 |
osslPadding = RSA_NO_PADDING; |
| 1262 |
} |
| 1263 |
else |
| 1264 |
{ |
| 1265 |
ERROR_MSG("Invalid padding mechanism supplied (%i)", padding); |
| 1266 |
|
| 1267 |
return false; |
| 1268 |
} |
| 1269 |
|
| 1270 |
// Perform the RSA operation |
| 1271 |
encryptedData.resize(RSA_size(rsa)); |
| 1272 |
|
| 1273 |
if (RSA_public_encrypt(data.size(), (unsigned char*) data.const_byte_str(), &encryptedData[0], rsa, osslPadding) == -1) |
| 1274 |
{ |
| 1275 |
ERROR_MSG("RSA public key encryption failed (0x%08X)", ERR_get_error()); |
| 1276 |
|
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
|
| 1280 |
return true; |
| 1281 |
} |
| 1282 |
|
| 1283 |
// Decryption functions |
| 1284 |
bool OSSLRSA::decrypt(PrivateKey* privateKey, const ByteString& encryptedData, |
| 1285 |
ByteString& data, const AsymMech::Type padding) |
| 1286 |
{ |
| 1287 |
// Check if the private key is the right type |
| 1288 |
if (!privateKey->isOfType(OSSLRSAPrivateKey::type)) |
| 1289 |
{ |
| 1290 |
ERROR_MSG("Invalid key type supplied"); |
| 1291 |
|
| 1292 |
return false; |
| 1293 |
} |
| 1294 |
|
| 1295 |
// Retrieve the OpenSSL key object |
| 1296 |
RSA* rsa = ((OSSLRSAPrivateKey*) privateKey)->getOSSLKey(); |
| 1297 |
|
| 1298 |
// Check the input size |
| 1299 |
if (encryptedData.size() != (size_t) RSA_size(rsa)) |
| 1300 |
{ |
| 1301 |
ERROR_MSG("Invalid amount of input data supplied for RSA decryption"); |
| 1302 |
|
| 1303 |
return false; |
| 1304 |
} |
| 1305 |
|
| 1306 |
// Determine the OpenSSL padding algorithm |
| 1307 |
int osslPadding = 0; |
| 1308 |
|
| 1309 |
switch (padding) |
| 1310 |
{ |
| 1311 |
case AsymMech::RSA_PKCS: |
| 1312 |
osslPadding = RSA_PKCS1_PADDING; |
| 1313 |
break; |
| 1314 |
case AsymMech::RSA_PKCS_OAEP: |
| 1315 |
osslPadding = RSA_PKCS1_OAEP_PADDING; |
| 1316 |
break; |
| 1317 |
case AsymMech::RSA: |
| 1318 |
osslPadding = RSA_NO_PADDING; |
| 1319 |
break; |
| 1320 |
default: |
| 1321 |
ERROR_MSG("Invalid padding mechanism supplied (%i)", padding); |
| 1322 |
return false; |
| 1323 |
} |
| 1324 |
|
| 1325 |
// Perform the RSA operation |
| 1326 |
data.resize(RSA_size(rsa)); |
| 1327 |
|
| 1328 |
int decSize = RSA_private_decrypt(encryptedData.size(), (unsigned char*) encryptedData.const_byte_str(), &data[0], rsa, osslPadding); |
| 1329 |
|
| 1330 |
if (decSize == -1) |
| 1331 |
{ |
| 1332 |
ERROR_MSG("RSA private key decryption failed (0x%08X)", ERR_get_error()); |
| 1333 |
|
| 1334 |
return false; |
| 1335 |
} |
| 1336 |
|
| 1337 |
data.resize(decSize); |
| 1338 |
|
| 1339 |
return true; |
| 1340 |
} |
| 1341 |
|
| 1342 |
// Key factory |
| 1343 |
bool OSSLRSA::generateKeyPair(AsymmetricKeyPair** ppKeyPair, AsymmetricParameters* parameters, RNG* /*rng = NULL */) |
| 1344 |
{ |
| 1345 |
// Check parameters |
| 1346 |
if ((ppKeyPair == NULL) || |
| 1347 |
(parameters == NULL)) |
| 1348 |
{ |
| 1349 |
return false; |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (!parameters->areOfType(RSAParameters::type)) |
| 1353 |
{ |
| 1354 |
ERROR_MSG("Invalid parameters supplied for RSA key generation"); |
| 1355 |
|
| 1356 |
return false; |
| 1357 |
} |
| 1358 |
|
| 1359 |
RSAParameters* params = (RSAParameters*) parameters; |
| 1360 |
|
| 1361 |
if (params->getBitLength() < getMinKeySize() || params->getBitLength() > getMaxKeySize()) |
| 1362 |
{ |
| 1363 |
ERROR_MSG("This RSA key size (%lu) is not supported", params->getBitLength()); |
| 1364 |
|
| 1365 |
return false; |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (params->getBitLength() < 1024) |
| 1369 |
{ |
| 1370 |
WARNING_MSG("Using an RSA key size < 1024 bits is not recommended"); |
| 1371 |
} |
| 1372 |
|
| 1373 |
// Retrieve the desired public exponent |
| 1374 |
unsigned long e = params->getE().long_val(); |
| 1375 |
|
| 1376 |
// Check the public exponent |
| 1377 |
if ((e == 0) || (e % 2 != 1)) |
| 1378 |
{ |
| 1379 |
ERROR_MSG("Invalid RSA public exponent %d", e); |
| 1380 |
|
| 1381 |
return false; |
| 1382 |
} |
| 1383 |
|
| 1384 |
// Generate the key-pair |
| 1385 |
RSA* rsa = RSA_new(); |
| 1386 |
if (rsa == NULL) |
| 1387 |
{ |
| 1388 |
ERROR_MSG("Failed to instantiate OpenSSL RSA object"); |
| 1389 |
|
| 1390 |
return false; |
| 1391 |
} |
| 1392 |
|
| 1393 |
BIGNUM* bn_e = OSSL::byteString2bn(params->getE()); |
| 1394 |
|
| 1395 |
// Check if the key was successfully generated |
| 1396 |
if (!RSA_generate_key_ex(rsa, params->getBitLength(), bn_e, NULL)) |
| 1397 |
{ |
| 1398 |
ERROR_MSG("RSA key generation failed (0x%08X)", ERR_get_error()); |
| 1399 |
BN_free(bn_e); |
| 1400 |
RSA_free(rsa); |
| 1401 |
|
| 1402 |
return false; |
| 1403 |
} |
| 1404 |
BN_free(bn_e); |
| 1405 |
|
| 1406 |
// Create an asymmetric key-pair object to return |
| 1407 |
OSSLRSAKeyPair* kp = new OSSLRSAKeyPair(); |
| 1408 |
|
| 1409 |
((OSSLRSAPublicKey*) kp->getPublicKey())->setFromOSSL(rsa); |
| 1410 |
((OSSLRSAPrivateKey*) kp->getPrivateKey())->setFromOSSL(rsa); |
| 1411 |
|
| 1412 |
*ppKeyPair = kp; |
| 1413 |
|
| 1414 |
// Release the key |
| 1415 |
RSA_free(rsa); |
| 1416 |
|
| 1417 |
return true; |
| 1418 |
} |
| 1419 |
|
| 1420 |
unsigned long OSSLRSA::getMinKeySize() |
| 1421 |
{ |
| 1422 |
#ifdef WITH_FIPS |
| 1423 |
// OPENSSL_RSA_FIPS_MIN_MODULUS_BITS is 1024 |
| 1424 |
return 1024; |
| 1425 |
#else |
| 1426 |
return 512; |
| 1427 |
#endif |
| 1428 |
} |
| 1429 |
|
| 1430 |
unsigned long OSSLRSA::getMaxKeySize() |
| 1431 |
{ |
| 1432 |
return OPENSSL_RSA_MAX_MODULUS_BITS; |
| 1433 |
} |
| 1434 |
|
| 1435 |
bool OSSLRSA::reconstructKeyPair(AsymmetricKeyPair** ppKeyPair, ByteString& serialisedData) |
| 1436 |
{ |
| 1437 |
// Check input |
| 1438 |
if ((ppKeyPair == NULL) || |
| 1439 |
(serialisedData.size() == 0)) |
| 1440 |
{ |
| 1441 |
return false; |
| 1442 |
} |
| 1443 |
|
| 1444 |
ByteString dPub = ByteString::chainDeserialise(serialisedData); |
| 1445 |
ByteString dPriv = ByteString::chainDeserialise(serialisedData); |
| 1446 |
|
| 1447 |
OSSLRSAKeyPair* kp = new OSSLRSAKeyPair(); |
| 1448 |
|
| 1449 |
bool rv = true; |
| 1450 |
|
| 1451 |
if (!((RSAPublicKey*) kp->getPublicKey())->deserialise(dPub)) |
| 1452 |
{ |
| 1453 |
rv = false; |
| 1454 |
} |
| 1455 |
|
| 1456 |
if (!((RSAPrivateKey*) kp->getPrivateKey())->deserialise(dPriv)) |
| 1457 |
{ |
| 1458 |
rv = false; |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (!rv) |
| 1462 |
{ |
| 1463 |
delete kp; |
| 1464 |
|
| 1465 |
return false; |
| 1466 |
} |
| 1467 |
|
| 1468 |
*ppKeyPair = kp; |
| 1469 |
|
| 1470 |
return true; |
| 1471 |
} |
| 1472 |
|
| 1473 |
bool OSSLRSA::reconstructPublicKey(PublicKey** ppPublicKey, ByteString& serialisedData) |
| 1474 |
{ |
| 1475 |
// Check input |
| 1476 |
if ((ppPublicKey == NULL) || |
| 1477 |
(serialisedData.size() == 0)) |
| 1478 |
{ |
| 1479 |
return false; |
| 1480 |
} |
| 1481 |
|
| 1482 |
OSSLRSAPublicKey* pub = new OSSLRSAPublicKey(); |
| 1483 |
|
| 1484 |
if (!pub->deserialise(serialisedData)) |
| 1485 |
{ |
| 1486 |
delete pub; |
| 1487 |
|
| 1488 |
return false; |
| 1489 |
} |
| 1490 |
|
| 1491 |
*ppPublicKey = pub; |
| 1492 |
|
| 1493 |
return true; |
| 1494 |
} |
| 1495 |
|
| 1496 |
bool OSSLRSA::reconstructPrivateKey(PrivateKey** ppPrivateKey, ByteString& serialisedData) |
| 1497 |
{ |
| 1498 |
// Check input |
| 1499 |
if ((ppPrivateKey == NULL) || |
| 1500 |
(serialisedData.size() == 0)) |
| 1501 |
{ |
| 1502 |
return false; |
| 1503 |
} |
| 1504 |
|
| 1505 |
OSSLRSAPrivateKey* priv = new OSSLRSAPrivateKey(); |
| 1506 |
|
| 1507 |
if (!priv->deserialise(serialisedData)) |
| 1508 |
{ |
| 1509 |
delete priv; |
| 1510 |
|
| 1511 |
return false; |
| 1512 |
} |
| 1513 |
|
| 1514 |
*ppPrivateKey = priv; |
| 1515 |
|
| 1516 |
return true; |
| 1517 |
} |
| 1518 |
|
| 1519 |
PublicKey* OSSLRSA::newPublicKey() |
| 1520 |
{ |
| 1521 |
return (PublicKey*) new OSSLRSAPublicKey(); |
| 1522 |
} |
| 1523 |
|
| 1524 |
PrivateKey* OSSLRSA::newPrivateKey() |
| 1525 |
{ |
| 1526 |
return (PrivateKey*) new OSSLRSAPrivateKey(); |
| 1527 |
} |
| 1528 |
|
| 1529 |
AsymmetricParameters* OSSLRSA::newParameters() |
| 1530 |
{ |
| 1531 |
return (AsymmetricParameters*) new RSAParameters(); |
| 1532 |
} |
| 1533 |
|
| 1534 |
bool OSSLRSA::reconstructParameters(AsymmetricParameters** ppParams, ByteString& serialisedData) |
| 1535 |
{ |
| 1536 |
// Check input parameters |
| 1537 |
if ((ppParams == NULL) || (serialisedData.size() == 0)) |
| 1538 |
{ |
| 1539 |
return false; |
| 1540 |
} |
| 1541 |
|
| 1542 |
RSAParameters* params = new RSAParameters(); |
| 1543 |
|
| 1544 |
if (!params->deserialise(serialisedData)) |
| 1545 |
{ |
| 1546 |
delete params; |
| 1547 |
|
| 1548 |
return false; |
| 1549 |
} |
| 1550 |
|
| 1551 |
*ppParams = params; |
| 1552 |
|
| 1553 |
return true; |
| 1554 |
} |