blob: 748abc20f3e87396a473f95d36de5484546a6470 [file] [log] [blame]
Greg Kerr019d59c2016-11-17 14:28:49 -08001// 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
5// This class abstracts away the details about the layout of the component dir
6// and how to verify/copy it.
7//
8// A component directory contains the following files:
9//
10// imageloader.json Manifest JSON file
11// imageloader.sig.1 Manifest signature
12// manifest.fingerprint Fingerprint file (used for delta updates)
13// image.squash squashfs image
14// table dm-verity table, including parameters
15#ifndef IMAGELOADER_COMPONENT_H_
16#define IMAGELOADER_COMPONENT_H_
17
Eric Caruso26a91442017-10-25 16:05:40 -070018#include <map>
Eric Carusocbe1c5c2017-03-15 14:21:08 -070019#include <memory>
Greg Kerr019d59c2016-11-17 14:28:49 -080020#include <string>
21#include <vector>
22
23#include <base/files/file.h>
24#include <base/files/file_path.h>
25#include <base/gtest_prod_util.h>
26#include <base/macros.h>
27#include <crypto/secure_hash.h>
28
Greg Kerr09f06de2018-02-16 15:32:07 -080029#include "imageloader/helper_process_proxy.h"
Ben Chan045849f2017-12-18 17:27:07 -080030#include "imageloader/imageloader_impl.h"
Xiaochu Liue61e1d62018-11-12 13:20:09 -080031#include "imageloader/manifest.h"
Greg Kerr019d59c2016-11-17 14:28:49 -080032
33namespace imageloader {
34
35// The permissions that the component update directory must use.
36constexpr int kComponentDirPerms = 0755;
37// The permissions that files in the component should have.
38constexpr int kComponentFilePerms = 0644;
Xiaochu Liu83a51ac2018-06-22 13:48:37 -070039// The maximum size of any file to read into memory.
40constexpr size_t kMaximumFilesize = 4096 * 10;
Greg Kerr019d59c2016-11-17 14:28:49 -080041
42class Component {
43 public:
Eric Carusocbe1c5c2017-03-15 14:21:08 -070044 // Creates a Component. Returns nullptr if initialization and verification
45 // fails.
Eric Caruso0b79bc82017-03-21 13:44:34 -070046 static std::unique_ptr<Component> Create(const base::FilePath& component_dir,
47 const Keys& public_keys);
Greg Kerr019d59c2016-11-17 14:28:49 -080048
49 // Copies the component into |dest_dir|. |dest_dir| must already exist. In
50 // order to be robust against files being modified on disk, this function
51 // verifies the files it copies against the manifest (which is loaded into
52 // memory).
53 bool CopyTo(const base::FilePath& dest_dir);
54
55 // Mounts the component into |mount_point|. |mount_point| must already exist.
Greg Kerr09f06de2018-02-16 15:32:07 -080056 bool Mount(HelperProcessProxy* proxy, const base::FilePath& mount_point);
Greg Kerr019d59c2016-11-17 14:28:49 -080057
58 // Return a reference to the parsed manifest object, which is stored in
59 // memory.
Xiaochu Liue61e1d62018-11-12 13:20:09 -080060 const Manifest& manifest();
Greg Kerr019d59c2016-11-17 14:28:49 -080061
62 private:
Eric Carusocbe1c5c2017-03-15 14:21:08 -070063 // Constructs a Component. We want to avoid using this where possible since
64 // you need to load the manifest before doing anything anyway, so use the
65 // static factory method above.
Eric Caruso089bbff2017-03-21 11:34:15 -070066 Component(const base::FilePath& component_dir, int key_number);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090067 Component(const Component&) = delete;
68 Component& operator=(const Component&) = delete;
Eric Carusocbe1c5c2017-03-15 14:21:08 -070069
70 // Loads and verifies the manfiest. Returns false on failure. |public_key| is
71 // the public key used to check the manifest signature.
Greg Kerr019d59c2016-11-17 14:28:49 -080072 bool LoadManifest(const std::vector<uint8_t>& public_key);
Amin Hassani17a185b2021-02-10 12:07:57 -080073 // Same as the above function, but it skips the verification.
74 bool LoadManifestWithoutVerifyingKeyForTestingOnly();
75
Colin Howesad6271a2018-11-21 15:36:05 -080076 bool CopyComponentFile(const base::FilePath& src,
77 const base::FilePath& dest,
Eric Caruso355e37c2017-03-15 14:31:41 -070078 const std::vector<uint8_t>& expected_hash);
Greg Kerr019d59c2016-11-17 14:28:49 -080079 // This reads the contents of |file|, hashes it with |sha256|, and if
80 // |out_file| is not null, copies it into |out_file|.
Colin Howesad6271a2018-11-21 15:36:05 -080081 bool ReadHashAndCopyFile(base::File* file,
82 std::vector<uint8_t>* sha256,
Greg Kerr019d59c2016-11-17 14:28:49 -080083 base::File* out_file);
84 // Copies the fingerprint file that Chrome users for delta updates.
85 bool CopyFingerprintFile(const base::FilePath& src,
86 const base::FilePath& dest);
Greg Kerr04c1cee2020-10-15 14:08:44 +000087 // Validate the fingerprint file.
Eric Carusocbe1c5c2017-03-15 14:21:08 -070088 static bool IsValidFingerprintFile(const std::string& contents);
Greg Kerr019d59c2016-11-17 14:28:49 -080089
90 FRIEND_TEST_ALL_PREFIXES(ComponentTest, IsValidFingerprintFile);
91 FRIEND_TEST_ALL_PREFIXES(ComponentTest, CopyValidImage);
92
Greg Kerr019d59c2016-11-17 14:28:49 -080093 const base::FilePath component_dir_;
Eric Caruso9588e642017-04-07 15:18:45 -070094 size_t key_number_;
Greg Kerr019d59c2016-11-17 14:28:49 -080095 std::string manifest_raw_;
96 std::string manifest_sig_;
Xiaochu Liue61e1d62018-11-12 13:20:09 -080097 Manifest manifest_;
Greg Kerr019d59c2016-11-17 14:28:49 -080098};
99
100} // namespace imageloader
101
102#endif // IMAGELOADER_COMPONENT_H_