| 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" |
| 11 #include "third_party/WebKit/public/platform/WebCryptoKey.h" | 11 #include "third_party/WebKit/public/platform/WebCryptoKey.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { |
| 16 |
| 17 bool IsAlgorithmAsymmetric(const WebKit::WebCryptoAlgorithm& algorithm) { |
| 18 const WebKit::WebCryptoAlgorithmId algorithm_id = algorithm.id(); |
| 19 // TODO(padolph): include all other asymmetric algorithms once they are |
| 20 // defined, e.g. EC and DH. |
| 21 return (algorithm_id == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| 22 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 23 algorithm_id == WebKit::WebCryptoAlgorithmIdRsaOaep); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 15 WebCryptoImpl::WebCryptoImpl() { | 28 WebCryptoImpl::WebCryptoImpl() { |
| 16 Init(); | 29 Init(); |
| 17 } | 30 } |
| 18 | 31 |
| 19 // static | 32 // static |
| 20 // TODO(eroman): This works by re-allocating a new buffer. It would be better if | 33 // TODO(eroman): This works by re-allocating a new buffer. It would be better if |
| 21 // the WebArrayBuffer could just be truncated instead. | 34 // the WebArrayBuffer could just be truncated instead. |
| 22 void WebCryptoImpl::ShrinkBuffer( | 35 void WebCryptoImpl::ShrinkBuffer( |
| 23 WebKit::WebArrayBuffer* buffer, | 36 WebKit::WebArrayBuffer* buffer, |
| 24 unsigned new_size) { | 37 unsigned new_size) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 exportable, |
| 81 WebKit::WebCryptoKeyUsageMask usage, | 94 WebKit::WebCryptoKeyUsageMask usage, |
| 82 WebKit::WebCryptoResult result) { | 95 WebKit::WebCryptoResult result) { |
| 96 if (IsAlgorithmAsymmetric(algorithm)) { |
| 97 scoped_ptr<WebKit::WebCryptoKeyHandle> public_key_handle; |
| 98 scoped_ptr<WebKit::WebCryptoKeyHandle> private_key_handle; |
| 99 if (!GenerateKeyPairInternal(algorithm, |
| 100 &public_key_handle, |
| 101 &private_key_handle)) { |
| 102 result.completeWithError(); |
| 103 return; |
| 104 } |
| 105 WebKit::WebCryptoKey public_key( |
| 106 WebKit::WebCryptoKey::create(public_key_handle.release(), |
| 107 WebKit::WebCryptoKeyTypePublic, |
| 108 exportable, |
| 109 algorithm, |
| 110 usage)); |
| 111 WebKit::WebCryptoKey private_key( |
| 112 WebKit::WebCryptoKey::create(private_key_handle.release(), |
| 113 WebKit::WebCryptoKeyTypePrivate, |
| 114 exportable, |
| 115 algorithm, |
| 116 usage)); |
| 117 result.completeWithKeyPair(public_key, private_key); |
| 118 return; |
| 119 } |
| 83 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; | 120 scoped_ptr<WebKit::WebCryptoKeyHandle> handle; |
| 84 WebKit::WebCryptoKeyType type; | 121 WebKit::WebCryptoKeyType type; |
| 85 if (!GenerateKeyInternal(algorithm, &handle, &type)) { | 122 if (!GenerateKeyInternal(algorithm, &handle, &type)) { |
| 86 result.completeWithError(); | 123 result.completeWithError(); |
| 87 } else { | 124 return; |
| 88 WebKit::WebCryptoKey key( | |
| 89 WebKit::WebCryptoKey::create(handle.release(), type, exportable, | |
| 90 algorithm, usage)); | |
| 91 result.completeWithKey(key); | |
| 92 } | 125 } |
| 126 WebKit::WebCryptoKey key( |
| 127 WebKit::WebCryptoKey::create(handle.release(), type, exportable, |
| 128 algorithm, usage)); |
| 129 result.completeWithKey(key); |
| 93 } | 130 } |
| 94 | 131 |
| 95 void WebCryptoImpl::importKey( | 132 void WebCryptoImpl::importKey( |
| 96 WebKit::WebCryptoKeyFormat format, | 133 WebKit::WebCryptoKeyFormat format, |
| 97 const unsigned char* key_data, | 134 const unsigned char* key_data, |
| 98 unsigned key_data_size, | 135 unsigned key_data_size, |
| 99 const WebKit::WebCryptoAlgorithm& algorithm, | 136 const WebKit::WebCryptoAlgorithm& algorithm, |
| 100 bool extractable, | 137 bool extractable, |
| 101 WebKit::WebCryptoKeyUsageMask usage_mask, | 138 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 102 WebKit::WebCryptoResult result) { | 139 WebKit::WebCryptoResult result) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 data, | 188 data, |
| 152 data_size, | 189 data_size, |
| 153 &signature_match)) { | 190 &signature_match)) { |
| 154 result.completeWithError(); | 191 result.completeWithError(); |
| 155 } else { | 192 } else { |
| 156 result.completeWithBoolean(signature_match); | 193 result.completeWithBoolean(signature_match); |
| 157 } | 194 } |
| 158 } | 195 } |
| 159 | 196 |
| 160 } // namespace content | 197 } // namespace content |
| OLD | NEW |