| 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/crx_file/crx_file.h" | 5 #include "components/crx_file/crx_file.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/numerics/safe_math.h" | 13 #include "base/numerics/safe_math.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "components/crx_file/id_util.h" | 16 #include "components/crx_file/id_util.h" |
| 17 #include "crypto/secure_hash.h" | 17 #include "crypto/secure_hash.h" |
| 18 #include "crypto/sha2.h" | 18 #include "crypto/sha2.h" |
| 19 #include "crypto/signature_verifier.h" | 19 #include "crypto/signature_verifier.h" |
| 20 | 20 |
| 21 namespace crx_file { | 21 namespace crx_file { |
| 22 | 22 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 return CrxFile::ValidateError::NONE; | 70 return CrxFile::ValidateError::NONE; |
| 71 } | 71 } |
| 72 } | 72 } |
| 73 | 73 |
| 74 } // namespace | 74 } // namespace |
| 75 | 75 |
| 76 // The magic string embedded in the header. | 76 // The magic string embedded in the header. |
| 77 const char kCrxFileHeaderMagic[] = "Cr24"; | 77 const char kCrxFileHeaderMagic[] = "Cr24"; |
| 78 const char kCrxDiffFileHeaderMagic[] = "CrOD"; | 78 const char kCrxDiffFileHeaderMagic[] = "CrOD"; |
| 79 | 79 |
| 80 scoped_ptr<CrxFile> CrxFile::Parse(const CrxFile::Header& header, | 80 std::unique_ptr<CrxFile> CrxFile::Parse(const CrxFile::Header& header, |
| 81 CrxFile::Error* error) { | 81 CrxFile::Error* error) { |
| 82 if (HeaderIsValid(header, error)) | 82 if (HeaderIsValid(header, error)) |
| 83 return scoped_ptr<CrxFile>(new CrxFile(header)); | 83 return base::WrapUnique(new CrxFile(header)); |
| 84 return scoped_ptr<CrxFile>(); | 84 return nullptr; |
| 85 } | 85 } |
| 86 | 86 |
| 87 scoped_ptr<CrxFile> CrxFile::Create(const uint32_t key_size, | 87 std::unique_ptr<CrxFile> CrxFile::Create(const uint32_t key_size, |
| 88 const uint32_t signature_size, | 88 const uint32_t signature_size, |
| 89 CrxFile::Error* error) { | 89 CrxFile::Error* error) { |
| 90 CrxFile::Header header; | 90 CrxFile::Header header; |
| 91 memcpy(&header.magic, kCrxFileHeaderMagic, kCrxFileHeaderMagicSize); | 91 memcpy(&header.magic, kCrxFileHeaderMagic, kCrxFileHeaderMagicSize); |
| 92 header.version = kCurrentVersion; | 92 header.version = kCurrentVersion; |
| 93 header.key_size = key_size; | 93 header.key_size = key_size; |
| 94 header.signature_size = signature_size; | 94 header.signature_size = signature_size; |
| 95 if (HeaderIsValid(header, error)) | 95 if (HeaderIsValid(header, error)) |
| 96 return scoped_ptr<CrxFile>(new CrxFile(header)); | 96 return base::WrapUnique(new CrxFile(header)); |
| 97 return scoped_ptr<CrxFile>(); | 97 return nullptr; |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool CrxFile::HeaderIsDelta(const CrxFile::Header& header) { | 100 bool CrxFile::HeaderIsDelta(const CrxFile::Header& header) { |
| 101 return !strncmp(kCrxDiffFileHeaderMagic, header.magic, sizeof(header.magic)); | 101 return !strncmp(kCrxDiffFileHeaderMagic, header.magic, sizeof(header.magic)); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 CrxFile::ValidateError CrxFile::ValidateSignature( | 105 CrxFile::ValidateError CrxFile::ValidateSignature( |
| 106 const base::FilePath& crx_path, | 106 const base::FilePath& crx_path, |
| 107 const std::string& expected_hash, | 107 const std::string& expected_hash, |
| 108 std::string* public_key, | 108 std::string* public_key, |
| 109 std::string* extension_id, | 109 std::string* extension_id, |
| 110 CrxFile::Header* header_out) { | 110 CrxFile::Header* header_out) { |
| 111 base::ScopedFILE file(base::OpenFile(crx_path, "rb")); | 111 base::ScopedFILE file(base::OpenFile(crx_path, "rb")); |
| 112 scoped_ptr<crypto::SecureHash> hash; | 112 std::unique_ptr<crypto::SecureHash> hash; |
| 113 if (!expected_hash.empty()) | 113 if (!expected_hash.empty()) |
| 114 hash.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); | 114 hash.reset(crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 115 | 115 |
| 116 if (!file.get()) | 116 if (!file.get()) |
| 117 return ValidateError::CRX_FILE_NOT_READABLE; | 117 return ValidateError::CRX_FILE_NOT_READABLE; |
| 118 | 118 |
| 119 CrxFile::Header header; | 119 CrxFile::Header header; |
| 120 size_t len = ReadAndHash(&header, sizeof(header), 1, file.get(), hash.get()); | 120 size_t len = ReadAndHash(&header, sizeof(header), 1, file.get(), hash.get()); |
| 121 if (len != sizeof(header)) | 121 if (len != sizeof(header)) |
| 122 return ValidateError::CRX_HEADER_INVALID; | 122 return ValidateError::CRX_HEADER_INVALID; |
| 123 if (header_out) | 123 if (header_out) |
| 124 *header_out = header; | 124 *header_out = header; |
| 125 | 125 |
| 126 CrxFile::Error error; | 126 CrxFile::Error error; |
| 127 scoped_ptr<CrxFile> crx(CrxFile::Parse(header, &error)); | 127 std::unique_ptr<CrxFile> crx(CrxFile::Parse(header, &error)); |
| 128 if (!crx) { | 128 if (!crx) { |
| 129 switch (error) { | 129 switch (error) { |
| 130 case CrxFile::kWrongMagic: | 130 case CrxFile::kWrongMagic: |
| 131 return ValidateError::CRX_MAGIC_NUMBER_INVALID; | 131 return ValidateError::CRX_MAGIC_NUMBER_INVALID; |
| 132 case CrxFile::kInvalidVersion: | 132 case CrxFile::kInvalidVersion: |
| 133 return ValidateError::CRX_VERSION_NUMBER_INVALID; | 133 return ValidateError::CRX_VERSION_NUMBER_INVALID; |
| 134 | 134 |
| 135 case CrxFile::kInvalidKeyTooLarge: | 135 case CrxFile::kInvalidKeyTooLarge: |
| 136 case CrxFile::kInvalidSignatureTooLarge: | 136 case CrxFile::kInvalidSignatureTooLarge: |
| 137 return ValidateError::CRX_EXCESSIVELY_LARGE_KEY_OR_SIGNATURE; | 137 return ValidateError::CRX_EXCESSIVELY_LARGE_KEY_OR_SIGNATURE; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 else if (header.signature_size > kMaxSignatureSize) | 211 else if (header.signature_size > kMaxSignatureSize) |
| 212 *error = kInvalidSignatureTooLarge; | 212 *error = kInvalidSignatureTooLarge; |
| 213 else if (header.signature_size == 0) | 213 else if (header.signature_size == 0) |
| 214 *error = kInvalidSignatureTooSmall; | 214 *error = kInvalidSignatureTooSmall; |
| 215 else | 215 else |
| 216 valid = true; | 216 valid = true; |
| 217 return valid; | 217 return valid; |
| 218 } | 218 } |
| 219 | 219 |
| 220 } // namespace crx_file | 220 } // namespace crx_file |
| OLD | NEW |