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 | |
| 14 | namespace imageloader { |
| 15 | |
| 16 | struct ImageLoaderConfig { |
| 17 | ImageLoaderConfig(const std::vector<uint8_t> key, const char* path) |
| 18 | : key(key), storage_dir(path) {} |
| 19 | std::vector<uint8_t> key; |
| 20 | base::FilePath storage_dir; |
| 21 | }; |
| 22 | |
| 23 | class ImageLoaderImpl { |
| 24 | public: |
| 25 | // Instantiate an object with a configuration object. |
| 26 | explicit ImageLoaderImpl(const ImageLoaderConfig& config) : config_(config) {} |
| 27 | |
| 28 | // Register a component. |
| 29 | bool RegisterComponent(const std::string& name, const std::string& version, |
| 30 | const std::string& component_folder_abs_path); |
| 31 | |
| 32 | // Get component version given component name. |
| 33 | std::string GetComponentVersion(const std::string& name); |
| 34 | |
| 35 | // Load the specified component. |
| 36 | std::string LoadComponent(const std::string& name); |
| 37 | |
| 38 | private: |
| 39 | // This is a parsed version of the imageloader.json manifest. |
| 40 | struct Manifest { |
| 41 | int manifest_version; |
| 42 | std::vector<uint8_t> image_sha256; |
| 43 | std::vector<uint8_t> params_sha256; |
| 44 | std::string version; |
| 45 | }; |
| 46 | |
| 47 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ECVerify); |
| 48 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ManifestFingerPrint); |
| 49 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidComponent); |
| 50 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyComponentWithBadManifest); |
| 51 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidImage); |
| 52 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidImage); |
| 53 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidHash); |
| 54 | FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ParseManifest); |
| 55 | |
| 56 | // Do the work to verify and mount components. |
| 57 | std::string LoadComponentUtil(const std::string& name); |
| 58 | |
| 59 | // Verify the data with the RSA (PKCS #1 v1.5) signature. |
| 60 | bool ECVerify(const base::StringPiece data, const base::StringPiece sig); |
| 61 | |
| 62 | // Copy the component directory from a user controlled location to an |
| 63 | // imageloader controlled location. Do not copy unless it verifies. |
| 64 | bool CopyComponentDirectory(const base::FilePath& component_path, |
| 65 | const base::FilePath& destination_folder, |
| 66 | const std::string& version); |
| 67 | |
| 68 | // Check the string contents to see if it matches the format of a |
| 69 | // manifest.fingerprint file. |
| 70 | bool IsValidFingerprintFile(const std::string& contents); |
| 71 | |
| 72 | // Verify the imageloader.json manifest file and parse the file information |
| 73 | // out of it. |
| 74 | bool VerifyAndParseManifest(const std::string& manifest_str, |
| 75 | const std::string& signature, Manifest* manifest); |
| 76 | |
| 77 | // Copies files over and checks their hash in the process. The copy fails if |
| 78 | // the hashes do not match. |
| 79 | bool CopyAndHashFile(const base::FilePath& src_path, |
| 80 | const base::FilePath& dest_path, |
| 81 | const std::vector<uint8_t>& known_hash); |
| 82 | |
| 83 | // Check if the client created a manifest.fingerprint, and preserve it. |
| 84 | bool CopyFingerprintFile(const base::FilePath& src, |
| 85 | const base::FilePath& dest); |
| 86 | |
| 87 | // The configuration traits. |
| 88 | ImageLoaderConfig config_; |
| 89 | |
| 90 | DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl); |
| 91 | }; |
| 92 | |
| 93 | } // namespace imageloader |
| 94 | |
| 95 | #endif // IMAGELOADER_IMAGELOADER_UTILITY_H_ |