-
Notifications
You must be signed in to change notification settings - Fork 15
/
license.go
255 lines (224 loc) · 7.3 KB
/
license.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
package widevine
import (
"encoding/base64"
"encoding/json"
"github.com/alfg/widevine/proto"
protobuf "github.com/golang/protobuf/proto"
)
// Widevine Cloud URLs.
const (
widevineCloudURL = "https://license.widevine.com"
widevineCloudURLTest = "https://license.uat.widevine.com"
)
// Widevine structure.
type Widevine struct {
Key []byte
IV []byte
Provider string
URL string
}
// Options provided to Widevine{} instance.
type Options struct {
Key []byte
IV []byte
Provider string
URL string
}
// Policy struct to set policy options for a ContentKey request.
type Policy struct {
ContentID string
Tracks []string
DRMTypes []string
Policy string
}
// GetContentKeyResponse JSON response from Widevine Cloud.
// /cenc/getcontentkey/<provider>
type GetContentKeyResponse struct {
Status string `json:"status"`
DRM []drm `json:"drm"`
Tracks []tracks `json:"tracks"`
AlreadyUsed bool `json:"already_used"`
}
type drm struct {
Type string `json:"type"`
SystemID string `json:"system_id"`
}
type tracks struct {
Type string `json:"type"`
KeyID string `json:"key_id"`
Key string `json:"key"`
PSSH []pssh `json:"pssh"`
}
type pssh struct {
DRMType string `json:"drm_type"`
Data string `json:"data"`
}
// GetLicenseResponse decoded JSON response from Widevine Cloud.
// /cenc/getlicense
type GetLicenseResponse struct {
Status string `json:"status"`
License string `json:"license"`
Make string `json:"make"`
Model string `json:"model"`
SecurityLevel int `json:"security_level"`
InternalStatus int `json:"internal_status"`
DRMCertSerialNumber string `json:"drm_cert_serial_number"`
DeviceWhitelistState string `json:"device_whitelist_state"`
MessageType string `json:"message_type"`
Platform string `json:"platform"`
DeviceState string `json:"device_state"`
ClientMaxHDCPVersion string `json:"client_max_hdcp_version"`
PlatformVerificationStatus string `json:"platform_verification_status"`
ContentOwner string `json:"content_owner"`
ContentPRovider string `json:"content_provider"`
SessionState sessionState `json:"session_state"`
LicenseMetadata licenseMetadata `json:"license_metadata"`
SupportedTracks []supportedTracks `json:"supported_tracks"`
PSSHData psshData `json:"pssh_data"`
ClientInfo []clientInfo `json:"client_info"`
}
type licenseMetadata struct {
ContentID string `json:"content_id"`
LicenseType string `json:"license_type"`
RequestType string `json:"request_type"`
}
type supportedTracks struct {
Type string `json:"type"`
KeyID string `json:"key_id"`
}
type sessionState struct {
LicenseID licenseID `json:"license_id"`
SigningKey string `json:"signing_key"`
KeyboxSystemID int `json:"keybox_system_id"`
LicenseCounter int `json:"license_counter"`
}
type licenseID struct {
RequestID string `json:"request_id"`
SessionID string `json:"session_id"`
PurchaseID string `json:"purchase_id"`
Type string `json:"type"`
Version int `json:"version"`
}
type psshData struct {
KeyID string `json:"key_id"`
ContentID string `json:"content_id"`
}
type clientInfo struct {
Name string `json:"name"`
Value string `json:"value"`
}
// New returns a Widevine instance with options.
func New(opts Options) *Widevine {
wv := &Widevine{
Key: opts.Key,
IV: opts.IV,
Provider: opts.Provider,
}
return wv
}
// GetContentKey creates a content key giving a contentID.
func (wp *Widevine) GetContentKey(contentID string, policy Policy) GetContentKeyResponse {
p := wp.setPolicy(contentID, policy)
msg := wp.buildCKMessage(p)
resp := wp.getContentKeyRequest(msg)
// TODO
// Build custom PSSH from protobuf.
// enc := wp.buildPSSH(contentID)
// fmt.Println("pssh build:", enc)
return resp
}
// GetLicense creates a license request used with a proxy server.
func (wp *Widevine) GetLicense(contentID string, body string) GetLicenseResponse {
msg := wp.buildLicenseMessage(contentID, body)
resp := wp.getLicenseRequest(msg)
return resp
}
func (wp *Widevine) buildPSSH(contentID string) string {
wvpssh := &proto.WidevineCencHeader{
Provider: protobuf.String(wp.Provider),
ContentId: []byte(contentID),
}
p, _ := protobuf.Marshal(wvpssh)
return base64.StdEncoding.EncodeToString(p)
}
func (wp *Widevine) buildCKMessage(policy map[string]interface{}) map[string]interface{} {
// Marshal and encode payload.
jsonPayload, _ := json.Marshal(policy)
b64payload := base64.StdEncoding.EncodeToString([]byte(jsonPayload))
// Create signature and postBody.
crypto := NewCrypto(wp.Key, wp.IV)
postBody := map[string]interface{}{
"request": b64payload,
"signature": crypto.generateSignature(jsonPayload),
"signer": wp.Provider,
}
return postBody
}
func (wp *Widevine) setPolicy(contentID string, policy Policy) map[string]interface{} {
enc := base64.StdEncoding.EncodeToString([]byte(contentID))
// Build tracks []interface.
var tracks []interface{}
for _, track := range policy.Tracks {
tracks = append(tracks, map[string]string{"type": track})
}
// Build policy interface.
// TODO: Set defaults.
p := map[string]interface{}{
"content_id": enc,
"tracks": tracks,
"drm_types": policy.DRMTypes,
"policy": policy.Policy,
}
return p
}
func (wp *Widevine) buildLicenseMessage(contentID string, body string) map[string]interface{} {
enc := base64.StdEncoding.EncodeToString([]byte(contentID))
message := map[string]interface{}{
"payload": body,
"content_id": enc,
"provider": wp.Provider,
"allowed_track_types": "SD_UHD1",
}
jsonMessage, _ := json.Marshal(message)
b64message := base64.StdEncoding.EncodeToString(jsonMessage)
// Create signature and postBody.
crypto := NewCrypto(wp.Key, wp.IV)
postBody := map[string]interface{}{
"request": b64message,
"signature": crypto.generateSignature(jsonMessage),
"signer": wp.Provider,
}
return postBody
}
func (wp *Widevine) getContentKeyRequest(body map[string]interface{}) GetContentKeyResponse {
// Set production or test portal.
var url string
if wp.Provider == "widevine_test" {
url = widevineCloudURLTest + "/cenc/getcontentkey/widevine_test"
} else {
url = widevineCloudURL + "/cenc/getcontentkey/" + wp.Provider
}
// Make client call.
resp := make(map[string]string)
client, _ := NewClient()
client.post(url, &resp, body)
// Decode and unmarshal the response.
dec, _ := base64.StdEncoding.DecodeString(resp["response"])
output := GetContentKeyResponse{}
json.Unmarshal(dec, &output)
return output
}
func (wp *Widevine) getLicenseRequest(body map[string]interface{}) GetLicenseResponse {
// Set production or test portal.
var url string
if wp.Provider == "widevine_test" {
url = widevineCloudURLTest + "/cenc/getlicense"
} else {
url = widevineCloudURL + "/cenc/getlicense"
}
// Make client call.
resp := GetLicenseResponse{}
client, _ := NewClient()
client.post(url, &resp, body)
return resp
}