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 <openssl/hmac.h> | 5 #include <openssl/hmac.h> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/numerics/safe_math.h" | 8 #include "base/numerics/safe_math.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/child/webcrypto/algorithm_implementation.h" | 10 #include "content/child/webcrypto/algorithm_implementation.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 default: | 98 default: |
99 return Status::ErrorUnsupportedImportKeyFormat(); | 99 return Status::ErrorUnsupportedImportKeyFormat(); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 Status ImportKeyRaw(const CryptoData& key_data, | 103 Status ImportKeyRaw(const CryptoData& key_data, |
104 const blink::WebCryptoAlgorithm& algorithm, | 104 const blink::WebCryptoAlgorithm& algorithm, |
105 bool extractable, | 105 bool extractable, |
106 blink::WebCryptoKeyUsageMask usages, | 106 blink::WebCryptoKeyUsageMask usages, |
107 blink::WebCryptoKey* key) const override { | 107 blink::WebCryptoKey* key) const override { |
| 108 if (usages == 0) |
| 109 return ErrorImportKeyEmptyUsages(); |
| 110 |
108 const blink::WebCryptoAlgorithm& hash = | 111 const blink::WebCryptoAlgorithm& hash = |
109 algorithm.hmacImportParams()->hash(); | 112 algorithm.hmacImportParams()->hash(); |
110 | 113 |
111 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); | 114 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
112 keylen_bits *= 8; | 115 keylen_bits *= 8; |
113 | 116 |
114 if (!keylen_bits.IsValid()) | 117 if (!keylen_bits.IsValid()) |
115 return Status::ErrorDataTooLarge(); | 118 return Status::ErrorDataTooLarge(); |
116 | 119 |
117 return ImportKeyRawOpenSsl(key_data, | 120 return ImportKeyRawOpenSsl(key_data, |
118 blink::WebCryptoKeyAlgorithm::createHmac( | 121 blink::WebCryptoKeyAlgorithm::createHmac( |
119 hash.id(), keylen_bits.ValueOrDie()), | 122 hash.id(), keylen_bits.ValueOrDie()), |
120 extractable, usages, key); | 123 extractable, usages, key); |
121 } | 124 } |
122 | 125 |
123 Status ImportKeyJwk(const CryptoData& key_data, | 126 Status ImportKeyJwk(const CryptoData& key_data, |
124 const blink::WebCryptoAlgorithm& algorithm, | 127 const blink::WebCryptoAlgorithm& algorithm, |
125 bool extractable, | 128 bool extractable, |
126 blink::WebCryptoKeyUsageMask usages, | 129 blink::WebCryptoKeyUsageMask usages, |
127 blink::WebCryptoKey* key) const override { | 130 blink::WebCryptoKey* key) const override { |
| 131 if (usages == 0) |
| 132 return ErrorImportKeyEmptyUsages(); |
| 133 |
128 const char* algorithm_name = | 134 const char* algorithm_name = |
129 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 135 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
130 if (!algorithm_name) | 136 if (!algorithm_name) |
131 return Status::ErrorUnexpected(); | 137 return Status::ErrorUnexpected(); |
132 | 138 |
133 std::vector<uint8_t> raw_data; | 139 std::vector<uint8_t> raw_data; |
134 Status status = ReadSecretKeyJwk(key_data, algorithm_name, extractable, | 140 Status status = ReadSecretKeyJwk(key_data, algorithm_name, extractable, |
135 usages, &raw_data); | 141 usages, &raw_data); |
136 if (status.IsError()) | 142 if (status.IsError()) |
137 return status; | 143 return status; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 220 |
215 } // namespace | 221 } // namespace |
216 | 222 |
217 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 223 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
218 return new HmacImplementation; | 224 return new HmacImplementation; |
219 } | 225 } |
220 | 226 |
221 } // namespace webcrypto | 227 } // namespace webcrypto |
222 | 228 |
223 } // namespace content | 229 } // namespace content |
OLD | NEW |