| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/quic/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
| 6 | 6 |
| 7 #include <openssl/bn.h> | 7 #include <openssl/bn.h> |
| 8 #include <openssl/ec.h> | 8 #include <openssl/ec.h> |
| 9 #include <openssl/ecdsa.h> | 9 #include <openssl/ecdsa.h> |
| 10 #include <openssl/evp.h> | 10 #include <openssl/evp.h> |
| 11 #include <openssl/obj_mac.h> | 11 #include <openssl/obj_mac.h> |
| 12 #include <openssl/sha.h> | 12 #include <openssl/sha.h> |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 | 15 |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
| 18 #include "crypto/scoped_openssl_types.h" | |
| 19 #include "crypto/secure_hash.h" | 18 #include "crypto/secure_hash.h" |
| 20 #include "net/quic/core/crypto/channel_id.h" | 19 #include "net/quic/core/crypto/channel_id.h" |
| 21 #include "net/quic/core/crypto/common_cert_set.h" | 20 #include "net/quic/core/crypto/common_cert_set.h" |
| 22 #include "net/quic/core/crypto/crypto_handshake.h" | 21 #include "net/quic/core/crypto/crypto_handshake.h" |
| 23 #include "net/quic/core/crypto/crypto_server_config_protobuf.h" | 22 #include "net/quic/core/crypto/crypto_server_config_protobuf.h" |
| 24 #include "net/quic/core/crypto/quic_crypto_server_config.h" | 23 #include "net/quic/core/crypto/quic_crypto_server_config.h" |
| 25 #include "net/quic/core/crypto/quic_decrypter.h" | 24 #include "net/quic/core/crypto/quic_decrypter.h" |
| 26 #include "net/quic/core/crypto/quic_encrypter.h" | 25 #include "net/quic/core/crypto/quic_encrypter.h" |
| 27 #include "net/quic/core/crypto/quic_random.h" | 26 #include "net/quic/core/crypto/quic_random.h" |
| 28 #include "net/quic/core/quic_clock.h" | 27 #include "net/quic/core/quic_clock.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 }; | 128 }; |
| 130 | 129 |
| 131 class TestChannelIDKey : public ChannelIDKey { | 130 class TestChannelIDKey : public ChannelIDKey { |
| 132 public: | 131 public: |
| 133 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} | 132 explicit TestChannelIDKey(EVP_PKEY* ecdsa_key) : ecdsa_key_(ecdsa_key) {} |
| 134 ~TestChannelIDKey() override {} | 133 ~TestChannelIDKey() override {} |
| 135 | 134 |
| 136 // ChannelIDKey implementation. | 135 // ChannelIDKey implementation. |
| 137 | 136 |
| 138 bool Sign(StringPiece signed_data, string* out_signature) const override { | 137 bool Sign(StringPiece signed_data, string* out_signature) const override { |
| 139 crypto::ScopedEVP_MD_CTX md_ctx(EVP_MD_CTX_create()); | 138 bssl::ScopedEVP_MD_CTX md_ctx; |
| 140 if (!md_ctx || | 139 if (EVP_DigestSignInit(md_ctx.get(), nullptr, EVP_sha256(), nullptr, |
| 141 EVP_DigestSignInit(md_ctx.get(), nullptr, EVP_sha256(), nullptr, | |
| 142 ecdsa_key_.get()) != 1) { | 140 ecdsa_key_.get()) != 1) { |
| 143 return false; | 141 return false; |
| 144 } | 142 } |
| 145 | 143 |
| 146 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kContextStr, | 144 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kContextStr, |
| 147 strlen(ChannelIDVerifier::kContextStr) + 1); | 145 strlen(ChannelIDVerifier::kContextStr) + 1); |
| 148 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kClientToServerStr, | 146 EVP_DigestUpdate(md_ctx.get(), ChannelIDVerifier::kClientToServerStr, |
| 149 strlen(ChannelIDVerifier::kClientToServerStr) + 1); | 147 strlen(ChannelIDVerifier::kClientToServerStr) + 1); |
| 150 EVP_DigestUpdate(md_ctx.get(), signed_data.data(), signed_data.size()); | 148 EVP_DigestUpdate(md_ctx.get(), signed_data.data(), signed_data.size()); |
| 151 | 149 |
| 152 size_t sig_len; | 150 size_t sig_len; |
| 153 if (!EVP_DigestSignFinal(md_ctx.get(), nullptr, &sig_len)) { | 151 if (!EVP_DigestSignFinal(md_ctx.get(), nullptr, &sig_len)) { |
| 154 return false; | 152 return false; |
| 155 } | 153 } |
| 156 | 154 |
| 157 std::unique_ptr<uint8_t[]> der_sig(new uint8_t[sig_len]); | 155 std::unique_ptr<uint8_t[]> der_sig(new uint8_t[sig_len]); |
| 158 if (!EVP_DigestSignFinal(md_ctx.get(), der_sig.get(), &sig_len)) { | 156 if (!EVP_DigestSignFinal(md_ctx.get(), der_sig.get(), &sig_len)) { |
| 159 return false; | 157 return false; |
| 160 } | 158 } |
| 161 | 159 |
| 162 uint8_t* derp = der_sig.get(); | 160 uint8_t* derp = der_sig.get(); |
| 163 crypto::ScopedECDSA_SIG sig( | 161 bssl::UniquePtr<ECDSA_SIG> sig( |
| 164 d2i_ECDSA_SIG(nullptr, const_cast<const uint8_t**>(&derp), sig_len)); | 162 d2i_ECDSA_SIG(nullptr, const_cast<const uint8_t**>(&derp), sig_len)); |
| 165 if (sig.get() == nullptr) { | 163 if (sig.get() == nullptr) { |
| 166 return false; | 164 return false; |
| 167 } | 165 } |
| 168 | 166 |
| 169 // The signature consists of a pair of 32-byte numbers. | 167 // The signature consists of a pair of 32-byte numbers. |
| 170 static const size_t kSignatureLength = 32 * 2; | 168 static const size_t kSignatureLength = 32 * 2; |
| 171 std::unique_ptr<uint8_t[]> signature(new uint8_t[kSignatureLength]); | 169 std::unique_ptr<uint8_t[]> signature(new uint8_t[kSignatureLength]); |
| 172 if (!BN_bn2bin_padded(&signature[0], 32, sig->r) || | 170 if (!BN_bn2bin_padded(&signature[0], 32, sig->r) || |
| 173 !BN_bn2bin_padded(&signature[32], 32, sig->s)) { | 171 !BN_bn2bin_padded(&signature[32], 32, sig->s)) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 192 } | 190 } |
| 193 | 191 |
| 194 uint8_t buf[kExpectedKeyLength]; | 192 uint8_t buf[kExpectedKeyLength]; |
| 195 uint8_t* derp = buf; | 193 uint8_t* derp = buf; |
| 196 i2d_PublicKey(ecdsa_key_.get(), &derp); | 194 i2d_PublicKey(ecdsa_key_.get(), &derp); |
| 197 | 195 |
| 198 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); | 196 return string(reinterpret_cast<char*>(buf + 1), kExpectedKeyLength - 1); |
| 199 } | 197 } |
| 200 | 198 |
| 201 private: | 199 private: |
| 202 crypto::ScopedEVP_PKEY ecdsa_key_; | 200 bssl::UniquePtr<EVP_PKEY> ecdsa_key_; |
| 203 }; | 201 }; |
| 204 | 202 |
| 205 class TestChannelIDSource : public ChannelIDSource { | 203 class TestChannelIDSource : public ChannelIDSource { |
| 206 public: | 204 public: |
| 207 ~TestChannelIDSource() override {} | 205 ~TestChannelIDSource() override {} |
| 208 | 206 |
| 209 // ChannelIDSource implementation. | 207 // ChannelIDSource implementation. |
| 210 | 208 |
| 211 QuicAsyncStatus GetChannelIDKey( | 209 QuicAsyncStatus GetChannelIDKey( |
| 212 const string& hostname, | 210 const string& hostname, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 228 SHA256_Init(&sha256); | 226 SHA256_Init(&sha256); |
| 229 SHA256_Update(&sha256, hostname.data(), hostname.size()); | 227 SHA256_Update(&sha256, hostname.data(), hostname.size()); |
| 230 | 228 |
| 231 unsigned char digest[SHA256_DIGEST_LENGTH]; | 229 unsigned char digest[SHA256_DIGEST_LENGTH]; |
| 232 SHA256_Final(digest, &sha256); | 230 SHA256_Final(digest, &sha256); |
| 233 | 231 |
| 234 // Ensure that the digest is less than the order of the P-256 group by | 232 // Ensure that the digest is less than the order of the P-256 group by |
| 235 // clearing the most-significant bit. | 233 // clearing the most-significant bit. |
| 236 digest[0] &= 0x7f; | 234 digest[0] &= 0x7f; |
| 237 | 235 |
| 238 crypto::ScopedBIGNUM k(BN_new()); | 236 bssl::UniquePtr<BIGNUM> k(BN_new()); |
| 239 CHECK(BN_bin2bn(digest, sizeof(digest), k.get()) != nullptr); | 237 CHECK(BN_bin2bn(digest, sizeof(digest), k.get()) != nullptr); |
| 240 | 238 |
| 241 crypto::ScopedEC_GROUP p256( | 239 bssl::UniquePtr<EC_GROUP> p256( |
| 242 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); | 240 EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); |
| 243 CHECK(p256); | 241 CHECK(p256); |
| 244 | 242 |
| 245 crypto::ScopedEC_KEY ecdsa_key(EC_KEY_new()); | 243 bssl::UniquePtr<EC_KEY> ecdsa_key(EC_KEY_new()); |
| 246 CHECK(ecdsa_key && EC_KEY_set_group(ecdsa_key.get(), p256.get())); | 244 CHECK(ecdsa_key && EC_KEY_set_group(ecdsa_key.get(), p256.get())); |
| 247 | 245 |
| 248 crypto::ScopedEC_POINT point(EC_POINT_new(p256.get())); | 246 bssl::UniquePtr<EC_POINT> point(EC_POINT_new(p256.get())); |
| 249 CHECK(EC_POINT_mul(p256.get(), point.get(), k.get(), nullptr, nullptr, | 247 CHECK(EC_POINT_mul(p256.get(), point.get(), k.get(), nullptr, nullptr, |
| 250 nullptr)); | 248 nullptr)); |
| 251 | 249 |
| 252 EC_KEY_set_private_key(ecdsa_key.get(), k.get()); | 250 EC_KEY_set_private_key(ecdsa_key.get(), k.get()); |
| 253 EC_KEY_set_public_key(ecdsa_key.get(), point.get()); | 251 EC_KEY_set_public_key(ecdsa_key.get(), point.get()); |
| 254 | 252 |
| 255 crypto::ScopedEVP_PKEY pkey(EVP_PKEY_new()); | 253 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new()); |
| 256 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here. | 254 // EVP_PKEY_set1_EC_KEY takes a reference so no |release| here. |
| 257 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get()); | 255 EVP_PKEY_set1_EC_KEY(pkey.get(), ecdsa_key.get()); |
| 258 | 256 |
| 259 return pkey.release(); | 257 return pkey.release(); |
| 260 } | 258 } |
| 261 }; | 259 }; |
| 262 | 260 |
| 263 } // anonymous namespace | 261 } // anonymous namespace |
| 264 | 262 |
| 265 CryptoTestUtils::FakeServerOptions::FakeServerOptions() {} | 263 CryptoTestUtils::FakeServerOptions::FakeServerOptions() {} |
| (...skipping 748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1014 // Pass a inchoate CHLO. | 1012 // Pass a inchoate CHLO. |
| 1015 FullChloGenerator generator(crypto_config, server_ip, client_addr, clock, | 1013 FullChloGenerator generator(crypto_config, server_ip, client_addr, clock, |
| 1016 proof, compressed_certs_cache, out); | 1014 proof, compressed_certs_cache, out); |
| 1017 crypto_config->ValidateClientHello( | 1015 crypto_config->ValidateClientHello( |
| 1018 inchoate_chlo, client_addr.address(), server_ip, version, clock, proof, | 1016 inchoate_chlo, client_addr.address(), server_ip, version, clock, proof, |
| 1019 generator.GetValidateClientHelloCallback()); | 1017 generator.GetValidateClientHelloCallback()); |
| 1020 } | 1018 } |
| 1021 | 1019 |
| 1022 } // namespace test | 1020 } // namespace test |
| 1023 } // namespace net | 1021 } // namespace net |
| OLD | NEW |