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/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "content/child/webcrypto/algorithm_implementation.h" | 10 #include "content/child/webcrypto/algorithm_implementation.h" |
10 #include "content/child/webcrypto/crypto_data.h" | 11 #include "content/child/webcrypto/crypto_data.h" |
11 #include "content/child/webcrypto/jwk.h" | 12 #include "content/child/webcrypto/jwk.h" |
12 #include "content/child/webcrypto/openssl/key_openssl.h" | 13 #include "content/child/webcrypto/openssl/key_openssl.h" |
13 #include "content/child/webcrypto/openssl/sym_key_openssl.h" | 14 #include "content/child/webcrypto/openssl/sym_key_openssl.h" |
14 #include "content/child/webcrypto/openssl/util_openssl.h" | 15 #include "content/child/webcrypto/openssl/util_openssl.h" |
15 #include "content/child/webcrypto/status.h" | 16 #include "content/child/webcrypto/status.h" |
16 #include "content/child/webcrypto/webcrypto_util.h" | 17 #include "content/child/webcrypto/webcrypto_util.h" |
17 #include "crypto/openssl_util.h" | 18 #include "crypto/openssl_util.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 } | 108 } |
108 | 109 |
109 virtual Status ImportKeyRaw(const CryptoData& key_data, | 110 virtual Status ImportKeyRaw(const CryptoData& key_data, |
110 const blink::WebCryptoAlgorithm& algorithm, | 111 const blink::WebCryptoAlgorithm& algorithm, |
111 bool extractable, | 112 bool extractable, |
112 blink::WebCryptoKeyUsageMask usage_mask, | 113 blink::WebCryptoKeyUsageMask usage_mask, |
113 blink::WebCryptoKey* key) const OVERRIDE { | 114 blink::WebCryptoKey* key) const OVERRIDE { |
114 const blink::WebCryptoAlgorithm& hash = | 115 const blink::WebCryptoAlgorithm& hash = |
115 algorithm.hmacImportParams()->hash(); | 116 algorithm.hmacImportParams()->hash(); |
116 | 117 |
117 // TODO(eroman): check for overflow. | 118 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); |
118 unsigned int keylen_bits = key_data.byte_length() * 8; | 119 keylen_bits *= 8; |
119 | 120 |
120 return ImportKeyRawOpenSsl( | 121 if (!keylen_bits.IsValid()) |
121 key_data, | 122 return Status::ErrorDataTooLarge(); |
122 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), | 123 |
123 extractable, | 124 return ImportKeyRawOpenSsl(key_data, |
124 usage_mask, | 125 blink::WebCryptoKeyAlgorithm::createHmac( |
125 key); | 126 hash.id(), keylen_bits.ValueOrDie()), |
| 127 extractable, |
| 128 usage_mask, |
| 129 key); |
126 } | 130 } |
127 | 131 |
128 virtual Status ImportKeyJwk(const CryptoData& key_data, | 132 virtual Status ImportKeyJwk(const CryptoData& key_data, |
129 const blink::WebCryptoAlgorithm& algorithm, | 133 const blink::WebCryptoAlgorithm& algorithm, |
130 bool extractable, | 134 bool extractable, |
131 blink::WebCryptoKeyUsageMask usage_mask, | 135 blink::WebCryptoKeyUsageMask usage_mask, |
132 blink::WebCryptoKey* key) const OVERRIDE { | 136 blink::WebCryptoKey* key) const OVERRIDE { |
133 const char* algorithm_name = | 137 const char* algorithm_name = |
134 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 138 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
135 if (!algorithm_name) | 139 if (!algorithm_name) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 | 208 |
205 } // namespace | 209 } // namespace |
206 | 210 |
207 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 211 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
208 return new HmacImplementation; | 212 return new HmacImplementation; |
209 } | 213 } |
210 | 214 |
211 } // namespace webcrypto | 215 } // namespace webcrypto |
212 | 216 |
213 } // namespace content | 217 } // namespace content |
OLD | NEW |