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