9 #include <gmock/gmock.h> 10 #include <gtest/gtest.h> 23 using namespace folly;
33 context_ = std::make_shared<FizzServerContext>();
35 auto mockFactory = std::make_unique<MockFactory>();
36 mockFactory->setDefaults();
37 factory_ = mockFactory.get();
38 context_->setFactory(
std::move(mockFactory));
39 cert_ = std::make_shared<MockSelfCert>();
40 auto certManager = std::make_unique<MockCertManager>();
41 certManager_ = certManager.get();
42 certVerifier_ = std::make_shared<MockCertificateVerifier>();
43 context_->setClientCertVerifier(certVerifier_);
44 context_->setCertManager(
std::move(certManager));
45 context_->setSupportedAlpns({
"h2",
"h3"});
46 mockTicketCipher_ = std::make_shared<MockTicketCipher>();
47 mockTicketCipher_->setDefaults();
48 context_->setTicketCipher(mockTicketCipher_);
49 extensions_ = std::make_shared<MockServerExtensions>();
59 while (executor_.run())
121 mockWrite_->setDefaults();
122 state_.readRecordLayer().reset(mockRead_);
123 state_.writeRecordLayer().reset(mockWrite_);
129 appWrite_->setDefaults();
130 state_.readRecordLayer().reset(appRead_);
131 state_.writeRecordLayer().reset(appWrite_);
136 mockKeyScheduler_->setDefaults();
137 state_.keyScheduler().reset(mockKeyScheduler_);
142 mockHandshakeContext_->setDefaults();
143 state_.handshakeContext().reset(mockHandshakeContext_);
147 replayCache_ = std::make_shared<MockReplayCache>();
151 context_->setEarlyDataSettings(
153 {std::chrono::milliseconds(-1000000),
154 std::chrono::milliseconds(1000000)},
159 mockCookieCipher_ = std::make_shared<MockCookieCipher>();
160 context_->setCookieCipher(mockCookieCipher_);
182 state_.executor() = &executor_;
183 state_.context() = context_;
184 state_.state() = StateEnum::ExpectingClientHello;
185 if (addExtensions_) {
186 state_.extensions() = extensions_;
192 setMockHandshakeContext();
193 state_.executor() = &executor_;
194 state_.context() = context_;
199 state_.state() = StateEnum::ExpectingClientHello;
200 if (addExtensions_) {
201 state_.extensions() = extensions_;
207 setMockKeyScheduler();
208 setMockHandshakeContext();
209 state_.executor() = &executor_;
210 state_.context() = context_;
215 state_.state() = StateEnum::ExpectingFinished;
216 state_.serverCert() = cert_;
218 state_.alpn() =
"h2";
223 setMockKeyScheduler();
224 setMockHandshakeContext();
225 state_.executor() = &executor_;
226 state_.context() = context_;
231 state_.state() = StateEnum::ExpectingCertificate;
232 state_.serverCert() = cert_;
234 state_.alpn() =
"h2";
235 context_->setClientAuthMode(ClientAuthMode::Required);
240 setMockKeyScheduler();
241 setMockHandshakeContext();
242 state_.executor() = &executor_;
243 state_.context() = context_;
248 state_.state() = StateEnum::ExpectingCertificateVerify;
249 state_.serverCert() = cert_;
250 clientIntCert_ = std::make_shared<MockPeerCert>();
251 clientLeafCert_ = std::make_shared<MockPeerCert>();
252 std::vector<std::shared_ptr<const PeerCert>> clientCerts = {clientLeafCert_,
254 state_.unverifiedCertChain() =
std::move(clientCerts);
256 state_.alpn() =
"h2";
257 context_->setClientAuthMode(ClientAuthMode::Required);
262 setMockKeyScheduler();
263 setMockHandshakeContext();
264 state_.handshakeReadRecordLayer().reset(
268 state_.executor() = &executor_;
269 state_.state() = StateEnum::AcceptingEarlyData;
274 setMockKeyScheduler();
275 setMockHandshakeContext();
276 state_.executor() = &executor_;
279 state_.context() = context_;
280 state_.executor() = &executor_;
281 state_.state() = StateEnum::AcceptingData;
282 state_.serverCert() = cert_;
284 state_.alpn() =
"h2";
297 std::shared_ptr<MockSelfCert>
cert_;
304 bool addExtensions_ =
true;
310 expectError<FizzException>(
actions,
none,
"invalid event");
318 expectError<FizzException>(
330 setUpAcceptingData();
340 EXPECT_CALL(*factory_, makePlaintextReadRecordLayer())
341 .WillOnce(
Invoke([&rrl]() {
342 auto ret = std::make_unique<PlaintextReadRecordLayer>();
346 EXPECT_CALL(*factory_, makePlaintextWriteRecordLayer())
347 .WillOnce(
Invoke([&wrl]() {
348 auto ret = std::make_unique<PlaintextWriteRecordLayer>();
353 state_, &executor_, context_, extensions_));
354 expectActions<MutateState>(
actions);
355 processStateMutations(
actions);
356 EXPECT_EQ(state_.state(), StateEnum::ExpectingClientHello);
357 EXPECT_EQ(state_.executor(), &executor_);
358 EXPECT_EQ(state_.context().get(), context_.get());
359 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
361 state_.readRecordLayer()->getEncryptionLevel(),
363 EXPECT_EQ(state_.writeRecordLayer().get(), wrl);
365 state_.writeRecordLayer()->getEncryptionLevel(),
370 setUpAcceptingData();
382 expectActions<MutateState, WriteToSocket>(
actions);
386 processStateMutations(
actions);
388 EXPECT_EQ(state_.readRecordLayer().get(),
nullptr);
389 EXPECT_EQ(state_.writeRecordLayer().get(),
nullptr);
394 expectActions<MutateState>(
actions);
395 processStateMutations(
actions);
397 EXPECT_EQ(state_.readRecordLayer().get(),
nullptr);
398 EXPECT_EQ(state_.writeRecordLayer().get(),
nullptr);
402 setUpExpectingClientHello();
408 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
412 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
415 *mockHandshakeContext_,
416 appendToTranscript(BufMatches(
"clienthelloencoding")))
417 .InSequence(contextSeq);
420 auto ret = std::make_unique<MockKeyExchange>();
422 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
430 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
431 .InSequence(contextSeq);
432 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
433 .InSequence(contextSeq)
436 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
457 return std::vector<uint8_t>({
's',
'h',
't'});
464 return std::vector<uint8_t>({
'c',
'h',
't'});
466 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
471 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
480 std::vector<Extension> exts;
491 expectAeadCreation({{
"clientkey", &raead},
492 {
"serverkey", &waead},
493 {
"serverappkey", &appwaead}});
494 expectEncryptedReadRecordLayerCreation(
497 expectEncryptedWriteRecordLayerCreation(
508 modifiedEncryptedExt.extensions.push_back(
std::move(ext));
511 getEncryptedHandshakeWrite(
523 expectEncryptedWriteRecordLayerCreation(
524 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
525 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
526 .InSequence(contextSeq);
530 const std::vector<SignatureScheme>& ,
531 const std::vector<SignatureScheme>& peerSigSchemes) {
541 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
542 .InSequence(contextSeq);
543 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
544 .InSequence(contextSeq)
552 RangeMatches(
"chlo_shlo_ee_cert")))
555 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
556 .InSequence(contextSeq);
558 .InSequence(contextSeq)
561 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
562 .InSequence(contextSeq);
563 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
564 .InSequence(contextSeq)
567 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
572 RangeMatches(
"chlo_shlo_ee_cert_sfin")))
574 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
578 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_ee_cert_sfin")));
582 return std::vector<uint8_t>({
's',
'a',
't'});
584 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
593 expectActions<MutateState, WriteToSocket>(
actions);
606 processStateMutations(
actions);
607 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
608 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
610 state_.readRecordLayer()->getEncryptionLevel(),
612 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
614 state_.writeRecordLayer()->getEncryptionLevel(),
616 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
617 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
634 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
638 setUpExpectingClientHello();
644 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
648 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
651 *mockHandshakeContext_,
652 appendToTranscript(BufMatches(
"clienthelloencoding")))
653 .InSequence(contextSeq);
656 auto ret = std::make_unique<MockKeyExchange>();
658 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
666 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
667 .InSequence(contextSeq);
668 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
669 .InSequence(contextSeq)
672 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
693 return std::vector<uint8_t>({
's',
'h',
't'});
700 return std::vector<uint8_t>({
'c',
'h',
't'});
702 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
707 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
716 std::vector<Extension> exts;
727 expectAeadCreation({{
"clientkey", &raead},
728 {
"serverkey", &waead},
729 {
"serverappkey", &appwaead}});
730 expectEncryptedReadRecordLayerCreation(
733 expectEncryptedWriteRecordLayerCreation(
744 modifiedEncryptedExt.extensions.push_back(
std::move(ext));
747 getEncryptedHandshakeWrite(
759 expectEncryptedWriteRecordLayerCreation(
760 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
761 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
762 .InSequence(contextSeq);
766 const std::vector<SignatureScheme>& ,
767 const std::vector<SignatureScheme>& peerSigSchemes) {
776 context_->setSupportedCompressionAlgorithms(
781 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
782 .InSequence(contextSeq);
783 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
784 .InSequence(contextSeq)
792 RangeMatches(
"chlo_shlo_ee_compcert")))
795 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
796 .InSequence(contextSeq);
798 .InSequence(contextSeq)
801 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
802 .InSequence(contextSeq);
803 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
804 .InSequence(contextSeq)
807 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
812 RangeMatches(
"chlo_shlo_ee_compcert_sfin")))
814 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
818 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_ee_compcert_sfin")));
822 return std::vector<uint8_t>({
's',
'a',
't'});
824 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
836 expectActions<MutateState, WriteToSocket>(
actions);
849 processStateMutations(
actions);
850 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
851 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
852 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
853 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
854 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
872 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
876 setUpExpectingClientHello();
877 context_->setClientAuthMode(ClientAuthMode::Required);
883 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
887 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
890 *mockHandshakeContext_,
891 appendToTranscript(BufMatches(
"clienthelloencoding")))
892 .InSequence(contextSeq);
895 auto ret = std::make_unique<MockKeyExchange>();
897 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
905 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
906 .InSequence(contextSeq);
907 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
908 .InSequence(contextSeq)
911 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
932 return std::vector<uint8_t>({
's',
'h',
't'});
939 return std::vector<uint8_t>({
'c',
'h',
't'});
941 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
946 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
955 std::vector<Extension> exts;
966 expectAeadCreation({{
"clientkey", &raead},
967 {
"serverkey", &waead},
968 {
"serverappkey", &appwaead}});
969 expectEncryptedReadRecordLayerCreation(
972 expectEncryptedWriteRecordLayerCreation(
982 modifiedEncryptedExt.extensions.push_back(
std::move(ext));
985 getEncryptedHandshakeWrite(
998 expectEncryptedWriteRecordLayerCreation(
999 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
1000 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1001 .InSequence(contextSeq);
1002 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1003 .InSequence(contextSeq);
1007 const std::vector<SignatureScheme>& ,
1008 const std::vector<SignatureScheme>& peerSigSchemes) {
1018 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1019 .InSequence(contextSeq);
1020 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1021 .InSequence(contextSeq)
1029 RangeMatches(
"chlo_shlo_ee_cert")))
1032 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1033 .InSequence(contextSeq);
1035 .InSequence(contextSeq)
1038 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1039 .InSequence(contextSeq);
1040 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1041 .InSequence(contextSeq)
1044 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
1049 RangeMatches(
"chlo_shlo_ee_cert_sfin")))
1051 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
1055 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_ee_cert_sfin")));
1059 return std::vector<uint8_t>({
's',
'a',
't'});
1061 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
1070 expectActions<MutateState, WriteToSocket>(
actions);
1082 processStateMutations(
actions);
1083 EXPECT_EQ(state_.state(), StateEnum::ExpectingCertificate);
1084 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
1085 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
1086 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
1087 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
1104 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1109 setUpExpectingClientHello();
1124 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
1128 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
1131 *mockKeyScheduler_, deriveEarlySecret(RangeMatches(
"resumesecret")));
1132 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(BufMatches(
"client")))
1133 .InSequence(contextSeq);
1135 .InSequence(contextSeq)
1139 *mockHandshakeContext_, appendToTranscript(BufMatches(
"helloencoding")))
1140 .InSequence(contextSeq);
1145 return std::vector<uint8_t>({
'b',
'd',
'r'});
1147 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1148 .InSequence(contextSeq);
1149 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1150 .InSequence(contextSeq)
1152 EXPECT_CALL(*mockKeyScheduler_, deriveHandshakeSecret());
1177 return std::vector<uint8_t>({
's',
'h',
't'});
1184 return std::vector<uint8_t>({
'c',
'h',
't'});
1186 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
1191 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
1202 expectAeadCreation({{
"clientkey", &raead},
1203 {
"serverkey", &waead},
1204 {
"serverappkey", &appwaead}});
1205 expectEncryptedReadRecordLayerCreation(
1208 expectEncryptedWriteRecordLayerCreation(
1216 getEncryptedHandshakeWrite(
1225 expectEncryptedWriteRecordLayerCreation(
1226 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
1227 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1228 .InSequence(contextSeq);
1230 .InSequence(contextSeq)
1233 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1234 .InSequence(contextSeq);
1235 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1236 .InSequence(contextSeq)
1239 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
1244 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
1248 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_sfin")));
1252 return std::vector<uint8_t>({
's',
'a',
't'});
1254 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
1263 expectActions<MutateState, WriteToSocket>(
actions);
1274 processStateMutations(
actions);
1275 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
1276 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
1278 state_.readRecordLayer()->getEncryptionLevel(),
1280 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
1282 state_.writeRecordLayer()->getEncryptionLevel(),
1284 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
1285 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
1302 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1307 setUpExpectingClientHello();
1322 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
1326 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
1329 *mockKeyScheduler_, deriveEarlySecret(RangeMatches(
"resumesecret")));
1330 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(BufMatches(
"client")))
1331 .InSequence(contextSeq);
1333 .InSequence(contextSeq)
1337 *mockHandshakeContext_, appendToTranscript(BufMatches(
"helloencoding")))
1338 .InSequence(contextSeq);
1343 return std::vector<uint8_t>({
'b',
'd',
'r'});
1347 auto ret = std::make_unique<MockKeyExchange>();
1349 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
1357 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1358 .InSequence(contextSeq);
1359 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1360 .InSequence(contextSeq)
1363 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
1387 return std::vector<uint8_t>({
's',
'h',
't'});
1394 return std::vector<uint8_t>({
'c',
'h',
't'});
1396 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
1401 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
1412 expectAeadCreation({{
"clientkey", &raead},
1413 {
"serverkey", &waead},
1414 {
"serverappkey", &appwaead}});
1415 expectEncryptedReadRecordLayerCreation(
1418 expectEncryptedWriteRecordLayerCreation(
1426 getEncryptedHandshakeWrite(
1435 expectEncryptedWriteRecordLayerCreation(
1436 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
1437 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1438 .InSequence(contextSeq);
1440 .InSequence(contextSeq)
1443 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1444 .InSequence(contextSeq);
1445 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1446 .InSequence(contextSeq)
1449 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
1454 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
1458 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_sfin")));
1462 return std::vector<uint8_t>({
's',
'a',
't'});
1464 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
1473 expectActions<MutateState, WriteToSocket>(
actions);
1485 processStateMutations(
actions);
1486 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
1487 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
1489 state_.readRecordLayer()->getEncryptionLevel(),
1491 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
1493 state_.writeRecordLayer()->getEncryptionLevel(),
1495 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
1496 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
1512 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1516 setUpExpectingClientHello();
1522 .InSequence(factorySeq)
1524 return std::unique_ptr<HandshakeContext>(firstHandshakeContext);
1527 *firstHandshakeContext,
1528 appendToTranscript(BufMatches(
"clienthelloencoding")))
1529 .InSequence(firstContextSeq);
1530 EXPECT_CALL(*firstHandshakeContext, getHandshakeContext())
1531 .InSequence(firstContextSeq)
1536 .InSequence(factorySeq)
1538 return std::unique_ptr<HandshakeContext>(secondHandshakeContext);
1540 EXPECT_CALL(*secondHandshakeContext, appendToTranscript(
_)).Times(2);
1552 EXPECT_CALL(*factory_, makePlaintextReadRecordLayer())
1553 .WillOnce(
Invoke([newRrl]() {
1554 return std::unique_ptr<PlaintextReadRecordLayer>(newRrl);
1556 EXPECT_CALL(*newRrl, setSkipEncryptedRecords(
false));
1562 expectActions<MutateState, WriteToSocket>(
actions);
1569 processStateMutations(
actions);
1570 EXPECT_EQ(state_.state(), StateEnum::ExpectingClientHello);
1571 EXPECT_EQ(state_.readRecordLayer().get(), newRrl);
1573 state_.readRecordLayer()->getEncryptionLevel(),
1575 EXPECT_EQ(state_.writeRecordLayer().get(), mockWrite_);
1577 state_.writeRecordLayer()->getEncryptionLevel(),
1579 EXPECT_EQ(state_.handshakeContext().get(), secondHandshakeContext);
1586 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1590 setUpExpectingClientHelloRetry();
1595 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
1597 *mockHandshakeContext_,
1598 appendToTranscript(BufMatches(
"clienthelloencoding")))
1599 .InSequence(contextSeq);
1602 auto ret = std::make_unique<MockKeyExchange>();
1604 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
1612 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1613 .InSequence(contextSeq);
1614 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1615 .InSequence(contextSeq)
1618 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
1639 return std::vector<uint8_t>({
's',
'h',
't'});
1646 return std::vector<uint8_t>({
'c',
'h',
't'});
1648 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
1653 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
1664 expectAeadCreation({{
"clientkey", &raead},
1665 {
"serverkey", &waead},
1666 {
"serverappkey", &appwaead}});
1667 expectEncryptedReadRecordLayerCreation(
1670 expectEncryptedWriteRecordLayerCreation(
1678 getEncryptedHandshakeWrite(
1690 expectEncryptedWriteRecordLayerCreation(
1691 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
1692 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1693 .InSequence(contextSeq);
1697 const std::vector<SignatureScheme>& ,
1698 const std::vector<SignatureScheme>& peerSigSchemes) {
1708 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1709 .InSequence(contextSeq);
1710 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1711 .InSequence(contextSeq)
1719 RangeMatches(
"chlo_shlo_ee_cert")))
1722 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1723 .InSequence(contextSeq);
1725 .InSequence(contextSeq)
1728 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1729 .InSequence(contextSeq);
1730 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1731 .InSequence(contextSeq)
1734 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
1739 RangeMatches(
"chlo_shlo_ee_cert_sfin")))
1741 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
1745 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_ee_cert_sfin")));
1749 return std::vector<uint8_t>({
's',
'a',
't'});
1751 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
1760 expectActions<MutateState, WriteToSocket>(
actions);
1772 processStateMutations(
actions);
1773 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
1774 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
1775 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
1776 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
1777 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
1794 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1799 setUpExpectingClientHelloRetry();
1813 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
1815 *mockKeyScheduler_, deriveEarlySecret(RangeMatches(
"resumesecret")));
1816 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(BufMatches(
"client")))
1817 .InSequence(contextSeq);
1819 .InSequence(contextSeq)
1823 *mockHandshakeContext_, appendToTranscript(BufMatches(
"helloencoding")))
1824 .InSequence(contextSeq);
1829 return std::vector<uint8_t>({
'b',
'd',
'r'});
1833 auto ret = std::make_unique<MockKeyExchange>();
1835 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
1843 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1844 .InSequence(contextSeq);
1845 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1846 .InSequence(contextSeq)
1849 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
1873 return std::vector<uint8_t>({
's',
'h',
't'});
1880 return std::vector<uint8_t>({
'c',
'h',
't'});
1882 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
1887 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
1898 expectAeadCreation({{
"clientkey", &raead},
1899 {
"serverkey", &waead},
1900 {
"serverappkey", &appwaead}});
1901 expectEncryptedReadRecordLayerCreation(
1904 expectEncryptedWriteRecordLayerCreation(
1912 getEncryptedHandshakeWrite(
1921 expectEncryptedWriteRecordLayerCreation(
1922 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
1923 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1924 .InSequence(contextSeq);
1926 .InSequence(contextSeq)
1929 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
1930 .InSequence(contextSeq);
1931 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
1932 .InSequence(contextSeq)
1935 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
1940 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
1944 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_sfin")));
1948 return std::vector<uint8_t>({
's',
'a',
't'});
1950 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
1959 expectActions<MutateState, WriteToSocket>(
actions);
1971 processStateMutations(
actions);
1972 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
1973 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
1974 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
1975 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
1976 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
1992 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
1998 setUpExpectingClientHello();
2017 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
2021 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
2024 *mockKeyScheduler_, deriveEarlySecret(RangeMatches(
"resumesecret")));
2025 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(BufMatches(
"client")))
2026 .InSequence(contextSeq);
2028 .InSequence(contextSeq)
2032 *mockHandshakeContext_, appendToTranscript(BufMatches(
"helloencoding")))
2033 .InSequence(contextSeq);
2034 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2035 .InSequence(contextSeq)
2041 return std::vector<uint8_t>({
'b',
'd',
'r'});
2047 return std::vector<uint8_t>({
'c',
'e',
't'});
2053 return std::vector<uint8_t>({
'e',
'e',
'm'});
2057 auto ret = std::make_unique<MockKeyExchange>();
2059 EXPECT_CALL(*ret, generateSharedSecret(RangeMatches(
"keyshare")))
2067 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2068 .InSequence(contextSeq);
2069 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2070 .InSequence(contextSeq)
2073 *mockKeyScheduler_, deriveHandshakeSecret(RangeMatches(
"sharedsecret")));
2097 return std::vector<uint8_t>({
's',
'h',
't'});
2104 return std::vector<uint8_t>({
'c',
'h',
't'});
2106 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cet"),
_,
_))
2111 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
2116 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
2129 expectAeadCreation({{
"earlykey", &earlyaead},
2130 {
"clientkey", &raead},
2131 {
"serverkey", &waead},
2132 {
"serverappkey", &appwaead}});
2134 expectEncryptedReadRecordLayerCreation(
2136 expectEncryptedReadRecordLayerCreation(
2137 &handshakerrl, &raead,
StringPiece(
"cht"),
false, &readRecSeq);
2139 expectEncryptedWriteRecordLayerCreation(
2147 getEncryptedHandshakeWrite(
2156 expectEncryptedWriteRecordLayerCreation(
2157 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
2158 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2159 .InSequence(contextSeq);
2161 .InSequence(contextSeq)
2164 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2165 .InSequence(contextSeq);
2166 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2167 .InSequence(contextSeq)
2170 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
2175 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
2179 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_sfin")));
2183 return std::vector<uint8_t>({
's',
'a',
't'});
2185 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
2194 expectActions<MutateState, WriteToSocket, ReportEarlyHandshakeSuccess>(
2207 processStateMutations(
actions);
2208 EXPECT_EQ(state_.state(), StateEnum::AcceptingEarlyData);
2209 EXPECT_EQ(state_.handshakeReadRecordLayer().get(), handshakerrl);
2210 EXPECT_EQ(state_.readRecordLayer().get(), earlyrrl);
2212 state_.readRecordLayer()->getEncryptionLevel(),
2214 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
2216 state_.writeRecordLayer()->getEncryptionLevel(),
2218 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
2219 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
2242 setUpExpectingClientHello();
2261 [=]() {
return std::unique_ptr<KeyScheduler>(mockKeyScheduler_); }));
2265 return std::unique_ptr<HandshakeContext>(mockHandshakeContext_);
2268 *mockKeyScheduler_, deriveEarlySecret(RangeMatches(
"resumesecret")));
2269 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(BufMatches(
"client")))
2270 .InSequence(contextSeq);
2272 .InSequence(contextSeq)
2276 *mockHandshakeContext_, appendToTranscript(BufMatches(
"helloencoding")))
2277 .InSequence(contextSeq);
2278 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2279 .InSequence(contextSeq)
2285 return std::vector<uint8_t>({
'b',
'd',
'r'});
2291 return std::vector<uint8_t>({
'c',
'e',
't'});
2297 return std::vector<uint8_t>({
'e',
'e',
'm'});
2299 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2300 .InSequence(contextSeq);
2301 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2302 .InSequence(contextSeq)
2304 EXPECT_CALL(*mockKeyScheduler_, deriveHandshakeSecret());
2329 return std::vector<uint8_t>({
's',
'h',
't'});
2336 return std::vector<uint8_t>({
'c',
'h',
't'});
2338 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cet"),
_,
_))
2343 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sht"),
_,
_))
2348 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cht"),
_,
_))
2361 expectAeadCreation({{
"earlykey", &earlyaead},
2362 {
"clientkey", &raead},
2363 {
"serverkey", &waead},
2364 {
"serverappkey", &appwaead}});
2366 expectEncryptedReadRecordLayerCreation(
2368 expectEncryptedReadRecordLayerCreation(
2369 &handshakerrl, &raead,
StringPiece(
"cht"),
false, &readRecSeq);
2371 expectEncryptedWriteRecordLayerCreation(
2379 getEncryptedHandshakeWrite(
2388 expectEncryptedWriteRecordLayerCreation(
2389 &appwrl, &appwaead,
StringPiece(
"sat"),
nullptr, &recSeq);
2390 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2391 .InSequence(contextSeq);
2393 .InSequence(contextSeq)
2396 EXPECT_CALL(*mockHandshakeContext_, appendToTranscript(
_))
2397 .InSequence(contextSeq);
2398 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
2399 .InSequence(contextSeq)
2402 EXPECT_CALL(*mockKeyScheduler_, deriveMasterSecret());
2407 return std::vector<uint8_t>({
'e',
'x',
'p',
'm'});
2411 deriveAppTrafficSecrets(RangeMatches(
"chlo_shlo_sfin")));
2415 return std::vector<uint8_t>({
's',
'a',
't'});
2417 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
2426 expectActions<MutateState, WriteToSocket, ReportEarlyHandshakeSuccess>(
2440 processStateMutations(
actions);
2441 EXPECT_EQ(state_.state(), StateEnum::AcceptingEarlyData);
2442 EXPECT_EQ(state_.handshakeReadRecordLayer().get(), handshakerrl);
2443 EXPECT_EQ(state_.readRecordLayer().get(), earlyrrl);
2445 state_.readRecordLayer()->getEncryptionLevel(),
2447 EXPECT_EQ(state_.writeRecordLayer().get(), appwrl);
2449 state_.writeRecordLayer()->getEncryptionLevel(),
2451 EXPECT_EQ(state_.handshakeContext().get(), mockHandshakeContext_);
2452 EXPECT_EQ(state_.keyScheduler().get(), mockKeyScheduler_);
2474 addExtensions_ =
false;
2475 setUpExpectingClientHello();
2478 expectActions<MutateState, WriteToSocket>(
actions);
2479 processStateMutations(
actions);
2484 setUpExpectingClientHello();
2489 expectActions<MutateState, WriteToSocket>(
actions);
2501 setUpExpectingClientHello();
2507 expectActions<MutateState, WriteToSocket>(
actions);
2514 processStateMutations(
actions);
2515 EXPECT_EQ(state_.state(), StateEnum::ExpectingClientHello);
2519 setUpExpectingClientHello();
2522 expectActions<MutateState, WriteToSocket>(
actions);
2527 setUpExpectingClientHello();
2530 expectActions<MutateState, WriteToSocket>(
actions);
2535 setUpExpectingClientHello();
2538 expectActions<MutateState, WriteToSocket>(
actions);
2542 setUpExpectingClientHello();
2549 expectActions<MutateState, WriteToSocket>(
actions);
2553 setUpExpectingClientHello();
2557 expectActions<MutateState, WriteToSocket>(
actions);
2561 setUpExpectingClientHello();
2568 expectActions<MutateState, WriteToSocket>(
actions);
2569 processStateMutations(
actions);
2570 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2575 setUpExpectingClientHello();
2579 expectError<FizzException>(
2584 setUpExpectingClientHello();
2588 expectActions<MutateState, WriteToSocket>(
actions);
2589 processStateMutations(
actions);
2590 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2596 setUpExpectingClientHello();
2608 expectError<FizzException>(
2613 context_->setVersionFallbackEnabled(
true);
2614 setUpExpectingClientHello();
2619 expectActions<MutateState, AttemptVersionFallback>(
actions);
2620 processStateMutations(
actions);
2623 auto fallback = expectAction<AttemptVersionFallback>(
actions);
2625 "\x16\x03\x01\x00\x13" 2626 "clienthelloencoding",
2628 EXPECT_EQ(fallback.clientHello->moveToFbString().toStdString(), expected);
2632 setUpExpectingClientHello();
2637 expectError<FizzException>(
2640 "supported version mismatch");
2644 setUpExpectingClientHello();
2648 supportedVersions.
versions.push_back(static_cast<ProtocolVersion>(0x0200));
2649 clientHello.extensions.push_back(
2653 expectError<FizzException>(
2656 "supported version mismatch");
2660 setUpExpectingClientHello();
2662 clientHello.cipher_suites.clear();
2665 expectError<FizzException>(
2670 setUpExpectingClientHello();
2675 expectError<FizzException>(
2680 setUpExpectingClientHello();
2688 expectError<FizzException>(
2693 setUpExpectingClientHello();
2698 expectError<FizzException>(
2703 setUpExpectingClientHello();
2709 expectError<FizzException>(
2714 setUpExpectingClientHello();
2715 EXPECT_CALL(*mockRead_, hasUnparsedHandshakeData())
2716 .WillRepeatedly(
Return(
true));
2719 expectError<FizzException>(
2724 setUpExpectingClientHello();
2729 expectActions<MutateState, WriteToSocket>(
actions);
2730 processStateMutations(
actions);
2735 setUpExpectingClientHello();
2745 expectActions<MutateState, WriteToSocket>(
actions);
2746 processStateMutations(
actions);
2751 setUpExpectingClientHello();
2764 expectActions<MutateState, WriteToSocket>(
actions);
2765 processStateMutations(
actions);
2771 setUpExpectingClientHello();
2775 expectActions<MutateState, WriteToSocket, ReportEarlyHandshakeSuccess>(
2777 processStateMutations(
actions);
2778 EXPECT_EQ(state_.state(), StateEnum::AcceptingEarlyData);
2783 state_.readRecordLayer()->getEncryptionLevel(),
2789 TestClientHelloEarlyDataNotAttemptedWithAppTokenValidator) {
2791 setUpExpectingClientHello();
2792 auto validator = std::make_unique<MockAppTokenValidator>();
2793 auto validatorPtr = validator.get();
2794 state_.appTokenValidator() =
std::move(validator);
2798 <<
"Early data not attempted, validator shoudn't be called";
2803 expectActions<MutateState, WriteToSocket>(
actions);
2804 processStateMutations(
actions);
2805 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2812 setUpExpectingClientHello();
2813 auto validator = std::make_unique<MockAppTokenValidator>();
2814 auto validatorPtr = validator.get();
2815 state_.appTokenValidator() =
std::move(validator);
2841 expectActions<MutateState, WriteToSocket, ReportEarlyHandshakeSuccess>(
2843 processStateMutations(
actions);
2844 EXPECT_EQ(state_.state(), StateEnum::AcceptingEarlyData);
2851 setUpExpectingClientHello();
2854 EXPECT_CALL(*factory_, makeEncryptedReadRecordLayer(
_))
2856 [rrl]() {
return std::unique_ptr<EncryptedReadRecordLayer>(rrl); }));
2861 expectActions<MutateState, WriteToSocket>(
actions);
2862 processStateMutations(
actions);
2863 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2864 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
2867 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
2869 state_.readRecordLayer()->getEncryptionLevel(),
2876 setUpExpectingClientHello();
2879 EXPECT_CALL(*factory_, makePlaintextReadRecordLayer())
2881 [rrl]() {
return std::unique_ptr<PlaintextReadRecordLayer>(rrl); }));
2886 expectActions<MutateState, WriteToSocket>(
actions);
2887 processStateMutations(
actions);
2888 EXPECT_EQ(state_.state(), StateEnum::ExpectingClientHello);
2889 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
2891 EXPECT_FALSE(state_.earlyExporterMasterSecret().hasValue());
2893 state_.readRecordLayer()->getEncryptionLevel(),
2896 state_.writeRecordLayer()->getEncryptionLevel(),
2903 setUpExpectingClientHello();
2913 expectActions<MutateState, WriteToSocket>(
actions);
2914 processStateMutations(
actions);
2915 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2919 state_.readRecordLayer()->getEncryptionLevel(),
2922 state_.writeRecordLayer()->getEncryptionLevel(),
2928 setUpExpectingClientHello();
2936 expectActions<MutateState, WriteToSocket>(
actions);
2937 processStateMutations(
actions);
2938 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2942 state_.readRecordLayer()->getEncryptionLevel(),
2945 state_.writeRecordLayer()->getEncryptionLevel(),
2951 setUpExpectingClientHello();
2959 expectActions<MutateState, WriteToSocket>(
actions);
2960 processStateMutations(
actions);
2961 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2969 setUpExpectingClientHello();
2976 expectActions<MutateState, WriteToSocket>(
actions);
2977 processStateMutations(
actions);
2978 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
2985 setUpExpectingClientHello();
2999 expectActions<MutateState, WriteToSocket>(
actions);
3000 processStateMutations(
actions);
3001 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3008 setUpExpectingClientHelloRetry();
3012 expectActions<MutateState, WriteToSocket>(
actions);
3013 processStateMutations(
actions);
3014 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3020 context_->setEarlyDataSettings(
3022 {std::chrono::milliseconds(-10), std::chrono::milliseconds(10)},
3024 setUpExpectingClientHello();
3031 expectActions<MutateState, WriteToSocket>(
actions);
3032 processStateMutations(
actions);
3033 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3036 EXPECT_LT(*state_.clientClockSkew(), std::chrono::milliseconds(-5000));
3040 context_->setEarlyDataSettings(
3042 {std::chrono::milliseconds(-10), std::chrono::milliseconds(10)},
3044 setUpExpectingClientHello();
3051 expectActions<MutateState, WriteToSocket>(
actions);
3052 processStateMutations(
actions);
3053 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3056 EXPECT_GT(*state_.clientClockSkew(), std::chrono::milliseconds(5000));
3061 setUpExpectingClientHello();
3081 expectActions<MutateState, WriteToSocket>(
actions);
3082 processStateMutations(
actions);
3083 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3086 EXPECT_GT(*state_.clientClockSkew(), std::chrono::milliseconds(5000));
3091 setUpExpectingClientHello();
3111 expectActions<MutateState, WriteToSocket>(
actions);
3112 processStateMutations(
actions);
3113 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3116 EXPECT_GT(*state_.clientClockSkew(), std::chrono::milliseconds(5000));
3121 setUpExpectingClientHello();
3122 auto validator = std::make_unique<MockAppTokenValidator>();
3123 auto validatorPtr = validator.get();
3124 state_.appTokenValidator() =
std::move(validator);
3151 expectActions<MutateState, WriteToSocket>(
actions);
3152 processStateMutations(
actions);
3153 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3159 setUpExpectingClientHello();
3160 state_.handshakeLogging() = std::make_unique<HandshakeLogging>();
3163 processStateMutations(actions);
3167 state_.handshakeLogging()->clientSupportedVersions,
3170 state_.handshakeLogging()->clientCiphers,
3174 state_.handshakeLogging()->clientExtensions,
3175 std::vector<ExtensionType>(
3184 state_.handshakeLogging()->clientSignatureAlgorithms,
3187 EXPECT_EQ(*state_.handshakeLogging()->clientSessionIdSent,
false);
3188 EXPECT_TRUE(state_.handshakeLogging()->clientRandom.hasValue());
3192 setUpExpectingClientHello();
3193 state_.handshakeLogging() = std::make_unique<HandshakeLogging>();
3198 state_.handshakeLogging()->clientLegacyVersion,
3203 setUpExpectingClientHello();
3205 chlo.legacy_compression_methods.clear();
3207 expectError<FizzException>(
3212 setUpExpectingClientHello();
3216 expectError<FizzException>(
3221 setUpExpectingClientHello();
3239 expectError<FizzException>(
3242 "duplicate client key share");
3246 setUpExpectingClientHelloRetry();
3253 expectError<FizzException>(
3258 setUpExpectingClientHelloRetry();
3266 expectError<FizzException>(
3269 "cookie after statefull hrr");
3273 context_->setVersionFallbackEnabled(
true);
3274 setUpExpectingClientHelloRetry();
3279 expectError<FizzException>(
3282 "version mismatch with previous negotiation");
3286 setUpExpectingClientHello();
3303 expectActions<MutateState, WriteToSocket>(
actions);
3304 processStateMutations(
actions);
3305 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3311 setUpExpectingClientHello();
3328 expectActions<MutateState, WriteToSocket>(
actions);
3329 processStateMutations(
actions);
3330 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3337 setUpExpectingClientHello();
3345 expectActions<MutateState, WriteToSocket>(
actions);
3346 processStateMutations(
actions);
3347 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3353 setUpExpectingClientHello();
3361 expectError<FizzException>(
3366 setUpExpectingClientHello();
3374 expectError<FizzException>(
3379 setUpExpectingClientHello();
3398 expectError<FizzException>(
3401 "version mismatch with cookie");
3405 setUpExpectingClientHello();
3424 expectError<FizzException>(
3427 "cipher mismatch with cookie");
3431 setUpExpectingClientHello();
3451 expectError<FizzException>(
3454 "group mismatch with cookie");
3459 setUpExpectingClientHello();
3470 expectError<FizzException>(
3475 setUpExpectingClientHello();
3476 context_->setSupportedCompressionAlgorithms(
3483 expectActions<MutateState, WriteToSocket>(
actions);
3484 processStateMutations(
actions);
3485 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3490 setUpExpectingClientHello();
3496 expectActions<MutateState, WriteToSocket>(
actions);
3497 processStateMutations(
actions);
3498 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3503 setUpAcceptingEarlyData();
3512 setUpAcceptingEarlyData();
3532 setUpAcceptingEarlyData();
3533 auto handshakeReadRecordLayer = state_.handshakeReadRecordLayer().get();
3536 *mockHandshakeContext_, appendToTranscript(BufMatches(
"eoedencoding")));
3540 expectActions<MutateState>(
actions);
3541 processStateMutations(
actions);
3542 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
3543 EXPECT_EQ(state_.readRecordLayer().get(), handshakeReadRecordLayer);
3544 EXPECT_EQ(state_.writeRecordLayer().get(), mockWrite_);
3546 state_.readRecordLayer()->getEncryptionLevel(),
3551 setUpAcceptingEarlyData();
3553 EXPECT_CALL(*mockRead_, hasUnparsedHandshakeData())
3554 .WillRepeatedly(
Return(
true));
3558 expectError<FizzException>(
3563 setUpExpectingFinished();
3567 .InSequence(contextSeq)
3570 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
3571 .InSequence(contextSeq)
3575 *mockHandshakeContext_,
3576 appendToTranscript(BufMatches(
"finishedencoding")))
3577 .InSequence(contextSeq);
3578 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
3579 .InSequence(contextSeq)
3586 return std::vector<uint8_t>({
'r',
's',
'e',
'c'});
3591 return std::vector<uint8_t>({
'c',
'a',
't'});
3593 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cat"),
_,
_))
3600 getResumptionSecret(RangeMatches(
"rsec"), RangeMatches(
"")))
3606 expectAeadCreation(&raead,
nullptr);
3607 expectEncryptedReadRecordLayerCreation(&rrl, &raead,
StringPiece(
"cat"));
3618 return std::make_pair(
3631 EXPECT_CALL(*mockKeyScheduler_, clearMasterSecret());
3636 expectActions<MutateState, ReportHandshakeSuccess, WriteToSocket>(
actions);
3637 processStateMutations(
actions);
3638 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3639 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
3641 state_.readRecordLayer()->getEncryptionLevel(),
3643 EXPECT_EQ(state_.writeRecordLayer().get(), mockWrite_);
3648 setUpExpectingFinished();
3655 expectActions<MutateState, ReportHandshakeSuccess>(
actions);
3656 processStateMutations(
actions);
3657 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3662 setUpExpectingFinished();
3681 expectActions<MutateState, ReportHandshakeSuccess, WriteToSocket>(
actions);
3682 processStateMutations(
actions);
3683 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3687 setUpExpectingFinished();
3692 expectActions<MutateState, ReportHandshakeSuccess>(
actions);
3693 processStateMutations(
actions);
3694 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3698 setUpExpectingFinished();
3699 context_->setSendNewSessionTicket(
false);
3701 EXPECT_CALL(*mockKeyScheduler_, clearMasterSecret());
3704 expectActions<MutateState, ReportHandshakeSuccess>(
actions);
3705 processStateMutations(
actions);
3706 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3710 setUpExpectingFinished();
3719 expectError<FizzException>(
actions,
none,
"finished verify failure");
3723 setUpExpectingFinished();
3724 EXPECT_CALL(*mockRead_, hasUnparsedHandshakeData())
3725 .WillRepeatedly(
Return(
true));
3730 expectError<FizzException>(
actions,
none,
"data after finished");
3734 setUpExpectingFinished();
3754 setUpAcceptingData();
3755 context_->setSendNewSessionTicket(
false);
3756 state_.resumptionMasterSecret() = std::vector<uint8_t>({
'r',
's',
'e',
'c'});
3758 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
3763 getResumptionSecret(RangeMatches(
"rsec"), RangeMatches(
"")))
3777 return std::make_pair(
3788 content.
data = nstBuf->clone();
3802 setUpAcceptingData();
3803 context_->setSendNewSessionTicket(
false);
3825 setUpAcceptingData();
3826 context_->setSendNewSessionTicket(
false);
3838 return std::make_pair(
3851 TestWriteNewSessionTicketWithAppTokenAfterAutomaticSend) {
3852 setUpExpectingFinished();
3853 context_->setSendNewSessionTicket(
true);
3863 return std::make_pair(
3868 expectActions<MutateState, ReportHandshakeSuccess, WriteToSocket>(
actions);
3869 processStateMutations(
actions);
3870 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
3882 return std::make_pair(
3887 auto writeNewSessionTicketActions = getActions(
3889 expectSingleAction<WriteToSocket>(
std::move(writeNewSessionTicketActions));
3893 setUpAcceptingData();
3894 context_->setSendNewSessionTicket(
false);
3906 setUpAcceptingData();
3907 context_->setSendNewSessionTicket(
false);
3916 setUpAcceptingData();
3925 setUpAcceptingData();
3947 setUpAcceptingData();
3950 expectActions<MutateState>(
actions);
3955 setUpAcceptingData();
3956 EXPECT_CALL(*appRead_, hasUnparsedHandshakeData())
3957 .WillRepeatedly(
Return(
true));
3961 expectError<FizzException>(
actions,
none,
"data after key_update");
3965 setUpAcceptingData();
3966 EXPECT_CALL(*mockKeyScheduler_, clientKeyUpdate());
3972 return std::vector<uint8_t>({
'c',
'a',
't'});
3986 EXPECT_CALL(*mockKeyScheduler_, serverKeyUpdate());
3990 return std::vector<uint8_t>({
's',
'a',
't'});
3993 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"cat"),
_,
_))
3999 EXPECT_CALL(*mockKeyScheduler_, getTrafficKey(RangeMatches(
"sat"),
_,
_))
4010 expectAeadCreation(&raead, &waead);
4011 expectEncryptedReadRecordLayerCreation(&rrl, &raead,
StringPiece(
"cat"));
4012 expectEncryptedWriteRecordLayerCreation(&wrl, &waead,
StringPiece(
"sat"));
4015 expectActions<MutateState, WriteToSocket>(
actions);
4019 processStateMutations(
actions);
4020 EXPECT_EQ(state_.readRecordLayer().get(), rrl);
4021 EXPECT_EQ(state_.writeRecordLayer().get(), wrl);
4022 EXPECT_EQ(state_.state(), StateEnum::AcceptingData);
4026 setUpExpectingCertificate();
4028 *mockHandshakeContext_, appendToTranscript(BufMatches(
"certencoding")));
4029 clientLeafCert_ = std::make_shared<MockPeerCert>();
4030 clientIntCert_ = std::make_shared<MockPeerCert>();
4031 EXPECT_CALL(*factory_, _makePeerCert(BufMatches(
"cert1")))
4032 .WillOnce(
Return(clientLeafCert_));
4033 EXPECT_CALL(*factory_, _makePeerCert(BufMatches(
"cert2")))
4034 .WillOnce(
Return(clientIntCert_));
4046 expectActions<MutateState>(
actions);
4047 processStateMutations(
actions);
4048 EXPECT_EQ(state_.unverifiedCertChain()->size(), 2);
4049 EXPECT_EQ(state_.unverifiedCertChain()->at(0), clientLeafCert_);
4050 EXPECT_EQ(state_.unverifiedCertChain()->at(1), clientIntCert_);
4051 EXPECT_EQ(state_.state(), StateEnum::ExpectingCertificateVerify);
4055 setUpExpectingCertificate();
4057 *mockHandshakeContext_, appendToTranscript(BufMatches(
"certencoding")));
4060 badCertMsg.certificate_request_context =
4065 expectError<FizzException>(
4068 "certificate request context must be empty");
4072 setUpExpectingCertificate();
4074 *mockHandshakeContext_, appendToTranscript(BufMatches(
"certencoding")));
4079 expectError<FizzException>(
4082 "certificate requested but none received");
4086 setUpExpectingCertificate();
4087 context_->setClientAuthMode(ClientAuthMode::Optional);
4089 *mockHandshakeContext_, appendToTranscript(BufMatches(
"certencoding")));
4094 expectActions<MutateState>(
actions);
4095 processStateMutations(
actions);
4097 EXPECT_EQ(state_.clientCert(),
nullptr);
4098 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
4102 setUpExpectingCertificateVerify();
4103 context_->setClientCertVerifier(
nullptr);
4105 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4106 .InSequence(contextSeq)
4115 RangeMatches(
"certcontext"),
4116 RangeMatches(
"signature")))
4117 .InSequence(contextSeq);
4120 *mockHandshakeContext_,
4121 appendToTranscript(BufMatches(
"certverifyencoding")))
4122 .InSequence(contextSeq);
4127 expectActions<MutateState>(
actions);
4128 processStateMutations(
actions);
4130 EXPECT_EQ(state_.clientCert(), clientLeafCert_);
4131 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
4135 setUpExpectingCertificateVerify();
4137 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4138 .InSequence(contextSeq)
4147 RangeMatches(
"certcontext"),
4148 RangeMatches(
"signature")))
4149 .InSequence(contextSeq);
4152 .InSequence(contextSeq)
4154 [
this](
const std::vector<std::shared_ptr<const PeerCert>>& certs) {
4161 *mockHandshakeContext_,
4162 appendToTranscript(BufMatches(
"certverifyencoding")))
4163 .InSequence(contextSeq);
4168 expectActions<MutateState>(
actions);
4169 processStateMutations(
actions);
4171 EXPECT_EQ(state_.clientCert(), clientLeafCert_);
4172 EXPECT_EQ(state_.state(), StateEnum::ExpectingFinished);
4176 setUpExpectingCertificateVerify();
4183 expectError<FizzException>(
4186 "client chose unsupported sig scheme:");
4190 setUpExpectingCertificateVerify();
4192 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4193 .InSequence(contextSeq)
4202 RangeMatches(
"certcontext"),
4203 RangeMatches(
"signature")))
4204 .InSequence(contextSeq)
4211 expectError<FizzException>(
4216 setUpExpectingCertificateVerify();
4218 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4219 .InSequence(contextSeq)
4228 RangeMatches(
"certcontext"),
4229 RangeMatches(
"signature")))
4230 .InSequence(contextSeq);
4233 .InSequence(contextSeq)
4240 expectError<FizzVerificationException>(
4245 setUpExpectingCertificateVerify();
4246 context_->setClientAuthMode(ClientAuthMode::Optional);
4248 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4249 .InSequence(contextSeq)
4258 RangeMatches(
"certcontext"),
4259 RangeMatches(
"signature")))
4260 .InSequence(contextSeq)
4267 expectError<FizzException>(
4272 setUpExpectingCertificateVerify();
4273 context_->setClientAuthMode(ClientAuthMode::Optional);
4275 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4276 .InSequence(contextSeq)
4285 RangeMatches(
"certcontext"),
4286 RangeMatches(
"signature")))
4287 .InSequence(contextSeq);
4290 .InSequence(contextSeq)
4297 expectError<FizzException>(
4302 setUpExpectingCertificateVerify();
4304 EXPECT_CALL(*mockHandshakeContext_, getHandshakeContext())
4305 .InSequence(contextSeq)
4314 RangeMatches(
"certcontext"),
4315 RangeMatches(
"signature")))
4316 .InSequence(contextSeq);
4319 .InSequence(contextSeq)
4320 .WillOnce(Throw(std::runtime_error(
"oops")));
4325 expectError<FizzException>(
4328 "client certificate failure: oops");
4332 setUpAcceptingData();
4335 expectError<FizzException>(
MockKeyScheduler * mockKeyScheduler_
MockCertManager * certManager_
Buf encodeHandshake(T &&handshakeMsg)
Integral2 random(Integral1 low, Integral2 up)
std::vector< PskBinder > binders
static std::unique_ptr< IOBuf > getEncryptedHandshakeWrite(EncryptedExtensions encryptedExt, CertificateRequest request, CertificateMsg certificate, CertificateVerify verify, Finished finished)
void setMockKeyScheduler()
static WriteNewSessionTicket writeNewSessionTicket(const std::string &str)
void write(const T &in, folly::io::Appender &appender)
uint32_t max_early_data_size
static void addPsk(ClientHello &chlo, uint32_t ticketAge=100000)
CertificateCompressionAlgorithm
MockPlaintextReadRecordLayer * mockRead_
#define ASSERT_EQ(val1, val2)
static const std::string chlo
static std::unique_ptr< IOBuf > create(std::size_t capacity)
static NewSessionTicket newSessionTicket()
AlertDescription description
void setUpExpectingClientHelloRetry()
void setMockHandshakeContext()
#define EXPECT_EQ(val1, val2)
std::chrono::steady_clock::time_point now()
constexpr detail::Map< Move > move
boost::variant< Actions, folly::Future< Actions >> AsyncActions
static CertificateMsg certificate()
std::shared_ptr< MockCertificateVerifier > certVerifier_
void setUpExpectingClientHello()
std::vector< NamedGroup > named_group_list
—— Concurrent Priority Queue Implementation ——
folly::Optional< std::string > alpn
std::shared_ptr< MockTicketCipher > mockTicketCipher_
Actions getActions(AsyncActions asyncActions, bool immediate=true)
static std::unique_ptr< IOBuf > getEncryptedHandshakeWrite(EncryptedExtensions encryptedExt, CertificateMsg certificate, CertificateVerify verify, Finished finished)
std::chrono::system_clock::time_point ticketIssueTime
PolymorphicAction< internal::InvokeWithoutArgsAction< FunctionImpl > > InvokeWithoutArgs(FunctionImpl function_impl)
folly::ssl::X509UniquePtr getCert(folly::StringPiece cert)
std::vector< KeyShareEntry > client_shares
MockPlaintextWriteRecordLayer * mockWrite_
void setUpExpectingCertificateVerify()
static ServerHello serverHello()
static Finished finished()
virtual Actions processAppClose(const State &)
std::shared_ptr< MockReplayCache > replayCache_
std::shared_ptr< MockPeerCert > clientIntCert_
static CertificateRequest certificateRequest()
uint16_t selected_identity
PolymorphicAction< internal::InvokeAction< FunctionImpl > > Invoke(FunctionImpl function_impl)
static std::unique_ptr< IOBuf > getEncryptedHandshakeWrite(EncryptedExtensions encryptedExt, CompressedCertificate certificate, CertificateVerify verify, Finished finished)
static AppWrite appWrite()
static EncryptedExtensions encryptedExt()
EncryptionLevel encryptionLevel
static EndOfEarlyData endOfEarlyData()
static CertificateVerify certificateVerify()
static ClientHello clientHelloPsk()
static CompressedCertificate compressedCertificate()
std::unique_ptr< folly::IOBuf > makeRandom(size_t n)
std::vector< ProtocolVersion > versions
static void removeExtension(T &msg, ExtensionType ext)
Buf getFinishedData(std::unique_ptr< KeyDerivation > &deriver, Buf &finishedMacKey, const Buf &finishedTranscript)
void setUpAcceptingEarlyData()
Actions actions(Args &&...act)
#define EXPECT_TRUE(condition)
#define ON_CALL(obj, call)
#define ASSERT_THAT(value, matcher)
Buf encode(TokenBindingMessage &&message)
TEST_F(RSAPSSTest, TestSignVerify)
std::shared_ptr< MockCookieCipher > mockCookieCipher_
static const std::string nst
static HelloRetryRequest helloRetryRequest()
ExtensionType extension_type
MockEncryptedWriteRecordLayer * appWrite_
std::unique_ptr< folly::IOBuf > Buf
std::shared_ptr< MockSelfCert > cert_
folly::Optional< NamedGroup > group
#define EXPECT_CALL(obj, call)
static EncryptedExtensions encryptedExtEarly()
const internal::AnythingMatcher _
decltype(auto) variant_match(Variant &&variant, Cases &&...cases)
std::shared_ptr< FizzServerContext > context_
std::vector< ProtocolName > protocol_name_list
std::vector< PskIdentity > identities
static KeyUpdate keyUpdate(bool reqUpdate)
#define EXPECT_FALSE(condition)
std::shared_ptr< const Cert > serverCert
Extension encodeExtension(const TokenBindingParameters ¶ms)
Range< const char * > StringPiece
#define EXPECT_LT(val1, val2)
Actions processEvent(const State &state, Param param)
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
MockEncryptedReadRecordLayer * appRead_
void setUpExpectingFinished()
void setUpExpectingCertificate()
internal::ElementsAreMatcher< ::testing::tuple<> > ElementsAre()
std::shared_ptr< MockServerExtensions > extensions_
static ClientHello clientHello()
bool check(const dynamic &schema, const dynamic &value, bool check=true)
Future< typename std::decay< T >::type > makeFuture(T &&t)
static std::unique_ptr< IOBuf > getEncryptedHandshakeWrite(EncryptedExtensions encryptedExt, Finished finished)
constexpr ProtocolVersion TestProtocolVersion
static ClientHello clientHelloPskEarly()
std::vector< CertificateCompressionAlgorithm > algorithms
void setUpAcceptingData()
internal::ReturnAction< R > Return(R value)
MockHandshakeContext * mockHandshakeContext_
#define EXPECT_GT(val1, val2)
std::shared_ptr< MockPeerCert > clientLeafCert_