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

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

Issue 777403004: [WebCrypto] Throw syntaxError if keyUsage is empty in ImportKey (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Support for throwing error if usage is empty in HMAC and AES Created 6 years 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
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"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 default: 91 default:
92 return Status::ErrorUnsupportedImportKeyFormat(); 92 return Status::ErrorUnsupportedImportKeyFormat();
93 } 93 }
94 } 94 }
95 95
96 Status ImportKeyRaw(const CryptoData& key_data, 96 Status ImportKeyRaw(const CryptoData& key_data,
97 const blink::WebCryptoAlgorithm& algorithm, 97 const blink::WebCryptoAlgorithm& algorithm,
98 bool extractable, 98 bool extractable,
99 blink::WebCryptoKeyUsageMask usages, 99 blink::WebCryptoKeyUsageMask usages,
100 blink::WebCryptoKey* key) const override { 100 blink::WebCryptoKey* key) const override {
101 if (usages == 0)
102 return Status::ErrorImportKeyEmptyUsages();
103
101 const blink::WebCryptoAlgorithm& hash = 104 const blink::WebCryptoAlgorithm& hash =
102 algorithm.hmacImportParams()->hash(); 105 algorithm.hmacImportParams()->hash();
103 106
104 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; 107 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM;
105 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) 108 if (!WebCryptoHashToHMACMechanism(hash, &mechanism))
106 return Status::ErrorUnsupported(); 109 return Status::ErrorUnsupported();
107 110
108 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length()); 111 base::CheckedNumeric<unsigned int> keylen_bits(key_data.byte_length());
109 keylen_bits *= 8; 112 keylen_bits *= 8;
110 113
111 if (!keylen_bits.IsValid()) 114 if (!keylen_bits.IsValid())
112 return Status::ErrorDataTooLarge(); 115 return Status::ErrorDataTooLarge();
113 116
114 return ImportKeyRawNss(key_data, blink::WebCryptoKeyAlgorithm::createHmac( 117 return ImportKeyRawNss(key_data, blink::WebCryptoKeyAlgorithm::createHmac(
115 hash.id(), keylen_bits.ValueOrDie()), 118 hash.id(), keylen_bits.ValueOrDie()),
116 extractable, usages, mechanism, 119 extractable, usages, mechanism,
117 CKF_SIGN | CKF_VERIFY, key); 120 CKF_SIGN | CKF_VERIFY, key);
118 } 121 }
119 122
120 Status ImportKeyJwk(const CryptoData& key_data, 123 Status ImportKeyJwk(const CryptoData& key_data,
121 const blink::WebCryptoAlgorithm& algorithm, 124 const blink::WebCryptoAlgorithm& algorithm,
122 bool extractable, 125 bool extractable,
123 blink::WebCryptoKeyUsageMask usages, 126 blink::WebCryptoKeyUsageMask usages,
124 blink::WebCryptoKey* key) const override { 127 blink::WebCryptoKey* key) const override {
128 if (usages == 0)
129 return Status::ErrorImportKeyEmptyUsages();
130
125 const char* algorithm_name = 131 const char* algorithm_name =
126 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); 132 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id());
127 if (!algorithm_name) 133 if (!algorithm_name)
128 return Status::ErrorUnexpected(); 134 return Status::ErrorUnexpected();
129 135
130 std::vector<uint8_t> raw_data; 136 std::vector<uint8_t> raw_data;
131 Status status = ReadSecretKeyJwk(key_data, algorithm_name, extractable, 137 Status status = ReadSecretKeyJwk(key_data, algorithm_name, extractable,
132 usages, &raw_data); 138 usages, &raw_data);
133 if (status.IsError()) 139 if (status.IsError())
134 return status; 140 return status;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 242
237 } // namespace 243 } // namespace
238 244
239 AlgorithmImplementation* CreatePlatformHmacImplementation() { 245 AlgorithmImplementation* CreatePlatformHmacImplementation() {
240 return new HmacImplementation; 246 return new HmacImplementation;
241 } 247 }
242 248
243 } // namespace webcrypto 249 } // namespace webcrypto
244 250
245 } // namespace content 251 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698