1 #ifndef AKTUALIZR_UPTANE_TUF_H_ 2 #define AKTUALIZR_UPTANE_TUF_H_ 11 #include "uptane/exceptions.h" 13 #include "crypto/crypto.h" 21 using KeyId = std::string;
30 static Role Timestamp() {
return Role{RoleEnum::kTimestamp}; }
31 static Role InvalidRole() {
return Role{RoleEnum::kInvalidRole}; }
34 explicit Role(
const std::string & );
35 std::string ToString()
const;
36 int ToInt()
const {
return static_cast<int>(role_); }
37 bool operator==(
const Role &other)
const {
return role_ == other.role_; }
38 bool operator!=(
const Role &other)
const {
return !(*
this == other); }
39 bool operator<(
const Role &other)
const {
return role_ < other.role_; }
41 friend std::ostream &operator<<(std::ostream &os,
const Role &t);
45 enum class RoleEnum { kRoot = 0, kSnapshot = 1, kTargets = 2, kTimestamp = 3, kInvalidRole = -1 };
47 explicit Role(RoleEnum role) : role_(role) {}
52 std::ostream &operator<<(std::ostream &os,
const Role &t);
59 Version() : version_(ANY_VERSION) {}
60 explicit Version(
int v) : version_(v) {}
61 std::string RoleFileName(
Role role)
const;
62 int version() {
return version_; }
65 static const int ANY_VERSION = -1;
67 friend std::ostream &operator<<(std::ostream &os,
const Version &v);
70 std::ostream &operator<<(std::ostream &os,
const Version &v);
78 bool IsExpiredAt(
const TimeStamp &now)
const;
80 std::string ToString()
const {
return time_; }
81 bool operator<(
const TimeStamp &other)
const;
82 bool operator>(
const TimeStamp &other)
const;
83 friend std::ostream &operator<<(std::ostream &os,
const TimeStamp &t);
84 bool operator==(
const TimeStamp &rhs)
const {
return time_ == rhs.time_; }
90 std::ostream &operator<<(std::ostream &os,
const TimeStamp &t);
95 static const int kMinLength = 0;
96 static const int kMaxLength = 200;
103 if (kMaxLength < hwid.length()) {
104 throw std::out_of_range(
"Hardware Identifier too long");
108 std::string ToString()
const {
return hwid_; }
121 std::ostream &operator<<(std::ostream &os, const HardwareIdentifier &hwid);
126 static const int kMinLength = 1;
127 static const int kMaxLength = 64;
129 static EcuSerial Unknown() { return EcuSerial("Unknown"); }
130 explicit EcuSerial(const std::string &ecu_serial) : ecu_serial_(ecu_serial) {
131 if (ecu_serial.length() < kMinLength) {
132 throw std::out_of_range("Ecu serial identifier is too short");
134 if (kMaxLength < ecu_serial.length()) {
135 throw std::out_of_range("Ecu serial identifier is too long");
139 std::string ToString() const { return ecu_serial_; }
141 bool operator==(const EcuSerial &rhs) const { return ecu_serial_ == rhs.ecu_serial_; }
142 bool operator!=(const EcuSerial &rhs) const { return !(*this == rhs); }
144 bool operator<(const EcuSerial &rhs) const { return ecu_serial_ < rhs.ecu_serial_; }
145 friend std::ostream &operator<<(std::ostream &os, const EcuSerial &ecu_serial);
146 friend struct std::hash<Uptane::EcuSerial>;
149 std::string ecu_serial_;
152 std::ostream &operator<<(std::ostream &os, const EcuSerial &ecu_serial);
161 enum class Type { kSha256, kSha512, kUnknownAlgorithm };
163 Hash(const std::string &type, const std::string &hash);
164 Hash(Type type, const std::string &hash);
166 bool HaveAlgorithm() const { return type_ != Type::kUnknownAlgorithm; }
167 bool operator==(const Hash &other) const;
168 bool operator!=(const Hash &other) const { return !operator==(other); }
169 std::string TypeString() const;
171 std::string HashString() const { return hash_; }
172 friend std::ostream &operator<<(std::ostream &os, const Hash &h);
179 std::ostream &operator<<(std::ostream &os, const Hash &h);
183 Target(std::string filename, const Json::Value &content);
185 const std::map<EcuSerial, HardwareIdentifier> &ecus() const { return ecus_; }
186 std::string filename() const { return filename_; }
187 std::string sha256Hash() const;
188 std::vector<Hash> hashes() const { return hashes_; };
190 bool MatchWith(const Hash &hash) const;
192 int64_t length() const { return length_; }
194 bool IsForSecondary(const EcuSerial &ecuIdentifier) const {
195 return (std::find_if(ecus_.cbegin(), ecus_.cend(), [&ecuIdentifier](std::pair<EcuSerial, HardwareIdentifier> pair) {
196 return pair.first == ecuIdentifier;
206 bool IsOstree() const;
208 bool operator==(const Target &t2) const {
210 if (filename_ != t2.filename_) {
213 if (length_ != t2.length_) {
220 bool oneMatchingHash = false;
221 for (const Hash &hash : hashes_) {
222 for (const Hash &hash2 : t2.hashes_) {
223 if (hash.type() == hash2.type() && !(hash == hash2)) {
227 oneMatchingHash = true;
231 return oneMatchingHash;
234 Json::Value toDebugJson() const;
235 friend std::ostream &operator<<(std::ostream &os, const Target &t);
238 std::string filename_;
240 std::map<EcuSerial, HardwareIdentifier> ecus_;
241 std::vector<Hash> hashes_;
245 std::ostream &operator<<(std::ostream &os, const Target &t);
251 BaseMeta() = default;
252 explicit BaseMeta(const Json::Value &json);
253 BaseMeta(RepositoryType repo, const Json::Value &json, Root &root);
254 int version() const { return version_; }
255 TimeStamp expiry() const { return expiry_; }
256 bool isExpired(const TimeStamp &now) const { return expiry_.IsExpiredAt(now); }
257 Json::Value original() const { return original_object_; }
259 bool operator==(const BaseMeta &rhs) const { return version_ == rhs.version() && expiry_ == rhs.expiry(); }
264 Json::Value original_object_;
267 void init(const Json::Value &json);
271 class Root : public BaseMeta {
273 enum class Policy { kRejectAll, kAcceptAll, kCheck };
277 explicit Root(Policy policy = Policy::kRejectAll) : policy_(policy) { version_ = 0; }
283 Root(RepositoryType repo, const Json::Value &json);
284 Root(RepositoryType repo, const Json::Value &json, Root &root);
299 void UnpackSignedObject(RepositoryType repo, const Json::Value &signed_object);
300 bool operator==(const Root &rhs) const {
301 return version_ == rhs.version_ && expiry_ == rhs.expiry_ && keys_ == rhs.keys_ &&
302 keys_for_role_ == rhs.keys_for_role_ && thresholds_for_role_ == rhs.thresholds_for_role_ &&
303 policy_ == rhs.policy_;
307 static const int64_t kMinSignatures = 1;
308 static const int64_t kMaxSignatures = 1000;
311 std::map<KeyId, PublicKey> keys_;
312 std::set<std::pair<Role, KeyId> > keys_for_role_;
313 std::map<Role, int64_t> thresholds_for_role_;
316 class Targets : public BaseMeta {
318 explicit Targets(const Json::Value &json);
319 Targets(RepositoryType repo, const Json::Value &json, Root &root);
322 std::vector<Uptane::Target> targets;
323 bool operator==(const Targets &rhs) const {
324 return version_ == rhs.version() && expiry_ == rhs.expiry() && targets == rhs.targets;
328 void init(const Json::Value &json);
331 class TimestampMeta : public BaseMeta {
333 explicit TimestampMeta(const Json::Value &json);
334 TimestampMeta(RepositoryType repo, const Json::Value &json, Root &root);
335 TimestampMeta() = default;
336 std::vector<Hash> snapshot_hashes() const { return snapshot_hashes_; };
337 int64_t snapshot_size() const { return snapshot_size_; };
338 int snapshot_version() const { return snapshot_version_; };
341 void init(const Json::Value &json);
343 std::vector<Hash> snapshot_hashes_;
344 int64_t snapshot_size_{0};
345 int snapshot_version_{-1};
348 class Snapshot : public BaseMeta {
350 explicit Snapshot(const Json::Value &json);
351 Snapshot(RepositoryType repo, const Json::Value &json, Root &root);
352 Snapshot() = default;
353 std::vector<Hash> targets_hashes() const { return targets_hashes_; };
354 int64_t targets_size() const { return targets_size_; };
355 int targets_version() const { return targets_version_; };
356 bool operator==(const Snapshot &rhs) const {
357 return version_ == rhs.version() && expiry_ == rhs.expiry() && targets_size_ == rhs.targets_size_ &&
358 targets_version_ == rhs.targets_version_ && targets_hashes_ == rhs.targets_hashes_;
362 void init(const Json::Value &json);
363 int64_t targets_size_{0};
364 int targets_version_{-1};
365 std::vector<Hash> targets_hashes_;
370 Targets director_targets;
372 Targets image_targets;
373 TimestampMeta image_timestamp;
374 Snapshot image_snapshot;
375 bool isConsistent() const;
379 std::string director_root;
380 std::string director_targets;
381 std::string image_root;
382 std::string image_targets;
383 std::string image_timestamp;
384 std::string image_snapshot;
387 int extractVersionUntrusted(const std::string &meta);
393 struct hash<Uptane::HardwareIdentifier> {
394 size_t operator()(const Uptane::HardwareIdentifier &hwid) const { return std::hash<std::string>()(hwid.hwid_); }
398 struct hash<Uptane::EcuSerial> {
399 size_t operator()(const Uptane::EcuSerial &ecu_serial) const {
400 return std::hash<std::string>()(ecu_serial.ecu_serial_);
Metadata version numbers.
TimeStamp()
An invalid TimeStamp.
RepositoryType
This must match the repo_type table in sqlstorage.
Base data types that are used in The Update Framework (TUF), part of UPTANE.