-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlsresumption_test.go
134 lines (115 loc) · 2.87 KB
/
tlsresumption_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
package tlsresumption
import (
"crypto/rand"
"crypto/tls"
"io"
"net"
"sync"
"testing"
"time"
"github.com/getlantern/errors"
"github.com/getlantern/keyman"
utls "github.com/refraction-networking/utls"
"github.com/stretchr/testify/assert"
)
var (
sessionTicketKey = makeSessionTicketKey()
text = []byte("Hello World")
)
func makeSessionTicketKey() [32]byte {
var b [32]byte
rand.Read(b[:])
return b
}
func TestMakeClientSecrets(t *testing.T) {
pk, err := keyman.GeneratePK(2048)
if !assert.NoError(t, err) {
return
}
cert, err := pk.TLSCertificateFor(time.Now().Add(1*time.Hour), true, nil, "tlsresumptiontest", "localhost")
if !assert.NoError(t, err) {
return
}
log.Debug("Getting session state from a temporary server")
ss, err := getSessionState(pk, cert)
if !assert.NoError(t, err) {
return
}
log.Debug("Starting actual server")
addr := "localhost:0"
l, err := listenTLS(pk, cert, addr)
if !assert.NoError(t, err) {
return
}
defer l.Close()
// Now we can spawn multiple clients that reuse the existing session
numClients := 2
var wg sync.WaitGroup
wg.Add(numClients)
for i := 0; i < numClients; i++ {
go func() {
defer wg.Done()
log.Debugf("Dialing %v", l.Addr().String())
cache := utls.NewLRUClientSessionCache(1)
conn, err := dialUTLS(cache, ss, l.Addr().String())
if !assert.NoError(t, err) {
return
}
css, ok := cache.Get(l.Addr().String())
if !assert.True(t, ok) {
return
}
defer conn.Close()
assert.EqualValues(t, ss.MasterSecret(), css.MasterSecret(), "New connection should reuse client session state")
assert.True(t, conn.ConnectionState().DidResume, "New connection should resume")
_, err = conn.Write(text)
if !assert.NoError(t, err) {
return
}
b := make([]byte, len(text))
_, err = io.ReadFull(conn, b)
if !assert.NoError(t, err) {
return
}
log.Debugf("Read: %v", string(b))
}()
}
wg.Wait()
}
func getSessionState(pk *keyman.PrivateKey, cert *keyman.Certificate) (*utls.ClientSessionState, error) {
// Listen on random port
l, err := listenTLS(pk, cert, "localhost:0")
if err != nil {
return nil, errors.New("Unable to listen on random port: %v", err)
}
defer l.Close()
ssStrings, err := MakeClientSessionStates(l.Addr().String(), 1)
if err != nil {
return nil, err
}
return ParseClientSessionState(ssStrings[0])
}
func listenTLS(pk *keyman.PrivateKey, cert *keyman.Certificate, addr string) (net.Listener, error) {
kp, err := tls.X509KeyPair(cert.PEMEncoded(), pk.PEMEncoded())
if err != nil {
return nil, err
}
l, err := tls.Listen("tcp", addr, &tls.Config{
Certificates: []tls.Certificate{kp},
SessionTicketKey: sessionTicketKey,
MaxVersion: tls.VersionTLS12,
})
if err == nil {
go func() {
for {
conn, err := l.Accept()
if err == nil {
go func() {
io.Copy(conn, conn)
}()
}
}
}()
}
return l, err
}