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 "content/renderer/webcrypto/crypto_data.h" | 8 #include "content/renderer/webcrypto/crypto_data.h" |
9 #include "content/renderer/webcrypto/shared_crypto.h" | 9 #include "content/renderer/webcrypto/shared_crypto.h" |
10 #include "content/renderer/webcrypto/webcrypto_util.h" | 10 #include "content/renderer/webcrypto/webcrypto_util.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 } | 25 } |
26 | 26 |
27 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { | 27 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { |
28 // TODO(padolph): include all other asymmetric algorithms once they are | 28 // TODO(padolph): include all other asymmetric algorithms once they are |
29 // defined, e.g. EC and DH. | 29 // defined, e.g. EC and DH. |
30 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | 30 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || |
31 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | 31 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || |
32 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); | 32 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); |
33 } | 33 } |
34 | 34 |
35 | |
36 } // namespace | 35 } // namespace |
37 | 36 |
38 WebCryptoImpl::WebCryptoImpl() { | 37 WebCryptoImpl::WebCryptoImpl() { webcrypto::Init(); } |
39 webcrypto::Init(); | |
40 } | |
41 | 38 |
42 WebCryptoImpl::~WebCryptoImpl() {} | 39 WebCryptoImpl::~WebCryptoImpl() {} |
43 | 40 |
44 void WebCryptoImpl::encrypt( | 41 void WebCryptoImpl::encrypt(const blink::WebCryptoAlgorithm& algorithm, |
45 const blink::WebCryptoAlgorithm& algorithm, | 42 const blink::WebCryptoKey& key, |
46 const blink::WebCryptoKey& key, | 43 const unsigned char* data, |
47 const unsigned char* data, | 44 unsigned int data_size, |
48 unsigned int data_size, | 45 blink::WebCryptoResult result) { |
49 blink::WebCryptoResult result) { | |
50 DCHECK(!algorithm.isNull()); | 46 DCHECK(!algorithm.isNull()); |
51 blink::WebArrayBuffer buffer; | 47 blink::WebArrayBuffer buffer; |
52 Status status = webcrypto::Encrypt( | 48 Status status = webcrypto::Encrypt( |
53 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | 49 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); |
54 if (status.IsError()) | 50 if (status.IsError()) |
55 CompleteWithError(status, &result); | 51 CompleteWithError(status, &result); |
56 else | 52 else |
57 result.completeWithBuffer(buffer); | 53 result.completeWithBuffer(buffer); |
58 } | 54 } |
59 | 55 |
60 void WebCryptoImpl::decrypt( | 56 void WebCryptoImpl::decrypt(const blink::WebCryptoAlgorithm& algorithm, |
61 const blink::WebCryptoAlgorithm& algorithm, | 57 const blink::WebCryptoKey& key, |
62 const blink::WebCryptoKey& key, | 58 const unsigned char* data, |
63 const unsigned char* data, | 59 unsigned int data_size, |
64 unsigned int data_size, | 60 blink::WebCryptoResult result) { |
65 blink::WebCryptoResult result) { | |
66 DCHECK(!algorithm.isNull()); | 61 DCHECK(!algorithm.isNull()); |
67 blink::WebArrayBuffer buffer; | 62 blink::WebArrayBuffer buffer; |
68 Status status = webcrypto::Decrypt( | 63 Status status = webcrypto::Decrypt( |
69 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | 64 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); |
70 if (status.IsError()) | 65 if (status.IsError()) |
71 CompleteWithError(status, &result); | 66 CompleteWithError(status, &result); |
72 else | 67 else |
73 result.completeWithBuffer(buffer); | 68 result.completeWithBuffer(buffer); |
74 } | 69 } |
75 | 70 |
76 void WebCryptoImpl::digest( | 71 void WebCryptoImpl::digest(const blink::WebCryptoAlgorithm& algorithm, |
77 const blink::WebCryptoAlgorithm& algorithm, | 72 const unsigned char* data, |
78 const unsigned char* data, | 73 unsigned int data_size, |
79 unsigned int data_size, | 74 blink::WebCryptoResult result) { |
80 blink::WebCryptoResult result) { | |
81 DCHECK(!algorithm.isNull()); | 75 DCHECK(!algorithm.isNull()); |
82 blink::WebArrayBuffer buffer; | 76 blink::WebArrayBuffer buffer; |
83 Status status = webcrypto::Digest( | 77 Status status = webcrypto::Digest( |
84 algorithm, webcrypto::CryptoData(data, data_size), &buffer); | 78 algorithm, webcrypto::CryptoData(data, data_size), &buffer); |
85 if (status.IsError()) | 79 if (status.IsError()) |
86 CompleteWithError(status, &result); | 80 CompleteWithError(status, &result); |
87 else | 81 else |
88 result.completeWithBuffer(buffer); | 82 result.completeWithBuffer(buffer); |
89 } | 83 } |
90 | 84 |
91 void WebCryptoImpl::generateKey( | 85 void WebCryptoImpl::generateKey(const blink::WebCryptoAlgorithm& algorithm, |
92 const blink::WebCryptoAlgorithm& algorithm, | 86 bool extractable, |
93 bool extractable, | 87 blink::WebCryptoKeyUsageMask usage_mask, |
94 blink::WebCryptoKeyUsageMask usage_mask, | 88 blink::WebCryptoResult result) { |
95 blink::WebCryptoResult result) { | |
96 DCHECK(!algorithm.isNull()); | 89 DCHECK(!algorithm.isNull()); |
97 if (IsAlgorithmAsymmetric(algorithm)) { | 90 if (IsAlgorithmAsymmetric(algorithm)) { |
98 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | 91 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); |
99 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | 92 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); |
100 Status status = webcrypto::GenerateKeyPair( | 93 Status status = webcrypto::GenerateKeyPair( |
101 algorithm, extractable, usage_mask, &public_key, &private_key); | 94 algorithm, extractable, usage_mask, &public_key, &private_key); |
102 if (status.IsError()) { | 95 if (status.IsError()) { |
103 CompleteWithError(status, &result); | 96 CompleteWithError(status, &result); |
104 } else { | 97 } else { |
105 DCHECK(public_key.handle()); | 98 DCHECK(public_key.handle()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 if (status.IsError()) { | 140 if (status.IsError()) { |
148 CompleteWithError(status, &result); | 141 CompleteWithError(status, &result); |
149 } else { | 142 } else { |
150 DCHECK(key.handle()); | 143 DCHECK(key.handle()); |
151 DCHECK(!key.algorithm().isNull()); | 144 DCHECK(!key.algorithm().isNull()); |
152 DCHECK_EQ(extractable, key.extractable()); | 145 DCHECK_EQ(extractable, key.extractable()); |
153 result.completeWithKey(key); | 146 result.completeWithKey(key); |
154 } | 147 } |
155 } | 148 } |
156 | 149 |
157 void WebCryptoImpl::exportKey( | 150 void WebCryptoImpl::exportKey(blink::WebCryptoKeyFormat format, |
158 blink::WebCryptoKeyFormat format, | 151 const blink::WebCryptoKey& key, |
159 const blink::WebCryptoKey& key, | 152 blink::WebCryptoResult result) { |
160 blink::WebCryptoResult result) { | |
161 blink::WebArrayBuffer buffer; | 153 blink::WebArrayBuffer buffer; |
162 Status status = webcrypto::ExportKey(format, key, &buffer); | 154 Status status = webcrypto::ExportKey(format, key, &buffer); |
163 if (status.IsError()) | 155 if (status.IsError()) |
164 CompleteWithError(status, &result); | 156 CompleteWithError(status, &result); |
165 else | 157 else |
166 result.completeWithBuffer(buffer); | 158 result.completeWithBuffer(buffer); |
167 } | 159 } |
168 | 160 |
169 void WebCryptoImpl::sign( | 161 void WebCryptoImpl::sign(const blink::WebCryptoAlgorithm& algorithm, |
170 const blink::WebCryptoAlgorithm& algorithm, | 162 const blink::WebCryptoKey& key, |
171 const blink::WebCryptoKey& key, | 163 const unsigned char* data, |
172 const unsigned char* data, | 164 unsigned int data_size, |
173 unsigned int data_size, | 165 blink::WebCryptoResult result) { |
174 blink::WebCryptoResult result) { | |
175 DCHECK(!algorithm.isNull()); | 166 DCHECK(!algorithm.isNull()); |
176 blink::WebArrayBuffer buffer; | 167 blink::WebArrayBuffer buffer; |
177 Status status = webcrypto::Sign( | 168 Status status = webcrypto::Sign( |
178 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | 169 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); |
179 if (status.IsError()) | 170 if (status.IsError()) |
180 CompleteWithError(status, &result); | 171 CompleteWithError(status, &result); |
181 else | 172 else |
182 result.completeWithBuffer(buffer); | 173 result.completeWithBuffer(buffer); |
183 } | 174 } |
184 | 175 |
185 void WebCryptoImpl::verifySignature( | 176 void WebCryptoImpl::verifySignature(const blink::WebCryptoAlgorithm& algorithm, |
186 const blink::WebCryptoAlgorithm& algorithm, | 177 const blink::WebCryptoKey& key, |
187 const blink::WebCryptoKey& key, | 178 const unsigned char* signature, |
188 const unsigned char* signature, | 179 unsigned int signature_size, |
189 unsigned int signature_size, | 180 const unsigned char* data, |
190 const unsigned char* data, | 181 unsigned int data_size, |
191 unsigned int data_size, | 182 blink::WebCryptoResult result) { |
192 blink::WebCryptoResult result) { | |
193 DCHECK(!algorithm.isNull()); | 183 DCHECK(!algorithm.isNull()); |
194 bool signature_match = false; | 184 bool signature_match = false; |
195 Status status = webcrypto::VerifySignature( | 185 Status status = webcrypto::VerifySignature( |
196 algorithm, | 186 algorithm, |
197 key, | 187 key, |
198 webcrypto::CryptoData(signature, signature_size), | 188 webcrypto::CryptoData(signature, signature_size), |
199 webcrypto::CryptoData(data, data_size), | 189 webcrypto::CryptoData(data, data_size), |
200 &signature_match); | 190 &signature_match); |
201 if (status.IsError()) | 191 if (status.IsError()) |
202 CompleteWithError(status, &result); | 192 CompleteWithError(status, &result); |
203 else | 193 else |
204 result.completeWithBoolean(signature_match); | 194 result.completeWithBoolean(signature_match); |
205 } | 195 } |
206 | 196 |
207 } // namespace content | 197 } // namespace content |
OLD | NEW |