| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | 9 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | 10 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 if (new_size == buffer->byteLength()) | 27 if (new_size == buffer->byteLength()) |
| 28 return; | 28 return; |
| 29 | 29 |
| 30 WebKit::WebArrayBuffer new_buffer = | 30 WebKit::WebArrayBuffer new_buffer = |
| 31 WebKit::WebArrayBuffer::create(new_size, 1); | 31 WebKit::WebArrayBuffer::create(new_size, 1); |
| 32 DCHECK(!new_buffer.isNull()); | 32 DCHECK(!new_buffer.isNull()); |
| 33 memcpy(new_buffer.data(), buffer->data(), new_size); | 33 memcpy(new_buffer.data(), buffer->data(), new_size); |
| 34 *buffer = new_buffer; | 34 *buffer = new_buffer; |
| 35 } | 35 } |
| 36 | 36 |
| 37 // static |
| 38 // TODO(eroman): Expose functionality in Blink instead. |
| 39 WebKit::WebCryptoKey WebCryptoImpl::NullKey() { |
| 40 // Needs a non-null algorithm to succeed. |
| 41 return WebKit::WebCryptoKey::create( |
| 42 NULL, WebKit::WebCryptoKeyTypeSecret, false, |
| 43 WebKit::WebCryptoAlgorithm::adoptParamsAndCreate( |
| 44 WebKit::WebCryptoAlgorithmIdAesGcm, NULL), 0); |
| 45 } |
| 46 |
| 37 void WebCryptoImpl::encrypt( | 47 void WebCryptoImpl::encrypt( |
| 38 const WebKit::WebCryptoAlgorithm& algorithm, | 48 const WebKit::WebCryptoAlgorithm& algorithm, |
| 39 const WebKit::WebCryptoKey& key, | 49 const WebKit::WebCryptoKey& key, |
| 40 const unsigned char* data, | 50 const unsigned char* data, |
| 41 unsigned data_size, | 51 unsigned data_size, |
| 42 WebKit::WebCryptoResult result) { | 52 WebKit::WebCryptoResult result) { |
| 53 DCHECK(!algorithm.isNull()); |
| 43 WebKit::WebArrayBuffer buffer; | 54 WebKit::WebArrayBuffer buffer; |
| 44 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { | 55 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { |
| 45 result.completeWithError(); | 56 result.completeWithError(); |
| 46 } else { | 57 } else { |
| 47 result.completeWithBuffer(buffer); | 58 result.completeWithBuffer(buffer); |
| 48 } | 59 } |
| 49 } | 60 } |
| 50 | 61 |
| 51 void WebCryptoImpl::decrypt( | 62 void WebCryptoImpl::decrypt( |
| 52 const WebKit::WebCryptoAlgorithm& algorithm, | 63 const WebKit::WebCryptoAlgorithm& algorithm, |
| 53 const WebKit::WebCryptoKey& key, | 64 const WebKit::WebCryptoKey& key, |
| 54 const unsigned char* data, | 65 const unsigned char* data, |
| 55 unsigned data_size, | 66 unsigned data_size, |
| 56 WebKit::WebCryptoResult result) { | 67 WebKit::WebCryptoResult result) { |
| 68 DCHECK(!algorithm.isNull()); |
| 57 WebKit::WebArrayBuffer buffer; | 69 WebKit::WebArrayBuffer buffer; |
| 58 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { | 70 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { |
| 59 result.completeWithError(); | 71 result.completeWithError(); |
| 60 } else { | 72 } else { |
| 61 result.completeWithBuffer(buffer); | 73 result.completeWithBuffer(buffer); |
| 62 } | 74 } |
| 63 } | 75 } |
| 64 | 76 |
| 65 void WebCryptoImpl::digest( | 77 void WebCryptoImpl::digest( |
| 66 const WebKit::WebCryptoAlgorithm& algorithm, | 78 const WebKit::WebCryptoAlgorithm& algorithm, |
| 67 const unsigned char* data, | 79 const unsigned char* data, |
| 68 unsigned data_size, | 80 unsigned data_size, |
| 69 WebKit::WebCryptoResult result) { | 81 WebKit::WebCryptoResult result) { |
| 82 DCHECK(!algorithm.isNull()); |
| 70 WebKit::WebArrayBuffer buffer; | 83 WebKit::WebArrayBuffer buffer; |
| 71 if (!DigestInternal(algorithm, data, data_size, &buffer)) { | 84 if (!DigestInternal(algorithm, data, data_size, &buffer)) { |
| 72 result.completeWithError(); | 85 result.completeWithError(); |
| 73 } else { | 86 } else { |
| 74 result.completeWithBuffer(buffer); | 87 result.completeWithBuffer(buffer); |
| 75 } | 88 } |
| 76 } | 89 } |
| 77 | 90 |
| 78 void WebCryptoImpl::generateKey( | 91 void WebCryptoImpl::generateKey( |
| 79 const WebKit::WebCryptoAlgorithm& algorithm, | 92 const WebKit::WebCryptoAlgorithm& algorithm, |
| 80 bool exportable, | 93 bool extractable, |
| 81 WebKit::WebCryptoKeyUsageMask usage, | 94 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 82 WebKit::WebCryptoResult result) { | 95 WebKit::WebCryptoResult result) { |
| 83 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | 96 DCHECK(!algorithm.isNull()); |
| 84 WebKit::WebCryptoKeyType type; | 97 WebKit::WebCryptoKey key = NullKey(); |
| 85 if (!GenerateKeyInternal(algorithm, &handle, &type)) { | 98 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { |
| 86 result.completeWithError(); | 99 result.completeWithError(); |
| 87 } else { | 100 } else { |
| 88 WebKit::WebCryptoKey key( | 101 DCHECK(key.handle()); |
| 89 WebKit::WebCryptoKey::create(handle.release(), type, exportable, | 102 DCHECK_EQ(algorithm.id(), key.algorithm().id()); |
| 90 algorithm, usage)); | 103 DCHECK_EQ(extractable, key.extractable()); |
| 104 DCHECK_EQ(usage_mask, key.usages()); |
| 91 result.completeWithKey(key); | 105 result.completeWithKey(key); |
| 92 } | 106 } |
| 93 } | 107 } |
| 94 | 108 |
| 95 void WebCryptoImpl::importKey( | 109 void WebCryptoImpl::importKey( |
| 96 WebKit::WebCryptoKeyFormat format, | 110 WebKit::WebCryptoKeyFormat format, |
| 97 const unsigned char* key_data, | 111 const unsigned char* key_data, |
| 98 unsigned key_data_size, | 112 unsigned key_data_size, |
| 99 const WebKit::WebCryptoAlgorithm& algorithm, | 113 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
| 100 bool extractable, | 114 bool extractable, |
| 101 WebKit::WebCryptoKeyUsageMask usage_mask, | 115 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 102 WebKit::WebCryptoResult result) { | 116 WebKit::WebCryptoResult result) { |
| 103 WebKit::WebCryptoKeyType type; | 117 WebKit::WebCryptoKey key = NullKey(); |
| 104 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | |
| 105 | |
| 106 if (!ImportKeyInternal(format, | 118 if (!ImportKeyInternal(format, |
| 107 key_data, | 119 key_data, |
| 108 key_data_size, | 120 key_data_size, |
| 109 algorithm, | 121 algorithm_or_null, |
| 122 extractable, |
| 110 usage_mask, | 123 usage_mask, |
| 111 &handle, | 124 &key)) { |
| 112 &type)) { | |
| 113 result.completeWithError(); | 125 result.completeWithError(); |
| 114 return; | 126 return; |
| 115 } | 127 } |
| 116 | 128 DCHECK(key.handle()); |
| 117 WebKit::WebCryptoKey key( | 129 DCHECK(!key.algorithm().isNull()); |
| 118 WebKit::WebCryptoKey::create( | 130 DCHECK_EQ(extractable, key.extractable()); |
| 119 handle.release(), type, extractable, algorithm, usage_mask)); | |
| 120 | |
| 121 result.completeWithKey(key); | 131 result.completeWithKey(key); |
| 122 } | 132 } |
| 123 | 133 |
| 124 void WebCryptoImpl::sign( | 134 void WebCryptoImpl::sign( |
| 125 const WebKit::WebCryptoAlgorithm& algorithm, | 135 const WebKit::WebCryptoAlgorithm& algorithm, |
| 126 const WebKit::WebCryptoKey& key, | 136 const WebKit::WebCryptoKey& key, |
| 127 const unsigned char* data, | 137 const unsigned char* data, |
| 128 unsigned data_size, | 138 unsigned data_size, |
| 129 WebKit::WebCryptoResult result) { | 139 WebKit::WebCryptoResult result) { |
| 140 DCHECK(!algorithm.isNull()); |
| 130 WebKit::WebArrayBuffer buffer; | 141 WebKit::WebArrayBuffer buffer; |
| 131 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { | 142 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { |
| 132 result.completeWithError(); | 143 result.completeWithError(); |
| 133 } else { | 144 } else { |
| 134 result.completeWithBuffer(buffer); | 145 result.completeWithBuffer(buffer); |
| 135 } | 146 } |
| 136 } | 147 } |
| 137 | 148 |
| 138 void WebCryptoImpl::verifySignature( | 149 void WebCryptoImpl::verifySignature( |
| 139 const WebKit::WebCryptoAlgorithm& algorithm, | 150 const WebKit::WebCryptoAlgorithm& algorithm, |
| 140 const WebKit::WebCryptoKey& key, | 151 const WebKit::WebCryptoKey& key, |
| 141 const unsigned char* signature, | 152 const unsigned char* signature, |
| 142 unsigned signature_size, | 153 unsigned signature_size, |
| 143 const unsigned char* data, | 154 const unsigned char* data, |
| 144 unsigned data_size, | 155 unsigned data_size, |
| 145 WebKit::WebCryptoResult result) { | 156 WebKit::WebCryptoResult result) { |
| 157 DCHECK(!algorithm.isNull()); |
| 146 bool signature_match = false; | 158 bool signature_match = false; |
| 147 if (!VerifySignatureInternal(algorithm, | 159 if (!VerifySignatureInternal(algorithm, |
| 148 key, | 160 key, |
| 149 signature, | 161 signature, |
| 150 signature_size, | 162 signature_size, |
| 151 data, | 163 data, |
| 152 data_size, | 164 data_size, |
| 153 &signature_match)) { | 165 &signature_match)) { |
| 154 result.completeWithError(); | 166 result.completeWithError(); |
| 155 } else { | 167 } else { |
| 156 result.completeWithBoolean(signature_match); | 168 result.completeWithBoolean(signature_match); |
| 157 } | 169 } |
| 158 } | 170 } |
| 159 | 171 |
| 160 } // namespace content | 172 } // namespace content |
| OLD | NEW |