-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwidevine.go
183 lines (161 loc) · 4.04 KB
/
widevine.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
package widevine
import (
"41.neocities.org/protobuf"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"github.com/chmike/cmac-go"
)
func unpad(b []byte) []byte {
if len(b) >= 1 {
pad := b[len(b)-1]
if len(b) >= int(pad) {
b = b[:len(b)-int(pad)]
}
}
return b
}
type Cdm struct {
license_request []byte
private_key *rsa.PrivateKey
}
func (c *Cdm) Block(body ResponseBody) (cipher.Block, error) {
session_key, err := rsa.DecryptOAEP(
sha1.New(), nil, c.private_key, body.session_key(), nil,
)
if err != nil {
return nil, err
}
hash, err := cmac.New(aes.NewCipher, session_key)
if err != nil {
return nil, err
}
var data []byte
data = append(data, 1)
data = append(data, "ENCRYPTION"...)
data = append(data, 0)
data = append(data, c.license_request...)
data = append(data, 0, 0, 0, 128) // hash.Size()
_, err = hash.Write(data)
if err != nil {
return nil, err
}
return aes.NewCipher(hash.Sum(nil))
}
func (c *Cdm) New(private_key, client_id, pssh []byte) error {
block, _ := pem.Decode(private_key)
var err error
c.private_key, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
// L1
key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return err
}
c.private_key = key.(*rsa.PrivateKey)
}
c.license_request = protobuf.Message{ // LicenseRequest
1: {protobuf.Bytes(client_id)}, // ClientIdentification client_id
2: {protobuf.Message{ // ContentIdentification content_id
1: {protobuf.Message{ // WidevinePsshData widevine_pssh_data
1: {protobuf.Bytes(pssh)},
}},
}},
}.Marshal()
return nil
}
func (c *Cdm) RequestBody() ([]byte, error) {
hash := sha1.Sum(c.license_request)
signature, err := rsa.SignPSS(
rand{},
c.private_key,
crypto.SHA1,
hash[:],
&rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash},
)
if err != nil {
return nil, err
}
// SignedMessage
signed := protobuf.Message{}
// kktv.me
// type: LICENSE_REQUEST
signed.AddVarint(1, 1)
// LicenseRequest msg
signed.AddBytes(2, c.license_request)
// bytes signature
signed.AddBytes(3, signature)
return signed.Marshal(), nil
}
type KeyContainer struct {
Message protobuf.Message
}
func (k KeyContainer) Id() []byte {
value, _ := k.Message.GetBytes(1)()
return value
}
func (k KeyContainer) iv() []byte {
value, _ := k.Message.GetBytes(2)()
return value
}
func (k KeyContainer) Key(block cipher.Block) []byte {
key, _ := k.Message.GetBytes(3)()
cipher.NewCBCDecrypter(block, k.iv()).CryptBlocks(key, key)
return unpad(key)
}
func (k KeyContainer) Type() uint64 {
value, _ := k.Message.GetVarint(4)()
return uint64(value)
}
func (k KeyContainer) SecurityLevel() uint64 {
value, _ := k.Message.GetVarint(5)()
return uint64(value)
}
func (k KeyContainer) TrackLabel() string {
value, _ := k.Message.GetBytes(12)()
return string(value)
}
type PsshData struct {
ContentId []byte
KeyIds [][]byte
}
func (p *PsshData) Marshal() []byte {
message := protobuf.Message{}
for _, key_id := range p.KeyIds {
message.AddBytes(2, key_id)
}
if len(p.ContentId) >= 1 {
message.AddBytes(4, p.ContentId)
}
return message.Marshal()
}
func (r ResponseBody) Container() func() (KeyContainer, bool) {
value, _ := r.message.Get(2)()
values := value.Get(3)
return func() (KeyContainer, bool) {
value, ok := values()
return KeyContainer{value}, ok
}
}
func (r *ResponseBody) Unmarshal(data []byte) error {
r.message = protobuf.Message{}
return r.message.Unmarshal(data)
}
type ResponseBody struct {
message protobuf.Message
}
func (r ResponseBody) session_key() []byte {
value, _ := r.message.GetBytes(4)()
return value
}
type Wrapper interface {
Wrap([]byte) ([]byte, error)
}
type rand struct{}
func (rand) Read(b []byte) (int, error) {
return len(b), nil
}