20 #include <openssl/pem.h>
21 #include <openssl/err.h>
22 #include "../../log.h"
23 #include "../../crypto/cert/X509Cert.h"
34 , privateKey(privateKey)
56 , privateKey(privateKey)
78 unsigned int blockSize = RSA_size(privateKey);
79 unsigned int neededSize = blockSize;
80 if(digest.length > blockSize)
82 if(digest.length % blockSize == 0)
83 neededSize = digest.length;
85 neededSize = ((digest.length / blockSize) + 1) * blockSize;
89 std::vector<unsigned char> signature(neededSize, 0);
92 unsigned int signatureLength = 0;
93 int result = RSA_sign(digest.type, digest.digest, digest.length, &signature[0], &signatureLength, privateKey);
98 THROW_IOEXCEPTION(
"Failed to sign the digest: %s", ERR_reason_error_string(ERR_get_error()));
101 if(signatureLength != neededSize)
125 THROW_IOEXCEPTION(
"X.509 certificate parameter is not set in RSACrypt, can not verify signature.");
129 EVP_PKEY* key = X509_get_pubkey(cert);
130 if(!key || EVP_PKEY_type(key->type) != EVP_PKEY_RSA)
133 THROW_IOEXCEPTION(
"Certificate does not have a RSA public key, can not verify signature.");
135 RSA* publicKey = EVP_PKEY_get1_RSA(key);
138 int result = RSA_verify(digestMethod, &digest[0], (
unsigned int)digest.size(),
139 &signature[0], (
unsigned int)signature.size(), publicKey);
142 return (result == 1);
157 BIO*
file = BIO_new_file(path.c_str(),
"rb");
161 path.c_str(), ERR_reason_error_string(ERR_get_error()));
165 RSA* key = PEM_read_bio_RSAPrivateKey(file, NULL, NULL, NULL);
170 path.c_str(), ERR_reason_error_string(ERR_get_error()));