OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <algorithm> | 5 #include <algorithm> |
6 #include <functional> | 6 #include <functional> |
7 #include <map> | 7 #include <map> |
8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "content/renderer/webcrypto/crypto_data.h" | 14 #include "content/renderer/webcrypto/crypto_data.h" |
15 #include "content/renderer/webcrypto/platform_crypto.h" | 15 #include "content/renderer/webcrypto/platform_crypto.h" |
16 #include "content/renderer/webcrypto/shared_crypto.h" | 16 #include "content/renderer/webcrypto/shared_crypto.h" |
17 #include "content/renderer/webcrypto/webcrypto_util.h" | 17 #include "content/renderer/webcrypto/webcrypto_util.h" |
18 #include "third_party/WebKit/public/platform/WebCrypto.h" // TODO(eroman): dele
te | |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 | 20 |
22 namespace webcrypto { | 21 namespace webcrypto { |
23 | 22 |
24 namespace { | 23 namespace { |
25 | 24 |
26 typedef blink::WebCryptoAlgorithm (*AlgorithmCreationFunc)(); | 25 typedef blink::WebCryptoAlgorithm (*AlgorithmCreationFunc)(); |
27 | 26 |
28 class JwkAlgorithmInfo { | 27 class JwkAlgorithmInfo { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 static blink::WebCryptoAlgorithm BindAlgorithmId() { | 134 static blink::WebCryptoAlgorithm BindAlgorithmId() { |
136 return func(algorithm_id); | 135 return func(algorithm_id); |
137 } | 136 } |
138 | 137 |
139 JwkAlgorithmInfoMap alg_to_info_; | 138 JwkAlgorithmInfoMap alg_to_info_; |
140 }; | 139 }; |
141 | 140 |
142 base::LazyInstance<JwkAlgorithmRegistry> jwk_alg_registry = | 141 base::LazyInstance<JwkAlgorithmRegistry> jwk_alg_registry = |
143 LAZY_INSTANCE_INITIALIZER; | 142 LAZY_INSTANCE_INITIALIZER; |
144 | 143 |
145 #ifdef WEBCRYPTO_HAS_KEY_ALGORITHM | |
146 bool ImportAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, | 144 bool ImportAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, |
147 const blink::WebCryptoAlgorithm& alg2) { | 145 const blink::WebCryptoAlgorithm& alg2) { |
148 DCHECK(!alg1.isNull()); | 146 DCHECK(!alg1.isNull()); |
149 DCHECK(!alg2.isNull()); | 147 DCHECK(!alg2.isNull()); |
150 if (alg1.id() != alg2.id()) | 148 if (alg1.id() != alg2.id()) |
151 return false; | 149 return false; |
152 if (alg1.paramsType() != alg2.paramsType()) | 150 if (alg1.paramsType() != alg2.paramsType()) |
153 return false; | 151 return false; |
154 switch (alg1.paramsType()) { | 152 switch (alg1.paramsType()) { |
155 case blink::WebCryptoAlgorithmParamsTypeNone: | 153 case blink::WebCryptoAlgorithmParamsTypeNone: |
156 return true; | 154 return true; |
157 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: | 155 case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: |
158 return ImportAlgorithmsConsistent(alg1.rsaHashedImportParams()->hash(), | 156 return ImportAlgorithmsConsistent(alg1.rsaHashedImportParams()->hash(), |
159 alg2.rsaHashedImportParams()->hash()); | 157 alg2.rsaHashedImportParams()->hash()); |
160 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: | 158 case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: |
161 return ImportAlgorithmsConsistent(alg1.hmacImportParams()->hash(), | 159 return ImportAlgorithmsConsistent(alg1.hmacImportParams()->hash(), |
162 alg2.hmacImportParams()->hash()); | 160 alg2.hmacImportParams()->hash()); |
163 default: | 161 default: |
164 return false; | 162 return false; |
165 } | 163 } |
166 } | 164 } |
167 #else | |
168 bool ImportAlgorithmsConsistent(const blink::WebCryptoAlgorithm& alg1, | |
169 const blink::WebCryptoAlgorithm& alg2) { | |
170 DCHECK(!alg1.isNull()); | |
171 DCHECK(!alg2.isNull()); | |
172 if (alg1.id() != alg2.id()) | |
173 return false; | |
174 switch (alg1.id()) { | |
175 case blink::WebCryptoAlgorithmIdHmac: | |
176 case blink::WebCryptoAlgorithmIdRsaOaep: | |
177 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
178 if (ImportAlgorithmsConsistent(GetInnerHashAlgorithm(alg1), | |
179 GetInnerHashAlgorithm(alg2))) { | |
180 return true; | |
181 } | |
182 break; | |
183 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5: | |
184 case blink::WebCryptoAlgorithmIdSha1: | |
185 case blink::WebCryptoAlgorithmIdSha224: | |
186 case blink::WebCryptoAlgorithmIdSha256: | |
187 case blink::WebCryptoAlgorithmIdSha384: | |
188 case blink::WebCryptoAlgorithmIdSha512: | |
189 case blink::WebCryptoAlgorithmIdAesCbc: | |
190 case blink::WebCryptoAlgorithmIdAesGcm: | |
191 case blink::WebCryptoAlgorithmIdAesCtr: | |
192 return true; | |
193 default: | |
194 NOTREACHED(); // Not a supported algorithm. | |
195 break; | |
196 } | |
197 return false; | |
198 } | |
199 #endif | |
200 | 165 |
201 // Extracts the required string property with key |path| from |dict| and saves | 166 // Extracts the required string property with key |path| from |dict| and saves |
202 // the result to |*result|. If the property does not exist or is not a string, | 167 // the result to |*result|. If the property does not exist or is not a string, |
203 // returns an error. | 168 // returns an error. |
204 Status GetJwkString(base::DictionaryValue* dict, | 169 Status GetJwkString(base::DictionaryValue* dict, |
205 const std::string& path, | 170 const std::string& path, |
206 std::string* result) { | 171 std::string* result) { |
207 base::Value* value = NULL; | 172 base::Value* value = NULL; |
208 if (!dict->Get(path, &value)) | 173 if (!dict->Get(path, &value)) |
209 return Status::ErrorJwkPropertyMissing(path); | 174 return Status::ErrorJwkPropertyMissing(path); |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 } else { | 545 } else { |
581 return Status::ErrorJwkUnrecognizedKty(); | 546 return Status::ErrorJwkUnrecognizedKty(); |
582 } | 547 } |
583 | 548 |
584 return Status::Success(); | 549 return Status::Success(); |
585 } | 550 } |
586 | 551 |
587 } // namespace webcrypto | 552 } // namespace webcrypto |
588 | 553 |
589 } // namespace content | 554 } // namespace content |
OLD | NEW |