| 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 "content/child/webcrypto/algorithm_dispatch.h" | 5 #include "content/child/webcrypto/algorithm_dispatch.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "content/child/webcrypto/algorithm_implementation.h" | 8 #include "content/child/webcrypto/algorithm_implementation.h" |
| 9 #include "content/child/webcrypto/algorithm_registry.h" | 9 #include "content/child/webcrypto/algorithm_registry.h" |
| 10 #include "content/child/webcrypto/crypto_data.h" | 10 #include "content/child/webcrypto/crypto_data.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 const AlgorithmImplementation* impl = NULL; | 97 const AlgorithmImplementation* impl = NULL; |
| 98 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 98 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 99 if (status.IsError()) | 99 if (status.IsError()) |
| 100 return status; | 100 return status; |
| 101 | 101 |
| 102 return impl->Digest(algorithm, data, buffer); | 102 return impl->Digest(algorithm, data, buffer); |
| 103 } | 103 } |
| 104 | 104 |
| 105 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, | 105 Status GenerateKey(const blink::WebCryptoAlgorithm& algorithm, |
| 106 bool extractable, | 106 bool extractable, |
| 107 blink::WebCryptoKeyUsageMask usage_mask, | 107 blink::WebCryptoKeyUsageMask usages, |
| 108 GenerateKeyResult* result) { | 108 GenerateKeyResult* result) { |
| 109 const AlgorithmImplementation* impl = NULL; | 109 const AlgorithmImplementation* impl = NULL; |
| 110 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 110 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 111 if (status.IsError()) | 111 if (status.IsError()) |
| 112 return status; | 112 return status; |
| 113 | 113 |
| 114 return impl->GenerateKey(algorithm, extractable, usage_mask, result); | 114 return impl->GenerateKey(algorithm, extractable, usages, result); |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Note that this function may be called from the target Blink thread. | 117 // Note that this function may be called from the target Blink thread. |
| 118 Status ImportKey(blink::WebCryptoKeyFormat format, | 118 Status ImportKey(blink::WebCryptoKeyFormat format, |
| 119 const CryptoData& key_data, | 119 const CryptoData& key_data, |
| 120 const blink::WebCryptoAlgorithm& algorithm, | 120 const blink::WebCryptoAlgorithm& algorithm, |
| 121 bool extractable, | 121 bool extractable, |
| 122 blink::WebCryptoKeyUsageMask usage_mask, | 122 blink::WebCryptoKeyUsageMask usages, |
| 123 blink::WebCryptoKey* key) { | 123 blink::WebCryptoKey* key) { |
| 124 const AlgorithmImplementation* impl = NULL; | 124 const AlgorithmImplementation* impl = NULL; |
| 125 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 125 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 126 if (status.IsError()) | 126 if (status.IsError()) |
| 127 return status; | 127 return status; |
| 128 | 128 |
| 129 status = impl->VerifyKeyUsagesBeforeImportKey(format, usage_mask); | 129 status = impl->VerifyKeyUsagesBeforeImportKey(format, usages); |
| 130 if (status.IsError()) | 130 if (status.IsError()) |
| 131 return status; | 131 return status; |
| 132 | 132 |
| 133 switch (format) { | 133 switch (format) { |
| 134 case blink::WebCryptoKeyFormatRaw: | 134 case blink::WebCryptoKeyFormatRaw: |
| 135 return impl->ImportKeyRaw( | 135 return impl->ImportKeyRaw(key_data, algorithm, extractable, usages, key); |
| 136 key_data, algorithm, extractable, usage_mask, key); | |
| 137 case blink::WebCryptoKeyFormatSpki: | 136 case blink::WebCryptoKeyFormatSpki: |
| 138 return impl->ImportKeySpki( | 137 return impl->ImportKeySpki(key_data, algorithm, extractable, usages, key); |
| 139 key_data, algorithm, extractable, usage_mask, key); | |
| 140 case blink::WebCryptoKeyFormatPkcs8: | 138 case blink::WebCryptoKeyFormatPkcs8: |
| 141 return impl->ImportKeyPkcs8( | 139 return impl->ImportKeyPkcs8( |
| 142 key_data, algorithm, extractable, usage_mask, key); | 140 key_data, algorithm, extractable, usages, key); |
| 143 case blink::WebCryptoKeyFormatJwk: | 141 case blink::WebCryptoKeyFormatJwk: |
| 144 return impl->ImportKeyJwk( | 142 return impl->ImportKeyJwk(key_data, algorithm, extractable, usages, key); |
| 145 key_data, algorithm, extractable, usage_mask, key); | |
| 146 default: | 143 default: |
| 147 return Status::ErrorUnsupported(); | 144 return Status::ErrorUnsupported(); |
| 148 } | 145 } |
| 149 } | 146 } |
| 150 | 147 |
| 151 Status ExportKey(blink::WebCryptoKeyFormat format, | 148 Status ExportKey(blink::WebCryptoKeyFormat format, |
| 152 const blink::WebCryptoKey& key, | 149 const blink::WebCryptoKey& key, |
| 153 std::vector<uint8_t>* buffer) { | 150 std::vector<uint8_t>* buffer) { |
| 154 if (!key.extractable()) | 151 if (!key.extractable()) |
| 155 return Status::ErrorKeyNotExtractable(); | 152 return Status::ErrorKeyNotExtractable(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return EncryptDontCheckUsage( | 203 return EncryptDontCheckUsage( |
| 207 wrapping_algorithm, wrapping_key, CryptoData(exported_data), buffer); | 204 wrapping_algorithm, wrapping_key, CryptoData(exported_data), buffer); |
| 208 } | 205 } |
| 209 | 206 |
| 210 Status UnwrapKey(blink::WebCryptoKeyFormat format, | 207 Status UnwrapKey(blink::WebCryptoKeyFormat format, |
| 211 const CryptoData& wrapped_key_data, | 208 const CryptoData& wrapped_key_data, |
| 212 const blink::WebCryptoKey& wrapping_key, | 209 const blink::WebCryptoKey& wrapping_key, |
| 213 const blink::WebCryptoAlgorithm& wrapping_algorithm, | 210 const blink::WebCryptoAlgorithm& wrapping_algorithm, |
| 214 const blink::WebCryptoAlgorithm& algorithm, | 211 const blink::WebCryptoAlgorithm& algorithm, |
| 215 bool extractable, | 212 bool extractable, |
| 216 blink::WebCryptoKeyUsageMask usage_mask, | 213 blink::WebCryptoKeyUsageMask usages, |
| 217 blink::WebCryptoKey* key) { | 214 blink::WebCryptoKey* key) { |
| 218 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageUnwrapKey)) | 215 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageUnwrapKey)) |
| 219 return Status::ErrorUnexpected(); | 216 return Status::ErrorUnexpected(); |
| 220 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) | 217 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) |
| 221 return Status::ErrorUnexpected(); | 218 return Status::ErrorUnexpected(); |
| 222 | 219 |
| 223 // Fail fast if the import is doomed to fail. | 220 // Fail fast if the import is doomed to fail. |
| 224 const AlgorithmImplementation* import_impl = NULL; | 221 const AlgorithmImplementation* import_impl = NULL; |
| 225 Status status = GetAlgorithmImplementation(algorithm.id(), &import_impl); | 222 Status status = GetAlgorithmImplementation(algorithm.id(), &import_impl); |
| 226 if (status.IsError()) | 223 if (status.IsError()) |
| 227 return status; | 224 return status; |
| 228 | 225 |
| 229 status = import_impl->VerifyKeyUsagesBeforeImportKey(format, usage_mask); | 226 status = import_impl->VerifyKeyUsagesBeforeImportKey(format, usages); |
| 230 if (status.IsError()) | 227 if (status.IsError()) |
| 231 return status; | 228 return status; |
| 232 | 229 |
| 233 std::vector<uint8_t> buffer; | 230 std::vector<uint8_t> buffer; |
| 234 status = DecryptDontCheckKeyUsage( | 231 status = DecryptDontCheckKeyUsage( |
| 235 wrapping_algorithm, wrapping_key, wrapped_key_data, &buffer); | 232 wrapping_algorithm, wrapping_key, wrapped_key_data, &buffer); |
| 236 if (status.IsError()) | 233 if (status.IsError()) |
| 237 return status; | 234 return status; |
| 238 | 235 |
| 239 // NOTE that returning the details of ImportKey() failures may leak | 236 // NOTE that returning the details of ImportKey() failures may leak |
| 240 // information about the plaintext of the encrypted key (for instance the JWK | 237 // information about the plaintext of the encrypted key (for instance the JWK |
| 241 // key_ops). As long as the ImportKey error messages don't describe actual | 238 // key_ops). As long as the ImportKey error messages don't describe actual |
| 242 // key bytes however this should be OK. For more discussion see | 239 // key bytes however this should be OK. For more discussion see |
| 243 // http://crubg.com/372040 | 240 // http://crubg.com/372040 |
| 244 return ImportKey( | 241 return ImportKey( |
| 245 format, CryptoData(buffer), algorithm, extractable, usage_mask, key); | 242 format, CryptoData(buffer), algorithm, extractable, usages, key); |
| 246 } | 243 } |
| 247 | 244 |
| 248 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( | 245 scoped_ptr<blink::WebCryptoDigestor> CreateDigestor( |
| 249 blink::WebCryptoAlgorithmId algorithm) { | 246 blink::WebCryptoAlgorithmId algorithm) { |
| 250 PlatformInit(); | 247 PlatformInit(); |
| 251 return CreatePlatformDigestor(algorithm); | 248 return CreatePlatformDigestor(algorithm); |
| 252 } | 249 } |
| 253 | 250 |
| 254 } // namespace webcrypto | 251 } // namespace webcrypto |
| 255 | 252 |
| 256 } // namespace content | 253 } // namespace content |
| OLD | NEW |