Aktualizr
C++ SOTA Client
ipuptanesecondary.cc
1 #include "ipuptanesecondary.h"
2 #include "asn1/asn1_message.h"
3 #include "der_encoder.h"
4 #include "logging/logging.h"
5 
6 #include <memory>
7 
8 namespace Uptane {
9 
10 PublicKey IpUptaneSecondary::getPublicKey() {
11  LOG_INFO << "Getting the public key of a secondary";
12  Asn1Message::Ptr req(Asn1Message::Empty());
13 
14  req->present(AKIpUptaneMes_PR_publicKeyReq);
15 
16  auto resp = Asn1Rpc(req, getAddr());
17 
18  if (resp->present() != AKIpUptaneMes_PR_publicKeyResp) {
19  LOG_ERROR << "Failed to get public key response message from secondary";
20  return PublicKey("", KeyType::kUnknown);
21  }
22  auto r = resp->publicKeyResp();
23 
24  std::string key = ToString(r->key);
25 
26  auto type = static_cast<KeyType>(r->type);
27  return PublicKey(key, type);
28 }
29 
30 bool IpUptaneSecondary::putMetadata(const RawMetaPack& meta_pack) {
31  LOG_INFO << "Sending Uptane metadata to the secondary";
32  Asn1Message::Ptr req(Asn1Message::Empty());
33  req->present(AKIpUptaneMes_PR_putMetaReq);
34 
35  auto m = req->putMetaReq();
36  m->image.present = image_PR_json;
37  SetString(&m->image.choice.json.root, meta_pack.image_root); // NOLINT
38  SetString(&m->image.choice.json.targets, meta_pack.image_targets); // NOLINT
39  SetString(&m->image.choice.json.snapshot, meta_pack.image_snapshot); // NOLINT
40  SetString(&m->image.choice.json.timestamp, meta_pack.image_timestamp); // NOLINT
41 
42  m->director.present = director_PR_json;
43  SetString(&m->director.choice.json.root, meta_pack.director_root); // NOLINT
44  SetString(&m->director.choice.json.targets, meta_pack.director_targets); // NOLINT
45 
46  auto resp = Asn1Rpc(req, getAddr());
47 
48  if (resp->present() != AKIpUptaneMes_PR_putMetaResp) {
49  LOG_ERROR << "Failed to get response to sending manifest to secondary";
50  return false;
51  }
52 
53  auto r = resp->putMetaResp();
54  return r->result == AKInstallationResult_success;
55 }
56 
57 bool IpUptaneSecondary::sendFirmwareAsync(const std::shared_ptr<std::string>& data) {
58  if (!install_future.valid() || install_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
59  install_future = std::async(std::launch::async, &IpUptaneSecondary::sendFirmware, this, data);
60  return true;
61  }
62  return false;
63 }
64 
65 bool IpUptaneSecondary::sendFirmware(const std::shared_ptr<std::string>& data) {
66  LOG_INFO << "Sending firmware to the secondary";
67  sendEvent(std::make_shared<event::InstallStarted>(getSerial()));
68  Asn1Message::Ptr req(Asn1Message::Empty());
69  req->present(AKIpUptaneMes_PR_sendFirmwareReq);
70 
71  auto m = req->sendFirmwareReq();
72  SetString(&m->firmware, *data);
73  auto resp = Asn1Rpc(req, getAddr());
74 
75  if (resp->present() != AKIpUptaneMes_PR_sendFirmwareResp) {
76  LOG_ERROR << "Failed to get response to sending firmware to secondary";
77  sendEvent(std::make_shared<event::InstallComplete>(getSerial()));
78  return false;
79  }
80 
81  auto r = resp->sendFirmwareResp();
82  sendEvent(std::make_shared<event::InstallComplete>(getSerial()));
83  return r->result == AKInstallationResult_success;
84 }
85 
86 Json::Value IpUptaneSecondary::getManifest() {
87  LOG_INFO << "Getting the manifest key of a secondary";
88  Asn1Message::Ptr req(Asn1Message::Empty());
89 
90  req->present(AKIpUptaneMes_PR_manifestReq);
91 
92  auto resp = Asn1Rpc(req, getAddr());
93 
94  if (resp->present() != AKIpUptaneMes_PR_manifestResp) {
95  LOG_ERROR << "Failed to get public key response message from secondary";
96  return Json::Value();
97  }
98  auto r = resp->manifestResp();
99 
100  if (r->manifest.present != manifest_PR_json) {
101  LOG_ERROR << "Manifest wasn't in json format";
102  return Json::Value();
103  }
104  std::string manifest = ToString(r->manifest.choice.json); // NOLINT
105  return Utils::parseJSON(manifest);
106 }
107 } // namespace Uptane
General data structures.
Definition: types.cc:6
static Asn1Message::Ptr Empty()
Create a new Asn1Message, in order to fill it with data and send it.
Definition: asn1_message.h:46
Base data types that are used in The Update Framework (TUF), part of UPTANE.