-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtp-session.go
222 lines (179 loc) · 4.59 KB
/
rtp-session.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
package rtp
/*
EKT defined in https://tools.ietf.org/html/draft-ietf-perc-srtp-ekt-diet-07
SRTP Profiles are at https://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
Currently only support DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM in half mode with EKT
*/
import (
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
)
type CipherID uint16
type Ssrc uint32
const (
// From https://www.iana.org/assignments/srtp-protection/srtp-protection.xhtml
NONE CipherID = 0x0000
SRTP_AEAD_AES_128_GCM CipherID = 0x0007
SRTP_AEAD_AES_256_GCM CipherID = 0x0008
DOUBLE_AEAD_AES_128_GCM_AEAD_AES_128_GCM CipherID = 0x0009
DOUBLE_AEAD_AES_256_GCM_AEAD_AES_256_GCM CipherID = 0x000a
)
type RTPSession struct {
extNameMap map[string]int
key []byte
salt []byte
seq uint16
roc uint32
rtcpKey []byte
rtcpSalt []byte
cipher CipherID
useEKT bool
rewriteSeq bool
}
func (s *RTPSession) Decode(packetData []byte) (*RTPPacket, error) {
p := new(RTPPacket)
if s.useEKT {
ektCmd := packetData[len(packetData)-1]
ektLen := 0
if ektCmd == 0 {
ektLen = 1
} else if ektCmd == 0x02 {
ektLen = int(binary.BigEndian.Uint16(packetData[len(packetData)-3:]))
ektLen += 2 + 2 + 1 // SPI + len + type
} else {
// bad EKT
return nil, errors.New("rtp: invalid EKT field")
}
if ektLen >= len(packetData) {
// bad EKT
return nil, errors.New("rtp: invalid EKT field - too big")
}
p.buffer = packetData[0 : len(packetData)-ektLen]
p.ekt = packetData[len(packetData)-ektLen : len(packetData)]
}
if s.cipher != NONE {
err := p.DecryptGCM(s.roc, s.key, s.salt)
if err != nil {
return nil, err
}
// remove the OHB if double RTP ( but not RTCP )
ohbLen := p.GetOHBLen()
p.buffer = p.buffer[0 : len(p.buffer)-ohbLen]
} else {
return nil, errors.New("rtp: cipher algorithm not supported")
}
return p, nil
}
func (s *RTPSession) DecodeRTCP(packetData []byte) (*RTCPCompoundPacket, error) {
p, err := NewSRTCPPacket(packetData)
if err != nil {
return nil, err
}
if s.cipher != NONE {
err = p.DecryptGCM(s.rtcpKey, s.rtcpSalt)
if err != nil {
return nil, err
}
return p, nil
}
return nil, errors.New("rtcp: cipher algorithm not supported")
}
func (s *RTPSession) Encode(p *RTPPacket) ([]byte, error) {
if s.cipher != NONE {
// Form the OHB with old seq
origPt := p.GetPT()
origSeq := p.GetSeq()
origMarker := p.GetMarker()
// Set the seq number
if ( s.rewriteSeq ) {
err := p.SetSeq(s.seq)
if err != nil {
return nil, err
}
}
err := p.SetOHB(origPt, origSeq, origMarker)
if err != nil {
return nil, err
}
// encrypt
err = p.EncryptGCM(s.roc, s.key, s.salt)
if err != nil {
return nil, err
}
} else {
return nil, errors.New("rtp: cipher algorithm not supported")
}
if ( s.rewriteSeq ) {
// increment seq
s.seq++
if s.seq == 0 {
s.roc++
}
}
if s.useEKT {
// add back EKT
rtpLen := len(p.buffer)
ektLen := len(p.ekt)
if rtpLen+ektLen > cap(p.buffer) {
return nil, errors.New("rtp: EKT too large to fit in packet MTU")
}
p.buffer = p.buffer[0 : rtpLen+ektLen]
copy(p.buffer[rtpLen:rtpLen+ektLen], p.ekt)
}
return p.buffer, nil
}
func (s* RTPSession) EncodeRTCP(p* RTCPCompoundPacket) ([]byte, error) {
if s.cipher != NONE {
err := p.EncryptGCM(s.rtcpKey, s.rtcpSalt)
if err != nil {
return nil, err
}
return p.GetBuffer(), nil
} else {
return nil, errors.New("rtp: cipher algorithm not supported")
}
}
func (s *RTPSession) NewRtcpRR() (*RTPPacket, error) {
return nil, nil
}
func (s *RTPSession) SetSRTP(cipher CipherID, useEKT bool, masterKey, masterSalt []byte) error {
kdf, err := NewKDF(masterKey, masterSalt)
if err != nil {
return err
}
rtpKey, rtpSalt, rtcpKey, rtcpSalt, err := kdf.DeriveForStream(cipher)
if err != nil {
return err
}
fmt.Printf("SRTP encryption key: %x\n", rtpKey)
s.key = rtpKey
s.salt = rtpSalt
s.rtcpKey = rtcpKey
s.rtcpSalt = rtcpSalt
s.cipher = cipher
s.useEKT = useEKT
return nil
}
func (s *RTPSession) SetExtMap(num int, name string) error {
if num > 14 {
return errors.New("rtp SetExtMap 2 byte headers are not implemented")
}
s.extNameMap[name] = num
return nil
}
func NewRTPSession( rewriteSeq bool ) *RTPSession {
s := new(RTPSession)
s.extNameMap = make(map[string]int)
randBytes := make([]byte, 2)
_, err := rand.Read(randBytes)
if err != nil {
fmt.Printf("rtp:NewRTPSession got %s\n", err.Error())
return nil
}
s.seq = binary.BigEndian.Uint16(randBytes) & 0x7FFF
s.roc = 0
s.rewriteSeq = rewriteSeq;
return s
}