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