1 #include "uptane/partialverificationsecondary.h" 6 #include <boost/filesystem.hpp> 9 #include "logging/logging.h" 10 #include "uptane/secondaryconfig.h" 11 #include "uptane/secondaryinterface.h" 12 #include "utilities/exceptions.h" 17 PartialVerificationSecondary::PartialVerificationSecondary(
const SecondaryConfig &sconfig_in)
18 : SecondaryInterface(sconfig_in), root_(Root::Policy::kAcceptAll) {
19 boost::filesystem::create_directories(sconfig.metadata_path);
22 std::string public_key_string;
23 if (!loadKeys(&public_key_string, &private_key_)) {
24 if (!Crypto::generateKeyPair(sconfig.key_type, &public_key_string, &private_key_)) {
25 LOG_ERROR <<
"Could not generate keys for secondary " << PartialVerificationSecondary::getSerial() <<
"@" 26 << sconfig.ecu_hardware_id;
27 throw std::runtime_error(
"Unable to generate secondary keys");
29 storeKeys(public_key_string, private_key_);
31 public_key_ =
PublicKey(public_key_string, sconfig.key_type);
34 bool PartialVerificationSecondary::putMetadata(
const RawMetaPack &meta) {
35 TimeStamp now(TimeStamp::Now());
36 detected_attack_.clear();
39 root_ =
Uptane::Root(RepositoryType::Director, Utils::parseJSON(meta.director_root), root_);
40 Uptane::Targets targets(RepositoryType::Director, Utils::parseJSON(meta.director_targets), root_);
41 if (meta_targets_.version() > targets.version()) {
42 detected_attack_ =
"Rollback attack detected";
45 meta_targets_ = targets;
46 std::vector<Uptane::Target>::const_iterator it;
47 bool target_found =
false;
48 for (it = meta_targets_.targets.begin(); it != meta_targets_.targets.end(); ++it) {
49 if (it->IsForSecondary(getSerial())) {
51 detected_attack_ =
"Duplicate entry for this ECU";
60 Json::Value PartialVerificationSecondary::getManifest() {
65 int PartialVerificationSecondary::getRootVersion(
bool director) {
71 bool PartialVerificationSecondary::putRoot(
const std::string &root,
bool director) {
79 bool PartialVerificationSecondary::sendFirmwareAsync(
const std::shared_ptr<std::string> &
data) {
81 sendEvent(std::make_shared<event::InstallStarted>(getSerial()));
86 void PartialVerificationSecondary::storeKeys(
const std::string &public_key,
const std::string &private_key) {
87 Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_private_key), private_key);
88 Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_public_key), public_key);
91 bool PartialVerificationSecondary::loadKeys(std::string *public_key, std::string *private_key) {
92 boost::filesystem::path public_key_path = sconfig.full_client_dir / sconfig.ecu_public_key;
93 boost::filesystem::path private_key_path = sconfig.full_client_dir / sconfig.ecu_private_key;
95 if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
99 *private_key = Utils::readFile(private_key_path.string());
100 *public_key = Utils::readFile(public_key_path.string());
Base data types that are used in The Update Framework (TUF), part of UPTANE.