OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/crx_file/crx2_file.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/numerics/safe_math.h" |
| 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" |
| 16 #include "components/crx_file/id_util.h" |
| 17 #include "crypto/secure_hash.h" |
| 18 #include "crypto/sha2.h" |
| 19 #include "crypto/signature_verifier.h" |
| 20 |
| 21 namespace crx_file { |
| 22 |
| 23 namespace { |
| 24 |
| 25 // The current version of the crx format. |
| 26 static const uint32_t kCurrentVersion = 2; |
| 27 |
| 28 // The current version of the crx diff format. |
| 29 static const uint32_t kCurrentDiffVersion = 0; |
| 30 |
| 31 // The maximum size the crx parser will tolerate for a public key. |
| 32 static const uint32_t kMaxPublicKeySize = 1 << 16; |
| 33 |
| 34 // The maximum size the crx parser will tolerate for a signature. |
| 35 static const uint32_t kMaxSignatureSize = 1 << 16; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 std::unique_ptr<Crx2File> Crx2File::Create(const uint32_t key_size, |
| 40 const uint32_t signature_size, |
| 41 Crx2File::Error* error) { |
| 42 Crx2File::Header header; |
| 43 memcpy(&header.magic, kCrx2FileHeaderMagic, kCrx2FileHeaderMagicSize); |
| 44 header.version = kCurrentVersion; |
| 45 header.key_size = key_size; |
| 46 header.signature_size = signature_size; |
| 47 if (HeaderIsValid(header, error)) |
| 48 return base::WrapUnique(new Crx2File(header)); |
| 49 return nullptr; |
| 50 } |
| 51 |
| 52 Crx2File::Crx2File(const Header& header) : header_(header) {} |
| 53 |
| 54 bool Crx2File::HeaderIsValid(const Crx2File::Header& header, |
| 55 Crx2File::Error* error) { |
| 56 bool valid = false; |
| 57 bool diffCrx = false; |
| 58 if (!strncmp(kCrxDiffFileHeaderMagic, header.magic, sizeof(header.magic))) |
| 59 diffCrx = true; |
| 60 if (strncmp(kCrx2FileHeaderMagic, header.magic, sizeof(header.magic)) && |
| 61 !diffCrx) |
| 62 *error = kWrongMagic; |
| 63 else if (header.version != kCurrentVersion && |
| 64 !(diffCrx && header.version == kCurrentDiffVersion)) |
| 65 *error = kInvalidVersion; |
| 66 else if (header.key_size > kMaxPublicKeySize) |
| 67 *error = kInvalidKeyTooLarge; |
| 68 else if (header.key_size == 0) |
| 69 *error = kInvalidKeyTooSmall; |
| 70 else if (header.signature_size > kMaxSignatureSize) |
| 71 *error = kInvalidSignatureTooLarge; |
| 72 else if (header.signature_size == 0) |
| 73 *error = kInvalidSignatureTooSmall; |
| 74 else |
| 75 valid = true; |
| 76 return valid; |
| 77 } |
| 78 |
| 79 } // namespace crx_file |
OLD | NEW |