1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-01-31 11:19:18 +00:00

Read .pem file with gnutls

This commit is contained in:
Carles Fernandez 2023-06-07 12:42:21 +02:00
parent a75c2acb31
commit d351049eb2
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
2 changed files with 64 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <iterator>
#include <numeric>
@ -46,17 +47,18 @@ namespace wht = std;
#endif
#if USE_OPENSSL_FALLBACK
#include <openssl/cmac.h>
#include <openssl/hmac.h>
#if USE_OPENSSL_3
#include <openssl/evp.h>
#define OPENSSL_ENGINE NULL
#else
#include <openssl/cmac.h>
#include <openssl/sha.h>
#endif
#else
#include <gnutls/crypto.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h>
#endif
osnma_msg_receiver_sptr osnma_msg_receiver_make()
@ -796,3 +798,62 @@ std::vector<uint8_t> osnma_msg_receiver::computeCMAC_AES(const std::vector<uint8
// {
// int verificationStatus = gnutls_pubkey_verify_data(publicKey, GNUTLS_DIG_SHA256, 0, message, messageSize, signature, signatureSize);
// return verificationStatus == 0;
std::vector<uint8_t> osnma_msg_receiver::readPublicKeyFromPEM(const std::string& filePath)
{
std::vector<uint8_t> publicKey;
#if USE_OPENSSL_FALLBACK
#if USE_OPENSSL_3
#else
#endif
#else
// Open the .pem file
std::ifstream file(filePath);
if (!file)
{
std::cerr << "Failed to open the file: " << filePath << std::endl;
return publicKey;
}
// Read the contents of the .pem file into a string
std::string pemContents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
gnutls_x509_crt_t cert;
gnutls_x509_crt_init(&cert);
// Import the certificate from the PEM file
gnutls_datum_t pemData;
pemData.data = reinterpret_cast<unsigned char*>(const_cast<char*>(pemContents.data()));
pemData.size = pemContents.size();
int ret = gnutls_x509_crt_import(cert, &pemData, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{
std::cerr << "Failed to import certificate from PEM file" << std::endl;
gnutls_x509_crt_deinit(cert);
return publicKey;
}
// Export the public key data
size_t pubkey_data_size = 0;
ret = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, nullptr, &pubkey_data_size);
if (ret < 0)
{
std::cerr << "Failed to export public key data" << std::endl;
gnutls_x509_crt_deinit(cert);
return publicKey;
}
publicKey.resize(pubkey_data_size);
ret = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, publicKey.data(), &pubkey_data_size);
if (ret < 0)
{
std::cerr << "Failed to export public key data" << std::endl;
gnutls_x509_crt_deinit(cert);
return publicKey;
}
gnutls_x509_crt_deinit(cert);
#endif
return publicKey;
}

View File

@ -73,6 +73,7 @@ private:
std::vector<uint8_t> computeSHA3_256(const std::vector<uint8_t>& input);
std::vector<uint8_t> computeHMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input);
std::vector<uint8_t> computeCMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input);
std::vector<uint8_t> readPublicKeyFromPEM(const std::string& filePath);
std::unique_ptr<OSNMA_DSM_Reader> d_dsm_reader;