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 { | 15 namespace { |
16 | 16 |
17 bool IsAlgorithmAsymmetric(const WebKit::WebCryptoAlgorithm& algorithm) { | 17 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { |
18 // TODO(padolph): include all other asymmetric algorithms once they are | 18 // TODO(padolph): include all other asymmetric algorithms once they are |
19 // defined, e.g. EC and DH. | 19 // defined, e.g. EC and DH. |
20 return (algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 20 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
21 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 21 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
22 algorithm.id() == WebKit::WebCryptoAlgorithmIdRsaOaep); | 22 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 WebCryptoImpl::WebCryptoImpl() { | 27 WebCryptoImpl::WebCryptoImpl() { |
28 Init(); | 28 Init(); |
29 } | 29 } |
30 | 30 |
31 // static | 31 // static |
32 // 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 |
33 // the WebArrayBuffer could just be truncated instead. | 33 // the WebArrayBuffer could just be truncated instead. |
34 void WebCryptoImpl::ShrinkBuffer( | 34 void WebCryptoImpl::ShrinkBuffer( |
35 WebKit::WebArrayBuffer* buffer, | 35 blink::WebArrayBuffer* buffer, |
36 unsigned new_size) { | 36 unsigned new_size) { |
37 DCHECK_LE(new_size, buffer->byteLength()); | 37 DCHECK_LE(new_size, buffer->byteLength()); |
38 | 38 |
39 if (new_size == buffer->byteLength()) | 39 if (new_size == buffer->byteLength()) |
40 return; | 40 return; |
41 | 41 |
42 WebKit::WebArrayBuffer new_buffer = | 42 blink::WebArrayBuffer new_buffer = |
43 WebKit::WebArrayBuffer::create(new_size, 1); | 43 blink::WebArrayBuffer::create(new_size, 1); |
44 DCHECK(!new_buffer.isNull()); | 44 DCHECK(!new_buffer.isNull()); |
45 memcpy(new_buffer.data(), buffer->data(), new_size); | 45 memcpy(new_buffer.data(), buffer->data(), new_size); |
46 *buffer = new_buffer; | 46 *buffer = new_buffer; |
47 } | 47 } |
48 | 48 |
49 void WebCryptoImpl::encrypt( | 49 void WebCryptoImpl::encrypt( |
50 const WebKit::WebCryptoAlgorithm& algorithm, | 50 const blink::WebCryptoAlgorithm& algorithm, |
51 const WebKit::WebCryptoKey& key, | 51 const blink::WebCryptoKey& key, |
52 const unsigned char* data, | 52 const unsigned char* data, |
53 unsigned data_size, | 53 unsigned data_size, |
54 WebKit::WebCryptoResult result) { | 54 blink::WebCryptoResult result) { |
55 DCHECK(!algorithm.isNull()); | 55 DCHECK(!algorithm.isNull()); |
56 WebKit::WebArrayBuffer buffer; | 56 blink::WebArrayBuffer buffer; |
57 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { | 57 if (!EncryptInternal(algorithm, key, data, data_size, &buffer)) { |
58 result.completeWithError(); | 58 result.completeWithError(); |
59 } else { | 59 } else { |
60 result.completeWithBuffer(buffer); | 60 result.completeWithBuffer(buffer); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 void WebCryptoImpl::decrypt( | 64 void WebCryptoImpl::decrypt( |
65 const WebKit::WebCryptoAlgorithm& algorithm, | 65 const blink::WebCryptoAlgorithm& algorithm, |
66 const WebKit::WebCryptoKey& key, | 66 const blink::WebCryptoKey& key, |
67 const unsigned char* data, | 67 const unsigned char* data, |
68 unsigned data_size, | 68 unsigned data_size, |
69 WebKit::WebCryptoResult result) { | 69 blink::WebCryptoResult result) { |
70 DCHECK(!algorithm.isNull()); | 70 DCHECK(!algorithm.isNull()); |
71 WebKit::WebArrayBuffer buffer; | 71 blink::WebArrayBuffer buffer; |
72 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { | 72 if (!DecryptInternal(algorithm, key, data, data_size, &buffer)) { |
73 result.completeWithError(); | 73 result.completeWithError(); |
74 } else { | 74 } else { |
75 result.completeWithBuffer(buffer); | 75 result.completeWithBuffer(buffer); |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 void WebCryptoImpl::digest( | 79 void WebCryptoImpl::digest( |
80 const WebKit::WebCryptoAlgorithm& algorithm, | 80 const blink::WebCryptoAlgorithm& algorithm, |
81 const unsigned char* data, | 81 const unsigned char* data, |
82 unsigned data_size, | 82 unsigned data_size, |
83 WebKit::WebCryptoResult result) { | 83 blink::WebCryptoResult result) { |
84 DCHECK(!algorithm.isNull()); | 84 DCHECK(!algorithm.isNull()); |
85 WebKit::WebArrayBuffer buffer; | 85 blink::WebArrayBuffer buffer; |
86 if (!DigestInternal(algorithm, data, data_size, &buffer)) { | 86 if (!DigestInternal(algorithm, data, data_size, &buffer)) { |
87 result.completeWithError(); | 87 result.completeWithError(); |
88 } else { | 88 } else { |
89 result.completeWithBuffer(buffer); | 89 result.completeWithBuffer(buffer); |
90 } | 90 } |
91 } | 91 } |
92 | 92 |
93 void WebCryptoImpl::generateKey( | 93 void WebCryptoImpl::generateKey( |
94 const WebKit::WebCryptoAlgorithm& algorithm, | 94 const blink::WebCryptoAlgorithm& algorithm, |
95 bool extractable, | 95 bool extractable, |
96 WebKit::WebCryptoKeyUsageMask usage_mask, | 96 blink::WebCryptoKeyUsageMask usage_mask, |
97 WebKit::WebCryptoResult result) { | 97 blink::WebCryptoResult result) { |
98 DCHECK(!algorithm.isNull()); | 98 DCHECK(!algorithm.isNull()); |
99 if (IsAlgorithmAsymmetric(algorithm)) { | 99 if (IsAlgorithmAsymmetric(algorithm)) { |
100 WebKit::WebCryptoKey public_key = WebKit::WebCryptoKey::createNull(); | 100 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
101 WebKit::WebCryptoKey private_key = WebKit::WebCryptoKey::createNull(); | 101 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
102 if (!GenerateKeyPairInternal( | 102 if (!GenerateKeyPairInternal( |
103 algorithm, extractable, usage_mask, &public_key, &private_key)) { | 103 algorithm, extractable, usage_mask, &public_key, &private_key)) { |
104 result.completeWithError(); | 104 result.completeWithError(); |
105 } else { | 105 } else { |
106 DCHECK(public_key.handle()); | 106 DCHECK(public_key.handle()); |
107 DCHECK(private_key.handle()); | 107 DCHECK(private_key.handle()); |
108 DCHECK_EQ(algorithm.id(), public_key.algorithm().id()); | 108 DCHECK_EQ(algorithm.id(), public_key.algorithm().id()); |
109 DCHECK_EQ(algorithm.id(), private_key.algorithm().id()); | 109 DCHECK_EQ(algorithm.id(), private_key.algorithm().id()); |
110 // TODO(padolph): The public key should probably always be extractable, | 110 // TODO(padolph): The public key should probably always be extractable, |
111 // regardless of the input 'extractable' parameter, but that is not called | 111 // regardless of the input 'extractable' parameter, but that is not called |
112 // out in the Web Crypto API spec. | 112 // out in the Web Crypto API spec. |
113 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=23695 | 113 // See https://www.w3.org/Bugs/Public/show_bug.cgi?id=23695 |
114 DCHECK_EQ(extractable, public_key.extractable()); | 114 DCHECK_EQ(extractable, public_key.extractable()); |
115 DCHECK_EQ(extractable, private_key.extractable()); | 115 DCHECK_EQ(extractable, private_key.extractable()); |
116 DCHECK_EQ(usage_mask, public_key.usages()); | 116 DCHECK_EQ(usage_mask, public_key.usages()); |
117 DCHECK_EQ(usage_mask, private_key.usages()); | 117 DCHECK_EQ(usage_mask, private_key.usages()); |
118 result.completeWithKeyPair(public_key, private_key); | 118 result.completeWithKeyPair(public_key, private_key); |
119 } | 119 } |
120 } else { | 120 } else { |
121 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 121 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
122 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { | 122 if (!GenerateKeyInternal(algorithm, extractable, usage_mask, &key)) { |
123 result.completeWithError(); | 123 result.completeWithError(); |
124 } else { | 124 } else { |
125 DCHECK(key.handle()); | 125 DCHECK(key.handle()); |
126 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | 126 DCHECK_EQ(algorithm.id(), key.algorithm().id()); |
127 DCHECK_EQ(extractable, key.extractable()); | 127 DCHECK_EQ(extractable, key.extractable()); |
128 DCHECK_EQ(usage_mask, key.usages()); | 128 DCHECK_EQ(usage_mask, key.usages()); |
129 result.completeWithKey(key); | 129 result.completeWithKey(key); |
130 } | 130 } |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 void WebCryptoImpl::importKey( | 134 void WebCryptoImpl::importKey( |
135 WebKit::WebCryptoKeyFormat format, | 135 blink::WebCryptoKeyFormat format, |
136 const unsigned char* key_data, | 136 const unsigned char* key_data, |
137 unsigned key_data_size, | 137 unsigned key_data_size, |
138 const WebKit::WebCryptoAlgorithm& algorithm_or_null, | 138 const blink::WebCryptoAlgorithm& algorithm_or_null, |
139 bool extractable, | 139 bool extractable, |
140 WebKit::WebCryptoKeyUsageMask usage_mask, | 140 blink::WebCryptoKeyUsageMask usage_mask, |
141 WebKit::WebCryptoResult result) { | 141 blink::WebCryptoResult result) { |
142 WebKit::WebCryptoKey key = WebKit::WebCryptoKey::createNull(); | 142 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); |
143 if (!ImportKeyInternal(format, | 143 if (!ImportKeyInternal(format, |
144 key_data, | 144 key_data, |
145 key_data_size, | 145 key_data_size, |
146 algorithm_or_null, | 146 algorithm_or_null, |
147 extractable, | 147 extractable, |
148 usage_mask, | 148 usage_mask, |
149 &key)) { | 149 &key)) { |
150 result.completeWithError(); | 150 result.completeWithError(); |
151 return; | 151 return; |
152 } | 152 } |
153 DCHECK(key.handle()); | 153 DCHECK(key.handle()); |
154 DCHECK(!key.algorithm().isNull()); | 154 DCHECK(!key.algorithm().isNull()); |
155 DCHECK_EQ(extractable, key.extractable()); | 155 DCHECK_EQ(extractable, key.extractable()); |
156 result.completeWithKey(key); | 156 result.completeWithKey(key); |
157 } | 157 } |
158 | 158 |
159 void WebCryptoImpl::sign( | 159 void WebCryptoImpl::sign( |
160 const WebKit::WebCryptoAlgorithm& algorithm, | 160 const blink::WebCryptoAlgorithm& algorithm, |
161 const WebKit::WebCryptoKey& key, | 161 const blink::WebCryptoKey& key, |
162 const unsigned char* data, | 162 const unsigned char* data, |
163 unsigned data_size, | 163 unsigned data_size, |
164 WebKit::WebCryptoResult result) { | 164 blink::WebCryptoResult result) { |
165 DCHECK(!algorithm.isNull()); | 165 DCHECK(!algorithm.isNull()); |
166 WebKit::WebArrayBuffer buffer; | 166 blink::WebArrayBuffer buffer; |
167 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { | 167 if (!SignInternal(algorithm, key, data, data_size, &buffer)) { |
168 result.completeWithError(); | 168 result.completeWithError(); |
169 } else { | 169 } else { |
170 result.completeWithBuffer(buffer); | 170 result.completeWithBuffer(buffer); |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 void WebCryptoImpl::verifySignature( | 174 void WebCryptoImpl::verifySignature( |
175 const WebKit::WebCryptoAlgorithm& algorithm, | 175 const blink::WebCryptoAlgorithm& algorithm, |
176 const WebKit::WebCryptoKey& key, | 176 const blink::WebCryptoKey& key, |
177 const unsigned char* signature, | 177 const unsigned char* signature, |
178 unsigned signature_size, | 178 unsigned signature_size, |
179 const unsigned char* data, | 179 const unsigned char* data, |
180 unsigned data_size, | 180 unsigned data_size, |
181 WebKit::WebCryptoResult result) { | 181 blink::WebCryptoResult result) { |
182 DCHECK(!algorithm.isNull()); | 182 DCHECK(!algorithm.isNull()); |
183 bool signature_match = false; | 183 bool signature_match = false; |
184 if (!VerifySignatureInternal(algorithm, | 184 if (!VerifySignatureInternal(algorithm, |
185 key, | 185 key, |
186 signature, | 186 signature, |
187 signature_size, | 187 signature_size, |
188 data, | 188 data, |
189 data_size, | 189 data_size, |
190 &signature_match)) { | 190 &signature_match)) { |
191 result.completeWithError(); | 191 result.completeWithError(); |
192 } else { | 192 } else { |
193 result.completeWithBoolean(signature_match); | 193 result.completeWithBoolean(signature_match); |
194 } | 194 } |
195 } | 195 } |
196 | 196 |
197 } // namespace content | 197 } // namespace content |
OLD | NEW |