1 // Copyright 2014 The Chromium 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 #ifndef COMPONENTS_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ 6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ 7 8 #include <stdint.h> 9 #include <string> 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/files/file_path.h" 14 #include "base/json/json_file_value_serializer.h" 15 #include "base/macros.h" 16 #include "base/memory/ref_counted.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/sequenced_task_runner.h" 19 20 namespace component_updater { 21 22 class ComponentInstaller; 23 class ComponentPatcher; 24 class OutOfProcessPatcher; 25 26 // Deserializes the CRX manifest. The top level must be a dictionary. 27 scoped_ptr<base::DictionaryValue> ReadManifest( 28 const base::FilePath& unpack_path); 29 30 // In charge of unpacking the component CRX package and verifying that it is 31 // well formed and the cryptographic signature is correct. If there is no 32 // error the component specific installer will be invoked to proceed with 33 // the component installation or update. 34 // 35 // This class should be used only by the component updater. It is inspired by 36 // and overlaps with code in the extension's SandboxedUnpacker. 37 // The main differences are: 38 // - The public key hash is full SHA256. 39 // - Does not use a sandboxed unpacker. A valid component is fully trusted. 40 // - The manifest can have different attributes and resources are not 41 // transcoded. 42 // 43 // If the CRX is a delta CRX, the flow is: 44 // [ComponentUpdater] [ComponentPatcher] 45 // Unpack 46 // \_ Verify 47 // \_ Unzip 48 // \_ BeginPatching ---> DifferentialUpdatePatch 49 // ... 50 // EndPatching <------------ ... 51 // \_ Install 52 // \_ Finish 53 // 54 // For a full CRX, the flow is: 55 // [ComponentUpdater] 56 // Unpack 57 // \_ Verify 58 // \_ Unzip 59 // \_ BeginPatching 60 // | 61 // V 62 // EndPatching 63 // \_ Install 64 // \_ Finish 65 // 66 // In both cases, if there is an error at any point, the remaining steps will 67 // be skipped and Finish will be called. 68 class ComponentUnpacker : public base::RefCountedThreadSafe<ComponentUnpacker> { 69 public: 70 // Possible error conditions. 71 // Add only to the bottom of this enum; the order must be kept stable. 72 enum Error { 73 kNone, 74 kInvalidParams, 75 kInvalidFile, 76 kUnzipPathError, 77 kUnzipFailed, 78 kNoManifest, 79 kBadManifest, 80 kBadExtension, 81 kInvalidId, 82 kInstallerError, 83 kIoError, 84 kDeltaVerificationFailure, 85 kDeltaBadCommands, 86 kDeltaUnsupportedCommand, 87 kDeltaOperationFailure, 88 kDeltaPatchProcessFailure, 89 kDeltaMissingExistingFile, 90 kFingerprintWriteFailed, 91 }; 92 93 typedef base::Callback<void(Error, int)> Callback; 94 95 // Constructs an unpacker for a specific component unpacking operation. 96 // |pk_hash| is the expected/ public key SHA256 hash. |path| is the current 97 // location of the CRX. 98 ComponentUnpacker(const std::vector<uint8_t>& pk_hash, 99 const base::FilePath& path, 100 const std::string& fingerprint, 101 ComponentInstaller* installer, 102 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher, 103 scoped_refptr<base::SequencedTaskRunner> task_runner); 104 105 // Begins the actual unpacking of the files. May invoke a patcher if the 106 // package is a differential update. Calls |callback| with the result. 107 void Unpack(const Callback& callback); 108 109 private: 110 friend class base::RefCountedThreadSafe<ComponentUnpacker>; 111 112 virtual ~ComponentUnpacker(); 113 114 bool UnpackInternal(); 115 116 // The first step of unpacking is to verify the file. Returns false if an 117 // error is encountered, the file is malformed, or the file is incorrectly 118 // signed. 119 bool Verify(); 120 121 // The second step of unpacking is to unzip. Returns false if an error 122 // occurs as part of unzipping. 123 bool Unzip(); 124 125 // The third step is to optionally patch files - this is a no-op for full 126 // (non-differential) updates. This step is asynchronous. Returns false if an 127 // error is encountered. 128 bool BeginPatching(); 129 130 // When patching is complete, EndPatching is called before moving on to step 131 // four. 132 void EndPatching(Error error, int extended_error); 133 134 // The fourth step is to install the unpacked component. 135 void Install(); 136 137 // The final step is to do clean-up for things that can't be tidied as we go. 138 // If there is an error at any step, the remaining steps are skipped and 139 // and Finish is called. 140 // Finish is responsible for calling the callback provided in Start(). 141 void Finish(); 142 143 std::vector<uint8_t> pk_hash_; 144 base::FilePath path_; 145 base::FilePath unpack_path_; 146 base::FilePath unpack_diff_path_; 147 bool is_delta_; 148 std::string fingerprint_; 149 scoped_refptr<ComponentPatcher> patcher_; 150 ComponentInstaller* installer_; 151 Callback callback_; 152 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher_; 153 Error error_; 154 int extended_error_; 155 scoped_refptr<base::SequencedTaskRunner> task_runner_; 156 157 DISALLOW_COPY_AND_ASSIGN(ComponentUnpacker); 158 }; 159 160 } // namespace component_updater 161 162 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_ 163