Commit d34a7bd8 authored by garciay's avatar garciay Committed by Alexandre Berge
Browse files

Bug fixed on security_cache, need to create a key_comp for OCTETSTRING and CHARSTRING

Note: std::less<unsigned char>
parent 16b042d9
Loading
Loading
Loading
Loading
+13 −6
Changes for ccsrc/Protocols/Security/certificates_loader.cc: 13 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ int certificates_loader::build_path(const std::string& p_root_directory) {
  return 0;
} // End of method build_path

int certificates_loader::load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s) {
int certificates_loader::load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, /*std::map<OCTETSTRING, std::string>*/std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s) {
  loggers::get_instance().log(">>> certificates_loader::load_certificates");

  // Sanity check
@@ -119,7 +119,7 @@ int certificates_loader::retrieve_certificates_list(std::set<std::experimental::
  return 0;
} // End of method retrieve_certificates_list

int certificates_loader::build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s) {
int certificates_loader::build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, /*std::map<OCTETSTRING, std::string>*/std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s) {
  loggers::get_instance().log(">>> certificates_loader::build_certificates_cache");

  std::set<std::experimental::filesystem::path>::const_iterator it = p_files.cbegin();
@@ -174,7 +174,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi
      loggers::get_instance().warning("certificates_loader::build_certificates_cache: Failed to open Private encryption key file");
      private_enc_key = OCTETSTRING(0, nullptr);
    } else {
      size = std::experimental::filesystem::file_size(*it);
      size = std::experimental::filesystem::file_size(p);
      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;
@@ -264,7 +264,7 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi
          loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: Whole-certificate SHA-384 hash: ", hash);
        }
        hashed_id = OCTETSTRING(8, static_cast<const unsigned char*>(hash) + hash.lengthof() - 8);
        issuer = int2oct(0, 8);
        issuer = hashed_id;
      } else {
        hash = OCTETSTRING(0, nullptr);
        hashed_id = int2oct(0, 8);
@@ -297,7 +297,12 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi
        delete p;
        return -1;
      }
      p_hashed_id8s.insert(std::pair<OCTETSTRING, std::string>(result.first->second.get()->hashed_id(), result.first->first));
      loggers::get_instance().log_msg("certificates_loader::build_certificates_cache: add key: ", result.first->second.get()->hashed_id());
      loggers::get_instance().log("certificates_loader::build_certificates_cache: for : %s", result.first->first.c_str());
      //p_hashed_id8s.insert(std::pair<OCTETSTRING, std::string>(result.first->second.get()->hashed_id(), result.first->first));
      std::vector<unsigned char> v(static_cast<const unsigned char*>(result.first->second.get()->hashed_id()), result.first->second.get()->hashed_id().lengthof() + static_cast<const unsigned char*>(result.first->second.get()->hashed_id()));
      loggers::get_instance().log_to_hexa("security_cache::store_certificate: Converted hashed_id8: ", v.data(), v.size());
      p_hashed_id8s.insert(std::pair<std::vector<unsigned char>, std::string>(v, result.first->first));
    }
    // Reset pointer
    it = p_files.cbegin();
@@ -307,7 +312,9 @@ int certificates_loader::build_certificates_cache(std::set<std::experimental::fi
} // End of method build_certificates_cache

int certificates_loader::save_certificate(const security_db_record& p_certificate) {
  loggers::get_instance().log(">>> certificates_loader::save_certificate");
  loggers::get_instance().log(">>> certificates_loader::save_certificate: certificate_id=%s", p_certificate.certificate_id().c_str());
  loggers::get_instance().log_msg("certificates_loader::save_certificate: hash=", p_certificate.hash());
  loggers::get_instance().log_msg("certificates_loader::save_certificate: hashedid8=", p_certificate.hashed_id());

  // Certificate file
  std::experimental::filesystem::path p(_full_path);
+2 −2
Changes for ccsrc/Protocols/Security/certificates_loader.hh: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -66,14 +66,14 @@ public: /*! \publicsection */

  int build_path(const std::string& p_root_directory);

  int load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s);
  int load_certificates(std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, /*std::map<OCTETSTRING, std::string>*/std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s);
  //int load_certificate(std::unique_ptr<security_db_record> >& p_certificate, std::map<const OCTETSTRING, const std::string&>& p_hashed_id8s);
  int save_certificate(const security_db_record& p_certificate);

private:
  int retrieve_certificates_list(std::set<std::experimental::filesystem::path>& p_files);

  int build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, std::map<OCTETSTRING, std::string>& p_hashed_id8s);
  int build_certificates_cache(std::set<std::experimental::filesystem::path>& p_files, std::map<std::string, std::unique_ptr<security_db_record> >& p_certificates, /*std::map<OCTETSTRING, std::string>*/std::map<std::vector<unsigned char>, std::string>& p_hashed_id8s);
  
  void fill_public_key_vectors(const ec_elliptic_curves p_elliptic_curve, const IEEE1609dot2BaseTypes::EccP256CurvePoint& p_ecc_point, OCTETSTRING& p_public_comp_key, OCTETSTRING& p_public_key_x, OCTETSTRING& p_public_key_y);
  void fill_public_key_vectors(const ec_elliptic_curves p_elliptic_curve, const IEEE1609dot2BaseTypes::EccP384CurvePoint& p_ecc_point, OCTETSTRING& p_public_comp_key, OCTETSTRING& p_public_key_x, OCTETSTRING& p_public_key_y);
+37 −6
Changes for ccsrc/Protocols/Security/security_cache.cc: 37 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,24 @@ using namespace std; // Required for isnan()

#include "loggers.hh"

/*bool security_cache_comp::operator()(const OCTETSTRING& p_lhs, const OCTETSTRING& p_rhs) const {
  loggers::get_instance().log_msg(">>> security_cache_comp::operator(): ", p_lhs);
  loggers::get_instance().log_msg(">>> security_cache_comp::operator(): ", p_rhs);
  if (p_lhs.lengthof() != p_rhs.lengthof()) {
    return !false;
  } else {
    for (int i = 0; i < p_lhs.lengthof(); i++) {
      loggers::get_instance().log("security_cache_comp::operator(): compare %02x with %02x", p_lhs[i].get_octet(), p_rhs[i].get_octet());
      if (p_lhs[i].get_octet() != p_rhs[i].get_octet()) {
        loggers::get_instance().log("<<< security_cache_comp::operator(): false");
        return !false;
      }
    }
  }
  loggers::get_instance().log("<<< security_cache_comp::operator(): true");
  return !true;
}*/

security_cache::security_cache(): _certificates(), _hashed_id8s() {
  loggers::get_instance().log(">>> security_cache::security_cache");
} // End of ctor
@@ -42,8 +60,11 @@ int security_cache::clear() {
int security_cache::get_certificate_id(const OCTETSTRING& p_hashed_id8, std::string& p_certifcate_id) const {
  loggers::get_instance().log_msg(">>> security_cache::get_certificate_id: ", p_hashed_id8);

  std::map<OCTETSTRING, std::string>::const_iterator it = _hashed_id8s.find(p_hashed_id8);
  //std::map<OCTETSTRING, std::string>::const_iterator it = _hashed_id8s.find(p_hashed_id8);
  std::vector<unsigned char> v(static_cast<const unsigned char*>(p_hashed_id8), p_hashed_id8.lengthof() + static_cast<const unsigned char*>(p_hashed_id8));
  std::map<std::vector<unsigned char>, std::string>::const_iterator it = _hashed_id8s.find(v);
  if (it == _hashed_id8s.cend()) {
    dump();
    p_certifcate_id = "";
    return -1;
  }
@@ -267,12 +288,20 @@ int security_cache::store_certificate(const CHARSTRING& p_cert_id, const OCTETST
  if (i != _certificates.cend()) {
    _certificates.erase(i);
  }
  std::map<OCTETSTRING, std::string>::const_iterator h = _hashed_id8s.find(hashed_id8);

  _certificates.insert(std::pair<std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>(p)));

  //std::map<OCTETSTRING, std::string>::const_iterator h = _hashed_id8s.find(hashed_id8);
  std::vector<unsigned char> v(static_cast<const unsigned char*>(hashed_id8), hashed_id8.lengthof() + static_cast<const unsigned char*>(hashed_id8));
  loggers::get_instance().log_to_hexa("security_cache::store_certificate: Converted hashed_id8: ", v.data(), v.size());
  std::map<std::vector<unsigned char>, std::string>::const_iterator h = _hashed_id8s.find(v);
  if (h != _hashed_id8s.cend()) {
    _hashed_id8s.erase(h);
  }
  _certificates.insert(std::pair<std::string, std::unique_ptr<security_db_record> >(key, std::unique_ptr<security_db_record>(p)));
  _hashed_id8s.insert(std::pair<OCTETSTRING, std::string&>(hashed_id8, key));
  //_hashed_id8s.insert(std::pair<OCTETSTRING, std::string&>(hashed_id8, key));
  loggers::get_instance().log("security_cache::store_certificate: Insert %s to _hashed_id8", key.c_str());
  _hashed_id8s.insert(std::pair<std::vector<unsigned char>, std::string&>(v, key));
  
  //dump();

  return 0;
@@ -297,7 +326,9 @@ void security_cache::dump() const {
    loggers::get_instance().log_msg("security_cache::dump: public_enc_key_y = ", p->public_enc_key_y());
    loggers::get_instance().log_msg("security_cache::dump: public_enc_comp_key = ", p->public_enc_comp_key());
  } // End of 'for' statement
  for (std::map<OCTETSTRING, std::string>::const_iterator it = _hashed_id8s.cbegin(); it != _hashed_id8s.cend(); ++it) {
    loggers::get_instance().log("security_cache::dump: Hashedid8 idx = %s", it->second.c_str());
  //for (std::map<OCTETSTRING, std::string>::const_iterator it = _hashed_id8s.cbegin(); it != _hashed_id8s.cend(); ++it) {
  for (std::map<std::vector<unsigned char>, std::string>::const_iterator it = _hashed_id8s.cbegin(); it != _hashed_id8s.cend(); ++it) {
    //    loggers::get_instance().log_to_hexa("security_cache::dump: Hashedid8 key = ", it->first.data(), it->first.size());
    loggers::get_instance().log("security_cache::dump: Hashedid8 idx ==> %s", it->second.c_str());
  } // End of 'for' statement
} // End of method dump
+7 −1
Changes for ccsrc/Protocols/Security/security_cache.hh: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@
#include <string>
#include <memory>
#include <map>
#include <vector>

#include "security_db_record.hh"

@@ -21,6 +22,10 @@ class OCTETSTRING; //! TITAN forward declaration
class INTEGER; //! TITAN forward declaration
class CHARSTRING; //! TITAN forward declaration

/*struct security_cache_comp {
  bool operator()(const OCTETSTRING& p_lhs, const OCTETSTRING& p_rhs) const;
  };*/

/*!
 * \class security_cache
 * \brief This class provides security record description for in memory security database. In memory mens there is no disk load/save mechanism
@@ -31,7 +36,8 @@ class security_cache {
protected: /*! \protectedsection */
  // TODO Enforce with const security_cache_record, and const std::string
  std::map<std::string, std::unique_ptr<security_db_record> > _certificates; //! List of the certificates indexed by the certificate identifier
  std::map<OCTETSTRING, std::string> _hashed_id8s; //! List of the certificates indexed by the HashedId8
  //std::map<OCTETSTRING, std::string, security_cache_comp> _hashed_id8s; //! List of the certificates indexed by the HashedId8
  std::map<std::vector<unsigned char>, std::string> _hashed_id8s; //! List of the certificates indexed by the HashedId8
  
public: /*! \publicsection */
  /*!
+1 −0
Changes for etc/AtsGenCert/AtsGenCert.cfg: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ LogEntityName := Yes

[EXECUTE]
# In this section you can specify what parts of your test suite you want to execute.
#ItsGencert_TestCases.TC_GEN_CERT
ItsGencert_TestCases.control

[GROUPS]
Loading