| 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 "components/webcrypto/algorithm_dispatch.h" | 5 #include "components/webcrypto/algorithm_dispatch.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "components/webcrypto/algorithm_implementation.h" | 8 #include "components/webcrypto/algorithm_implementation.h" |
| 9 #include "components/webcrypto/algorithm_registry.h" | 9 #include "components/webcrypto/algorithm_registry.h" |
| 10 #include "components/webcrypto/crypto_data.h" | 10 #include "components/webcrypto/crypto_data.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 return impl->ExportKey(format, key, buffer); | 59 return impl->ExportKey(format, key, buffer); |
| 60 } | 60 } |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, | 64 Status Encrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 65 const blink::WebCryptoKey& key, | 65 const blink::WebCryptoKey& key, |
| 66 const CryptoData& data, | 66 const CryptoData& data, |
| 67 std::vector<uint8_t>* buffer) { | 67 std::vector<uint8_t>* buffer) { |
| 68 if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageEncrypt)) | 68 if (!key.keyUsageAllows(blink::WebCryptoKeyUsageEncrypt)) |
| 69 return Status::ErrorUnexpected(); | 69 return Status::ErrorUnexpected(); |
| 70 return EncryptDontCheckUsage(algorithm, key, data, buffer); | 70 return EncryptDontCheckUsage(algorithm, key, data, buffer); |
| 71 } | 71 } |
| 72 | 72 |
| 73 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, | 73 Status Decrypt(const blink::WebCryptoAlgorithm& algorithm, |
| 74 const blink::WebCryptoKey& key, | 74 const blink::WebCryptoKey& key, |
| 75 const CryptoData& data, | 75 const CryptoData& data, |
| 76 std::vector<uint8_t>* buffer) { | 76 std::vector<uint8_t>* buffer) { |
| 77 if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageDecrypt)) | 77 if (!key.keyUsageAllows(blink::WebCryptoKeyUsageDecrypt)) |
| 78 return Status::ErrorUnexpected(); | 78 return Status::ErrorUnexpected(); |
| 79 return DecryptDontCheckKeyUsage(algorithm, key, data, buffer); | 79 return DecryptDontCheckKeyUsage(algorithm, key, data, buffer); |
| 80 } | 80 } |
| 81 | 81 |
| 82 Status Digest(const blink::WebCryptoAlgorithm& algorithm, | 82 Status Digest(const blink::WebCryptoAlgorithm& algorithm, |
| 83 const CryptoData& data, | 83 const CryptoData& data, |
| 84 std::vector<uint8_t>* buffer) { | 84 std::vector<uint8_t>* buffer) { |
| 85 const AlgorithmImplementation* impl = NULL; | 85 const AlgorithmImplementation* impl = NULL; |
| 86 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 86 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 87 if (status.IsError()) | 87 if (status.IsError()) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 std::vector<uint8_t>* buffer) { | 143 std::vector<uint8_t>* buffer) { |
| 144 if (!key.extractable()) | 144 if (!key.extractable()) |
| 145 return Status::ErrorKeyNotExtractable(); | 145 return Status::ErrorKeyNotExtractable(); |
| 146 return ExportKeyDontCheckExtractability(format, key, buffer); | 146 return ExportKeyDontCheckExtractability(format, key, buffer); |
| 147 } | 147 } |
| 148 | 148 |
| 149 Status Sign(const blink::WebCryptoAlgorithm& algorithm, | 149 Status Sign(const blink::WebCryptoAlgorithm& algorithm, |
| 150 const blink::WebCryptoKey& key, | 150 const blink::WebCryptoKey& key, |
| 151 const CryptoData& data, | 151 const CryptoData& data, |
| 152 std::vector<uint8_t>* buffer) { | 152 std::vector<uint8_t>* buffer) { |
| 153 if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageSign)) | 153 if (!key.keyUsageAllows(blink::WebCryptoKeyUsageSign)) |
| 154 return Status::ErrorUnexpected(); | 154 return Status::ErrorUnexpected(); |
| 155 if (algorithm.id() != key.algorithm().id()) | 155 if (algorithm.id() != key.algorithm().id()) |
| 156 return Status::ErrorUnexpected(); | 156 return Status::ErrorUnexpected(); |
| 157 | 157 |
| 158 const AlgorithmImplementation* impl = NULL; | 158 const AlgorithmImplementation* impl = NULL; |
| 159 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 159 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 160 if (status.IsError()) | 160 if (status.IsError()) |
| 161 return status; | 161 return status; |
| 162 | 162 |
| 163 return impl->Sign(algorithm, key, data, buffer); | 163 return impl->Sign(algorithm, key, data, buffer); |
| 164 } | 164 } |
| 165 | 165 |
| 166 Status Verify(const blink::WebCryptoAlgorithm& algorithm, | 166 Status Verify(const blink::WebCryptoAlgorithm& algorithm, |
| 167 const blink::WebCryptoKey& key, | 167 const blink::WebCryptoKey& key, |
| 168 const CryptoData& signature, | 168 const CryptoData& signature, |
| 169 const CryptoData& data, | 169 const CryptoData& data, |
| 170 bool* signature_match) { | 170 bool* signature_match) { |
| 171 if (!KeyUsageAllows(key, blink::WebCryptoKeyUsageVerify)) | 171 if (!key.keyUsageAllows(blink::WebCryptoKeyUsageVerify)) |
| 172 return Status::ErrorUnexpected(); | 172 return Status::ErrorUnexpected(); |
| 173 if (algorithm.id() != key.algorithm().id()) | 173 if (algorithm.id() != key.algorithm().id()) |
| 174 return Status::ErrorUnexpected(); | 174 return Status::ErrorUnexpected(); |
| 175 | 175 |
| 176 const AlgorithmImplementation* impl = NULL; | 176 const AlgorithmImplementation* impl = NULL; |
| 177 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 177 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 178 if (status.IsError()) | 178 if (status.IsError()) |
| 179 return status; | 179 return status; |
| 180 | 180 |
| 181 return impl->Verify(algorithm, key, signature, data, signature_match); | 181 return impl->Verify(algorithm, key, signature, data, signature_match); |
| 182 } | 182 } |
| 183 | 183 |
| 184 Status WrapKey(blink::WebCryptoKeyFormat format, | 184 Status WrapKey(blink::WebCryptoKeyFormat format, |
| 185 const blink::WebCryptoKey& key_to_wrap, | 185 const blink::WebCryptoKey& key_to_wrap, |
| 186 const blink::WebCryptoKey& wrapping_key, | 186 const blink::WebCryptoKey& wrapping_key, |
| 187 const blink::WebCryptoAlgorithm& wrapping_algorithm, | 187 const blink::WebCryptoAlgorithm& wrapping_algorithm, |
| 188 std::vector<uint8_t>* buffer) { | 188 std::vector<uint8_t>* buffer) { |
| 189 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageWrapKey)) | 189 if (!wrapping_key.keyUsageAllows(blink::WebCryptoKeyUsageWrapKey)) |
| 190 return Status::ErrorUnexpected(); | 190 return Status::ErrorUnexpected(); |
| 191 | 191 |
| 192 std::vector<uint8_t> exported_data; | 192 std::vector<uint8_t> exported_data; |
| 193 Status status = ExportKey(format, key_to_wrap, &exported_data); | 193 Status status = ExportKey(format, key_to_wrap, &exported_data); |
| 194 if (status.IsError()) | 194 if (status.IsError()) |
| 195 return status; | 195 return status; |
| 196 return EncryptDontCheckUsage(wrapping_algorithm, wrapping_key, | 196 return EncryptDontCheckUsage(wrapping_algorithm, wrapping_key, |
| 197 CryptoData(exported_data), buffer); | 197 CryptoData(exported_data), buffer); |
| 198 } | 198 } |
| 199 | 199 |
| 200 Status UnwrapKey(blink::WebCryptoKeyFormat format, | 200 Status UnwrapKey(blink::WebCryptoKeyFormat format, |
| 201 const CryptoData& wrapped_key_data, | 201 const CryptoData& wrapped_key_data, |
| 202 const blink::WebCryptoKey& wrapping_key, | 202 const blink::WebCryptoKey& wrapping_key, |
| 203 const blink::WebCryptoAlgorithm& wrapping_algorithm, | 203 const blink::WebCryptoAlgorithm& wrapping_algorithm, |
| 204 const blink::WebCryptoAlgorithm& algorithm, | 204 const blink::WebCryptoAlgorithm& algorithm, |
| 205 bool extractable, | 205 bool extractable, |
| 206 blink::WebCryptoKeyUsageMask usages, | 206 blink::WebCryptoKeyUsageMask usages, |
| 207 blink::WebCryptoKey* key) { | 207 blink::WebCryptoKey* key) { |
| 208 if (!KeyUsageAllows(wrapping_key, blink::WebCryptoKeyUsageUnwrapKey)) | 208 if (!wrapping_key.keyUsageAllows(blink::WebCryptoKeyUsageUnwrapKey)) |
| 209 return Status::ErrorUnexpected(); | 209 return Status::ErrorUnexpected(); |
| 210 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) | 210 if (wrapping_algorithm.id() != wrapping_key.algorithm().id()) |
| 211 return Status::ErrorUnexpected(); | 211 return Status::ErrorUnexpected(); |
| 212 | 212 |
| 213 // Fail fast if the import is doomed to fail. | 213 // Fail fast if the import is doomed to fail. |
| 214 const AlgorithmImplementation* import_impl = NULL; | 214 const AlgorithmImplementation* import_impl = NULL; |
| 215 Status status = GetAlgorithmImplementation(algorithm.id(), &import_impl); | 215 Status status = GetAlgorithmImplementation(algorithm.id(), &import_impl); |
| 216 if (status.IsError()) | 216 if (status.IsError()) |
| 217 return status; | 217 return status; |
| 218 | 218 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 232 // key bytes however this should be OK. For more discussion see | 232 // key bytes however this should be OK. For more discussion see |
| 233 // http://crubg.com/372040 | 233 // http://crubg.com/372040 |
| 234 return ImportKey(format, CryptoData(buffer), algorithm, extractable, usages, | 234 return ImportKey(format, CryptoData(buffer), algorithm, extractable, usages, |
| 235 key); | 235 key); |
| 236 } | 236 } |
| 237 | 237 |
| 238 Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, | 238 Status DeriveBits(const blink::WebCryptoAlgorithm& algorithm, |
| 239 const blink::WebCryptoKey& base_key, | 239 const blink::WebCryptoKey& base_key, |
| 240 unsigned int length_bits, | 240 unsigned int length_bits, |
| 241 std::vector<uint8_t>* derived_bytes) { | 241 std::vector<uint8_t>* derived_bytes) { |
| 242 if (!KeyUsageAllows(base_key, blink::WebCryptoKeyUsageDeriveBits)) | 242 if (!base_key.keyUsageAllows(blink::WebCryptoKeyUsageDeriveBits)) |
| 243 return Status::ErrorUnexpected(); | 243 return Status::ErrorUnexpected(); |
| 244 | 244 |
| 245 if (algorithm.id() != base_key.algorithm().id()) | 245 if (algorithm.id() != base_key.algorithm().id()) |
| 246 return Status::ErrorUnexpected(); | 246 return Status::ErrorUnexpected(); |
| 247 | 247 |
| 248 const AlgorithmImplementation* impl = NULL; | 248 const AlgorithmImplementation* impl = NULL; |
| 249 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 249 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 250 if (status.IsError()) | 250 if (status.IsError()) |
| 251 return status; | 251 return status; |
| 252 | 252 |
| 253 return impl->DeriveBits(algorithm, base_key, true, length_bits, | 253 return impl->DeriveBits(algorithm, base_key, true, length_bits, |
| 254 derived_bytes); | 254 derived_bytes); |
| 255 } | 255 } |
| 256 | 256 |
| 257 Status DeriveKey(const blink::WebCryptoAlgorithm& algorithm, | 257 Status DeriveKey(const blink::WebCryptoAlgorithm& algorithm, |
| 258 const blink::WebCryptoKey& base_key, | 258 const blink::WebCryptoKey& base_key, |
| 259 const blink::WebCryptoAlgorithm& import_algorithm, | 259 const blink::WebCryptoAlgorithm& import_algorithm, |
| 260 const blink::WebCryptoAlgorithm& key_length_algorithm, | 260 const blink::WebCryptoAlgorithm& key_length_algorithm, |
| 261 bool extractable, | 261 bool extractable, |
| 262 blink::WebCryptoKeyUsageMask usages, | 262 blink::WebCryptoKeyUsageMask usages, |
| 263 blink::WebCryptoKey* derived_key) { | 263 blink::WebCryptoKey* derived_key) { |
| 264 if (!KeyUsageAllows(base_key, blink::WebCryptoKeyUsageDeriveKey)) | 264 if (!base_key.keyUsageAllows(blink::WebCryptoKeyUsageDeriveKey)) |
| 265 return Status::ErrorUnexpected(); | 265 return Status::ErrorUnexpected(); |
| 266 | 266 |
| 267 if (algorithm.id() != base_key.algorithm().id()) | 267 if (algorithm.id() != base_key.algorithm().id()) |
| 268 return Status::ErrorUnexpected(); | 268 return Status::ErrorUnexpected(); |
| 269 | 269 |
| 270 if (import_algorithm.id() != key_length_algorithm.id()) | 270 if (import_algorithm.id() != key_length_algorithm.id()) |
| 271 return Status::ErrorUnexpected(); | 271 return Status::ErrorUnexpected(); |
| 272 | 272 |
| 273 const AlgorithmImplementation* import_impl = NULL; | 273 const AlgorithmImplementation* import_impl = NULL; |
| 274 Status status = | 274 Status status = |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); | 334 Status status = GetAlgorithmImplementation(algorithm.id(), &impl); |
| 335 if (status.IsError()) | 335 if (status.IsError()) |
| 336 return false; | 336 return false; |
| 337 | 337 |
| 338 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages, | 338 status = impl->DeserializeKeyForClone(algorithm, type, extractable, usages, |
| 339 key_data, key); | 339 key_data, key); |
| 340 return status.IsSuccess(); | 340 return status.IsSuccess(); |
| 341 } | 341 } |
| 342 | 342 |
| 343 } // namespace webcrypto | 343 } // namespace webcrypto |
| OLD | NEW |