| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_creator.h" | 5 #include "chrome/browser/extensions/extension_creator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_CRX_EXISTS); | 77 error_message_ = l10n_util::GetStringUTF8(IDS_EXTENSION_CRX_EXISTS); |
| 78 error_type_ = kCRXExists; | 78 error_type_ = kCRXExists; |
| 79 | 79 |
| 80 return false; | 80 return false; |
| 81 } | 81 } |
| 82 | 82 |
| 83 return true; | 83 return true; |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool ExtensionCreator::ValidateManifest(const FilePath& extension_dir, | 86 bool ExtensionCreator::ValidateManifest(const FilePath& extension_dir, |
| 87 crypto::RSAPrivateKey* key_pair) { | 87 crypto::RSAPrivateKey* key_pair, |
| 88 int run_flags) { |
| 88 std::vector<uint8> public_key_bytes; | 89 std::vector<uint8> public_key_bytes; |
| 89 if (!key_pair->ExportPublicKey(&public_key_bytes)) { | 90 if (!key_pair->ExportPublicKey(&public_key_bytes)) { |
| 90 error_message_ = | 91 error_message_ = |
| 91 l10n_util::GetStringUTF8(IDS_EXTENSION_PUBLIC_KEY_FAILED_TO_EXPORT); | 92 l10n_util::GetStringUTF8(IDS_EXTENSION_PUBLIC_KEY_FAILED_TO_EXPORT); |
| 92 return false; | 93 return false; |
| 93 } | 94 } |
| 94 | 95 |
| 95 std::string public_key; | 96 std::string public_key; |
| 96 public_key.insert(public_key.begin(), | 97 public_key.insert(public_key.begin(), |
| 97 public_key_bytes.begin(), public_key_bytes.end()); | 98 public_key_bytes.begin(), public_key_bytes.end()); |
| 98 | 99 |
| 99 std::string extension_id; | 100 std::string extension_id; |
| 100 if (!Extension::GenerateId(public_key, &extension_id)) | 101 if (!Extension::GenerateId(public_key, &extension_id)) |
| 101 return false; | 102 return false; |
| 102 | 103 |
| 103 // Load the extension once. We don't really need it, but this does a lot of | 104 // Load the extension once. We don't really need it, but this does a lot of |
| 104 // useful validation of the structure. | 105 // useful validation of the structure. |
| 106 int create_flags = |
| 107 Extension::FOLLOW_SYMLINKS_ANYWHERE | Extension::ERROR_ON_PRIVATE_KEY; |
| 108 if (run_flags & kRequireModernManifestVersion) |
| 109 create_flags |= Extension::REQUIRE_MODERN_MANIFEST_VERSION; |
| 110 |
| 105 scoped_refptr<Extension> extension( | 111 scoped_refptr<Extension> extension( |
| 106 extension_file_util::LoadExtension( | 112 extension_file_util::LoadExtension( |
| 107 extension_dir, | 113 extension_dir, |
| 108 extension_id, | 114 extension_id, |
| 109 Extension::INTERNAL, | 115 Extension::INTERNAL, |
| 110 Extension::FOLLOW_SYMLINKS_ANYWHERE | Extension::ERROR_ON_PRIVATE_KEY, | 116 create_flags, |
| 111 &error_message_)); | 117 &error_message_)); |
| 112 return !!extension.get(); | 118 return !!extension.get(); |
| 113 } | 119 } |
| 114 | 120 |
| 115 crypto::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& | 121 crypto::RSAPrivateKey* ExtensionCreator::ReadInputKey(const FilePath& |
| 116 private_key_path) { | 122 private_key_path) { |
| 117 if (!file_util::PathExists(private_key_path)) { | 123 if (!file_util::PathExists(private_key_path)) { |
| 118 error_message_ = | 124 error_message_ = |
| 119 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS); | 125 l10n_util::GetStringUTF8(IDS_EXTENSION_PRIVATE_KEY_NO_EXISTS); |
| 120 return NULL; | 126 return NULL; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 // Initialize Key Pair | 296 // Initialize Key Pair |
| 291 scoped_ptr<crypto::RSAPrivateKey> key_pair; | 297 scoped_ptr<crypto::RSAPrivateKey> key_pair; |
| 292 if (!private_key_path.value().empty()) | 298 if (!private_key_path.value().empty()) |
| 293 key_pair.reset(ReadInputKey(private_key_path)); | 299 key_pair.reset(ReadInputKey(private_key_path)); |
| 294 else | 300 else |
| 295 key_pair.reset(GenerateKey(output_private_key_path)); | 301 key_pair.reset(GenerateKey(output_private_key_path)); |
| 296 if (!key_pair.get()) | 302 if (!key_pair.get()) |
| 297 return false; | 303 return false; |
| 298 | 304 |
| 299 // Perform some extra validation by loading the extension. | 305 // Perform some extra validation by loading the extension. |
| 300 if (!ValidateManifest(extension_dir, key_pair.get())) | 306 // TODO(aa): Can this go before creating the key pair? This would mean not |
| 307 // passing ID into LoadExtension which seems OK. |
| 308 if (!ValidateManifest(extension_dir, key_pair.get(), run_flags)) |
| 301 return false; | 309 return false; |
| 302 | 310 |
| 303 ScopedTempDir temp_dir; | 311 ScopedTempDir temp_dir; |
| 304 if (!temp_dir.CreateUniqueTempDir()) | 312 if (!temp_dir.CreateUniqueTempDir()) |
| 305 return false; | 313 return false; |
| 306 | 314 |
| 307 // Zip up the extension. | 315 // Zip up the extension. |
| 308 FilePath zip_path; | 316 FilePath zip_path; |
| 309 std::vector<uint8> signature; | 317 std::vector<uint8> signature; |
| 310 bool result = false; | 318 bool result = false; |
| 311 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && | 319 if (CreateZip(extension_dir, temp_dir.path(), &zip_path) && |
| 312 SignZip(zip_path, key_pair.get(), &signature) && | 320 SignZip(zip_path, key_pair.get(), &signature) && |
| 313 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { | 321 WriteCRX(zip_path, key_pair.get(), signature, crx_path)) { |
| 314 result = true; | 322 result = true; |
| 315 } | 323 } |
| 316 | 324 |
| 317 file_util::Delete(zip_path, false); | 325 file_util::Delete(zip_path, false); |
| 318 return result; | 326 return result; |
| 319 } | 327 } |
| 320 | 328 |
| 321 } // namespace extensions | 329 } // namespace extensions |
| OLD | NEW |