Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: components/crx_file/crx_verifier.cc

Issue 2888853003: Expand CRX verifier to verify CRX₃ files. (Closed)
Patch Set: Through #5 Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/crx_file/crx3.proto ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/crx_file/crx_verifier.h" 5 #include "components/crx_file/crx_verifier.h"
6 6
7 #include <cstring> 7 #include <cstring>
8 #include <iterator>
8 #include <memory> 9 #include <memory>
10 #include <set>
11 #include <utility>
9 12
10 #include "base/base64.h" 13 #include "base/base64.h"
14 #include "base/bind.h"
15 #include "base/callback.h"
11 #include "base/files/file.h" 16 #include "base/files/file.h"
12 #include "base/files/file_path.h" 17 #include "base/files/file_path.h"
13 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/strings/string_number_conversions.h"
14 #include "components/crx_file/crx2_file.h" 20 #include "components/crx_file/crx2_file.h"
21 #include "components/crx_file/crx3.pb.h"
15 #include "components/crx_file/id_util.h" 22 #include "components/crx_file/id_util.h"
16 #include "crypto/secure_hash.h" 23 #include "crypto/secure_hash.h"
17 #include "crypto/secure_util.h" 24 #include "crypto/secure_util.h"
18 #include "crypto/sha2.h" 25 #include "crypto/sha2.h"
19 #include "crypto/signature_verifier.h" 26 #include "crypto/signature_verifier.h"
20 27
21 namespace crx_file { 28 namespace crx_file {
22 29
23 namespace { 30 namespace {
24 31
25 // The maximum size the crx2 parser will tolerate for a public key. 32 // The maximum size the Crx2 parser will tolerate for a public key.
26 constexpr uint32_t kMaxPublicKeySize = 1 << 16; 33 constexpr uint32_t kMaxPublicKeySize = 1 << 16;
27 34
28 // The maximum size the crx2 parser will tolerate for a signature. 35 // The maximum size the Crx2 parser will tolerate for a signature.
29 constexpr uint32_t kMaxSignatureSize = 1 << 16; 36 constexpr uint32_t kMaxSignatureSize = 1 << 16;
30 37
38 // The maximum size the Crx3 parser will tolerate for a header.
39 constexpr uint32_t kMaxHeaderSize = 1 << 18;
40
41 // The context for Crx3 signing, encoded in UTF8.
42 constexpr unsigned char kSignatureContext[] = u8"CRX3 SignedData\0";
43
44 // The SHA256 hash of the "ecdsa_2017_public" Crx3 key.
45 constexpr uint8_t kPublisherKeyHash[] = {
46 0x61, 0xf7, 0xf2, 0xa6, 0xbf, 0xcf, 0x74, 0xcd, 0x0b, 0xc1, 0xfe,
47 0x24, 0x97, 0xcc, 0x9b, 0x04, 0x25, 0x4c, 0x65, 0x8f, 0x79, 0xf2,
48 0x14, 0x53, 0x92, 0x86, 0x7e, 0xa8, 0x36, 0x63, 0x67, 0xcf};
49
31 int ReadAndHashBuffer(uint8_t* buffer, 50 int ReadAndHashBuffer(uint8_t* buffer,
32 int length, 51 int length,
33 base::File* file, 52 base::File* file,
34 crypto::SecureHash* hash) { 53 crypto::SecureHash* hash) {
35 static_assert(sizeof(char) == sizeof(uint8_t), "Unsupported char size."); 54 static_assert(sizeof(char) == sizeof(uint8_t), "Unsupported char size.");
36 int read = file->ReadAtCurrentPos(reinterpret_cast<char*>(buffer), length); 55 int read = file->ReadAtCurrentPos(reinterpret_cast<char*>(buffer), length);
37 hash->Update(buffer, read); 56 hash->Update(buffer, read);
38 return read; 57 return read;
39 } 58 }
40 59
41 // Returns UINT32_MAX in the case of an unexpected EOF or read error, else 60 // Returns UINT32_MAX in the case of an unexpected EOF or read error, else
42 // returns the read uint32. 61 // returns the read uint32.
43 uint32_t ReadAndHashLittleEndianUInt32(base::File* file, 62 uint32_t ReadAndHashLittleEndianUInt32(base::File* file,
44 crypto::SecureHash* hash) { 63 crypto::SecureHash* hash) {
45 uint8_t buffer[4] = {}; 64 uint8_t buffer[4] = {};
46 if (ReadAndHashBuffer(buffer, 4, file, hash) != 4) 65 if (ReadAndHashBuffer(buffer, 4, file, hash) != 4)
47 return UINT32_MAX; 66 return UINT32_MAX;
48 return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]; 67 return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
49 } 68 }
50 69
70 bool ReadHashAndVerifyArchive(
71 base::File* file,
72 crypto::SecureHash* hash,
73 const std::vector<std::unique_ptr<crypto::SignatureVerifier>>& verifiers) {
74 uint8_t buffer[1 << 12] = {};
75 size_t len = 0;
76 while ((len = ReadAndHashBuffer(buffer, arraysize(buffer), file, hash)) > 0) {
77 for (auto& verifier : verifiers)
78 verifier->VerifyUpdate(buffer, len);
79 }
80 for (auto& verifier : verifiers) {
81 if (!verifier->VerifyFinal())
82 return false;
83 }
84 return true;
85 }
86
51 VerifierResult VerifyCrx3( 87 VerifierResult VerifyCrx3(
52 base::File* file, 88 base::File* file,
53 crypto::SecureHash* hash, 89 crypto::SecureHash* hash,
54 const std::vector<std::vector<uint8_t>>& required_key_hashes, 90 const std::vector<std::vector<uint8_t>>& required_key_hashes,
55 std::string* public_key, 91 std::string* public_key,
56 std::string* crx_id) { 92 std::string* crx_id,
57 // Crx3 files are not yet supported - treat this as a malformed header. 93 bool require_publisher_key) {
58 return VerifierResult::ERROR_HEADER_INVALID; 94 // Parse header data.
95 const uint32_t header_size = ReadAndHashLittleEndianUInt32(file, hash);
96 if (header_size > kMaxHeaderSize)
97 return VerifierResult::ERROR_HEADER_INVALID;
98 std::vector<uint8_t> header_bytes(header_size);
99 if (ReadAndHashBuffer(header_bytes.data(), header_size, file, hash) !=
100 static_cast<int>(header_size))
101 return VerifierResult::ERROR_HEADER_INVALID;
102 CrxFileHeader header;
103 if (!header.ParseFromArray(header_bytes.data(), header_size))
104 return VerifierResult::ERROR_HEADER_INVALID;
105
106 // Parse signed header data.
107 const std::string& signed_header_data_str = header.signed_header_data();
108 SignedData signed_header_data;
109 if (!signed_header_data.ParseFromString(signed_header_data_str))
110 return VerifierResult::ERROR_HEADER_INVALID;
111 const std::string& crx_id_encoded = signed_header_data.crx_id();
112 const std::string declared_crx_id =
113 base::HexEncode(crx_id_encoded.data(), crx_id_encoded.size());
114
115 // Compute the little-endian signed header data size.
116 const int signed_header_size = signed_header_data_str.size();
117 const uint8_t header_size_octets[] = {
118 signed_header_size, signed_header_size >> 8, signed_header_size >> 16,
119 signed_header_size >> 24};
120
121 // Set up the required keys.
122 std::set<std::vector<uint8_t>> required_key_set(required_key_hashes.begin(),
123 required_key_hashes.end());
124 if (require_publisher_key) {
125 const std::vector<uint8_t> publisher_key(std::begin(kPublisherKeyHash),
126 std::end(kPublisherKeyHash));
127 required_key_set.insert(publisher_key);
128 }
129 std::string public_key_bytes;
130 std::vector<std::unique_ptr<crypto::SignatureVerifier>> verifiers;
131 const std::vector<
132 std::pair<std::function<const google::protobuf::RepeatedPtrField<
133 AsymmetricKeyProof>&()>,
134 crypto::SignatureVerifier::SignatureAlgorithm>>
135 proof_types = {
136 std::make_pair([&header] { return header.sha256_with_rsa(); },
137 crypto::SignatureVerifier::RSA_PKCS1_SHA256),
138 std::make_pair([&header] { return header.sha256_with_ecdsa(); },
139 crypto::SignatureVerifier::ECDSA_SHA256)};
140 // Initialize all verifiers and update them with the contexts / prefixes.
141 for (const auto& proof_type : proof_types) {
142 for (const auto& proof : proof_type.first()) {
143 const std::string& key = proof.public_key();
144 const std::string& sig = proof.signature();
145 if (id_util::GenerateId(key) == declared_crx_id)
146 public_key_bytes = key;
147 std::vector<uint8_t> key_hash(crypto::kSHA256Length);
148 crypto::SHA256HashString(key, key_hash.data(), key_hash.size());
149 required_key_set.erase(key_hash);
150 std::unique_ptr<crypto::SignatureVerifier> v =
151 base::MakeUnique<crypto::SignatureVerifier>();
152 static_assert(sizeof(unsigned char) == sizeof(uint8_t),
153 "Unsupported char size.");
154 if (!v->VerifyInit(
155 proof_type.second, reinterpret_cast<const uint8_t*>(sig.data()),
156 sig.size(), reinterpret_cast<const uint8_t*>(key.data()),
157 key.size()))
158 return VerifierResult::ERROR_SIGNATURE_INITIALIZATION_FAILED;
159 v->VerifyUpdate(kSignatureContext, arraysize(kSignatureContext));
160 v->VerifyUpdate(header_size_octets, arraysize(header_size_octets));
161 v->VerifyUpdate(
162 reinterpret_cast<const uint8_t*>(signed_header_data_str.data()),
163 signed_header_data_str.size());
164 verifiers.push_back(std::move(v));
165 }
166 }
167 if (!public_key_bytes.empty() || !required_key_set.empty())
168 return VerifierResult::ERROR_REQUIRED_PROOF_MISSING;
169
170 // Verify the entire archive.
171 if (!ReadHashAndVerifyArchive(file, hash, verifiers))
172 return VerifierResult::ERROR_SIGNATURE_VERIFICATION_FAILED;
173
174 if (public_key)
175 base::Base64Encode(public_key_bytes, public_key);
176 if (crx_id)
177 *crx_id = declared_crx_id;
178 return VerifierResult::OK_FULL;
59 } 179 }
60 180
61 VerifierResult VerifyCrx2( 181 VerifierResult VerifyCrx2(
62 base::File* file, 182 base::File* file,
63 crypto::SecureHash* hash, 183 crypto::SecureHash* hash,
64 const std::vector<std::vector<uint8_t>>& required_key_hashes, 184 const std::vector<std::vector<uint8_t>>& required_key_hashes,
65 std::string* public_key, 185 std::string* public_key,
66 std::string* crx_id) { 186 std::string* crx_id) {
67 const uint32_t key_size = ReadAndHashLittleEndianUInt32(file, hash); 187 const uint32_t key_size = ReadAndHashLittleEndianUInt32(file, hash);
68 if (key_size > kMaxPublicKeySize) 188 if (key_size > kMaxPublicKeySize)
69 return VerifierResult::ERROR_HEADER_INVALID; 189 return VerifierResult::ERROR_HEADER_INVALID;
70 const uint32_t sig_size = ReadAndHashLittleEndianUInt32(file, hash); 190 const uint32_t sig_size = ReadAndHashLittleEndianUInt32(file, hash);
71 if (sig_size > kMaxSignatureSize) 191 if (sig_size > kMaxSignatureSize)
72 return VerifierResult::ERROR_HEADER_INVALID; 192 return VerifierResult::ERROR_HEADER_INVALID;
73 std::vector<uint8_t> key(key_size); 193 std::vector<uint8_t> key(key_size);
74 if (ReadAndHashBuffer(key.data(), key_size, file, hash) != 194 if (ReadAndHashBuffer(key.data(), key_size, file, hash) !=
75 static_cast<int>(key_size)) 195 static_cast<int>(key_size))
76 return VerifierResult::ERROR_HEADER_INVALID; 196 return VerifierResult::ERROR_HEADER_INVALID;
77 for (const auto& expected_hash : required_key_hashes) { 197 for (const auto& expected_hash : required_key_hashes) {
78 // In practice we expect zero or one key_hashes_ for CRX2 files. 198 // In practice we expect zero or one key_hashes_ for Crx2 files.
79 std::vector<uint8_t> hash(crypto::kSHA256Length); 199 std::vector<uint8_t> hash(crypto::kSHA256Length);
80 std::unique_ptr<crypto::SecureHash> sha256 = 200 std::unique_ptr<crypto::SecureHash> sha256 =
81 crypto::SecureHash::Create(crypto::SecureHash::SHA256); 201 crypto::SecureHash::Create(crypto::SecureHash::SHA256);
82 sha256->Update(key.data(), key.size()); 202 sha256->Update(key.data(), key.size());
83 sha256->Finish(hash.data(), hash.size()); 203 sha256->Finish(hash.data(), hash.size());
84 if (hash != expected_hash) 204 if (hash != expected_hash)
85 return VerifierResult::ERROR_REQUIRED_PROOF_MISSING; 205 return VerifierResult::ERROR_REQUIRED_PROOF_MISSING;
86 } 206 }
87 207
88 std::vector<uint8_t> sig(sig_size); 208 std::vector<uint8_t> sig(sig_size);
89 if (ReadAndHashBuffer(sig.data(), sig_size, file, hash) != 209 if (ReadAndHashBuffer(sig.data(), sig_size, file, hash) !=
90 static_cast<int>(sig_size)) 210 static_cast<int>(sig_size))
91 return VerifierResult::ERROR_HEADER_INVALID; 211 return VerifierResult::ERROR_HEADER_INVALID;
92 crypto::SignatureVerifier verifier; 212 std::vector<std::unique_ptr<crypto::SignatureVerifier>> verifiers;
93 if (!verifier.VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1, 213 verifiers.push_back(base::MakeUnique<crypto::SignatureVerifier>());
Sorin Jianu 2017/05/18 00:53:31 we might be able to initialize the vector with the
waffles 2017/05/18 17:47:32 Unfortunately I don't think you can use an initial
94 sig.data(), sig.size(), key.data(), key.size())) { 214 if (!verifiers[0]->VerifyInit(crypto::SignatureVerifier::RSA_PKCS1_SHA1,
215 sig.data(), sig.size(), key.data(),
216 key.size())) {
95 return VerifierResult::ERROR_SIGNATURE_INITIALIZATION_FAILED; 217 return VerifierResult::ERROR_SIGNATURE_INITIALIZATION_FAILED;
96 } 218 }
97 219
98 // Read the rest of the file. 220 if (!ReadHashAndVerifyArchive(file, hash, verifiers))
99 uint8_t buffer[1 << 12] = {};
100 size_t len = 0;
101 while ((len = ReadAndHashBuffer(buffer, arraysize(buffer), file, hash)) > 0)
102 verifier.VerifyUpdate(buffer, len);
103 if (!verifier.VerifyFinal())
104 return VerifierResult::ERROR_SIGNATURE_VERIFICATION_FAILED; 221 return VerifierResult::ERROR_SIGNATURE_VERIFICATION_FAILED;
105 222
106 const std::string public_key_bytes(key.begin(), key.end()); 223 const std::string public_key_bytes(key.begin(), key.end());
107 if (public_key) 224 if (public_key)
108 base::Base64Encode(public_key_bytes, public_key); 225 base::Base64Encode(public_key_bytes, public_key);
109 if (crx_id) 226 if (crx_id)
110 *crx_id = id_util::GenerateId(public_key_bytes); 227 *crx_id = id_util::GenerateId(public_key_bytes);
111 return VerifierResult::OK_FULL; 228 return VerifierResult::OK_FULL;
112 } 229 }
113 230
(...skipping 27 matching lines...) Expand all
141 258
142 // Version number. 259 // Version number.
143 const uint32_t version = 260 const uint32_t version =
144 ReadAndHashLittleEndianUInt32(&file, file_hash.get()); 261 ReadAndHashLittleEndianUInt32(&file, file_hash.get());
145 VerifierResult result; 262 VerifierResult result;
146 if (format == VerifierFormat::CRX2_OR_CRX3 && 263 if (format == VerifierFormat::CRX2_OR_CRX3 &&
147 (version == 2 || (diff && version == 0))) 264 (version == 2 || (diff && version == 0)))
148 result = VerifyCrx2(&file, file_hash.get(), required_key_hashes, public_key, 265 result = VerifyCrx2(&file, file_hash.get(), required_key_hashes, public_key,
149 crx_id); 266 crx_id);
150 else if (version == 3) 267 else if (version == 3)
151 result = VerifyCrx3(&file, file_hash.get(), required_key_hashes, public_key, 268 result =
152 crx_id); 269 VerifyCrx3(&file, file_hash.get(), required_key_hashes, public_key,
270 crx_id, format == VerifierFormat::CRX3_WITH_PUBLISHER_PROOF);
153 else 271 else
154 result = VerifierResult::ERROR_HEADER_INVALID; 272 result = VerifierResult::ERROR_HEADER_INVALID;
155 if (result != VerifierResult::OK_FULL) 273 if (result != VerifierResult::OK_FULL)
156 return result; 274 return result;
157 275
158 // Finalize file hash. 276 // Finalize file hash.
159 uint8_t final_hash[crypto::kSHA256Length] = {}; 277 uint8_t final_hash[crypto::kSHA256Length] = {};
160 file_hash->Finish(final_hash, sizeof(final_hash)); 278 file_hash->Finish(final_hash, sizeof(final_hash));
161 if (!required_file_hash.empty()) { 279 if (!required_file_hash.empty()) {
162 if (required_file_hash.size() != crypto::kSHA256Length) 280 if (required_file_hash.size() != crypto::kSHA256Length)
163 return VerifierResult::ERROR_EXPECTED_HASH_INVALID; 281 return VerifierResult::ERROR_EXPECTED_HASH_INVALID;
164 if (!crypto::SecureMemEqual(final_hash, required_file_hash.data(), 282 if (!crypto::SecureMemEqual(final_hash, required_file_hash.data(),
165 crypto::kSHA256Length)) 283 crypto::kSHA256Length))
166 return VerifierResult::ERROR_FILE_HASH_FAILED; 284 return VerifierResult::ERROR_FILE_HASH_FAILED;
167 } 285 }
168 return diff ? VerifierResult::OK_DELTA : VerifierResult::OK_FULL; 286 return diff ? VerifierResult::OK_DELTA : VerifierResult::OK_FULL;
169 } 287 }
170 288
171 } // namespace crx_file 289 } // namespace crx_file
OLDNEW
« no previous file with comments | « components/crx_file/crx3.proto ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698