Skip to content

Commit 0473adf

Browse files
xiaokangwangstv0g
authored andcommittedFeb 3, 2023
Add SkipHelloVerify option to dTLS
This is a common behavior for WebRTC Peer on browser stack where DoS resistance on DTLS level is redundant as this is built into ICE.
1 parent 11ea8c2 commit 0473adf

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed
 

‎config.go

+5
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ type Config struct {
168168
// the server. If this is unacceptable to the server then it may abort
169169
// the handshake.
170170
GetClientCertificate func(*CertificateRequestInfo) (*tls.Certificate, error)
171+
172+
// InsecureSkipVerifyHello, if true and when acting as server, allow client to
173+
// skip hello verify phase and receive ServerHello after initial ClientHello.
174+
// This have implication on DoS attack resistance.
175+
InsecureSkipVerifyHello bool
171176
}
172177

173178
func defaultConnectContextMaker() (context.Context, func()) {

‎conn.go

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func createConn(ctx context.Context, nextConn net.Conn, config *Config, isClient
184184
ellipticCurves: curves,
185185
localGetCertificate: config.GetCertificate,
186186
localGetClientCertificate: config.GetClientCertificate,
187+
insecureSkipHelloVerify: config.InsecureSkipVerifyHello,
187188
}
188189

189190
// rfc5246#section-7.4.3

‎conn_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/pion/dtls/v2/pkg/protocol/extension"
3434
"github.com/pion/dtls/v2/pkg/protocol/handshake"
3535
"github.com/pion/dtls/v2/pkg/protocol/recordlayer"
36+
"github.com/pion/logging"
3637
"github.com/pion/transport/v2/test"
3738
)
3839

@@ -2914,3 +2915,58 @@ func TestEllipticCurveConfiguration(t *testing.T) {
29142915
}()
29152916
}
29162917
}
2918+
2919+
func TestSkipHelloVerify(t *testing.T) {
2920+
report := test.CheckRoutines(t)
2921+
defer report()
2922+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2923+
defer cancel()
2924+
2925+
ca, cb := dpipe.Pipe()
2926+
certificate, err := selfsign.GenerateSelfSigned()
2927+
if err != nil {
2928+
t.Fatal(err)
2929+
}
2930+
gotHello := make(chan struct{})
2931+
2932+
go func() {
2933+
server, sErr := testServer(ctx, cb, &Config{
2934+
Certificates: []tls.Certificate{certificate},
2935+
LoggerFactory: logging.NewDefaultLoggerFactory(),
2936+
InsecureSkipVerifyHello: true,
2937+
}, false)
2938+
if sErr != nil {
2939+
t.Error(sErr)
2940+
return
2941+
}
2942+
buf := make([]byte, 1024)
2943+
if _, sErr = server.Read(buf); sErr != nil {
2944+
t.Error(sErr)
2945+
}
2946+
gotHello <- struct{}{}
2947+
if sErr = server.Close(); sErr != nil { //nolint:contextcheck
2948+
t.Error(sErr)
2949+
}
2950+
}()
2951+
2952+
client, err := testClient(ctx, ca, &Config{
2953+
LoggerFactory: logging.NewDefaultLoggerFactory(),
2954+
InsecureSkipVerify: true,
2955+
}, false)
2956+
if err != nil {
2957+
t.Fatal(err)
2958+
}
2959+
if _, err = client.Write([]byte("hello")); err != nil {
2960+
t.Error(err)
2961+
}
2962+
select {
2963+
case <-gotHello:
2964+
// OK
2965+
case <-time.After(time.Second * 5):
2966+
t.Error("timeout")
2967+
}
2968+
2969+
if err = client.Close(); err != nil {
2970+
t.Error(err)
2971+
}
2972+
}

‎flight0handler.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ func flight0Parse(ctx context.Context, c flightConn, state *State, cache *handsh
8181
}
8282
}
8383

84-
return handleHelloResume(clientHello.SessionID, state, cfg, flight2)
84+
nextFlight := flight2
85+
86+
if cfg.insecureSkipHelloVerify {
87+
nextFlight = flight4
88+
}
89+
90+
return handleHelloResume(clientHello.SessionID, state, cfg, nextFlight)
8591
}
8692

8793
func handleHelloResume(sessionID []byte, state *State, cfg *handshakeConfig, next flightVal) (flightVal, *alert.Alert, error) {
@@ -109,9 +115,11 @@ func handleHelloResume(sessionID []byte, state *State, cfg *handshakeConfig, nex
109115

110116
func flight0Generate(c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) ([]*packet, *alert.Alert, error) {
111117
// Initialize
112-
state.cookie = make([]byte, cookieLength)
113-
if _, err := rand.Read(state.cookie); err != nil {
114-
return nil, nil, err
118+
if !cfg.insecureSkipHelloVerify {
119+
state.cookie = make([]byte, cookieLength)
120+
if _, err := rand.Read(state.cookie); err != nil {
121+
return nil, nil, err
122+
}
115123
}
116124

117125
var zeroEpoch uint16

‎handshaker.go

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type handshakeConfig struct {
109109
retransmitInterval time.Duration
110110
customCipherSuites func() []CipherSuite
111111
ellipticCurves []elliptic.Curve
112+
insecureSkipHelloVerify bool
112113

113114
onFlightState func(flightVal, handshakeState)
114115
log logging.LeveledLogger

0 commit comments

Comments
 (0)
Please sign in to comment.