Chromium Code Reviews| 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(); | |
|
eroman
2013/10/31 22:18:36
[optional]: seems equally short to inline.
padolph
2013/11/01 20:35:31
Done.
| |
| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 result.completeWithBuffer(buffer); | 100 result.completeWithBuffer(buffer); |
| 88 } | 101 } |
| 89 } | 102 } |
| 90 | 103 |
| 91 void WebCryptoImpl::generateKey( | 104 void WebCryptoImpl::generateKey( |
| 92 const WebKit::WebCryptoAlgorithm& algorithm, | 105 const WebKit::WebCryptoAlgorithm& algorithm, |
| 93 bool extractable, | 106 bool extractable, |
| 94 WebKit::WebCryptoKeyUsageMask usage_mask, | 107 WebKit::WebCryptoKeyUsageMask usage_mask, |
| 95 WebKit::WebCryptoResult result) { | 108 WebKit::WebCryptoResult result) { |
| 96 DCHECK(!algorithm.isNull()); | 109 DCHECK(!algorithm.isNull()); |
| 97 WebKit::WebCryptoKey key = NullKey(); | 110 if (IsAlgorithmAsymmetric(algorithm)) { |
| 98 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { | 111 WebKit::WebCryptoKey public_key = NullKey(); |
|
eroman
2013/10/31 22:18:36
[optional]: I recommend using WebKit::WebCryptoKey
padolph
2013/11/01 20:35:31
Done.
| |
| 99 result.completeWithError(); | 112 WebKit::WebCryptoKey private_key = NullKey(); |
| 113 if (!GenerateKeyPairInternal( | |
| 114 algorithm, extractable, usage_mask, &public_key, &private_key)) { | |
| 115 result.completeWithError(); | |
| 116 } else { | |
| 117 DCHECK(public_key.handle()); | |
| 118 DCHECK(private_key.handle()); | |
| 119 DCHECK_EQ(algorithm.id(), public_key.algorithm().id()); | |
| 120 DCHECK_EQ(algorithm.id(), private_key.algorithm().id()); | |
| 121 DCHECK_EQ(true, public_key.extractable()); | |
|
eroman
2013/10/31 22:18:36
Shouldn't this be comparing against "extractable"
padolph
2013/11/01 20:35:31
This is related to the spec bug you and Ryan discu
| |
| 122 DCHECK_EQ(extractable, private_key.extractable()); | |
| 123 DCHECK_EQ(usage_mask, public_key.usages()); | |
| 124 DCHECK_EQ(usage_mask, private_key.usages()); | |
| 125 result.completeWithKeyPair(public_key, private_key); | |
| 126 } | |
| 100 } else { | 127 } else { |
| 101 DCHECK(key.handle()); | 128 WebKit::WebCryptoKey key = NullKey(); |
| 102 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | 129 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { |
| 103 DCHECK_EQ(extractable, key.extractable()); | 130 result.completeWithError(); |
| 104 DCHECK_EQ(usage_mask, key.usages()); | 131 } else { |
| 105 result.completeWithKey(key); | 132 DCHECK(key.handle()); |
| 133 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | |
| 134 DCHECK_EQ(extractable, key.extractable()); | |
| 135 DCHECK_EQ(usage_mask, key.usages()); | |
| 136 result.completeWithKey(key); | |
| 137 } | |
| 106 } | 138 } |
| 107 } | 139 } |
| 108 | 140 |
| 109 void WebCryptoImpl::importKey( | 141 void WebCryptoImpl::importKey( |
| 110 WebKit::WebCryptoKeyFormat format, | 142 WebKit::WebCryptoKeyFormat format, |
| 111 const unsigned char* key_data, | 143 const unsigned char* key_data, |
| 112 unsigned key_data_size, | 144 unsigned key_data_size, |
| 113 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | 145 const WebKit::WebCryptoAlgorithm& algorithm_or_null, |
| 114 bool extractable, | 146 bool extractable, |
| 115 WebKit::WebCryptoKeyUsageMask usage_mask, | 147 WebKit::WebCryptoKeyUsageMask usage_mask, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 data, | 195 data, |
| 164 data_size, | 196 data_size, |
| 165 &signature_match)) { | 197 &signature_match)) { |
| 166 result.completeWithError(); | 198 result.completeWithError(); |
| 167 } else { | 199 } else { |
| 168 result.completeWithBoolean(signature_match); | 200 result.completeWithBoolean(signature_match); |
| 169 } | 201 } |
| 170 } | 202 } |
| 171 | 203 |
| 172 } // namespace content | 204 } // namespace content |
| OLD | NEW |