electron-cast 0.3.0 → 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.
Files changed (204) hide show
  1. package/CHANGELOG.md +66 -0
  2. package/README.md +122 -12
  3. package/dist/cjs/client/auth/build-time.d.ts +7 -0
  4. package/dist/cjs/client/auth/build-time.js +11 -0
  5. package/dist/cjs/client/auth/build-time.js.map +1 -0
  6. package/dist/cjs/client/auth/cast-auth.d.ts +44 -0
  7. package/dist/cjs/client/auth/cast-auth.js +145 -0
  8. package/dist/cjs/client/auth/cast-auth.js.map +1 -0
  9. package/dist/cjs/client/auth/cast-cert-validator.d.ts +34 -0
  10. package/dist/cjs/client/auth/cast-cert-validator.js +89 -0
  11. package/dist/cjs/client/auth/cast-cert-validator.js.map +1 -0
  12. package/dist/cjs/client/auth/crl.d.ts +45 -0
  13. package/dist/cjs/client/auth/crl.js +227 -0
  14. package/dist/cjs/client/auth/crl.js.map +1 -0
  15. package/dist/cjs/client/auth/der.d.ts +62 -0
  16. package/dist/cjs/client/auth/der.js +208 -0
  17. package/dist/cjs/client/auth/der.js.map +1 -0
  18. package/dist/cjs/client/auth/names.d.ts +52 -0
  19. package/dist/cjs/client/auth/names.js +447 -0
  20. package/dist/cjs/client/auth/names.js.map +1 -0
  21. package/dist/cjs/client/auth/path-validator.d.ts +29 -0
  22. package/dist/cjs/client/auth/path-validator.js +421 -0
  23. package/dist/cjs/client/auth/path-validator.js.map +1 -0
  24. package/dist/cjs/client/auth/trust-data.d.ts +24 -0
  25. package/dist/cjs/client/auth/trust-data.js +97 -0
  26. package/dist/cjs/client/auth/trust-data.js.map +1 -0
  27. package/dist/cjs/client/auth/x509.d.ts +109 -0
  28. package/dist/cjs/client/auth/x509.js +442 -0
  29. package/dist/cjs/client/auth/x509.js.map +1 -0
  30. package/dist/cjs/client/cast-client.d.ts +111 -0
  31. package/dist/cjs/client/cast-client.js +505 -0
  32. package/dist/cjs/client/cast-client.js.map +1 -0
  33. package/dist/cjs/client/errors.d.ts +28 -0
  34. package/dist/cjs/client/errors.js +50 -0
  35. package/dist/cjs/client/errors.js.map +1 -0
  36. package/dist/cjs/client/index.d.ts +6 -0
  37. package/dist/cjs/client/index.js +21 -0
  38. package/dist/cjs/client/index.js.map +1 -0
  39. package/dist/cjs/client/namespaces.d.ts +9 -0
  40. package/dist/cjs/client/namespaces.js +13 -0
  41. package/dist/cjs/client/namespaces.js.map +1 -0
  42. package/dist/cjs/client/types.d.ts +27 -0
  43. package/dist/cjs/client/types.js +3 -0
  44. package/dist/cjs/client/types.js.map +1 -0
  45. package/dist/cjs/discovery/cast-discovery.d.ts +103 -0
  46. package/dist/cjs/discovery/cast-discovery.js +323 -0
  47. package/dist/cjs/discovery/cast-discovery.js.map +1 -0
  48. package/dist/cjs/discovery/device-cache.d.ts +45 -0
  49. package/dist/cjs/discovery/device-cache.js +240 -0
  50. package/dist/cjs/discovery/device-cache.js.map +1 -0
  51. package/dist/cjs/discovery/dns-packet.d.ts +70 -0
  52. package/dist/cjs/discovery/dns-packet.js +251 -0
  53. package/dist/cjs/discovery/dns-packet.js.map +1 -0
  54. package/dist/cjs/discovery/index.d.ts +4 -0
  55. package/dist/cjs/discovery/index.js +21 -0
  56. package/dist/cjs/discovery/index.js.map +1 -0
  57. package/dist/cjs/discovery/udp-socket.d.ts +16 -0
  58. package/dist/cjs/discovery/udp-socket.js +163 -0
  59. package/dist/cjs/discovery/udp-socket.js.map +1 -0
  60. package/dist/cjs/electron/main.d.ts +86 -0
  61. package/dist/cjs/electron/main.js +408 -0
  62. package/dist/cjs/electron/main.js.map +1 -0
  63. package/dist/cjs/electron/manager-api.d.ts +132 -0
  64. package/dist/cjs/electron/manager-api.js +207 -0
  65. package/dist/cjs/electron/manager-api.js.map +1 -0
  66. package/dist/cjs/electron/preload.d.ts +39 -0
  67. package/dist/cjs/electron/preload.js +96 -0
  68. package/dist/cjs/electron/preload.js.map +1 -0
  69. package/dist/cjs/electron/presentation-envelope.d.ts +60 -0
  70. package/dist/cjs/electron/presentation-envelope.js +318 -0
  71. package/dist/cjs/electron/presentation-envelope.js.map +1 -0
  72. package/dist/cjs/electron/presentation-handler.d.ts +100 -0
  73. package/dist/cjs/electron/presentation-handler.js +499 -0
  74. package/dist/cjs/electron/presentation-handler.js.map +1 -0
  75. package/dist/cjs/electron/session-service.d.ts +174 -0
  76. package/dist/cjs/electron/session-service.js +891 -0
  77. package/dist/cjs/electron/session-service.js.map +1 -0
  78. package/dist/cjs/index.d.ts +8 -0
  79. package/dist/cjs/index.js +26 -0
  80. package/dist/cjs/index.js.map +1 -0
  81. package/dist/cjs/package.json +1 -0
  82. package/dist/cjs/polyfill/bridge-types.d.ts +86 -0
  83. package/dist/cjs/polyfill/bridge-types.js +3 -0
  84. package/dist/cjs/polyfill/bridge-types.js.map +1 -0
  85. package/dist/cjs/polyfill/install.d.ts +12 -0
  86. package/dist/cjs/polyfill/install.js +1569 -0
  87. package/dist/cjs/polyfill/install.js.map +1 -0
  88. package/dist/cjs/polyfill/presentation-standin.d.ts +13 -0
  89. package/dist/cjs/polyfill/presentation-standin.js +279 -0
  90. package/dist/cjs/polyfill/presentation-standin.js.map +1 -0
  91. package/dist/cjs/protocol/cast-message.d.ts +20 -0
  92. package/dist/cjs/protocol/cast-message.js +171 -0
  93. package/dist/cjs/protocol/cast-message.js.map +1 -0
  94. package/dist/cjs/protocol/device-auth.d.ts +61 -0
  95. package/dist/cjs/protocol/device-auth.js +113 -0
  96. package/dist/cjs/protocol/device-auth.js.map +1 -0
  97. package/dist/cjs/protocol/errors.d.ts +4 -0
  98. package/dist/cjs/protocol/errors.js +12 -0
  99. package/dist/cjs/protocol/errors.js.map +1 -0
  100. package/dist/cjs/protocol/framing.d.ts +18 -0
  101. package/dist/cjs/protocol/framing.js +100 -0
  102. package/dist/cjs/protocol/framing.js.map +1 -0
  103. package/dist/cjs/protocol/index.d.ts +4 -0
  104. package/dist/cjs/protocol/index.js +21 -0
  105. package/dist/cjs/protocol/index.js.map +1 -0
  106. package/dist/cjs/protocol/proto-wire.d.ts +11 -0
  107. package/dist/cjs/protocol/proto-wire.js +64 -0
  108. package/dist/cjs/protocol/proto-wire.js.map +1 -0
  109. package/dist/client/auth/build-time.d.ts +7 -0
  110. package/dist/client/auth/build-time.js +8 -0
  111. package/dist/client/auth/build-time.js.map +1 -0
  112. package/dist/client/auth/cast-auth.d.ts +44 -0
  113. package/dist/client/auth/cast-auth.js +141 -0
  114. package/dist/client/auth/cast-auth.js.map +1 -0
  115. package/dist/client/auth/cast-cert-validator.d.ts +34 -0
  116. package/dist/client/auth/cast-cert-validator.js +86 -0
  117. package/dist/client/auth/cast-cert-validator.js.map +1 -0
  118. package/dist/client/auth/crl.d.ts +45 -0
  119. package/dist/client/auth/crl.js +221 -0
  120. package/dist/client/auth/crl.js.map +1 -0
  121. package/dist/client/auth/der.d.ts +62 -0
  122. package/dist/client/auth/der.js +196 -0
  123. package/dist/client/auth/der.js.map +1 -0
  124. package/dist/client/auth/names.d.ts +52 -0
  125. package/dist/client/auth/names.js +438 -0
  126. package/dist/client/auth/names.js.map +1 -0
  127. package/dist/client/auth/path-validator.d.ts +29 -0
  128. package/dist/client/auth/path-validator.js +417 -0
  129. package/dist/client/auth/path-validator.js.map +1 -0
  130. package/dist/client/auth/trust-data.d.ts +24 -0
  131. package/dist/client/auth/trust-data.js +94 -0
  132. package/dist/client/auth/trust-data.js.map +1 -0
  133. package/dist/client/auth/x509.d.ts +109 -0
  134. package/dist/client/auth/x509.js +430 -0
  135. package/dist/client/auth/x509.js.map +1 -0
  136. package/dist/client/cast-client.d.ts +20 -1
  137. package/dist/client/cast-client.js +74 -3
  138. package/dist/client/cast-client.js.map +1 -1
  139. package/dist/client/errors.d.ts +11 -0
  140. package/dist/client/errors.js +13 -0
  141. package/dist/client/errors.js.map +1 -1
  142. package/dist/client/index.d.ts +2 -0
  143. package/dist/discovery/cast-discovery.d.ts +21 -2
  144. package/dist/discovery/cast-discovery.js +94 -14
  145. package/dist/discovery/cast-discovery.js.map +1 -1
  146. package/dist/discovery/device-cache.d.ts +6 -1
  147. package/dist/discovery/device-cache.js +31 -7
  148. package/dist/discovery/device-cache.js.map +1 -1
  149. package/dist/discovery/udp-socket.d.ts +5 -1
  150. package/dist/discovery/udp-socket.js +56 -5
  151. package/dist/discovery/udp-socket.js.map +1 -1
  152. package/dist/electron/main.d.ts +26 -4
  153. package/dist/electron/main.js +193 -55
  154. package/dist/electron/main.js.map +1 -1
  155. package/dist/electron/manager-api.d.ts +132 -0
  156. package/dist/electron/manager-api.js +198 -0
  157. package/dist/electron/manager-api.js.map +1 -0
  158. package/dist/electron/preload.d.ts +5 -0
  159. package/dist/electron/preload.js +9 -1
  160. package/dist/electron/preload.js.map +1 -1
  161. package/dist/electron/presentation-envelope.js +7 -1
  162. package/dist/electron/presentation-envelope.js.map +1 -1
  163. package/dist/electron/presentation-handler.js +16 -9
  164. package/dist/electron/presentation-handler.js.map +1 -1
  165. package/dist/electron/session-service.d.ts +50 -1
  166. package/dist/electron/session-service.js +256 -33
  167. package/dist/electron/session-service.js.map +1 -1
  168. package/dist/polyfill/presentation-standin.js +3 -2
  169. package/dist/polyfill/presentation-standin.js.map +1 -1
  170. package/dist/protocol/device-auth.d.ts +61 -0
  171. package/dist/protocol/device-auth.js +106 -0
  172. package/dist/protocol/device-auth.js.map +1 -0
  173. package/dist/protocol/index.d.ts +1 -0
  174. package/dist/protocol/index.js +1 -0
  175. package/dist/protocol/index.js.map +1 -1
  176. package/dist/protocol/proto-wire.d.ts +11 -0
  177. package/dist/protocol/proto-wire.js +60 -0
  178. package/dist/protocol/proto-wire.js.map +1 -0
  179. package/package.json +47 -12
  180. package/src/client/auth/build-time.ts +7 -0
  181. package/src/client/auth/cast-auth.ts +184 -0
  182. package/src/client/auth/cast-cert-validator.ts +133 -0
  183. package/src/client/auth/crl.ts +228 -0
  184. package/src/client/auth/der.ts +193 -0
  185. package/src/client/auth/names.ts +411 -0
  186. package/src/client/auth/path-validator.ts +449 -0
  187. package/src/client/auth/trust-data.ts +95 -0
  188. package/src/client/auth/x509.ts +488 -0
  189. package/src/client/cast-client.ts +85 -3
  190. package/src/client/errors.ts +29 -0
  191. package/src/client/index.ts +2 -0
  192. package/src/discovery/cast-discovery.ts +104 -19
  193. package/src/discovery/device-cache.ts +38 -8
  194. package/src/discovery/udp-socket.ts +63 -4
  195. package/src/electron/main.ts +220 -60
  196. package/src/electron/manager-api.ts +312 -0
  197. package/src/electron/preload.ts +15 -1
  198. package/src/electron/presentation-envelope.ts +7 -1
  199. package/src/electron/presentation-handler.ts +16 -10
  200. package/src/electron/session-service.ts +265 -34
  201. package/src/polyfill/presentation-standin.ts +3 -2
  202. package/src/protocol/device-auth.ts +126 -0
  203. package/src/protocol/index.ts +1 -0
  204. package/src/protocol/proto-wire.ts +55 -0
@@ -0,0 +1,61 @@
1
+ /**
2
+ * The device authentication messages of cast_channel.proto (DeviceAuthMessage and its parts), hand-coded like
3
+ * {@link ./cast-message.ts}. They travel as the binary payload of a CastMessage on the deviceauth namespace.
4
+ */
5
+ /** SignatureAlgorithm enum values. */
6
+ export declare const SignatureAlgorithm: {
7
+ readonly UNSPECIFIED: 0;
8
+ readonly RSASSA_PKCS1v15: 1;
9
+ readonly RSASSA_PSS: 2;
10
+ };
11
+ export type SignatureAlgorithm = (typeof SignatureAlgorithm)[keyof typeof SignatureAlgorithm];
12
+ /** HashAlgorithm enum values. */
13
+ export declare const HashAlgorithm: {
14
+ readonly SHA1: 0;
15
+ readonly SHA256: 1;
16
+ };
17
+ export type HashAlgorithm = (typeof HashAlgorithm)[keyof typeof HashAlgorithm];
18
+ /** AuthError.ErrorType enum values. */
19
+ export declare const AuthErrorType: {
20
+ readonly INTERNAL_ERROR: 0;
21
+ readonly NO_TLS: 1;
22
+ readonly SIGNATURE_ALGORITHM_UNAVAILABLE: 2;
23
+ };
24
+ export type AuthErrorType = (typeof AuthErrorType)[keyof typeof AuthErrorType];
25
+ /** A decoded AuthResponse. Absent optional fields carry the proto2 defaults. */
26
+ export interface AuthResponse {
27
+ signature: Buffer;
28
+ clientAuthCertificate: Buffer;
29
+ intermediateCertificates: Buffer[];
30
+ signatureAlgorithm: number;
31
+ senderNonce: Buffer;
32
+ hashAlgorithm: number;
33
+ crl: Buffer | undefined;
34
+ }
35
+ /** A decoded DeviceAuthMessage; a device's reply has `response` or `error`. */
36
+ export interface DeviceAuthMessage {
37
+ response?: AuthResponse;
38
+ error?: {
39
+ errorType: number;
40
+ };
41
+ }
42
+ /** Encode a DeviceAuthMessage carrying only an AuthChallenge (the sender's half). */
43
+ export declare function encodeAuthChallenge(challenge: {
44
+ senderNonce: Uint8Array;
45
+ hashAlgorithm?: HashAlgorithm;
46
+ signatureAlgorithm?: SignatureAlgorithm;
47
+ }): Buffer;
48
+ /** Encode an AuthResponse inside a DeviceAuthMessage. Used by tests and fake receivers. */
49
+ export declare function encodeAuthResponse(response: {
50
+ signature: Uint8Array;
51
+ clientAuthCertificate: Uint8Array;
52
+ intermediateCertificates?: Uint8Array[];
53
+ signatureAlgorithm?: number;
54
+ senderNonce?: Uint8Array;
55
+ hashAlgorithm?: number;
56
+ crl?: Uint8Array;
57
+ }): Buffer;
58
+ /** Encode a DeviceAuthMessage carrying an AuthError. Used by tests and fake receivers. */
59
+ export declare function encodeAuthError(errorType: number): Buffer;
60
+ /** Decode a DeviceAuthMessage, keeping only what a sender needs. Throws {@link CastProtocolError} on malformed input. */
61
+ export declare function decodeDeviceAuthMessage(bytes: Uint8Array): DeviceAuthMessage;
@@ -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
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* from "./framing.ts";
4
+ export * from "./device-auth.ts";
@@ -1,4 +1,5 @@
1
1
  export * from "./cast-message.js";
2
2
  export * from "./errors.js";
3
3
  export * from "./framing.js";
4
+ export * from "./device-auth.js";
4
5
  //# sourceMappingURL=index.js.map
@@ -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/proto-wire.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD;;;GAGG;AAEH,qDAAqD;AACrD,MAAM,UAAU,eAAe,CAAC,WAAmB,EAAE,OAAmB;IACtE,MAAM,MAAM,GAAa,CAAC,CAAC,WAAW,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;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C,GAAG,IAAI,EAAE,CAAC,CAAC;QACjC,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,GAAG,EAAE,CAAC,CAAC;QAClC,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;YACnB,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QAC7B,CAAC;aAAM,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;YAC1B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;YAChC,IAAI,GAAG,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM;gBAAE,MAAM,IAAI,iBAAiB,CAAC,+CAA+C,CAAC,CAAC;YAC9G,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC;YACpD,GAAG,IAAI,MAAM,CAAC;QAChB,CAAC;aAAM,IAAI,QAAQ,KAAK,CAAC,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;YAC5C,MAAM,IAAI,GAAG,QAAQ,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;YACpC,IAAI,GAAG,GAAG,IAAI,GAAG,KAAK,CAAC,MAAM;gBAAE,MAAM,IAAI,iBAAiB,CAAC,4BAA4B,CAAC,CAAC;YACzF,GAAG,IAAI,IAAI,CAAC;QACd,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,iBAAiB,CAAC,yBAAyB,QAAQ,EAAE,CAAC,CAAC;QACnE,CAAC;IACH,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-cast",
3
- "version": "0.3.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
- "types": "./dist/index.d.ts",
24
- "default": "./dist/index.js"
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
- "types": "./dist/electron/main.d.ts",
28
- "default": "./dist/electron/main.js"
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
- "types": "./dist/electron/preload.d.ts",
32
- "default": "./dist/electron/preload.js"
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
- "types": "./dist/polyfill/install.d.ts",
36
- "default": "./dist/polyfill/install.js"
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
+ }