1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/child/webcrypto/webcrypto_impl.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "content/child/webcrypto/crypto_data.h"
#include "content/child/webcrypto/shared_crypto.h"
#include "content/child/webcrypto/status.h"
#include "content/child/webcrypto/webcrypto_util.h"
#include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h"
#include "third_party/WebKit/public/platform/WebString.h"
namespace content {
using webcrypto::Status;
namespace {
void CompleteWithError(const Status& status, blink::WebCryptoResult* result) {
DCHECK(status.IsError());
#if defined(WEBCRYPTO_HAS_ERROR_TYPE)
result->completeWithError(status.error_type(),
blink::WebString::fromUTF8(status.error_details()));
#else
// TODO(eroman): Delete once Blink changes have rolled into Chromium.
if (!status.error_details().empty())
result->completeWithError(
blink::WebString::fromUTF8(status.error_details()));
else
result->completeWithError();
#endif
}
bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) {
// TODO(padolph): include all other asymmetric algorithms once they are
// defined, e.g. EC and DH.
return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 ||
algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 ||
algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep);
}
} // namespace
WebCryptoImpl::WebCryptoImpl() { webcrypto::Init(); }
WebCryptoImpl::~WebCryptoImpl() {}
void WebCryptoImpl::encrypt(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const unsigned char* data,
unsigned int data_size,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
blink::WebArrayBuffer buffer;
Status status = webcrypto::Encrypt(
algorithm, key, webcrypto::CryptoData(data, data_size), &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::decrypt(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const unsigned char* data,
unsigned int data_size,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
blink::WebArrayBuffer buffer;
Status status = webcrypto::Decrypt(
algorithm, key, webcrypto::CryptoData(data, data_size), &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::digest(const blink::WebCryptoAlgorithm& algorithm,
const unsigned char* data,
unsigned int data_size,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
blink::WebArrayBuffer buffer;
Status status = webcrypto::Digest(
algorithm, webcrypto::CryptoData(data, data_size), &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::generateKey(const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usage_mask,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
if (IsAlgorithmAsymmetric(algorithm)) {
blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull();
blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull();
Status status = webcrypto::GenerateKeyPair(
algorithm, extractable, usage_mask, &public_key, &private_key);
if (status.IsError()) {
CompleteWithError(status, &result);
} else {
DCHECK(public_key.handle());
DCHECK(private_key.handle());
DCHECK_EQ(algorithm.id(), public_key.algorithm().id());
DCHECK_EQ(algorithm.id(), private_key.algorithm().id());
DCHECK_EQ(true, public_key.extractable());
DCHECK_EQ(extractable, private_key.extractable());
DCHECK_EQ(usage_mask, public_key.usages());
DCHECK_EQ(usage_mask, private_key.usages());
result.completeWithKeyPair(public_key, private_key);
}
} else {
blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
Status status =
webcrypto::GenerateSecretKey(algorithm, extractable, usage_mask, &key);
if (status.IsError()) {
CompleteWithError(status, &result);
} else {
DCHECK(key.handle());
DCHECK_EQ(algorithm.id(), key.algorithm().id());
DCHECK_EQ(extractable, key.extractable());
DCHECK_EQ(usage_mask, key.usages());
result.completeWithKey(key);
}
}
}
void WebCryptoImpl::importKey(blink::WebCryptoKeyFormat format,
const unsigned char* key_data,
unsigned int key_data_size,
const blink::WebCryptoAlgorithm& algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usage_mask,
blink::WebCryptoResult result) {
blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
Status status =
webcrypto::ImportKey(format,
webcrypto::CryptoData(key_data, key_data_size),
algorithm,
extractable,
usage_mask,
&key);
if (status.IsError()) {
CompleteWithError(status, &result);
} else {
DCHECK(key.handle());
DCHECK(!key.algorithm().isNull());
DCHECK_EQ(extractable, key.extractable());
result.completeWithKey(key);
}
}
void WebCryptoImpl::exportKey(blink::WebCryptoKeyFormat format,
const blink::WebCryptoKey& key,
blink::WebCryptoResult result) {
blink::WebArrayBuffer buffer;
Status status = webcrypto::ExportKey(format, key, &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::sign(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const unsigned char* data,
unsigned int data_size,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
blink::WebArrayBuffer buffer;
Status status = webcrypto::Sign(
algorithm, key, webcrypto::CryptoData(data, data_size), &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::verifySignature(const blink::WebCryptoAlgorithm& algorithm,
const blink::WebCryptoKey& key,
const unsigned char* signature,
unsigned int signature_size,
const unsigned char* data,
unsigned int data_size,
blink::WebCryptoResult result) {
DCHECK(!algorithm.isNull());
bool signature_match = false;
Status status = webcrypto::VerifySignature(
algorithm,
key,
webcrypto::CryptoData(signature, signature_size),
webcrypto::CryptoData(data, data_size),
&signature_match);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBoolean(signature_match);
}
void WebCryptoImpl::wrapKey(blink::WebCryptoKeyFormat format,
const blink::WebCryptoKey& key,
const blink::WebCryptoKey& wrapping_key,
const blink::WebCryptoAlgorithm& wrap_algorithm,
blink::WebCryptoResult result) {
blink::WebArrayBuffer buffer;
// TODO(eroman): Use the same parameter ordering.
Status status = webcrypto::WrapKey(
format, wrapping_key, key, wrap_algorithm, &buffer);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithBuffer(buffer);
}
void WebCryptoImpl::unwrapKey(
blink::WebCryptoKeyFormat format,
const unsigned char* wrapped_key,
unsigned wrapped_key_size,
const blink::WebCryptoKey& wrapping_key,
const blink::WebCryptoAlgorithm& unwrap_algorithm,
const blink::WebCryptoAlgorithm& unwrapped_key_algorithm,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
blink::WebCryptoResult result) {
blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
Status status =
webcrypto::UnwrapKey(format,
webcrypto::CryptoData(wrapped_key, wrapped_key_size),
wrapping_key,
unwrap_algorithm,
unwrapped_key_algorithm,
extractable,
usages,
&key);
if (status.IsError())
CompleteWithError(status, &result);
else
result.completeWithKey(key);
}
bool WebCryptoImpl::digestSynchronous(
const blink::WebCryptoAlgorithmId algorithm_id,
const unsigned char* data,
unsigned int data_size,
blink::WebArrayBuffer& result) {
blink::WebCryptoAlgorithm algorithm =
blink::WebCryptoAlgorithm::adoptParamsAndCreate(algorithm_id, NULL);
return (webcrypto::Digest(
algorithm, webcrypto::CryptoData(data, data_size), &result))
.IsSuccess();
}
blink::WebCryptoDigestor* WebCryptoImpl::createDigestor(
blink::WebCryptoAlgorithmId algorithm_id) {
return webcrypto::CreateDigestor(algorithm_id).release();
}
bool WebCryptoImpl::deserializeKeyForClone(
const blink::WebCryptoKeyAlgorithm& algorithm,
blink::WebCryptoKeyType type,
bool extractable,
blink::WebCryptoKeyUsageMask usages,
const unsigned char* key_data,
unsigned key_data_size,
blink::WebCryptoKey& key) {
Status status = webcrypto::DeserializeKeyForClone(
algorithm,
type,
extractable,
usages,
webcrypto::CryptoData(key_data, key_data_size),
&key);
return status.IsSuccess();
}
bool WebCryptoImpl::serializeKeyForClone(
const blink::WebCryptoKey& key,
blink::WebVector<unsigned char>& key_data) {
Status status = webcrypto::SerializeKeyForClone(key, &key_data);
return status.IsSuccess();
}
} // namespace content
|