Aktualizr
C++ SOTA Client
uptanerepository.cc
1 #include "uptane/uptanerepository.h"
2 
3 #include <cstdio>
4 #include <utility>
5 
6 #include <openssl/bio.h>
7 #include <openssl/pem.h>
8 #include <openssl/x509.h>
9 #include <boost/algorithm/hex.hpp>
10 #include <boost/algorithm/string/replace.hpp>
11 #include <boost/algorithm/string/trim.hpp>
12 
13 #include "bootstrap/bootstrap.h"
14 #include "crypto/crypto.h"
15 #include "crypto/openssl_compat.h"
16 #include "logging/logging.h"
17 #include "storage/invstorage.h"
18 #include "utilities/utils.h"
19 
20 namespace Uptane {
21 
22 const std::string RepositoryType::DIRECTOR = "director";
23 const std::string RepositoryType::IMAGE = "image";
24 
25 void RepositoryCommon::initRoot(RepositoryType repo_type, const std::string& root_raw) {
26  try {
27  root = Root(type, Utils::parseJSON(root_raw)); // initialization and format check
28  root = Root(type, Utils::parseJSON(root_raw), root); // signature verification against itself
29  } catch (const std::exception& e) {
30  LOG_ERROR << "Loading initial " << repo_type.toString() << " Root metadata failed: " << e.what();
31  throw;
32  }
33 }
34 
35 void RepositoryCommon::verifyRoot(const std::string& root_raw) {
36  try {
37  int prev_version = rootVersion();
38  // 5.4.4.3.2.3. Version N+1 of the Root metadata file MUST have been signed
39  // by the following: (1) a threshold of keys specified in the latest Root
40  // metadata file (version N), and (2) a threshold of keys specified in the
41  // new Root metadata file being validated (version N+1).
42  root = Root(type, Utils::parseJSON(root_raw), root); // double signature verification
43  // 5.4.4.3.2.4. The version number of the latest Root metadata file (version
44  // N) must be less than or equal to the version number of the new Root
45  // metadata file (version N+1). NOTE: we do not accept an equal version
46  // number. It must increment.
47  if (root.version() != prev_version + 1) {
48  LOG_ERROR << "Version " << root.version() << " in Root metadata doesn't match the expected value "
49  << prev_version + 1;
50  throw Uptane::RootRotationError(type.toString());
51  }
52  } catch (const std::exception& e) {
53  LOG_ERROR << "Signature verification for Root metadata failed: " << e.what();
54  throw;
55  }
56 }
57 
58 void RepositoryCommon::resetRoot() { root = Root(Root::Policy::kAcceptAll); }
59 
60 void RepositoryCommon::updateRoot(INvStorage& storage, const IMetadataFetcher& fetcher,
61  const RepositoryType repo_type) {
62  // 5.4.4.3.1. Load the previous Root metadata file.
63  {
64  std::string root_raw;
65  if (storage.loadLatestRoot(&root_raw, repo_type)) {
66  initRoot(repo_type, root_raw);
67  } else {
68  fetcher.fetchRole(&root_raw, kMaxRootSize, repo_type, Role::Root(), Version(1));
69  initRoot(repo_type, root_raw);
70  storage.storeRoot(root_raw, repo_type, Version(1));
71  }
72  }
73 
74  // 5.4.4.3.2. Update to the latest Root metadata file.
75  for (int version = rootVersion() + 1; version < kMaxRotations; ++version) {
76  // 5.4.4.3.2.2. Try downloading a new version N+1 of the Root metadata file.
77  std::string root_raw;
78  try {
79  fetcher.fetchRole(&root_raw, kMaxRootSize, repo_type, Role::Root(), Version(version));
80  } catch (const std::exception& e) {
81  break;
82  }
83 
84  verifyRoot(root_raw);
85 
86  // 5.4.4.3.2.5. Set the latest Root metadata file to the new Root metadata
87  // file.
88  storage.storeRoot(root_raw, repo_type, Version(version));
89  storage.clearNonRootMeta(repo_type);
90  }
91 
92  // 5.4.4.3.3. Check that the current (or latest securely attested) time is
93  // lower than the expiration timestamp in the latest Root metadata file.
94  // (Checks for a freeze attack.)
95  if (rootExpired()) {
96  throw Uptane::ExpiredMetadata(repo_type.toString(), Role::ROOT);
97  }
98 }
99 
100 } // namespace Uptane
Uptane::ExpiredMetadata
Definition: exceptions.h:74
Uptane
Base data types that are used in The Update Framework (TUF), part of Uptane.
Definition: packagemanagerinterface.h:18
Uptane::RootRotationError
Definition: exceptions.h:122
INvStorage
Definition: invstorage.h:43