Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
manifest.cc
1 #include "manifest.h"
2 
3 #include "crypto/keymanager.h"
4 
5 namespace Uptane {
6 
7 std::string Manifest::filepath() const { return (*this)["signed"]["installed_image"]["filepath"].asString(); }
8 
9 Hash Manifest::installedImageHash() const {
10  // TODO: proper verification of the required fields
11  return Hash(Hash::Type::kSha256, (*this)["signed"]["installed_image"]["fileinfo"]["hashes"]["sha256"].asString());
12 }
13 
14 std::string Manifest::signature() const {
15  // TODO: proper verification of the required fields
16  return (*this)["signatures"][0]["sig"].asString();
17 }
18 
19 std::string Manifest::signedBody() const {
20  // TODO: proper verification of the required fields
21  return Utils::jsonToCanonicalStr((*this)["signed"]);
22 }
23 
24 bool Manifest::verifySignature(const PublicKey &pub_key) const {
25  if (!(isMember("signatures") && isMember("signed"))) {
26  LOG_ERROR << "Missing either signature or the signing body/subject: " << *this;
27  return false;
28  }
29 
30  return pub_key.VerifySignature(signature(), signedBody());
31 }
32 
33 Manifest ManifestIssuer::sign(const Manifest &manifest, const std::string &report_counter) const {
34  Manifest manifest_to_sign = manifest;
35  if (!report_counter.empty()) {
36  manifest_to_sign["report_counter"] = report_counter;
37  }
38  return key_mngr_->signTuf(manifest_to_sign);
39 }
40 
41 Manifest ManifestIssuer::assembleManifest(const InstalledImageInfo &installed_image_info,
42  const Uptane::EcuSerial &ecu_serial) {
43  Json::Value installed_image;
44  installed_image["filepath"] = installed_image_info.name;
45  installed_image["fileinfo"]["length"] = Json::UInt64(installed_image_info.len);
46  installed_image["fileinfo"]["hashes"]["sha256"] = installed_image_info.hash;
47 
48  Json::Value unsigned_ecu_version;
49  unsigned_ecu_version["attacks_detected"] = "";
50  unsigned_ecu_version["installed_image"] = installed_image;
51  unsigned_ecu_version["ecu_serial"] = ecu_serial.ToString();
52  unsigned_ecu_version["previous_timeserver_time"] = "1970-01-01T00:00:00Z";
53  unsigned_ecu_version["timeserver_time"] = "1970-01-01T00:00:00Z";
54  return unsigned_ecu_version;
55 }
56 
57 Hash ManifestIssuer::generateVersionHash(const std::string &data) { return Hash::generate(Hash::Type::kSha256, data); }
58 
59 std::string ManifestIssuer::generateVersionHashStr(const std::string &data) {
60  // think of unifying a hash case,we use both lower and upper cases
61  return boost::algorithm::to_lower_copy(generateVersionHash(data).HashString());
62 }
63 
64 Manifest ManifestIssuer::assembleManifest(const InstalledImageInfo &installed_image_info) const {
65  return assembleManifest(installed_image_info, ecu_serial_);
66 }
67 
68 Manifest ManifestIssuer::assembleManifest(const Uptane::Target &target) const {
69  return assembleManifest(target.getTargetImageInfo());
70 }
71 
72 Manifest ManifestIssuer::assembleAndSignManifest(const InstalledImageInfo &installed_image_info) const {
73  return key_mngr_->signTuf(assembleManifest(installed_image_info));
74 }
75 
76 } // 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:354
PublicKey
Definition: types.h:119
Uptane::Target
Definition: types.h:387
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:63