| 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 <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 "content/child/webcrypto/algorithm_implementation.h" |
| 11 #include "content/child/webcrypto/crypto_data.h" | 12 #include "content/child/webcrypto/crypto_data.h" |
| 12 #include "content/child/webcrypto/jwk.h" | 13 #include "content/child/webcrypto/jwk.h" |
| 13 #include "content/child/webcrypto/nss/key_nss.h" | 14 #include "content/child/webcrypto/nss/key_nss.h" |
| 14 #include "content/child/webcrypto/nss/sym_key_nss.h" | 15 #include "content/child/webcrypto/nss/sym_key_nss.h" |
| 15 #include "content/child/webcrypto/nss/util_nss.h" | 16 #include "content/child/webcrypto/nss/util_nss.h" |
| 16 #include "content/child/webcrypto/status.h" | 17 #include "content/child/webcrypto/status.h" |
| 17 #include "content/child/webcrypto/webcrypto_util.h" | 18 #include "content/child/webcrypto/webcrypto_util.h" |
| 18 #include "crypto/secure_util.h" | 19 #include "crypto/secure_util.h" |
| 19 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" | 20 #include "third_party/WebKit/public/platform/WebCryptoAlgorithmParams.h" |
| 20 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" | 21 #include "third_party/WebKit/public/platform/WebCryptoKeyAlgorithm.h" |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 virtual Status ImportKeyJwk(const CryptoData& key_data, | 123 virtual Status ImportKeyJwk(const CryptoData& key_data, |
| 123 const blink::WebCryptoAlgorithm& algorithm, | 124 const blink::WebCryptoAlgorithm& algorithm, |
| 124 bool extractable, | 125 bool extractable, |
| 125 blink::WebCryptoKeyUsageMask usage_mask, | 126 blink::WebCryptoKeyUsageMask usage_mask, |
| 126 blink::WebCryptoKey* key) const OVERRIDE { | 127 blink::WebCryptoKey* key) const OVERRIDE { |
| 127 const char* algorithm_name = | 128 const char* algorithm_name = |
| 128 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); | 129 GetJwkHmacAlgorithmName(algorithm.hmacImportParams()->hash().id()); |
| 129 if (!algorithm_name) | 130 if (!algorithm_name) |
| 130 return Status::ErrorUnexpected(); | 131 return Status::ErrorUnexpected(); |
| 131 | 132 |
| 132 std::vector<uint8> raw_data; | 133 std::vector<uint8_t> raw_data; |
| 133 Status status = ReadSecretKeyJwk( | 134 Status status = ReadSecretKeyJwk( |
| 134 key_data, algorithm_name, extractable, usage_mask, &raw_data); | 135 key_data, algorithm_name, extractable, usage_mask, &raw_data); |
| 135 if (status.IsError()) | 136 if (status.IsError()) |
| 136 return status; | 137 return status; |
| 137 | 138 |
| 138 return ImportKeyRaw( | 139 return ImportKeyRaw( |
| 139 CryptoData(raw_data), algorithm, extractable, usage_mask, key); | 140 CryptoData(raw_data), algorithm, extractable, usage_mask, key); |
| 140 } | 141 } |
| 141 | 142 |
| 142 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, | 143 virtual Status ExportKeyRaw(const blink::WebCryptoKey& key, |
| 143 std::vector<uint8>* buffer) const OVERRIDE { | 144 std::vector<uint8_t>* buffer) const OVERRIDE { |
| 144 *buffer = SymKeyNss::Cast(key)->raw_key_data(); | 145 *buffer = SymKeyNss::Cast(key)->raw_key_data(); |
| 145 return Status::Success(); | 146 return Status::Success(); |
| 146 } | 147 } |
| 147 | 148 |
| 148 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, | 149 virtual Status ExportKeyJwk(const blink::WebCryptoKey& key, |
| 149 std::vector<uint8>* buffer) const OVERRIDE { | 150 std::vector<uint8_t>* buffer) const OVERRIDE { |
| 150 SymKeyNss* sym_key = SymKeyNss::Cast(key); | 151 SymKeyNss* sym_key = SymKeyNss::Cast(key); |
| 151 const std::vector<uint8>& raw_data = sym_key->raw_key_data(); | 152 const std::vector<uint8_t>& raw_data = sym_key->raw_key_data(); |
| 152 | 153 |
| 153 const char* algorithm_name = | 154 const char* algorithm_name = |
| 154 GetJwkHmacAlgorithmName(key.algorithm().hmacParams()->hash().id()); | 155 GetJwkHmacAlgorithmName(key.algorithm().hmacParams()->hash().id()); |
| 155 if (!algorithm_name) | 156 if (!algorithm_name) |
| 156 return Status::ErrorUnexpected(); | 157 return Status::ErrorUnexpected(); |
| 157 | 158 |
| 158 WriteSecretKeyJwk(CryptoData(raw_data), | 159 WriteSecretKeyJwk(CryptoData(raw_data), |
| 159 algorithm_name, | 160 algorithm_name, |
| 160 key.extractable(), | 161 key.extractable(), |
| 161 key.usages(), | 162 key.usages(), |
| 162 buffer); | 163 buffer); |
| 163 | 164 |
| 164 return Status::Success(); | 165 return Status::Success(); |
| 165 } | 166 } |
| 166 | 167 |
| 167 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, | 168 virtual Status Sign(const blink::WebCryptoAlgorithm& algorithm, |
| 168 const blink::WebCryptoKey& key, | 169 const blink::WebCryptoKey& key, |
| 169 const CryptoData& data, | 170 const CryptoData& data, |
| 170 std::vector<uint8>* buffer) const OVERRIDE { | 171 std::vector<uint8_t>* buffer) const OVERRIDE { |
| 171 const blink::WebCryptoAlgorithm& hash = | 172 const blink::WebCryptoAlgorithm& hash = |
| 172 key.algorithm().hmacParams()->hash(); | 173 key.algorithm().hmacParams()->hash(); |
| 173 PK11SymKey* sym_key = SymKeyNss::Cast(key)->key(); | 174 PK11SymKey* sym_key = SymKeyNss::Cast(key)->key(); |
| 174 | 175 |
| 175 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; | 176 CK_MECHANISM_TYPE mechanism = CKM_INVALID_MECHANISM; |
| 176 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) | 177 if (!WebCryptoHashToHMACMechanism(hash, &mechanism)) |
| 177 return Status::ErrorUnexpected(); | 178 return Status::ErrorUnexpected(); |
| 178 | 179 |
| 179 SECItem param_item = {siBuffer, NULL, 0}; | 180 SECItem param_item = {siBuffer, NULL, 0}; |
| 180 SECItem data_item = MakeSECItemForBuffer(data); | 181 SECItem data_item = MakeSECItemForBuffer(data); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 200 | 201 |
| 201 CHECK_EQ(buffer->size(), signature_item.len); | 202 CHECK_EQ(buffer->size(), signature_item.len); |
| 202 return Status::Success(); | 203 return Status::Success(); |
| 203 } | 204 } |
| 204 | 205 |
| 205 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, | 206 virtual Status Verify(const blink::WebCryptoAlgorithm& algorithm, |
| 206 const blink::WebCryptoKey& key, | 207 const blink::WebCryptoKey& key, |
| 207 const CryptoData& signature, | 208 const CryptoData& signature, |
| 208 const CryptoData& data, | 209 const CryptoData& data, |
| 209 bool* signature_match) const OVERRIDE { | 210 bool* signature_match) const OVERRIDE { |
| 210 std::vector<uint8> result; | 211 std::vector<uint8_t> result; |
| 211 Status status = Sign(algorithm, key, data, &result); | 212 Status status = Sign(algorithm, key, data, &result); |
| 212 | 213 |
| 213 if (status.IsError()) | 214 if (status.IsError()) |
| 214 return status; | 215 return status; |
| 215 | 216 |
| 216 // Do not allow verification of truncated MACs. | 217 // Do not allow verification of truncated MACs. |
| 217 *signature_match = result.size() == signature.byte_length() && | 218 *signature_match = result.size() == signature.byte_length() && |
| 218 crypto::SecureMemEqual(Uint8VectorStart(result), | 219 crypto::SecureMemEqual(Uint8VectorStart(result), |
| 219 signature.bytes(), | 220 signature.bytes(), |
| 220 signature.byte_length()); | 221 signature.byte_length()); |
| 221 | 222 |
| 222 return Status::Success(); | 223 return Status::Success(); |
| 223 } | 224 } |
| 224 }; | 225 }; |
| 225 | 226 |
| 226 } // namespace | 227 } // namespace |
| 227 | 228 |
| 228 AlgorithmImplementation* CreatePlatformHmacImplementation() { | 229 AlgorithmImplementation* CreatePlatformHmacImplementation() { |
| 229 return new HmacImplementation; | 230 return new HmacImplementation; |
| 230 } | 231 } |
| 231 | 232 |
| 232 } // namespace webcrypto | 233 } // namespace webcrypto |
| 233 | 234 |
| 234 } // namespace content | 235 } // namespace content |
| OLD | NEW |