Chromium Code Reviews| 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 "chrome/browser/chromeos/platform_keys/platform_keys_service.h" | 5 #include "chrome/browser/chromeos/platform_keys/platform_keys_service.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" | 10 #include "chrome/browser/chromeos/platform_keys/platform_keys.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "extensions/browser/state_store.h" | 12 #include "extensions/browser/state_store.h" |
| 13 | 13 |
| 14 using content::BrowserThread; | 14 using content::BrowserThread; |
| 15 | 15 |
| 16 namespace chromeos { | 16 namespace chromeos { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kErrorInternal[] = "Internal Error."; | |
| 21 const char kErrorKeyNotAllowedForSigning[] = | 20 const char kErrorKeyNotAllowedForSigning[] = |
| 22 "This key is not allowed for signing. Either it was used for signing " | 21 "This key is not allowed for signing. Either it was used for signing " |
| 23 "before or it was not correctly generated."; | 22 "before or it was not correctly generated."; |
| 24 const char kStateStorePlatformKeys[] = "PlatformKeys"; | 23 const char kStateStorePlatformKeys[] = "PlatformKeys"; |
| 25 | 24 |
| 26 scoped_ptr<base::StringValue> GetPublicKeyValue( | 25 scoped_ptr<base::StringValue> GetPublicKeyValue( |
| 27 const std::string& public_key_spki_der) { | 26 const std::string& public_key_spki_der) { |
| 28 std::string public_key_spki_der_b64; | 27 std::string public_key_spki_der_b64; |
| 29 base::Base64Encode(public_key_spki_der, &public_key_spki_der_b64); | 28 base::Base64Encode(public_key_spki_der, &public_key_spki_der_b64); |
| 30 return make_scoped_ptr(new base::StringValue(public_key_spki_der_b64)); | 29 return make_scoped_ptr(new base::StringValue(public_key_spki_der_b64)); |
| 31 } | 30 } |
| 32 | 31 |
| 33 // Wraps |callback| into a void(bool) callback which forwards | 32 void RunGenerateKeyCallbackWithResult( |
|
Thiemo Nagel
2015/02/03 13:34:17
s/WithResult// ?
pneubeck (no reviews)
2015/02/03 15:33:44
Done.
| |
| 34 // |public_key_spki_der| if |true| is passed to it. | |
| 35 void WrapGenerateKeyCallback( | |
| 36 const PlatformKeysService::GenerateKeyCallback& callback, | 33 const PlatformKeysService::GenerateKeyCallback& callback, |
| 37 const std::string& public_key_spki_der, | 34 const std::string& public_key_spki_der) { |
| 38 bool success) { | 35 callback.Run(public_key_spki_der, std::string() /* no error */); |
| 39 if (success) | |
| 40 callback.Run(public_key_spki_der, std::string() /* no error */); | |
| 41 else | |
| 42 callback.Run(std::string() /* no public key */, kErrorInternal); | |
| 43 } | 36 } |
| 44 | 37 |
| 45 // Callback used by |PlatformKeysService::Sign|. | 38 // Callback used by |PlatformKeysService::Sign|. |
| 46 // Is called with the old validity of |public_key_spki_der| (or false if an | 39 // Is called with the old validity of |public_key_spki_der| (or false if an |
| 47 // error occurred during reading the StateStore). If allowed, starts the actual | 40 // error occurred during reading the StateStore). If allowed, starts the actual |
| 48 // signing operation which will call back |callback|. If not allowed, calls | 41 // signing operation which will call back |callback|. If not allowed, calls |
| 49 // |callback| with an error. | 42 // |callback| with an error. |
| 50 void CheckValidityAndSign(const std::string& token_id, | 43 void CheckValidityAndSign(const std::string& token_id, |
| 51 const std::string& public_key_spki_der, | 44 const std::string& public_key_spki_der, |
| 52 platform_keys::HashAlgorithm hash_algorithm, | 45 platform_keys::HashAlgorithm hash_algorithm, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 public_key_spki_der, | 104 public_key_spki_der, |
| 112 hash_algorithm, | 105 hash_algorithm, |
| 113 data, | 106 data, |
| 114 callback, | 107 callback, |
| 115 browser_context_)); | 108 browser_context_)); |
| 116 } | 109 } |
| 117 | 110 |
| 118 void PlatformKeysService::RegisterPublicKey( | 111 void PlatformKeysService::RegisterPublicKey( |
| 119 const std::string& extension_id, | 112 const std::string& extension_id, |
| 120 const std::string& public_key_spki_der, | 113 const std::string& public_key_spki_der, |
| 121 const base::Callback<void(bool)>& callback) { | 114 const base::Closure& callback) { |
| 122 GetPlatformKeysOfExtension( | 115 GetPlatformKeysOfExtension( |
| 123 extension_id, | 116 extension_id, |
| 124 base::Bind(&PlatformKeysService::RegisterPublicKeyGotPlatformKeys, | 117 base::Bind(&PlatformKeysService::RegisterPublicKeyGotPlatformKeys, |
| 125 weak_factory_.GetWeakPtr(), | 118 weak_factory_.GetWeakPtr(), |
| 126 extension_id, | 119 extension_id, |
| 127 public_key_spki_der, | 120 public_key_spki_der, |
| 128 callback)); | 121 callback)); |
| 129 } | 122 } |
| 130 | 123 |
| 131 void PlatformKeysService::ReadValidityAndInvalidateKey( | 124 void PlatformKeysService::ReadValidityAndInvalidateKey( |
| 132 const std::string& extension_id, | 125 const std::string& extension_id, |
| 133 const std::string& public_key_spki_der, | 126 const std::string& public_key_spki_der, |
| 134 const base::Callback<void(bool)>& callback) { | 127 const base::Callback<void(bool)>& callback) { |
| 135 GetPlatformKeysOfExtension(extension_id, | 128 GetPlatformKeysOfExtension(extension_id, |
| 136 base::Bind(&PlatformKeysService::InvalidateKey, | 129 base::Bind(&PlatformKeysService::InvalidateKey, |
| 137 weak_factory_.GetWeakPtr(), | 130 weak_factory_.GetWeakPtr(), |
| 138 extension_id, | 131 extension_id, |
| 139 public_key_spki_der, | 132 public_key_spki_der, |
| 140 callback)); | 133 callback)); |
| 141 } | 134 } |
| 142 | 135 |
| 143 void PlatformKeysService::GetPlatformKeysOfExtension( | 136 void PlatformKeysService::GetPlatformKeysOfExtension( |
| 144 const std::string& extension_id, | 137 const std::string& extension_id, |
| 145 const GetPlatformKeysCallback& callback) { | 138 const GetPlatformKeysCallback& callback) { |
| 146 state_store_->GetExtensionValue( | 139 state_store_->GetExtensionValue( |
| 147 extension_id, | 140 extension_id, kStateStorePlatformKeys, |
| 148 kStateStorePlatformKeys, | |
| 149 base::Bind(&PlatformKeysService::GotPlatformKeysOfExtension, | 141 base::Bind(&PlatformKeysService::GotPlatformKeysOfExtension, |
| 150 weak_factory_.GetWeakPtr(), | 142 weak_factory_.GetWeakPtr(), extension_id, callback)); |
| 151 extension_id, | 143 } |
| 152 callback)); | 144 |
| 145 void PlatformKeysService::SetPlatformKeysOfExtension( | |
| 146 const std::string& extension_id, | |
| 147 scoped_ptr<base::ListValue> platform_keys) { | |
| 148 state_store_->SetExtensionValue(extension_id, kStateStorePlatformKeys, | |
| 149 platform_keys.Pass()); | |
| 153 } | 150 } |
| 154 | 151 |
| 155 void PlatformKeysService::GenerateRSAKeyCallback( | 152 void PlatformKeysService::GenerateRSAKeyCallback( |
| 156 const std::string& extension_id, | 153 const std::string& extension_id, |
| 157 const GenerateKeyCallback& callback, | 154 const GenerateKeyCallback& callback, |
| 158 const std::string& public_key_spki_der, | 155 const std::string& public_key_spki_der, |
| 159 const std::string& error_message) { | 156 const std::string& error_message) { |
| 160 if (!error_message.empty()) { | 157 if (!error_message.empty()) { |
| 161 callback.Run(std::string() /* no public key */, error_message); | 158 callback.Run(std::string() /* no public key */, error_message); |
| 162 return; | 159 return; |
| 163 } | 160 } |
| 164 base::Callback<void(bool)> wrapped_callback( | 161 base::Closure wrapped_callback(base::Bind(&RunGenerateKeyCallbackWithResult, |
| 165 base::Bind(&WrapGenerateKeyCallback, callback, public_key_spki_der)); | 162 callback, public_key_spki_der)); |
| 166 RegisterPublicKey(extension_id, public_key_spki_der, wrapped_callback); | 163 RegisterPublicKey(extension_id, public_key_spki_der, wrapped_callback); |
| 167 } | 164 } |
| 168 | 165 |
| 169 void PlatformKeysService::RegisterPublicKeyGotPlatformKeys( | 166 void PlatformKeysService::RegisterPublicKeyGotPlatformKeys( |
| 170 const std::string& extension_id, | 167 const std::string& extension_id, |
| 171 const std::string& public_key_spki_der, | 168 const std::string& public_key_spki_der, |
| 172 const base::Callback<void(bool)>& callback, | 169 const base::Closure& callback, |
| 173 scoped_ptr<base::ListValue> platform_keys) { | 170 scoped_ptr<base::ListValue> platform_keys) { |
| 174 if (!platform_keys) { | |
| 175 LOG(ERROR) << "Error while reading the platform keys."; | |
| 176 callback.Run(false); | |
| 177 return; | |
| 178 } | |
| 179 | |
| 180 scoped_ptr<base::StringValue> key_value( | 171 scoped_ptr<base::StringValue> key_value( |
| 181 GetPublicKeyValue(public_key_spki_der)); | 172 GetPublicKeyValue(public_key_spki_der)); |
| 182 | 173 |
| 183 DCHECK(platform_keys->end() == platform_keys->Find(*key_value)) | 174 DCHECK(platform_keys->end() == platform_keys->Find(*key_value)) |
| 184 << "Keys are assumed to be generated and not to be registered multiple " | 175 << "Keys are assumed to be generated and not to be registered multiple " |
| 185 "times."; | 176 "times."; |
| 186 platform_keys->Append(key_value.release()); | 177 platform_keys->Append(key_value.release()); |
| 187 | 178 SetPlatformKeysOfExtension(extension_id, platform_keys.Pass()); |
| 188 state_store_->SetExtensionValue( | 179 callback.Run(); |
| 189 extension_id, kStateStorePlatformKeys, platform_keys.Pass()); | |
| 190 callback.Run(true); | |
| 191 } | 180 } |
| 192 | 181 |
| 193 void PlatformKeysService::InvalidateKey( | 182 void PlatformKeysService::InvalidateKey( |
| 194 const std::string& extension_id, | 183 const std::string& extension_id, |
| 195 const std::string& public_key_spki_der, | 184 const std::string& public_key_spki_der, |
| 196 const base::Callback<void(bool)>& callback, | 185 const base::Callback<void(bool)>& callback, |
| 197 scoped_ptr<base::ListValue> platform_keys) { | 186 scoped_ptr<base::ListValue> platform_keys) { |
| 198 scoped_ptr<base::StringValue> key_value( | 187 scoped_ptr<base::StringValue> key_value( |
| 199 GetPublicKeyValue(public_key_spki_der)); | 188 GetPublicKeyValue(public_key_spki_der)); |
| 200 | 189 |
| 201 size_t index = 0; | 190 size_t index = 0; |
| 202 if (!platform_keys->Remove(*key_value, &index)) { | 191 if (!platform_keys->Remove(*key_value, &index)) { |
| 203 // The key is not found, so it's not valid to use it for signing. | 192 // The key is not found, so it's not valid to use it for signing. |
| 204 callback.Run(false); | 193 callback.Run(false); |
| 205 return; | 194 return; |
| 206 } | 195 } |
| 207 | 196 |
| 208 state_store_->SetExtensionValue( | 197 SetPlatformKeysOfExtension(extension_id, platform_keys.Pass()); |
| 209 extension_id, kStateStorePlatformKeys, platform_keys.Pass()); | |
| 210 callback.Run(true); | 198 callback.Run(true); |
| 211 } | 199 } |
| 212 | 200 |
| 213 void PlatformKeysService::GotPlatformKeysOfExtension( | 201 void PlatformKeysService::GotPlatformKeysOfExtension( |
| 214 const std::string& extension_id, | 202 const std::string& extension_id, |
| 215 const GetPlatformKeysCallback& callback, | 203 const GetPlatformKeysCallback& callback, |
| 216 scoped_ptr<base::Value> value) { | 204 scoped_ptr<base::Value> value) { |
| 217 if (!value) | 205 if (!value) |
| 218 value.reset(new base::ListValue); | 206 value.reset(new base::ListValue); |
| 219 | 207 |
| 220 base::ListValue* keys = NULL; | 208 base::ListValue* keys = NULL; |
| 221 if (!value->GetAsList(&keys)) { | 209 if (!value->GetAsList(&keys)) { |
| 222 LOG(ERROR) << "Found a value of wrong type."; | 210 LOG(ERROR) << "Found a value of wrong type."; |
| 223 value.reset(); | 211 |
| 212 keys = new base::ListValue; | |
| 213 value.reset(keys); | |
| 224 } | 214 } |
| 215 | |
| 225 ignore_result(value.release()); | 216 ignore_result(value.release()); |
| 226 callback.Run(make_scoped_ptr(keys)); | 217 callback.Run(make_scoped_ptr(keys)); |
| 227 } | 218 } |
| 228 | 219 |
| 229 } // namespace chromeos | 220 } // namespace chromeos |
| OLD | NEW |