blob: c15b7b182c2047b181e57f0c4f6f1af3f224415a [file] [log] [blame]
Greg Kerra6c0c522016-07-25 11:15:31 -07001// Copyright 2016 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#ifndef IMAGELOADER_IMAGELOADER_UTILITY_H_
5#define IMAGELOADER_IMAGELOADER_UTILITY_H_
6
7#include <string>
8#include <vector>
9
10#include <base/files/file_path.h>
11#include <base/gtest_prod_util.h>
12#include <base/macros.h>
13
Greg Kerr89be05f2016-07-27 10:40:32 -070014#include "loop_mounter.h"
15
Greg Kerra6c0c522016-07-25 11:15:31 -070016namespace imageloader {
17
18struct ImageLoaderConfig {
Greg Kerr89be05f2016-07-27 10:40:32 -070019 ImageLoaderConfig(const std::vector<uint8_t> key, const char* storage_path,
20 const char* mount_path, std::unique_ptr<LoopMounter> ops)
21 : key(key),
22 storage_dir(storage_path),
23 mount_path(mount_path),
24 loop_mounter(std::move(ops)) {}
25
Greg Kerra6c0c522016-07-25 11:15:31 -070026 std::vector<uint8_t> key;
27 base::FilePath storage_dir;
Greg Kerr89be05f2016-07-27 10:40:32 -070028 base::FilePath mount_path;
29 std::unique_ptr<LoopMounter> loop_mounter;
Greg Kerra6c0c522016-07-25 11:15:31 -070030};
31
Greg Kerr89be05f2016-07-27 10:40:32 -070032
33
Greg Kerra6c0c522016-07-25 11:15:31 -070034class ImageLoaderImpl {
35 public:
36 // Instantiate an object with a configuration object.
Greg Kerr89be05f2016-07-27 10:40:32 -070037 explicit ImageLoaderImpl(ImageLoaderConfig config)
38 : config_(std::move(config)) {}
Greg Kerra6c0c522016-07-25 11:15:31 -070039
40 // Register a component.
41 bool RegisterComponent(const std::string& name, const std::string& version,
42 const std::string& component_folder_abs_path);
43
44 // Get component version given component name.
45 std::string GetComponentVersion(const std::string& name);
46
47 // Load the specified component.
48 std::string LoadComponent(const std::string& name);
49
Greg Kerrc5b91692016-09-14 12:09:22 -070050 // Load the specified component at a set mount point.
51 bool LoadComponent(const std::string& name, const std::string& mount_point);
52
Greg Kerra6c0c522016-07-25 11:15:31 -070053 private:
54 // This is a parsed version of the imageloader.json manifest.
55 struct Manifest {
56 int manifest_version;
57 std::vector<uint8_t> image_sha256;
58 std::vector<uint8_t> params_sha256;
59 std::string version;
60 };
61
62 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ECVerify);
63 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ManifestFingerPrint);
64 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidComponent);
65 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyComponentWithBadManifest);
66 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidImage);
67 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidImage);
68 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidHash);
69 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ParseManifest);
Greg Kerr89be05f2016-07-27 10:40:32 -070070 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, MountValidImage);
Greg Kerra6c0c522016-07-25 11:15:31 -070071
72 // Verify the data with the RSA (PKCS #1 v1.5) signature.
73 bool ECVerify(const base::StringPiece data, const base::StringPiece sig);
74
75 // Copy the component directory from a user controlled location to an
76 // imageloader controlled location. Do not copy unless it verifies.
77 bool CopyComponentDirectory(const base::FilePath& component_path,
78 const base::FilePath& destination_folder,
79 const std::string& version);
80
81 // Check the string contents to see if it matches the format of a
82 // manifest.fingerprint file.
83 bool IsValidFingerprintFile(const std::string& contents);
84
85 // Verify the imageloader.json manifest file and parse the file information
86 // out of it.
87 bool VerifyAndParseManifest(const std::string& manifest_str,
88 const std::string& signature, Manifest* manifest);
89
90 // Copies files over and checks their hash in the process. The copy fails if
91 // the hashes do not match.
92 bool CopyAndHashFile(const base::FilePath& src_path,
93 const base::FilePath& dest_path,
94 const std::vector<uint8_t>& known_hash);
95
96 // Check if the client created a manifest.fingerprint, and preserve it.
97 bool CopyFingerprintFile(const base::FilePath& src,
98 const base::FilePath& dest);
99
Greg Kerr89be05f2016-07-27 10:40:32 -0700100 // if |manifest| or |sig| are not null, they are set to the manifest contents
101 // and the signature contents.
102 bool GetAndVerifyManifest(const std::string& component_name,
103 const base::FilePath& component_path,
104 Manifest* manifest, std::string* manifest_str,
105 std::string* manifest_sig);
106
Greg Kerrc5b91692016-09-14 12:09:22 -0700107 // This performs the actual working of mounting the component. It must be
108 // passed a valid |manifest| argument and |mount_point| path.
109 bool LoadComponentHelper(const std::string& component_name,
110 const Manifest& manifest,
111 const base::FilePath& mount_point);
112
113 // Looks up the component path for |name| and returns a verified manifest.
114 bool GetManifestForComponent(const std::string& name, Manifest* manifest);
115
Greg Kerra6c0c522016-07-25 11:15:31 -0700116 // The configuration traits.
117 ImageLoaderConfig config_;
118
119 DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl);
120};
121
122} // namespace imageloader
123
124#endif // IMAGELOADER_IMAGELOADER_UTILITY_H_