summaryrefslogtreecommitdiffstats
path: root/chrome/browser/component_updater/component_patcher_operation.cc
blob: 6b86d9fc8a6dc241caa3db8eced617f6e5887f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/component_updater/component_patcher_operation.h"

#include <string>
#include <vector>

#include "base/file_util.h"
#include "base/files/memory_mapped_file.h"
#include "base/json/json_file_value_serializer.h"
#include "base/memory/scoped_handle.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/component_updater/component_patcher.h"
#include "chrome/browser/component_updater/component_updater_service.h"
#include "chrome/common/extensions/extension_constants.h"
#include "crypto/secure_hash.h"
#include "crypto/sha2.h"
#include "crypto/signature_verifier.h"
#include "extensions/common/crx_file.h"
#include "third_party/zlib/google/zip.h"

using crypto::SecureHash;

namespace component_updater {

namespace {

const char kInput[] = "input";
const char kOp[] = "op";
const char kOutput[] = "output";
const char kPatch[] = "patch";
const char kSha256[] = "sha256";

}  // namespace

DeltaUpdateOp* CreateDeltaUpdateOp(base::DictionaryValue* command) {
  std::string operation;
  if (!command->GetString(kOp, &operation))
    return NULL;
  if (operation == "copy")
    return new DeltaUpdateOpCopy();
  else if (operation == "create")
    return new DeltaUpdateOpCreate();
  else if (operation == "bsdiff")
    return new DeltaUpdateOpPatchBsdiff();
  else if (operation == "courgette")
    return new DeltaUpdateOpPatchCourgette();
  return NULL;
}

DeltaUpdateOp::DeltaUpdateOp() {}

DeltaUpdateOp::~DeltaUpdateOp() {}

ComponentUnpacker::Error DeltaUpdateOp::Run(base::DictionaryValue* command_args,
                                            const base::FilePath& input_dir,
                                            const base::FilePath& unpack_dir,
                                            ComponentPatcher* patcher,
                                            ComponentInstaller* installer,
                                            int* error) {
  std::string output_rel_path;
  if (!command_args->GetString(kOutput, &output_rel_path) ||
      !command_args->GetString(kSha256, &output_sha256_))
    return ComponentUnpacker::kDeltaBadCommands;

  output_abs_path_ = unpack_dir.Append(
      base::FilePath::FromUTF8Unsafe(output_rel_path));
  ComponentUnpacker::Error parse_result = DoParseArguments(
      command_args, input_dir, installer);
  if (parse_result != ComponentUnpacker::kNone)
    return parse_result;

  const base::FilePath parent = output_abs_path_.DirName();
  if (!base::DirectoryExists(parent)) {
    if (!base::CreateDirectory(parent))
      return ComponentUnpacker::kIoError;
  }

  ComponentUnpacker::Error run_result = DoRun(patcher, error);
  if (run_result != ComponentUnpacker::kNone)
    return run_result;

  return CheckHash();
}

// Uses the hash as a checksum to confirm that the file now residing in the
// output directory probably has the contents it should.
ComponentUnpacker::Error DeltaUpdateOp::CheckHash() {
  std::vector<uint8> expected_hash;
  if (!base::HexStringToBytes(output_sha256_, &expected_hash) ||
      expected_hash.size() != crypto::kSHA256Length)
    return ComponentUnpacker::kDeltaVerificationFailure;

  base::MemoryMappedFile output_file_mmapped;
  if (!output_file_mmapped.Initialize(output_abs_path_))
    return ComponentUnpacker::kDeltaVerificationFailure;

  uint8 actual_hash[crypto::kSHA256Length] = {0};
  const scoped_ptr<SecureHash> hasher(SecureHash::Create(SecureHash::SHA256));
  hasher->Update(output_file_mmapped.data(), output_file_mmapped.length());
  hasher->Finish(actual_hash, sizeof(actual_hash));
  if (memcmp(actual_hash, &expected_hash[0], sizeof(actual_hash)))
    return ComponentUnpacker::kDeltaVerificationFailure;

  return ComponentUnpacker::kNone;
}

DeltaUpdateOpCopy::DeltaUpdateOpCopy() {}

ComponentUnpacker::Error DeltaUpdateOpCopy::DoParseArguments(
    base::DictionaryValue* command_args,
    const base::FilePath& input_dir,
    ComponentInstaller* installer) {
  std::string input_rel_path;
  if (!command_args->GetString(kInput, &input_rel_path))
    return ComponentUnpacker::kDeltaBadCommands;

  if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
    return ComponentUnpacker::kDeltaMissingExistingFile;

  return ComponentUnpacker::kNone;
}

ComponentUnpacker::Error DeltaUpdateOpCopy::DoRun(ComponentPatcher*,
                                                  int* error) {
  *error = 0;
  if (!base::CopyFile(input_abs_path_, output_abs_path_))
    return ComponentUnpacker::kDeltaOperationFailure;

  return ComponentUnpacker::kNone;
}

DeltaUpdateOpCreate::DeltaUpdateOpCreate() {}

ComponentUnpacker::Error DeltaUpdateOpCreate::DoParseArguments(
    base::DictionaryValue* command_args,
    const base::FilePath& input_dir,
    ComponentInstaller* installer) {
  std::string patch_rel_path;
  if (!command_args->GetString(kPatch, &patch_rel_path))
    return ComponentUnpacker::kDeltaBadCommands;

  patch_abs_path_ = input_dir.Append(
      base::FilePath::FromUTF8Unsafe(patch_rel_path));

  return ComponentUnpacker::kNone;
}

ComponentUnpacker::Error DeltaUpdateOpCreate::DoRun(ComponentPatcher*,
                                                    int* error) {
  *error = 0;
  if (!base::Move(patch_abs_path_, output_abs_path_))
    return ComponentUnpacker::kDeltaOperationFailure;

  return ComponentUnpacker::kNone;
}

DeltaUpdateOpPatchBsdiff::DeltaUpdateOpPatchBsdiff() {}

ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoParseArguments(
    base::DictionaryValue* command_args,
    const base::FilePath& input_dir,
    ComponentInstaller* installer) {
  std::string patch_rel_path;
  std::string input_rel_path;
  if (!command_args->GetString(kPatch, &patch_rel_path) ||
      !command_args->GetString(kInput, &input_rel_path))
    return ComponentUnpacker::kDeltaBadCommands;

  if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
    return ComponentUnpacker::kDeltaMissingExistingFile;

  patch_abs_path_ = input_dir.Append(
      base::FilePath::FromUTF8Unsafe(patch_rel_path));

  return ComponentUnpacker::kNone;
}

ComponentUnpacker::Error DeltaUpdateOpPatchBsdiff::DoRun(
    ComponentPatcher* patcher,
    int* error) {
  *error = 0;
  return patcher->Patch(ComponentPatcher::kPatchTypeBsdiff,
                        input_abs_path_,
                        patch_abs_path_,
                        output_abs_path_,
                        error);
}

DeltaUpdateOpPatchCourgette::DeltaUpdateOpPatchCourgette() {}

ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoParseArguments(
    base::DictionaryValue* command_args,
    const base::FilePath& input_dir,
    ComponentInstaller* installer) {
  std::string patch_rel_path;
  std::string input_rel_path;
  if (!command_args->GetString(kPatch, &patch_rel_path) ||
      !command_args->GetString(kInput, &input_rel_path))
    return ComponentUnpacker::kDeltaBadCommands;

  if (!installer->GetInstalledFile(input_rel_path, &input_abs_path_))
    return ComponentUnpacker::kDeltaMissingExistingFile;

  patch_abs_path_ = input_dir.Append(
      base::FilePath::FromUTF8Unsafe(patch_rel_path));

  return ComponentUnpacker::kNone;
}

ComponentUnpacker::Error DeltaUpdateOpPatchCourgette::DoRun(
    ComponentPatcher* patcher,
    int* error) {
  *error = 0;
  return patcher->Patch(ComponentPatcher::kPatchTypeCourgette,
                        input_abs_path_,
                        patch_abs_path_,
                        output_abs_path_,
                        error);
}

}  // namespace component_updater