| 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 #ifndef CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace net { | 33 namespace net { |
| 34 class X509Certificate; | 34 class X509Certificate; |
| 35 typedef std::vector<scoped_refptr<X509Certificate>> CertificateList; | 35 typedef std::vector<scoped_refptr<X509Certificate>> CertificateList; |
| 36 } | 36 } |
| 37 | 37 |
| 38 namespace chromeos { | 38 namespace chromeos { |
| 39 | 39 |
| 40 class PlatformKeysService : public KeyedService { | 40 class PlatformKeysService : public KeyedService { |
| 41 public: | 41 public: |
| 42 struct KeyEntry; |
| 43 using KeyEntries = std::vector<KeyEntry>; |
| 44 |
| 45 // The SelectDelegate is used to select a single certificate from all |
| 46 // certificates matching a request (see SelectClientCertificates). E.g. this |
| 47 // can happen by exposing UI to let the user select. |
| 48 class SelectDelegate { |
| 49 public: |
| 50 // TODO(pneubeck): Handle if the selection was aborted, e.g. by the user. |
| 51 using CertificateSelectedCallback = |
| 52 base::Callback<void(scoped_refptr<net::X509Certificate> selection)>; |
| 53 |
| 54 SelectDelegate(); |
| 55 virtual ~SelectDelegate(); |
| 56 |
| 57 // Called on an interactive SelectClientCertificates call with the list of |
| 58 // matching certificates, |certs|. |
| 59 // The certificate passed to |callback| will be forwarded to the |
| 60 // calling extension and the extension will get unlimited sign permission |
| 61 // for this cert. By passing null to |callback|, no cert will be selected. |
| 62 // Must eventually call |callback| or be destructed. |callback| must not be |
| 63 // called after this delegate is destructed. |
| 64 virtual void Select(const std::string& extension_id, |
| 65 const net::CertificateList& certs, |
| 66 const CertificateSelectedCallback& callback) = 0; |
| 67 |
| 68 private: |
| 69 DISALLOW_ASSIGN(SelectDelegate); |
| 70 }; |
| 71 |
| 42 // Stores registration information in |state_store|, i.e. for each extension | 72 // Stores registration information in |state_store|, i.e. for each extension |
| 43 // the list of public keys that are valid to be used for signing. Each key can | 73 // the list of public keys that are valid to be used for signing. Each key can |
| 44 // be used for signing at most once. | 74 // be used for signing at most once. |
| 45 // The format written to |state_store| is: | 75 // The format written to |state_store| is: |
| 46 // kStateStorePlatformKeys maps to a list of strings. | 76 // kStateStorePlatformKeys maps to a list of strings. |
| 47 // Each string is the base64 encoding of the DER representation of a public | 77 // Each string is the base64 encoding of the DER representation of a public |
| 48 // key's SPKI. | 78 // key's SPKI. |
| 49 explicit PlatformKeysService(content::BrowserContext* browser_context, | 79 explicit PlatformKeysService(content::BrowserContext* browser_context, |
| 50 extensions::StateStore* state_store); | 80 extensions::StateStore* state_store); |
| 51 ~PlatformKeysService() override; | 81 ~PlatformKeysService() override; |
| 52 | 82 |
| 53 // Disables the checks whether an extension is allowed to read client | 83 // Sets the delegate which will be used for interactive |
| 54 // certificates or allowed to use the signing function of a key. | 84 // SelectClientCertificates calls. |
| 55 // TODO(pneubeck): Remove this once a permissions are implemented. | 85 void SetSelectDelegate(scoped_ptr<SelectDelegate> delegate); |
| 56 void DisablePermissionCheckForTesting(); | 86 |
| 87 // Grants unlimited sign permission for |cert| to the extension with the ID |
| 88 // |extension_id|. |
| 89 void GrantUnlimitedSignPermission(const std::string& extension_id, |
| 90 scoped_refptr<net::X509Certificate> cert); |
| 57 | 91 |
| 58 // If the generation was successful, |public_key_spki_der| will contain the | 92 // If the generation was successful, |public_key_spki_der| will contain the |
| 59 // DER encoding of the SubjectPublicKeyInfo of the generated key and | 93 // DER encoding of the SubjectPublicKeyInfo of the generated key and |
| 60 // |error_message| will be empty. If it failed, |public_key_spki_der| will be | 94 // |error_message| will be empty. If it failed, |public_key_spki_der| will be |
| 61 // empty and |error_message| contain an error message. | 95 // empty and |error_message| contain an error message. |
| 62 typedef base::Callback<void(const std::string& public_key_spki_der, | 96 using GenerateKeyCallback = |
| 63 const std::string& error_message)> | 97 base::Callback<void(const std::string& public_key_spki_der, |
| 64 GenerateKeyCallback; | 98 const std::string& error_message)>; |
| 65 | 99 |
| 66 // Generates an RSA key pair with |modulus_length_bits| and registers the key | 100 // Generates an RSA key pair with |modulus_length_bits| and registers the key |
| 67 // to allow a single sign operation by the given extension. |token_id| is | 101 // to allow a single sign operation by the given extension. |token_id| is |
| 68 // currently ignored, instead the user token associated with |browser_context| | 102 // currently ignored, instead the user token associated with |browser_context| |
| 69 // is always used. |callback| will be invoked with the resulting public key or | 103 // is always used. |callback| will be invoked with the resulting public key or |
| 70 // an error. | 104 // an error. |
| 71 // Will only call back during the lifetime of this object. | 105 // Will only call back during the lifetime of this object. |
| 72 void GenerateRSAKey(const std::string& token_id, | 106 void GenerateRSAKey(const std::string& token_id, |
| 73 unsigned int modulus_length_bits, | 107 unsigned int modulus_length_bits, |
| 74 const std::string& extension_id, | 108 const std::string& extension_id, |
| 75 const GenerateKeyCallback& callback); | 109 const GenerateKeyCallback& callback); |
| 76 | 110 |
| 77 // If signing was successful, |signature| will be contain the signature and | 111 // If signing was successful, |signature| will be contain the signature and |
| 78 // |error_message| will be empty. If it failed, |signature| will be empty and | 112 // |error_message| will be empty. If it failed, |signature| will be empty and |
| 79 // |error_message| contain an error message. | 113 // |error_message| contain an error message. |
| 80 typedef base::Callback<void(const std::string& signature, | 114 using SignCallback = base::Callback<void(const std::string& signature, |
| 81 const std::string& error_message)> SignCallback; | 115 const std::string& error_message)>; |
| 82 | 116 |
| 83 // Digests |data|, applies PKCS1 padding and afterwards signs the data with | 117 // Digests |data|, applies PKCS1 padding and afterwards signs the data with |
| 84 // the private key matching |params.public_key|. If a non empty token id is | 118 // the private key matching |params.public_key|. If a non empty token id is |
| 85 // provided and the key is not found in that token, the operation aborts. | 119 // provided and the key is not found in that token, the operation aborts. |
| 86 // If the extension does not have permissions for signing with this key, the | 120 // If the extension does not have permissions for signing with this key, the |
| 87 // operation aborts. In case of a one time permission (granted after | 121 // operation aborts. In case of a one time permission (granted after |
| 88 // generating the key), this function also removes the permission to prevent | 122 // generating the key), this function also removes the permission to prevent |
| 89 // future signing attempts. | 123 // future signing attempts. |
| 90 // |callback| will be invoked with the signature or an error message. | 124 // |callback| will be invoked with the signature or an error message. |
| 91 // Will only call back during the lifetime of this object. | 125 // Will only call back during the lifetime of this object. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 111 void SignRSAPKCS1Raw(const std::string& token_id, | 145 void SignRSAPKCS1Raw(const std::string& token_id, |
| 112 const std::string& data, | 146 const std::string& data, |
| 113 const std::string& public_key, | 147 const std::string& public_key, |
| 114 const std::string& extension_id, | 148 const std::string& extension_id, |
| 115 const SignCallback& callback); | 149 const SignCallback& callback); |
| 116 | 150 |
| 117 // If the certificate request could be processed successfully, |matches| will | 151 // If the certificate request could be processed successfully, |matches| will |
| 118 // contain the list of matching certificates (maybe empty) and |error_message| | 152 // contain the list of matching certificates (maybe empty) and |error_message| |
| 119 // will be empty. If an error occurred, |matches| will be null and | 153 // will be empty. If an error occurred, |matches| will be null and |
| 120 // |error_message| contain an error message. | 154 // |error_message| contain an error message. |
| 121 typedef base::Callback<void(scoped_ptr<net::CertificateList> matches, | 155 using SelectCertificatesCallback = |
| 122 const std::string& error_message)> | 156 base::Callback<void(scoped_ptr<net::CertificateList> matches, |
| 123 SelectCertificatesCallback; | 157 const std::string& error_message)>; |
| 124 | 158 |
| 125 // Returns the list of all certificates that match |request|. |callback| will | 159 // Returns a list of certificates matching |request|. |
| 126 // be invoked with these matches or an error message. | 160 // 1) all certificates that match the request (like being rooted in one of the |
| 161 // give CAs) are determined. 2) if |interactive| is true, the currently set |
| 162 // SelectDelegate is used to select a single certificate from these matches |
| 163 // which will the extension will also be granted access to. 3) only |
| 164 // certificates, that the extension has unlimited sign permission for, will be |
| 165 // returned. |
| 166 // |callback| will be invoked with these certificates or an error message. |
| 127 // Will only call back during the lifetime of this object. | 167 // Will only call back during the lifetime of this object. |
| 128 // TODO(pneubeck): Add the interactive option and integrate the select | |
| 129 // certificate dialog. | |
| 130 void SelectClientCertificates( | 168 void SelectClientCertificates( |
| 131 const platform_keys::ClientCertificateRequest& request, | 169 const platform_keys::ClientCertificateRequest& request, |
| 170 bool interactive, |
| 132 const std::string& extension_id, | 171 const std::string& extension_id, |
| 133 const SelectCertificatesCallback& callback); | 172 const SelectCertificatesCallback& callback); |
| 134 | 173 |
| 135 private: | 174 private: |
| 136 using GetPlatformKeysCallback = | 175 using GetPlatformKeysCallback = |
| 137 base::Callback<void(scoped_ptr<base::ListValue> platform_keys)>; | 176 base::Callback<void(scoped_ptr<KeyEntries> platform_keys)>; |
| 138 | 177 |
| 178 enum SignPermission { ONCE, UNLIMITED }; |
| 179 |
| 180 class PermissionUpdateTask; |
| 181 class SelectTask; |
| 182 class SignTask; |
| 139 class Task; | 183 class Task; |
| 140 class SignTask; | |
| 141 class PermissionUpdateTask; | |
| 142 | 184 |
| 143 // Starts |task| eventually. To ensure that at most one |Task| is running at a | 185 // Starts |task| eventually. To ensure that at most one |Task| is running at a |
| 144 // time, it queues |task| for later execution if necessary. | 186 // time, it queues |task| for later execution if necessary. |
| 145 void StartOrQueueTask(scoped_ptr<Task> task); | 187 void StartOrQueueTask(scoped_ptr<Task> task); |
| 146 | 188 |
| 147 // Must be called after |task| is done. |task| will be invalid after this | 189 // Must be called after |task| is done. |task| will be invalid after this |
| 148 // call. This must not be called for any but the task that ran last. If any | 190 // call. This must not be called for any but the task that ran last. If any |
| 149 // other tasks are queued (see StartOrQueueTask()), it will start the next | 191 // other tasks are queued (see StartOrQueueTask()), it will start the next |
| 150 // one. | 192 // one. |
| 151 void TaskFinished(Task* task); | 193 void TaskFinished(Task* task); |
| 152 | 194 |
| 153 // Reads the list of public keys currently registered for |extension_id| from | 195 // Reads the list of public keys currently registered for |extension_id| from |
| 154 // StateStore. Calls |callback| with the read list, or a new empty list if | 196 // StateStore. Calls |callback| with the read list, or a new empty list if |
| 155 // none existed. If an error occurred, calls |callback| with NULL. | 197 // none existed. If an error occurred, calls |callback| with NULL. |
| 156 void GetPlatformKeysOfExtension(const std::string& extension_id, | 198 void GetPlatformKeysOfExtension(const std::string& extension_id, |
| 157 const GetPlatformKeysCallback& callback); | 199 const GetPlatformKeysCallback& callback); |
| 158 | 200 |
| 159 // Writes |platform_keys| to the state store of the extension with id | 201 // Writes |platform_keys| to the state store of the extension with id |
| 160 // |extension_id|. | 202 // |extension_id|. |
| 161 void SetPlatformKeysOfExtension(const std::string& extension_id, | 203 void SetPlatformKeysOfExtension(const std::string& extension_id, |
| 162 scoped_ptr<base::ListValue> platform_keys); | 204 const KeyEntries& platform_keys); |
| 163 | 205 |
| 164 // Callback used by |GenerateRSAKey|. | 206 // Callback used by |GenerateRSAKey|. |
| 165 // If the key generation was successful, registers the generated public key | 207 // If the key generation was successful, registers the generated public key |
| 166 // for the given extension. If any error occurs during key generation or | 208 // for the given extension. If any error occurs during key generation or |
| 167 // registration, calls |callback| with an error. Otherwise, on success, calls | 209 // registration, calls |callback| with an error. Otherwise, on success, calls |
| 168 // |callback| with the public key. | 210 // |callback| with the public key. |
| 169 void GeneratedKey(const std::string& extension_id, | 211 void GeneratedKey(const std::string& extension_id, |
| 170 const GenerateKeyCallback& callback, | 212 const GenerateKeyCallback& callback, |
| 171 const std::string& public_key_spki_der, | 213 const std::string& public_key_spki_der, |
| 172 const std::string& error_message); | 214 const std::string& error_message); |
| 173 | 215 |
| 174 // Callback used by |GeneratedKey|. | 216 // Callback used by |GeneratedKey|. |
| 175 // |public_key_spki_der| will contain the X.509 Subject Public Key Info of | 217 // |public_key_spki_der| will contain the X.509 Subject Public Key Info of |
| 176 // the generated key in DER encoding. |task| points to the finished |Task| | 218 // the generated key in DER encoding. |task| points to the finished |Task| |
| 177 // object. | 219 // object. |
| 178 void RegisteredGeneratedKey(const GenerateKeyCallback& callback, | 220 void RegisteredGeneratedKey(const GenerateKeyCallback& callback, |
| 179 const std::string& public_key_spki_der, | 221 const std::string& public_key_spki_der, |
| 180 Task* task); | 222 Task* task); |
| 181 | 223 |
| 182 // Calback used by |SelectClientCertificates|. | |
| 183 // If the certificate request could be processed successfully, |matches| will | |
| 184 // contain the list of matching certificates (maybe empty) and |error_message| | |
| 185 // will be empty. If an error occurred, |matches| will be null and | |
| 186 // |error_message| contain an error message. | |
| 187 void SelectClientCertificatesCallback( | |
| 188 const std::string& extension_id, | |
| 189 const SelectCertificatesCallback& callback, | |
| 190 scoped_ptr<net::CertificateList> matches, | |
| 191 const std::string& error_message); | |
| 192 | |
| 193 // Callback used by |GetPlatformKeysOfExtension|. | 224 // Callback used by |GetPlatformKeysOfExtension|. |
| 194 // Is called with |value| set to the PlatformKeys value read from the | 225 // Is called with |value| set to the PlatformKeys value read from the |
| 195 // StateStore, which it forwards to |callback|. On error, calls |callback| | 226 // StateStore, which it forwards to |callback|. On error, calls |callback| |
| 196 // with NULL; if no value existed, with an empty list. | 227 // with NULL; if no value existed, with an empty list. |
| 197 void GotPlatformKeysOfExtension(const std::string& extension_id, | 228 void GotPlatformKeysOfExtension(const std::string& extension_id, |
| 198 const GetPlatformKeysCallback& callback, | 229 const GetPlatformKeysCallback& callback, |
| 199 scoped_ptr<base::Value> value); | 230 scoped_ptr<base::Value> value); |
| 200 | 231 |
| 201 content::BrowserContext* browser_context_; | 232 content::BrowserContext* browser_context_; |
| 202 extensions::StateStore* state_store_; | 233 extensions::StateStore* state_store_; |
| 203 bool permission_check_enabled_ = true; | 234 scoped_ptr<SelectDelegate> select_delegate_; |
| 204 std::queue<linked_ptr<Task>> tasks_; | 235 std::queue<linked_ptr<Task>> tasks_; |
| 205 base::WeakPtrFactory<PlatformKeysService> weak_factory_; | 236 base::WeakPtrFactory<PlatformKeysService> weak_factory_; |
| 206 | 237 |
| 207 DISALLOW_COPY_AND_ASSIGN(PlatformKeysService); | 238 DISALLOW_COPY_AND_ASSIGN(PlatformKeysService); |
| 208 }; | 239 }; |
| 209 | 240 |
| 210 } // namespace chromeos | 241 } // namespace chromeos |
| 211 | 242 |
| 212 #endif // CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ | 243 #endif // CHROME_BROWSER_CHROMEOS_PLATFORM_KEYS_PLATFORM_KEYS_SERVICE_H_ |
| OLD | NEW |