Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(724)

Side by Side Diff: content/child/webcrypto/nss/hmac_nss.cc

Issue 508793002: Check for integer overflow when HMAC key length as bits cannot fit in an unsigned int. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | content/child/webcrypto/openssl/hmac_openssl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <cryptohi.h> 5 #include <cryptohi.h>
6 #include <pk11pub.h> 6 #include <pk11pub.h>
7 #include <secerr.h> 7 #include <secerr.h>
8 #include <sechash.h> 8 #include <sechash.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/numerics/safe_math.h"
11 #include "base/stl_util.h" 12 #include "base/stl_util.h"
12 #include "content/child/webcrypto/algorithm_implementation.h" 13 #include "content/child/webcrypto/algorithm_implementation.h"
13 #include "content/child/webcrypto/crypto_data.h" 14 #include "content/child/webcrypto/crypto_data.h"
14 #include "content/child/webcrypto/jwk.h" 15 #include "content/child/webcrypto/jwk.h"
15 #include "content/child/webcrypto/nss/key_nss.h" 16 #include "content/child/webcrypto/nss/key_nss.h"
16 #include "content/child/webcrypto/nss/sym_key_nss.h" 17 #include "content/child/webcrypto/nss/sym_key_nss.h"
17 #include "content/child/webcrypto/nss/util_nss.h" 18 #include "content/child/webcrypto/nss/util_nss.h"
18 #include "content/child/webcrypto/status.h" 19 #include "content/child/webcrypto/status.h"
19 #include "content/child/webcrypto/webcrypto_util.h" 20 #include "content/child/webcrypto/webcrypto_util.h"
20 #include "crypto/secure_util.h" 21 #include "crypto/secure_util.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 bool extractable, 103 bool extractable,
103 blink::WebCryptoKeyUsageMask usage_mask, 104 blink::WebCryptoKeyUsageMask usage_mask,
104 blink::WebCryptoKey* key) const OVERRIDE { 105 blink::WebCryptoKey* key) const OVERRIDE {
105 const blink::WebCryptoAlgorithm& hash = 106 const blink::WebCryptoAlgorithm& hash =
106 algorithm.hmacImportParams()->hash(); 107 algorithm.hmacImportParams()->hash();
107 108
108 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; 109 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM;
109 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) 110 if (!WebCryptoHashToHMACMechanism(hash, &mechanism))
110 return Status::ErrorUnsupported(); 111 return Status::ErrorUnsupported();
111 112
112 // TODO(eroman): check for overflow. 113 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length());
113 unsigned int keylen_bits = key_data.byte_length() * 8; 114 keylen_bits *= 8;
114 return ImportKeyRawNss( 115
115 key_data, 116 if (!keylen_bits.IsValid())
116 blink::WebCryptoKeyAlgorithm::createHmac(hash.id(), keylen_bits), 117 return Status::ErrorDataTooLarge();
117 extractable, 118
118 usage_mask, 119 return ImportKeyRawNss(key_data,
119 mechanism, 120 blink::WebCryptoKeyAlgorithm::createHmac(
120 CKF_SIGN | CKF_VERIFY, 121 hash.id(), keylen_bits.ValueOrDie()),
121 key); 122 extractable,
123 usage_mask,
124 mechanism,
125 CKF_SIGN | CKF_VERIFY,
126 key);
122 } 127 }
123 128
124 virtual Status ImportKeyJwk(const CryptoData& key_data, 129 virtual Status ImportKeyJwk(const CryptoData& key_data,
125 const blink::WebCryptoAlgorithm& algorithm, 130 const blink::WebCryptoAlgorithm& algorithm,
126 bool extractable, 131 bool extractable,
127 blink::WebCryptoKeyUsageMask usage_mask, 132 blink::WebCryptoKeyUsageMask usage_mask,
128 blink::WebCryptoKey* key) const OVERRIDE { 133 blink::WebCryptoKey* key) const OVERRIDE {
129 const char* algorithm_name = 134 const char* algorithm_name =
130 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); 135 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id());
131 if (!algorithm_name) 136 if (!algorithm_name)
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 232
228 } // namespace 233 } // namespace
229 234
230 AlgorithmImplementation* CreatePlatformHmacImplementation() { 235 AlgorithmImplementation* CreatePlatformHmacImplementation() {
231 return new HmacImplementation; 236 return new HmacImplementation;
232 } 237 }
233 238
234 } // namespace webcrypto 239 } // namespace webcrypto
235 240
236 } // namespace content 241 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/child/webcrypto/openssl/hmac_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698