Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
types.h
Go to the documentation of this file.
1 #ifndef TYPES_H_
2 #define TYPES_H_
3 /** \file */
4 
5 #include <algorithm>
6 #include <stdexcept>
7 
8 #include <json/json.h>
9 #include <boost/filesystem.hpp>
10 
11 // Keep these int sync with AKIpUptaneKeyType ASN.1 definitions
12 enum class KeyType {
13  kED25519 = 0,
14  kFirstKnown = kED25519,
15  kRSA2048,
16  kRSA3072,
17  kRSA4096,
18  kLastKnown = kRSA4096,
19  kUnknown = 0xff
20 };
21 
22 inline std::ostream& operator<<(std::ostream& os, const KeyType kt) {
23  std::string kt_str;
24  switch (kt) {
25  case KeyType::kRSA2048:
26  kt_str = "RSA2048";
27  break;
28  case KeyType::kRSA3072:
29  kt_str = "RSA3072";
30  break;
31  case KeyType::kRSA4096:
32  kt_str = "RSA4096";
33  break;
34  case KeyType::kED25519:
35  kt_str = "ED25519";
36  break;
37  default:
38  kt_str = "unknown";
39  break;
40  }
41  os << '"' << kt_str << '"';
42  return os;
43 }
44 
45 inline std::istream& operator>>(std::istream& is, KeyType& kt) {
46  std::string kt_str;
47 
48  is >> kt_str;
49  std::transform(kt_str.begin(), kt_str.end(), kt_str.begin(), ::toupper);
50 
51  if (kt_str == "\"RSA2048\"") {
52  kt = KeyType::kRSA2048;
53  } else if (kt_str == "\"RSA3072\"") {
54  kt = KeyType::kRSA3072;
55  } else if (kt_str == "\"RSA4096\"") {
56  kt = KeyType::kRSA4096;
57  } else if (kt_str == "\"ED25519\"") {
58  kt = KeyType::kED25519;
59  } else {
60  kt = KeyType::kUnknown;
61  }
62  return is;
63 }
64 
65 enum class CryptoSource { kFile = 0, kPkcs11, kAndroid };
66 
67 inline std::ostream& operator<<(std::ostream& os, CryptoSource cs) {
68  std::string cs_str;
69  switch (cs) {
70  case CryptoSource::kFile:
71  cs_str = "file";
72  break;
73  case CryptoSource::kPkcs11:
74  cs_str = "pkcs11";
75  break;
76  default:
77  cs_str = "unknown";
78  break;
79  }
80  os << '"' << cs_str << '"';
81  return os;
82 }
83 
84 // timestamp, compatible with tuf
85 class TimeStamp {
86  public:
87  static TimeStamp Now();
88  /** An invalid TimeStamp */
89  TimeStamp() { ; }
90  explicit TimeStamp(std::string rfc3339);
91  bool IsExpiredAt(const TimeStamp& now) const;
92  bool IsValid() const;
93  std::string ToString() const { return time_; }
94  bool operator<(const TimeStamp& other) const;
95  bool operator>(const TimeStamp& other) const;
96  friend std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
97  bool operator==(const TimeStamp& rhs) const { return time_ == rhs.time_; }
98 
99  class InvalidTimeStamp : public std::domain_error {
100  public:
101  InvalidTimeStamp() : std::domain_error("invalid timestamp") {}
102  ~InvalidTimeStamp() noexcept override = default;
103  };
104 
105  private:
106  std::string time_;
107 };
108 
109 std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
110 
111 /// General data structures.
112 namespace data {
113 
114 using UpdateRequestId = std::string;
115 struct Package {
116  std::string name;
117  std::string version;
118  Json::Value toJson();
119  static Package fromJson(const std::string& /*json_str*/);
120 };
121 
122 struct ResultCode {
123  // These match the old enum representation
124  // A lot of them were unused and have been dropped
125  enum class Numeric {
126  kOk = 0,
127  /// Operation has already been processed
128  kAlreadyProcessed = 1,
129  /// Metadata verification failed
131  /// Package installation failed
132  kInstallFailed = 4,
133  /// Package download failed
134  kDownloadFailed = 5,
135  /// SWM Internal integrity error
136  kInternalError = 18,
137  /// Other error
138  kGeneralError = 19,
139  // Install needs to be finalized (e.g: reboot)
140  kNeedCompletion = 21,
141  // Customer specific
142  kCustomError = 22,
143  // Unknown
144  kUnknown = -1,
145  };
146 
147  // note: intentionally *not* explicit, to make the common case easier
148  ResultCode(ResultCode::Numeric in_num_code) : num_code(in_num_code) {}
149  ResultCode(ResultCode::Numeric in_num_code, std::string text_code_in)
150  : num_code(in_num_code), text_code(std::move(text_code_in)) {}
151 
152  bool operator==(const ResultCode& rhs) const { return num_code == rhs.num_code && toString() == rhs.toString(); }
153  bool operator!=(const ResultCode& rhs) const { return !(*this == rhs); }
154  friend std::ostream& operator<<(std::ostream& os, const ResultCode& result_code);
155 
156  Numeric num_code;
157  std::string text_code;
158 
159  // Allows to have a numeric code with a default representation, but also with
160  // any string representation
161  std::string toString() const {
162  if (text_code != "") {
163  return text_code;
164  }
165 
166  return std::string(string_repr.at(num_code));
167  }
168 
169  // non-lossy reprensation for serialization
170  std::string toRepr() const;
171  static ResultCode fromRepr(const std::string& repr);
172 
173  private:
174  static const std::map<Numeric, const char*> string_repr;
175 };
176 
177 std::ostream& operator<<(std::ostream& os, const ResultCode& result_code);
178 
180  InstallationResult() = default;
181  InstallationResult(ResultCode result_code_in, std::string description_in)
182  : success(result_code_in.num_code == ResultCode::Numeric::kOk),
183  result_code(std::move(result_code_in)),
184  description(std::move(description_in)) {}
185  InstallationResult(bool success_in, ResultCode result_code_in, std::string description_in)
186  : success(success_in), result_code(std::move(result_code_in)), description(std::move(description_in)) {}
187 
188  Json::Value toJson() const;
189  bool isSuccess() const { return success; };
190  bool needCompletion() const { return result_code == ResultCode::Numeric::kNeedCompletion; }
191 
192  bool success{true};
193  ResultCode result_code{ResultCode::Numeric::kOk};
194  std::string description;
195 };
196 
197 } // namespace data
198 
199 #endif
data::ResultCode
Definition: types.h:122
data::ResultCode::Numeric::kVerificationFailed
Metadata verification failed.
data::ResultCode::Numeric::kGeneralError
Other error.
data::ResultCode::Numeric::kAlreadyProcessed
Operation has already been processed.
data::InstallationResult
Definition: types.h:179
TimeStamp::InvalidTimeStamp
Definition: types.h:99
TimeStamp::TimeStamp
TimeStamp()
An invalid TimeStamp.
Definition: types.h:89
data
General data structures.
Definition: types.cc:44
data::Package
Definition: types.h:115
TimeStamp
Definition: types.h:85
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
data::ResultCode::Numeric
Numeric
Definition: types.h:125
data::ResultCode::Numeric::kDownloadFailed
Package download failed.
data::ResultCode::Numeric::kInstallFailed
Package installation failed.