Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
managedsecondary.h
1 #ifndef PRIMARY_MANAGEDSECONDARY_H_
2 #define PRIMARY_MANAGEDSECONDARY_H_
3 
4 #include <future>
5 #include <string>
6 #include <vector>
7 
8 #include <boost/filesystem.hpp>
9 #include "json/json.h"
10 
11 #include "libaktualizr/types.h"
12 #include "primary/secondary_config.h"
13 #include "primary/secondaryinterface.h"
14 
15 namespace Primary {
16 
18  public:
19  ManagedSecondaryConfig(const char* type = Type) : SecondaryConfig(type) {}
20 
21  public:
22  constexpr static const char* const Type = "managed";
23 
24  public:
25  bool partial_verifying{false};
26  std::string ecu_serial;
27  std::string ecu_hardware_id;
28  boost::filesystem::path full_client_dir;
29  std::string ecu_private_key;
30  std::string ecu_public_key;
31  boost::filesystem::path firmware_path;
32  boost::filesystem::path target_name_path;
33  boost::filesystem::path metadata_path;
34  KeyType key_type{KeyType::kRSA2048};
35 };
36 
37 struct MetaPack {
38  Uptane::Root director_root;
39  Uptane::Targets director_targets;
40  Uptane::Root image_root;
41  Uptane::Targets image_targets;
42  Uptane::TimestampMeta image_timestamp;
43  Uptane::Snapshot image_snapshot;
44  bool isConsistent() const;
45 };
46 
47 // ManagedSecondary is an abstraction over virtual and other types of legacy
48 // (non-Uptane) Secondaries. They require any the Uptane-related functionality
49 // to be implemented in aktualizr itself.
51  public:
53  ~ManagedSecondary() override = default;
54 
55  void init(std::shared_ptr<SecondaryProvider> secondary_provider_in) override {
56  secondary_provider_ = std::move(secondary_provider_in);
57  }
58  void Initialize();
59 
60  Uptane::EcuSerial getSerial() const override {
61  if (!sconfig.ecu_serial.empty()) {
62  return Uptane::EcuSerial(sconfig.ecu_serial);
63  }
64  return Uptane::EcuSerial(public_key_.KeyId());
65  }
66  Uptane::HardwareIdentifier getHwId() const override { return Uptane::HardwareIdentifier(sconfig.ecu_hardware_id); }
67  PublicKey getPublicKey() const override { return public_key_; }
68  data::InstallationResult putMetadata(const Uptane::Target& target) override;
69  int getRootVersion(bool director) const override;
70  data::InstallationResult putRoot(const std::string& root, bool director) override;
71 
72  data::InstallationResult sendFirmware(const Uptane::Target& target) override;
73  data::InstallationResult install(const Uptane::Target& target) override;
74 
75  Uptane::Manifest getManifest() const override;
76 
77  bool loadKeys(std::string* pub_key, std::string* priv_key);
78 
79  protected:
80  virtual bool getFirmwareInfo(Uptane::InstalledImageInfo& firmware_info) const;
81 
82  std::shared_ptr<SecondaryProvider> secondary_provider_;
84  std::string detected_attack;
85 
86  private:
87  void storeKeys(const std::string& pub_key, const std::string& priv_key);
88  void rawToMeta();
89 
90  // TODO: implement persistent storage.
91  bool storeMetadata() { return true; }
92  bool loadMetadata() { return true; }
93 
94  PublicKey public_key_;
95  std::string private_key;
96  MetaPack current_meta;
97  Uptane::MetaBundle meta_bundle_;
98 };
99 
100 } // namespace Primary
101 
102 #endif // PRIMARY_MANAGEDSECONDARY_H_