forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_tls_test.go
172 lines (159 loc) · 7.02 KB
/
server_tls_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package quic
import (
"bytes"
"io"
"github.com/bifurcation/mint"
"github.com/lucas-clemente/quic-go/internal/crypto"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/mocks"
"github.com/lucas-clemente/quic-go/internal/mocks/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/testdata"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/internal/wire"
"github.com/lucas-clemente/quic-go/qerr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Stateless TLS handling", func() {
var (
conn *mockPacketConn
server *serverTLS
sessionChan <-chan tlsSession
mintTLS *mockhandshake.MockMintTLS
extHandler *mocks.MockTLSExtensionHandler
mintReply io.Writer
)
BeforeEach(func() {
mintTLS = mockhandshake.NewMockMintTLS(mockCtrl)
extHandler = mocks.NewMockTLSExtensionHandler(mockCtrl)
conn = newMockPacketConn()
config := &Config{
Versions: []protocol.VersionNumber{protocol.VersionTLS},
}
var err error
server, sessionChan, err = newServerTLS(conn, config, nil, testdata.GetTLSConfig(), utils.DefaultLogger)
Expect(err).ToNot(HaveOccurred())
server.newMintConn = func(bc *handshake.CryptoStreamConn, v protocol.VersionNumber) (handshake.MintTLS, <-chan handshake.TransportParameters, error) {
mintReply = bc
return mintTLS, extHandler.GetPeerParams(), nil
}
})
getPacket := func(f wire.Frame) (*wire.Header, []byte) {
hdrBuf := &bytes.Buffer{}
hdr := &wire.Header{
IsLongHeader: true,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{8, 7, 6, 5, 4, 3, 2, 1},
PacketNumber: 1,
Version: protocol.VersionTLS,
}
err := hdr.Write(hdrBuf, protocol.PerspectiveClient, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
hdr.Raw = hdrBuf.Bytes()
aead, err := crypto.NewNullAEAD(protocol.PerspectiveClient, hdr.DestConnectionID, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
buf := &bytes.Buffer{}
err = f.Write(buf, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
// pad the packet such that is has exactly the required minimum size
buf.Write(bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize-len(hdr.Raw)-aead.Overhead()-buf.Len()))
data := aead.Seal(nil, buf.Bytes(), 1, hdr.Raw)
Expect(len(hdr.Raw) + len(data)).To(Equal(protocol.MinInitialPacketSize))
return hdr, data
}
unpackPacket := func(data []byte) (*wire.Header, []byte) {
r := bytes.NewReader(conn.dataWritten.Bytes())
hdr, err := wire.ParseHeaderSentByServer(r, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
hdr.Raw = data[:len(data)-r.Len()]
aead, err := crypto.NewNullAEAD(protocol.PerspectiveClient, hdr.SrcConnectionID, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
payload, err := aead.Open(nil, data[len(data)-r.Len():], hdr.PacketNumber, hdr.Raw)
Expect(err).ToNot(HaveOccurred())
return hdr, payload
}
It("sends a version negotiation packet if it doesn't support the version", func() {
hdr := &wire.Header{
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8},
Version: 0x1337,
}
server.HandleInitial(nil, hdr, bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize))
Expect(conn.dataWritten.Len()).ToNot(BeZero())
hdr, err := wire.ParseHeaderSentByServer(bytes.NewReader(conn.dataWritten.Bytes()), protocol.VersionUnknown)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsVersionNegotiation).To(BeTrue())
Expect(sessionChan).ToNot(Receive())
})
It("drops too small packets", func() {
hdr, data := getPacket(&wire.StreamFrame{Data: []byte("Client Hello")})
data = data[:len(data)-1] // the packet is now 1 byte too small
server.HandleInitial(nil, hdr, data)
Expect(conn.dataWritten.Len()).To(BeZero())
})
It("ignores packets with invalid contents", func() {
hdr, data := getPacket(&wire.StreamFrame{StreamID: 10, Offset: 11, Data: []byte("foobar")})
server.HandleInitial(nil, hdr, data)
Expect(conn.dataWritten.Len()).To(BeZero())
Expect(sessionChan).ToNot(Receive())
})
It("replies with a Retry packet, if a Cookie is required", func() {
extHandler.EXPECT().GetPeerParams()
mintTLS.EXPECT().Handshake().Return(mint.AlertStatelessRetry).Do(func() {
mintReply.Write([]byte("Retry with this Cookie"))
})
hdr, data := getPacket(&wire.StreamFrame{Data: []byte("Client Hello")})
server.HandleInitial(nil, hdr, data)
Expect(conn.dataWritten.Len()).ToNot(BeZero())
r := bytes.NewReader(conn.dataWritten.Bytes())
replyHdr, err := wire.ParseHeaderSentByServer(r, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
Expect(replyHdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(replyHdr.SrcConnectionID).To(Equal(hdr.DestConnectionID))
Expect(replyHdr.DestConnectionID).To(Equal(hdr.SrcConnectionID))
Expect(replyHdr.PayloadLen).To(BeEquivalentTo(r.Len()))
Expect(sessionChan).ToNot(Receive())
})
It("replies with a Handshake packet and creates a session, if no Cookie is required", func() {
mintTLS.EXPECT().Handshake().Return(mint.AlertNoAlert).Do(func() {
mintReply.Write([]byte("Server Hello"))
})
mintTLS.EXPECT().Handshake().Return(mint.AlertNoAlert)
mintTLS.EXPECT().State().Return(mint.StateServerNegotiated)
mintTLS.EXPECT().State().Return(mint.StateServerWaitFlight2)
paramsChan := make(chan handshake.TransportParameters, 1)
paramsChan <- handshake.TransportParameters{}
extHandler.EXPECT().GetPeerParams().Return(paramsChan)
hdr, data := getPacket(&wire.StreamFrame{Data: []byte("Client Hello")})
done := make(chan struct{})
go func() {
defer GinkgoRecover()
server.HandleInitial(nil, hdr, data)
// the Handshake packet is written by the session
Expect(conn.dataWritten.Len()).To(BeZero())
close(done)
}()
Eventually(sessionChan).Should(Receive())
Eventually(done).Should(BeClosed())
})
It("sends a CONNECTION_CLOSE, if mint returns an error", func() {
mintTLS.EXPECT().Handshake().Return(mint.AlertAccessDenied)
extHandler.EXPECT().GetPeerParams()
hdr, data := getPacket(&wire.StreamFrame{Data: []byte("Client Hello")})
server.HandleInitial(nil, hdr, data)
// the Handshake packet is written by the session
Expect(conn.dataWritten.Bytes()).ToNot(BeEmpty())
// unpack the packet to check that it actually contains a CONNECTION_CLOSE
replyHdr, data := unpackPacket(conn.dataWritten.Bytes())
Expect(replyHdr.Type).To(Equal(protocol.PacketTypeHandshake))
Expect(replyHdr.SrcConnectionID).To(Equal(hdr.DestConnectionID))
Expect(replyHdr.DestConnectionID).To(Equal(hdr.SrcConnectionID))
frame, err := wire.ParseNextFrame(bytes.NewReader(data), nil, protocol.VersionTLS)
Expect(err).ToNot(HaveOccurred())
Expect(frame).To(BeAssignableToTypeOf(&wire.ConnectionCloseFrame{}))
ccf := frame.(*wire.ConnectionCloseFrame)
Expect(ccf.ErrorCode).To(Equal(qerr.HandshakeFailed))
Expect(ccf.ReasonPhrase).To(Equal(mint.AlertAccessDenied.String()))
})
})