1 #include "partialverificationsecondary.h"
6 #include <boost/filesystem.hpp>
9 #include "logging/logging.h"
10 #include "uptane/secondaryinterface.h"
11 #include "utilities/exceptions.h"
17 : sconfig(std::move(sconfig_in)), root_(Root::Policy::kAcceptAll) {
18 boost::filesystem::create_directories(sconfig.metadata_path);
21 std::string public_key_string;
22 if (!loadKeys(&public_key_string, &private_key_)) {
23 if (!Crypto::generateKeyPair(sconfig.key_type, &public_key_string, &private_key_)) {
24 LOG_ERROR <<
"Could not generate keys for secondary " << PartialVerificationSecondary::getSerial() <<
"@"
25 << sconfig.ecu_hardware_id;
26 throw std::runtime_error(
"Unable to generate secondary keys");
28 storeKeys(public_key_string, private_key_);
30 public_key_ =
PublicKey(public_key_string, sconfig.key_type);
33 bool PartialVerificationSecondary::putMetadata(
const RawMetaPack &meta) {
35 detected_attack_.clear();
38 root_ =
Uptane::Root(RepositoryType::Director(), Utils::parseJSON(meta.director_root), root_);
39 Uptane::Targets targets(RepositoryType::Director(), Role::Targets(), Utils::parseJSON(meta.director_targets),
40 std::make_shared<Uptane::Root>(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->IsForEcu(getSerial())) {
51 detected_attack_ =
"Duplicate entry for this ECU";
65 int PartialVerificationSecondary::getRootVersion(
bool director)
const {
71 bool PartialVerificationSecondary::putRoot(
const std::string &root,
bool director) {
79 bool PartialVerificationSecondary::sendFirmware(
const std::string &
data) {
89 void PartialVerificationSecondary::storeKeys(
const std::string &public_key,
const std::string &private_key) {
90 Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_private_key), private_key);
91 Utils::writeFile((sconfig.full_client_dir / sconfig.ecu_public_key), public_key);
94 bool PartialVerificationSecondary::loadKeys(std::string *public_key, std::string *private_key) {
95 boost::filesystem::path public_key_path = sconfig.full_client_dir / sconfig.ecu_public_key;
96 boost::filesystem::path private_key_path = sconfig.full_client_dir / sconfig.ecu_private_key;
98 if (!boost::filesystem::exists(public_key_path) || !boost::filesystem::exists(private_key_path)) {
102 *private_key = Utils::readFile(private_key_path.string());
103 *public_key = Utils::readFile(public_key_path.string());