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