Aktualizr
C++ SOTA Client
partialverificationsecondary.cc
1 #include "uptane/partialverificationsecondary.h"
2 
3 #include <string>
4 #include <vector>
5 
6 #include <boost/filesystem.hpp>
7 #include "json/json.h"
8 
9 #include "logging/logging.h"
10 #include "uptane/secondaryconfig.h"
11 #include "uptane/secondaryinterface.h"
12 #include "utilities/exceptions.h"
13 #include "utilities/types.h"
14 
15 namespace Uptane {
16 
17 PartialVerificationSecondary::PartialVerificationSecondary(const SecondaryConfig &sconfig_in)
18  : SecondaryInterface(sconfig_in), root_(Root::Policy::kAcceptAll) {
19  boost::filesystem::create_directories(sconfig.metadata_path);
20 
21  // FIXME Probably we need to generate keys on the secondary
22  std::string public_key_string;
23  if (!loadKeys(&public_key_string, &private_key_)) {
24  if (!Crypto::generateKeyPair(sconfig.key_type, &public_key_string, &private_key_)) {
25  LOG_ERROR << "Could not generate keys for secondary " << PartialVerificationSecondary::getSerial() << "@"
26  << sconfig.ecu_hardware_id;
27  throw std::runtime_error("Unable to generate secondary keys");
28  }
29  storeKeys(public_key_string, private_key_);
30  }
31  public_key_ = PublicKey(public_key_string, sconfig.key_type);
32 }
33 
34 bool PartialVerificationSecondary::putMetadata(const RawMetaPack &meta) {
35  TimeStamp now(TimeStamp::Now());
36  detected_attack_.clear();
37 
38  // TODO: check for expiration and version downgrade
39  root_ = Uptane::Root(RepositoryType::Director, Utils::parseJSON(meta.director_root), root_);
40  Uptane::Targets targets(RepositoryType::Director, Utils::parseJSON(meta.director_targets), root_);
41  if (meta_targets_.version() > targets.version()) {
42  detected_attack_ = "Rollback attack detected";
43  return true;
44  }
45  meta_targets_ = targets;
46  std::vector<Uptane::Target>::const_iterator it;
47  bool target_found = false;
48  for (it = meta_targets_.targets.begin(); it != meta_targets_.targets.end(); ++it) {
49  if (it->IsForSecondary(getSerial())) {
50  if (target_found) {
51  detected_attack_ = "Duplicate entry for this ECU";
52  break;
53  }
54  target_found = true;
55  }
56  }
57  return true;
58 }
59 
60 Json::Value PartialVerificationSecondary::getManifest() {
62  return Json::Value();
63 }
64 
65 int PartialVerificationSecondary::getRootVersion(bool director) {
66  (void)director;
68  return 0;
69 }
70 
71 bool PartialVerificationSecondary::putRoot(const std::string &root, bool director) {
72  (void)root;
73  (void)director;
74 
76  return false;
77 }
78 
79 bool PartialVerificationSecondary::sendFirmwareAsync(const std::shared_ptr<std::string> &data) {
80  (void)data;
81  sendEvent(std::make_shared<event::InstallStarted>(getSerial()));
82 
83  return false;
84 }
85 
86 void PartialVerificationSecondary::storeKeys(const std::string &public_key, const std::string &private_key) {
87  Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_private_key), private_key);
88  Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_public_key), public_key);
89 }
90 
91 bool PartialVerificationSecondary::loadKeys(std::string *public_key, std::string *private_key) {
92  boost::filesystem::path public_key_path = sconfig.full_client_dir / sconfig.ecu_public_key;
93  boost::filesystem::path private_key_path = sconfig.full_client_dir / sconfig.ecu_private_key;
94 
95  if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
96  return false;
97  }
98 
99  *private_key = Utils::readFile(private_key_path.string());
100  *public_key = Utils::readFile(public_key_path.string());
101  return true;
102 }
103 } // namespace Uptane
General data structures.
Definition: types.cc:6
Base data types that are used in The Update Framework (TUF), part of UPTANE.