/
tlsTunnelConn.go
181 lines (151 loc) · 5.47 KB
/
tlsTunnelConn.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
/*
* 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 psiphon
import (
"context"
"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"
)
// TLSTunnelConfig specifies the behavior of a TLSTunnelConn.
type TLSTunnelConfig struct {
// CustomTLSConfig is the parameters that will be used to esablish a new
// TLS connection with CustomTLSDial.
CustomTLSConfig *CustomTLSConfig
// UseObfuscatedSessionTickets indicates whether to use obfuscated session
// tickets.
UseObfuscatedSessionTickets bool
// The following values are used to create the TLS passthrough message.
ObfuscatedKey string
ObfuscatorPaddingSeed *prng.Seed
}
// TLSTunnelConn is a network connection that tunnels net.Conn flows over TLS.
type TLSTunnelConn struct {
net.Conn
tlsPadding int
}
// DialTLSTunnel returns an initialized tls-tunnel connection.
func DialTLSTunnel(
ctx context.Context,
tlsTunnelConfig *TLSTunnelConfig,
dialConfig *DialConfig,
tlsOSSHApplyTrafficShaping bool,
tlsOSSHMinTLSPadding,
tlsOSSHMaxTLSPadding int,
) (*TLSTunnelConn, error) {
tlsPadding,
err :=
tlsTunnelTLSPadding(
tlsOSSHApplyTrafficShaping,
tlsOSSHMinTLSPadding,
tlsOSSHMaxTLSPadding,
tlsTunnelConfig.ObfuscatorPaddingSeed)
if err != nil {
return nil, errors.Trace(err)
}
tlsConfig := &CustomTLSConfig{
Parameters: tlsTunnelConfig.CustomTLSConfig.Parameters,
Dial: NewTCPDialer(dialConfig),
DialAddr: tlsTunnelConfig.CustomTLSConfig.DialAddr,
SNIServerName: tlsTunnelConfig.CustomTLSConfig.SNIServerName,
VerifyServerName: tlsTunnelConfig.CustomTLSConfig.VerifyServerName,
VerifyPins: tlsTunnelConfig.CustomTLSConfig.VerifyPins,
SkipVerify: tlsTunnelConfig.CustomTLSConfig.SkipVerify,
TLSProfile: tlsTunnelConfig.CustomTLSConfig.TLSProfile,
NoDefaultTLSSessionID: tlsTunnelConfig.CustomTLSConfig.NoDefaultTLSSessionID,
RandomizedTLSProfileSeed: tlsTunnelConfig.CustomTLSConfig.RandomizedTLSProfileSeed,
TLSPadding: tlsPadding,
TrustedCACertificatesFilename: dialConfig.TrustedCACertificatesFilename,
}
tlsConfig.EnableClientSessionCache()
if tlsTunnelConfig.UseObfuscatedSessionTickets {
tlsConfig.ObfuscatedSessionTicketKey = tlsTunnelConfig.ObfuscatedKey
}
// As the passthrough message is unique and indistinguishable from a normal
// TLS client random value, we set it unconditionally and not just for
// protocols which may support passthrough (even for those protocols,
// clients don't know which servers are configured to use it).
passthroughMessage, err := obfuscator.MakeTLSPassthroughMessage(
true,
tlsTunnelConfig.ObfuscatedKey)
if err != nil {
return nil, errors.Trace(err)
}
tlsConfig.PassthroughMessage = passthroughMessage
tlsDialer := NewCustomTLSDialer(tlsConfig)
// As DialAddr is set in the CustomTLSConfig, no address is required here.
conn, err := tlsDialer(ctx, "tcp", "")
if err != nil {
return nil, errors.Trace(err)
}
return &TLSTunnelConn{
Conn: conn,
tlsPadding: tlsPadding,
}, nil
}
// tlsTunnelTLSPadding returns the padding length to apply with the TLS padding
// extension to the TLS conn established with NewCustomTLSDialer. See
// CustomTLSConfig.TLSPadding for details.
func tlsTunnelTLSPadding(
tlsOSSHApplyTrafficShaping bool,
tlsOSSHMinTLSPadding int,
tlsOSSHMaxTLSPadding int,
obfuscatorPaddingPRNGSeed *prng.Seed,
) (tlsPadding int,
err error) {
tlsPadding = 0
if tlsOSSHApplyTrafficShaping {
minPadding := tlsOSSHMinTLSPadding
maxPadding := tlsOSSHMaxTLSPadding
// Maximum padding size per RFC 7685
if maxPadding > 65535 {
maxPadding = 65535
}
if maxPadding > 0 {
tlsPaddingPRNG, err := prng.NewPRNGWithSaltedSeed(obfuscatorPaddingPRNGSeed, "tls-padding")
if err != nil {
return 0, errors.Trace(err)
}
tlsPadding = tlsPaddingPRNG.Range(minPadding, maxPadding)
}
}
return tlsPadding, nil
}
func (conn *TLSTunnelConn) GetMetrics() common.LogFields {
logFields := make(common.LogFields)
logFields["tls_padding"] = conn.tlsPadding
// Include metrics, such as fragmentor metrics, from the underlying dial
// conn. Properties of subsequent underlying dial conns are not reflected
// in these metrics; we assume that the first dial conn, which most likely
// transits the various protocol handshakes, is most significant.
underlyingMetrics, ok := conn.Conn.(common.MetricsSource)
if ok {
logFields.Add(underlyingMetrics.GetMetrics())
}
return logFields
}
func (conn *TLSTunnelConn) IsClosed() bool {
closer, ok := conn.Conn.(common.Closer)
if !ok {
return false
}
return closer.IsClosed()
}