OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/webcrypto/webcrypto_impl.h" | 5 #include "content/renderer/webcrypto/webcrypto_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <functional> | 8 #include <functional> |
9 #include <map> | 9 #include <map> |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } // namespace | 172 } // namespace |
173 | 173 |
174 WebCryptoImpl::WebCryptoImpl() { | 174 WebCryptoImpl::WebCryptoImpl() { |
175 Init(); | 175 Init(); |
176 } | 176 } |
177 | 177 |
178 void WebCryptoImpl::encrypt( | 178 void WebCryptoImpl::encrypt( |
179 const blink::WebCryptoAlgorithm& algorithm, | 179 const blink::WebCryptoAlgorithm& algorithm, |
180 const blink::WebCryptoKey& key, | 180 const blink::WebCryptoKey& key, |
181 const unsigned char* data, | 181 const unsigned char* data, |
182 unsigned data_size, | 182 unsigned int data_size, |
183 blink::WebCryptoResult result) { | 183 blink::WebCryptoResult result) { |
184 DCHECK(!algorithm.isNull()); | 184 DCHECK(!algorithm.isNull()); |
185 blink::WebArrayBuffer buffer; | 185 blink::WebArrayBuffer buffer; |
186 Status status = EncryptInternal(algorithm, key, data, data_size, &buffer); | 186 Status status = EncryptInternal(algorithm, key, data, data_size, &buffer); |
187 if (status.IsError()) | 187 if (status.IsError()) |
188 CompleteWithError(status, &result); | 188 CompleteWithError(status, &result); |
189 else | 189 else |
190 result.completeWithBuffer(buffer); | 190 result.completeWithBuffer(buffer); |
191 } | 191 } |
192 | 192 |
193 void WebCryptoImpl::decrypt( | 193 void WebCryptoImpl::decrypt( |
194 const blink::WebCryptoAlgorithm& algorithm, | 194 const blink::WebCryptoAlgorithm& algorithm, |
195 const blink::WebCryptoKey& key, | 195 const blink::WebCryptoKey& key, |
196 const unsigned char* data, | 196 const unsigned char* data, |
197 unsigned data_size, | 197 unsigned int data_size, |
198 blink::WebCryptoResult result) { | 198 blink::WebCryptoResult result) { |
199 DCHECK(!algorithm.isNull()); | 199 DCHECK(!algorithm.isNull()); |
200 blink::WebArrayBuffer buffer; | 200 blink::WebArrayBuffer buffer; |
201 Status status = DecryptInternal(algorithm, key, data, data_size, &buffer); | 201 Status status = DecryptInternal(algorithm, key, data, data_size, &buffer); |
202 if (status.IsError()) | 202 if (status.IsError()) |
203 CompleteWithError(status, &result); | 203 CompleteWithError(status, &result); |
204 else | 204 else |
205 result.completeWithBuffer(buffer); | 205 result.completeWithBuffer(buffer); |
206 } | 206 } |
207 | 207 |
208 void WebCryptoImpl::digest( | 208 void WebCryptoImpl::digest( |
209 const blink::WebCryptoAlgorithm& algorithm, | 209 const blink::WebCryptoAlgorithm& algorithm, |
210 const unsigned char* data, | 210 const unsigned char* data, |
211 unsigned data_size, | 211 unsigned int data_size, |
212 blink::WebCryptoResult result) { | 212 blink::WebCryptoResult result) { |
213 DCHECK(!algorithm.isNull()); | 213 DCHECK(!algorithm.isNull()); |
214 blink::WebArrayBuffer buffer; | 214 blink::WebArrayBuffer buffer; |
215 Status status = DigestInternal(algorithm, data, data_size, &buffer); | 215 Status status = DigestInternal(algorithm, data, data_size, &buffer); |
216 if (status.IsError()) | 216 if (status.IsError()) |
217 CompleteWithError(status, &result); | 217 CompleteWithError(status, &result); |
218 else | 218 else |
219 result.completeWithBuffer(buffer); | 219 result.completeWithBuffer(buffer); |
220 } | 220 } |
221 | 221 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 DCHECK_EQ(extractable, key.extractable()); | 255 DCHECK_EQ(extractable, key.extractable()); |
256 DCHECK_EQ(usage_mask, key.usages()); | 256 DCHECK_EQ(usage_mask, key.usages()); |
257 result.completeWithKey(key); | 257 result.completeWithKey(key); |
258 } | 258 } |
259 } | 259 } |
260 } | 260 } |
261 | 261 |
262 void WebCryptoImpl::importKey( | 262 void WebCryptoImpl::importKey( |
263 blink::WebCryptoKeyFormat format, | 263 blink::WebCryptoKeyFormat format, |
264 const unsigned char* key_data, | 264 const unsigned char* key_data, |
265 unsigned key_data_size, | 265 unsigned int key_data_size, |
266 const blink::WebCryptoAlgorithm& algorithm_or_null, | 266 const blink::WebCryptoAlgorithm& algorithm_or_null, |
267 bool extractable, | 267 bool extractable, |
268 blink::WebCryptoKeyUsageMask usage_mask, | 268 blink::WebCryptoKeyUsageMask usage_mask, |
269 blink::WebCryptoResult result) { | 269 blink::WebCryptoResult result) { |
270 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | 270 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
271 Status status = Status::Error(); | 271 Status status = Status::Error(); |
272 if (format == blink::WebCryptoKeyFormatJwk) { | 272 if (format == blink::WebCryptoKeyFormatJwk) { |
273 status = ImportKeyJwk(key_data, | 273 status = ImportKeyJwk(key_data, |
274 key_data_size, | 274 key_data_size, |
275 algorithm_or_null, | 275 algorithm_or_null, |
(...skipping 28 matching lines...) Expand all Loading... |
304 if (status.IsError()) | 304 if (status.IsError()) |
305 CompleteWithError(status, &result); | 305 CompleteWithError(status, &result); |
306 else | 306 else |
307 result.completeWithBuffer(buffer); | 307 result.completeWithBuffer(buffer); |
308 } | 308 } |
309 | 309 |
310 void WebCryptoImpl::sign( | 310 void WebCryptoImpl::sign( |
311 const blink::WebCryptoAlgorithm& algorithm, | 311 const blink::WebCryptoAlgorithm& algorithm, |
312 const blink::WebCryptoKey& key, | 312 const blink::WebCryptoKey& key, |
313 const unsigned char* data, | 313 const unsigned char* data, |
314 unsigned data_size, | 314 unsigned int data_size, |
315 blink::WebCryptoResult result) { | 315 blink::WebCryptoResult result) { |
316 DCHECK(!algorithm.isNull()); | 316 DCHECK(!algorithm.isNull()); |
317 blink::WebArrayBuffer buffer; | 317 blink::WebArrayBuffer buffer; |
318 Status status = SignInternal(algorithm, key, data, data_size, &buffer); | 318 Status status = SignInternal(algorithm, key, data, data_size, &buffer); |
319 if (status.IsError()) | 319 if (status.IsError()) |
320 CompleteWithError(status, &result); | 320 CompleteWithError(status, &result); |
321 else | 321 else |
322 result.completeWithBuffer(buffer); | 322 result.completeWithBuffer(buffer); |
323 } | 323 } |
324 | 324 |
325 void WebCryptoImpl::verifySignature( | 325 void WebCryptoImpl::verifySignature( |
326 const blink::WebCryptoAlgorithm& algorithm, | 326 const blink::WebCryptoAlgorithm& algorithm, |
327 const blink::WebCryptoKey& key, | 327 const blink::WebCryptoKey& key, |
328 const unsigned char* signature, | 328 const unsigned char* signature, |
329 unsigned signature_size, | 329 unsigned int signature_size, |
330 const unsigned char* data, | 330 const unsigned char* data, |
331 unsigned data_size, | 331 unsigned int data_size, |
332 blink::WebCryptoResult result) { | 332 blink::WebCryptoResult result) { |
333 DCHECK(!algorithm.isNull()); | 333 DCHECK(!algorithm.isNull()); |
334 bool signature_match = false; | 334 bool signature_match = false; |
335 Status status = VerifySignatureInternal(algorithm, | 335 Status status = VerifySignatureInternal(algorithm, |
336 key, | 336 key, |
337 signature, | 337 signature, |
338 signature_size, | 338 signature_size, |
339 data, | 339 data, |
340 data_size, | 340 data_size, |
341 &signature_match); | 341 &signature_match); |
342 if (status.IsError()) | 342 if (status.IsError()) |
343 CompleteWithError(status, &result); | 343 CompleteWithError(status, &result); |
344 else | 344 else |
345 result.completeWithBoolean(signature_match); | 345 result.completeWithBoolean(signature_match); |
346 } | 346 } |
347 | 347 |
348 Status WebCryptoImpl::ImportKeyJwk( | 348 Status WebCryptoImpl::ImportKeyJwk( |
349 const unsigned char* key_data, | 349 const unsigned char* key_data, |
350 unsigned key_data_size, | 350 unsigned int key_data_size, |
351 const blink::WebCryptoAlgorithm& algorithm_or_null, | 351 const blink::WebCryptoAlgorithm& algorithm_or_null, |
352 bool extractable, | 352 bool extractable, |
353 blink::WebCryptoKeyUsageMask usage_mask, | 353 blink::WebCryptoKeyUsageMask usage_mask, |
354 blink::WebCryptoKey* key) { | 354 blink::WebCryptoKey* key) { |
355 | 355 |
356 // The goal of this method is to extract key material and meta data from the | 356 // The goal of this method is to extract key material and meta data from the |
357 // incoming JWK, combine them with the input parameters, and ultimately import | 357 // incoming JWK, combine them with the input parameters, and ultimately import |
358 // a Web Crypto Key. | 358 // a Web Crypto Key. |
359 // | 359 // |
360 // JSON Web Key Format (JWK) | 360 // JSON Web Key Format (JWK) |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 key); | 636 key); |
637 | 637 |
638 } else { | 638 } else { |
639 return Status::ErrorJwkUnrecognizedKty(); | 639 return Status::ErrorJwkUnrecognizedKty(); |
640 } | 640 } |
641 | 641 |
642 return Status::Success(); | 642 return Status::Success(); |
643 } | 643 } |
644 | 644 |
645 } // namespace content | 645 } // namespace content |
OLD | NEW |