1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2024-12-14 04:00:34 +00:00

Add work on OSNMA receiver

This commit is contained in:
Carles Fernandez 2023-06-11 20:30:03 +02:00
parent c8347584f8
commit e75bdeb5f6
No known key found for this signature in database
GPG Key ID: 4C583C52B0C3877D
4 changed files with 93 additions and 184 deletions

View File

@ -397,6 +397,11 @@ void osnma_msg_receiver::process_dsm_message(const std::vector<uint8_t>& dsm_msg
d_osnma_data.d_dsm_pkr_message.p_dp[k] = dsm_msg[l_dp - l_pd + k]; d_osnma_data.d_dsm_pkr_message.p_dp[k] = dsm_msg[l_dp - l_pd + k];
} }
// std::vector<uint8_t> mi; // (NPKT + NPKID + NPK) // std::vector<uint8_t> mi; // (NPKT + NPKID + NPK)
std::cout << "Galileo OSNMA: DSM-PKR with CID=" << static_cast<uint32_t>(d_osnma_data.d_nma_header.cid)
<< ", PKID=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.pkid)
<< ", WN=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.wn_k)
<< ", TOW=" << static_cast<uint32_t>(d_osnma_data.d_dsm_kroot_message.towh_k) * 3600
<< " received" << std::endl;
} }
} }
else else

View File

@ -134,37 +134,37 @@ target_include_directories(core_system_parameters
if(OPENSSL_FOUND) if(OPENSSL_FOUND)
if(TARGET OpenSSL::SSL) if(TARGET OpenSSL::SSL)
target_link_libraries(core_system_parameters target_link_libraries(core_system_parameters
PRIVATE PUBLIC
OpenSSL::SSL OpenSSL::SSL
) )
else() else()
target_link_libraries(core_system_parameters target_link_libraries(core_system_parameters
PRIVATE PUBLIC
${OPENSSL_LIBRARIES} ${OPENSSL_LIBRARIES}
) )
target_include_directories(core_system_parameters target_include_directories(core_system_parameters
PRIVATE PUBLIC
${OPENSSL_INCLUDE_DIR} ${OPENSSL_INCLUDE_DIR}
) )
endif() endif()
if(OPENSSL_VERSION) if(OPENSSL_VERSION)
if(OPENSSL_VERSION VERSION_GREATER "3.0.0") if(OPENSSL_VERSION VERSION_GREATER "3.0.0")
target_compile_definitions(core_system_parameters PRIVATE -DUSE_OPENSSL_3=1) target_compile_definitions(core_system_parameters PUBLIC -DUSE_OPENSSL_3=1)
endif() endif()
endif() endif()
else() else()
target_link_libraries(core_system_parameters target_link_libraries(core_system_parameters
PRIVATE PUBLIC
${GNUTLS_LIBRARIES} ${GNUTLS_LIBRARIES}
${GNUTLS_OPENSSL_LIBRARY} ${GNUTLS_OPENSSL_LIBRARY}
) )
target_include_directories(core_system_parameters target_include_directories(core_system_parameters
PRIVATE PUBLIC
${GNUTLS_INCLUDE_DIR} ${GNUTLS_INCLUDE_DIR}
) )
endif() endif()
if(OPENSSL_FOUND) if(OPENSSL_FOUND)
target_compile_definitions(core_system_parameters PRIVATE -DUSE_OPENSSL_FALLBACK=1) target_compile_definitions(core_system_parameters PUBLIC -DUSE_OPENSSL_FALLBACK=1)
endif() endif()
if(ENABLE_CLANG_TIDY) if(ENABLE_CLANG_TIDY)

View File

@ -25,6 +25,7 @@
#if USE_OPENSSL_FALLBACK #if USE_OPENSSL_FALLBACK
#include <openssl/cmac.h> #include <openssl/cmac.h>
#include <openssl/ecdsa.h>
#include <openssl/hmac.h> #include <openssl/hmac.h>
#include <openssl/pem.h> #include <openssl/pem.h>
#if USE_OPENSSL_3 #if USE_OPENSSL_3
@ -34,27 +35,49 @@
#include <openssl/sha.h> #include <openssl/sha.h>
#endif #endif
#else #else
#include <gnutls/abstract.h>
#include <gnutls/crypto.h> #include <gnutls/crypto.h>
#include <gnutls/gnutls.h>
#include <gnutls/x509.h> #include <gnutls/x509.h>
#endif #endif
Gnss_Crypto::Gnss_Crypto(const std::string& filePath) Gnss_Crypto::Gnss_Crypto(const std::string& filePath)
{ {
#if USE_OPENSSL_FALLBACK
#else
gnutls_global_init();
#endif
readPublicKeyFromPEM(filePath); readPublicKeyFromPEM(filePath);
} }
Gnss_Crypto::~Gnss_Crypto()
{
#if USE_OPENSSL_FALLBACK
if (d_PublicKey != nullptr)
{
EC_KEY_free(d_PublicKey);
}
#else
if (d_PublicKey != nullptr)
{
gnutls_pubkey_deinit(*d_PublicKey);
}
gnutls_global_deinit();
#endif
}
bool Gnss_Crypto::have_public_key() const bool Gnss_Crypto::have_public_key() const
{ {
return !d_PublicKey.empty(); return (d_PublicKey != nullptr);
} }
void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publickey) // void Gnss_Crypto::set_public_key(const std::vector<uint8_t>& publickey)
{ // {
d_PublicKey = publickey; // }
}
std::vector<uint8_t> Gnss_Crypto::computeSHA256(const std::vector<uint8_t>& input) const std::vector<uint8_t> Gnss_Crypto::computeSHA256(const std::vector<uint8_t>& input) const
@ -296,22 +319,22 @@ void Gnss_Crypto::readPublicKeyFromPEM(const std::string& filePath)
} }
// Get the EC key from the EVP_PKEY object // Get the EC key from the EVP_PKEY object
EC_KEY* ecKey = EVP_PKEY_get1_EC_KEY(evpKey); d_PublicKey = EVP_PKEY_get1_EC_KEY(evpKey);
EVP_PKEY_free(evpKey); EVP_PKEY_free(evpKey);
if (ecKey == nullptr) if (d_PublicKey == nullptr)
{ {
std::cout << "OpenSSL: Failed to get the EC key from file " << filePath << ". Aborting import" << std::endl; std::cout << "OpenSSL: Failed to get the EC key from file " << filePath << ". Aborting import" << std::endl;
return; return;
} }
// Get the EC group from the EC key // Get the EC group from the EC key
const EC_GROUP* ecGroup = EC_KEY_get0_group(ecKey); const EC_GROUP* ecGroup = EC_KEY_get0_group(d_PublicKey);
if (ecGroup == nullptr) if (ecGroup == nullptr)
{ {
std::cout << "OpenSSL: Failed to extract the EC group from file " << filePath << ". Aborting import" << std::endl; std::cout << "OpenSSL: Failed to extract the EC group from file " << filePath << ". Aborting import" << std::endl;
EC_KEY_free(ecKey); EC_KEY_free(d_PublicKey);
return; return;
} }
@ -319,131 +342,68 @@ void Gnss_Crypto::readPublicKeyFromPEM(const std::string& filePath)
if (EC_GROUP_get_curve_name(ecGroup) != NID_X9_62_prime256v1) if (EC_GROUP_get_curve_name(ecGroup) != NID_X9_62_prime256v1)
{ {
std::cerr << "Invalid curve name in file " << filePath << ". Expected P-256. Aborting import" << std::endl; std::cerr << "Invalid curve name in file " << filePath << ". Expected P-256. Aborting import" << std::endl;
EC_KEY_free(ecKey); EC_KEY_free(d_PublicKey);
return; return;
} }
// Convert the EC parameters to an octet string (raw binary)
// size_t octetSize = i2o_ECPublicKey(ecKey, nullptr);
// std::vector<uint8_t> ecParameters(octetSize);
// unsigned char* p = ecParameters.data();
// i2o_ECPublicKey(ecKey, &p);
std::vector<uint8_t> ecParameters(EC_GROUP_get_degree(ecGroup) / 8);
EC_POINT_point2oct(ecGroup, EC_KEY_get0_public_key(ecKey), POINT_CONVERSION_UNCOMPRESSED, ecParameters.data(), ecParameters.size(), nullptr);
// Get the EC public key from the EC key
const EC_POINT* ecPoint = EC_KEY_get0_public_key(ecKey);
// Convert the EC public key to an octet string (raw binary)
size_t pointSize = EC_POINT_point2oct(ecGroup, ecPoint, POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr);
publicKey = std::vector<uint8_t>(pointSize);
EC_POINT_point2oct(ecGroup, ecPoint, POINT_CONVERSION_UNCOMPRESSED, publicKey.data(), pointSize, nullptr);
size_t octetSize = i2o_ECPublicKey(ecKey, nullptr);
std::vector<uint8_t> publicKey2(octetSize);
unsigned char* p2 = publicKey2.data();
i2o_ECPublicKey(ecKey, &p2);
// Clean up the EC key
EC_KEY_free(ecKey);
std::cout << "EC parameters (size: " << ecParameters.size() << "):";
for (auto k : ecParameters)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << std::endl;
std::cout << "Public Key (size: " << publicKey.size() << "):";
for (auto k : publicKey)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << "Public Key2: (size: " << publicKey2.size() << "):";
for (auto k : publicKey2)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << std::endl;
#else #else
// Load the public key from the BIO // Load the public key from the BIO
EC_KEY* ecKeyPublic = PEM_read_bio_EC_PUBKEY(bio, nullptr, nullptr, nullptr); d_PublicKey = PEM_read_bio_EC_PUBKEY(bio, nullptr, nullptr, nullptr);
BIO_free(bio); BIO_free(bio);
if (ecKeyPublic == nullptr) if (d_PublicKey == nullptr)
{ {
std::cerr << "OpenSSL: error reading the Public Key from file " << filePath << ". Aborting import" << std::endl; std::cerr << "OpenSSL: error reading the Public Key from file " << filePath << ". Aborting import" << std::endl;
return; return;
} }
// // Get the EC group and EC point from the EC key
const EC_GROUP* ecGroup = EC_KEY_get0_group(ecKeyPublic);
const EC_POINT* ecPoint = EC_KEY_get0_public_key(ecKeyPublic);
// Convert the EC point to an octet string (raw binary)
const size_t octetSize = EC_POINT_point2oct(
ecGroup, ecPoint, POINT_CONVERSION_UNCOMPRESSED, nullptr, 0, nullptr);
publicKey = std::vector<uint8_t>(octetSize);
EC_POINT_point2oct(
ecGroup, ecPoint, POINT_CONVERSION_UNCOMPRESSED, publicKey.data(), octetSize, nullptr);
EC_KEY_free(ecKeyPublic);
std::cout << "Public Key:";
for (auto k : publicKey)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << std::endl;
#endif #endif
#else #else
// Find the beginning and end of the EC PARAMETERS section gnutls_pubkey_t pubKey;
std::size_t beginPos = pemContent.find("-----BEGIN EC PARAMETERS-----"); gnutls_pubkey_init(&pubKey);
std::size_t endPos = pemContent.find("-----END EC PARAMETERS-----"); d_PublicKey = &pubKey;
if (beginPos == std::string::npos || endPos == std::string::npos) // Import the PEM data
gnutls_datum_t pemDatum = {const_cast<unsigned char*>(reinterpret_cast<unsigned char*>(pemContent.data())), static_cast<unsigned int>(pemContent.size())};
int ret = gnutls_pubkey_import(*d_PublicKey, &pemDatum, GNUTLS_X509_FMT_PEM);
if (ret < 0)
{ {
std::cerr << "No EC Parameters found in file " << filePath << ". Aborting import" << std::endl; std::cerr << "GnuTLS: error reading the Public Key from file "
<< filePath
<< ". (Error: " << gnutls_strerror(ret) << "). Aborting import" << std::endl;
gnutls_pubkey_deinit(*d_PublicKey);
return; return;
} }
// Extract the EC parameters data
std::string ecParamsBase64 = pemContent.substr(beginPos + 30, endPos - beginPos - 31);
std::vector<uint8_t> ecParameters = base64Decode(ecParamsBase64);
std::cout << ecParamsBase64 << std::endl;
std::cout << "Size ecParamsBase64 : " << ecParamsBase64.size() << std::endl;
std::cout << "Size EC : " << ecParameters.size() << std::endl;
for (auto k : ecParameters)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << std::endl;
std::size_t beginPos2 = pemContent.find("-----BEGIN PUBLIC KEY-----");
std::size_t endPos2 = pemContent.find("-----END PUBLIC KEY-----");
if (beginPos2 == std::string::npos || endPos2 == std::string::npos)
{
std::cout << "No Public Key found in file " << filePath << ". Aborting import" << std::endl;
return;
}
auto PublickeyBase64 = pemContent.substr(beginPos2 + 27, endPos2 - beginPos2 - 28);
auto readpublickey_long = base64Decode(PublickeyBase64);
publicKey = std::vector<uint8_t>(readpublickey_long.begin() + 26, readpublickey_long.end()); // ??
std::cout << "Public Key (size: " << publicKey.size() << "):" << std::endl;
for (auto k : publicKey)
{
std::cout << " " << static_cast<uint32_t>(k);
}
std::cout << std::endl;
#endif #endif
d_PublicKey = publicKey;
std::cout << "Public key successfully read from file " << filePath << std::endl; std::cout << "Public key successfully read from file " << filePath << std::endl;
} }
// bool signature(const std::vector<uint8_t>& publicKey, const std::vector<uint8_t>& digest, const std::vector<uint8_t>& signature) // bool verify_signature(const std::vector<uint8_t>& message, const std::vector<uint8_t>& signature)
// { // {
// bool success = false; // bool success = false;
// #if USE_OPENSSL_FALLBACK // #if USE_OPENSSL_FALLBACK
/** Verifies that the given signature is valid ECDSA signature
* of the supplied hash value using the specified public key.
* \param type this parameter is ignored
* \param dgst pointer to the hash value
* \param dgstlen length of the hash value
* \param sig pointer to the DER encoded signature
* \param siglen length of the DER encoded signature
* \param eckey EC_KEY object containing a public EC key
* \return 1 if the signature is valid, 0 if the signature is invalid
* and -1 on error
*/
// int ECDSA_verify(int type, const unsigned char *dgst, int dgstlen, const unsigned char *sig, int siglen, EC_KEY *eckey);)
// int verification = ECDSA_verify(0, digest, SHA256_DIGEST_LENGTH, signature, signature_len, key_pair_obj);
// #else // #else
// gnutls_global_init(); // gnutls_global_init();
// int result = gnutls_pubkey_verify_data(publicKey.data(), GNUTLS_SIGN_ECDSA_SHA256, digest.data(), digest.size(), signature.data(), signature.size()); // int result = gnutls_pubkey_verify_data(publicKey.data(), GNUTLS_SIGN_ECDSA_SHA256, digest.data(), digest.size(), signature.data(), signature.size());
// success = (result == GNUTLS_E_SUCCESS); // success = (result == GNUTLS_E_SUCCESS);
// gnutls_global_deinit(); // gnutls_global_deinit();
// pubkey: Holds the public key
// algo: The signature algorithm used
// flags: Zero or an OR list of gnutls_certificate_verify_flags
// data: holds the signed data
// signature: contains the signature
// This function will verify the given signed data, using the parameters from the certificate.
// gnutls_pubkey_verify_data2 (gnutls_pubkey_t pubkey, gnutls_sign_algorithm_t algo, unsigned int flags, const gnutls_datum_t * data, const gnutls_datum_t * signature)
// #endif // #endif
// return success; // return success;
// } // }
@ -451,67 +411,3 @@ void Gnss_Crypto::readPublicKeyFromPEM(const std::string& filePath)
// // { // // {
// // int verificationStatus = gnutls_pubkey_verify_data(publicKey, GNUTLS_DIG_SHA256, 0, message, messageSize, signature, signatureSize); // // int verificationStatus = gnutls_pubkey_verify_data(publicKey, GNUTLS_DIG_SHA256, 0, message, messageSize, signature, signatureSize);
// // return verificationStatus == 0; // // return verificationStatus == 0;
std::vector<uint8_t> Gnss_Crypto::base64Decode(const std::string& encoded_string)
{
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
int in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
uint8_t char_array_4[4];
uint8_t char_array_3[3];
std::vector<uint8_t> decoded;
while (in_len-- && (encoded_string[in_] != '=') &&
(isalnum(encoded_string[in_]) || (encoded_string[in_] == '+') || (encoded_string[in_] == '/')))
{
char_array_4[i++] = encoded_string[in_];
in_++;
if (i == 4)
{
for (i = 0; i < 4; i++)
{
char_array_4[i] = base64_chars.find(char_array_4[i]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
{
decoded.push_back(char_array_3[i]);
}
i = 0;
}
}
if (i)
{
for (j = i; j < 4; j++)
{
char_array_4[j] = 0;
}
for (j = 0; j < 4; j++)
{
char_array_4[j] = base64_chars.find(char_array_4[j]);
}
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
{
decoded.push_back(char_array_3[j]);
}
}
return decoded;
}

View File

@ -21,30 +21,38 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
#if USE_OPENSSL_FALLBACK
#include <openssl/ec.h>
#else
#include <gnutls/gnutls.h>
#endif
/** \addtogroup Core /** \addtogroup Core
* \{ */ * \{ */
/** \addtogroup System_Parameters /** \addtogroup System_Parameters
* \{ */ * \{ */
class Gnss_Crypto class Gnss_Crypto
{ {
public: public:
Gnss_Crypto() = default; Gnss_Crypto() = default;
~Gnss_Crypto();
explicit Gnss_Crypto(const std::string& filePath); explicit Gnss_Crypto(const std::string& filePath);
bool have_public_key() const; bool have_public_key() const;
std::vector<uint8_t> computeSHA256(const std::vector<uint8_t>& input) const; std::vector<uint8_t> computeSHA256(const std::vector<uint8_t>& input) const;
std::vector<uint8_t> computeSHA3_256(const std::vector<uint8_t>& input) const; std::vector<uint8_t> computeSHA3_256(const std::vector<uint8_t>& input) const;
std::vector<uint8_t> computeHMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; std::vector<uint8_t> computeHMAC_SHA_256(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const;
std::vector<uint8_t> computeCMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const; std::vector<uint8_t> computeCMAC_AES(const std::vector<uint8_t>& key, const std::vector<uint8_t>& input) const;
void set_public_key(const std::vector<uint8_t>& publickey);
void readPublicKeyFromPEM(const std::string& filePath); void readPublicKeyFromPEM(const std::string& filePath);
// void set_public_key(const std::vector<uint8_t>& publickey);
private: private:
std::vector<uint8_t> base64Decode(const std::string& encoded_string); #if USE_OPENSSL_FALLBACK
std::vector<uint8_t> d_PublicKey; EC_KEY* d_PublicKey = nullptr;
#else
gnutls_pubkey_t* d_PublicKey;
#endif
}; };
/** \} */ /** \} */