Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 1 | // 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 Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 14 | #include "loop_mounter.h" |
| 15 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 16 | namespace imageloader { |
| 17 | |
| 18 | struct ImageLoaderConfig { |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 19 | 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 Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 26 | std::vector<uint8_t> key; |
| 27 | base::FilePath storage_dir; |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 28 | base::FilePath mount_path; |
| 29 | std::unique_ptr<LoopMounter> loop_mounter; |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 32 | |
| 33 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 34 | class ImageLoaderImpl { |
| 35 | public: |
| 36 | // Instantiate an object with a configuration object. |
Greg Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 37 | explicit ImageLoaderImpl(ImageLoaderConfig config) |
| 38 | : config_(std::move(config)) {} |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 39 | |
| 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 Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame^] | 50 | // Load the specified component at a set mount point. |
| 51 | bool LoadComponent(const std::string& name, const std::string& mount_point); |
| 52 | |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 53 | 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 Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 70 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, MountValidImage); |
Greg Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 71 | |
| 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 Kerr | 89be05f | 2016-07-27 10:40:32 -0700 | [diff] [blame] | 100 | // 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 Kerr | c5b9169 | 2016-09-14 12:09:22 -0700 | [diff] [blame^] | 107 | // 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 Kerr | a6c0c52 | 2016-07-25 11:15:31 -0700 | [diff] [blame] | 116 | // 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_ |