This repository has been archived by the owner on May 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathhandshake.go
145 lines (123 loc) · 3.14 KB
/
handshake.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
package rtmp
import (
"io"
"crypto/hmac"
"crypto/sha256"
"bytes"
"math/rand"
)
var (
clientKey = []byte{
'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
'F', 'l', 'a', 's', 'h', ' ', 'P', 'l', 'a', 'y', 'e', 'r', ' ',
'0', '0', '1',
0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1,
0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB,
0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE,
}
serverKey = []byte{
'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
'F', 'l', 'a', 's', 'h', ' ', 'M', 'e', 'd', 'i', 'a', ' ',
'S', 'e', 'r', 'v', 'e', 'r', ' ',
'0', '0', '1',
0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1,
0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB,
0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE,
}
clientKey2 = clientKey[:30]
serverKey2 = serverKey[:36]
serverVersion = []byte{
0x0D, 0x0E, 0x0A, 0x0D,
}
)
func makeDigest(key []byte, src []byte, skip int) (dst []byte) {
h := hmac.New(sha256.New, key)
if skip >= 0 && skip < len(src) {
if skip != 0 {
h.Write(src[:skip])
}
if len(src) != skip + 32 {
h.Write(src[skip+32:])
}
} else {
h.Write(src)
}
return h.Sum(nil)
}
func findDigest(b []byte, key []byte, base int) (int) {
offs := 0
for n := 0; n < 4; n++ {
offs += int(b[base + n])
}
offs = (offs % 728) + base + 4
// fmt.Printf("offs %v\n", offs)
dig := makeDigest(key, b, offs)
// fmt.Printf("digest %v\n", digest)
// fmt.Printf("p %v\n", b[offs:offs+32])
if bytes.Compare(b[offs:offs+32], dig) != 0 {
offs = -1
}
return offs
}
func writeDigest(b []byte, key []byte, base int) {
offs := 0
for n := 8; n < 12; n++ {
offs += int(b[base + n])
}
offs = (offs % 728) + base + 12
dig := makeDigest(key, b, offs)
copy(b[offs:], dig)
}
func createChal(b []byte, ver []byte, key []byte) {
b[0] = 3
copy(b[5:9], ver)
for i := 9; i < 1537; i++ {
b[i] = byte(rand.Int() % 256)
}
writeDigest(b[1:], key, 0)
}
func createResp(b []byte, key []byte) {
for i := 0; i < 1536; i++ {
b[i] = byte(rand.Int() % 256)
}
dig := makeDigest(key, b, 1536-32)
copy(b[1536-32:], dig)
}
func parseChal(b []byte, peerKey []byte, key []byte) (dig []byte, err int) {
if b[0] != 0x3 {
l.Printf("handshake: invalid rtmp version")
err = 1
return
}
epoch := b[1:5]
ver := b[5:9]
l.Printf("handshake: epoch %v ver %v", epoch, ver)
var offs int
if offs = findDigest(b[1:], peerKey, 772); offs == -1 {
if offs = findDigest(b[1:], peerKey, 8); offs == -1 {
l.Printf("handshake: digest not found")
err = 1
return
}
}
l.Printf("handshake: offs = %v", offs)
dig = makeDigest(key, b[1+offs:1+offs+32], -1)
return
}
func handShake(rw io.ReadWriter) {
b := ReadBuf(rw, 1537)
l.Printf("handshake: got client chal")
dig, err := parseChal(b, clientKey2, serverKey)
if err != 0 {
return
}
createChal(b, serverVersion, serverKey2)
l.Printf("handshake: send server chal")
rw.Write(b)
b = make([]byte, 1536)
createResp(b, dig)
l.Printf("handshake: send server resp")
rw.Write(b)
b = ReadBuf(rw, 1536)
l.Printf("handshake: got client resp")
}