blob: dd443f34470b7ab394141164b5cd6dec413b858b [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
50 private:
51 // This is a parsed version of the imageloader.json manifest.
52 struct Manifest {
53 int manifest_version;
54 std::vector<uint8_t> image_sha256;
55 std::vector<uint8_t> params_sha256;
56 std::string version;
57 };
58
59 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ECVerify);
60 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ManifestFingerPrint);
61 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidComponent);
62 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyComponentWithBadManifest);
63 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyValidImage);
64 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidImage);
65 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, CopyInvalidHash);
66 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, ParseManifest);
Greg Kerr89be05f2016-07-27 10:40:32 -070067 FRIEND_TEST_ALL_PREFIXES(ImageLoaderTest, MountValidImage);
Greg Kerra6c0c522016-07-25 11:15:31 -070068
69 // Verify the data with the RSA (PKCS #1 v1.5) signature.
70 bool ECVerify(const base::StringPiece data, const base::StringPiece sig);
71
72 // Copy the component directory from a user controlled location to an
73 // imageloader controlled location. Do not copy unless it verifies.
74 bool CopyComponentDirectory(const base::FilePath& component_path,
75 const base::FilePath& destination_folder,
76 const std::string& version);
77
78 // Check the string contents to see if it matches the format of a
79 // manifest.fingerprint file.
80 bool IsValidFingerprintFile(const std::string& contents);
81
82 // Verify the imageloader.json manifest file and parse the file information
83 // out of it.
84 bool VerifyAndParseManifest(const std::string& manifest_str,
85 const std::string& signature, Manifest* manifest);
86
87 // Copies files over and checks their hash in the process. The copy fails if
88 // the hashes do not match.
89 bool CopyAndHashFile(const base::FilePath& src_path,
90 const base::FilePath& dest_path,
91 const std::vector<uint8_t>& known_hash);
92
93 // Check if the client created a manifest.fingerprint, and preserve it.
94 bool CopyFingerprintFile(const base::FilePath& src,
95 const base::FilePath& dest);
96
Greg Kerr89be05f2016-07-27 10:40:32 -070097 // if |manifest| or |sig| are not null, they are set to the manifest contents
98 // and the signature contents.
99 bool GetAndVerifyManifest(const std::string& component_name,
100 const base::FilePath& component_path,
101 Manifest* manifest, std::string* manifest_str,
102 std::string* manifest_sig);
103
Greg Kerra6c0c522016-07-25 11:15:31 -0700104 // The configuration traits.
105 ImageLoaderConfig config_;
106
107 DISALLOW_COPY_AND_ASSIGN(ImageLoaderImpl);
108};
109
110} // namespace imageloader
111
112#endif // IMAGELOADER_IMAGELOADER_UTILITY_H_