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