forked from SagerNet/sing-openvpn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_codec_static.go
More file actions
436 lines (414 loc) · 13.5 KB
/
Copy pathdata_codec_static.go
File metadata and controls
436 lines (414 loc) · 13.5 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package openvpn
import (
"crypto/aes"
"crypto/cipher"
"crypto/des"
"crypto/hmac"
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"hash"
"strings"
"time"
"github.com/sagernet/sing/common/buf"
E "github.com/sagernet/sing/common/exceptions"
"github.com/RyuaNerin/go-krypto/aria"
"github.com/RyuaNerin/go-krypto/seed"
camellia "github.com/dgryski/go-camellia"
"github.com/tjfoc/gmsm/sm4"
"golang.org/x/crypto/blowfish" //nolint:staticcheck // OpenVPN BF-CBC compatibility requires Blowfish.
"golang.org/x/crypto/cast5" //nolint:staticcheck // OpenVPN CAST5-CBC compatibility requires CAST5.
"golang.org/x/crypto/ripemd160" //nolint:staticcheck // OpenVPN RIPEMD160 authentication compatibility requires RIPEMD-160.
)
type staticKeyDataCodec struct {
sendCipherBlock cipher.Block
sendHMACKey []byte
receiveCandidates []staticKeyDecodeCandidate
hashFactory func() hash.Hash
hmacSize int
sendTimestamp uint32
replayWindow *replayWindow
}
func (c *staticKeyDataCodec) EncodedLength(payloadLength int) int {
plainLength := 8 + payloadLength
protectedLength := plainLength
if c.sendCipherBlock != nil {
blockSize := c.sendCipherBlock.BlockSize()
protectedLength = blockSize + ((plainLength/blockSize)+1)*blockSize
}
return c.hmacSize + protectedLength
}
type staticKeyDecodeCandidate struct {
cipherBlock cipher.Block
hmacKey []byte
}
type staticKeySlot struct {
cipherKey []byte
hmacKey []byte
}
func (c *staticKeyDataCodec) Encode(packetID uint32, aadPrefix []byte, payload []byte) ([]byte, error) {
_ = aadPrefix
if packetID == 0 {
return nil, E.New("invalid static key packet id")
}
plainPayload := make([]byte, 8+len(payload))
binary.BigEndian.PutUint32(plainPayload[:4], packetID)
binary.BigEndian.PutUint32(plainPayload[4:8], c.sendTimestamp)
copy(plainPayload[8:], payload)
protectedPayload := plainPayload
if c.sendCipherBlock != nil {
blockSize := c.sendCipherBlock.BlockSize()
paddedPayload, err := pkcs7Pad(plainPayload, blockSize)
if err != nil {
return nil, err
}
initializationVector := make([]byte, blockSize)
_, err = rand.Read(initializationVector)
if err != nil {
return nil, err
}
cipherPayload := make([]byte, len(paddedPayload))
cipher.NewCBCEncrypter(c.sendCipherBlock, initializationVector).CryptBlocks(cipherPayload, paddedPayload)
protectedPayload = make([]byte, len(initializationVector)+len(cipherPayload))
copy(protectedPayload, initializationVector)
copy(protectedPayload[len(initializationVector):], cipherPayload)
}
if c.hmacSize > 0 {
mac := hmac.New(c.hashFactory, c.sendHMACKey)
mac.Write(protectedPayload)
macValue := mac.Sum(nil)
encodedPacket := make([]byte, len(macValue)+len(protectedPayload))
copy(encodedPacket, macValue)
copy(encodedPacket[len(macValue):], protectedPayload)
return encodedPacket, nil
}
encodedPacket := make([]byte, len(protectedPayload))
copy(encodedPacket, protectedPayload)
return encodedPacket, nil
}
func (c *staticKeyDataCodec) EncodeBuffer(packetID uint32, aadPrefix []byte, payload *buf.Buffer) (*buf.Buffer, error) {
encodedPayload, err := c.Encode(packetID, aadPrefix, payload.Bytes())
if err != nil {
payload.Release()
return nil, err
}
encodedBuffer := buf.NewSize(payload.Start() + len(encodedPayload))
encodedBuffer.Resize(payload.Start(), 0)
_, _ = encodedBuffer.Write(encodedPayload)
payload.Release()
return encodedBuffer, nil
}
func (c *staticKeyDataCodec) Decode(aadPrefix []byte, payload []byte) (uint32, []byte, error) {
_ = aadPrefix
var decodeError error
for _, candidate := range c.receiveCandidates {
packetID, packetTimestamp, decodedPayload, err := c.decodeWithCandidate(payload, candidate)
if err == nil {
if c.replayWindow != nil && !c.replayWindow.AcceptLongForm(packetID, packetTimestamp) {
return 0, nil, E.New("replayed static key data packet")
}
return packetID, decodedPayload, nil
}
decodeError = err
}
if decodeError == nil {
return 0, nil, E.New("invalid static key payload")
}
return 0, nil, decodeError
}
func (c *staticKeyDataCodec) DecodeBuffer(aadPrefix []byte, payload []byte, headroom int) (uint32, *buf.Buffer, error) {
packetID, decodedPayload, err := c.Decode(aadPrefix, payload)
if err != nil {
return 0, nil, err
}
return packetID, newDataPacketBuffer(headroom, decodedPayload), nil
}
func (c *staticKeyDataCodec) decodeWithCandidate(payload []byte, candidate staticKeyDecodeCandidate) (uint32, uint32, []byte, error) {
protectedPayload := payload
if c.hmacSize > 0 {
if len(protectedPayload) < c.hmacSize {
return 0, 0, nil, E.New("invalid static key payload")
}
receivedMAC := protectedPayload[:c.hmacSize]
protectedPayload = protectedPayload[c.hmacSize:]
mac := hmac.New(c.hashFactory, candidate.hmacKey)
mac.Write(protectedPayload)
expectedMAC := mac.Sum(nil)
if !hmac.Equal(receivedMAC, expectedMAC) {
return 0, 0, nil, E.New("invalid static key payload hmac")
}
}
plainPayload := protectedPayload
if candidate.cipherBlock != nil {
blockSize := candidate.cipherBlock.BlockSize()
if len(protectedPayload) < blockSize*2 || len(protectedPayload)%blockSize != 0 {
return 0, 0, nil, E.New("invalid static key payload")
}
initializationVector := protectedPayload[:blockSize]
cipherPayload := protectedPayload[blockSize:]
decodedPayload := make([]byte, len(cipherPayload))
cipher.NewCBCDecrypter(candidate.cipherBlock, initializationVector).CryptBlocks(decodedPayload, cipherPayload)
unpaddedPayload, err := pkcs7Unpad(decodedPayload, blockSize)
if err != nil {
return 0, 0, nil, err
}
plainPayload = unpaddedPayload
}
if len(plainPayload) < 8 {
return 0, 0, nil, E.New("invalid static key payload")
}
packetID := binary.BigEndian.Uint32(plainPayload[:4])
if packetID == 0 {
return 0, 0, nil, E.New("invalid static key packet id")
}
packetTimestamp := binary.BigEndian.Uint32(plainPayload[4:8])
decodedPayload := make([]byte, len(plainPayload)-8)
copy(decodedPayload, plainPayload[8:])
return packetID, packetTimestamp, decodedPayload, nil
}
func newStaticKeyDataCodec(staticKey Material, keyDirection int, cipherName string, authName string, replayWindowSize uint32, replayWindowTime time.Duration) (dataCodec, error) {
staticKeyMaterial, err := loadStaticKeyMaterialFromSource(staticKey)
if err != nil {
return nil, err
}
cipherKeySize, err := staticCipherKeySize(cipherName)
if err != nil {
return nil, err
}
hashFactory, hmacSize, err := staticHMACFactory(authName)
if err != nil {
return nil, err
}
keySlots := splitStaticKeyMaterial(staticKeyMaterial)
sendSlotIndex, receiveSlotIndex, receiveFallbackIndex := resolveStaticKeyDirection(keyDirection)
sendSlot := keySlots[sendSlotIndex]
receiveSlot := keySlots[receiveSlotIndex]
sendCipherBlock, err := newStaticCipherBlock(cipherName, sendSlot.cipherKey, cipherKeySize)
if err != nil {
return nil, err
}
receiveCipherBlock, err := newStaticCipherBlock(cipherName, receiveSlot.cipherKey, cipherKeySize)
if err != nil {
return nil, err
}
var sendHMACKey []byte
var receiveHMACKey []byte
if hmacSize > 0 {
sendHMACKey = make([]byte, hmacSize)
copy(sendHMACKey, sendSlot.hmacKey[:hmacSize])
receiveHMACKey = make([]byte, hmacSize)
copy(receiveHMACKey, receiveSlot.hmacKey[:hmacSize])
}
receiveCandidates := []staticKeyDecodeCandidate{
{
cipherBlock: receiveCipherBlock,
hmacKey: receiveHMACKey,
},
}
if receiveFallbackIndex >= 0 && receiveFallbackIndex != receiveSlotIndex {
fallbackSlot := keySlots[receiveFallbackIndex]
fallbackCipherBlock, fallbackCipherErr := newStaticCipherBlock(cipherName, fallbackSlot.cipherKey, cipherKeySize)
if fallbackCipherErr != nil {
return nil, fallbackCipherErr
}
var fallbackHMACKey []byte
if hmacSize > 0 {
fallbackHMACKey = make([]byte, hmacSize)
copy(fallbackHMACKey, fallbackSlot.hmacKey[:hmacSize])
}
receiveCandidates = append(receiveCandidates, staticKeyDecodeCandidate{
cipherBlock: fallbackCipherBlock,
hmacKey: fallbackHMACKey,
})
}
return &staticKeyDataCodec{
sendCipherBlock: sendCipherBlock,
sendHMACKey: sendHMACKey,
receiveCandidates: receiveCandidates,
hashFactory: hashFactory,
hmacSize: hmacSize,
sendTimestamp: uint32(time.Now().Unix()),
replayWindow: newReplayWindow(replayWindowSize, replayWindowTime),
}, nil
}
func resolveStaticKeyDirection(keyDirection int) (int, int, int) {
if keyDirection == 1 || keyDirection == 2 {
return 1, 0, 1
}
if keyDirection < 0 {
return 0, 0, 1
}
return 0, 1, 0
}
func splitStaticKeyMaterial(staticKeyMaterial []byte) []staticKeySlot {
return []staticKeySlot{
{
cipherKey: append([]byte{}, staticKeyMaterial[0:64]...),
hmacKey: append([]byte{}, staticKeyMaterial[64:128]...),
},
{
cipherKey: append([]byte{}, staticKeyMaterial[128:192]...),
hmacKey: append([]byte{}, staticKeyMaterial[192:256]...),
},
}
}
func staticCipherKeySize(cipherName string) (int, error) {
switch cipherName {
case "BF-CBC":
return 16, nil
case "DES-CBC":
return 8, nil
case "DES-EDE-CBC":
return 16, nil
case "DES-EDE3-CBC":
return 24, nil
case "CAST5-CBC":
return 16, nil
case "AES-128-CBC":
return 16, nil
case "AES-192-CBC":
return 24, nil
case "AES-256-CBC":
return 32, nil
case "ARIA-128-CBC", "CAMELLIA-128-CBC", "SEED-CBC", "SM4-CBC":
return 16, nil
case "ARIA-192-CBC", "CAMELLIA-192-CBC":
return 24, nil
case "ARIA-256-CBC", "CAMELLIA-256-CBC":
return 32, nil
case "NONE":
return 0, nil
default:
return 0, E.New("unsupported static key cipher")
}
}
func newStaticCipherBlock(cipherName string, keyMaterial []byte, keySize int) (cipher.Block, error) {
if cipherName == "NONE" {
return nil, nil
}
if keySize <= 0 || keySize > len(keyMaterial) {
return nil, E.New("invalid static key cipher key size")
}
cipherKey := make([]byte, keySize)
copy(cipherKey, keyMaterial[:keySize])
switch cipherName {
case "BF-CBC":
return blowfish.NewCipher(cipherKey)
case "DES-CBC":
return des.NewCipher(cipherKey)
case "DES-EDE-CBC":
expandedCipherKey := make([]byte, 24)
copy(expandedCipherKey, cipherKey)
copy(expandedCipherKey[16:], cipherKey[:8])
return des.NewTripleDESCipher(expandedCipherKey)
case "DES-EDE3-CBC":
return des.NewTripleDESCipher(cipherKey)
case "CAST5-CBC":
return cast5.NewCipher(cipherKey)
case "AES-128-CBC", "AES-192-CBC", "AES-256-CBC":
return aes.NewCipher(cipherKey)
case "ARIA-128-CBC", "ARIA-192-CBC", "ARIA-256-CBC":
return aria.NewCipher(cipherKey)
case "CAMELLIA-128-CBC", "CAMELLIA-192-CBC", "CAMELLIA-256-CBC":
return camellia.New(cipherKey)
case "SEED-CBC":
return seed.NewCipher(cipherKey)
case "SM4-CBC":
return sm4.NewCipher(cipherKey)
default:
return nil, E.New("unsupported static key cipher")
}
}
func staticHMACFactory(authName string) (func() hash.Hash, int, error) {
switch authName {
case "", "SHA1":
return sha1.New, sha1.Size, nil
case "SHA224":
return sha256.New224, sha256.Size224, nil
case "SHA256":
return sha256.New, sha256.Size, nil
case "SHA384":
return sha512.New384, sha512.Size384, nil
case "SHA512":
return sha512.New, sha512.Size, nil
case "RIPEMD160":
return ripemd160.New, ripemd160.Size, nil
case "MD5":
return md5.New, md5.Size, nil
case "NONE":
return nil, 0, nil
default:
return nil, 0, E.New("unsupported static key auth")
}
}
func pkcs7Pad(payload []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 {
return nil, E.New("invalid block size")
}
paddingSize := blockSize - (len(payload) % blockSize)
if paddingSize == 0 {
paddingSize = blockSize
}
paddedPayload := make([]byte, len(payload)+paddingSize)
copy(paddedPayload, payload)
for i := len(payload); i < len(paddedPayload); i++ {
paddedPayload[i] = byte(paddingSize)
}
return paddedPayload, nil
}
func pkcs7Unpad(payload []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 || len(payload) == 0 || len(payload)%blockSize != 0 {
return nil, E.New("invalid static key payload padding")
}
paddingSize := int(payload[len(payload)-1])
if paddingSize <= 0 || paddingSize > blockSize || paddingSize > len(payload) {
return nil, E.New("invalid static key payload padding")
}
paddingStart := len(payload) - paddingSize
for i := paddingStart; i < len(payload); i++ {
if payload[i] != byte(paddingSize) {
return nil, E.New("invalid static key payload padding")
}
}
unpaddedPayload := make([]byte, paddingStart)
copy(unpaddedPayload, payload[:paddingStart])
return unpaddedPayload, nil
}
func loadStaticKeyMaterialFromSource(staticKey Material) ([]byte, error) {
staticKeyContent, err := loadMaterial(staticKey)
if err != nil {
return nil, err
}
if len(staticKeyContent) == 0 {
return nil, ErrMissingStaticKey
}
if len(staticKeyContent) == 256 {
return append([]byte(nil), staticKeyContent...), nil
}
staticKeyText := string(staticKeyContent)
const beginMarker = "-----BEGIN OpenVPN Static key V1-----"
const endMarker = "-----END OpenVPN Static key V1-----"
beginIndex := strings.Index(staticKeyText, beginMarker)
if beginIndex >= 0 {
staticKeyText = staticKeyText[beginIndex+len(beginMarker):]
endIndex := strings.Index(staticKeyText, endMarker)
if endIndex < 0 {
return nil, E.New("invalid static key end marker")
}
staticKeyText = staticKeyText[:endIndex]
}
hexFields := strings.Fields(staticKeyText)
hexContent := strings.Join(hexFields, "")
if len(hexContent) != 512 {
return nil, E.New("static key must contain exactly 256 bytes")
}
decodedStaticKey, err := hex.DecodeString(hexContent)
if err != nil {
return nil, err
}
return decodedStaticKey, nil
}