[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/renderer/webcrypto_impl.h" |
| 6 | |
[email protected] | 1c879bc9 | 2013-09-18 07:45:32 | [diff] [blame] | 7 | #include "base/memory/scoped_ptr.h" |
[email protected] | e5b794b | 2013-08-30 01:32:54 | [diff] [blame] | 8 | #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
[email protected] | 1c879bc9 | 2013-09-18 07:45:32 | [diff] [blame] | 9 | #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| 10 | #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 11 | |
| 12 | namespace content { |
| 13 | |
[email protected] | 7e4c36f | 2013-09-12 06:10:19 | [diff] [blame] | 14 | WebCryptoImpl::WebCryptoImpl() { |
| 15 | Init(); |
| 16 | } |
| 17 | |
[email protected] | e5b794b | 2013-08-30 01:32:54 | [diff] [blame] | 18 | void WebCryptoImpl::digest( |
| 19 | const WebKit::WebCryptoAlgorithm& algorithm, |
| 20 | const unsigned char* data, |
[email protected] | ed80a09 | 2013-09-10 16:36:38 | [diff] [blame] | 21 | unsigned data_size, |
[email protected] | e5b794b | 2013-08-30 01:32:54 | [diff] [blame] | 22 | WebKit::WebCryptoResult result) { |
| 23 | WebKit::WebArrayBuffer buffer; |
[email protected] | ed80a09 | 2013-09-10 16:36:38 | [diff] [blame] | 24 | if (!DigestInternal(algorithm, data, data_size, &buffer)) { |
[email protected] | e5b794b | 2013-08-30 01:32:54 | [diff] [blame] | 25 | result.completeWithError(); |
| 26 | } else { |
| 27 | result.completeWithBuffer(buffer); |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
[email protected] | 1c879bc9 | 2013-09-18 07:45:32 | [diff] [blame] | 31 | void WebCryptoImpl::importKey( |
| 32 | WebKit::WebCryptoKeyFormat format, |
| 33 | const unsigned char* key_data, |
| 34 | unsigned key_data_size, |
| 35 | const WebKit::WebCryptoAlgorithm& algorithm, |
| 36 | bool extractable, |
| 37 | WebKit::WebCryptoKeyUsageMask usage_mask, |
| 38 | WebKit::WebCryptoResult result) { |
| 39 | WebKit::WebCryptoKeyType type; |
| 40 | scoped_ptr<WebKit::WebCryptoKeyHandle> handle; |
| 41 | |
| 42 | if (!ImportKeyInternal(format, |
| 43 | key_data, |
| 44 | key_data_size, |
| 45 | algorithm, |
| 46 | usage_mask, |
| 47 | &handle, |
| 48 | &type)) { |
| 49 | result.completeWithError(); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | WebKit::WebCryptoKey key( |
| 54 | WebKit::WebCryptoKey::create( |
| 55 | handle.release(), type, extractable, algorithm, usage_mask)); |
| 56 | |
| 57 | result.completeWithKey(key); |
| 58 | } |
| 59 | |
| 60 | void WebCryptoImpl::sign( |
| 61 | const WebKit::WebCryptoAlgorithm& algorithm, |
| 62 | const WebKit::WebCryptoKey& key, |
| 63 | const unsigned char* data, |
| 64 | unsigned data_size, |
| 65 | WebKit::WebCryptoResult result) { |
| 66 | WebKit::WebArrayBuffer buffer; |
| 67 | if (!SignInternal(algorithm, key, data, data_size, &buffer)) { |
| 68 | result.completeWithError(); |
| 69 | } else { |
| 70 | result.completeWithBuffer(buffer); |
| 71 | } |
| 72 | } |
| 73 | |
[email protected] | 3ed0026 | 2013-09-26 22:28:10 | [diff] [blame^] | 74 | void WebCryptoImpl::verifySignature( |
| 75 | const WebKit::WebCryptoAlgorithm& algorithm, |
| 76 | const WebKit::WebCryptoKey& key, |
| 77 | const unsigned char* signature, |
| 78 | unsigned signature_size, |
| 79 | const unsigned char* data, |
| 80 | unsigned data_size, |
| 81 | WebKit::WebCryptoResult result) { |
| 82 | bool signature_match = false; |
| 83 | if (!VerifySignatureInternal(algorithm, |
| 84 | key, |
| 85 | signature, |
| 86 | signature_size, |
| 87 | data, |
| 88 | data_size, |
| 89 | &signature_match)) { |
| 90 | result.completeWithError(); |
| 91 | } else { |
| 92 | result.completeWithBoolean(signature_match); |
| 93 | } |
| 94 | } |
| 95 | |
[email protected] | 408699c | 2013-07-17 21:23:16 | [diff] [blame] | 96 | } // namespace content |