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