-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpegts.go
318 lines (268 loc) · 7.62 KB
/
mpegts.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
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
// Copyright (c) 2016 Bob Ziuchkovski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package devo
import (
"bufio"
"fmt"
"github.com/bobziuchkovski/turing"
"io"
)
const (
tsSync = 0x47
tsPatID = 0x0000
tsIDMask = 0x1fff
tsPrivateType = 0x97
tsPrivateLength = 20
)
type packetID uint16
type tsDecryptor struct {
pool *cipherPool
ciphers map[packetID]*turing.Cipher
pmtID packetID
privateID packetID
}
func newTSDecryptor(mak string, iv []byte) *tsDecryptor {
return &tsDecryptor{
pool: newCipherPool(mak, iv),
ciphers: make(map[packetID]*turing.Cipher),
}
}
func (dec *tsDecryptor) decrypt(dst io.Writer, src *bufio.Reader) error {
var (
packet *tsPacket
count int
err error
)
for {
count++
// An mpeg-ts stream ends when there are no more packets to process.
_, err = src.Peek(1)
if err == io.EOF {
if dec.pmtID == 0 || dec.privateID == 0 {
err = io.ErrUnexpectedEOF
} else {
// Stream ends cleanly
err = nil
}
break
}
packet, err = readTSPacket(src)
if err != nil {
break
}
err = dec.processPacket(packet)
if err != nil {
break
}
err = writeTSPacket(dst, packet)
if err != nil {
break
}
}
if err != nil {
err = fmt.Errorf("failed while processing mpegts packet %d: %s", count, err)
}
return err
}
func (dec *tsDecryptor) processPacket(packet *tsPacket) error {
switch packet.id() {
case tsPatID:
return dec.processPAT(packet)
case dec.pmtID:
return dec.processPMT(packet)
case dec.privateID:
return dec.processPrivate(packet)
default:
if packet.scramble() != 0 {
return dec.decryptPacket(packet)
}
return nil
}
}
func (dec *tsDecryptor) processPAT(p *tsPacket) error {
offset := 0
payload := p.payload()
pointer := payload[offset]
if pointer != 0x00 {
return fmt.Errorf("PAT table contains unsupported pointer to additional data")
}
offset++
// Skip table id
offset++
// We expect only a single program in the stream, which should mean a length of 13
length := joinShort(payload[offset:offset+2]) & 0x0fff
if length != 13 {
return fmt.Errorf("bogus PAT length -- additional programs present?")
}
offset += 2
// Skip transport stream id (uint16), flags (byte), and section number/last section number info (2x uint8)
offset += 5
// What's remaining should be a tuple of [program id (uint16), packet id of program map (uint16)]
offset += 2
dec.pmtID = extractPacketID(payload[offset : offset+2])
return nil
}
func (dec *tsDecryptor) processPMT(p *tsPacket) error {
offset := 0
payload := p.payload()
pointer := payload[offset]
if pointer != 0x00 {
return fmt.Errorf("PMT table contains unsupported pointer to additional data")
}
offset++
// Skip table id
offset++
// Grab table length
length := joinShort(payload[offset:offset+2]) & 0x0fff
offset += 2
// Skip program id (uint16), flags (byte), section number/last section number info (2x uint8),
// PCR PID (uint16), and Program info length (uint16)
offset += 9
// What's remaining should be tuples of [type (byte), pid (uint16), ES info len (uint16)]
// offset < length only holds because there's 4 bytes prior to the table start (not included in length)
// and 4 bytes of CRC we want to skip (more correct would be offset - 4 < length - 4)
for uint16(offset) < length {
streamType := payload[offset]
if streamType == tsPrivateType {
dec.privateID = extractPacketID(payload[offset+1 : offset+3])
return nil
}
offset += 5
}
return fmt.Errorf("Failed to locate PID of private data")
}
func (dec *tsDecryptor) processPrivate(p *tsPacket) error {
payload := p.payload()
offset := 0
if string(payload[offset:4]) != "TiVo" {
return fmt.Errorf("bogus private data packet -- missing 'TiVo' magic bytes")
}
offset += 4
// Skip 5 unused bytes
offset += 5
// Grab length of confounder table
tableLength := payload[offset]
if tableLength%tsPrivateLength != 0 {
return fmt.Errorf("bogus private table length: %d", tableLength)
}
offset++
// Extract confounders from table and construct the appropriate cipher
dec.ciphers = make(map[packetID]*turing.Cipher)
for i := 0; i < int(tableLength/tsPrivateLength); i++ {
pid := extractPacketID(payload[offset : offset+2])
streamID := payload[offset+2]
cipher := dec.pool.getCipher(streamID, confounder(payload[offset+5:offset+9]))
dec.ciphers[pid] = cipher
offset += tsPrivateLength
}
return nil
}
func (dec *tsDecryptor) decryptPacket(p *tsPacket) error {
pid := p.id()
c, present := dec.ciphers[pid]
if !present {
return fmt.Errorf("cipher missing for scrambled packet with id 0x%04x", pid)
}
payload := p.payload()
if p.payloadStart() && (joinWord(payload[0:4])>>8) == psPrefix {
offset := 0
// Skip PES start code, length, and flags
offset += 8
// Skip past remaining header length
hdrlen := payload[offset]
offset++
offset += int(hdrlen)
// Skip sequence headers/extensions
for joinWord(payload[offset:offset+4]) == psCode(psSequenceHeader) {
intrabyte := payload[offset+11]
offset += 12
// Skip Q matrices
if intrabyte&(1<<1) != 0 {
offset += 64
}
if intrabyte&(1<<0) != 0 {
offset += 64
}
// Skip sequence extension
if joinWord(payload[offset:offset+4]) == psCode(psSequenceExtension) {
offset += 10
}
}
// Skip group header
if joinWord(payload[offset:offset+4]) == psCode(psGroupHeader) {
offset += 8
}
payload = payload[offset:]
}
c.XORKeyStream(payload, payload)
p.clearScramble()
return nil
}
type tsPacket struct {
content [188]byte
}
func readTSPacket(src io.Reader) (packet *tsPacket, err error) {
packet = &tsPacket{}
_, err = io.ReadFull(src, packet.content[:])
if err != nil {
return
}
if packet.content[0] != tsSync {
err = fmt.Errorf("expected sync byte, got 0x%02x instead", packet.content[0])
}
return
}
func writeTSPacket(dst io.Writer, packet *tsPacket) error {
_, err := dst.Write(packet.content[:])
return err
}
func (p *tsPacket) payloadStart() bool {
return p.content[1]&(1<<6) != 0
}
func (p *tsPacket) id() packetID {
return extractPacketID(p.content[1:3])
}
func (p *tsPacket) scramble() uint8 {
return p.content[3] >> 6
}
func (p *tsPacket) clearScramble() {
p.content[3] = p.content[3] ^ 0xc0
}
func (p *tsPacket) hasAdaptation() bool {
return p.content[3]&(1<<5) != 0
}
func (p *tsPacket) hasPayload() bool {
return p.content[3]&(1<<4) != 0
}
func (p *tsPacket) counter() uint8 {
return p.content[3] & 0x0f
}
func (p *tsPacket) payload() []byte {
var offset uint8
if p.hasAdaptation() {
offset = 5 + p.content[4]
} else {
offset = 4
}
return p.content[offset:]
}
func extractPacketID(b []byte) packetID {
return packetID(joinShort(b) & tsIDMask)
}