1 #ifndef AKTUALIZR_UPTANE_TUF_H_
2 #define AKTUALIZR_UPTANE_TUF_H_
12 #include <unordered_map>
15 #include "crypto/crypto.h"
17 #include "uptane/exceptions.h"
24 enum class Type { kUnknown = -1, kImage = 0, kDirector = 1 };
27 static const std::string IMAGE;
28 static const std::string DIRECTOR;
31 static constexpr
int Director() {
return static_cast<int>(Type::kDirector); }
32 static constexpr
int Image() {
return static_cast<int>(Type::kImage); }
33 RepositoryType(
int type) { type_ =
static_cast<RepositoryType::Type
>(type); }
35 if (repo_type == DIRECTOR) {
36 type_ = RepositoryType::Type::kDirector;
37 }
else if (repo_type == IMAGE) {
38 type_ = RepositoryType::Type::kImage;
40 throw std::runtime_error(std::string(
"Incorrect repo type: ") + repo_type);
43 operator int()
const {
return static_cast<int>(type_); }
44 operator std::string()
const {
return toString(); }
46 std::string toString()
const {
47 if (type_ == RepositoryType::Type::kDirector) {
49 }
else if (type_ == RepositoryType::Type::kImage) {
57 using KeyId = std::string;
63 static const std::string ROOT;
64 static const std::string SNAPSHOT;
65 static const std::string TARGETS;
66 static const std::string TIMESTAMP;
71 static Role Timestamp() {
return Role{RoleEnum::kTimestamp}; }
73 static Role InvalidRole() {
return Role{RoleEnum::kInvalidRole}; }
77 static bool IsReserved(
const std::string &name) {
78 return (name == ROOT || name == TARGETS || name == SNAPSHOT || name == TIMESTAMP);
81 explicit Role(
const std::string &role_name,
bool delegation =
false);
82 std::string ToString()
const;
83 int ToInt()
const {
return static_cast<int>(role_); }
84 bool IsDelegation()
const {
return role_ == RoleEnum::kDelegation; }
85 bool operator==(
const Role &other)
const {
return name_ == other.name_; }
86 bool operator!=(
const Role &other)
const {
return !(*
this == other); }
87 bool operator<(
const Role &other)
const {
return name_ < other.name_; }
89 friend std::ostream &operator<<(std::ostream &os,
const Role &role);
94 enum class RoleEnum { kRoot = 0, kSnapshot = 1, kTargets = 2, kTimestamp = 3, kDelegation = 4, kInvalidRole = -1 };
96 explicit Role(RoleEnum role) : role_(role) {
97 if (role_ == RoleEnum::kRoot) {
99 }
else if (role_ == RoleEnum::kSnapshot) {
101 }
else if (role_ == RoleEnum::kTargets) {
103 }
else if (role_ == RoleEnum::kTimestamp) {
106 role_ = RoleEnum::kInvalidRole;
107 name_ =
"invalidrole";
115 std::ostream &operator<<(std::ostream &os,
const Role &role);
122 Version() : version_(ANY_VERSION) {}
123 explicit Version(
int v) : version_(v) {}
124 std::string RoleFileName(
const Role &role)
const;
125 int version()
const {
return version_; }
126 bool operator==(
const Version &rhs)
const {
return version_ == rhs.version_; }
127 bool operator!=(
const Version &rhs)
const {
return version_ != rhs.version_; }
128 bool operator<(
const Version &rhs)
const {
return version_ < rhs.version_; }
131 static const int ANY_VERSION = -1;
133 friend std::ostream &operator<<(std::ostream &os,
const Version &v);
136 std::ostream &operator<<(std::ostream &os,
const Version &v);
143 explicit BaseMeta(
const Json::Value &json);
145 int version()
const {
return version_; }
146 TimeStamp expiry()
const {
return expiry_; }
147 bool isExpired(
const TimeStamp &now)
const {
return expiry_.IsExpiredAt(now); }
148 Json::Value original()
const {
return original_object_; }
150 bool operator==(
const BaseMeta &rhs)
const {
return version_ == rhs.version() && expiry_ == rhs.expiry(); }
155 Json::Value original_object_;
158 void init(
const Json::Value &json);
163 enum class Policy { kRejectAll, kAcceptAll, kCheck };
175 const std::shared_ptr<MetaWithKeys> &signer);
182 void ParseRole(
RepositoryType repo,
const Json::ValueConstIterator &it,
const Role &role,
183 const std::string &meta_role);
202 return version_ == rhs.version_ && expiry_ == rhs.expiry_ && keys_ == rhs.keys_ &&
203 keys_for_role_ == rhs.keys_for_role_ && thresholds_for_role_ == rhs.thresholds_for_role_;
207 static const int64_t kMinSignatures = 1;
208 static const int64_t kMaxSignatures = 1000;
210 std::map<KeyId, PublicKey> keys_;
211 std::set<std::pair<Role, KeyId>> keys_for_role_;
212 std::map<Role, int64_t> thresholds_for_role_;
221 explicit Root(Policy policy = Policy::kRejectAll) : policy_(policy) { version_ = 0; }
230 ~
Root()
override =
default;
248 bool operator==(
const Root &rhs)
const {
249 return version_ == rhs.version_ && expiry_ == rhs.expiry_ && keys_ == rhs.keys_ &&
250 keys_for_role_ == rhs.keys_for_role_ && thresholds_for_role_ == rhs.thresholds_for_role_ &&
251 policy_ == rhs.policy_;
258 static bool MatchTargetVector(
const std::vector<Uptane::Target> &v1,
const std::vector<Uptane::Target> &v2) {
259 if (v1.size() != v2.size()) {
262 for (
size_t i = 0; i < v1.size(); ++i) {
263 if (!v1[i].MatchTarget(v2[i])) {
273 explicit Targets(
const Json::Value &json);
278 bool operator==(
const Targets &rhs)
const {
279 return version_ == rhs.version() && expiry_ == rhs.expiry() && MatchTargetVector(targets, rhs.targets);
282 const std::string &correlation_id()
const {
return correlation_id_; }
286 delegated_role_names_.clear();
287 paths_for_role_.clear();
288 terminating_role_.clear();
293 std::vector<Uptane::Target>
result;
294 for (
auto it = targets.begin(); it != targets.end(); ++it) {
295 auto found_loc = std::find_if(it->ecus().begin(), it->ecus().end(),
296 [ecu_id, hw_id](
const std::pair<EcuSerial, HardwareIdentifier> &val) {
297 return ((ecu_id == val.first) && (hw_id == val.second));
300 if (found_loc != it->ecus().end()) {
307 std::vector<Uptane::Target> targets;
308 std::vector<std::string> delegated_role_names_;
309 std::map<Role, std::vector<std::string>> paths_for_role_;
310 std::map<Role, bool> terminating_role_;
313 void init(
const Json::Value &json);
316 std::string correlation_id_;
324 std::vector<Hash> snapshot_hashes()
const {
return snapshot_hashes_; };
325 int64_t snapshot_size()
const {
return snapshot_size_; };
326 int snapshot_version()
const {
return snapshot_version_; };
329 void init(
const Json::Value &json);
331 std::vector<Hash> snapshot_hashes_;
332 int64_t snapshot_size_{0};
333 int snapshot_version_{-1};
338 explicit Snapshot(
const Json::Value &json);
341 std::vector<Hash> role_hashes(
const Uptane::Role &role)
const;
344 bool operator==(
const Snapshot &rhs)
const {
345 return version_ == rhs.version() && expiry_ == rhs.expiry() && role_size_ == rhs.role_size_ &&
346 role_version_ == rhs.role_version_ && role_hashes_ == rhs.role_hashes_;
350 void init(
const Json::Value &json);
351 std::map<Uptane::Role, int64_t> role_size_;
352 std::map<Uptane::Role, int> role_version_;
353 std::map<Uptane::Role, std::vector<Hash>> role_hashes_;
357 std::size_t operator()(
const std::pair<RepositoryType, Role> &pair)
const {
358 return std::hash<std::string>()(pair.first.toString()) ^ std::hash<std::string>()(pair.second.ToString());
362 std::string getMetaFromBundle(
const MetaBundle &bundle,
RepositoryType repo,
const Role &role);
364 int extractVersionUntrusted(
const std::string &meta);
370 struct hash<
Uptane::HardwareIdentifier> {
377 return std::hash<std::string>()(ecu_serial.ecu_serial_);
382 #endif // AKTUALIZR_UPTANE_TUF_H_