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 "content/child/webcrypto/crypto_data.h" | 8 #include "content/child/webcrypto/crypto_data.h" |
9 #include "content/child/webcrypto/jwk.h" | 9 #include "content/child/webcrypto/jwk.h" |
10 #include "content/child/webcrypto/openssl/key_openssl.h" | 10 #include "content/child/webcrypto/openssl/key_openssl.h" |
11 #include "content/child/webcrypto/openssl/sym_key_openssl.h" | 11 #include "content/child/webcrypto/openssl/sym_key_openssl.h" |
12 #include "content/child/webcrypto/openssl/util_openssl.h" | 12 #include "content/child/webcrypto/openssl/util_openssl.h" |
13 #include "content/child/webcrypto/status.h" | 13 #include "content/child/webcrypto/status.h" |
14 #include "content/child/webcrypto/webcrypto_util.h" | 14 #include "content/child/webcrypto/webcrypto_util.h" |
15 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
16 #include "crypto/secure_util.h" | 16 #include "crypto/secure_util.h" |
17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 17 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
18 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 18 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 namespace webcrypto { | 22 namespace webcrypto { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const blink::WebCryptoKeyUsageMask kAllKeyUsages = | 26 const blink::WebCryptoKeyUsageMask kAllKeyUsages = |
27 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify; | 27 blink::WebCryptoKeyUsageSign | blink::WebCryptoKeyUsageVerify; |
28 | 28 |
29 Status SignHmac(const std::vector<uint8>& raw_key, | 29 Status SignHmac(const std::vector<uint8_t>& raw_key, |
30 const blink::WebCryptoAlgorithm& hash, | 30 const blink::WebCryptoAlgorithm& hash, |
31 const CryptoData& data, | 31 const CryptoData& data, |
32 std::vector<uint8>* buffer) { | 32 std::vector<uint8_t>* buffer) { |
33 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); | 33 crypto::OpenSSLErrStackTracer err_tracer(FROM_HERE); |
34 | 34 |
35 const EVP_MD* digest_algorithm = GetDigest(hash.id()); | 35 const EVP_MD* digest_algorithm = GetDigest(hash.id()); |
36 if (!digest_algorithm) | 36 if (!digest_algorithm) |
37 return Status::ErrorUnsupported(); | 37 return Status::ErrorUnsupported(); |
38 unsigned int hmac_expected_length = EVP_MD_size(digest_algorithm); | 38 unsigned int hmac_expected_length = EVP_MD_size(digest_algorithm); |
39 | 39 |
40 // OpenSSL wierdness here. | 40 // OpenSSL wierdness here. |
41 // First, HMAC() needs a void* for the key data, so make one up front as a | 41 // First, HMAC() needs a void* for the key data, so make one up front as a |
42 // cosmetic to avoid a cast. Second, OpenSSL does not like a NULL key, | 42 // cosmetic to avoid a cast. Second, OpenSSL does not like a NULL key, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 virtual Status ImportKeyJwk(const CryptoData& key_data, | 126 virtual Status ImportKeyJwk(const CryptoData& key_data, |
127 const blink::WebCryptoAlgorithm& algorithm, | 127 const blink::WebCryptoAlgorithm& algorithm, |
128 bool extractable, | 128 bool extractable, |
129 blink::WebCryptoKeyUsageMask usage_mask, | 129 blink::WebCryptoKeyUsageMask usage_mask, |
130 blink::WebCryptoKey* key) const OVERRIDE { | 130 blink::WebCryptoKey* key) const OVERRIDE { |
131 const char* algorithm_name = | 131 const char* algorithm_name = |
132 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 132 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
133 if (!algorithm_name) | 133 if (!algorithm_name) |
134 return Status::ErrorUnexpected(); | 134 return Status::ErrorUnexpected(); |
135 | 135 |
136 std::vector<uint8> raw_data; | 136 std::vector<uint8_t> raw_data; |
137 Status status = ReadSecretKeyJwk( | 137 Status status = ReadSecretKeyJwk( |
138 key_data, algorithm_name, extractable, usage_mask, &raw_data); | 138 key_data, algorithm_name, extractable, usage_mask, &raw_data); |
139 if (status.IsError()) | 139 if (status.IsError()) |
140 return status; | 140 return status; |
141 | 141 |
142 return ImportKeyRaw( | 142 return ImportKeyRaw( |
143 CryptoData(raw_data), algorithm, extractable, usage_mask, key); | 143 CryptoData(raw_data), algorithm, extractable, usage_mask, key); |
144 } | 144 } |
145 | 145 |
146 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, | 146 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, |
147 std::vector<uint8>* buffer) const OVERRIDE { | 147 std::vector<uint8_t>* buffer) const OVERRIDE { |
148 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); | 148 *buffer = SymKeyOpenSsl::Cast(key)->raw_key_data(); |
149 return Status::Success(); | 149 return Status::Success(); |
150 } | 150 } |
151 | 151 |
152 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, | 152 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, |
153 std::vector<uint8>* buffer) const OVERRIDE { | 153 std::vector<uint8_t>* buffer) const OVERRIDE { |
154 SymKeyOpenSsl* sym_key = SymKeyOpenSsl::Cast(key); | 154 SymKeyOpenSsl* sym_key = SymKeyOpenSsl::Cast(key); |
155 const std::vector<uint8>& raw_data = sym_key->raw_key_data(); | 155 const std::vector<uint8_t>& raw_data = sym_key->raw_key_data(); |
156 | 156 |
157 const char* algorithm_name = | 157 const char* algorithm_name = |
158 GetJwkHmacAlgorithmName(key.algorithm().hmacParams()->hash().id()); | 158 GetJwkHmacAlgorithmName(key.algorithm().hmacParams()->hash().id()); |
159 if (!algorithm_name) | 159 if (!algorithm_name) |
160 return Status::ErrorUnexpected(); | 160 return Status::ErrorUnexpected(); |
161 | 161 |
162 WriteSecretKeyJwk(CryptoData(raw_data), | 162 WriteSecretKeyJwk(CryptoData(raw_data), |
163 algorithm_name, | 163 algorithm_name, |
164 key.extractable(), | 164 key.extractable(), |
165 key.usages(), | 165 key.usages(), |
166 buffer); | 166 buffer); |
167 | 167 |
168 return Status::Success(); | 168 return Status::Success(); |
169 } | 169 } |
170 | 170 |
171 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, | 171 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, |
172 const blink::WebCryptoKey& key, | 172 const blink::WebCryptoKey& key, |
173 const CryptoData& data, | 173 const CryptoData& data, |
174 std::vector<uint8>* buffer) const OVERRIDE { | 174 std::vector<uint8_t>* buffer) const OVERRIDE { |
175 const blink::WebCryptoAlgorithm& hash = | 175 const blink::WebCryptoAlgorithm& hash = |
176 key.algorithm().hmacParams()->hash(); | 176 key.algorithm().hmacParams()->hash(); |
177 | 177 |
178 return SignHmac( | 178 return SignHmac( |
179 SymKeyOpenSsl::Cast(key)->raw_key_data(), hash, data, buffer); | 179 SymKeyOpenSsl::Cast(key)->raw_key_data(), hash, data, buffer); |
180 } | 180 } |
181 | 181 |
182 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, | 182 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, |
183 const blink::WebCryptoKey& key, | 183 const blink::WebCryptoKey& key, |
184 const CryptoData& signature, | 184 const CryptoData& signature, |
185 const CryptoData& data, | 185 const CryptoData& data, |
186 bool* signature_match) const OVERRIDE { | 186 bool* signature_match) const OVERRIDE { |
187 std::vector<uint8> result; | 187 std::vector<uint8_t> result; |
188 Status status = Sign(algorithm, key, data, &result); | 188 Status status = Sign(algorithm, key, data, &result); |
189 | 189 |
190 if (status.IsError()) | 190 if (status.IsError()) |
191 return status; | 191 return status; |
192 | 192 |
193 // Do not allow verification of truncated MACs. | 193 // Do not allow verification of truncated MACs. |
194 *signature_match = result.size() == signature.byte_length() && | 194 *signature_match = result.size() == signature.byte_length() && |
195 crypto::SecureMemEqual(Uint8VectorStart(result), | 195 crypto::SecureMemEqual(Uint8VectorStart(result), |
196 signature.bytes(), | 196 signature.bytes(), |
197 signature.byte_length()); | 197 signature.byte_length()); |
198 | 198 |
199 return Status::Success(); | 199 return Status::Success(); |
200 } | 200 } |
201 }; | 201 }; |
202 | 202 |
203 } // namespace | 203 } // namespace |
204 | 204 |
205 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 205 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
206 return new HmacImplementation; | 206 return new HmacImplementation; |
207 } | 207 } |
208 | 208 |
209 } // namespace webcrypto | 209 } // namespace webcrypto |
210 | 210 |
211 } // namespace content | 211 } // namespace content |
OLD | NEW |