blob: f84e18bc62d91e067be4173d428b858c806dec0a [file] [log] [blame]
[email protected]cf5d32e52014-03-07 18:00:081// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]408699c2013-07-17 21:23:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
erg56f12322015-04-17 00:51:485#include "components/webcrypto/webcrypto_impl.h"
[email protected]408699c2013-07-17 21:23:166
avi51ba3e692015-12-26 17:30:507#include <limits.h>
avi5dd91f82015-12-25 22:30:468#include <stdint.h>
9
dcheng7036d1e52016-04-21 23:13:0310#include <memory>
11
[email protected]88be98562014-04-30 11:18:5912#include "base/bind.h"
13#include "base/lazy_instance.h"
14#include "base/location.h"
[email protected]c1b944a2013-10-23 06:33:2115#include "base/logging.h"
gab2c979bb2016-09-14 02:36:4716#include "base/macros.h"
[email protected]88be98562014-04-30 11:18:5917#include "base/single_thread_task_runner.h"
18#include "base/task_runner.h"
gab2c979bb2016-09-14 02:36:4719#include "base/threading/thread.h"
gab7966d312016-05-11 20:35:0120#include "base/threading/thread_task_runner_handle.h"
Alexei Filippovb4e75fa2017-11-23 04:44:2121#include "base/trace_event/trace_event.h"
erg56f12322015-04-17 00:51:4822#include "components/webcrypto/algorithm_dispatch.h"
23#include "components/webcrypto/crypto_data.h"
24#include "components/webcrypto/generate_key_result.h"
25#include "components/webcrypto/status.h"
[email protected]8238bb1c2014-02-26 15:16:2526#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
[email protected]cf5d32e52014-03-07 18:00:0827#include "third_party/WebKit/public/platform/WebString.h"
[email protected]408699c2013-07-17 21:23:1628
erg56f12322015-04-17 00:51:4829namespace webcrypto {
[email protected]408699c2013-07-17 21:23:1630
[email protected]cca897482014-01-30 22:40:1931using webcrypto::Status;
32
[email protected]043cf1d32013-11-02 13:27:3033namespace {
34
[email protected]88be98562014-04-30 11:18:5935// ---------------------
36// Threading
37// ---------------------
38//
39// WebCrypto operations can be slow. For instance generating an RSA key can
eroman984ead6c2016-06-23 00:25:2540// take seconds.
[email protected]88be98562014-04-30 11:18:5941//
gab2c979bb2016-09-14 02:36:4742// The strategy used here is to run a worker pool for all WebCrypto operations
43// (except structured cloning). This same pool is also used by requests started
44// from Blink Web Workers.
[email protected]88be98562014-04-30 11:18:5945//
46// A few notes to keep in mind:
47//
eroman984ead6c2016-06-23 00:25:2548// * PostTaskAndReply() is not used because of how it handles failures -- it
49// leaks the callback when failing to post back to the origin thread.
[email protected]88be98562014-04-30 11:18:5950//
eroman984ead6c2016-06-23 00:25:2551// This is a problem since WebCrypto may be called from WebWorker threads,
52// which may be aborted at any time. Leaking would be undesirable, and
53// reachable in practice.
[email protected]88be98562014-04-30 11:18:5954//
55// * blink::WebArrayBuffer is NOT threadsafe, and should therefore be allocated
eroman984ead6c2016-06-23 00:25:2556// only on the target Blink thread.
[email protected]88be98562014-04-30 11:18:5957//
58// TODO(eroman): Is there any way around this? Copying the result between
59// threads is silly.
60//
eroman984ead6c2016-06-23 00:25:2561// * WebCryptoAlgorithm and WebCryptoKey are threadsafe, by virtue of being
62// immutable. Internally asymmetric WebCryptoKeys wrap BoringSSL's EVP_PKEY.
63// These are safe to use for BoringSSL operations across threads, provided
64// the internals of the EVP_PKEY are not mutated (they never should be
65// following ImportKey()).
[email protected]88be98562014-04-30 11:18:5966//
67// * blink::WebCryptoResult is not threadsafe and should only be operated on
68// the target Blink thread. HOWEVER, it is safe to delete it from any thread.
69// This can happen if by the time the operation has completed in the crypto
70// worker pool, the Blink worker thread that initiated the request is gone.
71// Posting back to the origin thread will fail, and the WebCryptoResult will
72// be deleted while running in the crypto worker pool.
73class CryptoThreadPool {
74 public:
gab2c979bb2016-09-14 02:36:4775 CryptoThreadPool() : worker_thread_("WebCrypto") {
76 base::Thread::Options options;
77 options.joinable = false;
78 worker_thread_.StartWithOptions(options);
79 }
[email protected]88be98562014-04-30 11:18:5980
Brett Wilson1c990022017-09-12 20:11:1581 static bool PostTask(const base::Location& from_here,
[email protected]88be98562014-04-30 11:18:5982 const base::Closure& task);
83
84 private:
gab2c979bb2016-09-14 02:36:4785 // 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]88be98562014-04-30 11:18:5993};
94
95base::LazyInstance<CryptoThreadPool>::Leaky crypto_thread_pool =
96 LAZY_INSTANCE_INITIALIZER;
97
Brett Wilson1c990022017-09-12 20:11:1598bool CryptoThreadPool::PostTask(const base::Location& from_here,
[email protected]88be98562014-04-30 11:18:5999 const base::Closure& task) {
gab2c979bb2016-09-14 02:36:47100 return crypto_thread_pool.Get().worker_thread_.task_runner()->PostTask(
101 from_here, task);
[email protected]88be98562014-04-30 11:18:59102}
103
104void CompleteWithThreadPoolError(blink::WebCryptoResult* result) {
Blink Reformat1c4d759e2017-04-09 16:34:54105 result->CompleteWithError(blink::kWebCryptoErrorTypeOperation,
[email protected]88be98562014-04-30 11:18:59106 "Failed posting to crypto worker pool");
107}
108
[email protected]cca897482014-01-30 22:40:19109void CompleteWithError(const Status& status, blink::WebCryptoResult* result) {
110 DCHECK(status.IsError());
[email protected]c5039362014-04-28 19:10:34111
Blink Reformat1c4d759e2017-04-09 16:34:54112 result->CompleteWithError(status.error_type(),
113 blink::WebString::FromUTF8(status.error_details()));
[email protected]cca897482014-01-30 22:40:19114}
115
[email protected]88be98562014-04-30 11:18:59116void CompleteWithBufferOrError(const Status& status,
[email protected]53b6c9d22014-07-19 05:08:38117 const std::vector<uint8_t>& buffer,
[email protected]88be98562014-04-30 11:18:59118 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 Reformat1c4d759e2017-04-09 16:34:54127 result->CompleteWithBuffer(buffer.data(),
brettw690c96672015-04-21 16:19:54128 static_cast<unsigned int>(buffer.size()));
[email protected]88be98562014-04-30 11:18:59129 }
130 }
131}
132
133void CompleteWithKeyOrError(const Status& status,
134 const blink::WebCryptoKey& key,
135 blink::WebCryptoResult* result) {
136 if (status.IsError()) {
137 CompleteWithError(status, result);
138 } else {
Blink Reformat1c4d759e2017-04-09 16:34:54139 result->CompleteWithKey(key);
[email protected]88be98562014-04-30 11:18:59140 }
141}
142
Sadrul Habib Chowdhury3a4b2d12015-02-04 01:10:42143// Gets a task runner for the current thread.
[email protected]88be98562014-04-30 11:18:59144scoped_refptr<base::TaskRunner> GetCurrentBlinkThread() {
Sadrul Habib Chowdhury3a4b2d12015-02-04 01:10:42145 DCHECK(base::ThreadTaskRunnerHandle::IsSet());
146 return base::ThreadTaskRunnerHandle::Get();
[email protected]88be98562014-04-30 11:18:59147}
148
149// --------------------------------------------------------------------
150// State
151// --------------------------------------------------------------------
152//
153// Explicit state classes are used rather than base::Bind(). This is done
154// both for clarity, but also to avoid extraneous allocations for things
155// like passing buffers and result objects between threads.
156//
157// BaseState is the base class common to all of the async operations, and
158// keeps track of the thread to complete on, the error state, and the
159// callback into Blink.
160//
161// Ownership of the State object is passed between the crypto thread and the
162// Blink thread. Under normal completion it is destroyed on the Blink thread.
163// However it may also be destroyed on the crypto thread if the Blink thread
164// has vanished (which can happen for Blink web worker threads).
165
166struct BaseState {
167 explicit BaseState(const blink::WebCryptoResult& result)
168 : origin_thread(GetCurrentBlinkThread()), result(result) {}
169
Blink Reformat1c4d759e2017-04-09 16:34:54170 bool cancelled() { return result.Cancelled(); }
[email protected]345bd202014-06-19 04:51:16171
[email protected]88be98562014-04-30 11:18:59172 scoped_refptr<base::TaskRunner> origin_thread;
173
174 webcrypto::Status status;
175 blink::WebCryptoResult result;
176
177 protected:
178 // Since there is no virtual destructor, must not delete directly as a
179 // BaseState.
180 ~BaseState() {}
181};
182
183struct EncryptState : public BaseState {
184 EncryptState(const blink::WebCryptoAlgorithm& algorithm,
185 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58186 blink::WebVector<unsigned char> data,
[email protected]88be98562014-04-30 11:18:59187 const blink::WebCryptoResult& result)
188 : BaseState(result),
189 algorithm(algorithm),
190 key(key),
eromaneeb3a0e2016-07-20 20:55:58191 data(std::move(data)) {}
[email protected]88be98562014-04-30 11:18:59192
193 const blink::WebCryptoAlgorithm algorithm;
194 const blink::WebCryptoKey key;
eromaneeb3a0e2016-07-20 20:55:58195 const blink::WebVector<unsigned char> data;
[email protected]88be98562014-04-30 11:18:59196
[email protected]53b6c9d22014-07-19 05:08:38197 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59198};
199
200typedef EncryptState DecryptState;
201typedef EncryptState DigestState;
202
203struct GenerateKeyState : public BaseState {
204 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm,
205 bool extractable,
eroman0e1d34e2014-10-21 19:13:31206 blink::WebCryptoKeyUsageMask usages,
[email protected]88be98562014-04-30 11:18:59207 const blink::WebCryptoResult& result)
208 : BaseState(result),
209 algorithm(algorithm),
210 extractable(extractable),
eroman0e1d34e2014-10-21 19:13:31211 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59212
213 const blink::WebCryptoAlgorithm algorithm;
214 const bool extractable;
eroman0e1d34e2014-10-21 19:13:31215 const blink::WebCryptoKeyUsageMask usages;
[email protected]88be98562014-04-30 11:18:59216
eroman9b747eaf2014-10-18 22:03:28217 webcrypto::GenerateKeyResult generate_key_result;
[email protected]88be98562014-04-30 11:18:59218};
219
220struct ImportKeyState : public BaseState {
221 ImportKeyState(blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58222 blink::WebVector<unsigned char> key_data,
[email protected]88be98562014-04-30 11:18:59223 const blink::WebCryptoAlgorithm& algorithm,
224 bool extractable,
eroman0e1d34e2014-10-21 19:13:31225 blink::WebCryptoKeyUsageMask usages,
[email protected]88be98562014-04-30 11:18:59226 const blink::WebCryptoResult& result)
227 : BaseState(result),
228 format(format),
eromaneeb3a0e2016-07-20 20:55:58229 key_data(std::move(key_data)),
[email protected]88be98562014-04-30 11:18:59230 algorithm(algorithm),
231 extractable(extractable),
eroman0e1d34e2014-10-21 19:13:31232 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59233
234 const blink::WebCryptoKeyFormat format;
eromaneeb3a0e2016-07-20 20:55:58235 const blink::WebVector<unsigned char> key_data;
[email protected]88be98562014-04-30 11:18:59236 const blink::WebCryptoAlgorithm algorithm;
237 const bool extractable;
eroman0e1d34e2014-10-21 19:13:31238 const blink::WebCryptoKeyUsageMask usages;
[email protected]88be98562014-04-30 11:18:59239
240 blink::WebCryptoKey key;
241};
242
243struct ExportKeyState : public BaseState {
244 ExportKeyState(blink::WebCryptoKeyFormat format,
245 const blink::WebCryptoKey& key,
246 const blink::WebCryptoResult& result)
247 : BaseState(result), format(format), key(key) {}
248
249 const blink::WebCryptoKeyFormat format;
250 const blink::WebCryptoKey key;
251
[email protected]53b6c9d22014-07-19 05:08:38252 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59253};
254
255typedef EncryptState SignState;
256
257struct VerifySignatureState : public BaseState {
258 VerifySignatureState(const blink::WebCryptoAlgorithm& algorithm,
259 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58260 blink::WebVector<unsigned char> signature,
261 blink::WebVector<unsigned char> data,
[email protected]88be98562014-04-30 11:18:59262 const blink::WebCryptoResult& result)
263 : BaseState(result),
264 algorithm(algorithm),
265 key(key),
eromaneeb3a0e2016-07-20 20:55:58266 signature(std::move(signature)),
267 data(std::move(data)),
[email protected]88be98562014-04-30 11:18:59268 verify_result(false) {}
269
270 const blink::WebCryptoAlgorithm algorithm;
271 const blink::WebCryptoKey key;
eromaneeb3a0e2016-07-20 20:55:58272 blink::WebVector<unsigned char> signature;
273 blink::WebVector<unsigned char> data;
[email protected]88be98562014-04-30 11:18:59274
275 bool verify_result;
276};
277
278struct 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,
283 const blink::WebCryptoResult& result)
284 : BaseState(result),
285 format(format),
286 key(key),
287 wrapping_key(wrapping_key),
288 wrap_algorithm(wrap_algorithm) {}
289
290 const blink::WebCryptoKeyFormat format;
291 const blink::WebCryptoKey key;
292 const blink::WebCryptoKey wrapping_key;
293 const blink::WebCryptoAlgorithm wrap_algorithm;
294
[email protected]53b6c9d22014-07-19 05:08:38295 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59296};
297
298struct UnwrapKeyState : public BaseState {
299 UnwrapKeyState(blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58300 blink::WebVector<unsigned char> wrapped_key,
[email protected]88be98562014-04-30 11:18:59301 const blink::WebCryptoKey& wrapping_key,
302 const blink::WebCryptoAlgorithm& unwrap_algorithm,
303 const blink::WebCryptoAlgorithm& unwrapped_key_algorithm,
304 bool extractable,
305 blink::WebCryptoKeyUsageMask usages,
306 const blink::WebCryptoResult& result)
307 : BaseState(result),
308 format(format),
eromaneeb3a0e2016-07-20 20:55:58309 wrapped_key(std::move(wrapped_key)),
[email protected]88be98562014-04-30 11:18:59310 wrapping_key(wrapping_key),
311 unwrap_algorithm(unwrap_algorithm),
312 unwrapped_key_algorithm(unwrapped_key_algorithm),
313 extractable(extractable),
eroman7b9591ab2014-10-21 17:13:01314 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59315
316 const blink::WebCryptoKeyFormat format;
eromaneeb3a0e2016-07-20 20:55:58317 blink::WebVector<unsigned char> wrapped_key;
[email protected]88be98562014-04-30 11:18:59318 const blink::WebCryptoKey wrapping_key;
319 const blink::WebCryptoAlgorithm unwrap_algorithm;
320 const blink::WebCryptoAlgorithm unwrapped_key_algorithm;
321 const bool extractable;
322 const blink::WebCryptoKeyUsageMask usages;
323
324 blink::WebCryptoKey unwrapped_key;
325};
326
eroman1499b4942014-11-26 19:59:53327struct DeriveBitsState : public BaseState {
328 DeriveBitsState(const blink::WebCryptoAlgorithm& algorithm,
329 const blink::WebCryptoKey& base_key,
330 unsigned int length_bits,
331 const blink::WebCryptoResult& result)
332 : BaseState(result),
333 algorithm(algorithm),
334 base_key(base_key),
335 length_bits(length_bits) {}
336
337 const blink::WebCryptoAlgorithm algorithm;
338 const blink::WebCryptoKey base_key;
339 const unsigned int length_bits;
340
341 std::vector<uint8_t> derived_bytes;
342};
343
eroman20bf4c3c2014-12-12 17:22:37344struct DeriveKeyState : public BaseState {
345 DeriveKeyState(const blink::WebCryptoAlgorithm& algorithm,
346 const blink::WebCryptoKey& base_key,
347 const blink::WebCryptoAlgorithm& import_algorithm,
348 const blink::WebCryptoAlgorithm& key_length_algorithm,
349 bool extractable,
350 blink::WebCryptoKeyUsageMask usages,
351 const blink::WebCryptoResult& result)
352 : BaseState(result),
353 algorithm(algorithm),
354 base_key(base_key),
355 import_algorithm(import_algorithm),
356 key_length_algorithm(key_length_algorithm),
357 extractable(extractable),
358 usages(usages) {}
359
360 const blink::WebCryptoAlgorithm algorithm;
361 const blink::WebCryptoKey base_key;
362 const blink::WebCryptoAlgorithm import_algorithm;
363 const blink::WebCryptoAlgorithm key_length_algorithm;
364 bool extractable;
365 blink::WebCryptoKeyUsageMask usages;
366
367 blink::WebCryptoKey derived_key;
368};
369
[email protected]88be98562014-04-30 11:18:59370// --------------------------------------------------------------------
371// Wrapper functions
372// --------------------------------------------------------------------
373//
374// * The methods named Do*() run on the crypto thread.
375// * The methods named Do*Reply() run on the target Blink thread
376
dcheng7036d1e52016-04-21 23:13:03377void DoEncryptReply(std::unique_ptr<EncryptState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21378 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
379 "DoEncryptReply");
[email protected]88be98562014-04-30 11:18:59380 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
381}
382
dcheng7036d1e52016-04-21 23:13:03383void DoEncrypt(std::unique_ptr<EncryptState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21384 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoEncrypt");
[email protected]88be98562014-04-30 11:18:59385 EncryptState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16386 if (state->cancelled())
387 return;
eroman38bb4bd2014-11-24 23:47:06388 state->status =
389 webcrypto::Encrypt(state->algorithm, state->key,
390 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59391 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20392 FROM_HERE, base::Bind(DoEncryptReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59393}
394
dcheng7036d1e52016-04-21 23:13:03395void DoDecryptReply(std::unique_ptr<DecryptState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21396 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
397 "DoDecryptReply");
[email protected]88be98562014-04-30 11:18:59398 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
399}
400
dcheng7036d1e52016-04-21 23:13:03401void DoDecrypt(std::unique_ptr<DecryptState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21402 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDecrypt");
[email protected]88be98562014-04-30 11:18:59403 DecryptState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16404 if (state->cancelled())
405 return;
eroman38bb4bd2014-11-24 23:47:06406 state->status =
407 webcrypto::Decrypt(state->algorithm, state->key,
408 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59409 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20410 FROM_HERE, base::Bind(DoDecryptReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59411}
412
dcheng7036d1e52016-04-21 23:13:03413void DoDigestReply(std::unique_ptr<DigestState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21414 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigestReply");
[email protected]88be98562014-04-30 11:18:59415 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
416}
417
dcheng7036d1e52016-04-21 23:13:03418void DoDigest(std::unique_ptr<DigestState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21419 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigest");
[email protected]88be98562014-04-30 11:18:59420 DigestState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16421 if (state->cancelled())
422 return;
[email protected]88be98562014-04-30 11:18:59423 state->status = webcrypto::Digest(
424 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer);
425 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20426 FROM_HERE, base::Bind(DoDigestReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59427}
428
dcheng7036d1e52016-04-21 23:13:03429void DoGenerateKeyReply(std::unique_ptr<GenerateKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21430 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
431 "DoGenerateKeyReply");
[email protected]88be98562014-04-30 11:18:59432 if (state->status.IsError()) {
433 CompleteWithError(state->status, &state->result);
434 } else {
eroman9b747eaf2014-10-18 22:03:28435 state->generate_key_result.Complete(&state->result);
[email protected]88be98562014-04-30 11:18:59436 }
437}
438
dcheng7036d1e52016-04-21 23:13:03439void DoGenerateKey(std::unique_ptr<GenerateKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21440 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoGenerateKey");
[email protected]88be98562014-04-30 11:18:59441 GenerateKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16442 if (state->cancelled())
443 return;
eroman38bb4bd2014-11-24 23:47:06444 state->status =
445 webcrypto::GenerateKey(state->algorithm, state->extractable,
446 state->usages, &state->generate_key_result);
[email protected]88be98562014-04-30 11:18:59447 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20448 FROM_HERE, base::Bind(DoGenerateKeyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59449}
450
dcheng7036d1e52016-04-21 23:13:03451void DoImportKeyReply(std::unique_ptr<ImportKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21452 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
453 "DoImportKeyReply");
[email protected]88be98562014-04-30 11:18:59454 CompleteWithKeyOrError(state->status, state->key, &state->result);
455}
456
dcheng7036d1e52016-04-21 23:13:03457void DoImportKey(std::unique_ptr<ImportKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21458 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoImportKey");
[email protected]88be98562014-04-30 11:18:59459 ImportKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16460 if (state->cancelled())
461 return;
eroman38bb4bd2014-11-24 23:47:06462 state->status = webcrypto::ImportKey(
463 state->format, webcrypto::CryptoData(state->key_data), state->algorithm,
464 state->extractable, state->usages, &state->key);
[email protected]88be98562014-04-30 11:18:59465 if (state->status.IsSuccess()) {
Blink Reformat1c4d759e2017-04-09 16:34:54466 DCHECK(state->key.Handle());
467 DCHECK(!state->key.Algorithm().IsNull());
468 DCHECK_EQ(state->extractable, state->key.Extractable());
[email protected]88be98562014-04-30 11:18:59469 }
470
471 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20472 FROM_HERE, base::Bind(DoImportKeyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59473}
474
dcheng7036d1e52016-04-21 23:13:03475void DoExportKeyReply(std::unique_ptr<ExportKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21476 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
477 "DoExportKeyReply");
Blink Reformat1c4d759e2017-04-09 16:34:54478 if (state->format != blink::kWebCryptoKeyFormatJwk) {
[email protected]b7deaa082014-06-17 22:24:37479 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
480 return;
481 }
482
483 if (state->status.IsError()) {
484 CompleteWithError(state->status, &state->result);
485 } else {
Blink Reformat1c4d759e2017-04-09 16:34:54486 state->result.CompleteWithJson(
davidbenaa62f382015-11-20 22:10:01487 reinterpret_cast<const char*>(state->buffer.data()),
brettw690c96672015-04-21 16:19:54488 static_cast<unsigned int>(state->buffer.size()));
[email protected]b7deaa082014-06-17 22:24:37489 }
[email protected]88be98562014-04-30 11:18:59490}
491
dcheng7036d1e52016-04-21 23:13:03492void DoExportKey(std::unique_ptr<ExportKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21493 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoExportKey");
[email protected]88be98562014-04-30 11:18:59494 ExportKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16495 if (state->cancelled())
496 return;
[email protected]88be98562014-04-30 11:18:59497 state->status =
498 webcrypto::ExportKey(state->format, state->key, &state->buffer);
499 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20500 FROM_HERE, base::Bind(DoExportKeyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59501}
502
dcheng7036d1e52016-04-21 23:13:03503void DoSignReply(std::unique_ptr<SignState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21504 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSignReply");
[email protected]88be98562014-04-30 11:18:59505 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
506}
507
dcheng7036d1e52016-04-21 23:13:03508void DoSign(std::unique_ptr<SignState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21509 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSign");
[email protected]88be98562014-04-30 11:18:59510 SignState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16511 if (state->cancelled())
512 return;
eroman38bb4bd2014-11-24 23:47:06513 state->status =
514 webcrypto::Sign(state->algorithm, state->key,
515 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59516
517 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20518 FROM_HERE, base::Bind(DoSignReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59519}
520
dcheng7036d1e52016-04-21 23:13:03521void DoVerifyReply(std::unique_ptr<VerifySignatureState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21522 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerifyReply");
[email protected]88be98562014-04-30 11:18:59523 if (state->status.IsError()) {
524 CompleteWithError(state->status, &state->result);
525 } else {
Blink Reformat1c4d759e2017-04-09 16:34:54526 state->result.CompleteWithBoolean(state->verify_result);
[email protected]88be98562014-04-30 11:18:59527 }
528}
529
dcheng7036d1e52016-04-21 23:13:03530void DoVerify(std::unique_ptr<VerifySignatureState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21531 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerify");
[email protected]88be98562014-04-30 11:18:59532 VerifySignatureState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16533 if (state->cancelled())
534 return;
eroman38bb4bd2014-11-24 23:47:06535 state->status = webcrypto::Verify(
536 state->algorithm, state->key, webcrypto::CryptoData(state->signature),
537 webcrypto::CryptoData(state->data), &state->verify_result);
[email protected]88be98562014-04-30 11:18:59538
539 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20540 FROM_HERE, base::Bind(DoVerifyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59541}
542
dcheng7036d1e52016-04-21 23:13:03543void DoWrapKeyReply(std::unique_ptr<WrapKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21544 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
545 "DoWrapKeyReply");
[email protected]88be98562014-04-30 11:18:59546 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
547}
548
dcheng7036d1e52016-04-21 23:13:03549void DoWrapKey(std::unique_ptr<WrapKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21550 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoWrapKey");
[email protected]88be98562014-04-30 11:18:59551 WrapKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16552 if (state->cancelled())
553 return;
eroman38bb4bd2014-11-24 23:47:06554 state->status =
555 webcrypto::WrapKey(state->format, state->key, state->wrapping_key,
556 state->wrap_algorithm, &state->buffer);
[email protected]88be98562014-04-30 11:18:59557
558 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20559 FROM_HERE, base::Bind(DoWrapKeyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59560}
561
dcheng7036d1e52016-04-21 23:13:03562void DoUnwrapKeyReply(std::unique_ptr<UnwrapKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21563 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
564 "DoUnwrapKeyReply");
[email protected]88be98562014-04-30 11:18:59565 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result);
566}
567
dcheng7036d1e52016-04-21 23:13:03568void DoUnwrapKey(std::unique_ptr<UnwrapKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21569 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoUnwrapKey");
[email protected]88be98562014-04-30 11:18:59570 UnwrapKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16571 if (state->cancelled())
572 return;
eroman38bb4bd2014-11-24 23:47:06573 state->status = webcrypto::UnwrapKey(
574 state->format, webcrypto::CryptoData(state->wrapped_key),
575 state->wrapping_key, state->unwrap_algorithm,
576 state->unwrapped_key_algorithm, state->extractable, state->usages,
577 &state->unwrapped_key);
[email protected]88be98562014-04-30 11:18:59578
579 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20580 FROM_HERE, base::Bind(DoUnwrapKeyReply, base::Passed(&passed_state)));
[email protected]88be98562014-04-30 11:18:59581}
582
dcheng7036d1e52016-04-21 23:13:03583void DoDeriveBitsReply(std::unique_ptr<DeriveBitsState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21584 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
585 "DoDeriveBitsReply");
eroman1499b4942014-11-26 19:59:53586 CompleteWithBufferOrError(state->status, state->derived_bytes,
587 &state->result);
588}
589
dcheng7036d1e52016-04-21 23:13:03590void DoDeriveBits(std::unique_ptr<DeriveBitsState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21591 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveBits");
eroman1499b4942014-11-26 19:59:53592 DeriveBitsState* state = passed_state.get();
593 if (state->cancelled())
594 return;
595 state->status =
596 webcrypto::DeriveBits(state->algorithm, state->base_key,
597 state->length_bits, &state->derived_bytes);
598 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20599 FROM_HERE, base::Bind(DoDeriveBitsReply, base::Passed(&passed_state)));
eroman1499b4942014-11-26 19:59:53600}
601
dcheng7036d1e52016-04-21 23:13:03602void DoDeriveKeyReply(std::unique_ptr<DeriveKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21603 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
604 "DoDeriveKeyReply");
eroman20bf4c3c2014-12-12 17:22:37605 CompleteWithKeyOrError(state->status, state->derived_key, &state->result);
606}
607
dcheng7036d1e52016-04-21 23:13:03608void DoDeriveKey(std::unique_ptr<DeriveKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21609 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveKey");
eroman20bf4c3c2014-12-12 17:22:37610 DeriveKeyState* state = passed_state.get();
611 if (state->cancelled())
612 return;
613 state->status = webcrypto::DeriveKey(
614 state->algorithm, state->base_key, state->import_algorithm,
615 state->key_length_algorithm, state->extractable, state->usages,
616 &state->derived_key);
617 state->origin_thread->PostTask(
dcheng0917ec42015-11-19 07:00:20618 FROM_HERE, base::Bind(DoDeriveKeyReply, base::Passed(&passed_state)));
eroman20bf4c3c2014-12-12 17:22:37619}
620
[email protected]043cf1d32013-11-02 13:27:30621} // namespace
622
[email protected]88be98562014-04-30 11:18:59623WebCryptoImpl::WebCryptoImpl() {
[email protected]88be98562014-04-30 11:18:59624}
[email protected]7e4c36f2013-09-12 06:10:19625
[email protected]88be98562014-04-30 11:18:59626WebCryptoImpl::~WebCryptoImpl() {
627}
[email protected]04166f82014-02-19 06:11:04628
Blink Reformat1c4d759e2017-04-09 16:34:54629void WebCryptoImpl::Encrypt(const blink::WebCryptoAlgorithm& algorithm,
[email protected]a60326552014-02-19 22:58:24630 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58631 blink::WebVector<unsigned char> data,
[email protected]a60326552014-02-19 22:58:24632 blink::WebCryptoResult result) {
Blink Reformat1c4d759e2017-04-09 16:34:54633 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59634
dcheng7036d1e52016-04-21 23:13:03635 std::unique_ptr<EncryptState> state(
eromaneeb3a0e2016-07-20 20:55:58636 new EncryptState(algorithm, key, std::move(data), result));
dcheng0917ec42015-11-19 07:00:20637 if (!CryptoThreadPool::PostTask(
638 FROM_HERE, base::Bind(DoEncrypt, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59639 CompleteWithThreadPoolError(&result);
640 }
[email protected]a2a06c732013-09-27 10:50:54641}
642
Blink Reformat1c4d759e2017-04-09 16:34:54643void WebCryptoImpl::Decrypt(const blink::WebCryptoAlgorithm& algorithm,
[email protected]a60326552014-02-19 22:58:24644 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58645 blink::WebVector<unsigned char> data,
[email protected]a60326552014-02-19 22:58:24646 blink::WebCryptoResult result) {
Blink Reformat1c4d759e2017-04-09 16:34:54647 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59648
dcheng7036d1e52016-04-21 23:13:03649 std::unique_ptr<DecryptState> state(
eromaneeb3a0e2016-07-20 20:55:58650 new DecryptState(algorithm, key, std::move(data), result));
dcheng0917ec42015-11-19 07:00:20651 if (!CryptoThreadPool::PostTask(
652 FROM_HERE, base::Bind(DoDecrypt, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59653 CompleteWithThreadPoolError(&result);
654 }
[email protected]868085a92013-10-01 00:42:30655}
656
Blink Reformat1c4d759e2017-04-09 16:34:54657void WebCryptoImpl::Digest(const blink::WebCryptoAlgorithm& algorithm,
eromaneeb3a0e2016-07-20 20:55:58658 blink::WebVector<unsigned char> data,
[email protected]a60326552014-02-19 22:58:24659 blink::WebCryptoResult result) {
Blink Reformat1c4d759e2017-04-09 16:34:54660 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59661
dcheng7036d1e52016-04-21 23:13:03662 std::unique_ptr<DigestState> state(new DigestState(
Blink Reformat1c4d759e2017-04-09 16:34:54663 algorithm, blink::WebCryptoKey::CreateNull(), std::move(data), result));
[email protected]88be98562014-04-30 11:18:59664 if (!CryptoThreadPool::PostTask(FROM_HERE,
dcheng0917ec42015-11-19 07:00:20665 base::Bind(DoDigest, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59666 CompleteWithThreadPoolError(&result);
667 }
[email protected]408699c2013-07-17 21:23:16668}
669
Blink Reformat1c4d759e2017-04-09 16:34:54670void WebCryptoImpl::GenerateKey(const blink::WebCryptoAlgorithm& algorithm,
[email protected]a60326552014-02-19 22:58:24671 bool extractable,
eroman0e1d34e2014-10-21 19:13:31672 blink::WebCryptoKeyUsageMask usages,
[email protected]a60326552014-02-19 22:58:24673 blink::WebCryptoResult result) {
Blink Reformat1c4d759e2017-04-09 16:34:54674 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59675
dcheng7036d1e52016-04-21 23:13:03676 std::unique_ptr<GenerateKeyState> state(
eroman0e1d34e2014-10-21 19:13:31677 new GenerateKeyState(algorithm, extractable, usages, result));
dcheng0917ec42015-11-19 07:00:20678 if (!CryptoThreadPool::PostTask(
679 FROM_HERE, base::Bind(DoGenerateKey, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59680 CompleteWithThreadPoolError(&result);
[email protected]dfae8ab2013-10-10 19:45:06681 }
682}
683
Blink Reformat1c4d759e2017-04-09 16:34:54684void WebCryptoImpl::ImportKey(blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58685 blink::WebVector<unsigned char> key_data,
[email protected]c7a94682014-03-20 22:58:40686 const blink::WebCryptoAlgorithm& algorithm,
687 bool extractable,
eroman0e1d34e2014-10-21 19:13:31688 blink::WebCryptoKeyUsageMask usages,
[email protected]c7a94682014-03-20 22:58:40689 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03690 std::unique_ptr<ImportKeyState> state(new ImportKeyState(
eromaneeb3a0e2016-07-20 20:55:58691 format, std::move(key_data), algorithm, extractable, usages, result));
dcheng0917ec42015-11-19 07:00:20692 if (!CryptoThreadPool::PostTask(
693 FROM_HERE, base::Bind(DoImportKey, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59694 CompleteWithThreadPoolError(&result);
[email protected]cca897482014-01-30 22:40:19695 }
[email protected]1c879bc92013-09-18 07:45:32696}
697
Blink Reformat1c4d759e2017-04-09 16:34:54698void WebCryptoImpl::ExportKey(blink::WebCryptoKeyFormat format,
[email protected]a60326552014-02-19 22:58:24699 const blink::WebCryptoKey& key,
700 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03701 std::unique_ptr<ExportKeyState> state(
702 new ExportKeyState(format, key, result));
dcheng0917ec42015-11-19 07:00:20703 if (!CryptoThreadPool::PostTask(
704 FROM_HERE, base::Bind(DoExportKey, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59705 CompleteWithThreadPoolError(&result);
706 }
[email protected]e9b2c102013-11-26 04:26:33707}
708
Blink Reformat1c4d759e2017-04-09 16:34:54709void WebCryptoImpl::Sign(const blink::WebCryptoAlgorithm& algorithm,
[email protected]a60326552014-02-19 22:58:24710 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58711 blink::WebVector<unsigned char> data,
[email protected]a60326552014-02-19 22:58:24712 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03713 std::unique_ptr<SignState> state(
eromaneeb3a0e2016-07-20 20:55:58714 new SignState(algorithm, key, std::move(data), result));
[email protected]88be98562014-04-30 11:18:59715 if (!CryptoThreadPool::PostTask(FROM_HERE,
dcheng0917ec42015-11-19 07:00:20716 base::Bind(DoSign, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59717 CompleteWithThreadPoolError(&result);
718 }
[email protected]1c879bc92013-09-18 07:45:32719}
720
Blink Reformat1c4d759e2017-04-09 16:34:54721void WebCryptoImpl::VerifySignature(const blink::WebCryptoAlgorithm& algorithm,
[email protected]a60326552014-02-19 22:58:24722 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58723 blink::WebVector<unsigned char> signature,
724 blink::WebVector<unsigned char> data,
[email protected]a60326552014-02-19 22:58:24725 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03726 std::unique_ptr<VerifySignatureState> state(new VerifySignatureState(
eromaneeb3a0e2016-07-20 20:55:58727 algorithm, key, std::move(signature), std::move(data), result));
[email protected]88be98562014-04-30 11:18:59728 if (!CryptoThreadPool::PostTask(FROM_HERE,
dcheng0917ec42015-11-19 07:00:20729 base::Bind(DoVerify, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59730 CompleteWithThreadPoolError(&result);
731 }
[email protected]3ed00262013-09-26 22:28:10732}
733
Blink Reformat1c4d759e2017-04-09 16:34:54734void WebCryptoImpl::WrapKey(blink::WebCryptoKeyFormat format,
[email protected]baa92842014-03-25 01:07:38735 const blink::WebCryptoKey& key,
736 const blink::WebCryptoKey& wrapping_key,
737 const blink::WebCryptoAlgorithm& wrap_algorithm,
738 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03739 std::unique_ptr<WrapKeyState> state(
[email protected]88be98562014-04-30 11:18:59740 new WrapKeyState(format, key, wrapping_key, wrap_algorithm, result));
dcheng0917ec42015-11-19 07:00:20741 if (!CryptoThreadPool::PostTask(
742 FROM_HERE, base::Bind(DoWrapKey, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59743 CompleteWithThreadPoolError(&result);
744 }
[email protected]baa92842014-03-25 01:07:38745}
746
Blink Reformat1c4d759e2017-04-09 16:34:54747void WebCryptoImpl::UnwrapKey(
[email protected]baa92842014-03-25 01:07:38748 blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58749 blink::WebVector<unsigned char> wrapped_key,
[email protected]baa92842014-03-25 01:07:38750 const blink::WebCryptoKey& wrapping_key,
751 const blink::WebCryptoAlgorithm& unwrap_algorithm,
752 const blink::WebCryptoAlgorithm& unwrapped_key_algorithm,
753 bool extractable,
754 blink::WebCryptoKeyUsageMask usages,
755 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03756 std::unique_ptr<UnwrapKeyState> state(new UnwrapKeyState(
eromaneeb3a0e2016-07-20 20:55:58757 format, std::move(wrapped_key), wrapping_key, unwrap_algorithm,
eroman38bb4bd2014-11-24 23:47:06758 unwrapped_key_algorithm, extractable, usages, result));
dcheng0917ec42015-11-19 07:00:20759 if (!CryptoThreadPool::PostTask(
760 FROM_HERE, base::Bind(DoUnwrapKey, base::Passed(&state)))) {
[email protected]88be98562014-04-30 11:18:59761 CompleteWithThreadPoolError(&result);
762 }
[email protected]bd48e6412014-02-22 08:32:53763}
764
Blink Reformat1c4d759e2017-04-09 16:34:54765void WebCryptoImpl::DeriveBits(const blink::WebCryptoAlgorithm& algorithm,
eroman1499b4942014-11-26 19:59:53766 const blink::WebCryptoKey& base_key,
767 unsigned int length_bits,
768 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03769 std::unique_ptr<DeriveBitsState> state(
eroman1499b4942014-11-26 19:59:53770 new DeriveBitsState(algorithm, base_key, length_bits, result));
dcheng0917ec42015-11-19 07:00:20771 if (!CryptoThreadPool::PostTask(
772 FROM_HERE, base::Bind(DoDeriveBits, base::Passed(&state)))) {
eroman1499b4942014-11-26 19:59:53773 CompleteWithThreadPoolError(&result);
774 }
775}
776
Blink Reformat1c4d759e2017-04-09 16:34:54777void WebCryptoImpl::DeriveKey(
eroman20bf4c3c2014-12-12 17:22:37778 const blink::WebCryptoAlgorithm& algorithm,
779 const blink::WebCryptoKey& base_key,
780 const blink::WebCryptoAlgorithm& import_algorithm,
781 const blink::WebCryptoAlgorithm& key_length_algorithm,
782 bool extractable,
783 blink::WebCryptoKeyUsageMask usages,
784 blink::WebCryptoResult result) {
dcheng7036d1e52016-04-21 23:13:03785 std::unique_ptr<DeriveKeyState> state(
eroman20bf4c3c2014-12-12 17:22:37786 new DeriveKeyState(algorithm, base_key, import_algorithm,
787 key_length_algorithm, extractable, usages, result));
dcheng0917ec42015-11-19 07:00:20788 if (!CryptoThreadPool::PostTask(
789 FROM_HERE, base::Bind(DoDeriveKey, base::Passed(&state)))) {
eroman20bf4c3c2014-12-12 17:22:37790 CompleteWithThreadPoolError(&result);
791 }
792}
793
Blink Reformat1c4d759e2017-04-09 16:34:54794std::unique_ptr<blink::WebCryptoDigestor> WebCryptoImpl::CreateDigestor(
[email protected]6f778f02014-04-01 02:31:33795 blink::WebCryptoAlgorithmId algorithm_id) {
dcheng7578e8ee2016-07-13 03:52:45796 return webcrypto::CreateDigestor(algorithm_id);
[email protected]6f778f02014-04-01 02:31:33797}
798
Blink Reformat1c4d759e2017-04-09 16:34:54799bool WebCryptoImpl::DeserializeKeyForClone(
[email protected]5daca0472014-03-18 20:27:08800 const blink::WebCryptoKeyAlgorithm& algorithm,
801 blink::WebCryptoKeyType type,
802 bool extractable,
803 blink::WebCryptoKeyUsageMask usages,
804 const unsigned char* key_data,
805 unsigned key_data_size,
806 blink::WebCryptoKey& key) {
[email protected]88be98562014-04-30 11:18:59807 return webcrypto::DeserializeKeyForClone(
eroman38bb4bd2014-11-24 23:47:06808 algorithm, type, extractable, usages,
809 webcrypto::CryptoData(key_data, key_data_size), &key);
[email protected]5daca0472014-03-18 20:27:08810}
811
Blink Reformat1c4d759e2017-04-09 16:34:54812bool WebCryptoImpl::SerializeKeyForClone(
[email protected]5daca0472014-03-18 20:27:08813 const blink::WebCryptoKey& key,
814 blink::WebVector<unsigned char>& key_data) {
[email protected]88be98562014-04-30 11:18:59815 return webcrypto::SerializeKeyForClone(key, &key_data);
[email protected]5daca0472014-03-18 20:27:08816}
817
erg56f12322015-04-17 00:51:48818} // namespace webcrypto