forked from getlantern/tlsresumption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsresumption.go
112 lines (97 loc) · 3.08 KB
/
tlsresumption.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
// Package tlsresumption provides utilities for implementing out of band sharing of client session
// states for tls session resumption.
package tlsresumption
import (
"encoding/base64"
"encoding/json"
"net"
"github.com/getlantern/errors"
"github.com/getlantern/golog"
utls "github.com/refraction-networking/utls"
)
var (
log = golog.LoggerFor("tlsresumption")
)
// MakeClientSessionStates makes num client session states for connecting to the TLS server at the given
// address. It connections to the server and handshakes num times and returns as many client session states
// as it can successfully build, returning the latest error.
//
// Note, this does not verify the server's certificate so is potentially susceptible to MITM attacks.
func MakeClientSessionStates(addr string, num int) ([]string, error) {
result := make([]string, 0, num)
var finalErr error
for i := 0; i < num; i++ {
conn, err := dialUTLS(nil, addr)
if err != nil {
finalErr = err
continue
}
ss := conn.HandshakeState.Session
conn.Close()
if ss == nil {
finalErr = errors.New("no client session state found")
continue
}
ssString, err := SerializeClientSessionState(ss)
if err != nil {
finalErr = err
continue
}
result = append(result, ssString)
}
return result, finalErr
}
// ParseClientSessionState parses the serialized client session state into a utls.ClientSessionState
func ParseClientSessionState(serialized string) (*utls.ClientSessionState, error) {
b, err := base64.StdEncoding.DecodeString(serialized)
if err != nil {
return nil, errors.New("unable to base64 decode serialized client session state: %v", err)
}
sss := &serializedClientSessionState{}
err = json.Unmarshal(b, sss)
if err != nil {
return nil, err
}
ss := &utls.ClientSessionState{}
ss.SetSessionTicket(sss.SessionTicket)
ss.SetVers(sss.Vers)
ss.SetCipherSuite(sss.CipherSuite)
ss.SetMasterSecret(sss.MasterSecret)
return ss, nil
}
func dialUTLS(ss *utls.ClientSessionState, addr string) (*utls.UConn, error) {
cfg := &utls.Config{
InsecureSkipVerify: true,
}
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, errors.New("unable to dial %v: %v", addr, err)
}
uconn := utls.UClient(conn, cfg, utls.HelloChrome_Auto)
uconn.SetSessionState(ss)
if handshakeErr := uconn.Handshake(); handshakeErr != nil {
conn.Close()
return nil, errors.New("error handshaking with %v: %v", addr, handshakeErr)
}
return uconn, nil
}
type serializedClientSessionState struct {
SessionTicket []uint8
Vers uint16
CipherSuite uint16
MasterSecret []byte
}
// SerializeClientSessionState serializes a ClientSessionState into a string representation of the same.
func SerializeClientSessionState(ss *utls.ClientSessionState) (string, error) {
sss := &serializedClientSessionState{
SessionTicket: ss.SessionTicket(),
Vers: ss.Vers(),
CipherSuite: ss.CipherSuite(),
MasterSecret: ss.MasterSecret(),
}
b, err := json.Marshal(sss)
if err != nil {
return "", errors.New("unable to marshal client session state to JSON: %v", err)
}
return base64.StdEncoding.EncodeToString(b), nil
}