Aktualizr
C++ SOTA Client
manifest.cc
1 #include "manifest.h"
2 
3 #include "crypto/keymanager.h"
4 
5 namespace Uptane {
6 
7 std::string Manifest::filepath() const {
8  try {
9  return (*this)["signed"]["installed_image"]["filepath"].asString();
10  } catch (const std::exception &ex) {
11  LOG_ERROR << "Unable to parse manifest: " << ex.what();
12  return "";
13  }
14 }
15 
16 Hash Manifest::installedImageHash() const {
17  try {
18  return Hash(Hash::Type::kSha256, (*this)["signed"]["installed_image"]["fileinfo"]["hashes"]["sha256"].asString());
19  } catch (const std::exception &ex) {
20  LOG_ERROR << "Unable to parse manifest: " << ex.what();
21  return Hash(Hash::Type::kUnknownAlgorithm, "");
22  }
23 }
24 
25 std::string Manifest::signature() const {
26  try {
27  return (*this)["signatures"][0]["sig"].asString();
28  } catch (const std::exception &ex) {
29  LOG_ERROR << "Unable to parse manifest: " << ex.what();
30  return "";
31  }
32 }
33 
34 std::string Manifest::signedBody() const {
35  try {
36  return Utils::jsonToCanonicalStr((*this)["signed"]);
37  } catch (const std::exception &ex) {
38  LOG_ERROR << "Unable to parse manifest: " << ex.what();
39  return "";
40  }
41 }
42 
43 bool Manifest::verifySignature(const PublicKey &pub_key) const {
44  if (!(isMember("signatures") && isMember("signed"))) {
45  LOG_ERROR << "Missing either signature or the signing body/subject: " << *this;
46  return false;
47  }
48 
49  return pub_key.VerifySignature(signature(), signedBody());
50 }
51 
52 Manifest ManifestIssuer::sign(const Manifest &manifest, const std::string &report_counter) const {
53  Manifest manifest_to_sign = manifest;
54  if (!report_counter.empty()) {
55  manifest_to_sign["report_counter"] = report_counter;
56  }
57  return key_mngr_->signTuf(manifest_to_sign);
58 }
59 
60 Manifest ManifestIssuer::assembleManifest(const InstalledImageInfo &installed_image_info,
61  const Uptane::EcuSerial &ecu_serial) {
62  Json::Value installed_image;
63  installed_image["filepath"] = installed_image_info.name;
64  installed_image["fileinfo"]["length"] = Json::UInt64(installed_image_info.len);
65  installed_image["fileinfo"]["hashes"]["sha256"] = installed_image_info.hash;
66 
67  Json::Value unsigned_ecu_version;
68  unsigned_ecu_version["attacks_detected"] = "";
69  unsigned_ecu_version["installed_image"] = installed_image;
70  unsigned_ecu_version["ecu_serial"] = ecu_serial.ToString();
71  unsigned_ecu_version["previous_timeserver_time"] = "1970-01-01T00:00:00Z";
72  unsigned_ecu_version["timeserver_time"] = "1970-01-01T00:00:00Z";
73  return unsigned_ecu_version;
74 }
75 
76 Hash ManifestIssuer::generateVersionHash(const std::string &data) { return Hash::generate(Hash::Type::kSha256, data); }
77 
78 std::string ManifestIssuer::generateVersionHashStr(const std::string &data) {
79  // think of unifying a hash case,we use both lower and upper cases
80  return boost::algorithm::to_lower_copy(generateVersionHash(data).HashString());
81 }
82 
83 Manifest ManifestIssuer::assembleManifest(const InstalledImageInfo &installed_image_info) const {
84  return assembleManifest(installed_image_info, ecu_serial_);
85 }
86 
87 Manifest ManifestIssuer::assembleManifest(const Uptane::Target &target) const {
88  return assembleManifest(target.getTargetImageInfo());
89 }
90 
91 Manifest ManifestIssuer::assembleAndSignManifest(const InstalledImageInfo &installed_image_info) const {
92  return key_mngr_->signTuf(assembleManifest(installed_image_info));
93 }
94 
95 } // namespace Uptane
Hash
The Hash class The hash of a file or Uptane metadata.
Definition: types.h:159
data
General data structures.
Definition: types.h:217
Uptane::EcuSerial
Definition: types.h:346
PublicKey
Definition: types.h:119
Uptane::Target
Definition: types.h:379
Uptane
Base data types that are used in The Update Framework (TUF), part of Uptane.
Definition: packagemanagerinterface.h:18
PublicKey::VerifySignature
bool VerifySignature(const std::string &signature, const std::string &message) const
Verify a signature using this public key.
Definition: crypto.cc:72