blob: 5720991912f0bbf28665a7d64bc80a4cb866646e [file] [log] [blame]
[email protected]c244c5a12013-05-07 20:55:041// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
rch16c74d1d2016-04-22 06:14:075#include <memory>
6
[email protected]2662ed562013-07-03 10:27:467#include "base/files/file_path.h"
rtenneti7dac4be2014-12-15 21:10:018#include "net/base/ip_endpoint.h"
[email protected]2662ed562013-07-03 10:27:469#include "net/base/net_errors.h"
10#include "net/base/test_completion_callback.h"
[email protected]a69af0522013-07-12 19:23:4711#include "net/cert/cert_status_flags.h"
12#include "net/cert/cert_verify_result.h"
[email protected]2662ed562013-07-03 10:27:4613#include "net/cert/x509_certificate.h"
[email protected]2662ed562013-07-03 10:27:4614#include "net/test/cert_test_util.h"
rsleevia69c79a2016-06-22 03:28:4315#include "net/test/test_data_directory.h"
Victor Vasiliev6bb59d22019-03-08 21:34:5116#include "net/third_party/quiche/src/quic/core/crypto/proof_source.h"
17#include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h"
18#include "net/third_party/quiche/src/quic/test_tools/crypto_test_utils.h"
[email protected]c244c5a12013-05-07 20:55:0419#include "testing/gtest/include/gtest/gtest.h"
Nick Harperb42e4e7d2017-08-31 00:18:1120#include "third_party/boringssl/src/include/openssl/ssl.h"
[email protected]c244c5a12013-05-07 20:55:0421
22using std::string;
[email protected]c244c5a12013-05-07 20:55:0423
24namespace net {
25namespace test {
[email protected]6a731c92014-03-22 00:43:2426namespace {
[email protected]c244c5a12013-05-07 20:55:0427
Ryan Hamilton8d9ee76e2018-05-29 23:52:5228// TestProofVerifierCallback is a simple callback for a quic::ProofVerifier that
[email protected]72e65992013-07-30 17:16:1429// signals a TestCompletionCallback when called and stores the results from the
Ryan Hamilton8d9ee76e2018-05-29 23:52:5230// quic::ProofVerifier in pointers passed to the constructor.
31class TestProofVerifierCallback : public quic::ProofVerifierCallback {
[email protected]72e65992013-07-30 17:16:1432 public:
33 TestProofVerifierCallback(TestCompletionCallback* comp_callback,
34 bool* ok,
[email protected]b9475b582014-03-20 20:04:3335 string* error_details)
rjshaded5ced072015-12-18 19:26:0236 : comp_callback_(comp_callback), ok_(ok), error_details_(error_details) {}
[email protected]72e65992013-07-30 17:16:1437
dchengb03027d2014-10-21 12:00:2038 void Run(bool ok,
39 const string& error_details,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5240 std::unique_ptr<quic::ProofVerifyDetails>* details) override {
[email protected]72e65992013-07-30 17:16:1441 *ok_ = ok;
42 *error_details_ = error_details;
43
44 comp_callback_->callback().Run(0);
45 }
46
47 private:
48 TestCompletionCallback* const comp_callback_;
49 bool* const ok_;
[email protected]b9475b582014-03-20 20:04:3350 string* const error_details_;
[email protected]72e65992013-07-30 17:16:1451};
52
53// RunVerification runs |verifier->VerifyProof| and asserts that the result
54// matches |expected_ok|.
Ryan Hamilton8d9ee76e2018-05-29 23:52:5255void RunVerification(quic::ProofVerifier* verifier,
[email protected]6a731c92014-03-22 00:43:2456 const string& hostname,
elawrence954bb5472016-04-04 22:03:1157 const uint16_t port,
[email protected]6a731c92014-03-22 00:43:2458 const string& server_config,
Ryan Hamilton8d9ee76e2018-05-29 23:52:5259 quic::QuicTransportVersion quic_version,
Zhongyi Shid1c00fc42019-12-14 06:05:0960 quiche::QuicheStringPiece chlo_hash,
rch872e00e2016-12-02 02:48:1861 const std::vector<string>& certs,
[email protected]6a731c92014-03-22 00:43:2462 const string& proof,
63 bool expected_ok) {
Ryan Hamilton8d9ee76e2018-05-29 23:52:5264 std::unique_ptr<quic::ProofVerifyDetails> details;
[email protected]72e65992013-07-30 17:16:1465 TestCompletionCallback comp_callback;
66 bool ok;
[email protected]b9475b582014-03-20 20:04:3367 string error_details;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5268 std::unique_ptr<quic::ProofVerifyContext> verify_context(
69 quic::test::crypto_test_utils::ProofVerifyContextForTesting());
ckrasic6567aa52016-07-08 09:24:3570 std::unique_ptr<TestProofVerifierCallback> callback(
71 new TestProofVerifierCallback(&comp_callback, &ok, &error_details));
[email protected]72e65992013-07-30 17:16:1472
Ryan Hamilton8d9ee76e2018-05-29 23:52:5273 quic::QuicAsyncStatus status = verifier->VerifyProof(
elawrence954bb5472016-04-04 22:03:1174 hostname, port, server_config, quic_version, chlo_hash, certs, "", proof,
ckrasic6567aa52016-07-08 09:24:3575 verify_context.get(), &error_details, &details, std::move(callback));
[email protected]72e65992013-07-30 17:16:1476
77 switch (status) {
Ryan Hamilton8d9ee76e2018-05-29 23:52:5278 case quic::QUIC_FAILURE:
[email protected]72e65992013-07-30 17:16:1479 ASSERT_FALSE(expected_ok);
80 ASSERT_NE("", error_details);
81 return;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5282 case quic::QUIC_SUCCESS:
[email protected]72e65992013-07-30 17:16:1483 ASSERT_TRUE(expected_ok);
84 ASSERT_EQ("", error_details);
85 return;
Ryan Hamilton8d9ee76e2018-05-29 23:52:5286 case quic::QUIC_PENDING:
[email protected]72e65992013-07-30 17:16:1487 comp_callback.WaitForResult();
88 ASSERT_EQ(expected_ok, ok);
89 break;
90 }
91}
92
Ryan Hamilton8d9ee76e2018-05-29 23:52:5293class TestCallback : public quic::ProofSource::Callback {
ckrasic6567aa52016-07-08 09:24:3594 public:
Ryan Hamilton8d9ee76e2018-05-29 23:52:5295 explicit TestCallback(
96 bool* called,
97 bool* ok,
98 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain>* chain,
99 quic::QuicCryptoProof* proof)
mpw94250b82016-11-19 18:13:30100 : called_(called), ok_(ok), chain_(chain), proof_(proof) {}
ckrasic6567aa52016-07-08 09:24:35101
Ryan Hamilton8d9ee76e2018-05-29 23:52:52102 void Run(
103 bool ok,
104 const quic::QuicReferenceCountedPointer<quic::ProofSource::Chain>& chain,
105 const quic::QuicCryptoProof& proof,
106 std::unique_ptr<quic::ProofSource::Details> /* details */) override {
ckrasic6567aa52016-07-08 09:24:35107 *ok_ = ok;
108 *chain_ = chain;
mpw94250b82016-11-19 18:13:30109 *proof_ = proof;
ckrasic6567aa52016-07-08 09:24:35110 *called_ = true;
111 }
112
113 private:
114 bool* called_;
115 bool* ok_;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52116 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain>* chain_;
117 quic::QuicCryptoProof* proof_;
ckrasic6567aa52016-07-08 09:24:35118};
119
Renjie Tang1085bf52019-11-20 00:10:53120class ProofTest : public ::testing::TestWithParam<quic::ParsedQuicVersion> {};
rch28f6469d2016-03-13 21:13:08121
[email protected]6a731c92014-03-22 00:43:24122} // namespace
123
Renjie Tang1085bf52019-11-20 00:10:53124INSTANTIATE_TEST_SUITE_P(QuicTransportVersion,
125 ProofTest,
126 ::testing::ValuesIn(quic::AllSupportedVersions()),
127 ::testing::PrintToStringParamName());
rch28f6469d2016-03-13 21:13:08128
David Benjaminb28c476f2018-12-15 04:47:15129TEST_P(ProofTest, Verify) {
Ryan Hamilton8d9ee76e2018-05-29 23:52:52130 std::unique_ptr<quic::ProofSource> source(
131 quic::test::crypto_test_utils::ProofSourceForTesting());
132 std::unique_ptr<quic::ProofVerifier> verifier(
133 quic::test::crypto_test_utils::ProofVerifierForTesting());
[email protected]6a731c92014-03-22 00:43:24134
135 const string server_config = "server config bytes";
136 const string hostname = "test.example.com";
elawrence954bb5472016-04-04 22:03:11137 const uint16_t port = 8443;
rch28f6469d2016-03-13 21:13:08138 const string first_chlo_hash = "first chlo hash bytes";
139 const string second_chlo_hash = "first chlo hash bytes";
Renjie Tang1085bf52019-11-20 00:10:53140 const quic::QuicTransportVersion quic_version = GetParam().transport_version;
elawrence954bb5472016-04-04 22:03:11141
fayangf5f4cd512017-02-07 16:57:11142 bool called = false;
143 bool first_called = false;
144 bool ok, first_ok;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52145 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain> chain;
146 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain> first_chain;
mpw94250b82016-11-19 18:13:30147 string error_details;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52148 quic::QuicCryptoProof proof, first_proof;
149 quic::QuicSocketAddress server_addr;
Zhongyi Shi43a8f2242020-05-04 23:34:31150 quic::QuicSocketAddress client_addr;
[email protected]6a731c92014-03-22 00:43:24151
Ryan Hamilton8d9ee76e2018-05-29 23:52:52152 std::unique_ptr<quic::ProofSource::Callback> cb(
fayangf5f4cd512017-02-07 16:57:11153 new TestCallback(&called, &ok, &chain, &proof));
Ryan Hamilton8d9ee76e2018-05-29 23:52:52154 std::unique_ptr<quic::ProofSource::Callback> first_cb(
fayangf5f4cd512017-02-07 16:57:11155 new TestCallback(&first_called, &first_ok, &first_chain, &first_proof));
156
157 // GetProof here expects the async method to invoke the callback
158 // synchronously.
Zhongyi Shi43a8f2242020-05-04 23:34:31159 source->GetProof(server_addr, client_addr, hostname, server_config,
160 quic_version, first_chlo_hash, std::move(first_cb));
161 source->GetProof(server_addr, client_addr, hostname, server_config,
162 quic_version, second_chlo_hash, std::move(cb));
fayangf5f4cd512017-02-07 16:57:11163 ASSERT_TRUE(called);
164 ASSERT_TRUE(first_called);
165 ASSERT_TRUE(ok);
166 ASSERT_TRUE(first_ok);
[email protected]6a731c92014-03-22 00:43:24167
168 // Check that the proof source is caching correctly:
fayanga64c1a92016-02-13 01:55:58169 ASSERT_EQ(first_chain->certs, chain->certs);
mpw94250b82016-11-19 18:13:30170 ASSERT_NE(proof.signature, first_proof.signature);
171 ASSERT_EQ(first_proof.leaf_cert_scts, proof.leaf_cert_scts);
[email protected]6a731c92014-03-22 00:43:24172
elawrence954bb5472016-04-04 22:03:11173 RunVerification(verifier.get(), hostname, port, server_config, quic_version,
mpw94250b82016-11-19 18:13:30174 first_chlo_hash, chain->certs, proof.signature, true);
[email protected]6a731c92014-03-22 00:43:24175
elawrence954bb5472016-04-04 22:03:11176 RunVerification(verifier.get(), "foo.com", port, server_config, quic_version,
mpw94250b82016-11-19 18:13:30177 first_chlo_hash, chain->certs, proof.signature, false);
[email protected]6a731c92014-03-22 00:43:24178
elawrence954bb5472016-04-04 22:03:11179 RunVerification(verifier.get(), server_config.substr(1, string::npos), port,
rch28f6469d2016-03-13 21:13:08180 server_config, quic_version, first_chlo_hash, chain->certs,
mpw94250b82016-11-19 18:13:30181 proof.signature, false);
[email protected]6a731c92014-03-22 00:43:24182
mpw94250b82016-11-19 18:13:30183 const string corrupt_signature = "1" + proof.signature;
elawrence954bb5472016-04-04 22:03:11184 RunVerification(verifier.get(), hostname, port, server_config, quic_version,
rch28f6469d2016-03-13 21:13:08185 first_chlo_hash, chain->certs, corrupt_signature, false);
[email protected]6a731c92014-03-22 00:43:24186
rch872e00e2016-12-02 02:48:18187 std::vector<string> wrong_certs;
fayanga64c1a92016-02-13 01:55:58188 for (size_t i = 1; i < chain->certs.size(); i++) {
189 wrong_certs.push_back(chain->certs[i]);
[email protected]6a731c92014-03-22 00:43:24190 }
elawrence954bb5472016-04-04 22:03:11191
192 RunVerification(verifier.get(), "foo.com", port, server_config, quic_version,
rch28f6469d2016-03-13 21:13:08193 first_chlo_hash, wrong_certs, corrupt_signature, false);
[email protected]6a731c92014-03-22 00:43:24194}
195
Nick Harperb42e4e7d2017-08-31 00:18:11196namespace {
Nick Harperb00c90892017-08-31 04:08:09197
Ryan Hamilton8d9ee76e2018-05-29 23:52:52198class TestingSignatureCallback : public quic::ProofSource::SignatureCallback {
Nick Harperb42e4e7d2017-08-31 00:18:11199 public:
200 TestingSignatureCallback(bool* ok_out, std::string* signature_out)
201 : ok_out_(ok_out), signature_out_(signature_out) {}
202
Fan Yang7abc6bd2020-03-27 17:58:18203 void Run(bool ok,
204 std::string signature,
205 std::unique_ptr<quic::ProofSource::Details> /*details*/) override {
Nick Harperb42e4e7d2017-08-31 00:18:11206 *ok_out_ = ok;
207 *signature_out_ = std::move(signature);
208 }
209
210 private:
211 bool* ok_out_;
212 std::string* signature_out_;
213};
214
215} // namespace
216
217TEST_P(ProofTest, TlsSignature) {
Ryan Hamilton8d9ee76e2018-05-29 23:52:52218 std::unique_ptr<quic::ProofSource> source(
219 quic::test::crypto_test_utils::ProofSourceForTesting());
Nick Harperb42e4e7d2017-08-31 00:18:11220
Ryan Hamilton8d9ee76e2018-05-29 23:52:52221 quic::QuicSocketAddress server_address;
Nick Harperb42e4e7d2017-08-31 00:18:11222 const string hostname = "test.example.com";
223
Zhongyi Shi43a8f2242020-05-04 23:34:31224 quic::QuicSocketAddress client_address;
225
Ryan Hamilton8d9ee76e2018-05-29 23:52:52226 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain> chain =
Zhongyi Shi43a8f2242020-05-04 23:34:31227 source->GetCertChain(server_address, client_address, hostname);
Nick Harperb42e4e7d2017-08-31 00:18:11228 ASSERT_GT(chain->certs.size(), 0ul);
229
230 // Generate a value to be signed similar to the example in TLS 1.3 section
231 // 4.4.3. The value to be signed starts with octed 0x20 repeated 64 times,
232 // followed by the context string, followed by a single 0 byte, followed by
233 // the transcript hash. Since there's no TLS stack here, we're using 32 bytes
234 // of 01 as the transcript hash.
235 string to_be_signed(64, ' ');
236 to_be_signed.append("TLS 1.3, server CertificateVerify");
237 to_be_signed.append(1, '\0');
238 to_be_signed.append(32, 1);
239
240 string sig;
241 bool success;
242 std::unique_ptr<TestingSignatureCallback> callback =
Victor Vasiliev7da08172019-10-14 06:04:25243 std::make_unique<TestingSignatureCallback>(&success, &sig);
Zhongyi Shi43a8f2242020-05-04 23:34:31244 source->ComputeTlsSignature(server_address, client_address, hostname,
245 SSL_SIGN_RSA_PSS_SHA256, to_be_signed,
246 std::move(callback));
Nick Harperb42e4e7d2017-08-31 00:18:11247 EXPECT_TRUE(success);
248
249 // Verify that the signature from ComputeTlsSignature can be verified with the
250 // leaf cert from GetCertChain.
251 const uint8_t* data;
252 const uint8_t* orig_data;
253 orig_data = data = reinterpret_cast<const uint8_t*>(chain->certs[0].data());
254 bssl::UniquePtr<X509> leaf(d2i_X509(nullptr, &data, chain->certs[0].size()));
255 ASSERT_NE(leaf.get(), nullptr);
256 EXPECT_EQ(data - orig_data, static_cast<ptrdiff_t>(chain->certs[0].size()));
257 bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(leaf.get()));
258 bssl::ScopedEVP_MD_CTX md_ctx;
259 EVP_PKEY_CTX* ctx;
260 ASSERT_EQ(EVP_DigestVerifyInit(md_ctx.get(), &ctx, EVP_sha256(), nullptr,
261 pkey.get()),
262 1);
263 ASSERT_EQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING), 1);
264 ASSERT_EQ(EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, -1), 1);
265 ASSERT_EQ(EVP_DigestVerifyUpdate(md_ctx.get(), to_be_signed.data(),
266 to_be_signed.size()),
267 1);
268 EXPECT_EQ(EVP_DigestVerifyFinal(md_ctx.get(),
269 reinterpret_cast<const uint8_t*>(sig.data()),
270 sig.size()),
271 1);
272}
273
rch28f6469d2016-03-13 21:13:08274TEST_P(ProofTest, UseAfterFree) {
Ryan Hamilton8d9ee76e2018-05-29 23:52:52275 std::unique_ptr<quic::ProofSource> source(
276 quic::test::crypto_test_utils::ProofSourceForTesting());
fayanga64c1a92016-02-13 01:55:58277
278 const string server_config = "server config bytes";
279 const string hostname = "test.example.com";
rch28f6469d2016-03-13 21:13:08280 const string chlo_hash = "proof nonce bytes";
fayangf5f4cd512017-02-07 16:57:11281 bool called = false;
282 bool ok;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52283 quic::QuicReferenceCountedPointer<quic::ProofSource::Chain> chain;
mpw94250b82016-11-19 18:13:30284 string error_details;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52285 quic::QuicCryptoProof proof;
286 quic::QuicSocketAddress server_addr;
Zhongyi Shi43a8f2242020-05-04 23:34:31287 quic::QuicSocketAddress client_addr;
Ryan Hamilton8d9ee76e2018-05-29 23:52:52288 std::unique_ptr<quic::ProofSource::Callback> cb(
fayangf5f4cd512017-02-07 16:57:11289 new TestCallback(&called, &ok, &chain, &proof));
fayanga64c1a92016-02-13 01:55:58290
fayangf5f4cd512017-02-07 16:57:11291 // GetProof here expects the async method to invoke the callback
292 // synchronously.
Zhongyi Shi43a8f2242020-05-04 23:34:31293 source->GetProof(server_addr, client_addr, hostname, server_config,
Renjie Tang1085bf52019-11-20 00:10:53294 GetParam().transport_version, chlo_hash, std::move(cb));
fayangf5f4cd512017-02-07 16:57:11295 ASSERT_TRUE(called);
296 ASSERT_TRUE(ok);
fayanga64c1a92016-02-13 01:55:58297
298 // Make sure we can safely access results after deleting where they came from.
299 EXPECT_FALSE(chain->HasOneRef());
fayang199cfb02016-07-13 03:56:39300 source = nullptr;
fayanga64c1a92016-02-13 01:55:58301 EXPECT_TRUE(chain->HasOneRef());
302
303 EXPECT_FALSE(chain->certs.empty());
304 for (const string& cert : chain->certs) {
305 EXPECT_FALSE(cert.empty());
306 }
307}
308
[email protected]c244c5a12013-05-07 20:55:04309} // namespace test
310} // namespace net