Aktualizr
C++ SOTA Client
repo.h
1 #ifndef REPO_H_
2 #define REPO_H_
3 
4 #include <fnmatch.h>
5 
6 #include <crypto/crypto.h>
7 #include <boost/filesystem.hpp>
8 #include "json/json.h"
9 #include "uptane/tuf.h"
10 
11 struct KeyPair {
12  KeyPair() = default;
13  KeyPair(PublicKey public_key_in, std::string private_key_in)
14  : public_key(std::move(public_key_in)), private_key(std::move(private_key_in)) {}
15  PublicKey public_key;
16  std::string private_key;
17 };
18 
19 struct Delegation {
20  Delegation() = default;
21  Delegation(const boost::filesystem::path &repo_path, std::string delegation_name);
22  bool isMatched(const boost::filesystem::path &image_path) const {
23  return (fnmatch(pattern.c_str(), image_path.c_str(), 0) == 0);
24  }
25  operator bool() const { return (!name.empty() && !pattern.empty()); }
26  std::string name;
27  std::string pattern;
28 
29  private:
30  static std::string findPatternInTree(const boost::filesystem::path &repo_path, const std::string &name,
31  const Json::Value &targets_json);
32 };
33 
34 class Repo {
35  public:
36  Repo(Uptane::RepositoryType repo_type, boost::filesystem::path path, const std::string &expires,
37  std::string correlation_id);
38  void generateRepo(KeyType key_type = KeyType::kRSA2048);
39  Json::Value getTarget(const std::string &target_name);
40  Json::Value signTuf(const Uptane::Role &role, const Json::Value &json);
41  void generateCampaigns() const;
42  void refresh(const Uptane::Role &role);
43 
44  protected:
45  void generateRepoKeys(KeyType key_type);
46  void generateKeyPair(KeyType key_type, const Uptane::Role &key_name);
47  static std::string getExpirationTime(const std::string &expires);
48  void readKeys();
49  void updateRepo();
50  Uptane::RepositoryType repo_type_;
51  boost::filesystem::path path_;
52  boost::filesystem::path repo_dir_;
53  std::string correlation_id_;
54  std::string expiration_time_;
55  std::map<Uptane::Role, KeyPair> keys_;
56 
57  private:
58  void addDelegationToSnapshot(Json::Value *snapshot, const Uptane::Role &role);
59 };
60 
61 #endif // REPO_H_
Uptane::RepositoryType
Definition: tuf.h:21
Repo
Definition: repo.h:34
PublicKey
Definition: types.h:119
Uptane::Role
TUF Roles.
Definition: tuf.h:61
Delegation
Definition: repo.h:19
KeyPair
Definition: repo.h:11