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