electron-cast 0.3.1 → 0.4.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/CHANGELOG.md +60 -0
- package/README.md +123 -14
- package/dist/cjs/client/auth/build-time.d.ts +7 -0
- package/dist/cjs/client/auth/build-time.js +11 -0
- package/dist/cjs/client/auth/build-time.js.map +1 -0
- package/dist/cjs/client/auth/cast-auth.d.ts +44 -0
- package/dist/cjs/client/auth/cast-auth.js +145 -0
- package/dist/cjs/client/auth/cast-auth.js.map +1 -0
- package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
- package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/cjs/client/auth/crl.d.ts +45 -0
- package/dist/cjs/client/auth/crl.js +227 -0
- package/dist/cjs/client/auth/crl.js.map +1 -0
- package/dist/cjs/client/auth/der.d.ts +62 -0
- package/dist/cjs/client/auth/der.js +208 -0
- package/dist/cjs/client/auth/der.js.map +1 -0
- package/dist/cjs/client/auth/names.d.ts +52 -0
- package/dist/cjs/client/auth/names.js +447 -0
- package/dist/cjs/client/auth/names.js.map +1 -0
- package/dist/cjs/client/auth/path-validator.d.ts +29 -0
- package/dist/cjs/client/auth/path-validator.js +421 -0
- package/dist/cjs/client/auth/path-validator.js.map +1 -0
- package/dist/cjs/client/auth/trust-data.d.ts +24 -0
- package/dist/cjs/client/auth/trust-data.js +97 -0
- package/dist/cjs/client/auth/trust-data.js.map +1 -0
- package/dist/cjs/client/auth/x509.d.ts +109 -0
- package/dist/cjs/client/auth/x509.js +442 -0
- package/dist/cjs/client/auth/x509.js.map +1 -0
- package/dist/cjs/client/cast-client.d.ts +111 -0
- package/dist/cjs/client/cast-client.js +505 -0
- package/dist/cjs/client/cast-client.js.map +1 -0
- package/dist/cjs/client/errors.d.ts +28 -0
- package/dist/cjs/client/errors.js +50 -0
- package/dist/cjs/client/errors.js.map +1 -0
- package/dist/cjs/client/index.d.ts +6 -0
- package/dist/cjs/client/index.js +21 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/client/namespaces.d.ts +9 -0
- package/dist/cjs/client/namespaces.js +13 -0
- package/dist/cjs/client/namespaces.js.map +1 -0
- package/dist/cjs/client/types.d.ts +27 -0
- package/dist/cjs/client/types.js +3 -0
- package/dist/cjs/client/types.js.map +1 -0
- package/dist/cjs/discovery/cast-discovery.d.ts +103 -0
- package/dist/cjs/discovery/cast-discovery.js +323 -0
- package/dist/cjs/discovery/cast-discovery.js.map +1 -0
- package/dist/cjs/discovery/device-cache.d.ts +45 -0
- package/dist/cjs/discovery/device-cache.js +240 -0
- package/dist/cjs/discovery/device-cache.js.map +1 -0
- package/dist/cjs/discovery/dns-packet.d.ts +70 -0
- package/dist/cjs/discovery/dns-packet.js +251 -0
- package/dist/cjs/discovery/dns-packet.js.map +1 -0
- package/dist/cjs/discovery/index.d.ts +4 -0
- package/dist/cjs/discovery/index.js +21 -0
- package/dist/cjs/discovery/index.js.map +1 -0
- package/dist/cjs/discovery/udp-socket.d.ts +16 -0
- package/dist/cjs/discovery/udp-socket.js +163 -0
- package/dist/cjs/discovery/udp-socket.js.map +1 -0
- package/dist/cjs/electron/main.d.ts +86 -0
- package/dist/cjs/electron/main.js +408 -0
- package/dist/cjs/electron/main.js.map +1 -0
- package/dist/cjs/electron/manager-api.d.ts +132 -0
- package/dist/cjs/electron/manager-api.js +207 -0
- package/dist/cjs/electron/manager-api.js.map +1 -0
- package/dist/cjs/electron/preload.d.ts +39 -0
- package/dist/cjs/electron/preload.js +96 -0
- package/dist/cjs/electron/preload.js.map +1 -0
- package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
- package/dist/cjs/electron/presentation-envelope.js +318 -0
- package/dist/cjs/electron/presentation-envelope.js.map +1 -0
- package/dist/cjs/electron/presentation-handler.d.ts +100 -0
- package/dist/cjs/electron/presentation-handler.js +499 -0
- package/dist/cjs/electron/presentation-handler.js.map +1 -0
- package/dist/cjs/electron/session-service.d.ts +174 -0
- package/dist/cjs/electron/session-service.js +891 -0
- package/dist/cjs/electron/session-service.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/polyfill/bridge-types.d.ts +86 -0
- package/dist/cjs/polyfill/bridge-types.js +3 -0
- package/dist/cjs/polyfill/bridge-types.js.map +1 -0
- package/dist/cjs/polyfill/install.d.ts +12 -0
- package/dist/cjs/polyfill/install.js +1569 -0
- package/dist/cjs/polyfill/install.js.map +1 -0
- package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
- package/dist/cjs/polyfill/presentation-standin.js +279 -0
- package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
- package/dist/cjs/protocol/cast-message.d.ts +20 -0
- package/dist/cjs/protocol/cast-message.js +171 -0
- package/dist/cjs/protocol/cast-message.js.map +1 -0
- package/dist/cjs/protocol/device-auth.d.ts +61 -0
- package/dist/cjs/protocol/device-auth.js +113 -0
- package/dist/cjs/protocol/device-auth.js.map +1 -0
- package/dist/cjs/protocol/errors.d.ts +4 -0
- package/dist/cjs/protocol/errors.js +12 -0
- package/dist/cjs/protocol/errors.js.map +1 -0
- package/dist/cjs/protocol/framing.d.ts +18 -0
- package/dist/cjs/protocol/framing.js +100 -0
- package/dist/cjs/protocol/framing.js.map +1 -0
- package/dist/cjs/protocol/index.d.ts +4 -0
- package/dist/cjs/protocol/index.js +21 -0
- package/dist/cjs/protocol/index.js.map +1 -0
- package/dist/cjs/protocol/proto-wire.d.ts +11 -0
- package/dist/cjs/protocol/proto-wire.js +64 -0
- package/dist/cjs/protocol/proto-wire.js.map +1 -0
- package/dist/client/auth/build-time.d.ts +7 -0
- package/dist/client/auth/build-time.js +8 -0
- package/dist/client/auth/build-time.js.map +1 -0
- package/dist/client/auth/cast-auth.d.ts +44 -0
- package/dist/client/auth/cast-auth.js +141 -0
- package/dist/client/auth/cast-auth.js.map +1 -0
- package/dist/client/auth/cast-cert-validator.d.ts +34 -0
- package/dist/client/auth/cast-cert-validator.js +86 -0
- package/dist/client/auth/cast-cert-validator.js.map +1 -0
- package/dist/client/auth/crl.d.ts +45 -0
- package/dist/client/auth/crl.js +221 -0
- package/dist/client/auth/crl.js.map +1 -0
- package/dist/client/auth/der.d.ts +62 -0
- package/dist/client/auth/der.js +196 -0
- package/dist/client/auth/der.js.map +1 -0
- package/dist/client/auth/names.d.ts +52 -0
- package/dist/client/auth/names.js +438 -0
- package/dist/client/auth/names.js.map +1 -0
- package/dist/client/auth/path-validator.d.ts +29 -0
- package/dist/client/auth/path-validator.js +417 -0
- package/dist/client/auth/path-validator.js.map +1 -0
- package/dist/client/auth/trust-data.d.ts +24 -0
- package/dist/client/auth/trust-data.js +94 -0
- package/dist/client/auth/trust-data.js.map +1 -0
- package/dist/client/auth/x509.d.ts +109 -0
- package/dist/client/auth/x509.js +430 -0
- package/dist/client/auth/x509.js.map +1 -0
- package/dist/client/cast-client.d.ts +20 -1
- package/dist/client/cast-client.js +74 -3
- package/dist/client/cast-client.js.map +1 -1
- package/dist/client/errors.d.ts +11 -0
- package/dist/client/errors.js +13 -0
- package/dist/client/errors.js.map +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/discovery/cast-discovery.d.ts +21 -2
- package/dist/discovery/cast-discovery.js +94 -14
- package/dist/discovery/cast-discovery.js.map +1 -1
- package/dist/discovery/device-cache.d.ts +6 -1
- package/dist/discovery/device-cache.js +31 -7
- package/dist/discovery/device-cache.js.map +1 -1
- package/dist/discovery/udp-socket.d.ts +5 -1
- package/dist/discovery/udp-socket.js +56 -5
- package/dist/discovery/udp-socket.js.map +1 -1
- package/dist/electron/main.d.ts +26 -4
- package/dist/electron/main.js +191 -54
- package/dist/electron/main.js.map +1 -1
- package/dist/electron/manager-api.d.ts +132 -0
- package/dist/electron/manager-api.js +198 -0
- package/dist/electron/manager-api.js.map +1 -0
- package/dist/electron/preload.d.ts +5 -0
- package/dist/electron/preload.js +9 -1
- package/dist/electron/preload.js.map +1 -1
- package/dist/electron/presentation-envelope.js +7 -1
- package/dist/electron/presentation-envelope.js.map +1 -1
- package/dist/electron/presentation-handler.js +16 -9
- package/dist/electron/presentation-handler.js.map +1 -1
- package/dist/electron/session-service.d.ts +42 -1
- package/dist/electron/session-service.js +226 -23
- package/dist/electron/session-service.js.map +1 -1
- package/dist/polyfill/presentation-standin.js +3 -2
- package/dist/polyfill/presentation-standin.js.map +1 -1
- package/dist/protocol/device-auth.d.ts +61 -0
- package/dist/protocol/device-auth.js +106 -0
- package/dist/protocol/device-auth.js.map +1 -0
- package/dist/protocol/index.d.ts +1 -0
- package/dist/protocol/index.js +1 -0
- package/dist/protocol/index.js.map +1 -1
- package/dist/protocol/proto-wire.d.ts +11 -0
- package/dist/protocol/proto-wire.js +60 -0
- package/dist/protocol/proto-wire.js.map +1 -0
- package/package.json +47 -12
- package/src/client/auth/build-time.ts +7 -0
- package/src/client/auth/cast-auth.ts +184 -0
- package/src/client/auth/cast-cert-validator.ts +133 -0
- package/src/client/auth/crl.ts +228 -0
- package/src/client/auth/der.ts +193 -0
- package/src/client/auth/names.ts +411 -0
- package/src/client/auth/path-validator.ts +449 -0
- package/src/client/auth/trust-data.ts +95 -0
- package/src/client/auth/x509.ts +488 -0
- package/src/client/cast-client.ts +85 -3
- package/src/client/errors.ts +29 -0
- package/src/client/index.ts +2 -0
- package/src/discovery/cast-discovery.ts +104 -19
- package/src/discovery/device-cache.ts +38 -8
- package/src/discovery/udp-socket.ts +63 -4
- package/src/electron/main.ts +218 -59
- package/src/electron/manager-api.ts +312 -0
- package/src/electron/preload.ts +15 -1
- package/src/electron/presentation-envelope.ts +7 -1
- package/src/electron/presentation-handler.ts +16 -10
- package/src/electron/session-service.ts +230 -26
- package/src/polyfill/presentation-standin.ts +3 -2
- package/src/protocol/device-auth.ts +126 -0
- package/src/protocol/index.ts +1 -0
- package/src/protocol/proto-wire.ts +55 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { CastProtocolError } from "./errors.js";
|
|
2
|
+
import { forEachField, lengthDelimited } from "./proto-wire.js";
|
|
3
|
+
/**
|
|
4
|
+
* The device authentication messages of cast_channel.proto (DeviceAuthMessage and its parts), hand-coded like
|
|
5
|
+
* {@link ./cast-message.ts}. They travel as the binary payload of a CastMessage on the deviceauth namespace.
|
|
6
|
+
*/
|
|
7
|
+
/** SignatureAlgorithm enum values. */
|
|
8
|
+
export const SignatureAlgorithm = { UNSPECIFIED: 0, RSASSA_PKCS1v15: 1, RSASSA_PSS: 2 };
|
|
9
|
+
/** HashAlgorithm enum values. */
|
|
10
|
+
export const HashAlgorithm = { SHA1: 0, SHA256: 1 };
|
|
11
|
+
/** AuthError.ErrorType enum values. */
|
|
12
|
+
export const AuthErrorType = { INTERNAL_ERROR: 0, NO_TLS: 1, SIGNATURE_ALGORITHM_UNAVAILABLE: 2 };
|
|
13
|
+
/** Encode a DeviceAuthMessage carrying only an AuthChallenge (the sender's half). */
|
|
14
|
+
export function encodeAuthChallenge(challenge) {
|
|
15
|
+
const inner = [];
|
|
16
|
+
if (challenge.signatureAlgorithm !== undefined)
|
|
17
|
+
inner.push(Buffer.from([0x08, challenge.signatureAlgorithm]));
|
|
18
|
+
inner.push(lengthDelimited(2, challenge.senderNonce));
|
|
19
|
+
if (challenge.hashAlgorithm !== undefined)
|
|
20
|
+
inner.push(Buffer.from([0x18, challenge.hashAlgorithm]));
|
|
21
|
+
// DeviceAuthMessage.challenge is field 1.
|
|
22
|
+
return lengthDelimited(1, Buffer.concat(inner));
|
|
23
|
+
}
|
|
24
|
+
/** Encode an AuthResponse inside a DeviceAuthMessage. Used by tests and fake receivers. */
|
|
25
|
+
export function encodeAuthResponse(response) {
|
|
26
|
+
const inner = [lengthDelimited(1, response.signature), lengthDelimited(2, response.clientAuthCertificate)];
|
|
27
|
+
for (const cert of response.intermediateCertificates ?? [])
|
|
28
|
+
inner.push(lengthDelimited(3, cert));
|
|
29
|
+
if (response.signatureAlgorithm !== undefined)
|
|
30
|
+
inner.push(Buffer.from([0x20, response.signatureAlgorithm]));
|
|
31
|
+
if (response.senderNonce !== undefined)
|
|
32
|
+
inner.push(lengthDelimited(5, response.senderNonce));
|
|
33
|
+
if (response.hashAlgorithm !== undefined)
|
|
34
|
+
inner.push(Buffer.from([0x30, response.hashAlgorithm]));
|
|
35
|
+
if (response.crl !== undefined)
|
|
36
|
+
inner.push(lengthDelimited(7, response.crl));
|
|
37
|
+
return lengthDelimited(2, Buffer.concat(inner));
|
|
38
|
+
}
|
|
39
|
+
/** Encode a DeviceAuthMessage carrying an AuthError. Used by tests and fake receivers. */
|
|
40
|
+
export function encodeAuthError(errorType) {
|
|
41
|
+
return lengthDelimited(3, Buffer.from([0x08, errorType]));
|
|
42
|
+
}
|
|
43
|
+
/** Decode a DeviceAuthMessage, keeping only what a sender needs. Throws {@link CastProtocolError} on malformed input. */
|
|
44
|
+
export function decodeDeviceAuthMessage(bytes) {
|
|
45
|
+
const out = {};
|
|
46
|
+
forEachField(bytes, (number, wireType, value) => {
|
|
47
|
+
if (number === 2 && wireType === 2)
|
|
48
|
+
out.response = decodeAuthResponse(value);
|
|
49
|
+
else if (number === 3 && wireType === 2)
|
|
50
|
+
out.error = decodeAuthError(value);
|
|
51
|
+
});
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
function decodeAuthResponse(bytes) {
|
|
55
|
+
let signature;
|
|
56
|
+
let clientAuthCertificate;
|
|
57
|
+
const intermediateCertificates = [];
|
|
58
|
+
const response = {
|
|
59
|
+
signature: Buffer.alloc(0),
|
|
60
|
+
clientAuthCertificate: Buffer.alloc(0),
|
|
61
|
+
intermediateCertificates,
|
|
62
|
+
signatureAlgorithm: SignatureAlgorithm.RSASSA_PKCS1v15,
|
|
63
|
+
senderNonce: Buffer.alloc(0),
|
|
64
|
+
hashAlgorithm: HashAlgorithm.SHA1,
|
|
65
|
+
crl: undefined,
|
|
66
|
+
};
|
|
67
|
+
forEachField(bytes, (number, wireType, value) => {
|
|
68
|
+
if (wireType === 2) {
|
|
69
|
+
const data = Buffer.from(value);
|
|
70
|
+
if (number === 1)
|
|
71
|
+
signature = data;
|
|
72
|
+
else if (number === 2)
|
|
73
|
+
clientAuthCertificate = data;
|
|
74
|
+
else if (number === 3)
|
|
75
|
+
intermediateCertificates.push(data);
|
|
76
|
+
else if (number === 5)
|
|
77
|
+
response.senderNonce = data;
|
|
78
|
+
else if (number === 7)
|
|
79
|
+
response.crl = data;
|
|
80
|
+
}
|
|
81
|
+
else if (wireType === 0) {
|
|
82
|
+
if (number === 4)
|
|
83
|
+
response.signatureAlgorithm = Number(value);
|
|
84
|
+
else if (number === 6)
|
|
85
|
+
response.hashAlgorithm = Number(value);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
if (signature === undefined)
|
|
89
|
+
throw new CastProtocolError("AuthResponse is missing required field signature");
|
|
90
|
+
if (clientAuthCertificate === undefined)
|
|
91
|
+
throw new CastProtocolError("AuthResponse is missing required field client_auth_certificate");
|
|
92
|
+
response.signature = signature;
|
|
93
|
+
response.clientAuthCertificate = clientAuthCertificate;
|
|
94
|
+
return response;
|
|
95
|
+
}
|
|
96
|
+
function decodeAuthError(bytes) {
|
|
97
|
+
let errorType;
|
|
98
|
+
forEachField(bytes, (number, wireType, value) => {
|
|
99
|
+
if (number === 1 && wireType === 0)
|
|
100
|
+
errorType = Number(value);
|
|
101
|
+
});
|
|
102
|
+
if (errorType === undefined)
|
|
103
|
+
throw new CastProtocolError("AuthError is missing required field error_type");
|
|
104
|
+
return { errorType };
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=device-auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"device-auth.js","sourceRoot":"","sources":["../../src/protocol/device-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,YAAY,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,QAAQ,CAAC,GAAG,GAAG,IAAI,CAAC;QAC7C,CAAC;aAAM,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;YAC1B,IAAI,MAAM,KAAK,CAAC;gBAAE,QAAQ,CAAC,kBAAkB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;iBACzD,IAAI,MAAM,KAAK,CAAC;gBAAE,QAAQ,CAAC,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;QAChE,CAAC;IACH,CAAC,CAAC,CAAC;IACH,IAAI,SAAS,KAAK,SAAS;QAAE,MAAM,IAAI,iBAAiB,CAAC,kDAAkD,CAAC,CAAC;IAC7G,IAAI,qBAAqB,KAAK,SAAS;QAAE,MAAM,IAAI,iBAAiB,CAAC,gEAAgE,CAAC,CAAC;IACvI,QAAQ,CAAC,SAAS,GAAG,SAAS,CAAC;IAC/B,QAAQ,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;IACvD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,SAAS,eAAe,CAAC,KAAiB;IACxC,IAAI,SAA6B,CAAC;IAClC,YAAY,CAAC,KAAK,EAAE,CAAC,MAAM,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;QAC9C,IAAI,MAAM,KAAK,CAAC,IAAI,QAAQ,KAAK,CAAC;YAAE,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAChE,CAAC,CAAC,CAAC;IACH,IAAI,SAAS,KAAK,SAAS;QAAE,MAAM,IAAI,iBAAiB,CAAC,gDAAgD,CAAC,CAAC;IAC3G,OAAO,EAAE,SAAS,EAAE,CAAC;AACvB,CAAC"}
|
package/dist/protocol/index.d.ts
CHANGED
package/dist/protocol/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/protocol/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal protobuf wire reading shared by the device authentication messages and the Cast revocation list. Not
|
|
3
|
+
* exported from the package: only the typed decoders built on it are.
|
|
4
|
+
*/
|
|
5
|
+
/** Append a length-delimited field (wire type 2). */
|
|
6
|
+
export declare function lengthDelimited(fieldNumber: number, payload: Uint8Array): Buffer;
|
|
7
|
+
/**
|
|
8
|
+
* Visit each field in order. Varints arrive as bigint (so uint64 survives), length-delimited fields as byte slices;
|
|
9
|
+
* fixed-width fields are skipped. Throws {@link CastProtocolError} on truncated or unsupported input.
|
|
10
|
+
*/
|
|
11
|
+
export declare function forEachField(bytes: Uint8Array, visit: (number: number, wireType: number, value: bigint | Uint8Array) => void): void;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { CastProtocolError } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal protobuf wire reading shared by the device authentication messages and the Cast revocation list. Not
|
|
4
|
+
* exported from the package: only the typed decoders built on it are.
|
|
5
|
+
*/
|
|
6
|
+
/** Append a length-delimited field (wire type 2). */
|
|
7
|
+
export function lengthDelimited(fieldNumber, payload) {
|
|
8
|
+
const header = [(fieldNumber << 3) | 2];
|
|
9
|
+
let length = payload.length;
|
|
10
|
+
while (length >= 0x80) {
|
|
11
|
+
header.push((length & 0x7f) | 0x80);
|
|
12
|
+
length = Math.floor(length / 128);
|
|
13
|
+
}
|
|
14
|
+
header.push(length);
|
|
15
|
+
return Buffer.concat([Buffer.from(header), payload]);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Visit each field in order. Varints arrive as bigint (so uint64 survives), length-delimited fields as byte slices;
|
|
19
|
+
* fixed-width fields are skipped. Throws {@link CastProtocolError} on truncated or unsupported input.
|
|
20
|
+
*/
|
|
21
|
+
export function forEachField(bytes, visit) {
|
|
22
|
+
let pos = 0;
|
|
23
|
+
const varint = () => {
|
|
24
|
+
let value = 0n;
|
|
25
|
+
for (let i = 0; i < 10; i++) {
|
|
26
|
+
const byte = bytes[pos++];
|
|
27
|
+
if (byte === undefined)
|
|
28
|
+
throw new CastProtocolError("varint runs past end of input");
|
|
29
|
+
value |= BigInt(byte & 0x7f) << BigInt(7 * i);
|
|
30
|
+
if ((byte & 0x80) === 0)
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
throw new CastProtocolError("varint longer than 10 bytes");
|
|
34
|
+
};
|
|
35
|
+
while (pos < bytes.length) {
|
|
36
|
+
const tag = varint();
|
|
37
|
+
const number = Number(tag >> 3n);
|
|
38
|
+
const wireType = Number(tag & 7n);
|
|
39
|
+
if (wireType === 0) {
|
|
40
|
+
visit(number, 0, varint());
|
|
41
|
+
}
|
|
42
|
+
else if (wireType === 2) {
|
|
43
|
+
const length = Number(varint());
|
|
44
|
+
if (pos + length > bytes.length)
|
|
45
|
+
throw new CastProtocolError("length-delimited field runs past end of input");
|
|
46
|
+
visit(number, 2, bytes.subarray(pos, pos + length));
|
|
47
|
+
pos += length;
|
|
48
|
+
}
|
|
49
|
+
else if (wireType === 1 || wireType === 5) {
|
|
50
|
+
const size = wireType === 1 ? 8 : 4;
|
|
51
|
+
if (pos + size > bytes.length)
|
|
52
|
+
throw new CastProtocolError("truncated fixed-size field");
|
|
53
|
+
pos += size;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw new CastProtocolError(`unsupported wire type ${wireType}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=proto-wire.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proto-wire.js","sourceRoot":"","sources":["../../src/protocol/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E,CAAC;IACH,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "electron-cast",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Chromecast for Electron: lets pages run Google's own Cast SDK by supplying the Media Router Electron lacks, with device discovery and the Cast v2 protocol in the main process.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"electron",
|
|
@@ -20,24 +20,48 @@
|
|
|
20
20
|
"type": "module",
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
|
-
"
|
|
24
|
-
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/cjs/index.d.ts",
|
|
29
|
+
"default": "./dist/cjs/index.js"
|
|
30
|
+
}
|
|
25
31
|
},
|
|
26
32
|
"./main": {
|
|
27
|
-
"
|
|
28
|
-
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./dist/electron/main.d.ts",
|
|
35
|
+
"default": "./dist/electron/main.js"
|
|
36
|
+
},
|
|
37
|
+
"require": {
|
|
38
|
+
"types": "./dist/cjs/electron/main.d.ts",
|
|
39
|
+
"default": "./dist/cjs/electron/main.js"
|
|
40
|
+
}
|
|
29
41
|
},
|
|
30
42
|
"./preload": {
|
|
31
|
-
"
|
|
32
|
-
|
|
43
|
+
"import": {
|
|
44
|
+
"types": "./dist/electron/preload.d.ts",
|
|
45
|
+
"default": "./dist/electron/preload.js"
|
|
46
|
+
},
|
|
47
|
+
"require": {
|
|
48
|
+
"types": "./dist/cjs/electron/preload.d.ts",
|
|
49
|
+
"default": "./dist/cjs/electron/preload.js"
|
|
50
|
+
}
|
|
33
51
|
},
|
|
34
52
|
"./polyfill": {
|
|
35
|
-
"
|
|
36
|
-
|
|
53
|
+
"import": {
|
|
54
|
+
"types": "./dist/polyfill/install.d.ts",
|
|
55
|
+
"default": "./dist/polyfill/install.js"
|
|
56
|
+
},
|
|
57
|
+
"require": {
|
|
58
|
+
"types": "./dist/cjs/polyfill/install.d.ts",
|
|
59
|
+
"default": "./dist/cjs/polyfill/install.js"
|
|
60
|
+
}
|
|
37
61
|
},
|
|
38
62
|
"./package.json": "./package.json"
|
|
39
63
|
},
|
|
40
|
-
"main": "./dist/index.js",
|
|
64
|
+
"main": "./dist/cjs/index.js",
|
|
41
65
|
"types": "./dist/index.d.ts",
|
|
42
66
|
"engines": {
|
|
43
67
|
"node": ">=20"
|
|
@@ -48,16 +72,27 @@
|
|
|
48
72
|
"CHANGELOG.md"
|
|
49
73
|
],
|
|
50
74
|
"scripts": {
|
|
51
|
-
"build": "tsc -p tsconfig.build.json",
|
|
75
|
+
"build": "tsc -p tsconfig.build.json && tsc -p tsconfig.cjs.json && node scripts/mark-cjs.mjs && node scripts/stamp-build-time.mjs",
|
|
52
76
|
"typecheck": "tsc --noEmit",
|
|
53
77
|
"test": "node --test --test-force-exit --test-timeout=20000 \"test/**/*.test.ts\"",
|
|
54
78
|
"lint": "oxlint src test",
|
|
79
|
+
"canary": "node tools/sdk-canary.mjs",
|
|
55
80
|
"check": "npm run typecheck && npm run lint && npm test",
|
|
56
|
-
"prepack": "npm run build"
|
|
81
|
+
"prepack": "npm run build",
|
|
82
|
+
"smoke": "npm run build && node smoke/run.mjs"
|
|
57
83
|
},
|
|
58
84
|
"devDependencies": {
|
|
59
85
|
"@types/node": "^24.0.0",
|
|
86
|
+
"esbuild": "^0.28.2",
|
|
60
87
|
"oxlint": "^1.89.0",
|
|
61
88
|
"typescript": "^6.0.3"
|
|
89
|
+
},
|
|
90
|
+
"peerDependencies": {
|
|
91
|
+
"electron": ">=35"
|
|
92
|
+
},
|
|
93
|
+
"peerDependenciesMeta": {
|
|
94
|
+
"electron": {
|
|
95
|
+
"optional": true
|
|
96
|
+
}
|
|
62
97
|
}
|
|
63
98
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When this package was built, epoch milliseconds: the stand-in for Chromium's base::GetBuildTime(), where the built-in
|
|
3
|
+
* fallback CRL's 20-week window starts. It is `undefined` in the source; `npm run build` (scripts/stamp-build-time.mjs)
|
|
4
|
+
* rewrites this one assignment in the emitted dist files, so source checkouts and tests fall back to the date in
|
|
5
|
+
* trust-data.ts and the working tree stays clean.
|
|
6
|
+
*/
|
|
7
|
+
export const BUILD_TIME: number | undefined = undefined;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { randomBytes, verify as cryptoVerify } from "node:crypto";
|
|
2
|
+
import { CastProtocolError } from "../../protocol/errors.ts";
|
|
3
|
+
import { PayloadType } from "../../protocol/cast-message.ts";
|
|
4
|
+
import { decodeDeviceAuthMessage, encodeAuthChallenge, HashAlgorithm } from "../../protocol/device-auth.ts";
|
|
5
|
+
import { CastAuthError } from "../errors.ts";
|
|
6
|
+
import { verifyDeviceCert, type CastCertError, type CastDeviceCertPolicy } from "./cast-cert-validator.ts";
|
|
7
|
+
import { isFallbackCrlStale, parseAndVerifyCrl, type CastCrl } from "./crl.ts";
|
|
8
|
+
import {
|
|
9
|
+
CAST_CRL_ROOT_CA_B64,
|
|
10
|
+
CAST_FALLBACK_CRL_B64,
|
|
11
|
+
CAST_ROOT_CA_B64,
|
|
12
|
+
EUREKA_ROOT_CA_B64,
|
|
13
|
+
FALLBACK_CRL_BUILD_TIME,
|
|
14
|
+
} from "./trust-data.ts";
|
|
15
|
+
import { parseCertificate, type ParsedCertificate } from "./x509.ts";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The sender side of Cast device authentication, ported from components/media_router/common/providers/cast/channel/
|
|
19
|
+
* cast_auth_util.cc (AuthenticateChallengeReply, VerifyCredentialsImpl, VerifyTLSCertificate, ParseAuthMessage,
|
|
20
|
+
* VerifyAndMapDigestAlgorithm, AuthContext::VerifySenderNonce) and cast_message_util.cc (CreateAuthChallengeMessage).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** Chromium's kMaxSelfSignedCertLifetimeInDays. */
|
|
24
|
+
const MAX_SELF_SIGNED_CERT_LIFETIME_MS = 4 * 24 * 60 * 60 * 1000;
|
|
25
|
+
/** Chromium's kNonceSizeInBytes. */
|
|
26
|
+
const NONCE_SIZE = 16;
|
|
27
|
+
|
|
28
|
+
export interface DeviceAuthOptions {
|
|
29
|
+
/** Verification time, epoch milliseconds. Defaults to now. */
|
|
30
|
+
now?: number;
|
|
31
|
+
/** Trust anchors (DER) replacing the Cast Root CA and Eureka Root CA. For tests and private Cast deployments. */
|
|
32
|
+
roots?: Uint8Array[];
|
|
33
|
+
/** CRL trust anchors (DER) replacing the Cast CRL Root CA. */
|
|
34
|
+
crlRoots?: Uint8Array[];
|
|
35
|
+
/** Replacement fallback CRL (a serialized CrlBundle), or `null` for none. */
|
|
36
|
+
fallbackCrl?: Uint8Array | null;
|
|
37
|
+
/** The "build time" the fallback CRL's 20-week window starts at, epoch milliseconds. */
|
|
38
|
+
fallbackCrlBuildTime?: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface DeviceAuthResult {
|
|
42
|
+
policy: CastDeviceCertPolicy;
|
|
43
|
+
hashAlgorithm: "sha1" | "sha256";
|
|
44
|
+
/** False when the device's reply did not echo our nonce; Chromium records this and carries on. */
|
|
45
|
+
nonceEchoed: boolean;
|
|
46
|
+
/** True when the device sent no usable CRL and the built-in fallback CRL was used instead. */
|
|
47
|
+
usedFallbackCrl: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* False only when the built-in fallback CRL had expired and the device sent no valid CRL, so the revocation check was
|
|
50
|
+
* skipped (Chromium would refuse the device). Revocation by a device CRL or a still-valid fallback CRL is always checked.
|
|
51
|
+
*/
|
|
52
|
+
revocationChecked: boolean;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A challenge: the nonce and the DeviceAuthMessage bytes that go in the CastMessage's binary payload. */
|
|
56
|
+
export function createAuthChallenge(): { nonce: Buffer; payload: Buffer } {
|
|
57
|
+
const nonce = randomBytes(NONCE_SIZE);
|
|
58
|
+
return { nonce, payload: encodeAuthChallenge({ senderNonce: nonce, hashAlgorithm: HashAlgorithm.SHA256 }) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let defaultAnchors: ParsedCertificate[] | undefined;
|
|
62
|
+
let defaultCrlAnchors: ParsedCertificate[] | undefined;
|
|
63
|
+
|
|
64
|
+
const parseAll = (ders: Uint8Array[]): ParsedCertificate[] => ders.map((d) => parseCertificate(d));
|
|
65
|
+
|
|
66
|
+
function anchorsFor(options: DeviceAuthOptions): ParsedCertificate[] {
|
|
67
|
+
if (options.roots !== undefined) return parseAll(options.roots);
|
|
68
|
+
defaultAnchors ??= parseAll([Buffer.from(CAST_ROOT_CA_B64, "base64"), Buffer.from(EUREKA_ROOT_CA_B64, "base64")]);
|
|
69
|
+
return defaultAnchors;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function crlAnchorsFor(options: DeviceAuthOptions): ParsedCertificate[] {
|
|
73
|
+
if (options.crlRoots !== undefined) return parseAll(options.crlRoots);
|
|
74
|
+
defaultCrlAnchors ??= parseAll([Buffer.from(CAST_CRL_ROOT_CA_B64, "base64")]);
|
|
75
|
+
return defaultCrlAnchors;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The decoded fields of a binary CastMessage reply on the deviceauth namespace. */
|
|
79
|
+
export interface AuthReplyMessage {
|
|
80
|
+
payloadType: number;
|
|
81
|
+
payloadBinary: Buffer | undefined;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const REASONS: Record<CastCertError, (detail: string) => CastAuthError | undefined> = {
|
|
85
|
+
OK: () => undefined,
|
|
86
|
+
OK_FALLBACK_CRL: () => undefined,
|
|
87
|
+
OK_FALLBACK_CRL_STALE: () => undefined,
|
|
88
|
+
ERR_CERTS_MISSING: (d) => new CastAuthError("invalid_certificate", `no device certificate: ${d}`),
|
|
89
|
+
ERR_CERTS_PARSE: (d) => new CastAuthError("invalid_certificate", d),
|
|
90
|
+
ERR_CERTS_DATE_INVALID: (d) => new CastAuthError("invalid_certificate", `failed date validity check: ${d}`),
|
|
91
|
+
ERR_CERTS_VERIFY_GENERIC: (d) => new CastAuthError("untrusted_chain", `the certificate chain is not trusted: ${d}`),
|
|
92
|
+
ERR_CERTS_RESTRICTIONS: (d) => new CastAuthError("invalid_certificate", d),
|
|
93
|
+
ERR_CERTS_REVOKED: (d) => new CastAuthError("revoked", d),
|
|
94
|
+
ERR_CERTS_REVOKED_BY_FALLBACK_CRL: (d) => new CastAuthError("revoked", d),
|
|
95
|
+
ERR_FALLBACK_CRL_INVALID: (d) => new CastAuthError("crl_invalid", d),
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* AuthenticateChallengeReply. Throws {@link CastAuthError} unless the reply proves the TLS peer is a genuine,
|
|
100
|
+
* Google-certified Cast device.
|
|
101
|
+
*
|
|
102
|
+
* `peerCertDer` is the DER of the TLS certificate the device presented on this connection; the signature in the
|
|
103
|
+
* reply is over `senderNonce || peerCertDer`, which binds the proof to this TLS session's certificate.
|
|
104
|
+
*/
|
|
105
|
+
export function authenticateChallengeReply(
|
|
106
|
+
reply: AuthReplyMessage,
|
|
107
|
+
peerCertDer: Uint8Array,
|
|
108
|
+
nonce: Uint8Array,
|
|
109
|
+
options: DeviceAuthOptions = {},
|
|
110
|
+
): DeviceAuthResult {
|
|
111
|
+
const now = options.now ?? Date.now();
|
|
112
|
+
|
|
113
|
+
// ParseAuthMessage
|
|
114
|
+
if (reply.payloadType !== PayloadType.BINARY) throw new CastAuthError("malformed_reply", "wrong payload type in the challenge reply");
|
|
115
|
+
if (reply.payloadBinary === undefined) throw new CastAuthError("malformed_reply", "the challenge reply has no binary payload");
|
|
116
|
+
let message;
|
|
117
|
+
try {
|
|
118
|
+
message = decodeDeviceAuthMessage(reply.payloadBinary);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
if (err instanceof CastProtocolError) throw new CastAuthError("malformed_reply", `cannot parse the challenge reply: ${err.message}`);
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
if (message.error !== undefined) throw new CastAuthError("device_error", `the device answered with auth error ${message.error.errorType}`);
|
|
124
|
+
const response = message.response;
|
|
125
|
+
if (response === undefined) throw new CastAuthError("malformed_reply", "the challenge reply has no response");
|
|
126
|
+
|
|
127
|
+
// VerifyTLSCertificate: the TLS certificate's validity period is repurposed as the signature's lifetime.
|
|
128
|
+
let peer: ParsedCertificate;
|
|
129
|
+
try {
|
|
130
|
+
peer = parseCertificate(peerCertDer);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
throw new CastAuthError("tls_certificate_invalid", `the TLS certificate does not parse: ${err instanceof Error ? err.message : String(err)}`);
|
|
133
|
+
}
|
|
134
|
+
if (peer.notBefore > now) throw new CastAuthError("tls_certificate_invalid", "the TLS certificate's valid start date is in the future");
|
|
135
|
+
if (peer.notAfter < now) throw new CastAuthError("tls_certificate_invalid", "the TLS certificate has expired");
|
|
136
|
+
if (peer.notAfter > now + MAX_SELF_SIGNED_CERT_LIFETIME_MS) throw new CastAuthError("tls_certificate_invalid", "the TLS certificate's lifetime is too long");
|
|
137
|
+
|
|
138
|
+
// AuthContext::VerifySenderNonce records a missing or different nonce but does not fail.
|
|
139
|
+
const nonceEchoed = Buffer.from(nonce).equals(response.senderNonce);
|
|
140
|
+
|
|
141
|
+
// VerifyCredentialsImpl
|
|
142
|
+
const signatureInput = Buffer.concat([response.senderNonce, peerCertDer]);
|
|
143
|
+
const anchors = anchorsFor(options);
|
|
144
|
+
const crlAnchors = crlAnchorsFor(options);
|
|
145
|
+
|
|
146
|
+
let crl: CastCrl | undefined;
|
|
147
|
+
if (response.crl !== undefined && response.crl.length > 0) {
|
|
148
|
+
crl = parseAndVerifyCrl(response.crl, now, { anchors: crlAnchors, isFallback: false });
|
|
149
|
+
}
|
|
150
|
+
const fallbackBytes = options.fallbackCrl === undefined ? Buffer.from(CAST_FALLBACK_CRL_B64, "base64") : options.fallbackCrl;
|
|
151
|
+
const fallbackOptions = { anchors: crlAnchors, isFallback: true, fallbackBuildTime: options.fallbackCrlBuildTime ?? FALLBACK_CRL_BUILD_TIME };
|
|
152
|
+
const fallbackCrl = fallbackBytes === null ? undefined : parseAndVerifyCrl(fallbackBytes, now, fallbackOptions);
|
|
153
|
+
const fallbackCrlStale = fallbackBytes !== null && fallbackCrl === undefined && isFallbackCrlStale(fallbackBytes, now, fallbackOptions);
|
|
154
|
+
|
|
155
|
+
const chain = [response.clientAuthCertificate, ...response.intermediateCertificates];
|
|
156
|
+
const verified = verifyDeviceCert(chain, now, { anchors, crl, fallbackCrl, fallbackCrlStale });
|
|
157
|
+
const failure = REASONS[verified.error](verified.detail);
|
|
158
|
+
if (failure !== undefined) throw failure;
|
|
159
|
+
|
|
160
|
+
if (response.signature.length === 0 && signatureInput.length > 0) throw new CastAuthError("bad_signature", "the signature is empty");
|
|
161
|
+
|
|
162
|
+
let hash: "sha1" | "sha256";
|
|
163
|
+
if (response.hashAlgorithm === HashAlgorithm.SHA1) hash = "sha1";
|
|
164
|
+
else if (response.hashAlgorithm === HashAlgorithm.SHA256) hash = "sha256";
|
|
165
|
+
else throw new CastAuthError("unsupported_algorithm", `unsupported hash algorithm ${response.hashAlgorithm}`);
|
|
166
|
+
|
|
167
|
+
// CertVerificationContextImpl::VerifySignatureOverData: RSASSA-PKCS1-v1_5 with the chosen digest, RSA keys only.
|
|
168
|
+
const key = verified.publicKey;
|
|
169
|
+
let signatureOk = false;
|
|
170
|
+
try {
|
|
171
|
+
signatureOk = key !== undefined && key.asymmetricKeyType === "rsa" && cryptoVerify(hash, signatureInput, key, response.signature);
|
|
172
|
+
} catch {
|
|
173
|
+
signatureOk = false;
|
|
174
|
+
}
|
|
175
|
+
if (!signatureOk) throw new CastAuthError("bad_signature", "failed verifying the signature over the TLS certificate");
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
policy: verified.policy,
|
|
179
|
+
hashAlgorithm: hash,
|
|
180
|
+
nonceEchoed,
|
|
181
|
+
usedFallbackCrl: verified.error === "OK_FALLBACK_CRL",
|
|
182
|
+
revocationChecked: verified.error !== "OK_FALLBACK_CRL_STALE",
|
|
183
|
+
};
|
|
184
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { createPublicKey, type KeyObject } from "node:crypto";
|
|
2
|
+
import type { CastCrl } from "./crl.ts";
|
|
3
|
+
import { buildAndVerifyPath } from "./path-validator.ts";
|
|
4
|
+
import { KEY_USAGE_BIT, keyUsageAsserts, OID, parseCertificate, type ParsedCertificate } from "./x509.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Cast device certificate verification, ported from components/media_router/common/providers/cast/certificate/
|
|
8
|
+
* cast_cert_validator.cc (VerifyDeviceCertUsingCustomTrustStore, GetValidationDate, CheckTargetCertificate,
|
|
9
|
+
* DetermineDeviceCertificatePolicy) with the CRL_REQUIRED_WITH_FALLBACK policy Chromium's cast_auth_util.cc uses.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type CastCertError =
|
|
13
|
+
| "OK"
|
|
14
|
+
| "OK_FALLBACK_CRL"
|
|
15
|
+
| "OK_FALLBACK_CRL_STALE"
|
|
16
|
+
| "ERR_CERTS_MISSING"
|
|
17
|
+
| "ERR_CERTS_PARSE"
|
|
18
|
+
| "ERR_CERTS_DATE_INVALID"
|
|
19
|
+
| "ERR_CERTS_VERIFY_GENERIC"
|
|
20
|
+
| "ERR_CERTS_RESTRICTIONS"
|
|
21
|
+
| "ERR_CERTS_REVOKED"
|
|
22
|
+
| "ERR_CERTS_REVOKED_BY_FALLBACK_CRL"
|
|
23
|
+
| "ERR_FALLBACK_CRL_INVALID";
|
|
24
|
+
|
|
25
|
+
/** The device may be restricted to audio by its certificate chain (CastDeviceCertPolicy). */
|
|
26
|
+
export type CastDeviceCertPolicy = "unrestricted" | "audio_only";
|
|
27
|
+
|
|
28
|
+
export interface DeviceCertVerification {
|
|
29
|
+
error: CastCertError;
|
|
30
|
+
/** Set when the error is OK, OK_FALLBACK_CRL or OK_FALLBACK_CRL_STALE (and set earlier too, as in Chromium). */
|
|
31
|
+
policy: CastDeviceCertPolicy;
|
|
32
|
+
/** The device certificate's public key, for checking the challenge signature. */
|
|
33
|
+
publicKey: KeyObject | undefined;
|
|
34
|
+
/** Detail for diagnostics. */
|
|
35
|
+
detail: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
39
|
+
/** Certificates valid for more than this count as long-term (GetValidationDate). */
|
|
40
|
+
const MIN_LONG_TERM_VALIDITY_MS = 5 * 365 * DAY_MS;
|
|
41
|
+
/** How far past the leaf's notAfter the long-term grace period reaches. */
|
|
42
|
+
const MAX_GRACE_WINDOW_MS = 15 * 365 * DAY_MS;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* GetValidationDate: for long-term certificate chains that have just expired (within 15 years of the leaf's notAfter),
|
|
46
|
+
* validate as of a week before the earliest expiry instead of "now". See b/416790717 in Chromium's source.
|
|
47
|
+
*/
|
|
48
|
+
function getValidationDate(target: ParsedCertificate, parsed: ParsedCertificate[], time: number): number {
|
|
49
|
+
let minNotAfter = Infinity;
|
|
50
|
+
let minDuration = Infinity;
|
|
51
|
+
for (const cert of parsed) {
|
|
52
|
+
minNotAfter = Math.min(minNotAfter, cert.notAfter);
|
|
53
|
+
minDuration = Math.min(minDuration, cert.notAfter - cert.notBefore);
|
|
54
|
+
}
|
|
55
|
+
const maxValidity = target.notAfter + MAX_GRACE_WINDOW_MS;
|
|
56
|
+
if (minNotAfter <= time && time <= maxValidity && minDuration > MIN_LONG_TERM_VALIDITY_MS) return minNotAfter - 7 * DAY_MS;
|
|
57
|
+
return time;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface DeviceCertOptions {
|
|
61
|
+
anchors: ParsedCertificate[];
|
|
62
|
+
/** The CRL the device supplied, if it parsed and verified (undefined otherwise, like a null CastCRL). */
|
|
63
|
+
crl: CastCrl | undefined;
|
|
64
|
+
/** The built-in fallback CRL, if it parsed and verified. */
|
|
65
|
+
fallbackCrl: CastCrl | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* True when `fallbackCrl` is missing only because its 20-week window has lapsed (it otherwise verifies). Without a
|
|
68
|
+
* device CRL the revocation check is then skipped, where Chromium would fail closed; see docs/DESIGN.md.
|
|
69
|
+
*/
|
|
70
|
+
fallbackCrlStale?: boolean;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** VerifyDeviceCertUsingCustomTrustStore with CRLPolicy::CRL_REQUIRED_WITH_FALLBACK. */
|
|
74
|
+
export function verifyDeviceCert(certs: Uint8Array[], time: number, options: DeviceCertOptions): DeviceCertVerification {
|
|
75
|
+
const result = (error: CastCertError, detail: string, extra: Partial<DeviceCertVerification> = {}): DeviceCertVerification => ({
|
|
76
|
+
error,
|
|
77
|
+
policy: "unrestricted",
|
|
78
|
+
publicKey: undefined,
|
|
79
|
+
detail,
|
|
80
|
+
...extra,
|
|
81
|
+
});
|
|
82
|
+
if (certs.length === 0) return result("ERR_CERTS_MISSING", "no certificates");
|
|
83
|
+
|
|
84
|
+
const parsed: ParsedCertificate[] = [];
|
|
85
|
+
for (const der of certs) {
|
|
86
|
+
try {
|
|
87
|
+
parsed.push(parseCertificate(der));
|
|
88
|
+
} catch (err) {
|
|
89
|
+
return result("ERR_CERTS_PARSE", `certificate does not parse: ${err instanceof Error ? err.message : String(err)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const target = parsed[0] as ParsedCertificate;
|
|
93
|
+
|
|
94
|
+
const validationDate = getValidationDate(target, parsed, time);
|
|
95
|
+
const built = buildAndVerifyPath(target, parsed.slice(1), options.anchors, validationDate, "client-auth");
|
|
96
|
+
if (built.path === undefined) {
|
|
97
|
+
const detail = built.errors === undefined ? "no path to a trusted Cast root" : built.errors.messages.join("; ");
|
|
98
|
+
return result(built.errors?.date === true ? "ERR_CERTS_DATE_INVALID" : "ERR_CERTS_VERIFY_GENERIC", detail, { policy: "unrestricted" });
|
|
99
|
+
}
|
|
100
|
+
const path = built.path;
|
|
101
|
+
|
|
102
|
+
const audioOnly = path.some((cert) => cert.policyOids?.includes(OID.CAST_AUDIO_ONLY_POLICY) === true);
|
|
103
|
+
const policy: CastDeviceCertPolicy = audioOnly ? "audio_only" : "unrestricted";
|
|
104
|
+
|
|
105
|
+
// CheckTargetCertificate: digitalSignature key usage, a common name, a usable public key.
|
|
106
|
+
let publicKey: KeyObject | undefined;
|
|
107
|
+
try {
|
|
108
|
+
publicKey = createPublicKey({ key: target.spkiTlv, format: "der", type: "spki" });
|
|
109
|
+
} catch {
|
|
110
|
+
publicKey = undefined;
|
|
111
|
+
}
|
|
112
|
+
if (
|
|
113
|
+
target.keyUsage === undefined ||
|
|
114
|
+
!keyUsageAsserts(target.keyUsage, KEY_USAGE_BIT.DIGITAL_SIGNATURE) ||
|
|
115
|
+
target.commonName === undefined ||
|
|
116
|
+
publicKey === undefined
|
|
117
|
+
) {
|
|
118
|
+
return result("ERR_CERTS_RESTRICTIONS", "the device certificate lacks digitalSignature key usage, a common name or a usable key", { policy });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const ok = (error: CastCertError, detail: string) => result(error, detail, { policy, publicKey });
|
|
122
|
+
|
|
123
|
+
// Revocation. Note the CRL checks use the real time, not the (possibly adjusted) validation date.
|
|
124
|
+
if (options.fallbackCrl !== undefined) {
|
|
125
|
+
if (!options.fallbackCrl.checkRevocation(path, time)) return ok("ERR_CERTS_REVOKED_BY_FALLBACK_CRL", "revoked by the fallback CRL");
|
|
126
|
+
} else if (options.crl === undefined) {
|
|
127
|
+
if (options.fallbackCrlStale === true) return ok("OK_FALLBACK_CRL_STALE", "no revocation check: no CRL from the device and the built-in fallback CRL has expired");
|
|
128
|
+
return ok("ERR_FALLBACK_CRL_INVALID", "no valid CRL from the device and no valid fallback CRL");
|
|
129
|
+
}
|
|
130
|
+
if (options.crl === undefined) return ok("OK_FALLBACK_CRL", "accepted on the fallback CRL");
|
|
131
|
+
if (!options.crl.checkRevocation(path, time)) return ok("ERR_CERTS_REVOKED", "revoked by the device's CRL");
|
|
132
|
+
return ok("OK", "");
|
|
133
|
+
}
|