blob: ffd52546cf7b428bcabac26b59271f8faf81fde5 [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>
Claudio DeSouza591a9972018-02-21 17:27:1611#include <utility>
dcheng7036d1e52016-04-21 23:13:0312
[email protected]88be98562014-04-30 11:18:5913#include "base/bind.h"
14#include "base/lazy_instance.h"
15#include "base/location.h"
[email protected]c1b944a2013-10-23 06:33:2116#include "base/logging.h"
gab2c979bb2016-09-14 02:36:4717#include "base/macros.h"
[email protected]88be98562014-04-30 11:18:5918#include "base/single_thread_task_runner.h"
19#include "base/task_runner.h"
gab2c979bb2016-09-14 02:36:4720#include "base/threading/thread.h"
gab7966d312016-05-11 20:35:0121#include "base/threading/thread_task_runner_handle.h"
Alexei Filippovb4e75fa2017-11-23 04:44:2122#include "base/trace_event/trace_event.h"
erg56f12322015-04-17 00:51:4823#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 Reformata30d4232018-04-07 15:31:0627#include "third_party/blink/public/platform/web_crypto_key_algorithm.h"
28#include "third_party/blink/public/platform/web_string.h"
[email protected]408699c2013-07-17 21:23:1629
erg56f12322015-04-17 00:51:4830namespace webcrypto {
[email protected]408699c2013-07-17 21:23:1631
[email protected]cca897482014-01-30 22:40:1932using webcrypto::Status;
33
[email protected]043cf1d32013-11-02 13:27:3034namespace {
35
[email protected]88be98562014-04-30 11:18:5936// ---------------------
37// Threading
38// ---------------------
39//
40// WebCrypto operations can be slow. For instance generating an RSA key can
eroman984ead6c2016-06-23 00:25:2541// take seconds.
[email protected]88be98562014-04-30 11:18:5942//
gab2c979bb2016-09-14 02:36:4743// 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]88be98562014-04-30 11:18:5946//
47// A few notes to keep in mind:
48//
eroman984ead6c2016-06-23 00:25:2549// * 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]88be98562014-04-30 11:18:5951//
eroman984ead6c2016-06-23 00:25:2552// 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]88be98562014-04-30 11:18:5955//
56// * blink::WebArrayBuffer is NOT threadsafe, and should therefore be allocated
eroman984ead6c2016-06-23 00:25:2557// only on the target Blink thread.
[email protected]88be98562014-04-30 11:18:5958//
59// TODO(eroman): Is there any way around this? Copying the result between
60// threads is silly.
61//
eroman984ead6c2016-06-23 00:25:2562// * 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]88be98562014-04-30 11:18:5967//
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.
74class CryptoThreadPool {
75 public:
gab2c979bb2016-09-14 02:36:4776 CryptoThreadPool() : worker_thread_("WebCrypto") {
77 base::Thread::Options options;
78 options.joinable = false;
79 worker_thread_.StartWithOptions(options);
80 }
[email protected]88be98562014-04-30 11:18:5981
Claudio DeSouza591a9972018-02-21 17:27:1682 static bool PostTask(const base::Location& from_here, base::OnceClosure task);
[email protected]88be98562014-04-30 11:18:5983
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,
Claudio DeSouza591a9972018-02-21 17:27:1699 base::OnceClosure task) {
gab2c979bb2016-09-14 02:36:47100 return crypto_thread_pool.Get().worker_thread_.task_runner()->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16101 from_here, std::move(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
[email protected]88be98562014-04-30 11:18:59143// --------------------------------------------------------------------
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
160struct BaseState {
Hajime Hoshi757009b2018-01-16 06:42:40161 BaseState(const blink::WebCryptoResult& result,
162 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
163 : origin_thread(task_runner), result(result) {}
[email protected]88be98562014-04-30 11:18:59164
Blink Reformat1c4d759e2017-04-09 16:34:54165 bool cancelled() { return result.Cancelled(); }
[email protected]345bd202014-06-19 04:51:16166
[email protected]88be98562014-04-30 11:18:59167 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
178struct EncryptState : public BaseState {
179 EncryptState(const blink::WebCryptoAlgorithm& algorithm,
180 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58181 blink::WebVector<unsigned char> data,
Hajime Hoshi757009b2018-01-16 06:42:40182 const blink::WebCryptoResult& result,
183 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
184 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59185 algorithm(algorithm),
186 key(key),
eromaneeb3a0e2016-07-20 20:55:58187 data(std::move(data)) {}
[email protected]88be98562014-04-30 11:18:59188
189 const blink::WebCryptoAlgorithm algorithm;
190 const blink::WebCryptoKey key;
eromaneeb3a0e2016-07-20 20:55:58191 const blink::WebVector<unsigned char> data;
[email protected]88be98562014-04-30 11:18:59192
[email protected]53b6c9d22014-07-19 05:08:38193 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59194};
195
196typedef EncryptState DecryptState;
197typedef EncryptState DigestState;
198
199struct GenerateKeyState : public BaseState {
200 GenerateKeyState(const blink::WebCryptoAlgorithm& algorithm,
201 bool extractable,
eroman0e1d34e2014-10-21 19:13:31202 blink::WebCryptoKeyUsageMask usages,
Hajime Hoshi757009b2018-01-16 06:42:40203 const blink::WebCryptoResult& result,
204 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
205 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59206 algorithm(algorithm),
207 extractable(extractable),
eroman0e1d34e2014-10-21 19:13:31208 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59209
210 const blink::WebCryptoAlgorithm algorithm;
211 const bool extractable;
eroman0e1d34e2014-10-21 19:13:31212 const blink::WebCryptoKeyUsageMask usages;
[email protected]88be98562014-04-30 11:18:59213
eroman9b747eaf2014-10-18 22:03:28214 webcrypto::GenerateKeyResult generate_key_result;
[email protected]88be98562014-04-30 11:18:59215};
216
217struct ImportKeyState : public BaseState {
218 ImportKeyState(blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58219 blink::WebVector<unsigned char> key_data,
[email protected]88be98562014-04-30 11:18:59220 const blink::WebCryptoAlgorithm& algorithm,
221 bool extractable,
eroman0e1d34e2014-10-21 19:13:31222 blink::WebCryptoKeyUsageMask usages,
Hajime Hoshi757009b2018-01-16 06:42:40223 const blink::WebCryptoResult& result,
224 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
225 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59226 format(format),
eromaneeb3a0e2016-07-20 20:55:58227 key_data(std::move(key_data)),
[email protected]88be98562014-04-30 11:18:59228 algorithm(algorithm),
229 extractable(extractable),
eroman0e1d34e2014-10-21 19:13:31230 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59231
232 const blink::WebCryptoKeyFormat format;
eromaneeb3a0e2016-07-20 20:55:58233 const blink::WebVector<unsigned char> key_data;
[email protected]88be98562014-04-30 11:18:59234 const blink::WebCryptoAlgorithm algorithm;
235 const bool extractable;
eroman0e1d34e2014-10-21 19:13:31236 const blink::WebCryptoKeyUsageMask usages;
[email protected]88be98562014-04-30 11:18:59237
238 blink::WebCryptoKey key;
239};
240
241struct ExportKeyState : public BaseState {
242 ExportKeyState(blink::WebCryptoKeyFormat format,
243 const blink::WebCryptoKey& key,
Hajime Hoshi757009b2018-01-16 06:42:40244 const blink::WebCryptoResult& result,
245 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
246 : BaseState(result, std::move(task_runner)), format(format), key(key) {}
[email protected]88be98562014-04-30 11:18:59247
248 const blink::WebCryptoKeyFormat format;
249 const blink::WebCryptoKey key;
250
[email protected]53b6c9d22014-07-19 05:08:38251 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59252};
253
254typedef EncryptState SignState;
255
256struct VerifySignatureState : public BaseState {
257 VerifySignatureState(const blink::WebCryptoAlgorithm& algorithm,
258 const blink::WebCryptoKey& key,
eromaneeb3a0e2016-07-20 20:55:58259 blink::WebVector<unsigned char> signature,
260 blink::WebVector<unsigned char> data,
Hajime Hoshi757009b2018-01-16 06:42:40261 const blink::WebCryptoResult& result,
262 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
263 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59264 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,
Hajime Hoshi757009b2018-01-16 06:42:40283 const blink::WebCryptoResult& result,
284 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
285 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59286 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]53b6c9d22014-07-19 05:08:38296 std::vector<uint8_t> buffer;
[email protected]88be98562014-04-30 11:18:59297};
298
299struct UnwrapKeyState : public BaseState {
300 UnwrapKeyState(blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58301 blink::WebVector<unsigned char> wrapped_key,
[email protected]88be98562014-04-30 11:18:59302 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 Hoshi757009b2018-01-16 06:42:40307 const blink::WebCryptoResult& result,
308 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
309 : BaseState(result, std::move(task_runner)),
[email protected]88be98562014-04-30 11:18:59310 format(format),
eromaneeb3a0e2016-07-20 20:55:58311 wrapped_key(std::move(wrapped_key)),
[email protected]88be98562014-04-30 11:18:59312 wrapping_key(wrapping_key),
313 unwrap_algorithm(unwrap_algorithm),
314 unwrapped_key_algorithm(unwrapped_key_algorithm),
315 extractable(extractable),
eroman7b9591ab2014-10-21 17:13:01316 usages(usages) {}
[email protected]88be98562014-04-30 11:18:59317
318 const blink::WebCryptoKeyFormat format;
eromaneeb3a0e2016-07-20 20:55:58319 blink::WebVector<unsigned char> wrapped_key;
[email protected]88be98562014-04-30 11:18:59320 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
eroman1499b4942014-11-26 19:59:53329struct DeriveBitsState : public BaseState {
330 DeriveBitsState(const blink::WebCryptoAlgorithm& algorithm,
331 const blink::WebCryptoKey& base_key,
332 unsigned int length_bits,
Hajime Hoshi757009b2018-01-16 06:42:40333 const blink::WebCryptoResult& result,
334 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
335 : BaseState(result, std::move(task_runner)),
eroman1499b4942014-11-26 19:59:53336 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
eroman20bf4c3c2014-12-12 17:22:37347struct 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 Hoshi757009b2018-01-16 06:42:40354 const blink::WebCryptoResult& result,
355 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
356 : BaseState(result, std::move(task_runner)),
eroman20bf4c3c2014-12-12 17:22:37357 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]88be98562014-04-30 11:18:59374// --------------------------------------------------------------------
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
dcheng7036d1e52016-04-21 23:13:03381void DoEncryptReply(std::unique_ptr<EncryptState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21382 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
383 "DoEncryptReply");
[email protected]88be98562014-04-30 11:18:59384 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
385}
386
dcheng7036d1e52016-04-21 23:13:03387void DoEncrypt(std::unique_ptr<EncryptState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21388 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoEncrypt");
[email protected]88be98562014-04-30 11:18:59389 EncryptState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16390 if (state->cancelled())
391 return;
eroman38bb4bd2014-11-24 23:47:06392 state->status =
393 webcrypto::Encrypt(state->algorithm, state->key,
394 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59395 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16396 FROM_HERE, base::BindOnce(DoEncryptReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59397}
398
dcheng7036d1e52016-04-21 23:13:03399void DoDecryptReply(std::unique_ptr<DecryptState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21400 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
401 "DoDecryptReply");
[email protected]88be98562014-04-30 11:18:59402 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
403}
404
dcheng7036d1e52016-04-21 23:13:03405void DoDecrypt(std::unique_ptr<DecryptState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21406 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDecrypt");
[email protected]88be98562014-04-30 11:18:59407 DecryptState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16408 if (state->cancelled())
409 return;
eroman38bb4bd2014-11-24 23:47:06410 state->status =
411 webcrypto::Decrypt(state->algorithm, state->key,
412 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59413 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16414 FROM_HERE, base::BindOnce(DoDecryptReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59415}
416
dcheng7036d1e52016-04-21 23:13:03417void DoDigestReply(std::unique_ptr<DigestState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21418 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigestReply");
[email protected]88be98562014-04-30 11:18:59419 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
420}
421
dcheng7036d1e52016-04-21 23:13:03422void DoDigest(std::unique_ptr<DigestState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21423 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDigest");
[email protected]88be98562014-04-30 11:18:59424 DigestState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16425 if (state->cancelled())
426 return;
[email protected]88be98562014-04-30 11:18:59427 state->status = webcrypto::Digest(
428 state->algorithm, webcrypto::CryptoData(state->data), &state->buffer);
429 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16430 FROM_HERE, base::BindOnce(DoDigestReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59431}
432
dcheng7036d1e52016-04-21 23:13:03433void DoGenerateKeyReply(std::unique_ptr<GenerateKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21434 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
435 "DoGenerateKeyReply");
[email protected]88be98562014-04-30 11:18:59436 if (state->status.IsError()) {
437 CompleteWithError(state->status, &state->result);
438 } else {
eroman9b747eaf2014-10-18 22:03:28439 state->generate_key_result.Complete(&state->result);
[email protected]88be98562014-04-30 11:18:59440 }
441}
442
dcheng7036d1e52016-04-21 23:13:03443void DoGenerateKey(std::unique_ptr<GenerateKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21444 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoGenerateKey");
[email protected]88be98562014-04-30 11:18:59445 GenerateKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16446 if (state->cancelled())
447 return;
eroman38bb4bd2014-11-24 23:47:06448 state->status =
449 webcrypto::GenerateKey(state->algorithm, state->extractable,
450 state->usages, &state->generate_key_result);
[email protected]88be98562014-04-30 11:18:59451 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16452 FROM_HERE, base::BindOnce(DoGenerateKeyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59453}
454
dcheng7036d1e52016-04-21 23:13:03455void DoImportKeyReply(std::unique_ptr<ImportKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21456 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
457 "DoImportKeyReply");
[email protected]88be98562014-04-30 11:18:59458 CompleteWithKeyOrError(state->status, state->key, &state->result);
459}
460
dcheng7036d1e52016-04-21 23:13:03461void DoImportKey(std::unique_ptr<ImportKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21462 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoImportKey");
[email protected]88be98562014-04-30 11:18:59463 ImportKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16464 if (state->cancelled())
465 return;
eroman38bb4bd2014-11-24 23:47:06466 state->status = webcrypto::ImportKey(
467 state->format, webcrypto::CryptoData(state->key_data), state->algorithm,
468 state->extractable, state->usages, &state->key);
[email protected]88be98562014-04-30 11:18:59469 if (state->status.IsSuccess()) {
Blink Reformat1c4d759e2017-04-09 16:34:54470 DCHECK(state->key.Handle());
471 DCHECK(!state->key.Algorithm().IsNull());
472 DCHECK_EQ(state->extractable, state->key.Extractable());
[email protected]88be98562014-04-30 11:18:59473 }
474
475 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16476 FROM_HERE, base::BindOnce(DoImportKeyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59477}
478
dcheng7036d1e52016-04-21 23:13:03479void DoExportKeyReply(std::unique_ptr<ExportKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21480 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
481 "DoExportKeyReply");
Blink Reformat1c4d759e2017-04-09 16:34:54482 if (state->format != blink::kWebCryptoKeyFormatJwk) {
[email protected]b7deaa082014-06-17 22:24:37483 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 Reformat1c4d759e2017-04-09 16:34:54490 state->result.CompleteWithJson(
davidbenaa62f382015-11-20 22:10:01491 reinterpret_cast<const char*>(state->buffer.data()),
brettw690c96672015-04-21 16:19:54492 static_cast<unsigned int>(state->buffer.size()));
[email protected]b7deaa082014-06-17 22:24:37493 }
[email protected]88be98562014-04-30 11:18:59494}
495
dcheng7036d1e52016-04-21 23:13:03496void DoExportKey(std::unique_ptr<ExportKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21497 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoExportKey");
[email protected]88be98562014-04-30 11:18:59498 ExportKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16499 if (state->cancelled())
500 return;
[email protected]88be98562014-04-30 11:18:59501 state->status =
502 webcrypto::ExportKey(state->format, state->key, &state->buffer);
503 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16504 FROM_HERE, base::BindOnce(DoExportKeyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59505}
506
dcheng7036d1e52016-04-21 23:13:03507void DoSignReply(std::unique_ptr<SignState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21508 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSignReply");
[email protected]88be98562014-04-30 11:18:59509 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
510}
511
dcheng7036d1e52016-04-21 23:13:03512void DoSign(std::unique_ptr<SignState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21513 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoSign");
[email protected]88be98562014-04-30 11:18:59514 SignState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16515 if (state->cancelled())
516 return;
eroman38bb4bd2014-11-24 23:47:06517 state->status =
518 webcrypto::Sign(state->algorithm, state->key,
519 webcrypto::CryptoData(state->data), &state->buffer);
[email protected]88be98562014-04-30 11:18:59520
521 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16522 FROM_HERE, base::BindOnce(DoSignReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59523}
524
dcheng7036d1e52016-04-21 23:13:03525void DoVerifyReply(std::unique_ptr<VerifySignatureState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21526 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerifyReply");
[email protected]88be98562014-04-30 11:18:59527 if (state->status.IsError()) {
528 CompleteWithError(state->status, &state->result);
529 } else {
Blink Reformat1c4d759e2017-04-09 16:34:54530 state->result.CompleteWithBoolean(state->verify_result);
[email protected]88be98562014-04-30 11:18:59531 }
532}
533
dcheng7036d1e52016-04-21 23:13:03534void DoVerify(std::unique_ptr<VerifySignatureState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21535 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoVerify");
[email protected]88be98562014-04-30 11:18:59536 VerifySignatureState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16537 if (state->cancelled())
538 return;
eroman38bb4bd2014-11-24 23:47:06539 state->status = webcrypto::Verify(
540 state->algorithm, state->key, webcrypto::CryptoData(state->signature),
541 webcrypto::CryptoData(state->data), &state->verify_result);
[email protected]88be98562014-04-30 11:18:59542
543 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16544 FROM_HERE, base::BindOnce(DoVerifyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59545}
546
dcheng7036d1e52016-04-21 23:13:03547void DoWrapKeyReply(std::unique_ptr<WrapKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21548 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
549 "DoWrapKeyReply");
[email protected]88be98562014-04-30 11:18:59550 CompleteWithBufferOrError(state->status, state->buffer, &state->result);
551}
552
dcheng7036d1e52016-04-21 23:13:03553void DoWrapKey(std::unique_ptr<WrapKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21554 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoWrapKey");
[email protected]88be98562014-04-30 11:18:59555 WrapKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16556 if (state->cancelled())
557 return;
eroman38bb4bd2014-11-24 23:47:06558 state->status =
559 webcrypto::WrapKey(state->format, state->key, state->wrapping_key,
560 state->wrap_algorithm, &state->buffer);
[email protected]88be98562014-04-30 11:18:59561
562 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16563 FROM_HERE, base::BindOnce(DoWrapKeyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59564}
565
dcheng7036d1e52016-04-21 23:13:03566void DoUnwrapKeyReply(std::unique_ptr<UnwrapKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21567 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
568 "DoUnwrapKeyReply");
[email protected]88be98562014-04-30 11:18:59569 CompleteWithKeyOrError(state->status, state->unwrapped_key, &state->result);
570}
571
dcheng7036d1e52016-04-21 23:13:03572void DoUnwrapKey(std::unique_ptr<UnwrapKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21573 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoUnwrapKey");
[email protected]88be98562014-04-30 11:18:59574 UnwrapKeyState* state = passed_state.get();
[email protected]345bd202014-06-19 04:51:16575 if (state->cancelled())
576 return;
eroman38bb4bd2014-11-24 23:47:06577 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]88be98562014-04-30 11:18:59582
583 state->origin_thread->PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16584 FROM_HERE, base::BindOnce(DoUnwrapKeyReply, std::move(passed_state)));
[email protected]88be98562014-04-30 11:18:59585}
586
dcheng7036d1e52016-04-21 23:13:03587void DoDeriveBitsReply(std::unique_ptr<DeriveBitsState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21588 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
589 "DoDeriveBitsReply");
eroman1499b4942014-11-26 19:59:53590 CompleteWithBufferOrError(state->status, state->derived_bytes,
591 &state->result);
592}
593
dcheng7036d1e52016-04-21 23:13:03594void DoDeriveBits(std::unique_ptr<DeriveBitsState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21595 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveBits");
eroman1499b4942014-11-26 19:59:53596 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 DeSouza591a9972018-02-21 17:27:16603 FROM_HERE, base::BindOnce(DoDeriveBitsReply, std::move(passed_state)));
eroman1499b4942014-11-26 19:59:53604}
605
dcheng7036d1e52016-04-21 23:13:03606void DoDeriveKeyReply(std::unique_ptr<DeriveKeyState> state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21607 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"),
608 "DoDeriveKeyReply");
eroman20bf4c3c2014-12-12 17:22:37609 CompleteWithKeyOrError(state->status, state->derived_key, &state->result);
610}
611
dcheng7036d1e52016-04-21 23:13:03612void DoDeriveKey(std::unique_ptr<DeriveKeyState> passed_state) {
Alexei Filippovb4e75fa2017-11-23 04:44:21613 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "DoDeriveKey");
eroman20bf4c3c2014-12-12 17:22:37614 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 DeSouza591a9972018-02-21 17:27:16622 FROM_HERE, base::BindOnce(DoDeriveKeyReply, std::move(passed_state)));
eroman20bf4c3c2014-12-12 17:22:37623}
624
[email protected]043cf1d32013-11-02 13:27:30625} // namespace
626
[email protected]88be98562014-04-30 11:18:59627WebCryptoImpl::WebCryptoImpl() {
[email protected]88be98562014-04-30 11:18:59628}
[email protected]7e4c36f2013-09-12 06:10:19629
[email protected]88be98562014-04-30 11:18:59630WebCryptoImpl::~WebCryptoImpl() {
631}
[email protected]04166f82014-02-19 06:11:04632
Hajime Hoshi757009b2018-01-16 06:42:40633void 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 Reformat1c4d759e2017-04-09 16:34:54639 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59640
Hajime Hoshi757009b2018-01-16 06:42:40641 std::unique_ptr<EncryptState> state(new EncryptState(
642 algorithm, key, std::move(data), result, std::move(task_runner)));
dcheng0917ec42015-11-19 07:00:20643 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16644 FROM_HERE, base::BindOnce(DoEncrypt, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59645 CompleteWithThreadPoolError(&result);
646 }
[email protected]a2a06c732013-09-27 10:50:54647}
648
Hajime Hoshi757009b2018-01-16 06:42:40649void 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 Reformat1c4d759e2017-04-09 16:34:54655 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59656
Hajime Hoshi757009b2018-01-16 06:42:40657 std::unique_ptr<DecryptState> state(new DecryptState(
658 algorithm, key, std::move(data), result, std::move(task_runner)));
dcheng0917ec42015-11-19 07:00:20659 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16660 FROM_HERE, base::BindOnce(DoDecrypt, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59661 CompleteWithThreadPoolError(&result);
662 }
[email protected]868085a92013-10-01 00:42:30663}
664
Hajime Hoshi757009b2018-01-16 06:42:40665void 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 Reformat1c4d759e2017-04-09 16:34:54670 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59671
Hajime Hoshi757009b2018-01-16 06:42:40672 std::unique_ptr<DigestState> state(
673 new DigestState(algorithm, blink::WebCryptoKey::CreateNull(),
674 std::move(data), result, std::move(task_runner)));
[email protected]88be98562014-04-30 11:18:59675 if (!CryptoThreadPool::PostTask(FROM_HERE,
Claudio DeSouza591a9972018-02-21 17:27:16676 base::BindOnce(DoDigest, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59677 CompleteWithThreadPoolError(&result);
678 }
[email protected]408699c2013-07-17 21:23:16679}
680
Hajime Hoshi757009b2018-01-16 06:42:40681void 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 Reformat1c4d759e2017-04-09 16:34:54687 DCHECK(!algorithm.IsNull());
[email protected]88be98562014-04-30 11:18:59688
Hajime Hoshi757009b2018-01-16 06:42:40689 std::unique_ptr<GenerateKeyState> state(new GenerateKeyState(
690 algorithm, extractable, usages, result, std::move(task_runner)));
dcheng0917ec42015-11-19 07:00:20691 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16692 FROM_HERE, base::BindOnce(DoGenerateKey, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59693 CompleteWithThreadPoolError(&result);
[email protected]dfae8ab2013-10-10 19:45:06694 }
695}
696
Hajime Hoshi757009b2018-01-16 06:42:40697void 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)));
dcheng0917ec42015-11-19 07:00:20708 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16709 FROM_HERE, base::BindOnce(DoImportKey, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59710 CompleteWithThreadPoolError(&result);
[email protected]cca897482014-01-30 22:40:19711 }
[email protected]1c879bc92013-09-18 07:45:32712}
713
Hajime Hoshi757009b2018-01-16 06:42:40714void WebCryptoImpl::ExportKey(
715 blink::WebCryptoKeyFormat format,
716 const blink::WebCryptoKey& key,
717 blink::WebCryptoResult result,
718 scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
dcheng7036d1e52016-04-21 23:13:03719 std::unique_ptr<ExportKeyState> state(
Hajime Hoshi757009b2018-01-16 06:42:40720 new ExportKeyState(format, key, result, std::move(task_runner)));
dcheng0917ec42015-11-19 07:00:20721 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16722 FROM_HERE, base::BindOnce(DoExportKey, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59723 CompleteWithThreadPoolError(&result);
724 }
[email protected]e9b2c102013-11-26 04:26:33725}
726
Hajime Hoshi757009b2018-01-16 06:42:40727void 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]88be98562014-04-30 11:18:59735 if (!CryptoThreadPool::PostTask(FROM_HERE,
Claudio DeSouza591a9972018-02-21 17:27:16736 base::BindOnce(DoSign, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59737 CompleteWithThreadPoolError(&result);
738 }
[email protected]1c879bc92013-09-18 07:45:32739}
740
Hajime Hoshi757009b2018-01-16 06:42:40741void 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) {
dcheng7036d1e52016-04-21 23:13:03748 std::unique_ptr<VerifySignatureState> state(new VerifySignatureState(
Hajime Hoshi757009b2018-01-16 06:42:40749 algorithm, key, std::move(signature), std::move(data), result,
750 std::move(task_runner)));
[email protected]88be98562014-04-30 11:18:59751 if (!CryptoThreadPool::PostTask(FROM_HERE,
Claudio DeSouza591a9972018-02-21 17:27:16752 base::BindOnce(DoVerify, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59753 CompleteWithThreadPoolError(&result);
754 }
[email protected]3ed00262013-09-26 22:28:10755}
756
Hajime Hoshi757009b2018-01-16 06:42:40757void 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) {
dcheng7036d1e52016-04-21 23:13:03764 std::unique_ptr<WrapKeyState> state(
Hajime Hoshi757009b2018-01-16 06:42:40765 new WrapKeyState(format, key, wrapping_key, wrap_algorithm, result,
766 std::move(task_runner)));
dcheng0917ec42015-11-19 07:00:20767 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16768 FROM_HERE, base::BindOnce(DoWrapKey, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59769 CompleteWithThreadPoolError(&result);
770 }
[email protected]baa92842014-03-25 01:07:38771}
772
Blink Reformat1c4d759e2017-04-09 16:34:54773void WebCryptoImpl::UnwrapKey(
[email protected]baa92842014-03-25 01:07:38774 blink::WebCryptoKeyFormat format,
eromaneeb3a0e2016-07-20 20:55:58775 blink::WebVector<unsigned char> wrapped_key,
[email protected]baa92842014-03-25 01:07:38776 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 Hoshi757009b2018-01-16 06:42:40781 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)));
dcheng0917ec42015-11-19 07:00:20787 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16788 FROM_HERE, base::BindOnce(DoUnwrapKey, std::move(state)))) {
[email protected]88be98562014-04-30 11:18:59789 CompleteWithThreadPoolError(&result);
790 }
[email protected]bd48e6412014-02-22 08:32:53791}
792
Hajime Hoshi757009b2018-01-16 06:42:40793void 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)));
dcheng0917ec42015-11-19 07:00:20801 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16802 FROM_HERE, base::BindOnce(DoDeriveBits, std::move(state)))) {
eroman1499b4942014-11-26 19:59:53803 CompleteWithThreadPoolError(&result);
804 }
805}
806
Blink Reformat1c4d759e2017-04-09 16:34:54807void WebCryptoImpl::DeriveKey(
eroman20bf4c3c2014-12-12 17:22:37808 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 Hoshi757009b2018-01-16 06:42:40814 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)));
dcheng0917ec42015-11-19 07:00:20819 if (!CryptoThreadPool::PostTask(
Claudio DeSouza591a9972018-02-21 17:27:16820 FROM_HERE, base::BindOnce(DoDeriveKey, std::move(state)))) {
eroman20bf4c3c2014-12-12 17:22:37821 CompleteWithThreadPoolError(&result);
822 }
823}
824
Blink Reformat1c4d759e2017-04-09 16:34:54825std::unique_ptr<blink::WebCryptoDigestor> WebCryptoImpl::CreateDigestor(
[email protected]6f778f02014-04-01 02:31:33826 blink::WebCryptoAlgorithmId algorithm_id) {
dcheng7578e8ee2016-07-13 03:52:45827 return webcrypto::CreateDigestor(algorithm_id);
[email protected]6f778f02014-04-01 02:31:33828}
829
Blink Reformat1c4d759e2017-04-09 16:34:54830bool WebCryptoImpl::DeserializeKeyForClone(
[email protected]5daca0472014-03-18 20:27:08831 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]88be98562014-04-30 11:18:59838 return webcrypto::DeserializeKeyForClone(
eroman38bb4bd2014-11-24 23:47:06839 algorithm, type, extractable, usages,
840 webcrypto::CryptoData(key_data, key_data_size), &key);
[email protected]5daca0472014-03-18 20:27:08841}
842
Blink Reformat1c4d759e2017-04-09 16:34:54843bool WebCryptoImpl::SerializeKeyForClone(
[email protected]5daca0472014-03-18 20:27:08844 const blink::WebCryptoKey& key,
845 blink::WebVector<unsigned char>& key_data) {
[email protected]88be98562014-04-30 11:18:59846 return webcrypto::SerializeKeyForClone(key, &key_data);
[email protected]5daca0472014-03-18 20:27:08847}
848
erg56f12322015-04-17 00:51:48849} // namespace webcrypto