/
tlsTunnel.go
256 lines (212 loc) · 7.43 KB
/
tlsTunnel.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) 2023, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"net"
"github.com/astaguna/popon-core/psiphon/common"
"github.com/astaguna/popon-core/psiphon/common/errors"
"github.com/astaguna/popon-core/psiphon/common/obfuscator"
"github.com/astaguna/popon-core/psiphon/common/prng"
"github.com/astaguna/popon-core/psiphon/common/protocol"
"github.com/astaguna/popon-core/psiphon/common/values"
tris "github.com/astaguna/tls-tris"
)
// TLSTunnelServer tunnels TCP traffic (in the case of Psiphon, Obfuscated SSH
// traffic) over TLS.
type TLSTunnelServer struct {
support *SupportServices
listener net.Listener
listenerTunnelProtocol string
listenerPort int
passthroughAddress string
tlsConfig *tris.Config
obfuscatorSeedHistory *obfuscator.SeedHistory
}
// ListenTLSTunnel returns the listener of a new TLSTunnelServer.
// Note: the first Read or Write call on a connection returned by the listener
// will trigger the underlying TLS handshake.
func ListenTLSTunnel(
support *SupportServices,
listener net.Listener,
listenerTunnelProtocol string,
listenerPort int,
) (net.Listener, error) {
server, err := NewTLSTunnelServer(support, listener, listenerTunnelProtocol, listenerPort)
if err != nil {
return nil, errors.Trace(err)
}
listener = tris.NewListener(server.listener, server.tlsConfig)
return NewTLSTunnelListener(listener, server), nil
}
// NewTLSTunnelServer initializes a new TLSTunnelServer.
func NewTLSTunnelServer(
support *SupportServices,
listener net.Listener,
listenerTunnelProtocol string,
listenerPort int) (*TLSTunnelServer, error) {
passthroughAddress := support.Config.TunnelProtocolPassthroughAddresses[listenerTunnelProtocol]
tlsServer := &TLSTunnelServer{
support: support,
listener: listener,
listenerTunnelProtocol: listenerTunnelProtocol,
listenerPort: listenerPort,
passthroughAddress: passthroughAddress,
obfuscatorSeedHistory: obfuscator.NewSeedHistory(nil),
}
tlsConfig, err := tlsServer.makeTLSTunnelConfig()
if err != nil {
return nil, errors.Trace(err)
}
tlsServer.tlsConfig = tlsConfig
return tlsServer, nil
}
// makeTLSTunnelConfig creates a TLS config for a TLSTunnelServer listener.
func (server *TLSTunnelServer) makeTLSTunnelConfig() (*tris.Config, error) {
// Limitation: certificate value changes on restart.
certificate, privateKey, err := common.GenerateWebServerCertificate(values.GetHostName())
if err != nil {
return nil, errors.Trace(err)
}
tlsCertificate, err := tris.X509KeyPair(
[]byte(certificate), []byte(privateKey))
if err != nil {
return nil, errors.Trace(err)
}
var minVersion uint16
if protocol.TunnelProtocolUsesTLSOSSH(server.listenerTunnelProtocol) {
// Use min TLS 1.3 so cert is not plaintext on the wire.
minVersion = tris.VersionTLS13
} else {
// Need to support older TLS versions for backwards compatibility.
// Vary the minimum version to frustrate scanning/fingerprinting of unfronted servers.
// Limitation: like the certificate, this value changes on restart.
minVersionCandidates := []uint16{tris.VersionTLS10, tris.VersionTLS11, tris.VersionTLS12}
minVersion = minVersionCandidates[prng.Intn(len(minVersionCandidates))]
}
config := &tris.Config{
Certificates: []tris.Certificate{tlsCertificate},
NextProtos: []string{"http/1.1"},
MinVersion: minVersion,
UseExtendedMasterSecret: true,
}
// When configured, initialize passthrough mode, an anti-probing defense.
// Clients must prove knowledge of the obfuscated key via a message sent in
// the TLS ClientHello random field.
//
// When clients fail to provide a valid message, the client connection is
// relayed to the designated passthrough address, typically another web site.
// The entire flow is relayed, including the original ClientHello, so the
// client will perform a TLS handshake with the passthrough target.
//
// Irregular events are logged for invalid client activity.
if server.passthroughAddress != "" {
config.PassthroughAddress = server.passthroughAddress
config.PassthroughVerifyMessage = func(
message []byte) bool {
return obfuscator.VerifyTLSPassthroughMessage(
true,
// Meek obfuscated key used for legacy reasons. See comment for
// MeekObfuscatedKey.
server.support.Config.MeekObfuscatedKey,
message)
}
config.PassthroughLogInvalidMessage = func(
clientIP string) {
logIrregularTunnel(
server.support,
server.listenerTunnelProtocol,
server.listenerPort,
clientIP,
errors.TraceNew("invalid passthrough message"),
nil)
}
config.PassthroughHistoryAddNew = func(
clientIP string,
clientRandom []byte) bool {
// Use a custom, shorter TTL based on the validity period of the
// passthrough message.
TTL := obfuscator.TLS_PASSTHROUGH_TIME_PERIOD
// strictMode is true as legitimate clients never retry TLS
// connections using a previous random value.
ok, logFields := server.obfuscatorSeedHistory.AddNewWithTTL(
true,
clientIP,
"client-random",
clientRandom,
TTL)
if logFields != nil {
logIrregularTunnel(
server.support,
server.listenerTunnelProtocol,
server.listenerPort,
clientIP,
errors.TraceNew("duplicate passthrough message"),
LogFields(*logFields))
}
return ok
}
}
return config, nil
}
// TLSTunnelListener implements the net.Listener interface. Accept returns a
// net.Conn which implements the common.MetricsSource interface.
type TLSTunnelListener struct {
net.Listener
server *TLSTunnelServer
}
// NewTLSTunnelListener initializes a new TLSTunnelListener.
func NewTLSTunnelListener(listener net.Listener, server *TLSTunnelServer) *TLSTunnelListener {
return &TLSTunnelListener{
Listener: listener,
server: server,
}
}
func (l *TLSTunnelListener) Accept() (net.Conn, error) {
conn, err := l.Listener.Accept()
if err != nil {
return nil, errors.Trace(err)
}
return NewTLSTunnelConn(conn, l.server), nil
}
// TLSTunnelConn implements the net.Conn and common.MetricsSource interfaces.
type TLSTunnelConn struct {
net.Conn
server *TLSTunnelServer
}
// NewTLSTunnelConn initializes a new TLSTunnelConn.
func NewTLSTunnelConn(conn net.Conn, server *TLSTunnelServer) *TLSTunnelConn {
return &TLSTunnelConn{
Conn: conn,
server: server,
}
}
// GetMetrics implements the common.MetricsSource interface.
func (conn *TLSTunnelConn) GetMetrics() common.LogFields {
var logFields common.LogFields
// Relay any metrics from the underlying conn.
if m, ok := conn.Conn.(common.MetricsSource); ok {
logFields = m.GetMetrics()
} else {
logFields = make(common.LogFields)
}
if conn.server.passthroughAddress != "" {
logFields["passthrough_address"] = conn.server.passthroughAddress
}
return logFields
}