Commit 26a24511 authored by garciay's avatar garciay
Browse files

STF538: Finalise certificates generation ATS

parent 2db6ec0c
Loading
Loading
Loading
Loading
+80 −55
Original line number Diff line number Diff line
@@ -17,21 +17,19 @@

using namespace LibItsGeoNetworking__TypesAndValues;

GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::GeoNetworkingPort>(p_type), _params(), _codec(), _beacon(nullptr), _gbc_packet(nullptr), _shb_packet(nullptr), _tsb_packet(nullptr), _ls_reply(nullptr), _location_table(), _pass_beacon_table(), _device_mode{false}, _secured_mode{false}, _enable_security_checks{false}, _sendData(), _timerid{0}, _sev{0}, _its{0}, _freq_nanosecs(0), _mask{0}, _sa{0}, _sequence_number{0} {
GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::string & param) : TLayer<LibItsGeoNetworking__TestSystem::GeoNetworkingPort>(p_type), _params(), _codec(), _beacon(nullptr), _gbc_packet(nullptr), _shb_packet(nullptr), _tsb_packet(nullptr), _ls_reply(nullptr), _location_table(), _pass_beacon_table(), _device_mode{false}, _secured_mode{false}, _enable_security_checks{false}, _sendData(), _timerid{0}, _sev{0}, _its{0}, _freq_nanosecs(0), _mask{0}, _sa{0}, _sequence_number{0}, _latitude{0}, _longitude{0} {
  loggers::get_instance().log(">>> GeoNetworkingLayer::GeoNetworkingLayer: %s, %s", to_string().c_str(), param.c_str());
  
  // Setup parameters
  Params::convert(_params, param);
  // Sanity checks
  INTEGER latitude;
  Params::const_iterator it = _params.find(Params::latitude);
  if (it != _params.cend()) {
    latitude = converter::get_instance().string_to_int(it->second);
    _latitude = converter::get_instance().string_to_int(it->second);
  }
  INTEGER longitude;
  it = _params.find(Params::longitude);
  if (it != _params.cend()) {
    longitude = converter::get_instance().string_to_int(it->second);
    _longitude = converter::get_instance().string_to_int(it->second);
  }
  OCTETSTRING ll_address;
  it = _params.find(Params::ll_address);
@@ -92,47 +90,29 @@ GeoNetworkingLayer::GeoNetworkingLayer(const std::string & p_type, const std::st
    _params.insert(std::pair<std::string, std::string>(std::string("mac_bc"), "FFFFFFFFFFFF"));
  }
  
  // Register this object for AdapterControlPort
  loggers::get_instance().log("GeoNetworkingLayer::GeoNetworkingLayer: register %s/%p", p_type.c_str(), this);
  registration<GeoNetworkingLayer>::get_instance().add_item(p_type, this);

  // Set up default security parameters value
  if (_secured_mode == 1) {
    loggers::get_instance().log("GeoNetworkingLayer::GeoNetworkingLayer: Secured mod is activated");
    it = _params.find(Params::certificate);
    if (it == _params.cend()) {
      _params.insert(std::pair<std::string, std::string>(std::string("certificate"), "cert_ta"));
    }
    it = _params.find(Params::sec_db_path);
    if (it == _params.cend()) {
      _params.insert(std::pair<std::string, std::string>(std::string("sec_db_path"), ""));
    }
    it = _params.find(Params::hash);
    if (it == _params.cend()) {
      _params.insert(std::pair<std::string, std::string>(std::string("hash"), "SHA-256"));
    }
    it = _params.find(Params::signature);
    if (it == _params.cend()) {
      _params.insert(std::pair<std::string, std::string>(std::string("signature"), "NISTP-256"));
    }
    // Set up security services even if secured_mode is set to 0. Later, we can receive an AcEnableSecurity request, the sertificate caching will be ready to go
    security_services::get_instance().setup(_params);
    security_services::get_instance().set_position(latitude, longitude);
  if (_secured_mode == true) {
    loggers::get_instance().log("GeoNetworkingLayer::GeoNetworkingLayer: Setup secured mode");
    setup_secured_mode();
  }

  // Automatic beaconing mode
  Params::const_iterator i = _params.find(Params::beaconing);
  if ((i != _params.cend()) && (i->second.compare("1") == 0)) { // Immediate beaconing was requested
    // Prepare beaconing operation
    fill_beacon(latitude, longitude, ll_address, station_type, country, type_of_address);
    fill_beacon(ll_address, station_type, country, type_of_address);
    start_beaconing();
  }

  // Fill packet templates
  fill_gbc_packet(latitude, longitude, ll_address, latitude, longitude, distanceA, distanceB, angle);// TODO Check if GeoBroadcastArea lat/lon are identical to lat/lon of the Test System
  fill_shb_packet(latitude, longitude, ll_address);
  fill_tsb_packet(latitude, longitude, ll_address);
  fill_ls_reply(latitude, longitude, ll_address);
  fill_gbc_packet(ll_address, _latitude, _longitude, distanceA, distanceB, angle);// TODO Check if GeoBroadcastArea lat/lon are identical to lat/lon of the Test System
  fill_shb_packet(ll_address);
  fill_tsb_packet(ll_address);
  fill_ls_reply(ll_address);
  
  // Register this object for AdapterControlPort
  loggers::get_instance().log("GeoNetworkingLayer::GeoNetworkingLayer: Register %s/%p", p_type.c_str(), this);
  registration<GeoNetworkingLayer>::get_instance().add_item(p_type, this);
} // End of constructor

GeoNetworkingLayer::~GeoNetworkingLayer() {
@@ -498,10 +478,27 @@ void GeoNetworkingLayer::stop_pass_beaconing() {
} // End of stop_pass_beaconing method


int GeoNetworkingLayer::enable_secured_mode(const std::string p_certificate_id, const boolean p_enforce_security) {
  loggers::get_instance().log(">>> GeoNetworkingLayer::enable_secured_mode: '%s' - %x", p_certificate_id, p_enforce_security);
int GeoNetworkingLayer::enable_secured_mode(const std::string& p_certificate_id, const boolean p_enforce_security) {
  loggers::get_instance().log(">>> GeoNetworkingLayer::enable_secured_mode: '%s' - %x", p_certificate_id.c_str(), p_enforce_security);

  return -1;
  loggers::get_instance().log("GeoNetworkingLayer::enable_secured_mode: GN Layer address = %p", this);

  loggers::get_instance().log("GeoNetworkingLayer::enable_secured_mode: _secured_mode = %x", _secured_mode);
  if (!_secured_mode) {
    loggers::get_instance().log("GeoNetworkingLayer::enable_secured_mode: Setup secured mode");
    _secured_mode = true;
    setup_secured_mode();
  }
  _enable_security_checks = p_enforce_security;
  Params::const_iterator it = _params.find(Params::certificate);
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("certificate"), p_certificate_id));
  } else {
    _params[Params::certificate] = p_certificate_id;
  }
  loggers::get_instance().log("GeoNetworkingLayer::enable_secured_mode: Certificate to be used: '%s'", _params[Params::certificate].c_str());
  
  return 0;
}

const LongPosVector* GeoNetworkingLayer::get_lpv(const GN__Address& p_gn_address)
@@ -515,7 +512,7 @@ const LongPosVector* GeoNetworkingLayer::get_lpv(const GN__Address& p_gn_address
  return lpv;
} // End of get_lpv

void GeoNetworkingLayer::fill_beacon(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const INTEGER p_station_type, const INTEGER p_country, const INTEGER type_of_address)
void GeoNetworkingLayer::fill_beacon(const OCTETSTRING& p_ll_address, const INTEGER p_station_type, const INTEGER p_country, const INTEGER type_of_address)
{
  _beacon = new GeoNetworkingPdu();
  HeaderTST h;
@@ -533,8 +530,8 @@ void GeoNetworkingLayer::fill_beacon(const INTEGER& p_latitude, const INTEGER& p
                                                             p_ll_address
                                                             ),
                                                 0,
                                                 p_latitude,
                                                 p_longitude,
                                                 _latitude,
                                                 _longitude,
                                                 int2bit(0, 1),
                                                 0,
                                                 0
@@ -577,7 +574,7 @@ void GeoNetworkingLayer::fill_beacon(const INTEGER& p_latitude, const INTEGER& p
  //loggers::get_instance().log_msg("GeoNetworkingLayer::fill_beacon: beacon value: ", *_beacon);
} // End of fill_beacon method
  
void GeoNetworkingLayer::fill_gbc_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const INTEGER& p_geoAreaPosLatitude, const INTEGER& p_geoAreaPosLongitude, const INTEGER& p_distanceA, const INTEGER& p_distanceB, const INTEGER& p_angle)
void GeoNetworkingLayer::fill_gbc_packet(const OCTETSTRING& p_ll_address, const INTEGER& p_geoAreaPosLatitude, const INTEGER& p_geoAreaPosLongitude, const INTEGER& p_distanceA, const INTEGER& p_distanceB, const INTEGER& p_angle)
{
  _gbc_packet = new GeoNetworkingPdu();
  HeaderTST h;
@@ -597,8 +594,8 @@ void GeoNetworkingLayer::fill_gbc_packet(const INTEGER& p_latitude, const INTEGE
                                                                       p_ll_address
                                                                       ),
                                                           0,
                                                           p_latitude,
                                                           p_longitude,
                                                           _latitude,
                                                           _longitude,
                                                           int2bit(1, 1), // PAI
                                                           0,
                                                           0
@@ -647,7 +644,7 @@ void GeoNetworkingLayer::fill_gbc_packet(const INTEGER& p_latitude, const INTEGE
  //loggers::get_instance().log_msg("GeoNetworkingLayer::fill_gbc_packet: packet value: ", *_gbc_packet);
} // End of fill_gbc_packet method

void GeoNetworkingLayer::fill_shb_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address)
void GeoNetworkingLayer::fill_shb_packet(const OCTETSTRING& p_ll_address)
{
  _shb_packet = new GeoNetworkingPdu();
  HeaderTST h;
@@ -665,8 +662,8 @@ void GeoNetworkingLayer::fill_shb_packet(const INTEGER& p_latitude, const INTEGE
                                                       p_ll_address
                                                       ),
                                           0,
                                           p_latitude,
                                           p_longitude,
                                           _latitude,
                                           _longitude,
                                           int2bit(1, 1), // PAI
                                           0,
                                           0
@@ -710,7 +707,7 @@ void GeoNetworkingLayer::fill_shb_packet(const INTEGER& p_latitude, const INTEGE
  //loggers::get_instance().log_msg("GeoNetworkingLayer::fill_shb_packet: packet value: ", *_shb_packet);
} // End of fill_shb_packet method

void GeoNetworkingLayer::fill_tsb_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const int p_hop_number, const int p_max_hop_limit)
void GeoNetworkingLayer::fill_tsb_packet(const OCTETSTRING& p_ll_address, const int p_hop_number, const int p_max_hop_limit)
{
  _tsb_packet = new GeoNetworkingPdu();
  HeaderTST h;
@@ -730,8 +727,8 @@ void GeoNetworkingLayer::fill_tsb_packet(const INTEGER& p_latitude, const INTEGE
                                                       p_ll_address
                                                       ),
                                           0,
                                           p_latitude,
                                           p_longitude,
                                           _latitude,
                                           _longitude,
                                           int2bit(1, 1), // PAI
                                           0,
                                           0
@@ -774,7 +771,7 @@ void GeoNetworkingLayer::fill_tsb_packet(const INTEGER& p_latitude, const INTEGE
  //loggers::get_instance().log_msg("GeoNetworkingLayer::fill_tsb_packet: packet value: ", *_tsb_packet);
} // End of fill_tsb_packet method

void GeoNetworkingLayer::fill_ls_reply(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address)
void GeoNetworkingLayer::fill_ls_reply(const OCTETSTRING& p_ll_address)
{
  _ls_reply = new GeoNetworkingPdu();
  HeaderTST h;
@@ -794,8 +791,8 @@ void GeoNetworkingLayer::fill_ls_reply(const INTEGER& p_latitude, const INTEGER&
                                                               p_ll_address
                                                               ),
                                                   0,
                                                   p_latitude,
                                                   p_longitude,
                                                   _latitude,
                                                   _longitude,
                                                   int2bit(1, 1), // PAI
                                                   0,
                                                   0
@@ -808,8 +805,8 @@ void GeoNetworkingLayer::fill_ls_reply(const INTEGER& p_latitude, const INTEGER&
                                                                p_ll_address
                                                                ),
                                                    0,
                                                    p_latitude,
                                                    p_longitude
                                                    _latitude,
                                                    _longitude
                                                    )
                                     );
  _ls_reply->basicHeader() = BasicHeader(
@@ -1012,4 +1009,32 @@ int GeoNetworkingLayer::decode_basic_header(const OCTETSTRING& p_data, LibItsGeo
  return 0;
}

int GeoNetworkingLayer::setup_secured_mode() {
  loggers::get_instance().log(">>> GeoNetworkingLayer::setup_secured_mode");

  loggers::get_instance().log("GeoNetworkingLayer::setup_secured_mode: GN Layer address = %p", this);

  Params::const_iterator it = _params.find(Params::certificate);
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("certificate"), "CERT_TS_A_AT"));
  }
  it = _params.find(Params::sec_db_path);
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("sec_db_path"), ""));
  }
  it = _params.find(Params::hash);
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("hash"), "SHA-256"));
  }
  it = _params.find(Params::signature);
  if (it == _params.cend()) {
    _params.insert(std::pair<std::string, std::string>(std::string("signature"), "NISTP-256"));
  }
  // Set up security services even if secured_mode is set to 0. Later, we can receive an AcEnableSecurity request, the sertificate caching will be ready to go
  security_services::get_instance().setup(_params);
  security_services::get_instance().set_position(_latitude, _longitude);

  return 0;
}

GeoNetworkingLayerFactory GeoNetworkingLayerFactory::_f;
+9 −6
Original line number Diff line number Diff line
@@ -58,6 +58,8 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
  sigset_t _mask;
  struct sigaction _sa;
  unsigned int _sequence_number;
  int _latitude;
  int _longitude;

  /*!
   * \brief Initialize a beacon object for a stand alone beaconing
@@ -66,7 +68,7 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
   * \param[in] p_longitude
   * \param[in] p_address
   */ 
  void fill_beacon(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const INTEGER p_station_type = 5, const INTEGER p_country = 0, const INTEGER type_of_address = 1);
  void fill_beacon(const OCTETSTRING& p_ll_address, const INTEGER p_station_type = 5, const INTEGER p_country = 0, const INTEGER type_of_address = 1);
  /*!
   * \brief Initialize a GeoNetworking packet to send GeoNetworking Facility messages
   *        This object shall be updated before to be sent
@@ -74,7 +76,7 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
   * \param[in] p_longitude
   * \param[in] p_address
   */ 
  void fill_gbc_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const INTEGER& p_geoAreaPosLatitude, const INTEGER& p_geoAreaPosLongitude, const INTEGER& p_distanceA, const INTEGER& p_distanceB, const INTEGER& p_angle);
  void fill_gbc_packet(const OCTETSTRING& p_ll_address, const INTEGER& p_geoAreaPosLatitude, const INTEGER& p_geoAreaPosLongitude, const INTEGER& p_distanceA, const INTEGER& p_distanceB, const INTEGER& p_angle);
  /*!
   * \brief Initialize a GeoNetworking packet to send GeoNetworking Facility messages
   *        This object shall be updated before to be sent
@@ -82,7 +84,7 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
   * \param[in] p_longitude
   * \param[in] p_address
   */
  void fill_shb_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address);
  void fill_shb_packet(const OCTETSTRING& p_ll_address);
  /*!
   * \brief Initialize a GeoNetworking packet to send GeoNetworking Facility messages
   *        This object shall be updated before to be sent
@@ -92,7 +94,7 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
   * \param[in] p_hop_number
   * \param[in] p_max_hop_number
   */
  void fill_tsb_packet(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address, const int p_hop_number = 3, const int p_max_hop_limit = 5);
  void fill_tsb_packet(const OCTETSTRING& p_ll_address, const int p_hop_number = 3, const int p_max_hop_limit = 5);
  /*!
   * \brief Initialize an LS_REPLY GeoNetworking packet to reply to an LS_REQUEST
   *        This object shall be updated before to be sent
@@ -100,7 +102,7 @@ class GeoNetworkingLayer : public TLayer<LibItsGeoNetworking__TestSystem::GeoNet
   * \param[in] p_longitude
   * \param[in] p_address
   */ 
  void fill_ls_reply(const INTEGER& p_latitude, const INTEGER& p_longitude, const OCTETSTRING& p_ll_address);
  void fill_ls_reply(const OCTETSTRING& p_ll_address);
  
  static void timer_irq_sigalrm_handler(int p_signal, siginfo_t *p_signal_info, void *p_uc);

@@ -123,13 +125,14 @@ public:
  void stop_beaconing();
  void start_pass_beaconing(const LibItsGeoNetworking__TypesAndValues::BeaconHeader& p_beacon);
  void stop_pass_beaconing();
  int enable_secured_mode(const std::string p_certificate_id, const boolean p_enforce_security = false);
  int enable_secured_mode(const std::string& p_certificate_id, const boolean p_enforce_security = false);
  
private:
  void send_beacon();
  int build_geonetworking_pdu(OCTETSTRING& data, Params& params);
  int build_secured_pdu(OCTETSTRING& data, Params& params);
  int decode_basic_header(const OCTETSTRING& p_data, LibItsGeoNetworking__TypesAndValues::BasicHeader& p_basic_header);
  int setup_secured_mode();
  
}; // End of class GeoNetworkingLayer
+7 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@

security_services * security_services::instance = nullptr;

security_services::security_services() : _ec_keys(nullptr), _security_cache(new security_cache), _security_db(nullptr), _last_generation_time(0), _unknown_certificate(), _latitude(0), _longitude(0), _elevation(0) {
security_services::security_services() : _setup_done{false}, _ec_keys(nullptr), _security_cache(new security_cache), _security_db(nullptr), _last_generation_time(0), _unknown_certificate(), _latitude(0), _longitude(0), _elevation(0) {
  loggers::get_instance().log(">>> security_services::security_services");
} // End of ctor

@@ -29,6 +29,12 @@ int security_services::setup(Params& p_params) { // FIXME Rename this method
  loggers::get_instance().log(">>> security_services::setup");
  p_params.log();

  if (_setup_done) {
    loggers::get_instance().warning("security_services::setup: Already done");
    return 0;
  }
  _setup_done = true;
  
  _security_db.reset(new security_db(p_params[Params::sec_db_path]));
  if (_security_db.get() == nullptr) { // Memory allocation issue
    loggers::get_instance().warning("security_services::setup: _security_db pointer is NULL");
+1 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ class security_services {
   * \brief Unique static object reference of this class
   */
  static security_services* instance;
  bool _setup_done;
  std::unique_ptr<ec_keys> _ec_keys;
  std::unique_ptr<security_cache> _security_cache;
  std::unique_ptr<security_db> _security_db;