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  kt_str.erase(std::remove(kt_str.begin(), kt_str.end(), '"'), kt_str.end());
51 
52  if (kt_str == "RSA2048") {
53  kt = KeyType::kRSA2048;
54  } else if (kt_str == "RSA3072") {
55  kt = KeyType::kRSA3072;
56  } else if (kt_str == "RSA4096") {
57  kt = KeyType::kRSA4096;
58  } else if (kt_str == "ED25519") {
59  kt = KeyType::kED25519;
60  } else {
61  kt = KeyType::kUnknown;
62  }
63  return is;
64 }
65 
66 enum class CryptoSource { kFile = 0, kPkcs11, kAndroid };
67 
68 inline std::ostream& operator<<(std::ostream& os, CryptoSource cs) {
69  std::string cs_str;
70  switch (cs) {
71  case CryptoSource::kFile:
72  cs_str = "file";
73  break;
74  case CryptoSource::kPkcs11:
75  cs_str = "pkcs11";
76  break;
77  default:
78  cs_str = "unknown";
79  break;
80  }
81  os << '"' << cs_str << '"';
82  return os;
83 }
84 
85 // timestamp, compatible with tuf
86 class TimeStamp {
87  public:
88  static TimeStamp Now();
89  static struct tm CurrentTime();
90  /** An invalid TimeStamp */
91  TimeStamp() { ; }
92  explicit TimeStamp(std::string rfc3339);
93  explicit TimeStamp(struct tm time);
94  bool IsExpiredAt(const TimeStamp& now) const;
95  bool IsValid() const;
96  std::string ToString() const { return time_; }
97  bool operator<(const TimeStamp& other) const;
98  bool operator>(const TimeStamp& other) const;
99  friend std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
100  bool operator==(const TimeStamp& rhs) const { return time_ == rhs.time_; }
101 
102  class InvalidTimeStamp : public std::domain_error {
103  public:
104  InvalidTimeStamp() : std::domain_error("invalid timestamp") {}
105  ~InvalidTimeStamp() noexcept override = default;
106  };
107 
108  private:
109  std::string time_;
110 };
111 
112 std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
113 
114 /// General data structures.
115 namespace data {
116 
117 using UpdateRequestId = std::string;
118 struct Package {
119  std::string name;
120  std::string version;
121  Json::Value toJson() const;
122  static Package fromJson(const std::string& /*json_str*/);
123 };
124 
125 struct ResultCode {
126  // These match the old enum representation
127  // A lot of them were unused and have been dropped
128  enum class Numeric {
129  kOk = 0,
130  /// Operation has already been processed
131  kAlreadyProcessed = 1,
132  /// Metadata verification failed
134  /// Package installation failed
135  kInstallFailed = 4,
136  /// Package download failed
137  kDownloadFailed = 5,
138  /// SWM Internal integrity error
139  kInternalError = 18,
140  /// Other error
141  kGeneralError = 19,
142  // Install needs to be finalized (e.g: reboot)
143  kNeedCompletion = 21,
144  // Customer specific
145  kCustomError = 22,
146  // Unknown
147  kUnknown = -1,
148  };
149 
150  // note: intentionally *not* explicit, to make the common case easier
151  ResultCode(ResultCode::Numeric in_num_code) : num_code(in_num_code) {}
152  ResultCode(ResultCode::Numeric in_num_code, std::string text_code_in)
153  : num_code(in_num_code), text_code(std::move(text_code_in)) {}
154 
155  bool operator==(const ResultCode& rhs) const { return num_code == rhs.num_code && toString() == rhs.toString(); }
156  bool operator!=(const ResultCode& rhs) const { return !(*this == rhs); }
157  friend std::ostream& operator<<(std::ostream& os, const ResultCode& result_code);
158 
159  Numeric num_code;
160  std::string text_code;
161 
162  // Allows to have a numeric code with a default representation, but also with
163  // any string representation
164  std::string toString() const {
165  if (text_code != "") {
166  return text_code;
167  }
168 
169  return std::string(string_repr.at(num_code));
170  }
171 
172  // non-lossy reprensation for serialization
173  std::string toRepr() const;
174  static ResultCode fromRepr(const std::string& repr);
175 
176  private:
177  static const std::map<Numeric, const char*> string_repr;
178 };
179 
180 std::ostream& operator<<(std::ostream& os, const ResultCode& result_code);
181 
183  InstallationResult() = default;
184  InstallationResult(ResultCode result_code_in, std::string description_in)
185  : success(result_code_in.num_code == ResultCode::Numeric::kOk),
186  result_code(std::move(result_code_in)),
187  description(std::move(description_in)) {}
188  InstallationResult(bool success_in, ResultCode result_code_in, std::string description_in)
189  : success(success_in), result_code(std::move(result_code_in)), description(std::move(description_in)) {}
190 
191  Json::Value toJson() const;
192  bool isSuccess() const { return success; };
193  bool needCompletion() const { return result_code == ResultCode::Numeric::kNeedCompletion; }
194 
195  bool success{true};
196  ResultCode result_code{ResultCode::Numeric::kOk};
197  std::string description;
198 };
199 
200 } // namespace data
201 
202 #endif
data::ResultCode
Definition: types.h:125
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:182
TimeStamp::InvalidTimeStamp
Definition: types.h:102
TimeStamp::TimeStamp
TimeStamp()
An invalid TimeStamp.
Definition: types.h:91
data
General data structures.
Definition: types.cc:55
data::Package
Definition: types.h:118
TimeStamp
Definition: types.h:86
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
data::ResultCode::Numeric
Numeric
Definition: types.h:128
data::ResultCode::Numeric::kDownloadFailed
Package download failed.
data::ResultCode::Numeric::kInstallFailed
Package installation failed.