diff --git a/src/core/libs/osnma_msg_receiver.cc b/src/core/libs/osnma_msg_receiver.cc index d284dcd85..8572ec03d 100644 --- a/src/core/libs/osnma_msg_receiver.cc +++ b/src/core/libs/osnma_msg_receiver.cc @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -46,17 +47,18 @@ namespace wht = std; #endif #if USE_OPENSSL_FALLBACK +#include #include #if USE_OPENSSL_3 #include #define OPENSSL_ENGINE NULL #else -#include #include #endif #else #include #include +#include #endif osnma_msg_receiver_sptr osnma_msg_receiver_make() @@ -795,4 +797,63 @@ std::vector osnma_msg_receiver::computeCMAC_AES(const std::vector osnma_msg_receiver::readPublicKeyFromPEM(const std::string& filePath) +{ + std::vector 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(file)), std::istreambuf_iterator()); + + 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(const_cast(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; +} \ No newline at end of file diff --git a/src/core/libs/osnma_msg_receiver.h b/src/core/libs/osnma_msg_receiver.h index 0a182f216..149d20b43 100644 --- a/src/core/libs/osnma_msg_receiver.h +++ b/src/core/libs/osnma_msg_receiver.h @@ -73,6 +73,7 @@ private: std::vector computeSHA3_256(const std::vector& input); std::vector computeHMAC_SHA_256(const std::vector& key, const std::vector& input); std::vector computeCMAC_AES(const std::vector& key, const std::vector& input); + std::vector readPublicKeyFromPEM(const std::string& filePath); std::unique_ptr d_dsm_reader;