mirror of
https://github.com/gnss-sdr/gnss-sdr
synced 2025-07-04 19:12:57 +00:00
Fix building against OpenSSL 1.0
This commit is contained in:
parent
9a1def7aa4
commit
23bb5c85c5
@ -380,24 +380,16 @@ std::vector<uint8_t> Gnss_Crypto::computeHMAC_SHA_256(const std::vector<uint8_t>
|
|||||||
hmac.resize(output_length);
|
hmac.resize(output_length);
|
||||||
output = hmac;
|
output = hmac;
|
||||||
#else
|
#else
|
||||||
std::vector<uint8_t> hmac(32);
|
unsigned int outputLength = EVP_MAX_MD_SIZE;
|
||||||
// Create HMAC context
|
unsigned char* result = HMAC(EVP_sha256(), key.data(), key.size(), input.data(), input.size(), output.data(), &outputLength);
|
||||||
HMAC_CTX* ctx = HMAC_CTX_new();
|
if (result == nullptr)
|
||||||
HMAC_Init_ex(ctx, key.data(), key.size(), EVP_sha256(), nullptr);
|
{
|
||||||
|
LOG(WARNING) << "OSNMA HMAC_SHA_256 computation failed to compute HMAC-SHA256";
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
// Update HMAC context with the message
|
// Resize the output vector to the actual length of the HMAC-SHA256 output
|
||||||
HMAC_Update(ctx, input.data(), input.size());
|
output.resize(outputLength);
|
||||||
|
|
||||||
// Finalize HMAC computation
|
|
||||||
unsigned int hmacLen;
|
|
||||||
HMAC_Final(ctx, hmac.data(), &hmacLen);
|
|
||||||
|
|
||||||
// Clean up HMAC context
|
|
||||||
HMAC_CTX_free(ctx);
|
|
||||||
|
|
||||||
// Resize the HMAC vector to the actual length
|
|
||||||
hmac.resize(hmacLen);
|
|
||||||
output = hmac;
|
|
||||||
#endif
|
#endif
|
||||||
#else // GnuTLS
|
#else // GnuTLS
|
||||||
std::vector<uint8_t> output_aux(32);
|
std::vector<uint8_t> output_aux(32);
|
||||||
@ -595,6 +587,7 @@ bool Gnss_Crypto::readPublicKeyFromCRT(const std::string& crtFilePath)
|
|||||||
|
|
||||||
// Read the public key from the certificate
|
// Read the public key from the certificate
|
||||||
EVP_PKEY* pubkey = X509_get_pubkey(cert);
|
EVP_PKEY* pubkey = X509_get_pubkey(cert);
|
||||||
|
#if USE_OPENSSL_3
|
||||||
if (!pubkey)
|
if (!pubkey)
|
||||||
{
|
{
|
||||||
std::cerr << "Failed to extract the public key" << std::endl;
|
std::cerr << "Failed to extract the public key" << std::endl;
|
||||||
@ -603,6 +596,18 @@ bool Gnss_Crypto::readPublicKeyFromCRT(const std::string& crtFilePath)
|
|||||||
}
|
}
|
||||||
pubkey_copy(pubkey, &d_PublicKey);
|
pubkey_copy(pubkey, &d_PublicKey);
|
||||||
EVP_PKEY_free(pubkey);
|
EVP_PKEY_free(pubkey);
|
||||||
|
#else
|
||||||
|
EC_KEY* ec_pubkey = EVP_PKEY_get1_EC_KEY(pubkey);
|
||||||
|
EVP_PKEY_free(pubkey);
|
||||||
|
if (!ec_pubkey)
|
||||||
|
{
|
||||||
|
std::cerr << "Failed to extract the public key" << std::endl;
|
||||||
|
X509_free(cert);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
pubkey_copy(ec_pubkey, &d_PublicKey);
|
||||||
|
EC_KEY_free(ec_pubkey);
|
||||||
|
#endif
|
||||||
BIO_free(bio);
|
BIO_free(bio);
|
||||||
X509_free(cert);
|
X509_free(cert);
|
||||||
#else // GnuTLS
|
#else // GnuTLS
|
||||||
@ -838,12 +843,19 @@ void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publicKey)
|
|||||||
LOG(INFO) << "OpenSSL: error setting the OSNMA public key.";
|
LOG(INFO) << "OpenSSL: error setting the OSNMA public key.";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#if USE_OPENSSL_3
|
||||||
if (!pubkey_copy(pkey, &d_PublicKey))
|
if (!pubkey_copy(pkey, &d_PublicKey))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
EC_KEY* ec_pkey = EVP_PKEY_get1_EC_KEY(pkey);
|
||||||
|
if (!pubkey_copy(ec_pkey, &d_PublicKey))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
EC_KEY_free(ec_pkey);
|
||||||
|
#endif
|
||||||
EVP_PKEY_free(pkey);
|
EVP_PKEY_free(pkey);
|
||||||
#else // GnuTLS
|
#else // GnuTLS
|
||||||
gnutls_pubkey_t pubkey;
|
gnutls_pubkey_t pubkey;
|
||||||
@ -865,6 +877,7 @@ void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publicKey)
|
|||||||
|
|
||||||
|
|
||||||
#if USE_OPENSSL_FALLBACK
|
#if USE_OPENSSL_FALLBACK
|
||||||
|
#if USE_OPENSSL_3
|
||||||
bool Gnss_Crypto::pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest)
|
bool Gnss_Crypto::pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest)
|
||||||
{
|
{
|
||||||
// Open a memory buffer
|
// Open a memory buffer
|
||||||
@ -909,6 +922,54 @@ bool Gnss_Crypto::pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else // OpenSSL 1.x
|
||||||
|
|
||||||
|
bool Gnss_Crypto::pubkey_copy(EC_KEY* src, EC_KEY** dest)
|
||||||
|
{
|
||||||
|
// Open a memory buffer
|
||||||
|
BIO* mem_bio = BIO_new(BIO_s_mem());
|
||||||
|
if (mem_bio == nullptr)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Export the public key from src into the memory buffer in PEM format
|
||||||
|
if (!PEM_write_bio_EC_PUBKEY(mem_bio, src))
|
||||||
|
{
|
||||||
|
BIO_free(mem_bio);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the data from the memory buffer
|
||||||
|
char* bio_data;
|
||||||
|
long data_len = BIO_get_mem_data(mem_bio, &bio_data);
|
||||||
|
|
||||||
|
// Create a new memory buffer and load the data into it
|
||||||
|
BIO* mem_bio2 = BIO_new_mem_buf(bio_data, data_len);
|
||||||
|
if (mem_bio2 == nullptr)
|
||||||
|
{
|
||||||
|
BIO_free(mem_bio);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the public key from the new memory buffer
|
||||||
|
*dest = PEM_read_bio_EC_PUBKEY(mem_bio2, nullptr, nullptr, nullptr);
|
||||||
|
if (*dest == nullptr)
|
||||||
|
{
|
||||||
|
BIO_free(mem_bio);
|
||||||
|
BIO_free(mem_bio2);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
BIO_free(mem_bio);
|
||||||
|
BIO_free(mem_bio2);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#else // GnuTLS-specific functions
|
#else // GnuTLS-specific functions
|
||||||
|
|
||||||
bool Gnss_Crypto::convert_raw_to_der_ecdsa(const std::vector<uint8_t>& raw_signature, std::vector<uint8_t>& der_signature) const
|
bool Gnss_Crypto::convert_raw_to_der_ecdsa(const std::vector<uint8_t>& raw_signature, std::vector<uint8_t>& der_signature) const
|
||||||
|
@ -61,11 +61,12 @@ private:
|
|||||||
std::vector<uint8_t> convert_from_hex_str(const std::string& input) const;
|
std::vector<uint8_t> convert_from_hex_str(const std::string& input) const;
|
||||||
#if USE_OPENSSL_FALLBACK
|
#if USE_OPENSSL_FALLBACK
|
||||||
#if USE_OPENSSL_3
|
#if USE_OPENSSL_3
|
||||||
|
bool pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest);
|
||||||
EVP_PKEY* d_PublicKey{};
|
EVP_PKEY* d_PublicKey{};
|
||||||
#else
|
#else
|
||||||
|
bool pubkey_copy(EC_KEY* src, EC_KEY** dest);
|
||||||
EC_KEY* d_PublicKey = nullptr;
|
EC_KEY* d_PublicKey = nullptr;
|
||||||
#endif
|
#endif
|
||||||
bool pubkey_copy(EVP_PKEY* src, EVP_PKEY** dest);
|
|
||||||
#else // GnuTLS
|
#else // GnuTLS
|
||||||
gnutls_pubkey_t d_PublicKey{};
|
gnutls_pubkey_t d_PublicKey{};
|
||||||
bool convert_raw_to_der_ecdsa(const std::vector<uint8_t>& raw_signature, std::vector<uint8_t>& der_signature) const;
|
bool convert_raw_to_der_ecdsa(const std::vector<uint8_t>& raw_signature, std::vector<uint8_t>& der_signature) const;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user