OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/webcrypto/webcrypto_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "content/renderer/webcrypto/crypto_data.h" | |
9 #include "content/renderer/webcrypto/shared_crypto.h" | |
10 #include "content/renderer/webcrypto/webcrypto_util.h" | |
11 #include "third_party/WebKit/public/platform/WebString.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | |
13 | |
14 namespace content { | |
15 | |
16 using webcrypto::Status; | |
17 | |
18 namespace { | |
19 | |
20 void CompleteWithError(const Status& status, blink::WebCryptoResult* result) { | |
21 DCHECK(status.IsError()); | |
22 if (status.HasErrorDetails()) | |
23 result->completeWithError(blink::WebString::fromUTF8(status.ToString())); | |
24 else | |
25 result->completeWithError(); | |
26 } | |
27 | |
28 bool IsAlgorithmAsymmetric(const blink::WebCryptoAlgorithm& algorithm) { | |
29 // TODO(padolph): include all other asymmetric algorithms once they are | |
30 // defined, e.g. EC and DH. | |
31 return (algorithm.id() == blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5 || | |
32 algorithm.id() == blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5 || | |
33 algorithm.id() == blink::WebCryptoAlgorithmIdRsaOaep); | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 WebCryptoImpl::WebCryptoImpl() { webcrypto::Init(); } | |
39 | |
40 WebCryptoImpl::~WebCryptoImpl() {} | |
41 | |
42 void WebCryptoImpl::encrypt(const blink::WebCryptoAlgorithm& algorithm, | |
43 const blink::WebCryptoKey& key, | |
44 const unsigned char* data, | |
45 unsigned int data_size, | |
46 blink::WebCryptoResult result) { | |
47 DCHECK(!algorithm.isNull()); | |
48 blink::WebArrayBuffer buffer; | |
49 Status status = webcrypto::Encrypt( | |
50 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | |
51 if (status.IsError()) | |
52 CompleteWithError(status, &result); | |
53 else | |
54 result.completeWithBuffer(buffer); | |
55 } | |
56 | |
57 void WebCryptoImpl::decrypt(const blink::WebCryptoAlgorithm& algorithm, | |
58 const blink::WebCryptoKey& key, | |
59 const unsigned char* data, | |
60 unsigned int data_size, | |
61 blink::WebCryptoResult result) { | |
62 DCHECK(!algorithm.isNull()); | |
63 blink::WebArrayBuffer buffer; | |
64 Status status = webcrypto::Decrypt( | |
65 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | |
66 if (status.IsError()) | |
67 CompleteWithError(status, &result); | |
68 else | |
69 result.completeWithBuffer(buffer); | |
70 } | |
71 | |
72 void WebCryptoImpl::digest(const blink::WebCryptoAlgorithm& algorithm, | |
73 const unsigned char* data, | |
74 unsigned int data_size, | |
75 blink::WebCryptoResult result) { | |
76 DCHECK(!algorithm.isNull()); | |
77 blink::WebArrayBuffer buffer; | |
78 Status status = webcrypto::Digest( | |
79 algorithm, webcrypto::CryptoData(data, data_size), &buffer); | |
80 if (status.IsError()) | |
81 CompleteWithError(status, &result); | |
82 else | |
83 result.completeWithBuffer(buffer); | |
84 } | |
85 | |
86 void WebCryptoImpl::generateKey(const blink::WebCryptoAlgorithm& algorithm, | |
87 bool extractable, | |
88 blink::WebCryptoKeyUsageMask usage_mask, | |
89 blink::WebCryptoResult result) { | |
90 DCHECK(!algorithm.isNull()); | |
91 if (IsAlgorithmAsymmetric(algorithm)) { | |
92 blink::WebCryptoKey public_key = blink::WebCryptoKey::createNull(); | |
93 blink::WebCryptoKey private_key = blink::WebCryptoKey::createNull(); | |
94 Status status = webcrypto::GenerateKeyPair( | |
95 algorithm, extractable, usage_mask, &public_key, &private_key); | |
96 if (status.IsError()) { | |
97 CompleteWithError(status, &result); | |
98 } else { | |
99 DCHECK(public_key.handle()); | |
100 DCHECK(private_key.handle()); | |
101 DCHECK_EQ(algorithm.id(), public_key.algorithm().id()); | |
102 DCHECK_EQ(algorithm.id(), private_key.algorithm().id()); | |
103 DCHECK_EQ(true, public_key.extractable()); | |
104 DCHECK_EQ(extractable, private_key.extractable()); | |
105 DCHECK_EQ(usage_mask, public_key.usages()); | |
106 DCHECK_EQ(usage_mask, private_key.usages()); | |
107 result.completeWithKeyPair(public_key, private_key); | |
108 } | |
109 } else { | |
110 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | |
111 Status status = | |
112 webcrypto::GenerateSecretKey(algorithm, extractable, usage_mask, &key); | |
113 if (status.IsError()) { | |
114 CompleteWithError(status, &result); | |
115 } else { | |
116 DCHECK(key.handle()); | |
117 DCHECK_EQ(algorithm.id(), key.algorithm().id()); | |
118 DCHECK_EQ(extractable, key.extractable()); | |
119 DCHECK_EQ(usage_mask, key.usages()); | |
120 result.completeWithKey(key); | |
121 } | |
122 } | |
123 } | |
124 | |
125 void WebCryptoImpl::importKey( | |
126 blink::WebCryptoKeyFormat format, | |
127 const unsigned char* key_data, | |
128 unsigned int key_data_size, | |
129 const blink::WebCryptoAlgorithm& algorithm_or_null, | |
130 bool extractable, | |
131 blink::WebCryptoKeyUsageMask usage_mask, | |
132 blink::WebCryptoResult result) { | |
133 blink::WebCryptoKey key = blink::WebCryptoKey::createNull(); | |
134 Status status = | |
135 webcrypto::ImportKey(format, | |
136 webcrypto::CryptoData(key_data, key_data_size), | |
137 algorithm_or_null, | |
138 extractable, | |
139 usage_mask, | |
140 &key); | |
141 if (status.IsError()) { | |
142 CompleteWithError(status, &result); | |
143 } else { | |
144 DCHECK(key.handle()); | |
145 DCHECK(!key.algorithm().isNull()); | |
146 DCHECK_EQ(extractable, key.extractable()); | |
147 result.completeWithKey(key); | |
148 } | |
149 } | |
150 | |
151 void WebCryptoImpl::exportKey(blink::WebCryptoKeyFormat format, | |
152 const blink::WebCryptoKey& key, | |
153 blink::WebCryptoResult result) { | |
154 blink::WebArrayBuffer buffer; | |
155 Status status = webcrypto::ExportKey(format, key, &buffer); | |
156 if (status.IsError()) | |
157 CompleteWithError(status, &result); | |
158 else | |
159 result.completeWithBuffer(buffer); | |
160 } | |
161 | |
162 void WebCryptoImpl::sign(const blink::WebCryptoAlgorithm& algorithm, | |
163 const blink::WebCryptoKey& key, | |
164 const unsigned char* data, | |
165 unsigned int data_size, | |
166 blink::WebCryptoResult result) { | |
167 DCHECK(!algorithm.isNull()); | |
168 blink::WebArrayBuffer buffer; | |
169 Status status = webcrypto::Sign( | |
170 algorithm, key, webcrypto::CryptoData(data, data_size), &buffer); | |
171 if (status.IsError()) | |
172 CompleteWithError(status, &result); | |
173 else | |
174 result.completeWithBuffer(buffer); | |
175 } | |
176 | |
177 void WebCryptoImpl::verifySignature(const blink::WebCryptoAlgorithm& algorithm, | |
178 const blink::WebCryptoKey& key, | |
179 const unsigned char* signature, | |
180 unsigned int signature_size, | |
181 const unsigned char* data, | |
182 unsigned int data_size, | |
183 blink::WebCryptoResult result) { | |
184 DCHECK(!algorithm.isNull()); | |
185 bool signature_match = false; | |
186 Status status = webcrypto::VerifySignature( | |
187 algorithm, | |
188 key, | |
189 webcrypto::CryptoData(signature, signature_size), | |
190 webcrypto::CryptoData(data, data_size), | |
191 &signature_match); | |
192 if (status.IsError()) | |
193 CompleteWithError(status, &result); | |
194 else | |
195 result.completeWithBoolean(signature_match); | |
196 } | |
197 | |
198 bool WebCryptoImpl::digestSynchronous( | |
199 const blink::WebCryptoAlgorithmId algorithm_id, | |
200 const unsigned char* data, | |
201 unsigned int data_size, | |
202 blink::WebArrayBuffer& result) { | |
203 blink::WebCryptoAlgorithm algorithm = | |
204 blink::WebCryptoAlgorithm::adoptParamsAndCreate(algorithm_id, NULL); | |
205 return (webcrypto::Digest( | |
206 algorithm, webcrypto::CryptoData(data, data_size), &result)) | |
207 .IsSuccess(); | |
208 } | |
209 | |
210 } // namespace content | |
OLD | NEW |