OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/component_updater/component_unpacker.h" | 5 #include "components/component_updater/component_unpacker.h" |
6 | 6 |
| 7 #include <stdint.h> |
7 #include <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
13 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
14 #include "base/json/json_file_value_serializer.h" | 15 #include "base/json/json_file_value_serializer.h" |
15 #include "base/location.h" | 16 #include "base/location.h" |
16 #include "base/logging.h" | 17 #include "base/logging.h" |
(...skipping 26 matching lines...) Expand all Loading... |
43 if (len < sizeof(header)) | 44 if (len < sizeof(header)) |
44 return; | 45 return; |
45 | 46 |
46 crx_file::CrxFile::Error error; | 47 crx_file::CrxFile::Error error; |
47 scoped_ptr<crx_file::CrxFile> crx( | 48 scoped_ptr<crx_file::CrxFile> crx( |
48 crx_file::CrxFile::Parse(header, &error)); | 49 crx_file::CrxFile::Parse(header, &error)); |
49 if (!crx.get()) | 50 if (!crx.get()) |
50 return; | 51 return; |
51 is_delta_ = crx_file::CrxFile::HeaderIsDelta(header); | 52 is_delta_ = crx_file::CrxFile::HeaderIsDelta(header); |
52 | 53 |
53 std::vector<uint8> key(header.key_size); | 54 std::vector<uint8_t> key(header.key_size); |
54 len = fread(&key[0], sizeof(uint8), header.key_size, crx_file); | 55 len = fread(&key[0], sizeof(uint8_t), header.key_size, crx_file); |
55 if (len < header.key_size) | 56 if (len < header.key_size) |
56 return; | 57 return; |
57 | 58 |
58 std::vector<uint8> signature(header.signature_size); | 59 std::vector<uint8_t> signature(header.signature_size); |
59 len = fread(&signature[0], sizeof(uint8), header.signature_size, crx_file); | 60 len = |
| 61 fread(&signature[0], sizeof(uint8_t), header.signature_size, crx_file); |
60 if (len < header.signature_size) | 62 if (len < header.signature_size) |
61 return; | 63 return; |
62 | 64 |
63 crypto::SignatureVerifier verifier; | 65 crypto::SignatureVerifier verifier; |
64 if (!verifier.VerifyInit(crx_file::kSignatureAlgorithm, | 66 if (!verifier.VerifyInit(crx_file::kSignatureAlgorithm, |
65 base::checked_cast<int>( | 67 base::checked_cast<int>( |
66 sizeof(crx_file::kSignatureAlgorithm)), | 68 sizeof(crx_file::kSignatureAlgorithm)), |
67 &signature[0], | 69 &signature[0], |
68 base::checked_cast<int>(signature.size()), | 70 base::checked_cast<int>(signature.size()), |
69 &key[0], | 71 &key[0], |
70 base::checked_cast<int>(key.size()))) { | 72 base::checked_cast<int>(key.size()))) { |
71 // Signature verification initialization failed. This is most likely | 73 // Signature verification initialization failed. This is most likely |
72 // caused by a public key in the wrong format (should encode algorithm). | 74 // caused by a public key in the wrong format (should encode algorithm). |
73 return; | 75 return; |
74 } | 76 } |
75 | 77 |
76 const size_t kBufSize = 8 * 1024; | 78 const size_t kBufSize = 8 * 1024; |
77 scoped_ptr<uint8[]> buf(new uint8[kBufSize]); | 79 scoped_ptr<uint8_t[]> buf(new uint8_t[kBufSize]); |
78 while ((len = fread(buf.get(), 1, kBufSize, crx_file)) > 0) | 80 while ((len = fread(buf.get(), 1, kBufSize, crx_file)) > 0) |
79 verifier.VerifyUpdate(buf.get(), base::checked_cast<int>(len)); | 81 verifier.VerifyUpdate(buf.get(), base::checked_cast<int>(len)); |
80 | 82 |
81 if (!verifier.VerifyFinal()) | 83 if (!verifier.VerifyFinal()) |
82 return; | 84 return; |
83 | 85 |
84 public_key_.swap(key); | 86 public_key_.swap(key); |
85 valid_ = true; | 87 valid_ = true; |
86 } | 88 } |
87 | 89 |
88 bool valid() const { return valid_; } | 90 bool valid() const { return valid_; } |
89 | 91 |
90 bool is_delta() const { return is_delta_; } | 92 bool is_delta() const { return is_delta_; } |
91 | 93 |
92 const std::vector<uint8>& public_key() const { return public_key_; } | 94 const std::vector<uint8_t>& public_key() const { return public_key_; } |
93 | 95 |
94 private: | 96 private: |
95 bool valid_; | 97 bool valid_; |
96 bool is_delta_; | 98 bool is_delta_; |
97 std::vector<uint8> public_key_; | 99 std::vector<uint8_t> public_key_; |
98 }; | 100 }; |
99 | 101 |
100 } // namespace | 102 } // namespace |
101 | 103 |
102 ComponentUnpacker::ComponentUnpacker( | 104 ComponentUnpacker::ComponentUnpacker( |
103 const std::vector<uint8>& pk_hash, | 105 const std::vector<uint8_t>& pk_hash, |
104 const base::FilePath& path, | 106 const base::FilePath& path, |
105 const std::string& fingerprint, | 107 const std::string& fingerprint, |
106 ComponentInstaller* installer, | 108 ComponentInstaller* installer, |
107 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher, | 109 scoped_refptr<OutOfProcessPatcher> out_of_process_patcher, |
108 scoped_refptr<base::SequencedTaskRunner> task_runner) | 110 scoped_refptr<base::SequencedTaskRunner> task_runner) |
109 : pk_hash_(pk_hash), | 111 : pk_hash_(pk_hash), |
110 path_(path), | 112 path_(path), |
111 is_delta_(false), | 113 is_delta_(false), |
112 fingerprint_(fingerprint), | 114 fingerprint_(fingerprint), |
113 installer_(installer), | 115 installer_(installer), |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 file.reset(); | 166 file.reset(); |
165 if (!validator.valid()) { | 167 if (!validator.valid()) { |
166 error_ = kInvalidFile; | 168 error_ = kInvalidFile; |
167 return false; | 169 return false; |
168 } | 170 } |
169 is_delta_ = validator.is_delta(); | 171 is_delta_ = validator.is_delta(); |
170 | 172 |
171 // File is valid and the digital signature matches. Now make sure | 173 // File is valid and the digital signature matches. Now make sure |
172 // the public key hash matches the expected hash. If they do we fully | 174 // the public key hash matches the expected hash. If they do we fully |
173 // trust this CRX. | 175 // trust this CRX. |
174 uint8 hash[32] = {}; | 176 uint8_t hash[32] = {}; |
175 scoped_ptr<SecureHash> sha256(SecureHash::Create(SecureHash::SHA256)); | 177 scoped_ptr<SecureHash> sha256(SecureHash::Create(SecureHash::SHA256)); |
176 sha256->Update(&(validator.public_key()[0]), validator.public_key().size()); | 178 sha256->Update(&(validator.public_key()[0]), validator.public_key().size()); |
177 sha256->Finish(hash, arraysize(hash)); | 179 sha256->Finish(hash, arraysize(hash)); |
178 | 180 |
179 if (!std::equal(pk_hash_.begin(), pk_hash_.end(), hash)) { | 181 if (!std::equal(pk_hash_.begin(), pk_hash_.end(), hash)) { |
180 VLOG(1) << "Hash mismatch: " << path_.value(); | 182 VLOG(1) << "Hash mismatch: " << path_.value(); |
181 error_ = kInvalidId; | 183 error_ = kInvalidId; |
182 return false; | 184 return false; |
183 } | 185 } |
184 VLOG(1) << "Verification successful: " << path_.value(); | 186 VLOG(1) << "Verification successful: " << path_.value(); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 base::DeleteFile(unpack_diff_path_, true); | 279 base::DeleteFile(unpack_diff_path_, true); |
278 if (!unpack_path_.empty()) | 280 if (!unpack_path_.empty()) |
279 base::DeleteFile(unpack_path_, true); | 281 base::DeleteFile(unpack_path_, true); |
280 callback_.Run(error_, extended_error_); | 282 callback_.Run(error_, extended_error_); |
281 } | 283 } |
282 | 284 |
283 ComponentUnpacker::~ComponentUnpacker() { | 285 ComponentUnpacker::~ComponentUnpacker() { |
284 } | 286 } |
285 | 287 |
286 } // namespace component_updater | 288 } // namespace component_updater |
OLD | NEW |