@li0ard/widevine 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikolai Konovalov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,57 @@
1
+ <p align="center">
2
+ <a href="https://github.com/li0ard/widevine/">
3
+ <img src="https://raw.githubusercontent.com/li0ard/widevine/main/.github/logo.svg" alt="widevine logo" title="widevine" width="120" /><br>
4
+ </a><br>
5
+ <b>@li0ard/widevine</b><br>
6
+ <b>Simple Widevine CDM implementation</b>
7
+ <br>
8
+ <a href="https://li0ard.is-cool.dev/widevine">docs</a>
9
+ <br><br>
10
+ <a href="https://github.com/li0ard/widevine/blob/main/LICENSE"><img src="https://img.shields.io/github/license/li0ard/widevine" /></a>
11
+ <a href="https://npmjs.com/package/@li0ard/widevine"><img src="https://img.shields.io/npm/v/@li0ard/widevine" />
12
+ <br>
13
+ <hr>
14
+ </p>
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ # from NPM
20
+ npm i @li0ard/widevine
21
+
22
+ # from JSR
23
+ bunx jsr add @li0ard/widevine
24
+ ```
25
+
26
+ ## Example
27
+
28
+ ```ts
29
+ import { CDM, PSSH, Device, DeviceType, KeyType } from "@li0ard/widevine";
30
+
31
+ const device = Device.decode(
32
+ DeviceType.ANDROID,
33
+ Buffer.from("....", "base64"),
34
+ Buffer.from("....", "base64")
35
+ );
36
+
37
+ const cdm = new CDM(device);
38
+ const sessionId = cdm.open();
39
+
40
+ const pssh = PSSH.decode(Buffer.from("....", "base64"));
41
+
42
+ const challenge = cdm.get_license_challenge(sessionId, pssh);
43
+ const license = await (await fetch(`https://cwip-shaka-proxy.appspot.com/no_auth`, {
44
+ method: "POST",
45
+ body: challenge
46
+ })).arrayBuffer();
47
+
48
+ for(const key of cdm.parse_license(sessionId, new Uint8Array(license)))
49
+ console.log(`- [${KeyType[key.type]}] ${bytesToHex(key.kid)}:${bytesToHex(key.key)}`);
50
+
51
+ cdm.close(sessionId);
52
+ ```
53
+
54
+ ## Links
55
+
56
+ - [Widevine](https://widevine.com) - Widevine (and Widevine icon) by Google
57
+ - [pywidevine](https://github.com/devine-dl/pywidevine) - An Open Source Python Implementation of Widevine CDM (greatly inspired)
@@ -0,0 +1,10 @@
1
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
+ /** Widevine System ID */
3
+ export declare const WIDEVINE_SID: Readonly<Uint8Array>;
4
+ /** Device type */
5
+ export declare enum DeviceType {
6
+ CHROME = 1,
7
+ ANDROID = 2
8
+ }
9
+ /** Key type */
10
+ export declare const KeyType: typeof pywidevine_license_protocol.License.KeyContainer.KeyType;
package/dist/const.js ADDED
@@ -0,0 +1,11 @@
1
+ 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]);
4
+ /** Device type */
5
+ export var DeviceType;
6
+ (function (DeviceType) {
7
+ DeviceType[DeviceType["CHROME"] = 1] = "CHROME";
8
+ DeviceType[DeviceType["ANDROID"] = 2] = "ANDROID";
9
+ })(DeviceType || (DeviceType = {}));
10
+ /** Key type */
11
+ export const KeyType = pywidevine_license_protocol.License.KeyContainer.KeyType;
@@ -0,0 +1,25 @@
1
+ import type { PublicKey, PrivateKey } from 'micro-rsa-dsa-dh/rsa.js';
2
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
3
+ import type { DeviceType } from './const.js';
4
+ /** Device instance for CDM */
5
+ export declare class Device {
6
+ type: DeviceType;
7
+ privateKey: PrivateKey;
8
+ client_id: pywidevine_license_protocol.ClientIdentification;
9
+ /**
10
+ * Device instance for CDM
11
+ * @param type Device type
12
+ * @param privateKey Device private key
13
+ * @param client_id Device client identification
14
+ */
15
+ constructor(type: DeviceType, privateKey: PrivateKey, client_id: pywidevine_license_protocol.ClientIdentification);
16
+ /** Device public key */
17
+ get publicKey(): PublicKey;
18
+ /**
19
+ * Get device instance from dump
20
+ * @param type Device type
21
+ * @param client_id Device client identification blob (`client_id.bin`)
22
+ * @param privateKey Device private key blob (ASN.1 encoded)
23
+ */
24
+ static decode(type: DeviceType, client_id: Uint8Array, privateKey: Uint8Array): Device;
25
+ }
package/dist/device.js ADDED
@@ -0,0 +1,38 @@
1
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
+ import { decodePrivateKey, decodePublicKey, parseCerificate } from './utils.js';
3
+ /** Device instance for CDM */
4
+ export class Device {
5
+ type;
6
+ privateKey;
7
+ client_id;
8
+ /**
9
+ * Device instance for CDM
10
+ * @param type Device type
11
+ * @param privateKey Device private key
12
+ * @param client_id Device client identification
13
+ */
14
+ constructor(type, privateKey, client_id) {
15
+ this.type = type;
16
+ this.privateKey = privateKey;
17
+ this.client_id = client_id;
18
+ }
19
+ /** Device public key */
20
+ get publicKey() {
21
+ const certificate = parseCerificate(this.client_id.token);
22
+ if (!certificate.public_key)
23
+ throw new Error("Missing public key in DRM certificate");
24
+ return decodePublicKey(certificate.public_key);
25
+ }
26
+ /**
27
+ * Get device instance from dump
28
+ * @param type Device type
29
+ * @param client_id Device client identification blob (`client_id.bin`)
30
+ * @param privateKey Device private key blob (ASN.1 encoded)
31
+ */
32
+ static decode(type, client_id, privateKey) {
33
+ const clientId = pywidevine_license_protocol.ClientIdentification.deserialize(client_id);
34
+ if (!clientId.token)
35
+ throw new Error("Missing token in Client ID");
36
+ return new Device(type, decodePrivateKey(privateKey), clientId);
37
+ }
38
+ }
@@ -0,0 +1,42 @@
1
+ import type { Device } from "./device.js";
2
+ import type { PSSH } from "./pssh.js";
3
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
4
+ import { Key } from "./key.js";
5
+ /** Widevine Content Decryption Module (CDM) instance */
6
+ export declare class CDM {
7
+ device: Device;
8
+ private sessions;
9
+ private signer;
10
+ private crypter;
11
+ /**
12
+ * Widevine Content Decryption Module (CDM) instance
13
+ * @param device Device instance
14
+ */
15
+ constructor(device: Device);
16
+ /** Open session */
17
+ open(): string;
18
+ /**
19
+ * Close session
20
+ * @param sessionId Session ID
21
+ */
22
+ close(sessionId: string): void;
23
+ /**
24
+ * Get license request (challenge)
25
+ * @param sessionId Session ID
26
+ * @param pssh PSSH object
27
+ * @param license_type License type (default - `STREAMING`)
28
+ */
29
+ get_license_challenge(sessionId: string, pssh: PSSH, license_type?: pywidevine_license_protocol.LicenseType): Uint8Array;
30
+ /**
31
+ * Get keys from license response
32
+ * @param sessionId Session ID
33
+ * @param license_response License response
34
+ */
35
+ parse_license(sessionId: string, license_response: Uint8Array): Key[];
36
+ private derive_keys;
37
+ private derive_context;
38
+ }
39
+ export { WIDEVINE_SID, KeyType, DeviceType } from "./const.js";
40
+ export { Device } from "./device.js";
41
+ export { Key } from "./key.js";
42
+ export { PSSH } from "./pssh.js";
package/dist/index.js ADDED
@@ -0,0 +1,126 @@
1
+ import { bytesToHex, concatBytes, equalBytes, numberToBytesBE, randomBytes } from "@noble/ciphers/utils.js";
2
+ import { DeviceType } from "./const.js";
3
+ import { Session } from "./session.js";
4
+ import { PSS, OAEP, mgf1 } from 'micro-rsa-dsa-dh/rsa.js';
5
+ import { sha1 } from "@noble/hashes/legacy.js";
6
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
7
+ import { hmac } from "@noble/hashes/hmac.js";
8
+ import { sha256 } from "@noble/hashes/sha2.js";
9
+ import { cmac } from "@noble/ciphers/aes.js";
10
+ import { Key } from "./key.js";
11
+ const { LicenseType, LicenseRequest, ProtocolVersion, SignedMessage, License } = pywidevine_license_protocol;
12
+ const ENCRYPTION_LABEL = new Uint8Array([0x45, 0x4e, 0x43, 0x52, 0x59, 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x00]); // ENCRYPTION + \x00
13
+ const ENCRYPTION_SIZE = new Uint8Array([0, 0, 0, 0x80]); // 128
14
+ const AUTHENTICATION_LABEL = new Uint8Array([0x41, 0x55, 0x54, 0x48, 0x45, 0x4e, 0x54, 0x49, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x00]); // AUTHENTICATION + \x00
15
+ const AUTHENTICATION_SIZE = new Uint8Array([0, 0, 2, 0]); // 512
16
+ /** Widevine Content Decryption Module (CDM) instance */
17
+ export class CDM {
18
+ device;
19
+ sessions = new Map();
20
+ signer;
21
+ crypter;
22
+ /**
23
+ * Widevine Content Decryption Module (CDM) instance
24
+ * @param device Device instance
25
+ */
26
+ constructor(device) {
27
+ this.device = device;
28
+ this.signer = PSS(sha1, mgf1(sha1), 20);
29
+ this.crypter = OAEP(sha1, mgf1(sha1));
30
+ }
31
+ /** Open session */
32
+ open() {
33
+ const session = new Session(this.sessions.size + 1);
34
+ const id = bytesToHex(session.id);
35
+ this.sessions.set(id, session);
36
+ return id;
37
+ }
38
+ /**
39
+ * Close session
40
+ * @param sessionId Session ID
41
+ */
42
+ close(sessionId) {
43
+ if (!this.sessions.get(sessionId))
44
+ throw new Error("Session identifier is invalid");
45
+ this.sessions.delete(sessionId);
46
+ }
47
+ /**
48
+ * Get license request (challenge)
49
+ * @param sessionId Session ID
50
+ * @param pssh PSSH object
51
+ * @param license_type License type (default - `STREAMING`)
52
+ */
53
+ get_license_challenge(sessionId, pssh, license_type = LicenseType.STREAMING) {
54
+ const session = this.sessions.get(sessionId);
55
+ if (!session)
56
+ throw new Error("Session identifier is invalid");
57
+ let request_id;
58
+ if (this.device.type == DeviceType.ANDROID)
59
+ request_id = new TextEncoder().encode(bytesToHex(concatBytes(randomBytes(4), new Uint8Array(4), numberToBytesBE(session.number, 8).reverse())).toUpperCase());
60
+ else
61
+ request_id = randomBytes(16);
62
+ const wvdPsshData = new LicenseRequest.ContentIdentification.WidevinePsshData();
63
+ wvdPsshData.pssh_data = [pssh.init_data];
64
+ wvdPsshData.license_type = license_type;
65
+ wvdPsshData.request_id = request_id;
66
+ const contentId = new LicenseRequest.ContentIdentification();
67
+ contentId.widevine_pssh_data = wvdPsshData;
68
+ const licenseRequest = new LicenseRequest();
69
+ licenseRequest.client_id = this.device.client_id;
70
+ licenseRequest.content_id = contentId;
71
+ licenseRequest.type = LicenseRequest.RequestType.NEW;
72
+ licenseRequest.request_time = Math.round(Date.now() / 1000);
73
+ licenseRequest.protocol_version = ProtocolVersion.VERSION_2_1;
74
+ licenseRequest.key_control_nonce = Math.floor(Math.random() * 2000000) + 1;
75
+ const licenseRequestSerialized = licenseRequest.serializeBinary();
76
+ const signedLicenseRequest = new SignedMessage();
77
+ signedLicenseRequest.type = SignedMessage.MessageType.LICENSE_REQUEST;
78
+ signedLicenseRequest.msg = licenseRequestSerialized;
79
+ signedLicenseRequest.signature = this.signer.sign(this.device.privateKey, licenseRequestSerialized);
80
+ session.context.set(bytesToHex(request_id), this.derive_context(licenseRequestSerialized));
81
+ return signedLicenseRequest.serializeBinary();
82
+ }
83
+ /**
84
+ * Get keys from license response
85
+ * @param sessionId Session ID
86
+ * @param license_response License response
87
+ */
88
+ parse_license(sessionId, license_response) {
89
+ const session = this.sessions.get(sessionId);
90
+ if (!session)
91
+ throw new Error("Session identifier is invalid");
92
+ const license_message = SignedMessage.deserializeBinary(license_response);
93
+ if (license_message.type != SignedMessage.MessageType.LICENSE)
94
+ throw new Error("Invalid message type");
95
+ const license = License.deserializeBinary(license_message.msg);
96
+ const context = session.context.get(bytesToHex(license.id.request_id));
97
+ if (!context)
98
+ 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));
100
+ const computed_signature = hmac(sha256, mac_key_server, license_message.oemcrypto_core_message.length != 0
101
+ ? concatBytes(license_message.oemcrypto_core_message, license_message.msg)
102
+ : license_message.msg);
103
+ if (!equalBytes(computed_signature, license_message.signature))
104
+ throw new Error("Signature mismatch on license message");
105
+ session.keys = license.key.map(i => Key.fromContainer(i, enc_key));
106
+ session.context.delete(bytesToHex(license.id.request_id));
107
+ return session.keys;
108
+ }
109
+ derive_keys(enc_context, mac_context, key) {
110
+ 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));
114
+ return [enc_key, mac_key_server, mac_key_client];
115
+ }
116
+ derive_context(message) {
117
+ return [
118
+ concatBytes(ENCRYPTION_LABEL, message, ENCRYPTION_SIZE),
119
+ concatBytes(AUTHENTICATION_LABEL, message, AUTHENTICATION_SIZE)
120
+ ];
121
+ }
122
+ }
123
+ export { WIDEVINE_SID, KeyType, DeviceType } from "./const.js";
124
+ export { Device } from "./device.js";
125
+ export { Key } from "./key.js";
126
+ export { PSSH } from "./pssh.js";
package/dist/key.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
2
+ /** Key instance */
3
+ export declare class Key {
4
+ type: pywidevine_license_protocol.License.KeyContainer.KeyType;
5
+ kid: Uint8Array;
6
+ key: Uint8Array;
7
+ /**
8
+ * Key instance
9
+ * @param type Type
10
+ * @param kid ID
11
+ * @param key Key
12
+ */
13
+ constructor(type: pywidevine_license_protocol.License.KeyContainer.KeyType, kid: Uint8Array, key: Uint8Array);
14
+ /**
15
+ * Get key from license key container
16
+ * @param key Key container
17
+ * @param encKey Decryption key
18
+ */
19
+ static fromContainer(key: pywidevine_license_protocol.License.KeyContainer, encKey: Uint8Array): Key;
20
+ }
package/dist/key.js ADDED
@@ -0,0 +1,27 @@
1
+ import { cbc } from "@noble/ciphers/aes.js";
2
+ import { pywidevine_license_protocol } from "./protos/license_protocol.js";
3
+ /** Key instance */
4
+ export class Key {
5
+ type;
6
+ kid;
7
+ key;
8
+ /**
9
+ * Key instance
10
+ * @param type Type
11
+ * @param kid ID
12
+ * @param key Key
13
+ */
14
+ constructor(type, kid, key) {
15
+ this.type = type;
16
+ this.kid = kid;
17
+ this.key = key;
18
+ }
19
+ /**
20
+ * Get key from license key container
21
+ * @param key Key container
22
+ * @param encKey Decryption key
23
+ */
24
+ static fromContainer(key, encKey) {
25
+ return new Key(key.type, key.id, cbc(encKey, key.iv).decrypt(key.key));
26
+ }
27
+ }