Loading ccsrc/Protocols/GeoNetworking/geonetworking_codec.cc +21 −7 Original line number Diff line number Diff line Loading @@ -161,7 +161,11 @@ int geonetworking_codec::decode (const OCTETSTRING& data, LibItsGeoNetworking__T (*_params)[params::gn_header_sub_type] = std::string(static_cast<const char *>(oct2str(OCTETSTRING(1, &_dc.header_sub_type)))); (*_params)[params::gn_traffic_class] = std::string(static_cast<const char *>(oct2str(OCTETSTRING(1, &_dc.traffic_class)))); (*_params)[params::gn_lifetime] = std::to_string(_dc.lifetime); if (_dc.payload != nullptr) { (*_params)[params::gn_payload] = _dc.payload; delete [] _dc.payload; _dc.payload = nullptr; } params->log(); } Loading Loading @@ -242,18 +246,28 @@ int geonetworking_codec::decode_ (Base_Type& type, const TTCN_Typedescriptor_t& if (_dc.length != 0) { if ((unsigned char)s.lengthof() <= _dc.length) { os = OCTETSTRING(s.lengthof(), p); _dc.payload = std::string(static_cast<const char *>(oct2str(os))); //_dc.payload.assign(p, p + s.lengthof()); CHARSTRING c(oct2str(os)); _dc.payload = new char[c.lengthof() + 1]; strcpy(_dc.payload, static_cast<const char*>(c)); //_dc.payload = std::string(static_cast<const char *>(oct2str(os))); } else { os = OCTETSTRING(_dc.length, p); _dc.payload = std::string(static_cast<const char *>(oct2str(os))); //_dc.payload.assign(p, p + _dc.length); CHARSTRING c(oct2str(os)); _dc.payload = new char[c.lengthof() + 1]; strcpy(_dc.payload, static_cast<const char*>(c)); //_dc.payload = std::string(static_cast<const char *>(oct2str(os))); } } else { os = s; _dc.payload.clear(); if (_dc.payload != nullptr) { delete [] _dc.payload; _dc.payload = nullptr; } //_dc.payload.clear(); } loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", _dc.payload.c_str()); loggers::get_instance().log_msg("geonetworking_codec::decode_: Payload: os: ", os); // loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", _dc.payload.c_str()); loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", (_dc.payload != nullptr) ? _dc.payload : "(null)"); if (os.lengthof() != 0) { dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &>(type) = OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload>(os); loggers::get_instance().log_msg("geonetworking_codec::decode_: Set OPTIONAL<GnRawPayload> to ", type); Loading ccsrc/Protocols/GeoNetworking/geonetworking_codec.hh +6 −3 Original line number Diff line number Diff line #pragma once #include <memory> #include "codec.hh" #include "params.hh" Loading Loading @@ -38,10 +40,11 @@ struct decoding_context { unsigned char traffic_class; unsigned int lifetime; unsigned int length; std::string payload; char* payload; // FIXME Use smart pointer instead of char* decoding_context() : payload() { reset(); } void reset() { next_header = 0xff; header_type = 0xff; header_sub_type = 0xff; lifetime = 0; length = -1; payload.clear(); } decoding_context() : payload(nullptr) { reset(); } ~decoding_context() { if (payload == nullptr) delete [] payload; } void reset() { next_header = 0xff; header_type = 0xff; header_sub_type = 0xff; lifetime = 0; length = -1; if (payload != nullptr) { delete [] payload; payload = nullptr; }; } }; //typedef struct decoding_context decoding_context; Loading ccsrc/Protocols/Security/certificates_loader.cc +179 −32 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ certificates_loader * certificates_loader::instance = nullptr; certificates_loader::certificates_loader(): _certificateExt{".oer"}, _privateKeyExt{".vkey"}, _publicKeysExt{".vkey_pub"}, _publicCompKeysExt(".cvkey"), _privateEncKeyExt{".ekey"}, _publicEncKeysExt{".ekey_pub"}, _hashedidDigestExt{".hashedid"}, _issuerDigestExt{".issuer"}, _full_path(), _is_cache_initialized{false}, _directory_filter{".svn", "._.DS_Store", ".DS_Store"} { certificates_loader::certificates_loader(): _certificateExt{".oer"}, _privateKeyExt{".vkey"}, _privateEncKeyExt{".ekey"}, _publicKeysExt{".vkey_pub"}, _publicCompKeysExt(".cvkey"), _publicEncKeysExt{".ekey_pub"}, _hashedidDigestExt{".hashedid"}, _issuerDigestExt{".issuer"}, _full_path(), _is_cache_initialized{false}, _directory_filter{".svn", "._.DS_Store", ".DS_Store"} { loggers::get_instance().log(">>> certificates_loader::certificates_loader"); } // End of ctor Loading Loading @@ -94,7 +94,7 @@ int certificates_loader::retrieve_certificates_list(std::set<std::experimental:: } // Process files p_files.clear(); std::set<std::string> extensions_filter{ _certificateExt, _privateKeyExt, _publicKeysExt, _publicCompKeysExt, _privateEncKeyExt, _publicEncKeysExt, _hashedidDigestExt, _issuerDigestExt }; std::set<std::string> extensions_filter{ _certificateExt, _privateKeyExt, _privateEncKeyExt, _publicKeysExt, _publicCompKeysExt, _publicEncKeysExt, _hashedidDigestExt, _issuerDigestExt }; for (std::set<std::experimental::filesystem::path>::const_reverse_iterator f = folders.crbegin(); f != folders.crend(); ++f) { //loggers::get_instance().log("certificates_loader::retrieve_certificates_list: Processing directory '%s'", f->string().c_str()); for(const std::experimental::filesystem::directory_entry it : std::experimental::filesystem::recursive_directory_iterator(*f)) { Loading Loading @@ -123,10 +123,10 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi std::set<std::experimental::filesystem::path>::const_iterator it = p_files.cbegin(); do { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching '%s'", it->string().c_str()); std::experimental::filesystem::path p = *it; const std::string& key = p.stem(); //loggers::get_instance().log("certificates_loader::build_certificates_cache: Key = '%s'", key.c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Key = '%s'", key.c_str()); // Load certificate file it = p_files.find(p.replace_extension(_certificateExt)); Loading @@ -134,7 +134,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Certificate file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching certificate '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching certificate '%s'", it->string().c_str()); std::ifstream is(it->string(), ios::in | ios::binary); std::vector<unsigned char> certificate(std::experimental::filesystem::file_size(*it), 0x00); is.read(reinterpret_cast<char *>(certificate.data()), certificate.size()); Loading @@ -148,7 +148,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if ((size != 32) && (size != 48)) { Loading @@ -161,13 +161,131 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi // Remove items from the list p_files.erase(it); // Load private encryption key file if present std::vector<unsigned char> private_enc_key; it = p_files.find(p.replace_extension(_privateEncKeyExt)); if (it != p_files.cend()) { loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if (size != 32) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private encryption key size is incorrect for '%s'", key.c_str()); return -1; } private_enc_key.resize(size, 0x00); is.read(reinterpret_cast<char *>(private_enc_key.data()), private_enc_key.size()); is.close(); // Remove items from the list p_files.erase(it); } // Load public keys file it = p_files.find(p.replace_extension(_publicKeysExt)); if (it == p_files.cend()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Public keys file not found for '%s'", key.c_str()); // Try with only binary format of the certificate and the private key etsi_ts103097_certificate_codec codec; IEEE1609dot2::CertificateBase decoded_certificate; OCTETSTRING os(certificate.size(), certificate.data()); codec.decode(os, decoded_certificate); if (!decoded_certificate.is_value()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Failed to decode certificate for '%s'", key.c_str()); return -1; } else { loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); // Prepare all fields if (!decoded_certificate.toBeSigned().verifyKeyIndicator().ischosen(IEEE1609dot2::VerificationKeyIndicator::ALT_verificationKey)) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Wrong VerificationKeyIndicator variant for '%s'", key.c_str()); return -1; // Error } std::vector<unsigned char> public_key_x; std::vector<unsigned char> public_key_y; std::vector<unsigned char> public_comp_key; // public compressed key, 33 or 49 bytes length, byte #0 indicating compressed-y-0 (0x02) or compressed-y-1 (0x03) IEEE1609dot2BaseTypes::PublicVerificationKey& b = decoded_certificate.toBeSigned().verifyKeyIndicator().verificationKey(); if (b.ischosen(IEEE1609dot2BaseTypes::PublicVerificationKey::ALT_ecdsaNistP256)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = b.ecdsaNistP256(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } else if (b.ischosen(IEEE1609dot2BaseTypes::PublicVerificationKey::ALT_ecdsaBrainpoolP256r1)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = b.ecdsaBrainpoolP256r1(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } else { // ALT_ecdsaBrainpoolP384r1 IEEE1609dot2BaseTypes::EccP384CurvePoint& p = b.ecdsaBrainpoolP384r1(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } std::vector<unsigned char> public_enc_key_x; std::vector<unsigned char> public_enc_key_y; std::vector<unsigned char> public_enc_comp_key; if (decoded_certificate.toBeSigned().encryptionKey().ispresent()) { IEEE1609dot2BaseTypes::PublicEncryptionKey& v = static_cast<IEEE1609dot2BaseTypes::PublicEncryptionKey&>(decoded_certificate.toBeSigned().encryptionKey()); IEEE1609dot2BaseTypes::BasePublicEncryptionKey& b = v.publicKey(); if (b.ischosen(IEEE1609dot2BaseTypes::BasePublicEncryptionKey::ALT_eciesNistP256)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = v.publicKey().eciesNistP256(); fill_public_key_vectors(p, public_enc_comp_key, public_enc_key_x, public_enc_key_y); } else if (b.ischosen(IEEE1609dot2BaseTypes::BasePublicEncryptionKey::ALT_eciesBrainpoolP256r1)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = v.publicKey().eciesBrainpoolP256r1(); fill_public_key_vectors(p, public_enc_comp_key, public_enc_key_x, public_enc_key_y); } else { public_enc_key_x.resize(32); public_enc_key_y.resize(32); public_enc_comp_key.resize(32); } } else { public_enc_key_x.resize(32); public_enc_key_y.resize(32); public_enc_comp_key.resize(32); } std::vector<unsigned char> issuer; if (decoded_certificate.issuer().ischosen(IEEE1609dot2::IssuerIdentifier::ALT_sha256AndDigest)) { issuer.assign( static_cast<const unsigned char*>(decoded_certificate.issuer().sha256AndDigest()), decoded_certificate.issuer().sha256AndDigest().lengthof() + static_cast<const unsigned char*>(decoded_certificate.issuer().sha256AndDigest()) ); } else if (decoded_certificate.issuer().ischosen(IEEE1609dot2::IssuerIdentifier::ALT_sha384AndDigest)) { issuer.assign( static_cast<const unsigned char*>(decoded_certificate.issuer().sha384AndDigest()), decoded_certificate.issuer().sha384AndDigest().lengthof() + static_cast<const unsigned char*>(decoded_certificate.issuer().sha384AndDigest()) ); } else { issuer.resize(8); } std::vector<unsigned char> hashed_id(32, 0x00); // Create new record p_certificates.insert(std::pair<const std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>( new security_db_record( key, certificate, // Certificate decoded_certificate, issuer, // Hashed ID fo the issuer hashed_id, // Hashed ID private_key, // Private key public_key_x, // public keys X-coordinate public_key_y, // public keys Y-coordinate public_comp_key, // public compressed key, 33 or 49 bytes length, byte #0 indicating compressed-y-0 (0x02) or compressed-y-1 (0x03) private_enc_key, // Private enciption key public_enc_key_x, // Public enciption key X-coordinate public_enc_key_y // Public enciption key Y-coordinate )) )); std::map<std::string, std::unique_ptr<security_db_record> >::const_iterator i = p_certificates.find(key); if (i == p_certificates.cend()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Failed to insert new record '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public keys '%s'", it->string().c_str()); p_hashed_id8s.insert(std::pair<std::vector<unsigned char>, std::string>(i->second.get()->hashed_id(), i->first)); // Reset pointer p_files.erase(it); it = p_files.cbegin(); // Continue with next certificte in the list continue; } } // else, continue with detailed files loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if ((size != 64) && (size != 96)) { Loading @@ -188,7 +306,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Public compress key file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public compressed ke '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public compressed ke '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if ((size != 33) && (size != 49)) { Loading @@ -201,30 +319,12 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi // Remove items from the list p_files.erase(it); // Load private encryption key file if present std::vector<unsigned char> private_enc_key; it = p_files.find(p.replace_extension(_privateEncKeyExt)); if (it != p_files.cend()) { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if (size != 32) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private encryption key size is incorrect for '%s'", key.c_str()); return -1; } private_enc_key.resize(size, 0x00); is.read(reinterpret_cast<char *>(private_enc_key.data()), private_enc_key.size()); is.close(); // Remove items from the list p_files.erase(it); } // Load public encryption key file std::vector<unsigned char> public_enc_key_x; std::vector<unsigned char> public_enc_key_y; it = p_files.find(p.replace_extension(_publicEncKeysExt)); if (it != p_files.cend()) { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public encryption keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 64) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 Loading @@ -246,7 +346,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Issuer keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching issuer keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching issuer keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 8) { Loading @@ -265,7 +365,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Hashed_Id keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching hashed_id keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching hashed_id keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 8) { Loading @@ -283,7 +383,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi IEEE1609dot2::CertificateBase decoded_certificate; OCTETSTRING os(certificate.size(), certificate.data()); codec.decode(os, decoded_certificate); //loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); p_certificates.insert(std::pair<const std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>( new security_db_record( key, Loading Loading @@ -427,3 +527,50 @@ int certificates_loader::save_certificate(const security_db_record& p_certificat return 0; } // End of method save_certificate void certificates_loader::fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP256CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y) { if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_compressed__y__0)) { p_public_comp_key.resize(33); p_public_comp_key[0] = 0x02; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_ecc_point.compressed__y__0().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_public_comp_key.begin() + 1); p_public_key_x.resize(32); p_public_key_y.resize(32); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_compressed__y__1)) { p_public_comp_key.resize(33); p_public_comp_key[0] = 0x03; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_ecc_point.compressed__y__1().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_public_comp_key.begin() + 1); p_public_key_x.resize(32); p_public_key_y.resize(32); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_uncompressedP256)) { p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().x()), p_ecc_point.uncompressedP256().x().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().x())); p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().y()), p_ecc_point.uncompressedP256().y().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().y())); p_public_comp_key.assign(33, 0x00); } else { p_public_key_x.resize(32); p_public_key_y.resize(32); p_public_comp_key.resize(33); } } void certificates_loader::fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP384CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y) { if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_compressed__y__0)) { p_public_comp_key.resize(49); p_public_comp_key[0] = 0x02; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_ecc_point.compressed__y__0().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_public_comp_key.begin() + 1); p_public_key_x.resize(48); p_public_key_y.resize(48); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_compressed__y__1)) { p_public_comp_key.resize(49); p_public_comp_key[0] = 0x03; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_ecc_point.compressed__y__1().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_public_comp_key.begin() + 1); p_public_key_x.resize(48); p_public_key_y.resize(48); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_uncompressedP384)) { p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().x()), p_ecc_point.uncompressedP384().x().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().x())); p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().y()), p_ecc_point.uncompressedP384().y().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().y())); p_public_comp_key.assign(49, 0x00); } else { p_public_key_x.resize(48); p_public_key_y.resize(48); p_public_comp_key.resize(49); } } ccsrc/Protocols/Security/certificates_loader.hh +5 −2 File changed.Preview size limit exceeded, changes collapsed. Show changes Loading
ccsrc/Protocols/GeoNetworking/geonetworking_codec.cc +21 −7 Original line number Diff line number Diff line Loading @@ -161,7 +161,11 @@ int geonetworking_codec::decode (const OCTETSTRING& data, LibItsGeoNetworking__T (*_params)[params::gn_header_sub_type] = std::string(static_cast<const char *>(oct2str(OCTETSTRING(1, &_dc.header_sub_type)))); (*_params)[params::gn_traffic_class] = std::string(static_cast<const char *>(oct2str(OCTETSTRING(1, &_dc.traffic_class)))); (*_params)[params::gn_lifetime] = std::to_string(_dc.lifetime); if (_dc.payload != nullptr) { (*_params)[params::gn_payload] = _dc.payload; delete [] _dc.payload; _dc.payload = nullptr; } params->log(); } Loading Loading @@ -242,18 +246,28 @@ int geonetworking_codec::decode_ (Base_Type& type, const TTCN_Typedescriptor_t& if (_dc.length != 0) { if ((unsigned char)s.lengthof() <= _dc.length) { os = OCTETSTRING(s.lengthof(), p); _dc.payload = std::string(static_cast<const char *>(oct2str(os))); //_dc.payload.assign(p, p + s.lengthof()); CHARSTRING c(oct2str(os)); _dc.payload = new char[c.lengthof() + 1]; strcpy(_dc.payload, static_cast<const char*>(c)); //_dc.payload = std::string(static_cast<const char *>(oct2str(os))); } else { os = OCTETSTRING(_dc.length, p); _dc.payload = std::string(static_cast<const char *>(oct2str(os))); //_dc.payload.assign(p, p + _dc.length); CHARSTRING c(oct2str(os)); _dc.payload = new char[c.lengthof() + 1]; strcpy(_dc.payload, static_cast<const char*>(c)); //_dc.payload = std::string(static_cast<const char *>(oct2str(os))); } } else { os = s; _dc.payload.clear(); if (_dc.payload != nullptr) { delete [] _dc.payload; _dc.payload = nullptr; } //_dc.payload.clear(); } loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", _dc.payload.c_str()); loggers::get_instance().log_msg("geonetworking_codec::decode_: Payload: os: ", os); // loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", _dc.payload.c_str()); loggers::get_instance().log("geonetworking_codec::decode_: Payload: '%s'", (_dc.payload != nullptr) ? _dc.payload : "(null)"); if (os.lengthof() != 0) { dynamic_cast<OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload> &>(type) = OPTIONAL<LibItsGeoNetworking__TypesAndValues::GnRawPayload>(os); loggers::get_instance().log_msg("geonetworking_codec::decode_: Set OPTIONAL<GnRawPayload> to ", type); Loading
ccsrc/Protocols/GeoNetworking/geonetworking_codec.hh +6 −3 Original line number Diff line number Diff line #pragma once #include <memory> #include "codec.hh" #include "params.hh" Loading Loading @@ -38,10 +40,11 @@ struct decoding_context { unsigned char traffic_class; unsigned int lifetime; unsigned int length; std::string payload; char* payload; // FIXME Use smart pointer instead of char* decoding_context() : payload() { reset(); } void reset() { next_header = 0xff; header_type = 0xff; header_sub_type = 0xff; lifetime = 0; length = -1; payload.clear(); } decoding_context() : payload(nullptr) { reset(); } ~decoding_context() { if (payload == nullptr) delete [] payload; } void reset() { next_header = 0xff; header_type = 0xff; header_sub_type = 0xff; lifetime = 0; length = -1; if (payload != nullptr) { delete [] payload; payload = nullptr; }; } }; //typedef struct decoding_context decoding_context; Loading
ccsrc/Protocols/Security/certificates_loader.cc +179 −32 Original line number Diff line number Diff line Loading @@ -13,7 +13,7 @@ certificates_loader * certificates_loader::instance = nullptr; certificates_loader::certificates_loader(): _certificateExt{".oer"}, _privateKeyExt{".vkey"}, _publicKeysExt{".vkey_pub"}, _publicCompKeysExt(".cvkey"), _privateEncKeyExt{".ekey"}, _publicEncKeysExt{".ekey_pub"}, _hashedidDigestExt{".hashedid"}, _issuerDigestExt{".issuer"}, _full_path(), _is_cache_initialized{false}, _directory_filter{".svn", "._.DS_Store", ".DS_Store"} { certificates_loader::certificates_loader(): _certificateExt{".oer"}, _privateKeyExt{".vkey"}, _privateEncKeyExt{".ekey"}, _publicKeysExt{".vkey_pub"}, _publicCompKeysExt(".cvkey"), _publicEncKeysExt{".ekey_pub"}, _hashedidDigestExt{".hashedid"}, _issuerDigestExt{".issuer"}, _full_path(), _is_cache_initialized{false}, _directory_filter{".svn", "._.DS_Store", ".DS_Store"} { loggers::get_instance().log(">>> certificates_loader::certificates_loader"); } // End of ctor Loading Loading @@ -94,7 +94,7 @@ int certificates_loader::retrieve_certificates_list(std::set<std::experimental:: } // Process files p_files.clear(); std::set<std::string> extensions_filter{ _certificateExt, _privateKeyExt, _publicKeysExt, _publicCompKeysExt, _privateEncKeyExt, _publicEncKeysExt, _hashedidDigestExt, _issuerDigestExt }; std::set<std::string> extensions_filter{ _certificateExt, _privateKeyExt, _privateEncKeyExt, _publicKeysExt, _publicCompKeysExt, _publicEncKeysExt, _hashedidDigestExt, _issuerDigestExt }; for (std::set<std::experimental::filesystem::path>::const_reverse_iterator f = folders.crbegin(); f != folders.crend(); ++f) { //loggers::get_instance().log("certificates_loader::retrieve_certificates_list: Processing directory '%s'", f->string().c_str()); for(const std::experimental::filesystem::directory_entry it : std::experimental::filesystem::recursive_directory_iterator(*f)) { Loading Loading @@ -123,10 +123,10 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi std::set<std::experimental::filesystem::path>::const_iterator it = p_files.cbegin(); do { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching '%s'", it->string().c_str()); std::experimental::filesystem::path p = *it; const std::string& key = p.stem(); //loggers::get_instance().log("certificates_loader::build_certificates_cache: Key = '%s'", key.c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Key = '%s'", key.c_str()); // Load certificate file it = p_files.find(p.replace_extension(_certificateExt)); Loading @@ -134,7 +134,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Certificate file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching certificate '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching certificate '%s'", it->string().c_str()); std::ifstream is(it->string(), ios::in | ios::binary); std::vector<unsigned char> certificate(std::experimental::filesystem::file_size(*it), 0x00); is.read(reinterpret_cast<char *>(certificate.data()), certificate.size()); Loading @@ -148,7 +148,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if ((size != 32) && (size != 48)) { Loading @@ -161,13 +161,131 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi // Remove items from the list p_files.erase(it); // Load private encryption key file if present std::vector<unsigned char> private_enc_key; it = p_files.find(p.replace_extension(_privateEncKeyExt)); if (it != p_files.cend()) { loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if (size != 32) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private encryption key size is incorrect for '%s'", key.c_str()); return -1; } private_enc_key.resize(size, 0x00); is.read(reinterpret_cast<char *>(private_enc_key.data()), private_enc_key.size()); is.close(); // Remove items from the list p_files.erase(it); } // Load public keys file it = p_files.find(p.replace_extension(_publicKeysExt)); if (it == p_files.cend()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Public keys file not found for '%s'", key.c_str()); // Try with only binary format of the certificate and the private key etsi_ts103097_certificate_codec codec; IEEE1609dot2::CertificateBase decoded_certificate; OCTETSTRING os(certificate.size(), certificate.data()); codec.decode(os, decoded_certificate); if (!decoded_certificate.is_value()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Failed to decode certificate for '%s'", key.c_str()); return -1; } else { loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); // Prepare all fields if (!decoded_certificate.toBeSigned().verifyKeyIndicator().ischosen(IEEE1609dot2::VerificationKeyIndicator::ALT_verificationKey)) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Wrong VerificationKeyIndicator variant for '%s'", key.c_str()); return -1; // Error } std::vector<unsigned char> public_key_x; std::vector<unsigned char> public_key_y; std::vector<unsigned char> public_comp_key; // public compressed key, 33 or 49 bytes length, byte #0 indicating compressed-y-0 (0x02) or compressed-y-1 (0x03) IEEE1609dot2BaseTypes::PublicVerificationKey& b = decoded_certificate.toBeSigned().verifyKeyIndicator().verificationKey(); if (b.ischosen(IEEE1609dot2BaseTypes::PublicVerificationKey::ALT_ecdsaNistP256)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = b.ecdsaNistP256(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } else if (b.ischosen(IEEE1609dot2BaseTypes::PublicVerificationKey::ALT_ecdsaBrainpoolP256r1)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = b.ecdsaBrainpoolP256r1(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } else { // ALT_ecdsaBrainpoolP384r1 IEEE1609dot2BaseTypes::EccP384CurvePoint& p = b.ecdsaBrainpoolP384r1(); fill_public_key_vectors(p, public_comp_key, public_key_x, public_key_y); } std::vector<unsigned char> public_enc_key_x; std::vector<unsigned char> public_enc_key_y; std::vector<unsigned char> public_enc_comp_key; if (decoded_certificate.toBeSigned().encryptionKey().ispresent()) { IEEE1609dot2BaseTypes::PublicEncryptionKey& v = static_cast<IEEE1609dot2BaseTypes::PublicEncryptionKey&>(decoded_certificate.toBeSigned().encryptionKey()); IEEE1609dot2BaseTypes::BasePublicEncryptionKey& b = v.publicKey(); if (b.ischosen(IEEE1609dot2BaseTypes::BasePublicEncryptionKey::ALT_eciesNistP256)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = v.publicKey().eciesNistP256(); fill_public_key_vectors(p, public_enc_comp_key, public_enc_key_x, public_enc_key_y); } else if (b.ischosen(IEEE1609dot2BaseTypes::BasePublicEncryptionKey::ALT_eciesBrainpoolP256r1)) { IEEE1609dot2BaseTypes::EccP256CurvePoint& p = v.publicKey().eciesBrainpoolP256r1(); fill_public_key_vectors(p, public_enc_comp_key, public_enc_key_x, public_enc_key_y); } else { public_enc_key_x.resize(32); public_enc_key_y.resize(32); public_enc_comp_key.resize(32); } } else { public_enc_key_x.resize(32); public_enc_key_y.resize(32); public_enc_comp_key.resize(32); } std::vector<unsigned char> issuer; if (decoded_certificate.issuer().ischosen(IEEE1609dot2::IssuerIdentifier::ALT_sha256AndDigest)) { issuer.assign( static_cast<const unsigned char*>(decoded_certificate.issuer().sha256AndDigest()), decoded_certificate.issuer().sha256AndDigest().lengthof() + static_cast<const unsigned char*>(decoded_certificate.issuer().sha256AndDigest()) ); } else if (decoded_certificate.issuer().ischosen(IEEE1609dot2::IssuerIdentifier::ALT_sha384AndDigest)) { issuer.assign( static_cast<const unsigned char*>(decoded_certificate.issuer().sha384AndDigest()), decoded_certificate.issuer().sha384AndDigest().lengthof() + static_cast<const unsigned char*>(decoded_certificate.issuer().sha384AndDigest()) ); } else { issuer.resize(8); } std::vector<unsigned char> hashed_id(32, 0x00); // Create new record p_certificates.insert(std::pair<const std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>( new security_db_record( key, certificate, // Certificate decoded_certificate, issuer, // Hashed ID fo the issuer hashed_id, // Hashed ID private_key, // Private key public_key_x, // public keys X-coordinate public_key_y, // public keys Y-coordinate public_comp_key, // public compressed key, 33 or 49 bytes length, byte #0 indicating compressed-y-0 (0x02) or compressed-y-1 (0x03) private_enc_key, // Private enciption key public_enc_key_x, // Public enciption key X-coordinate public_enc_key_y // Public enciption key Y-coordinate )) )); std::map<std::string, std::unique_ptr<security_db_record> >::const_iterator i = p_certificates.find(key); if (i == p_certificates.cend()) { loggers::get_instance().warning("certificates_loader::build_certificates_cache: Failed to insert new record '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public keys '%s'", it->string().c_str()); p_hashed_id8s.insert(std::pair<std::vector<unsigned char>, std::string>(i->second.get()->hashed_id(), i->first)); // Reset pointer p_files.erase(it); it = p_files.cbegin(); // Continue with next certificte in the list continue; } } // else, continue with detailed files loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if ((size != 64) && (size != 96)) { Loading @@ -188,7 +306,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Public compress key file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public compressed ke '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public compressed ke '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if ((size != 33) && (size != 49)) { Loading @@ -201,30 +319,12 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi // Remove items from the list p_files.erase(it); // Load private encryption key file if present std::vector<unsigned char> private_enc_key; it = p_files.find(p.replace_extension(_privateEncKeyExt)); if (it != p_files.cend()) { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching private encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); int size = std::experimental::filesystem::file_size(*it); if (size != 32) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 loggers::get_instance().warning("certificates_loader::build_certificates_cache: Private encryption key size is incorrect for '%s'", key.c_str()); return -1; } private_enc_key.resize(size, 0x00); is.read(reinterpret_cast<char *>(private_enc_key.data()), private_enc_key.size()); is.close(); // Remove items from the list p_files.erase(it); } // Load public encryption key file std::vector<unsigned char> public_enc_key_x; std::vector<unsigned char> public_enc_key_y; it = p_files.find(p.replace_extension(_publicEncKeysExt)); if (it != p_files.cend()) { //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public encryption keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching public encryption keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 64) { // IEEE Std 1609.2 2017: NistP256 or BrainpoolP256r1 Loading @@ -246,7 +346,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Issuer keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching issuer keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching issuer keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 8) { Loading @@ -265,7 +365,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi loggers::get_instance().warning("certificates_loader::build_certificates_cache: Hashed_Id keys file not found for '%s'", key.c_str()); return -1; } //loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching hashed_id keys '%s'", it->string().c_str()); loggers::get_instance().log("certificates_loader::build_certificates_cache: Caching hashed_id keys '%s'", it->string().c_str()); is.open(it->string(), ios::in | ios::binary); size = std::experimental::filesystem::file_size(*it); if (size != 8) { Loading @@ -283,7 +383,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi IEEE1609dot2::CertificateBase decoded_certificate; OCTETSTRING os(certificate.size(), certificate.data()); codec.decode(os, decoded_certificate); //loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Decoded certificate: ", decoded_certificate); p_certificates.insert(std::pair<const std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>( new security_db_record( key, Loading Loading @@ -427,3 +527,50 @@ int certificates_loader::save_certificate(const security_db_record& p_certificat return 0; } // End of method save_certificate void certificates_loader::fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP256CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y) { if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_compressed__y__0)) { p_public_comp_key.resize(33); p_public_comp_key[0] = 0x02; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_ecc_point.compressed__y__0().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_public_comp_key.begin() + 1); p_public_key_x.resize(32); p_public_key_y.resize(32); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_compressed__y__1)) { p_public_comp_key.resize(33); p_public_comp_key[0] = 0x03; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_ecc_point.compressed__y__1().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_public_comp_key.begin() + 1); p_public_key_x.resize(32); p_public_key_y.resize(32); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP256CurvePoint::ALT_uncompressedP256)) { p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().x()), p_ecc_point.uncompressedP256().x().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().x())); p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().y()), p_ecc_point.uncompressedP256().y().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP256().y())); p_public_comp_key.assign(33, 0x00); } else { p_public_key_x.resize(32); p_public_key_y.resize(32); p_public_comp_key.resize(33); } } void certificates_loader::fill_public_key_vectors(const IEEE1609dot2BaseTypes::EccP384CurvePoint& p_ecc_point, std::vector<unsigned char>& p_public_comp_key, std::vector<unsigned char>& p_public_key_x, std::vector<unsigned char>& p_public_key_y) { if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_compressed__y__0)) { p_public_comp_key.resize(49); p_public_comp_key[0] = 0x02; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_ecc_point.compressed__y__0().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__0()), p_public_comp_key.begin() + 1); p_public_key_x.resize(48); p_public_key_y.resize(48); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_compressed__y__1)) { p_public_comp_key.resize(49); p_public_comp_key[0] = 0x03; std::copy(static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_ecc_point.compressed__y__1().lengthof() + static_cast<const unsigned char*>(p_ecc_point.compressed__y__1()), p_public_comp_key.begin() + 1); p_public_key_x.resize(48); p_public_key_y.resize(48); } else if (p_ecc_point.ischosen(IEEE1609dot2BaseTypes::EccP384CurvePoint::ALT_uncompressedP384)) { p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().x()), p_ecc_point.uncompressedP384().x().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().x())); p_public_key_x.assign(static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().y()), p_ecc_point.uncompressedP384().y().lengthof() + static_cast<const unsigned char*>(p_ecc_point.uncompressedP384().y())); p_public_comp_key.assign(49, 0x00); } else { p_public_key_x.resize(48); p_public_key_y.resize(48); p_public_comp_key.resize(49); } }
ccsrc/Protocols/Security/certificates_loader.hh +5 −2 File changed.Preview size limit exceeded, changes collapsed. Show changes