@li0ard/widevine 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -39,13 +39,13 @@ const sessionId = cdm.open();
39
39
 
40
40
  const pssh = PSSH.decode(Buffer.from("....", "base64"));
41
41
 
42
- const challenge = cdm.get_license_challenge(sessionId, pssh);
42
+ const challenge = cdm.getLicenseChallenge(sessionId, pssh);
43
43
  const license = await (await fetch(`https://cwip-shaka-proxy.appspot.com/no_auth`, {
44
44
  method: "POST",
45
45
  body: challenge
46
46
  })).arrayBuffer();
47
47
 
48
- for(const key of cdm.parse_license(sessionId, new Uint8Array(license)))
48
+ for(const key of cdm.parseLicense(sessionId, new Uint8Array(license)))
49
49
  console.log(`- [${KeyType[key.type]}] ${bytesToHex(key.kid)}:${bytesToHex(key.key)}`);
50
50
 
51
51
  cdm.close(sessionId);
package/dist/const.d.ts CHANGED
@@ -1,6 +1,4 @@
1
1
  import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
- /** Widevine System ID */
3
- export declare const WIDEVINE_SID: Readonly<Uint8Array>;
4
2
  /** Device type */
5
3
  export declare enum DeviceType {
6
4
  CHROME = 1,
@@ -8,3 +6,8 @@ export declare enum DeviceType {
8
6
  }
9
7
  /** Key type */
10
8
  export declare const KeyType: typeof pywidevine_license_protocol.License.KeyContainer.KeyType;
9
+ /** License type */
10
+ export declare const LicenseType: typeof pywidevine_license_protocol.LicenseType;
11
+ export declare const ROOT_CERT: pywidevine_license_protocol.DrmCertificate;
12
+ /** Challenge to get service certificate */
13
+ export declare const SERVICE_CERTIFICATE_CHALLENGE: Readonly<Uint8Array>;
package/dist/const.js CHANGED
@@ -1,6 +1,6 @@
1
+ import { hexToBytes } from "@noble/ciphers/utils.js";
1
2
  import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
- /** Widevine System ID */
3
- export const WIDEVINE_SID = new Uint8Array([0xed, 0xef, 0x8b, 0xa9, 0x79, 0xd6, 0x4a, 0xce, 0xa3, 0xc8, 0x27, 0xdc, 0xd5, 0x1d, 0x21, 0xed]);
3
+ import { parseCerificate } from "./utils.js";
4
4
  /** Device type */
5
5
  export var DeviceType;
6
6
  (function (DeviceType) {
@@ -9,3 +9,8 @@ export var DeviceType;
9
9
  })(DeviceType || (DeviceType = {}));
10
10
  /** Key type */
11
11
  export const KeyType = pywidevine_license_protocol.License.KeyContainer.KeyType;
12
+ /** License type */
13
+ export const LicenseType = pywidevine_license_protocol.LicenseType;
14
+ export const ROOT_CERT = parseCerificate(hexToBytes("0a9c03080012010018dd94888b05228e033082018a0282018100b4fe39c3659003db3c119709e868cdf2c35e9bf2e74d23b110db8765dfdcfb9f35a05703534cf66d357da678dbb336d23f9c40a99526727fb8be66dfc52198781516685d2f460e43cb8a8439abfbb0358022be34238bab535b72ec4bb54869533e475ffd09fda776138f0f92d64cdfae76a9bad92210a99d7145d6d7e11925859c539a97eb84d7cca8888220702620fd7e405027e225936fbc3e72a0fac1bd29b44d825cc1b4cb9c727eb0e98a173e1963fcfd82482bb7b233b97dec4bba891f27b89b884884aa18920e65f5c86c11ff6b36e47434ca8c33b1f9b88eb4e612e0029879525e4533ff11dcebc353ba7c601a113d00fbd2b7aa30fa4f5e48775b17dc75ef6fd2196ddcbe7fb0788fdc82604cbfe429065e698c3913ad1425ed19b2f29f01820d564488c835ec1f11b324e0590d37e4473cea4b7f97311c817c948a4c7d681584ffa508fd18e7e72be447271211b823ec58933cac12d2886d413dc5fe1cdcb9f8d4513e07e5036fa712e812f7b5cea696553f78b4648250d2335f91020301000112800358f1d64d04097bdfd7ef5d3b023917fa1436754a3867855712a714ee3516d53dbf4286f6690076cd93f47cb2df9e44cd4cd4ae0918534432ece0611be5da13d355c5dd1acb901e7e5bc6e90f229fbe8502fe9031cc6b0384bd22c455faf5f208cd654158e87d29da045882f53769bcf35a5784177b328770b2b0769cb2c315d111262a2375993eb97722320dbc1a19c1d565907655740f0e694d5f4d8f19afdfd6163194a8925f4fbc7a31f8ae8ead33b7e930d08c0a8a6c8335f88a81b2fe1c88ac2a66c5ffbde617d0620bdc8a45f7b03e5ac81e4a242f6ca5e31c881483d5c5ef5e9f3d854573e26b5052574cfb926c66758ad60d1baef3ecaf5122035d0a2e63939c0b0120a8a9842e17caae73ec221b79aef6a0722cdf0747db888630147821112288acd75474f9f326c2a556c8564f00291d087b7afb9589c3ee98549e3c6b94051312f671b9ab13c30c9b46087b3d326a68ca1e9c9062c5ed10b91f1725ce90b96dcdc446f5a362137402a762a4fa55d9decfa2e68074550649d5020c"));
15
+ /** Challenge to get service certificate */
16
+ export const SERVICE_CERTIFICATE_CHALLENGE = new Uint8Array([8, 4]);
package/dist/device.d.ts CHANGED
@@ -4,22 +4,24 @@ import type { DeviceType } from './const.js';
4
4
  /** Device instance for CDM */
5
5
  export declare class Device {
6
6
  type: DeviceType;
7
+ clientId: pywidevine_license_protocol.ClientIdentification;
7
8
  privateKey: PrivateKey;
8
- client_id: pywidevine_license_protocol.ClientIdentification;
9
9
  /**
10
10
  * Device instance for CDM
11
11
  * @param type Device type
12
+ * @param clientId Device client identification
12
13
  * @param privateKey Device private key
13
- * @param client_id Device client identification
14
14
  */
15
- constructor(type: DeviceType, privateKey: PrivateKey, client_id: pywidevine_license_protocol.ClientIdentification);
15
+ constructor(type: DeviceType, clientId: pywidevine_license_protocol.ClientIdentification, privateKey: PrivateKey);
16
16
  /** Device public key */
17
17
  get publicKey(): PublicKey;
18
18
  /**
19
19
  * Get device instance from dump
20
20
  * @param type Device type
21
- * @param client_id Device client identification blob (`client_id.bin`)
21
+ * @param clientId Device client identification blob (`client_id.bin`)
22
22
  * @param privateKey Device private key blob (ASN.1 encoded)
23
23
  */
24
- static decode(type: DeviceType, client_id: Uint8Array, privateKey: Uint8Array): Device;
24
+ static decode(type: DeviceType, clientId: Uint8Array, privateKey: Uint8Array): Device;
25
+ /** Get device instance from `.wvd` file */
26
+ static fromWvd(data: Uint8Array): Device;
25
27
  }
package/dist/device.js CHANGED
@@ -3,22 +3,22 @@ import { decodePrivateKey, decodePublicKey, parseCerificate } from './utils.js';
3
3
  /** Device instance for CDM */
4
4
  export class Device {
5
5
  type;
6
+ clientId;
6
7
  privateKey;
7
- client_id;
8
8
  /**
9
9
  * Device instance for CDM
10
10
  * @param type Device type
11
+ * @param clientId Device client identification
11
12
  * @param privateKey Device private key
12
- * @param client_id Device client identification
13
13
  */
14
- constructor(type, privateKey, client_id) {
14
+ constructor(type, clientId, privateKey) {
15
15
  this.type = type;
16
+ this.clientId = clientId;
16
17
  this.privateKey = privateKey;
17
- this.client_id = client_id;
18
18
  }
19
19
  /** Device public key */
20
20
  get publicKey() {
21
- const certificate = parseCerificate(this.client_id.token);
21
+ const certificate = parseCerificate(this.clientId.token);
22
22
  if (!certificate.public_key)
23
23
  throw new Error("Missing public key in DRM certificate");
24
24
  return decodePublicKey(certificate.public_key);
@@ -26,13 +26,42 @@ export class Device {
26
26
  /**
27
27
  * Get device instance from dump
28
28
  * @param type Device type
29
- * @param client_id Device client identification blob (`client_id.bin`)
29
+ * @param clientId Device client identification blob (`client_id.bin`)
30
30
  * @param privateKey Device private key blob (ASN.1 encoded)
31
31
  */
32
- static decode(type, client_id, privateKey) {
33
- const clientId = pywidevine_license_protocol.ClientIdentification.deserialize(client_id);
34
- if (!clientId.token)
32
+ static decode(type, clientId, privateKey) {
33
+ const client_id = pywidevine_license_protocol.ClientIdentification.deserialize(clientId);
34
+ if (!client_id.token)
35
35
  throw new Error("Missing token in Client ID");
36
- return new Device(type, decodePrivateKey(privateKey), clientId);
36
+ return new Device(type, client_id, decodePrivateKey(privateKey));
37
+ }
38
+ /** Get device instance from `.wvd` file */
39
+ static fromWvd(data) {
40
+ const rdr = new DataView(data.buffer);
41
+ let offset = 0;
42
+ const magic = new TextDecoder().decode(data.subarray(offset, offset += 3));
43
+ if (magic != "WVD")
44
+ throw new Error("Invalid magic constant, not a WVD file");
45
+ const version = rdr.getUint8(offset);
46
+ offset += 1;
47
+ if (version != 1 && version != 2)
48
+ throw new Error("Invalid version, not a WVD file");
49
+ const type = rdr.getUint8(offset);
50
+ offset += 1;
51
+ const level = rdr.getUint8(offset);
52
+ offset += 1;
53
+ if (level < 1 || level > 3)
54
+ throw new Error("Invalid device version, not a WVD file");
55
+ //const flags = rdr.getUint8(offset);
56
+ offset += 1;
57
+ const privateKeyLength = rdr.getUint16(offset);
58
+ offset += 2;
59
+ const privateKey = data.slice(offset, offset += privateKeyLength);
60
+ const clientIdLen = rdr.getUint16(offset);
61
+ offset += 2;
62
+ const clientId = pywidevine_license_protocol.ClientIdentification.deserializeBinary(data.slice(offset, offset += clientIdLen));
63
+ if (!clientId.token)
64
+ throw new Error("Missing token in Client ID, not a WVD file");
65
+ return new Device(type, clientId, decodePrivateKey(privateKey));
37
66
  }
38
67
  }
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ import { Key } from "./key.js";
5
5
  /** Widevine Content Decryption Module (CDM) instance */
6
6
  export declare class CDM {
7
7
  device: Device;
8
+ static MAX_SESSIONS: number;
8
9
  private sessions;
9
10
  private signer;
10
11
  private crypter;
@@ -24,19 +25,32 @@ export declare class CDM {
24
25
  * Get license request (challenge)
25
26
  * @param sessionId Session ID
26
27
  * @param pssh PSSH object
27
- * @param license_type License type (default - `STREAMING`)
28
+ * @param licenseType License type (default - `STREAMING`)
29
+ * @param privacyMode Encrypt the Client ID using service certificate (If service certificate is set)
28
30
  */
29
- get_license_challenge(sessionId: string, pssh: PSSH, license_type?: pywidevine_license_protocol.LicenseType): Uint8Array;
31
+ getLicenseChallenge(sessionId: string, pssh: PSSH, licenseType?: pywidevine_license_protocol.LicenseType, privacyMode?: boolean): Uint8Array;
32
+ /**
33
+ * Set service certificate for privacy mode
34
+ * @param sessionId Session ID
35
+ * @param certificate Service certificate (If none remove current)
36
+ */
37
+ setServiceCertificate(sessionId: string, certificate?: Uint8Array): string | null;
38
+ /**
39
+ * Get service certificate of session
40
+ * @param sessionId Session ID
41
+ */
42
+ getServiceCertificate(sessionId: string): pywidevine_license_protocol.SignedDrmCertificate | null;
30
43
  /**
31
44
  * Get keys from license response
32
45
  * @param sessionId Session ID
33
- * @param license_response License response
46
+ * @param licenseResponse License response
34
47
  */
35
- parse_license(sessionId: string, license_response: Uint8Array): Key[];
36
- private derive_keys;
37
- private derive_context;
48
+ parseLicense(sessionId: string, licenseResponse: Uint8Array): Key[];
49
+ private encryptClientId;
50
+ private deriveKeys;
51
+ private deriveContext;
38
52
  }
39
- export { WIDEVINE_SID, KeyType, DeviceType } from "./const.js";
53
+ export { KeyType, DeviceType, LicenseType, SERVICE_CERTIFICATE_CHALLENGE } from "./const.js";
40
54
  export { Device } from "./device.js";
41
55
  export { Key } from "./key.js";
42
- export { PSSH } from "./pssh.js";
56
+ export { SystemID, PSSH } from "./pssh.js";
package/dist/index.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import { bytesToHex, concatBytes, equalBytes, numberToBytesBE, randomBytes } from "@noble/ciphers/utils.js";
2
- import { DeviceType } from "./const.js";
2
+ import { DeviceType, ROOT_CERT } from "./const.js";
3
3
  import { Session } from "./session.js";
4
4
  import { PSS, OAEP, mgf1 } from 'micro-rsa-dsa-dh/rsa.js';
5
5
  import { sha1 } from "@noble/hashes/legacy.js";
6
6
  import { pywidevine_license_protocol } from "./protos/license_protocol.js";
7
7
  import { hmac } from "@noble/hashes/hmac.js";
8
8
  import { sha256 } from "@noble/hashes/sha2.js";
9
- import { cmac } from "@noble/ciphers/aes.js";
9
+ import { cbc, cmac } from "@noble/ciphers/aes.js";
10
10
  import { Key } from "./key.js";
11
- const { LicenseType, LicenseRequest, ProtocolVersion, SignedMessage, License } = pywidevine_license_protocol;
11
+ import { decodePublicKey } from "./utils.js";
12
12
  const ENCRYPTION_LABEL = new Uint8Array([0x45, 0x4e, 0x43, 0x52, 0x59, 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x00]); // ENCRYPTION + \x00
13
13
  const ENCRYPTION_SIZE = new Uint8Array([0, 0, 0, 0x80]); // 128
14
14
  const AUTHENTICATION_LABEL = new Uint8Array([0x41, 0x55, 0x54, 0x48, 0x45, 0x4e, 0x54, 0x49, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x00]); // AUTHENTICATION + \x00
@@ -16,6 +16,7 @@ const AUTHENTICATION_SIZE = new Uint8Array([0, 0, 2, 0]); // 512
16
16
  /** Widevine Content Decryption Module (CDM) instance */
17
17
  export class CDM {
18
18
  device;
19
+ static MAX_SESSIONS = 16;
19
20
  sessions = new Map();
20
21
  signer;
21
22
  crypter;
@@ -30,6 +31,8 @@ export class CDM {
30
31
  }
31
32
  /** Open session */
32
33
  open() {
34
+ if (this.sessions.size > CDM.MAX_SESSIONS)
35
+ throw new Error("Too many sessions");
33
36
  const session = new Session(this.sessions.size + 1);
34
37
  const id = bytesToHex(session.id);
35
38
  this.sessions.set(id, session);
@@ -48,9 +51,10 @@ export class CDM {
48
51
  * Get license request (challenge)
49
52
  * @param sessionId Session ID
50
53
  * @param pssh PSSH object
51
- * @param license_type License type (default - `STREAMING`)
54
+ * @param licenseType License type (default - `STREAMING`)
55
+ * @param privacyMode Encrypt the Client ID using service certificate (If service certificate is set)
52
56
  */
53
- get_license_challenge(sessionId, pssh, license_type = LicenseType.STREAMING) {
57
+ getLicenseChallenge(sessionId, pssh, licenseType = pywidevine_license_protocol.LicenseType.STREAMING, privacyMode = true) {
54
58
  const session = this.sessions.get(sessionId);
55
59
  if (!session)
56
60
  throw new Error("Session identifier is invalid");
@@ -59,44 +63,95 @@ export class CDM {
59
63
  request_id = new TextEncoder().encode(bytesToHex(concatBytes(randomBytes(4), new Uint8Array(4), numberToBytesBE(session.number, 8).reverse())).toUpperCase());
60
64
  else
61
65
  request_id = randomBytes(16);
62
- const wvdPsshData = new LicenseRequest.ContentIdentification.WidevinePsshData();
63
- wvdPsshData.pssh_data = [pssh.init_data];
64
- wvdPsshData.license_type = license_type;
66
+ const wvdPsshData = new pywidevine_license_protocol.LicenseRequest.ContentIdentification.WidevinePsshData();
67
+ wvdPsshData.pssh_data = [pssh.initData];
68
+ wvdPsshData.license_type = licenseType;
65
69
  wvdPsshData.request_id = request_id;
66
- const contentId = new LicenseRequest.ContentIdentification();
70
+ const contentId = new pywidevine_license_protocol.LicenseRequest.ContentIdentification();
67
71
  contentId.widevine_pssh_data = wvdPsshData;
68
- const licenseRequest = new LicenseRequest();
69
- licenseRequest.client_id = this.device.client_id;
72
+ const licenseRequest = new pywidevine_license_protocol.LicenseRequest();
73
+ if (!(session.serviceCertificate && privacyMode))
74
+ licenseRequest.client_id = this.device.clientId;
75
+ if (session.serviceCertificate && privacyMode)
76
+ licenseRequest.encrypted_client_id = this.encryptClientId(this.device.clientId, session.serviceCertificate);
70
77
  licenseRequest.content_id = contentId;
71
- licenseRequest.type = LicenseRequest.RequestType.NEW;
78
+ licenseRequest.type = pywidevine_license_protocol.LicenseRequest.RequestType.NEW;
72
79
  licenseRequest.request_time = Math.round(Date.now() / 1000);
73
- licenseRequest.protocol_version = ProtocolVersion.VERSION_2_1;
80
+ licenseRequest.protocol_version = pywidevine_license_protocol.ProtocolVersion.VERSION_2_1;
74
81
  licenseRequest.key_control_nonce = Math.floor(Math.random() * 2000000) + 1;
75
82
  const licenseRequestSerialized = licenseRequest.serializeBinary();
76
- const signedLicenseRequest = new SignedMessage();
77
- signedLicenseRequest.type = SignedMessage.MessageType.LICENSE_REQUEST;
83
+ const signedLicenseRequest = new pywidevine_license_protocol.SignedMessage();
84
+ signedLicenseRequest.type = pywidevine_license_protocol.SignedMessage.MessageType.LICENSE_REQUEST;
78
85
  signedLicenseRequest.msg = licenseRequestSerialized;
79
86
  signedLicenseRequest.signature = this.signer.sign(this.device.privateKey, licenseRequestSerialized);
80
- session.context.set(bytesToHex(request_id), this.derive_context(licenseRequestSerialized));
87
+ session.context.set(bytesToHex(request_id), this.deriveContext(licenseRequestSerialized));
81
88
  return signedLicenseRequest.serializeBinary();
82
89
  }
90
+ /**
91
+ * Set service certificate for privacy mode
92
+ * @param sessionId Session ID
93
+ * @param certificate Service certificate (If none remove current)
94
+ */
95
+ setServiceCertificate(sessionId, certificate) {
96
+ const session = this.sessions.get(sessionId);
97
+ if (!session)
98
+ throw new Error("Session identifier is invalid");
99
+ let providerId;
100
+ if (!certificate) {
101
+ if (session.serviceCertificate) {
102
+ const drmCertificate = pywidevine_license_protocol.DrmCertificate.deserializeBinary(session.serviceCertificate.drm_certificate);
103
+ providerId = drmCertificate.provider_id;
104
+ }
105
+ else
106
+ providerId = null;
107
+ session.serviceCertificate = undefined;
108
+ return providerId;
109
+ }
110
+ let signedDrmCertificate;
111
+ try {
112
+ const signedMessage = pywidevine_license_protocol.SignedMessage.deserialize(certificate);
113
+ signedDrmCertificate = pywidevine_license_protocol.SignedDrmCertificate.deserialize(signedMessage.msg);
114
+ if (signedDrmCertificate.drm_certificate.length == 0)
115
+ throw new Error("");
116
+ }
117
+ catch (e) {
118
+ signedDrmCertificate = pywidevine_license_protocol.SignedDrmCertificate.deserialize(certificate);
119
+ }
120
+ if (!signedDrmCertificate.drm_certificate)
121
+ throw new Error("Can't decode DRM certificate");
122
+ if (!this.signer.verify(decodePublicKey(ROOT_CERT.public_key), signedDrmCertificate.drm_certificate, signedDrmCertificate.signature))
123
+ throw new Error("Signature mismatch");
124
+ const drmCertificate = pywidevine_license_protocol.DrmCertificate.deserializeBinary(signedDrmCertificate.drm_certificate);
125
+ session.serviceCertificate = signedDrmCertificate;
126
+ return drmCertificate.provider_id;
127
+ }
128
+ /**
129
+ * Get service certificate of session
130
+ * @param sessionId Session ID
131
+ */
132
+ getServiceCertificate(sessionId) {
133
+ const session = this.sessions.get(sessionId);
134
+ if (!session)
135
+ throw new Error("Session identifier is invalid");
136
+ return session.serviceCertificate ?? null;
137
+ }
83
138
  /**
84
139
  * Get keys from license response
85
140
  * @param sessionId Session ID
86
- * @param license_response License response
141
+ * @param licenseResponse License response
87
142
  */
88
- parse_license(sessionId, license_response) {
143
+ parseLicense(sessionId, licenseResponse) {
89
144
  const session = this.sessions.get(sessionId);
90
145
  if (!session)
91
146
  throw new Error("Session identifier is invalid");
92
- const license_message = SignedMessage.deserializeBinary(license_response);
93
- if (license_message.type != SignedMessage.MessageType.LICENSE)
147
+ const license_message = pywidevine_license_protocol.SignedMessage.deserializeBinary(licenseResponse);
148
+ if (license_message.type != pywidevine_license_protocol.SignedMessage.MessageType.LICENSE)
94
149
  throw new Error("Invalid message type");
95
- const license = License.deserializeBinary(license_message.msg);
150
+ const license = pywidevine_license_protocol.License.deserializeBinary(license_message.msg);
96
151
  const context = session.context.get(bytesToHex(license.id.request_id));
97
152
  if (!context)
98
153
  throw new Error("Cannot parse a license message without first making a license request");
99
- const [enc_key, mac_key_server, _] = this.derive_keys(context[0], context[1], this.crypter.decrypt(this.device.privateKey, license_message.session_key));
154
+ const [enc_key, mac_key_server, _] = this.deriveKeys(context[0], context[1], this.crypter.decrypt(this.device.privateKey, license_message.session_key));
100
155
  const computed_signature = hmac(sha256, mac_key_server, license_message.oemcrypto_core_message.length != 0
101
156
  ? concatBytes(license_message.oemcrypto_core_message, license_message.msg)
102
157
  : license_message.msg);
@@ -106,21 +161,32 @@ export class CDM {
106
161
  session.context.delete(bytesToHex(license.id.request_id));
107
162
  return session.keys;
108
163
  }
109
- derive_keys(enc_context, mac_context, key) {
164
+ encryptClientId(clientId, serviceCertificate, key, iv) {
165
+ const privacy_key = key ?? randomBytes(16), privacy_iv = iv ?? randomBytes(16);
166
+ const drmCertificate = pywidevine_license_protocol.DrmCertificate.deserializeBinary(serviceCertificate.drm_certificate);
167
+ const encryptClientIdentification = new pywidevine_license_protocol.EncryptedClientIdentification();
168
+ encryptClientIdentification.provider_id = drmCertificate.provider_id;
169
+ encryptClientIdentification.service_certificate_serial_number = drmCertificate.serial_number;
170
+ encryptClientIdentification.encrypted_client_id = cbc(privacy_key, privacy_iv).encrypt(clientId.serializeBinary());
171
+ encryptClientIdentification.encrypted_client_id_iv = privacy_iv;
172
+ encryptClientIdentification.encrypted_privacy_key = this.crypter.encrypt(decodePublicKey(drmCertificate.public_key), privacy_key);
173
+ return encryptClientIdentification;
174
+ }
175
+ deriveKeys(encContext, macContext, key) {
110
176
  const _derive = (session_key, context, counter) => cmac(session_key, concatBytes(numberToBytesBE(counter, 1), context));
111
- const enc_key = _derive(key, enc_context, 1);
112
- const mac_key_server = concatBytes(_derive(key, mac_context, 1), _derive(key, mac_context, 2));
113
- const mac_key_client = concatBytes(_derive(key, mac_context, 3), _derive(key, mac_context, 4));
177
+ const enc_key = _derive(key, encContext, 1);
178
+ const mac_key_server = concatBytes(_derive(key, macContext, 1), _derive(key, macContext, 2));
179
+ const mac_key_client = concatBytes(_derive(key, macContext, 3), _derive(key, macContext, 4));
114
180
  return [enc_key, mac_key_server, mac_key_client];
115
181
  }
116
- derive_context(message) {
182
+ deriveContext(message) {
117
183
  return [
118
184
  concatBytes(ENCRYPTION_LABEL, message, ENCRYPTION_SIZE),
119
185
  concatBytes(AUTHENTICATION_LABEL, message, AUTHENTICATION_SIZE)
120
186
  ];
121
187
  }
122
188
  }
123
- export { WIDEVINE_SID, KeyType, DeviceType } from "./const.js";
189
+ export { KeyType, DeviceType, LicenseType, SERVICE_CERTIFICATE_CHALLENGE } from "./const.js";
124
190
  export { Device } from "./device.js";
125
191
  export { Key } from "./key.js";
126
- export { PSSH } from "./pssh.js";
192
+ export { SystemID, PSSH } from "./pssh.js";
package/dist/pssh.d.ts CHANGED
@@ -1,15 +1,21 @@
1
1
  import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
+ /** System IDs */
3
+ export declare const SystemID: Record<"WIDEVINE" | "PLAYREADY", Readonly<Uint8Array>>;
4
+ export interface PlayReadyRecord {
5
+ type: number;
6
+ value: string;
7
+ }
2
8
  /** Protection System Specific Header (PSSH) object */
3
9
  export declare class PSSH {
4
10
  version: number;
5
11
  flags: number;
6
- system_id: Uint8Array;
12
+ systemId: Uint8Array;
7
13
  kids: Uint8Array[];
8
- init_data: Uint8Array;
14
+ initData: Uint8Array;
9
15
  /** Protection System Specific Header (PSSH) */
10
- constructor(version: number, flags: number, system_id: Uint8Array, kids: Uint8Array[] | undefined, init_data: Uint8Array);
11
- /** Get decoded payload (Widevine only) */
12
- get decoded(): pywidevine_license_protocol.WidevinePsshData | null;
16
+ constructor(version: number, flags: number, systemId: Uint8Array, kids: Uint8Array[] | undefined, initData: Uint8Array);
17
+ /** Get decoded payload (Widevine and PlayReady only) */
18
+ get decoded(): pywidevine_license_protocol.WidevinePsshData | PlayReadyRecord[] | null;
13
19
  /** Get PSSH object from bytes */
14
20
  static decode(pssh: Uint8Array): PSSH;
15
21
  }
package/dist/pssh.js CHANGED
@@ -1,30 +1,56 @@
1
- import { equalBytes } from "@noble/ciphers/utils.js";
1
+ import { bytesToNumberBE, equalBytes, hexToBytes } from "@noble/ciphers/utils.js";
2
2
  import { pywidevine_license_protocol } from "./protos/license_protocol.js";
3
- import { WIDEVINE_SID } from "./const.js";
3
+ /** System IDs */
4
+ export const SystemID = {
5
+ WIDEVINE: hexToBytes("edef8ba979d64acea3c827dcd51d21ed"),
6
+ PLAYREADY: hexToBytes("9a04f07998404286ab92e65be0885f95")
7
+ };
8
+ const bytesToNumberLE = (bytes) => bytesToNumberBE(new Uint8Array(bytes).reverse());
4
9
  /** Protection System Specific Header (PSSH) object */
5
10
  export class PSSH {
6
11
  version;
7
12
  flags;
8
- system_id;
13
+ systemId;
9
14
  kids;
10
- init_data;
15
+ initData;
11
16
  /** Protection System Specific Header (PSSH) */
12
- constructor(version, flags, system_id, kids = [], init_data) {
17
+ constructor(version, flags, systemId, kids = [], initData) {
13
18
  this.version = version;
14
19
  this.flags = flags;
15
- this.system_id = system_id;
20
+ this.systemId = systemId;
16
21
  this.kids = kids;
17
- this.init_data = init_data;
22
+ this.initData = initData;
18
23
  }
19
- /** Get decoded payload (Widevine only) */
24
+ /** Get decoded payload (Widevine and PlayReady only) */
20
25
  get decoded() {
21
- return equalBytes(this.system_id, WIDEVINE_SID) ? pywidevine_license_protocol.WidevinePsshData.deserialize(this.init_data) : null;
26
+ if (equalBytes(this.systemId, SystemID.WIDEVINE))
27
+ return pywidevine_license_protocol.WidevinePsshData.deserialize(this.initData);
28
+ else if (equalBytes(this.systemId, SystemID.PLAYREADY)) {
29
+ let offset = 0;
30
+ const proLength = bytesToNumberLE(this.initData.slice(offset, offset += 4));
31
+ if (proLength != BigInt(this.initData.length))
32
+ return null;
33
+ const proRecordCount = bytesToNumberLE(this.initData.slice(offset, offset += 2));
34
+ const records = [];
35
+ for (let _ = 0n; _ < proRecordCount; _++) {
36
+ const type = Number(bytesToNumberLE(this.initData.slice(offset, offset += 2)));
37
+ const length = Number(bytesToNumberLE(this.initData.slice(offset, offset += 2)));
38
+ if (type == 1)
39
+ records.push({
40
+ type,
41
+ value: new TextDecoder("utf-16le").decode(this.initData.slice(offset, offset += length))
42
+ });
43
+ }
44
+ return records;
45
+ }
46
+ else
47
+ return null;
22
48
  }
23
49
  /** Get PSSH object from bytes */
24
50
  static decode(pssh) {
25
51
  const rdr = new DataView(pssh.buffer, pssh.byteOffset);
26
- // const size = rdr.getUint32(offset); offset += 4;
27
52
  let offset = 4;
53
+ // const size = rdr.getUint32(offset); offset += 4;
28
54
  const box_header = new TextDecoder().decode(pssh.subarray(offset, offset + 4));
29
55
  offset += 4;
30
56
  if (box_header !== "pssh")
package/dist/session.d.ts CHANGED
@@ -3,7 +3,7 @@ import type { Key } from "./key.js";
3
3
  export declare class Session {
4
4
  number: number;
5
5
  id: Uint8Array;
6
- servicCertificate?: pywidevine_license_protocol.SignedDrmCertificate;
6
+ serviceCertificate?: pywidevine_license_protocol.SignedDrmCertificate;
7
7
  context: Map<string, Uint8Array<ArrayBufferLike>[]>;
8
8
  keys: Key[];
9
9
  constructor(number: number);
package/dist/session.js CHANGED
@@ -2,7 +2,7 @@ import { randomBytes } from "@noble/ciphers/utils.js";
2
2
  export class Session {
3
3
  number;
4
4
  id;
5
- servicCertificate;
5
+ serviceCertificate;
6
6
  context = new Map();
7
7
  keys = [];
8
8
  constructor(number) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/widevine",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",