-
Notifications
You must be signed in to change notification settings - Fork 3
/
restls_utils.go
273 lines (233 loc) · 6.15 KB
/
restls_utils.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// #Restls# Begin
package tls
import (
"fmt"
"hash"
"math/rand"
"strings"
"sync/atomic"
"lukechampine.com/blake3"
)
type RestlsPlugin struct {
isClient bool
isInbound bool
numCipherChange int
backupCipher any
writingClientFinished bool
clientFinished []byte
ConnId int64
}
func (r *RestlsPlugin) initAsClientInbound(id int64) {
r.isClient = true
r.isInbound = true
r.ConnId = id
}
func (r *RestlsPlugin) initAsClientOutbound(id int64) {
r.isClient = true
r.isInbound = false
r.ConnId = id
}
var IDCounter = atomic.Int64{}
func initRestlsPlugin(inPlugin *RestlsPlugin, outPlugin *RestlsPlugin) {
id := IDCounter.Add(1)
inPlugin.initAsClientInbound(id)
outPlugin.initAsClientOutbound(id)
}
func (r *RestlsPlugin) setBackupCipher(backupCipher ...any) {
if r.isClient && r.isInbound {
if len(backupCipher) != 1 {
panic("must provide exact 1 backup cipher")
}
r.backupCipher = backupCipher[0]
}
}
func (r *RestlsPlugin) changeCipher() {
debugf(nil, "[%d]RestlsPlugin changeCipher\n", r.ConnId)
r.numCipherChange += 1
}
func (r *RestlsPlugin) expectServerAuth(rType recordType) any {
if rType != recordTypeChangeCipherSpec && r.isClient && r.isInbound && r.numCipherChange == 1 && r.backupCipher != nil {
cipher := r.backupCipher
r.backupCipher = nil
return cipher
} else {
return nil
}
}
func (r *RestlsPlugin) captureClientFinished(record []byte) {
if r.isClient && !r.isInbound && r.writingClientFinished {
debugf(nil, "[%d]ClientFinished captured %v", r.ConnId, record)
r.writingClientFinished = false
r.clientFinished = append([]byte(nil), record...)
}
}
func (r *RestlsPlugin) WritingClientFinished() {
if !(r.isClient && !r.isInbound) {
panic("invalid operation")
}
r.writingClientFinished = true
}
func (r *RestlsPlugin) takeClientFinished() []byte {
if len(r.clientFinished) > 0 {
ret := r.clientFinished
r.clientFinished = nil
return ret
}
return nil
}
func RestlsHmac(key []byte) hash.Hash {
return blake3.New(32, key)
}
type Line struct {
targetLen TargetLength
command restlsCommand
}
type restlsCommand interface {
toBytes() [2]byte
needInterrupt() bool
}
type ActResponse int8
func (a ActResponse) toBytes() [2]byte {
return [2]byte{0x01, byte(a)}
}
func (a ActResponse) needInterrupt() bool {
return true
}
type ActNoop struct{}
func (a ActNoop) toBytes() [2]byte {
return [2]byte{0x00, 0}
}
func (a ActNoop) needInterrupt() bool {
return false
}
func parseCommand(buf []byte) (restlsCommand, error) {
if buf[0] == 0 {
return ActNoop{}, nil
} else if buf[0] == 1 {
return ActResponse(buf[1]), nil
} else {
return nil, fmt.Errorf("unsupported restls command")
}
}
type TargetLength [2]int16
func (t TargetLength) Len() int {
if t[1] != 0 {
return int(t[0] + int16(rand.Intn(int(t[1]))))
}
return int(t[0])
}
func parseRecordScript(script string) []Line {
script_split := strings.Split(strings.ReplaceAll(script, " ", ""), ",")
lines := []Line{}
for _, line_raw := range script_split {
if len(line_raw) == 0 {
continue
}
line_bytes := []byte(line_raw)
targetLen := TargetLength{getInteger(&line_bytes)}
if len(line_bytes) == 0 {
lines = append(lines, Line{targetLen, ActNoop{}})
continue
} else if line_bytes[0] == '~' || line_bytes[0] == '?' {
t := line_bytes[0]
line_bytes = line_bytes[1:]
randomRange := getInteger(&line_bytes)
if int(randomRange)+int(targetLen[0]) > 32768 {
panic("random target len > 32768")
}
targetLen[1] = randomRange
if t == '?' {
targetLen[0] = int16(targetLen.Len())
targetLen[1] = 0
}
}
if len(line_bytes) == 0 {
lines = append(lines, Line{targetLen, ActNoop{}})
continue
} else if line_bytes[0] == '<' {
line_bytes = line_bytes[1:]
numResponse := getInteger(&line_bytes)
lines = append(lines, Line{targetLen, ActResponse(numResponse)})
} else {
panic(fmt.Sprintf("invalid script %s, %v", line_raw, line_bytes))
}
}
debugf(nil, "script: %v\n", lines)
return lines
}
func getInteger(script *[]byte) int16 {
res := 0
i := 0
for i = 0; i < len(*script); i++ {
b := (*script)[i]
if b <= '9' && b >= '0' {
res = res*10 + int(b-'0')
} else {
break
}
if res > 32768 {
panic("target len > 32768")
}
}
*script = (*script)[i:]
return int16(res)
}
var curveIDMap = map[CurveID]int{
X25519: 0,
CurveP256: 1,
CurveP384: 2,
}
var curveIDList = []CurveID{X25519, CurveP256, CurveP384}
var versionMap = map[string]versionHint{
"tls12": TLS12Hint,
"tls13": TLS13Hint,
}
var clientIDMap = map[string]*ClientHelloID{
"chrome": &HelloChrome_Auto,
"firefox": &HelloFirefox_Auto,
"safari": &HelloSafari_Auto,
"ios": &HelloIOS_Auto,
}
var tls12GCMCiphers = []uint16{0xc02f, 0xc02b, 0xc030, 0xc02c}
var defaultRestlsScript = "250?100<1,350~100<1,600~100,300~200,300~100"
const debugLog = false
func debugf(conn *Conn, format string, a ...any) {
if debugLog {
if conn != nil {
fmt.Printf("[%d]"+format, append([]any{conn.in.restlsPlugin.ConnId}, a...)...)
} else {
fmt.Printf(format, a...)
}
}
}
func NewRestlsConfig(serverName string, password string, versionHintString string, restlsScript string, clientIDStr string) (*Config, error) {
key := make([]byte, 32)
blake3.DeriveKey(key, "restls-traffic-key", []byte(password))
versionHint, ok := versionMap[strings.ToLower(versionHintString)]
if !ok {
return nil, fmt.Errorf("invalid version hint: should be either tls12 or tls13")
}
sessionTicketsDisabled := true
if versionHint == TLS12Hint {
sessionTicketsDisabled = false
}
if len(restlsScript) == 0 {
restlsScript = defaultRestlsScript
}
clientIDPtr, ok := clientIDMap[clientIDStr]
if !ok {
clientIDPtr = &HelloChrome_Auto
}
clientID := atomic.Pointer[ClientHelloID]{}
clientID.Store(clientIDPtr)
return &Config{RestlsSecret: key, VersionHint: versionHint, ServerName: serverName, RestlsScript: parseRecordScript(restlsScript), ClientSessionCache: NewLRUClientSessionCache(100), ClientID: &clientID, SessionTicketsDisabled: sessionTicketsDisabled}, nil
}
func AnyTrue[T any](vals []T, predicate func(T) bool) bool {
for _, v := range vals {
if predicate(v) {
return true
}
}
return false
}
// #Restls# End