-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdevice.go
159 lines (132 loc) · 3.85 KB
/
device.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
package widevine
import (
"bytes"
"crypto/rsa"
"crypto/x509"
"encoding/binary"
"encoding/pem"
"fmt"
"io"
"google.golang.org/protobuf/proto"
wvpb "github.com/iyear/gowidevine/widevinepb"
)
// Device represents a Widevine device.
type Device struct {
clientID *wvpb.ClientIdentification
cert *wvpb.DrmCertificate
privateKey *rsa.PrivateKey
}
// DeviceSource is a function that returns a Device.
type DeviceSource func() (*Device, error)
// FromRaw creates a Device from raw client ID and private key data.
func FromRaw(clientID, privateKey []byte) DeviceSource {
return func() (*Device, error) {
return toDevice(clientID, privateKey)
}
}
// FromWVD creates a Device from a WVD file.
func FromWVD(r io.Reader) DeviceSource {
return func() (*Device, error) {
return fromWVD(r)
}
}
// NewDevice creates a Device from a DeviceSource.
func NewDevice(src DeviceSource) (*Device, error) {
return src()
}
// ClientID returns the client ID of the device.
func (d *Device) ClientID() *wvpb.ClientIdentification {
return d.clientID
}
// DrmCertificate returns the DRM certificate of the device.
func (d *Device) DrmCertificate() *wvpb.DrmCertificate {
return d.cert
}
// PrivateKey returns the private key of the device.
func (d *Device) PrivateKey() *rsa.PrivateKey {
return d.privateKey
}
type wvdHeader struct {
Signature [3]byte
Version uint8
Type uint8
SecurityLevel uint8
Flags byte
}
type wvdDataV2 struct {
PrivateKeyLen uint16
PrivateKey []byte
ClientIDLen uint16
ClientID []byte
}
func fromWVD(r io.Reader) (*Device, error) {
header := &wvdHeader{}
if err := binary.Read(r, binary.BigEndian, header); err != nil {
return nil, fmt.Errorf("read header: %w", err)
}
if header.Signature != [3]byte{'W', 'V', 'D'} {
return nil, fmt.Errorf("invalid signature: %v", header.Signature)
}
rest, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("read rest bytes: %w", err)
}
switch header.Version {
case 2:
data := &wvdDataV2{}
data.PrivateKeyLen = binary.BigEndian.Uint16(rest[:2])
data.PrivateKey = rest[2 : 2+data.PrivateKeyLen]
data.ClientIDLen = binary.BigEndian.Uint16(rest[2+data.PrivateKeyLen : 2+data.PrivateKeyLen+2])
data.ClientID = rest[2+data.PrivateKeyLen+2 : 2+data.PrivateKeyLen+2+data.ClientIDLen]
return toDevice(data.ClientID, data.PrivateKey)
default:
return nil, fmt.Errorf("unsupported version: %d", header.Version)
}
}
func toDevice(clientID, privateKey []byte) (*Device, error) {
c := &wvpb.ClientIdentification{}
if err := proto.Unmarshal(clientID, c); err != nil {
return nil, fmt.Errorf("unmarshal client id: %w", err)
}
signedCert := &wvpb.SignedDrmCertificate{}
if err := proto.Unmarshal(c.Token, signedCert); err != nil {
return nil, fmt.Errorf("unmarshal signed cert: %w", err)
}
cert := &wvpb.DrmCertificate{}
if err := proto.Unmarshal(signedCert.DrmCertificate, cert); err != nil {
return nil, fmt.Errorf("unmarshal cert: %w", err)
}
key, err := parsePrivateKey(privateKey)
if err != nil {
return nil, fmt.Errorf("parse private key: %w", err)
}
return &Device{
clientID: c,
cert: cert,
privateKey: key,
}, nil
}
// parsePrivateKey modified from https://go.dev/src/crypto/tls/tls.go#L339
func parsePrivateKey(data []byte) (*rsa.PrivateKey, error) {
b := make([]byte, len(data))
copy(b, data)
if bytes.HasPrefix(data, []byte("-----")) {
block, _ := pem.Decode(data)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM block containing private key")
}
b = block.Bytes
}
if key, err := x509.ParsePKCS1PrivateKey(b); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(b); err == nil {
switch k := key.(type) {
case *rsa.PrivateKey:
return k, nil
default:
return nil, fmt.Errorf("unsupported private key type: %T", k)
}
}
return nil, fmt.Errorf("unsupported private key type")
}