[email protected] | cf5d32e5 | 2014-03-07 18:00:08 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 5 | #include "components/webcrypto/webcrypto_impl.h" |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 6 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 7 | #include <limits.h> |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 10 | #include <memory> |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 11 | #include <utility> |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 12 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 13 | #include "base/bind.h" |
| 14 | #include "base/lazy_instance.h" |
| 15 | #include "base/location.h" |
[email protected] | c1b944a | 2013-10-23 06:33:21 | [diff] [blame] | 16 | #include "base/logging.h" |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 17 | #include "base/macros.h" |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
| 19 | #include "base/task_runner.h" |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 20 | #include "base/threading/thread.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 21 | #include "base/threading/thread_task_runner_handle.h" |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 22 | #include "base/trace_event/trace_event.h" |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 23 | #include "components/webcrypto/algorithm_dispatch.h" |
| 24 | #include "components/webcrypto/crypto_data.h" |
| 25 | #include "components/webcrypto/generate_key_result.h" |
| 26 | #include "components/webcrypto/status.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 27 | #include "third_party/blink/public/platform/web_crypto_key_algorithm.h" |
| 28 | #include "third_party/blink/public/platform/web_string.h" |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 29 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 30 | namespace webcrypto { |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 31 | |
[email protected] | cca89748 | 2014-01-30 22:40:19 | [diff] [blame] | 32 | using webcrypto::Status; |
| 33 | |
[email protected] | 043cf1d3 | 2013-11-02 13:27:30 | [diff] [blame] | 34 | namespace { |
| 35 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 36 | // --------------------- |
| 37 | // Threading |
| 38 | // --------------------- |
| 39 | // |
| 40 | // WebCrypto operations can be slow. For instance generating an RSA key can |
eroman | 984ead6c | 2016-06-23 00:25:25 | [diff] [blame] | 41 | // take seconds. |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 42 | // |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 43 | // The strategy used here is to run a worker pool for all WebCrypto operations |
| 44 | // (except structured cloning). This same pool is also used by requests started |
| 45 | // from Blink Web Workers. |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 46 | // |
| 47 | // A few notes to keep in mind: |
| 48 | // |
eroman | 984ead6c | 2016-06-23 00:25:25 | [diff] [blame] | 49 | // * PostTaskAndReply() is not used because of how it handles failures -- it |
| 50 | // leaks the callback when failing to post back to the origin thread. |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 51 | // |
eroman | 984ead6c | 2016-06-23 00:25:25 | [diff] [blame] | 52 | // This is a problem since WebCrypto may be called from WebWorker threads, |
| 53 | // which may be aborted at any time. Leaking would be undesirable, and |
| 54 | // reachable in practice. |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 55 | // |
| 56 | // * blink::WebArrayBuffer is NOT threadsafe, and should therefore be allocated |
eroman | 984ead6c | 2016-06-23 00:25:25 | [diff] [blame] | 57 | // only on the target Blink thread. |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 58 | // |
| 59 | // TODO(eroman): Is there any way around this? Copying the result between |
| 60 | // threads is silly. |
| 61 | // |
eroman | 984ead6c | 2016-06-23 00:25:25 | [diff] [blame] | 62 | // * WebCryptoAlgorithm and WebCryptoKey are threadsafe, by virtue of being |
| 63 | // immutable. Internally asymmetric WebCryptoKeys wrap BoringSSL's EVP_PKEY. |
| 64 | // These are safe to use for BoringSSL operations across threads, provided |
| 65 | // the internals of the EVP_PKEY are not mutated (they never should be |
| 66 | // following ImportKey()). |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 67 | // |
| 68 | // * blink::WebCryptoResult is not threadsafe and should only be operated on |
| 69 | // the target Blink thread. HOWEVER, it is safe to delete it from any thread. |
| 70 | // This can happen if by the time the operation has completed in the crypto |
| 71 | // worker pool, the Blink worker thread that initiated the request is gone. |
| 72 | // Posting back to the origin thread will fail, and the WebCryptoResult will |
| 73 | // be deleted while running in the crypto worker pool. |
| 74 | class CryptoThreadPool { |
| 75 | public: |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 76 | CryptoThreadPool() : worker_thread_("WebCrypto") { |
| 77 | base::Thread::Options options; |
| 78 | options.joinable = false; |
| 79 | worker_thread_.StartWithOptions(options); |
| 80 | } |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 81 | |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 82 | static bool PostTask(const base::Location& from_here, base::OnceClosure task); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 83 | |
| 84 | private: |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 85 | // TODO(gab): the pool is currently using a single non-joinable thread to |
| 86 | // mimic the old behavior of using a CONTINUE_ON_SHUTDOWN SequencedTaskRunner |
| 87 | // on a single-threaded SequencedWorkerPool, but we'd like to consider using |
| 88 | // the TaskScheduler here and allowing multiple threads (SEQUENCED or even |
| 89 | // PARALLEL ExecutionMode: http://crbug.com/623700). |
| 90 | base::Thread worker_thread_; |
| 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(CryptoThreadPool); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | base::LazyInstance<CryptoThreadPool>::Leaky crypto_thread_pool = |
| 96 | LAZY_INSTANCE_INITIALIZER; |
| 97 | |
Brett Wilson | 1c99002 | 2017-09-12 20:11:15 | [diff] [blame] | 98 | bool CryptoThreadPool::PostTask(const base::Location& from_here, |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 99 | base::OnceClosure task) { |
gab | 2c979bb | 2016-09-14 02:36:47 | [diff] [blame] | 100 | return crypto_thread_pool.Get().worker_thread_.task_runner()->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 101 | from_here, std::move(task)); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void CompleteWithThreadPoolError(blink::WebCryptoResult* result) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 105 | result->CompleteWithError(blink::kWebCryptoErrorTypeOperation, |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 106 | "Failed posting to crypto worker pool"); |
| 107 | } |
| 108 | |
[email protected] | cca89748 | 2014-01-30 22:40:19 | [diff] [blame] | 109 | void CompleteWithError(const Status& status, blink::WebCryptoResult* result) { |
| 110 | DCHECK(status.IsError()); |
[email protected] | c503936 | 2014-04-28 19:10:34 | [diff] [blame] | 111 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 112 | result->CompleteWithError(status.error_type(), |
| 113 | blink::WebString::FromUTF8(status.error_details())); |
[email protected] | cca89748 | 2014-01-30 22:40:19 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 116 | void CompleteWithBufferOrError(const Status& status, |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 117 | const std::vector<uint8_t>& buffer, |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 118 | blink::WebCryptoResult* result) { |
| 119 | if (status.IsError()) { |
| 120 | CompleteWithError(status, result); |
| 121 | } else { |
| 122 | if (buffer.size() > UINT_MAX) { |
| 123 | // WebArrayBuffers have a smaller range than std::vector<>, so |
| 124 | // theoretically this could overflow. |
| 125 | CompleteWithError(Status::ErrorUnexpected(), result); |
| 126 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 127 | result->CompleteWithBuffer(buffer.data(), |
brettw | 690c9667 | 2015-04-21 16:19:54 | [diff] [blame] | 128 | static_cast<unsigned int>(buffer.size())); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void CompleteWithKeyOrError(const Status& status, |
| 134 | const blink::WebCryptoKey& key, |
| 135 | blink::WebCryptoResult* result) { |
| 136 | if (status.IsError()) { |
| 137 | CompleteWithError(status, result); |
| 138 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 139 | result->CompleteWithKey(key); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 143 | // -------------------------------------------------------------------- |
| 144 | // State |
| 145 | // -------------------------------------------------------------------- |
| 146 | // |
| 147 | // Explicit state classes are used rather than base::Bind(). This is done |
| 148 | // both for clarity, but also to avoid extraneous allocations for things |
| 149 | // like passing buffers and result objects between threads. |
| 150 | // |
| 151 | // BaseState is the base class common to all of the async operations, and |
| 152 | // keeps track of the thread to complete on, the error state, and the |
| 153 | // callback into Blink. |
| 154 | // |
| 155 | // Ownership of the State object is passed between the crypto thread and the |
| 156 | // Blink thread. Under normal completion it is destroyed on the Blink thread. |
| 157 | // However it may also be destroyed on the crypto thread if the Blink thread |
| 158 | // has vanished (which can happen for Blink web worker threads). |
| 159 | |
| 160 | struct BaseState { |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 161 | BaseState(const blink::WebCryptoResult& result, |
| 162 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 163 | : origin_thread(task_runner), result(result) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 164 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 165 | bool cancelled() { return result.Cancelled(); } |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 166 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 167 | scoped_refptr<base::TaskRunner> origin_thread; |
| 168 | |
| 169 | webcrypto::Status status; |
| 170 | blink::WebCryptoResult result; |
| 171 | |
| 172 | protected: |
| 173 | // Since there is no virtual destructor, must not delete directly as a |
| 174 | // BaseState. |
| 175 | ~BaseState() {} |
| 176 | }; |
| 177 | |
| 178 | struct EncryptState : public BaseState { |
| 179 | EncryptState(const blink::WebCryptoAlgorithm& algorithm, |
| 180 | const blink::WebCryptoKey& key, |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 181 | blink::WebVector<unsigned char> data, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 182 | const blink::WebCryptoResult& result, |
| 183 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 184 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 185 | algorithm(algorithm), |
| 186 | key(key), |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 187 | data(std::move(data)) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 188 | |
| 189 | const blink::WebCryptoAlgorithm algorithm; |
| 190 | const blink::WebCryptoKey key; |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 191 | const blink::WebVector<unsigned char> data; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 192 | |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 193 | std::vector<uint8_t> buffer; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | typedef EncryptState DecryptState; |
| 197 | typedef EncryptState DigestState; |
| 198 | |
| 199 | struct GenerateKeyState : public BaseState { |
| 200 | GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm, |
| 201 | bool extractable, |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 202 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 203 | const blink::WebCryptoResult& result, |
| 204 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 205 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 206 | algorithm(algorithm), |
| 207 | extractable(extractable), |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 208 | usages(usages) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 209 | |
| 210 | const blink::WebCryptoAlgorithm algorithm; |
| 211 | const bool extractable; |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 212 | const blink::WebCryptoKeyUsageMask usages; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 213 | |
eroman | 9b747eaf | 2014-10-18 22:03:28 | [diff] [blame] | 214 | webcrypto::GenerateKeyResult generate_key_result; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | struct ImportKeyState : public BaseState { |
| 218 | ImportKeyState(blink::WebCryptoKeyFormat format, |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 219 | blink::WebVector<unsigned char> key_data, |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 220 | const blink::WebCryptoAlgorithm& algorithm, |
| 221 | bool extractable, |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 222 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 223 | const blink::WebCryptoResult& result, |
| 224 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 225 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 226 | format(format), |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 227 | key_data(std::move(key_data)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 228 | algorithm(algorithm), |
| 229 | extractable(extractable), |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 230 | usages(usages) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 231 | |
| 232 | const blink::WebCryptoKeyFormat format; |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 233 | const blink::WebVector<unsigned char> key_data; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 234 | const blink::WebCryptoAlgorithm algorithm; |
| 235 | const bool extractable; |
eroman | 0e1d34e | 2014-10-21 19:13:31 | [diff] [blame] | 236 | const blink::WebCryptoKeyUsageMask usages; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 237 | |
| 238 | blink::WebCryptoKey key; |
| 239 | }; |
| 240 | |
| 241 | struct ExportKeyState : public BaseState { |
| 242 | ExportKeyState(blink::WebCryptoKeyFormat format, |
| 243 | const blink::WebCryptoKey& key, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 244 | const blink::WebCryptoResult& result, |
| 245 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 246 | : BaseState(result, std::move(task_runner)), format(format), key(key) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 247 | |
| 248 | const blink::WebCryptoKeyFormat format; |
| 249 | const blink::WebCryptoKey key; |
| 250 | |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 251 | std::vector<uint8_t> buffer; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | typedef EncryptState SignState; |
| 255 | |
| 256 | struct VerifySignatureState : public BaseState { |
| 257 | VerifySignatureState(const blink::WebCryptoAlgorithm& algorithm, |
| 258 | const blink::WebCryptoKey& key, |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 259 | blink::WebVector<unsigned char> signature, |
| 260 | blink::WebVector<unsigned char> data, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 261 | const blink::WebCryptoResult& result, |
| 262 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 263 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 264 | algorithm(algorithm), |
| 265 | key(key), |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 266 | signature(std::move(signature)), |
| 267 | data(std::move(data)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 268 | verify_result(false) {} |
| 269 | |
| 270 | const blink::WebCryptoAlgorithm algorithm; |
| 271 | const blink::WebCryptoKey key; |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 272 | blink::WebVector<unsigned char> signature; |
| 273 | blink::WebVector<unsigned char> data; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 274 | |
| 275 | bool verify_result; |
| 276 | }; |
| 277 | |
| 278 | struct WrapKeyState : public BaseState { |
| 279 | WrapKeyState(blink::WebCryptoKeyFormat format, |
| 280 | const blink::WebCryptoKey& key, |
| 281 | const blink::WebCryptoKey& wrapping_key, |
| 282 | const blink::WebCryptoAlgorithm& wrap_algorithm, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 283 | const blink::WebCryptoResult& result, |
| 284 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 285 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 286 | format(format), |
| 287 | key(key), |
| 288 | wrapping_key(wrapping_key), |
| 289 | wrap_algorithm(wrap_algorithm) {} |
| 290 | |
| 291 | const blink::WebCryptoKeyFormat format; |
| 292 | const blink::WebCryptoKey key; |
| 293 | const blink::WebCryptoKey wrapping_key; |
| 294 | const blink::WebCryptoAlgorithm wrap_algorithm; |
| 295 | |
[email protected] | 53b6c9d2 | 2014-07-19 05:08:38 | [diff] [blame] | 296 | std::vector<uint8_t> buffer; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 297 | }; |
| 298 | |
| 299 | struct UnwrapKeyState : public BaseState { |
| 300 | UnwrapKeyState(blink::WebCryptoKeyFormat format, |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 301 | blink::WebVector<unsigned char> wrapped_key, |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 302 | const blink::WebCryptoKey& wrapping_key, |
| 303 | const blink::WebCryptoAlgorithm& unwrap_algorithm, |
| 304 | const blink::WebCryptoAlgorithm& unwrapped_key_algorithm, |
| 305 | bool extractable, |
| 306 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 307 | const blink::WebCryptoResult& result, |
| 308 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 309 | : BaseState(result, std::move(task_runner)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 310 | format(format), |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 311 | wrapped_key(std::move(wrapped_key)), |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 312 | wrapping_key(wrapping_key), |
| 313 | unwrap_algorithm(unwrap_algorithm), |
| 314 | unwrapped_key_algorithm(unwrapped_key_algorithm), |
| 315 | extractable(extractable), |
eroman | 7b9591ab | 2014-10-21 17:13:01 | [diff] [blame] | 316 | usages(usages) {} |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 317 | |
| 318 | const blink::WebCryptoKeyFormat format; |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 319 | blink::WebVector<unsigned char> wrapped_key; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 320 | const blink::WebCryptoKey wrapping_key; |
| 321 | const blink::WebCryptoAlgorithm unwrap_algorithm; |
| 322 | const blink::WebCryptoAlgorithm unwrapped_key_algorithm; |
| 323 | const bool extractable; |
| 324 | const blink::WebCryptoKeyUsageMask usages; |
| 325 | |
| 326 | blink::WebCryptoKey unwrapped_key; |
| 327 | }; |
| 328 | |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 329 | struct DeriveBitsState : public BaseState { |
| 330 | DeriveBitsState(const blink::WebCryptoAlgorithm& algorithm, |
| 331 | const blink::WebCryptoKey& base_key, |
| 332 | unsigned int length_bits, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 333 | const blink::WebCryptoResult& result, |
| 334 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 335 | : BaseState(result, std::move(task_runner)), |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 336 | algorithm(algorithm), |
| 337 | base_key(base_key), |
| 338 | length_bits(length_bits) {} |
| 339 | |
| 340 | const blink::WebCryptoAlgorithm algorithm; |
| 341 | const blink::WebCryptoKey base_key; |
| 342 | const unsigned int length_bits; |
| 343 | |
| 344 | std::vector<uint8_t> derived_bytes; |
| 345 | }; |
| 346 | |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 347 | struct DeriveKeyState : public BaseState { |
| 348 | DeriveKeyState(const blink::WebCryptoAlgorithm& algorithm, |
| 349 | const blink::WebCryptoKey& base_key, |
| 350 | const blink::WebCryptoAlgorithm& import_algorithm, |
| 351 | const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 352 | bool extractable, |
| 353 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 354 | const blink::WebCryptoResult& result, |
| 355 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 356 | : BaseState(result, std::move(task_runner)), |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 357 | algorithm(algorithm), |
| 358 | base_key(base_key), |
| 359 | import_algorithm(import_algorithm), |
| 360 | key_length_algorithm(key_length_algorithm), |
| 361 | extractable(extractable), |
| 362 | usages(usages) {} |
| 363 | |
| 364 | const blink::WebCryptoAlgorithm algorithm; |
| 365 | const blink::WebCryptoKey base_key; |
| 366 | const blink::WebCryptoAlgorithm import_algorithm; |
| 367 | const blink::WebCryptoAlgorithm key_length_algorithm; |
| 368 | bool extractable; |
| 369 | blink::WebCryptoKeyUsageMask usages; |
| 370 | |
| 371 | blink::WebCryptoKey derived_key; |
| 372 | }; |
| 373 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 374 | // -------------------------------------------------------------------- |
| 375 | // Wrapper functions |
| 376 | // -------------------------------------------------------------------- |
| 377 | // |
| 378 | // * The methods named Do*() run on the crypto thread. |
| 379 | // * The methods named Do*Reply() run on the target Blink thread |
| 380 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 381 | void DoEncryptReply(std::unique_ptr<EncryptState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 382 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 383 | "DoEncryptReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 384 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 385 | } |
| 386 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 387 | void DoEncrypt(std::unique_ptr<EncryptState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 388 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoEncrypt"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 389 | EncryptState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 390 | if (state->cancelled()) |
| 391 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 392 | state->status = |
| 393 | webcrypto::Encrypt(state->algorithm, state->key, |
| 394 | webcrypto::CryptoData(state->data), &state->buffer); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 395 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 396 | FROM_HERE, base::BindOnce(DoEncryptReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 397 | } |
| 398 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 399 | void DoDecryptReply(std::unique_ptr<DecryptState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 400 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 401 | "DoDecryptReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 402 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 403 | } |
| 404 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 405 | void DoDecrypt(std::unique_ptr<DecryptState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 406 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDecrypt"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 407 | DecryptState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 408 | if (state->cancelled()) |
| 409 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 410 | state->status = |
| 411 | webcrypto::Decrypt(state->algorithm, state->key, |
| 412 | webcrypto::CryptoData(state->data), &state->buffer); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 413 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 414 | FROM_HERE, base::BindOnce(DoDecryptReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 415 | } |
| 416 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 417 | void DoDigestReply(std::unique_ptr<DigestState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 418 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigestReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 419 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 420 | } |
| 421 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 422 | void DoDigest(std::unique_ptr<DigestState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 423 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigest"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 424 | DigestState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 425 | if (state->cancelled()) |
| 426 | return; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 427 | state->status = webcrypto::Digest( |
| 428 | state->algorithm, webcrypto::CryptoData(state->data), &state->buffer); |
| 429 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 430 | FROM_HERE, base::BindOnce(DoDigestReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 431 | } |
| 432 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 433 | void DoGenerateKeyReply(std::unique_ptr<GenerateKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 434 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 435 | "DoGenerateKeyReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 436 | if (state->status.IsError()) { |
| 437 | CompleteWithError(state->status, &state->result); |
| 438 | } else { |
eroman | 9b747eaf | 2014-10-18 22:03:28 | [diff] [blame] | 439 | state->generate_key_result.Complete(&state->result); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 443 | void DoGenerateKey(std::unique_ptr<GenerateKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 444 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoGenerateKey"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 445 | GenerateKeyState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 446 | if (state->cancelled()) |
| 447 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 448 | state->status = |
| 449 | webcrypto::GenerateKey(state->algorithm, state->extractable, |
| 450 | state->usages, &state->generate_key_result); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 451 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 452 | FROM_HERE, base::BindOnce(DoGenerateKeyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 453 | } |
| 454 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 455 | void DoImportKeyReply(std::unique_ptr<ImportKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 456 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 457 | "DoImportKeyReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 458 | CompleteWithKeyOrError(state->status, state->key, &state->result); |
| 459 | } |
| 460 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 461 | void DoImportKey(std::unique_ptr<ImportKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 462 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoImportKey"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 463 | ImportKeyState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 464 | if (state->cancelled()) |
| 465 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 466 | state->status = webcrypto::ImportKey( |
| 467 | state->format, webcrypto::CryptoData(state->key_data), state->algorithm, |
| 468 | state->extractable, state->usages, &state->key); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 469 | if (state->status.IsSuccess()) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 470 | DCHECK(state->key.Handle()); |
| 471 | DCHECK(!state->key.Algorithm().IsNull()); |
| 472 | DCHECK_EQ(state->extractable, state->key.Extractable()); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 476 | FROM_HERE, base::BindOnce(DoImportKeyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 477 | } |
| 478 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 479 | void DoExportKeyReply(std::unique_ptr<ExportKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 480 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 481 | "DoExportKeyReply"); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 482 | if (state->format != blink::kWebCryptoKeyFormatJwk) { |
[email protected] | b7deaa08 | 2014-06-17 22:24:37 | [diff] [blame] | 483 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | if (state->status.IsError()) { |
| 488 | CompleteWithError(state->status, &state->result); |
| 489 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 490 | state->result.CompleteWithJson( |
davidben | aa62f38 | 2015-11-20 22:10:01 | [diff] [blame] | 491 | reinterpret_cast<const char*>(state->buffer.data()), |
brettw | 690c9667 | 2015-04-21 16:19:54 | [diff] [blame] | 492 | static_cast<unsigned int>(state->buffer.size())); |
[email protected] | b7deaa08 | 2014-06-17 22:24:37 | [diff] [blame] | 493 | } |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 494 | } |
| 495 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 496 | void DoExportKey(std::unique_ptr<ExportKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 497 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoExportKey"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 498 | ExportKeyState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 499 | if (state->cancelled()) |
| 500 | return; |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 501 | state->status = |
| 502 | webcrypto::ExportKey(state->format, state->key, &state->buffer); |
| 503 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 504 | FROM_HERE, base::BindOnce(DoExportKeyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 505 | } |
| 506 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 507 | void DoSignReply(std::unique_ptr<SignState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 508 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSignReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 509 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 510 | } |
| 511 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 512 | void DoSign(std::unique_ptr<SignState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 513 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSign"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 514 | SignState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 515 | if (state->cancelled()) |
| 516 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 517 | state->status = |
| 518 | webcrypto::Sign(state->algorithm, state->key, |
| 519 | webcrypto::CryptoData(state->data), &state->buffer); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 520 | |
| 521 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 522 | FROM_HERE, base::BindOnce(DoSignReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 523 | } |
| 524 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 525 | void DoVerifyReply(std::unique_ptr<VerifySignatureState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 526 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerifyReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 527 | if (state->status.IsError()) { |
| 528 | CompleteWithError(state->status, &state->result); |
| 529 | } else { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 530 | state->result.CompleteWithBoolean(state->verify_result); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 534 | void DoVerify(std::unique_ptr<VerifySignatureState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 535 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerify"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 536 | VerifySignatureState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 537 | if (state->cancelled()) |
| 538 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 539 | state->status = webcrypto::Verify( |
| 540 | state->algorithm, state->key, webcrypto::CryptoData(state->signature), |
| 541 | webcrypto::CryptoData(state->data), &state->verify_result); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 542 | |
| 543 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 544 | FROM_HERE, base::BindOnce(DoVerifyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 545 | } |
| 546 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 547 | void DoWrapKeyReply(std::unique_ptr<WrapKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 548 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 549 | "DoWrapKeyReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 550 | CompleteWithBufferOrError(state->status, state->buffer, &state->result); |
| 551 | } |
| 552 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 553 | void DoWrapKey(std::unique_ptr<WrapKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 554 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoWrapKey"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 555 | WrapKeyState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 556 | if (state->cancelled()) |
| 557 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 558 | state->status = |
| 559 | webcrypto::WrapKey(state->format, state->key, state->wrapping_key, |
| 560 | state->wrap_algorithm, &state->buffer); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 561 | |
| 562 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 563 | FROM_HERE, base::BindOnce(DoWrapKeyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 564 | } |
| 565 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 566 | void DoUnwrapKeyReply(std::unique_ptr<UnwrapKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 567 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 568 | "DoUnwrapKeyReply"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 569 | CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result); |
| 570 | } |
| 571 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 572 | void DoUnwrapKey(std::unique_ptr<UnwrapKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 573 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoUnwrapKey"); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 574 | UnwrapKeyState* state = passed_state.get(); |
[email protected] | 345bd20 | 2014-06-19 04:51:16 | [diff] [blame] | 575 | if (state->cancelled()) |
| 576 | return; |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 577 | state->status = webcrypto::UnwrapKey( |
| 578 | state->format, webcrypto::CryptoData(state->wrapped_key), |
| 579 | state->wrapping_key, state->unwrap_algorithm, |
| 580 | state->unwrapped_key_algorithm, state->extractable, state->usages, |
| 581 | &state->unwrapped_key); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 582 | |
| 583 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 584 | FROM_HERE, base::BindOnce(DoUnwrapKeyReply, std::move(passed_state))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 585 | } |
| 586 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 587 | void DoDeriveBitsReply(std::unique_ptr<DeriveBitsState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 588 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 589 | "DoDeriveBitsReply"); |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 590 | CompleteWithBufferOrError(state->status, state->derived_bytes, |
| 591 | &state->result); |
| 592 | } |
| 593 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 594 | void DoDeriveBits(std::unique_ptr<DeriveBitsState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 595 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveBits"); |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 596 | DeriveBitsState* state = passed_state.get(); |
| 597 | if (state->cancelled()) |
| 598 | return; |
| 599 | state->status = |
| 600 | webcrypto::DeriveBits(state->algorithm, state->base_key, |
| 601 | state->length_bits, &state->derived_bytes); |
| 602 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 603 | FROM_HERE, base::BindOnce(DoDeriveBitsReply, std::move(passed_state))); |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 604 | } |
| 605 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 606 | void DoDeriveKeyReply(std::unique_ptr<DeriveKeyState> state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 607 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), |
| 608 | "DoDeriveKeyReply"); |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 609 | CompleteWithKeyOrError(state->status, state->derived_key, &state->result); |
| 610 | } |
| 611 | |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 612 | void DoDeriveKey(std::unique_ptr<DeriveKeyState> passed_state) { |
Alexei Filippov | b4e75fa | 2017-11-23 04:44:21 | [diff] [blame] | 613 | TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveKey"); |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 614 | DeriveKeyState* state = passed_state.get(); |
| 615 | if (state->cancelled()) |
| 616 | return; |
| 617 | state->status = webcrypto::DeriveKey( |
| 618 | state->algorithm, state->base_key, state->import_algorithm, |
| 619 | state->key_length_algorithm, state->extractable, state->usages, |
| 620 | &state->derived_key); |
| 621 | state->origin_thread->PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 622 | FROM_HERE, base::BindOnce(DoDeriveKeyReply, std::move(passed_state))); |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 623 | } |
| 624 | |
[email protected] | 043cf1d3 | 2013-11-02 13:27:30 | [diff] [blame] | 625 | } // namespace |
| 626 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 627 | WebCryptoImpl::WebCryptoImpl() { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 628 | } |
[email protected] | 7e4c36f | 2013-09-12 06:10:19 | [diff] [blame] | 629 | |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 630 | WebCryptoImpl::~WebCryptoImpl() { |
| 631 | } |
[email protected] | 04166f8 | 2014-02-19 06:11:04 | [diff] [blame] | 632 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 633 | void WebCryptoImpl::Encrypt( |
| 634 | const blink::WebCryptoAlgorithm& algorithm, |
| 635 | const blink::WebCryptoKey& key, |
| 636 | blink::WebVector<unsigned char> data, |
| 637 | blink::WebCryptoResult result, |
| 638 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 639 | DCHECK(!algorithm.IsNull()); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 640 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 641 | std::unique_ptr<EncryptState> state(new EncryptState( |
| 642 | algorithm, key, std::move(data), result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 643 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 644 | FROM_HERE, base::BindOnce(DoEncrypt, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 645 | CompleteWithThreadPoolError(&result); |
| 646 | } |
[email protected] | a2a06c73 | 2013-09-27 10:50:54 | [diff] [blame] | 647 | } |
| 648 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 649 | void WebCryptoImpl::Decrypt( |
| 650 | const blink::WebCryptoAlgorithm& algorithm, |
| 651 | const blink::WebCryptoKey& key, |
| 652 | blink::WebVector<unsigned char> data, |
| 653 | blink::WebCryptoResult result, |
| 654 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 655 | DCHECK(!algorithm.IsNull()); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 656 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 657 | std::unique_ptr<DecryptState> state(new DecryptState( |
| 658 | algorithm, key, std::move(data), result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 659 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 660 | FROM_HERE, base::BindOnce(DoDecrypt, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 661 | CompleteWithThreadPoolError(&result); |
| 662 | } |
[email protected] | 868085a9 | 2013-10-01 00:42:30 | [diff] [blame] | 663 | } |
| 664 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 665 | void WebCryptoImpl::Digest( |
| 666 | const blink::WebCryptoAlgorithm& algorithm, |
| 667 | blink::WebVector<unsigned char> data, |
| 668 | blink::WebCryptoResult result, |
| 669 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 670 | DCHECK(!algorithm.IsNull()); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 671 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 672 | std::unique_ptr<DigestState> state( |
| 673 | new DigestState(algorithm, blink::WebCryptoKey::CreateNull(), |
| 674 | std::move(data), result, std::move(task_runner))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 675 | if (!CryptoThreadPool::PostTask(FROM_HERE, |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 676 | base::BindOnce(DoDigest, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 677 | CompleteWithThreadPoolError(&result); |
| 678 | } |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 679 | } |
| 680 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 681 | void WebCryptoImpl::GenerateKey( |
| 682 | const blink::WebCryptoAlgorithm& algorithm, |
| 683 | bool extractable, |
| 684 | blink::WebCryptoKeyUsageMask usages, |
| 685 | blink::WebCryptoResult result, |
| 686 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 687 | DCHECK(!algorithm.IsNull()); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 688 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 689 | std::unique_ptr<GenerateKeyState> state(new GenerateKeyState( |
| 690 | algorithm, extractable, usages, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 691 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 692 | FROM_HERE, base::BindOnce(DoGenerateKey, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 693 | CompleteWithThreadPoolError(&result); |
[email protected] | dfae8ab | 2013-10-10 19:45:06 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 697 | void WebCryptoImpl::ImportKey( |
| 698 | blink::WebCryptoKeyFormat format, |
| 699 | blink::WebVector<unsigned char> key_data, |
| 700 | const blink::WebCryptoAlgorithm& algorithm, |
| 701 | bool extractable, |
| 702 | blink::WebCryptoKeyUsageMask usages, |
| 703 | blink::WebCryptoResult result, |
| 704 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 705 | std::unique_ptr<ImportKeyState> state( |
| 706 | new ImportKeyState(format, std::move(key_data), algorithm, extractable, |
| 707 | usages, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 708 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 709 | FROM_HERE, base::BindOnce(DoImportKey, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 710 | CompleteWithThreadPoolError(&result); |
[email protected] | cca89748 | 2014-01-30 22:40:19 | [diff] [blame] | 711 | } |
[email protected] | 1c879bc9 | 2013-09-18 07:45:32 | [diff] [blame] | 712 | } |
| 713 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 714 | void WebCryptoImpl::ExportKey( |
| 715 | blink::WebCryptoKeyFormat format, |
| 716 | const blink::WebCryptoKey& key, |
| 717 | blink::WebCryptoResult result, |
| 718 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 719 | std::unique_ptr<ExportKeyState> state( |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 720 | new ExportKeyState(format, key, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 721 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 722 | FROM_HERE, base::BindOnce(DoExportKey, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 723 | CompleteWithThreadPoolError(&result); |
| 724 | } |
[email protected] | e9b2c10 | 2013-11-26 04:26:33 | [diff] [blame] | 725 | } |
| 726 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 727 | void WebCryptoImpl::Sign( |
| 728 | const blink::WebCryptoAlgorithm& algorithm, |
| 729 | const blink::WebCryptoKey& key, |
| 730 | blink::WebVector<unsigned char> data, |
| 731 | blink::WebCryptoResult result, |
| 732 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 733 | std::unique_ptr<SignState> state(new SignState( |
| 734 | algorithm, key, std::move(data), result, std::move(task_runner))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 735 | if (!CryptoThreadPool::PostTask(FROM_HERE, |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 736 | base::BindOnce(DoSign, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 737 | CompleteWithThreadPoolError(&result); |
| 738 | } |
[email protected] | 1c879bc9 | 2013-09-18 07:45:32 | [diff] [blame] | 739 | } |
| 740 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 741 | void WebCryptoImpl::VerifySignature( |
| 742 | const blink::WebCryptoAlgorithm& algorithm, |
| 743 | const blink::WebCryptoKey& key, |
| 744 | blink::WebVector<unsigned char> signature, |
| 745 | blink::WebVector<unsigned char> data, |
| 746 | blink::WebCryptoResult result, |
| 747 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 748 | std::unique_ptr<VerifySignatureState> state(new VerifySignatureState( |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 749 | algorithm, key, std::move(signature), std::move(data), result, |
| 750 | std::move(task_runner))); |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 751 | if (!CryptoThreadPool::PostTask(FROM_HERE, |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 752 | base::BindOnce(DoVerify, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 753 | CompleteWithThreadPoolError(&result); |
| 754 | } |
[email protected] | 3ed0026 | 2013-09-26 22:28:10 | [diff] [blame] | 755 | } |
| 756 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 757 | void WebCryptoImpl::WrapKey( |
| 758 | blink::WebCryptoKeyFormat format, |
| 759 | const blink::WebCryptoKey& key, |
| 760 | const blink::WebCryptoKey& wrapping_key, |
| 761 | const blink::WebCryptoAlgorithm& wrap_algorithm, |
| 762 | blink::WebCryptoResult result, |
| 763 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
dcheng | 7036d1e5 | 2016-04-21 23:13:03 | [diff] [blame] | 764 | std::unique_ptr<WrapKeyState> state( |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 765 | new WrapKeyState(format, key, wrapping_key, wrap_algorithm, result, |
| 766 | std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 767 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 768 | FROM_HERE, base::BindOnce(DoWrapKey, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 769 | CompleteWithThreadPoolError(&result); |
| 770 | } |
[email protected] | baa9284 | 2014-03-25 01:07:38 | [diff] [blame] | 771 | } |
| 772 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 773 | void WebCryptoImpl::UnwrapKey( |
[email protected] | baa9284 | 2014-03-25 01:07:38 | [diff] [blame] | 774 | blink::WebCryptoKeyFormat format, |
eroman | eeb3a0e | 2016-07-20 20:55:58 | [diff] [blame] | 775 | blink::WebVector<unsigned char> wrapped_key, |
[email protected] | baa9284 | 2014-03-25 01:07:38 | [diff] [blame] | 776 | const blink::WebCryptoKey& wrapping_key, |
| 777 | const blink::WebCryptoAlgorithm& unwrap_algorithm, |
| 778 | const blink::WebCryptoAlgorithm& unwrapped_key_algorithm, |
| 779 | bool extractable, |
| 780 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 781 | blink::WebCryptoResult result, |
| 782 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 783 | std::unique_ptr<UnwrapKeyState> state( |
| 784 | new UnwrapKeyState(format, std::move(wrapped_key), wrapping_key, |
| 785 | unwrap_algorithm, unwrapped_key_algorithm, extractable, |
| 786 | usages, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 787 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 788 | FROM_HERE, base::BindOnce(DoUnwrapKey, std::move(state)))) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 789 | CompleteWithThreadPoolError(&result); |
| 790 | } |
[email protected] | bd48e641 | 2014-02-22 08:32:53 | [diff] [blame] | 791 | } |
| 792 | |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 793 | void WebCryptoImpl::DeriveBits( |
| 794 | const blink::WebCryptoAlgorithm& algorithm, |
| 795 | const blink::WebCryptoKey& base_key, |
| 796 | unsigned int length_bits, |
| 797 | blink::WebCryptoResult result, |
| 798 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 799 | std::unique_ptr<DeriveBitsState> state(new DeriveBitsState( |
| 800 | algorithm, base_key, length_bits, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 801 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 802 | FROM_HERE, base::BindOnce(DoDeriveBits, std::move(state)))) { |
eroman | 1499b494 | 2014-11-26 19:59:53 | [diff] [blame] | 803 | CompleteWithThreadPoolError(&result); |
| 804 | } |
| 805 | } |
| 806 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 807 | void WebCryptoImpl::DeriveKey( |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 808 | const blink::WebCryptoAlgorithm& algorithm, |
| 809 | const blink::WebCryptoKey& base_key, |
| 810 | const blink::WebCryptoAlgorithm& import_algorithm, |
| 811 | const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 812 | bool extractable, |
| 813 | blink::WebCryptoKeyUsageMask usages, |
Hajime Hoshi | 757009b | 2018-01-16 06:42:40 | [diff] [blame] | 814 | blink::WebCryptoResult result, |
| 815 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 816 | std::unique_ptr<DeriveKeyState> state(new DeriveKeyState( |
| 817 | algorithm, base_key, import_algorithm, key_length_algorithm, extractable, |
| 818 | usages, result, std::move(task_runner))); |
dcheng | 0917ec4 | 2015-11-19 07:00:20 | [diff] [blame] | 819 | if (!CryptoThreadPool::PostTask( |
Claudio DeSouza | 591a997 | 2018-02-21 17:27:16 | [diff] [blame] | 820 | FROM_HERE, base::BindOnce(DoDeriveKey, std::move(state)))) { |
eroman | 20bf4c3c | 2014-12-12 17:22:37 | [diff] [blame] | 821 | CompleteWithThreadPoolError(&result); |
| 822 | } |
| 823 | } |
| 824 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 825 | std::unique_ptr<blink::WebCryptoDigestor> WebCryptoImpl::CreateDigestor( |
[email protected] | 6f778f0 | 2014-04-01 02:31:33 | [diff] [blame] | 826 | blink::WebCryptoAlgorithmId algorithm_id) { |
dcheng | 7578e8ee | 2016-07-13 03:52:45 | [diff] [blame] | 827 | return webcrypto::CreateDigestor(algorithm_id); |
[email protected] | 6f778f0 | 2014-04-01 02:31:33 | [diff] [blame] | 828 | } |
| 829 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 830 | bool WebCryptoImpl::DeserializeKeyForClone( |
[email protected] | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 831 | const blink::WebCryptoKeyAlgorithm& algorithm, |
| 832 | blink::WebCryptoKeyType type, |
| 833 | bool extractable, |
| 834 | blink::WebCryptoKeyUsageMask usages, |
| 835 | const unsigned char* key_data, |
| 836 | unsigned key_data_size, |
| 837 | blink::WebCryptoKey& key) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 838 | return webcrypto::DeserializeKeyForClone( |
eroman | 38bb4bd | 2014-11-24 23:47:06 | [diff] [blame] | 839 | algorithm, type, extractable, usages, |
| 840 | webcrypto::CryptoData(key_data, key_data_size), &key); |
[email protected] | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 841 | } |
| 842 | |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 843 | bool WebCryptoImpl::SerializeKeyForClone( |
[email protected] | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 844 | const blink::WebCryptoKey& key, |
| 845 | blink::WebVector<unsigned char>& key_data) { |
[email protected] | 88be9856 | 2014-04-30 11:18:59 | [diff] [blame] | 846 | return webcrypto::SerializeKeyForClone(key, &key_data); |
[email protected] | 5daca047 | 2014-03-18 20:27:08 | [diff] [blame] | 847 | } |
| 848 | |
erg | 56f1232 | 2015-04-17 00:51:48 | [diff] [blame] | 849 | } // namespace webcrypto |