Aktualizr
C++ SOTA Client
imagesrepository.cc
1 #include "imagesrepository.h"
2 
3 namespace Uptane {
4 
5 void ImagesRepository::resetMeta() {
6  resetRoot();
7  targets = Targets();
8  snapshot = Snapshot();
9  timestamp = TimestampMeta();
10 }
11 
12 bool ImagesRepository::verifyTimestamp(const std::string& timestamp_raw) {
13  try {
14  timestamp = TimestampMeta(RepositoryType::Images, Utils::parseJSON(timestamp_raw), root); // signature verification
15  } catch (const Exception& e) {
16  LOG_ERROR << "Signature verification for timestamp metadata failed";
17  last_exception = e;
18  return false;
19  }
20  return true;
21 }
22 
23 bool ImagesRepository::verifySnapshot(const std::string& snapshot_raw) {
24  try {
25  std::string canonical = Utils::jsonToCanonicalStr(Utils::parseJSON(snapshot_raw));
26  bool hash_exists = false;
27  for (const auto& it : timestamp.snapshot_hashes()) {
28  switch (it.type()) {
29  case Hash::Type::kSha256:
30  if (Hash(Hash::Type::kSha256, boost::algorithm::hex(Crypto::sha256digest(canonical))) != it) {
31  LOG_ERROR << "Hash verification for snapshot metadata failed";
32  return false;
33  }
34  hash_exists = true;
35  break;
36  case Hash::Type::kSha512:
37  if (Hash(Hash::Type::kSha512, boost::algorithm::hex(Crypto::sha512digest(canonical))) != it) {
38  LOG_ERROR << "Hash verification for snapshot metadata failed";
39  return false;
40  }
41  hash_exists = true;
42  break;
43  default:
44  break;
45  }
46  }
47  if (!hash_exists) {
48  LOG_ERROR << "No hash found for shapshot.json";
49  return false;
50  }
51  snapshot = Snapshot(RepositoryType::Images, Utils::parseJSON(snapshot_raw), root); // signature verification
52  if (snapshot.version() != timestamp.snapshot_version()) {
53  return false;
54  }
55  } catch (const Exception& e) {
56  LOG_ERROR << "Signature verification for snapshot metadata failed";
57  last_exception = e;
58  return false;
59  }
60  return true;
61 }
62 
63 bool ImagesRepository::verifyTargets(const std::string& targets_raw) {
64  try {
65  std::string canonical = Utils::jsonToCanonicalStr(Utils::parseJSON(targets_raw));
66  bool hash_exists = false;
67  for (const auto& it : snapshot.targets_hashes()) {
68  switch (it.type()) {
69  case Hash::Type::kSha256:
70  if (Hash(Hash::Type::kSha256, boost::algorithm::hex(Crypto::sha256digest(canonical))) != it) {
71  LOG_ERROR << "Hash verification for targets metadata failed";
72  return false;
73  }
74  hash_exists = true;
75  break;
76  case Hash::Type::kSha512:
77  if (Hash(Hash::Type::kSha512, boost::algorithm::hex(Crypto::sha512digest(canonical))) != it) {
78  LOG_ERROR << "Hash verification for targets metadata failed";
79  return false;
80  }
81  hash_exists = true;
82  break;
83  default:
84  break;
85  }
86  }
87  if (!hash_exists) {
88  LOG_ERROR << "No hash found for targets.json";
89  return false;
90  }
91  targets = Targets(RepositoryType::Images, Utils::parseJSON(targets_raw), root); // signature verification
92  if (targets.version() != snapshot.targets_version()) {
93  return false;
94  }
95  } catch (const Exception& e) {
96  LOG_ERROR << "Signature verification for images targets metadata failed";
97  last_exception = e;
98  return false;
99  }
100  return true;
101 }
102 
103 std::unique_ptr<Uptane::Target> ImagesRepository::getTarget(const Uptane::Target& director_target) {
104  auto it = std::find(targets.targets.begin(), targets.targets.end(), director_target);
105  if (it == targets.targets.end()) {
106  return std::unique_ptr<Uptane::Target>(nullptr);
107  } else {
108  return std_::make_unique<Uptane::Target>(*it);
109  }
110 }
111 
112 } // namespace Uptane
Base data types that are used in The Update Framework (TUF), part of UPTANE.