Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
partialverificationsecondary.cc
1 #include "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/secondaryinterface.h"
11 #include "utilities/exceptions.h"
12 #include "utilities/types.h"
13 
14 namespace Uptane {
15 
16 PartialVerificationSecondary::PartialVerificationSecondary(Primary::PartialVerificationSecondaryConfig sconfig_in)
17  : sconfig(std::move(sconfig_in)), root_(Root::Policy::kAcceptAll) {
18  boost::filesystem::create_directories(sconfig.metadata_path);
19 
20  // TODO(OTA-2484): Probably we need to generate keys on the secondary
21  std::string public_key_string;
22  if (!loadKeys(&public_key_string, &private_key_)) {
23  if (!Crypto::generateKeyPair(sconfig.key_type, &public_key_string, &private_key_)) {
24  LOG_ERROR << "Could not generate keys for secondary " << PartialVerificationSecondary::getSerial() << "@"
25  << sconfig.ecu_hardware_id;
26  throw std::runtime_error("Unable to generate secondary keys");
27  }
28  storeKeys(public_key_string, private_key_);
29  }
30  public_key_ = PublicKey(public_key_string, sconfig.key_type);
31 }
32 
33 bool PartialVerificationSecondary::putMetadata(const RawMetaPack &meta) {
34  TimeStamp now(TimeStamp::Now());
35  detected_attack_.clear();
36 
37  // TODO(OTA-2484): check for expiration and version downgrade
38  root_ = Uptane::Root(RepositoryType::Director(), Utils::parseJSON(meta.director_root), root_);
39  Uptane::Targets targets(RepositoryType::Director(), Role::Targets(), Utils::parseJSON(meta.director_targets),
40  std::make_shared<Uptane::Root>(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->IsForEcu(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 Uptane::Manifest PartialVerificationSecondary::getManifest() const {
62  return Json::Value();
63 }
64 
65 int PartialVerificationSecondary::getRootVersion(bool director) const {
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::sendFirmware(const std::string &data) {
80  (void)data;
82 }
83 
84 data::ResultCode::Numeric PartialVerificationSecondary::install(const std::string &target_name) {
85  (void)target_name;
87 }
88 
89 void PartialVerificationSecondary::storeKeys(const std::string &public_key, const std::string &private_key) {
90  Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_private_key), private_key);
91  Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_public_key), public_key);
92 }
93 
94 bool PartialVerificationSecondary::loadKeys(std::string *public_key, std::string *private_key) {
95  boost::filesystem::path public_key_path = sconfig.full_client_dir / sconfig.ecu_public_key;
96  boost::filesystem::path private_key_path = sconfig.full_client_dir / sconfig.ecu_private_key;
97 
98  if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
99  return false;
100  }
101 
102  *private_key = Utils::readFile(private_key_path.string());
103  *public_key = Utils::readFile(public_key_path.string());
104  return true;
105 }
106 } // namespace Uptane
types.h
Primary::PartialVerificationSecondaryConfig
Definition: partialverificationsecondary.h:17
data
General data structures.
Definition: types.cc:55
TimeStamp
Definition: types.h:86
Uptane::Targets
Definition: tuf.h:412
NotImplementedException
Definition: exceptions.h:14
PublicKey
Definition: crypto.h:26
data::ResultCode::Numeric
Numeric
Definition: types.h:128
Uptane::Root
Definition: tuf.h:357
Uptane
Base data types that are used in The Update Framework (TUF), part of Uptane.
Definition: ipuptanesecondary.cc:11
Uptane::Manifest
Definition: manifest.h:15