OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/extensions/api/platform_keys/platform_keys_api.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
| 11 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" |
| 12 #include "chrome/browser/chromeos/platform_keys/platform_keys_service.h" |
| 13 #include "chrome/browser/chromeos/platform_keys/platform_keys_service_factory.h" |
| 14 #include "chrome/common/extensions/api/platform_keys_internal.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "net/cert/x509_certificate.h" |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 namespace api_pki = api::platform_keys_internal; |
| 21 |
| 22 namespace platform_keys { |
| 23 |
| 24 const char kErrorInvalidToken[] = "The token is not valid."; |
| 25 const char kErrorAlgorithmNotSupported[] = "Algorithm not supported."; |
| 26 const char kTokenIdUser[] = "user"; |
| 27 const char kTokenIdSystem[] = "system"; |
| 28 |
| 29 // Returns whether |token_id| references a known Token. |
| 30 bool ValidateToken(const std::string& token_id, |
| 31 std::string* platform_keys_token_id) { |
| 32 platform_keys_token_id->clear(); |
| 33 if (token_id == kTokenIdUser) { |
| 34 *platform_keys_token_id = chromeos::platform_keys::kTokenIdUser; |
| 35 return true; |
| 36 } |
| 37 if (token_id == kTokenIdSystem) { |
| 38 *platform_keys_token_id = chromeos::platform_keys::kTokenIdSystem; |
| 39 return true; |
| 40 } |
| 41 return false; |
| 42 } |
| 43 |
| 44 std::string PlatformKeysTokenIdToApiId( |
| 45 const std::string& platform_keys_token_id) { |
| 46 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdUser) |
| 47 return kTokenIdUser; |
| 48 if (platform_keys_token_id == chromeos::platform_keys::kTokenIdSystem) |
| 49 return kTokenIdSystem; |
| 50 |
| 51 return std::string(); |
| 52 } |
| 53 |
| 54 } // namespace platform_keys |
| 55 |
| 56 PlatformKeysInternalSignFunction::~PlatformKeysInternalSignFunction() { |
| 57 } |
| 58 |
| 59 ExtensionFunction::ResponseAction PlatformKeysInternalSignFunction::Run() { |
| 60 scoped_ptr<api_pki::Sign::Params> params( |
| 61 api_pki::Sign::Params::Create(*args_)); |
| 62 EXTENSION_FUNCTION_VALIDATE(params); |
| 63 std::string platform_keys_token_id; |
| 64 if (!platform_keys::ValidateToken(params->token_id, &platform_keys_token_id)) |
| 65 return RespondNow(Error(platform_keys::kErrorInvalidToken)); |
| 66 |
| 67 chromeos::platform_keys::HashAlgorithm hash_algorithm; |
| 68 if (params->hash_algorithm_name == "SHA-1") |
| 69 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA1; |
| 70 else if (params->hash_algorithm_name == "SHA-256") |
| 71 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA256; |
| 72 else if (params->hash_algorithm_name == "SHA-384") |
| 73 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA384; |
| 74 else if (params->hash_algorithm_name == "SHA-512") |
| 75 hash_algorithm = chromeos::platform_keys::HASH_ALGORITHM_SHA512; |
| 76 else |
| 77 return RespondNow(Error(platform_keys::kErrorAlgorithmNotSupported)); |
| 78 |
| 79 chromeos::PlatformKeysService* service = |
| 80 chromeos::PlatformKeysServiceFactory::GetForBrowserContext( |
| 81 browser_context()); |
| 82 DCHECK(service); |
| 83 |
| 84 service->Sign( |
| 85 platform_keys_token_id, |
| 86 std::string(params->public_key.begin(), params->public_key.end()), |
| 87 hash_algorithm, std::string(params->data.begin(), params->data.end()), |
| 88 extension_id(), |
| 89 base::Bind(&PlatformKeysInternalSignFunction::OnSigned, this)); |
| 90 return RespondLater(); |
| 91 } |
| 92 |
| 93 void PlatformKeysInternalSignFunction::OnSigned( |
| 94 const std::string& signature, |
| 95 const std::string& error_message) { |
| 96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 97 if (error_message.empty()) |
| 98 Respond(ArgumentList(api_pki::Sign::Results::Create( |
| 99 std::vector<char>(signature.begin(), signature.end())))); |
| 100 else |
| 101 Respond(Error(error_message)); |
| 102 } |
| 103 |
| 104 } // namespace extensions |
OLD | NEW |