Skip to content

Commit

Permalink
Bug 1385917 - Part 2: Add alternate handshake support to tstclnt. r=mt
Browse files Browse the repository at this point in the history
Reviewers: mt

Differential Revision: https://nss-review.dev.mozaws.net/D389
  • Loading branch information
ekr committed Aug 4, 2017
1 parent 579eaed commit 4fe258d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
22 changes: 21 additions & 1 deletion cmd/tstclnt/tstclnt.c
Expand Up @@ -31,6 +31,7 @@
#include "ocsp.h"
#include "ssl.h"
#include "sslproto.h"
#include "sslexp.h"
#include "pk11func.h"
#include "secmod.h"
#include "plgetopt.h"
Expand Down Expand Up @@ -251,6 +252,7 @@ PrintParameterUsage(void)
"%-20s The following values are valid:\n"
"%-20s P256, P384, P521, x25519, FF2048, FF3072, FF4096, FF6144, FF8192\n",
"-I", "", "");
fprintf(stderr, "%-20s Enable alternate content type for TLS 1.3 ServerHello\n", "-X alt-server-hello");
}

static void
Expand Down Expand Up @@ -914,6 +916,7 @@ char *requestString = NULL;
PRInt32 requestStringLen = 0;
PRBool requestSent = PR_FALSE;
PRBool enableZeroRtt = PR_FALSE;
PRBool enableAltServerHello = PR_FALSE;

static int
writeBytesToServer(PRFileDesc *s, const char *buf, int nb)
Expand Down Expand Up @@ -1178,6 +1181,16 @@ run_client(void)
}
}

/* Alternate ServerHello content type (TLS 1.3 only) */
if (enableAltServerHello) {
rv = SSL_UseAltServerHelloType(s, PR_TRUE);
if (rv != SECSuccess) {
SECU_PrintError(progName, "error enabling alternate ServerHello type");
error = 1;
goto done;
}
}

/* require the use of fixed finite-field DH groups */
if (requireDHNamedGroups) {
rv = SSL_OptionSet(s, SSL_REQUIRE_DH_NAMED_GROUPS, PR_TRUE);
Expand Down Expand Up @@ -1512,7 +1525,7 @@ main(int argc, char **argv)
/* XXX: 'B' was used in the past but removed in 3.28,
* please leave some time before resuing it. */
optstate = PL_CreateOptState(argc, argv,
"46A:CDFGHI:KL:M:OR:STUV:W:YZa:bc:d:fgh:m:n:op:qr:st:uvw:z");
"46A:CDFGHI:KL:M:OR:STUV:W:X:YZa:bc:d:fgh:m:n:op:qr:st:uvw:z");
while ((optstatus = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
switch (optstate->option) {
case '?':
Expand Down Expand Up @@ -1618,6 +1631,13 @@ main(int argc, char **argv)
}
break;

case 'X':
if (!strcmp(optstate->value, "alt-server-hello")) {
enableAltServerHello = PR_TRUE;
} else {
Usage(progName);
}
break;
case 'Y':
PrintCipherUsage(progName);
exit(0);
Expand Down
21 changes: 16 additions & 5 deletions gtests/ssl_gtest/ssl_loopback_unittest.cc
Expand Up @@ -6,10 +6,12 @@

#include <functional>
#include <memory>
#include <vector>
#include "secerr.h"
#include "ssl.h"
#include "sslerr.h"
#include "sslproto.h"
#include "ssl3prot.h"

extern "C" {
// This is not something that should make you happy.
Expand Down Expand Up @@ -342,12 +344,21 @@ TEST_F(TlsConnectStreamTls13, ServerAltHandshakeType) {
TEST_F(TlsConnectStreamTls13, BothAltHandshakeType) {
client_->SetAltHandshakeTypeEnabled();
server_->SetAltHandshakeTypeEnabled();
auto filter = std::make_shared<TlsHeaderRecorder>();
server_->SetTlsRecordFilter(filter);
filter->EnableDecryption();
auto header_filter = std::make_shared<TlsHeaderRecorder>();
auto sh_filter = std::make_shared<TlsInspectorRecordHandshakeMessage>(
kTlsHandshakeServerHello);
std::vector<std::shared_ptr<PacketFilter>> filters = {header_filter,
sh_filter};
auto chained = std::make_shared<ChainedPacketFilter>(filters);
server_->SetPacketFilter(chained);
header_filter->SetAgent(server_.get());
header_filter->EnableDecryption();
Connect();
ASSERT_EQ(kTlsAltHandshakeType, filter->header(0)->content_type());
ASSERT_EQ(kTlsHandshakeType, filter->header(1)->content_type());
ASSERT_EQ(kTlsAltHandshakeType, header_filter->header(0)->content_type());
ASSERT_EQ(kTlsHandshakeType, header_filter->header(1)->content_type());
uint32_t ver;
ASSERT_TRUE(sh_filter->buffer().Read(0, 2, &ver));
ASSERT_EQ((uint32_t)(0x7a00 | TLS_1_3_DRAFT_VERSION), ver);
}

INSTANTIATE_TEST_CASE_P(
Expand Down
5 changes: 3 additions & 2 deletions lib/ssl/ssl3con.c
Expand Up @@ -1090,7 +1090,8 @@ ssl_ClientReadVersion(sslSocket *ss, PRUint8 **b, unsigned int *len,
PORT_SetError(SSL_ERROR_UNSUPPORTED_VERSION);
return SECFailure;
}
if (temp == tls13_EncodeDraftVersion(SSL_LIBRARY_VERSION_TLS_1_3)) {
if (temp == tls13_EncodeDraftVersion(SSL_LIBRARY_VERSION_TLS_1_3) || (ss->opt.enableAltHandshaketype &&
(temp == tls13_EncodeAltDraftVersion(SSL_LIBRARY_VERSION_TLS_1_3)))) {
v = SSL_LIBRARY_VERSION_TLS_1_3;
} else {
v = (SSL3ProtocolVersion)temp;
Expand Down Expand Up @@ -9327,7 +9328,7 @@ ssl3_SendServerHello(sslSocket *ss)
if (IS_DTLS(ss) && ss->version < SSL_LIBRARY_VERSION_TLS_1_3) {
version = dtls_TLSVersionToDTLSVersion(ss->version);
} else {
version = tls13_EncodeDraftVersion(ss->version);
version = ss->ssl3.hs.altHandshakeType ? tls13_EncodeAltDraftVersion(ss->version) : tls13_EncodeDraftVersion(ss->version);
}

rv = ssl3_AppendHandshakeNumber(ss, version, 2);
Expand Down

0 comments on commit 4fe258d

Please sign in to comment.