Skip to content

Commit a5d1fac

Browse files
SnawootSean-Der
authored andcommitted
Flight3: respect curves configuration
Reuse logic from Flight1 to ensure correct behaviour. Signed-off-by: Vladislav Yarmak <vladislav-ex-src@vm-0.com>
1 parent 61b3466 commit a5d1fac

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

flight3handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ func flight3Generate(_ flightConn, state *State, _ *handshakeCache, cfg *handsha
249249
RenegotiatedConnection: 0,
250250
},
251251
}
252+
252253
if state.namedCurve != 0 {
253254
extensions = append(extensions, []extension.Extension{
254255
&extension.SupportedEllipticCurves{
255-
EllipticCurves: []elliptic.Curve{elliptic.X25519, elliptic.P256, elliptic.P384},
256+
EllipticCurves: cfg.ellipticCurves,
256257
},
257258
&extension.SupportedPointFormats{
258259
PointFormats: []elliptic.CurvePointFormat{elliptic.CurvePointFormatUncompressed},

flight3handler_test.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
package dtls
5+
6+
import (
7+
"context"
8+
"math/rand"
9+
"testing"
10+
"time"
11+
12+
"github.com/pion/dtls/v2/pkg/crypto/elliptic"
13+
dtlsnet "github.com/pion/dtls/v2/pkg/net"
14+
"github.com/pion/dtls/v2/pkg/protocol/extension"
15+
"github.com/pion/dtls/v2/pkg/protocol/handshake"
16+
"github.com/pion/dtls/v2/pkg/protocol/recordlayer"
17+
"github.com/pion/transport/v3/dpipe"
18+
"github.com/pion/transport/v3/test"
19+
)
20+
21+
// Assert that SupportedEllipticCurves is only sent when a ECC CipherSuite is available
22+
func TestSupportedEllipticCurves(t *testing.T) {
23+
// Limit runtime in case of deadlocks
24+
lim := test.TimeOut(time.Second * 20)
25+
defer lim.Stop()
26+
27+
// Check for leaking routines
28+
report := test.CheckRoutines(t)
29+
defer report()
30+
31+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
32+
defer cancel()
33+
34+
expectedCurves := defaultCurves
35+
var actualCurves []elliptic.Curve
36+
37+
rand.Shuffle(len(expectedCurves), func(i, j int) {
38+
expectedCurves[i], expectedCurves[j] = expectedCurves[j], expectedCurves[i]
39+
})
40+
41+
clientErr := make(chan error, 1)
42+
ca, cb := dpipe.Pipe()
43+
caAnalyzer := &connWithCallback{Conn: ca}
44+
caAnalyzer.onWrite = func(in []byte) {
45+
messages, err := recordlayer.UnpackDatagram(in)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
for i := range messages {
51+
h := &handshake.Handshake{}
52+
_ = h.Unmarshal(messages[i][recordlayer.FixedHeaderSize:])
53+
54+
if h.Header.Type == handshake.TypeClientHello {
55+
clientHello := &handshake.MessageClientHello{}
56+
msg, err := h.Message.Marshal()
57+
58+
if err != nil {
59+
t.Fatal(err)
60+
} else if err = clientHello.Unmarshal(msg); err != nil {
61+
t.Fatal(err)
62+
}
63+
64+
for _, e := range clientHello.Extensions {
65+
if e.TypeValue() == extension.SupportedEllipticCurvesTypeValue {
66+
if c, ok := e.(*extension.SupportedEllipticCurves); ok {
67+
actualCurves = c.EllipticCurves
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
75+
go func() {
76+
conf := &Config{
77+
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
78+
EllipticCurves: expectedCurves,
79+
}
80+
81+
if client, err := testClient(ctx, dtlsnet.PacketConnFromConn(caAnalyzer), caAnalyzer.RemoteAddr(), conf, false); err != nil {
82+
clientErr <- err
83+
} else {
84+
clientErr <- client.Close() //nolint
85+
}
86+
}()
87+
88+
config := &Config{
89+
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
90+
}
91+
92+
if server, err := testServer(ctx, dtlsnet.PacketConnFromConn(cb), cb.RemoteAddr(), config, true); err != nil {
93+
t.Fatalf("Server error %v", err)
94+
} else {
95+
if err = server.Close(); err != nil {
96+
t.Fatal(err)
97+
}
98+
}
99+
100+
if err := <-clientErr; err != nil {
101+
t.Fatalf("Client error %v", err)
102+
}
103+
104+
for i := range expectedCurves {
105+
if expectedCurves[i] != actualCurves[i] {
106+
t.Fatal("List of curves in SupportedEllipticCurves does not match config")
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)