| 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 // TODO(padolph): include all other asymmetric algorithms once they are |
| 19 // defined, e.g. EC and DH. |
| 20 return (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
| 21 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
| 22 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaOaep); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 15 WebCryptoImpl::WebCryptoImpl() { | 27 WebCryptoImpl::WebCryptoImpl() { |
| 16 Init(); | 28 Init(); |
| 17 } | 29 } |
| 18 | 30 |
| 19 // static | 31 // static |
| 20 // TODO(eroman): This works by re-allocating a new buffer. It would be better if | 32 // TODO(eroman): This works by re-allocating a new buffer. It would be better if |
| 21 // the WebArrayBuffer could just be truncated instead. | 33 // the WebArrayBuffer could just be truncated instead. |
| 22 void WebCryptoImpl::ShrinkBuffer( | 34 void WebCryptoImpl::ShrinkBuffer( |
| 23 WebKit::WebArrayBuffer* buffer, | 35 WebKit::WebArrayBuffer* buffer, |
| 24 unsigned new_size) { | 36 unsigned new_size) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 result.completeWithBuffer(buffer); | 99 result.completeWithBuffer(buffer); |
| 88 } | 100 } |
| 89 } | 101 } |
| 90 | 102 |
| 91 void WebCryptoImpl::generateKey( | 103 void WebCryptoImpl::generateKey( |
| 92 const WebKit::WebCryptoAlgorithm& algorithm, | 104 const WebKit::WebCryptoAlgorithm& algorithm, |
| 93 bool extractable, | 105 bool extractable, |
| 94 WebKit::WebCryptoKeyUsageMask usage_mask, | 106 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 95 WebKit::WebCryptoResult result) { | 107 WebKit::WebCryptoResult result) { |
| 96 DCHECK(!algorithm.isNull()); | 108 DCHECK(!algorithm.isNull()); |
| 97 WebKit::WebCryptoKey key = NullKey(); | 109 if (IsAlgorithmAsymmetric(algorithm)) { |
| 98 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { | 110 WebKit::WebCryptoKey public_key = WebKit::WebCryptoKey::createNull(); |
| 99 result.completeWithError(); | 111 WebKit::WebCryptoKey private_key = WebKit::WebCryptoKey::createNull(); |
| 112 if (!GenerateKeyPairInternal( |
| 113 algorithm, extractable, usage_mask, &public_key, &private_key)) { |
| 114 result.completeWithError(); |
| 115 } else { |
| 116 DCHECK(public_key.handle()); |
| 117 DCHECK(private_key.handle()); |
| 118 DCHECK_EQ(algorithm.id(), public_key.algorithm().id()); |
| 119 DCHECK_EQ(algorithm.id(), private_key.algorithm().id()); |
| 120 // TODO(padolph): The public key should probably always be extractable, |
| 121 // regardless of the input 'extractable' parameter, but that is not called |
| 122 // out in the Web Crypto API spec. |
| 123 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=23695 |
| 124 DCHECK_EQ(extractable, public_key.extractable()); |
| 125 DCHECK_EQ(extractable, private_key.extractable()); |
| 126 DCHECK_EQ(usage_mask, public_key.usages()); |
| 127 DCHECK_EQ(usage_mask, private_key.usages()); |
| 128 result.completeWithKeyPair(public_key, private_key); |
| 129 } |
| 100 } else { | 130 } else { |
| 101 DCHECK(key.handle()); | 131 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); |
| 102 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | 132 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { |
| 103 DCHECK_EQ(extractable, key.extractable()); | 133 result.completeWithError(); |
| 104 DCHECK_EQ(usage_mask, key.usages()); | 134 } else { |
| 105 result.completeWithKey(key); | 135 DCHECK(key.handle()); |
| 136 DCHECK_EQ(algorithm.id(), key.algorithm().id()); |
| 137 DCHECK_EQ(extractable, key.extractable()); |
| 138 DCHECK_EQ(usage_mask, key.usages()); |
| 139 result.completeWithKey(key); |
| 140 } |
| 106 } | 141 } |
| 107 } | 142 } |
| 108 | 143 |
| 109 void WebCryptoImpl::importKey( | 144 void WebCryptoImpl::importKey( |
| 110 WebKit::WebCryptoKeyFormat format, | 145 WebKit::WebCryptoKeyFormat format, |
| 111 const unsigned char* key_data, | 146 const unsigned char* key_data, |
| 112 unsigned key_data_size, | 147 unsigned key_data_size, |
| 113 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | 148 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
| 114 bool extractable, | 149 bool extractable, |
| 115 WebKit::WebCryptoKeyUsageMask usage_mask, | 150 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 116 WebKit::WebCryptoResult result) { | 151 WebKit::WebCryptoResult result) { |
| 117 WebKit::WebCryptoKey key = NullKey(); | 152 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); |
| 118 if (!ImportKeyInternal(format, | 153 if (!ImportKeyInternal(format, |
| 119 key_data, | 154 key_data, |
| 120 key_data_size, | 155 key_data_size, |
| 121 algorithm_or_null, | 156 algorithm_or_null, |
| 122 extractable, | 157 extractable, |
| 123 usage_mask, | 158 usage_mask, |
| 124 &key)) { | 159 &key)) { |
| 125 result.completeWithError(); | 160 result.completeWithError(); |
| 126 return; | 161 return; |
| 127 } | 162 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 data, | 198 data, |
| 164 data_size, | 199 data_size, |
| 165 &signature_match)) { | 200 &signature_match)) { |
| 166 result.completeWithError(); | 201 result.completeWithError(); |
| 167 } else { | 202 } else { |
| 168 result.completeWithBoolean(signature_match); | 203 result.completeWithBoolean(signature_match); |
| 169 } | 204 } |
| 170 } | 205 } |
| 171 | 206 |
| 172 } // namespace content | 207 } // namespace content |
| OLD | NEW |