-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsmasq_impl.go
199 lines (179 loc) · 6.08 KB
/
tlsmasq_impl.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package chained
import (
"context"
stls "crypto/tls"
"encoding/binary"
"encoding/hex"
stderrors "errors"
"net"
"strings"
"sync"
"time"
tls "github.com/refraction-networking/utls"
"github.com/getlantern/common/config"
"github.com/getlantern/errors"
"github.com/getlantern/flashlight/v7/common"
"github.com/getlantern/flashlight/v7/ops"
"github.com/getlantern/hellosplitter"
"github.com/getlantern/netx"
"github.com/getlantern/tlsmasq"
"github.com/getlantern/tlsmasq/ptlshs"
"github.com/getlantern/tlsutil"
)
type tlsMasqImpl struct {
nopCloser
reportDialCore reportDialCoreFn
addr string
cfg tlsmasq.DialerConfig
tlsClientHelloSplitting bool
}
func newTLSMasqImpl(configDir, name, addr string, pc *config.ProxyConfig, uc common.UserConfig, reportDialCore reportDialCoreFn) (proxyImpl, error) {
const timeout = 5 * time.Second
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
suites, err := cipherSuites(ptSetting(pc, "tlsmasq_suites"), name)
if err != nil {
return nil, errors.New("could not parse suites for: %v", name)
}
versStr := ptSetting(pc, "tlsmasq_tlsminversion")
minVersion, err := decodeUint16(versStr)
if err != nil {
return nil, errors.New("bad TLS version string '%s': %v", versStr, err)
}
secretString := ptSetting(pc, "tlsmasq_secret")
secretBytes, err := hex.DecodeString(strings.TrimPrefix(secretString, "0x"))
if err != nil {
return nil, errors.New("bad server-secret string '%s': %v", secretString, err)
}
secret := ptlshs.Secret{}
if len(secretBytes) != len(secret) {
return nil, errors.New("expected %d-byte secret string, got %d bytes", len(secret), len(secretBytes))
}
copy(secret[:], secretBytes)
masqSNI := ptSetting(pc, "tlsmasq_sni")
if masqSNI == "" {
return nil, errors.New("server name indicator must be configured")
}
// It's okay if this is unset - it'll just result in us using the default.
nonceTTL := time.Duration(ptSettingInt(pc, "tlsmasq_noncettl"))
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, errors.New("malformed server address: %v", err)
}
proxyTLS, hellos, err := tlsConfigForProxy(ctx, configDir, name, pc, uc)
if err != nil {
return nil, errors.New("error generating TLS config: %v", err)
}
// For tlsmasq proxies, the TLS config we generated in the previous step is the config used in
// the initial handshake with the masquerade origin. Thus we need to make some modifications.
outerTLS := tls.Config{
CipherSuites: proxyTLS.CipherSuites,
KeyLogWriter: proxyTLS.KeyLogWriter,
ServerName: masqSNI,
InsecureSkipVerify: InsecureSkipVerifyTLSMasqOrigin,
}
cfg := tlsmasq.DialerConfig{
ProxiedHandshakeConfig: ptlshs.DialerConfig{
Handshaker: &utlsHandshaker{&outerTLS, &helloRoller{hellos: hellos}, sync.Mutex{}},
Secret: secret,
NonceTTL: nonceTTL,
},
// This is the config used for the second, wrapped handshake with the proxy itself.
TLSConfig: &stls.Config{
MinVersion: minVersion,
CipherSuites: suites,
// Proxy certificates are valid for the host (usually their IP address).
ServerName: host,
InsecureSkipVerify: proxyTLS.InsecureSkipVerify,
VerifyPeerCertificate: proxyTLS.VerifyPeerCertificate,
RootCAs: proxyTLS.RootCAs,
KeyLogWriter: proxyTLS.KeyLogWriter,
},
}
return &tlsMasqImpl{reportDialCore: reportDialCore, addr: addr, cfg: cfg, tlsClientHelloSplitting: pc.TLSClientHelloSplitting}, nil
}
func decodeUint16(s string) (uint16, error) {
b, err := hex.DecodeString(strings.TrimPrefix(s, "0x"))
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
func cipherSuites(cipherSuites, name string) ([]uint16, error) {
suiteStrings := strings.Split(cipherSuites, ",")
if len(suiteStrings) == 1 && suiteStrings[0] == "" {
// No suites specified. Setting them to nil will cause the default suites to be used.
log.Debugf("No suites specified, using default suites for %s", name)
return nil, nil
}
suites := []uint16{}
for _, s := range suiteStrings {
suite, err := decodeUint16(s)
if err != nil {
return nil, errors.New("bad cipher string '%s': %v", s, err)
}
suites = append(suites, suite)
}
return suites, nil
}
func (impl *tlsMasqImpl) dialServer(op *ops.Op, ctx context.Context) (net.Conn, error) {
tcpConn, err := impl.reportDialCore(op, func() (net.Conn, error) {
return netx.DialContext(ctx, "tcp", impl.addr)
})
if err != nil {
return nil, err
}
if impl.tlsClientHelloSplitting {
tcpConn = hellosplitter.Wrap(tcpConn, splitClientHello)
}
conn := tlsmasq.Client(tcpConn, impl.cfg)
// We execute the handshake as part of the dial. Otherwise, preconnecting wouldn't do
// much for us.
errc := make(chan error, 1)
go func() { errc <- conn.Handshake() }()
select {
case err := <-errc:
if err != nil {
conn.Close()
var alertErr tlsutil.UnexpectedAlertError
if stderrors.As(err, &alertErr) {
log.Debugf("received alert from origin in tlsmasq handshake: %v", alertErr.Alert)
}
return nil, errors.New("handshake failed: %v", err)
}
return conn, nil
case <-ctx.Done():
conn.Close()
return nil, ctx.Err()
}
}
// utlsHandshaker implements tlsmasq/ptlshs.Handshaker. This allows us to parrot browsers like
// Chrome in our handshakes with tlsmasq origins.
type utlsHandshaker struct {
cfg *tls.Config
roller *helloRoller
sync.Mutex
}
func (h *utlsHandshaker) Handshake(conn net.Conn) (*ptlshs.HandshakeResult, error) {
r := h.roller.getCopy()
defer h.roller.updateTo(r)
currentHello := r.current()
uconn, err := currentHello.uconn(conn, h.cfg.Clone())
if err != nil {
// An error from helloSpec.uconn implies an invalid hello.
log.Debugf("invalid custom hello; advancing roller: %v", err)
r.advance()
return nil, err
}
if err = uconn.Handshake(); err != nil {
if isHelloErr(err) {
log.Debugf("got error likely related to bad hello; advancing roller: %v", err)
r.advance()
}
return nil, err
}
return &ptlshs.HandshakeResult{
Version: uconn.ConnectionState().Version,
CipherSuite: uconn.ConnectionState().CipherSuite,
}, nil
}