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.
- package/CHANGELOG.md +66 -0
- package/README.md +122 -12
- 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 +193 -55
- 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 +50 -1
- package/dist/electron/session-service.js +256 -33
- 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 +220 -60
- 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 +265 -34
- 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,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
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { createHash, createPublicKey, verify as cryptoVerify } from "node:crypto";
|
|
2
|
+
import { forEachField } from "../../protocol/proto-wire.ts";
|
|
3
|
+
import { buildAndVerifyPath } from "./path-validator.ts";
|
|
4
|
+
import { CertParseError, parseCertificate, type ParsedCertificate } from "./x509.ts";
|
|
5
|
+
import { DerError } from "./der.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Cast certificate revocation lists, ported from components/media_router/common/providers/cast/certificate/
|
|
9
|
+
* cast_crl.cc (ParseAndVerifyCRLUsingCustomTrustStore, VerifyCRL, CastCRLImpl) and the revocation.proto messages
|
|
10
|
+
* (CrlBundle, Crl, TbsCrl, SerialNumberRange) it reads.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Chromium's kFallbackCrlValidityInSeconds: the built-in fallback CRL is valid for 20 weeks from its "build time". */
|
|
14
|
+
const FALLBACK_CRL_VALIDITY_MS = 20 * 7 * 24 * 60 * 60 * 1000;
|
|
15
|
+
|
|
16
|
+
interface SerialRange {
|
|
17
|
+
first: bigint;
|
|
18
|
+
last: bigint;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface TbsCrl {
|
|
22
|
+
version: bigint;
|
|
23
|
+
notBeforeSeconds: bigint;
|
|
24
|
+
notAfterSeconds: bigint;
|
|
25
|
+
revokedPublicKeyHashes: Buffer[];
|
|
26
|
+
ranges: { issuerPublicKeyHash: Buffer; first: bigint; last: bigint }[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface CrlMessage {
|
|
30
|
+
tbsCrl: Buffer | undefined;
|
|
31
|
+
signerCert: Buffer | undefined;
|
|
32
|
+
signature: Buffer | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseCrlBundle(bytes: Uint8Array): CrlMessage[] {
|
|
36
|
+
const crls: CrlMessage[] = [];
|
|
37
|
+
forEachField(bytes, (number, wireType, value) => {
|
|
38
|
+
if (number !== 1 || wireType !== 2) return;
|
|
39
|
+
const crl: CrlMessage = { tbsCrl: undefined, signerCert: undefined, signature: undefined };
|
|
40
|
+
forEachField(value as Uint8Array, (n, w, v) => {
|
|
41
|
+
if (w !== 2) return;
|
|
42
|
+
const data = Buffer.from(v as Uint8Array);
|
|
43
|
+
if (n === 1) crl.tbsCrl = data;
|
|
44
|
+
else if (n === 2) crl.signerCert = data;
|
|
45
|
+
else if (n === 3) crl.signature = data;
|
|
46
|
+
});
|
|
47
|
+
crls.push(crl);
|
|
48
|
+
});
|
|
49
|
+
return crls;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseTbsCrl(bytes: Uint8Array): TbsCrl {
|
|
53
|
+
const tbs: TbsCrl = { version: 0n, notBeforeSeconds: 0n, notAfterSeconds: 0n, revokedPublicKeyHashes: [], ranges: [] };
|
|
54
|
+
forEachField(bytes, (number, wireType, value) => {
|
|
55
|
+
if (wireType === 0) {
|
|
56
|
+
if (number === 1) tbs.version = value as bigint;
|
|
57
|
+
else if (number === 2) tbs.notBeforeSeconds = value as bigint;
|
|
58
|
+
else if (number === 3) tbs.notAfterSeconds = value as bigint;
|
|
59
|
+
} else if (wireType === 2) {
|
|
60
|
+
if (number === 4) tbs.revokedPublicKeyHashes.push(Buffer.from(value as Uint8Array));
|
|
61
|
+
else if (number === 5) {
|
|
62
|
+
const range = { issuerPublicKeyHash: Buffer.alloc(0), first: 0n, last: 0n };
|
|
63
|
+
forEachField(value as Uint8Array, (n, w, v) => {
|
|
64
|
+
if (w === 2 && n === 1) range.issuerPublicKeyHash = Buffer.from(v as Uint8Array);
|
|
65
|
+
else if (w === 0 && n === 2) range.first = v as bigint;
|
|
66
|
+
else if (w === 0 && n === 3) range.last = v as bigint;
|
|
67
|
+
});
|
|
68
|
+
tbs.ranges.push(range);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
return tbs;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** der::ParseUint64 on an INTEGER's content octets. */
|
|
76
|
+
function parseSerialUint64(content: Buffer): bigint | undefined {
|
|
77
|
+
if (content.length === 0) return undefined;
|
|
78
|
+
const first = content[0] as number;
|
|
79
|
+
if (first & 0x80) return undefined;
|
|
80
|
+
if (content.length > 1 && first === 0 && ((content[1] as number) & 0x80) === 0) return undefined;
|
|
81
|
+
const digits = first === 0 ? content.subarray(1) : content;
|
|
82
|
+
if (digits.length > 8) return undefined;
|
|
83
|
+
return digits.length === 0 ? 0n : BigInt("0x" + digits.toString("hex"));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const sha256Hex = (data: Uint8Array): string => createHash("sha256").update(data).digest().toString("latin1");
|
|
87
|
+
|
|
88
|
+
/** A verified CRL: CastCRLImpl. */
|
|
89
|
+
export class CastCrl {
|
|
90
|
+
private readonly notBefore: number;
|
|
91
|
+
private readonly notAfter: number;
|
|
92
|
+
private readonly revokedHashes: Set<string>;
|
|
93
|
+
private readonly revokedSerials = new Map<string, SerialRange[]>();
|
|
94
|
+
|
|
95
|
+
constructor(tbs: TbsCrl, notBeforeMs: number, notAfterMs: number, overallNotAfterMs: number) {
|
|
96
|
+
this.notBefore = notBeforeMs;
|
|
97
|
+
this.notAfter = Math.min(notAfterMs, overallNotAfterMs);
|
|
98
|
+
this.revokedHashes = new Set(tbs.revokedPublicKeyHashes.map((h) => h.toString("latin1")));
|
|
99
|
+
for (const range of tbs.ranges) {
|
|
100
|
+
const key = range.issuerPublicKeyHash.toString("latin1");
|
|
101
|
+
const list = this.revokedSerials.get(key) ?? [];
|
|
102
|
+
list.push({ first: range.first, last: range.last });
|
|
103
|
+
this.revokedSerials.set(key, list);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* CheckRevocation: false when the chain (target first, trust anchor last) contains a revoked key or serial number,
|
|
109
|
+
* and also when the CRL is not valid at `time` (Chromium hard-fails there).
|
|
110
|
+
*/
|
|
111
|
+
checkRevocation(chain: ParsedCertificate[], time: number): boolean {
|
|
112
|
+
if (chain.length === 0) return false;
|
|
113
|
+
if (time < this.notBefore || time > this.notAfter) return false;
|
|
114
|
+
for (let i = 0; i < chain.length; i++) {
|
|
115
|
+
const cert = chain[i] as ParsedCertificate;
|
|
116
|
+
const spkiHash = sha256Hex(cert.spkiTlv);
|
|
117
|
+
if (this.revokedHashes.has(spkiHash)) return false;
|
|
118
|
+
if (i > 0) {
|
|
119
|
+
const ranges = this.revokedSerials.get(spkiHash);
|
|
120
|
+
if (ranges !== undefined) {
|
|
121
|
+
const subordinate = chain[i - 1] as ParsedCertificate;
|
|
122
|
+
const serial = parseSerialUint64(subordinate.serial);
|
|
123
|
+
if (serial === undefined) continue;
|
|
124
|
+
if (ranges.some((r) => r.first <= serial && r.last >= serial)) return false;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The unix-seconds timestamp Chromium verifies the fallback CRL's signer certificate at (kCastFallbackCRLTimestamp). */
|
|
133
|
+
export const FALLBACK_CRL_SIGNER_CHECK_TIME_S = 1692255600;
|
|
134
|
+
|
|
135
|
+
export interface CrlVerificationOptions {
|
|
136
|
+
/** The CRL trust anchors (the Cast CRL Root CA). */
|
|
137
|
+
anchors: ParsedCertificate[];
|
|
138
|
+
isFallback: boolean;
|
|
139
|
+
/** For the fallback only: the "build time" its validity window starts at. */
|
|
140
|
+
fallbackBuildTime?: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* ParseAndVerifyCRLUsingCustomTrustStore. Returns undefined when the CRL cannot be parsed or verified, or when it has
|
|
145
|
+
* no supported version.
|
|
146
|
+
*/
|
|
147
|
+
export function parseAndVerifyCrl(crlProto: Uint8Array, time: number, options: CrlVerificationOptions): CastCrl | undefined {
|
|
148
|
+
let bundle: CrlMessage[];
|
|
149
|
+
try {
|
|
150
|
+
bundle = parseCrlBundle(crlProto);
|
|
151
|
+
} catch {
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
for (const crl of bundle) {
|
|
155
|
+
let tbs: TbsCrl;
|
|
156
|
+
try {
|
|
157
|
+
tbs = parseTbsCrl(crl.tbsCrl ?? Buffer.alloc(0));
|
|
158
|
+
} catch {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (tbs.version !== 0n) continue;
|
|
162
|
+
|
|
163
|
+
let notBeforeMs = Number(tbs.notBeforeSeconds) * 1000;
|
|
164
|
+
let notAfterMs = Number(tbs.notAfterSeconds) * 1000;
|
|
165
|
+
if (options.isFallback) {
|
|
166
|
+
const build = options.fallbackBuildTime as number;
|
|
167
|
+
notBeforeMs = build;
|
|
168
|
+
notAfterMs = build + FALLBACK_CRL_VALIDITY_MS;
|
|
169
|
+
}
|
|
170
|
+
const overall = verifyCrl(crl, tbs, notBeforeMs, notAfterMs, time, options);
|
|
171
|
+
if (overall === undefined) return undefined;
|
|
172
|
+
return new CastCrl(tbs, notBeforeMs, notAfterMs, overall);
|
|
173
|
+
}
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* True when the fallback CRL is intact (it parses and its signer is trusted) but its 20-week window, counted from
|
|
179
|
+
* `fallbackBuildTime`, has lapsed at `time`. This tells "the built-in data is old" from "the data is broken".
|
|
180
|
+
*/
|
|
181
|
+
export function isFallbackCrlStale(crlProto: Uint8Array, time: number, options: CrlVerificationOptions): boolean {
|
|
182
|
+
const build = options.fallbackBuildTime as number;
|
|
183
|
+
const notAfterMs = build + FALLBACK_CRL_VALIDITY_MS;
|
|
184
|
+
if (time <= notAfterMs) return false;
|
|
185
|
+
return parseAndVerifyCrl(crlProto, notAfterMs, { ...options, isFallback: true }) !== undefined;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** VerifyCRL; returns the overall not-after (ms) or undefined on failure. */
|
|
189
|
+
function verifyCrl(crl: CrlMessage, tbs: TbsCrl, notBeforeMs: number, notAfterMs: number, time: number, options: CrlVerificationOptions): number | undefined {
|
|
190
|
+
if (crl.signature === undefined || crl.signerCert === undefined) return undefined;
|
|
191
|
+
let signer: ParsedCertificate;
|
|
192
|
+
try {
|
|
193
|
+
signer = parseCertificate(crl.signerCert);
|
|
194
|
+
} catch (err) {
|
|
195
|
+
if (err instanceof CertParseError || err instanceof DerError) return undefined;
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
// crypto::evp::PublicKeyFromBytes accepts any parseable key; only RSA can then verify the SHA-256 PKCS#1 signature.
|
|
199
|
+
let key;
|
|
200
|
+
try {
|
|
201
|
+
key = createPublicKey({ key: signer.spkiTlv, format: "der", type: "spki" });
|
|
202
|
+
} catch {
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
let signatureOk = false;
|
|
206
|
+
try {
|
|
207
|
+
signatureOk = key.asymmetricKeyType === "rsa" && cryptoVerify("sha256", crl.tbsCrl ?? Buffer.alloc(0), key, crl.signature);
|
|
208
|
+
} catch {
|
|
209
|
+
signatureOk = false;
|
|
210
|
+
}
|
|
211
|
+
if (!signatureOk) return undefined;
|
|
212
|
+
|
|
213
|
+
const issuerCheckTime = options.isFallback ? FALLBACK_CRL_SIGNER_CHECK_TIME_S * 1000 : time;
|
|
214
|
+
const built = buildAndVerifyPath(signer, [], options.anchors, issuerCheckTime, "any");
|
|
215
|
+
if (built.path === undefined) return undefined;
|
|
216
|
+
|
|
217
|
+
if (time < notBeforeMs || time > notAfterMs) return undefined;
|
|
218
|
+
|
|
219
|
+
let overall = notAfterMs;
|
|
220
|
+
if (!options.isFallback) {
|
|
221
|
+
for (const cert of built.path) overall = Math.min(overall, cert.notAfter);
|
|
222
|
+
}
|
|
223
|
+
for (const range of tbs.ranges) {
|
|
224
|
+
if (range.last < range.first) return undefined;
|
|
225
|
+
}
|
|
226
|
+
return overall;
|
|
227
|
+
}
|
|
228
|
+
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A strict DER reader, in the spirit of BoringSSL's `bssl::der::Parser` (pki/parser.cc, pki/input.cc): definite
|
|
3
|
+
* lengths only, minimal length encodings, single-byte tags.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export class DerError extends Error {
|
|
7
|
+
constructor(message: string) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "DerError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const Tag = {
|
|
14
|
+
BOOLEAN: 0x01,
|
|
15
|
+
INTEGER: 0x02,
|
|
16
|
+
BIT_STRING: 0x03,
|
|
17
|
+
OCTET_STRING: 0x04,
|
|
18
|
+
NULL: 0x05,
|
|
19
|
+
OID: 0x06,
|
|
20
|
+
UTF8_STRING: 0x0c,
|
|
21
|
+
SEQUENCE: 0x30,
|
|
22
|
+
SET: 0x31,
|
|
23
|
+
PRINTABLE_STRING: 0x13,
|
|
24
|
+
T61_STRING: 0x14,
|
|
25
|
+
IA5_STRING: 0x16,
|
|
26
|
+
UTC_TIME: 0x17,
|
|
27
|
+
GENERALIZED_TIME: 0x18,
|
|
28
|
+
UNIVERSAL_STRING: 0x1c,
|
|
29
|
+
BMP_STRING: 0x1e,
|
|
30
|
+
} as const;
|
|
31
|
+
|
|
32
|
+
/** Context-specific tag, `constructed` for the 0x20 bit. */
|
|
33
|
+
export function contextTag(number: number, constructed: boolean): number {
|
|
34
|
+
return 0x80 | (constructed ? 0x20 : 0) | number;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One tag-length-value; `value` is the content, `raw` the whole encoding. */
|
|
38
|
+
export interface Tlv {
|
|
39
|
+
tag: number;
|
|
40
|
+
value: Buffer;
|
|
41
|
+
raw: Buffer;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class DerReader {
|
|
45
|
+
private pos = 0;
|
|
46
|
+
private readonly bytes: Buffer;
|
|
47
|
+
|
|
48
|
+
constructor(bytes: Uint8Array) {
|
|
49
|
+
this.bytes = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get hasMore(): boolean {
|
|
53
|
+
return this.pos < this.bytes.length;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
peekTag(): number | undefined {
|
|
57
|
+
return this.bytes[this.pos];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Read any TLV. */
|
|
61
|
+
read(): Tlv {
|
|
62
|
+
const start = this.pos;
|
|
63
|
+
const tag = this.bytes[this.pos++];
|
|
64
|
+
if (tag === undefined) throw new DerError("unexpected end of input");
|
|
65
|
+
if ((tag & 0x1f) === 0x1f) throw new DerError("high tag numbers are not supported");
|
|
66
|
+
const first = this.bytes[this.pos++];
|
|
67
|
+
if (first === undefined) throw new DerError("unexpected end of input");
|
|
68
|
+
let length = first;
|
|
69
|
+
if (first & 0x80) {
|
|
70
|
+
const count = first & 0x7f;
|
|
71
|
+
if (count === 0) throw new DerError("indefinite length is not DER");
|
|
72
|
+
if (count > 4) throw new DerError("length too large");
|
|
73
|
+
length = 0;
|
|
74
|
+
for (let i = 0; i < count; i++) {
|
|
75
|
+
const b = this.bytes[this.pos++];
|
|
76
|
+
if (b === undefined) throw new DerError("unexpected end of input");
|
|
77
|
+
if (i === 0 && b === 0) throw new DerError("length is not minimally encoded");
|
|
78
|
+
length = length * 256 + b;
|
|
79
|
+
}
|
|
80
|
+
if (length < 0x80) throw new DerError("length is not minimally encoded");
|
|
81
|
+
}
|
|
82
|
+
const end = this.pos + length;
|
|
83
|
+
if (end > this.bytes.length) throw new DerError("length runs past end of input");
|
|
84
|
+
const value = this.bytes.subarray(this.pos, end);
|
|
85
|
+
this.pos = end;
|
|
86
|
+
return { tag, value, raw: this.bytes.subarray(start, end) };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Read a TLV that must have `tag`. */
|
|
90
|
+
expect(tag: number): Tlv {
|
|
91
|
+
const tlv = this.read();
|
|
92
|
+
if (tlv.tag !== tag) throw new DerError(`expected tag 0x${tag.toString(16)}, got 0x${tlv.tag.toString(16)}`);
|
|
93
|
+
return tlv;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Read a TLV only if the next tag matches. */
|
|
97
|
+
optional(tag: number): Tlv | undefined {
|
|
98
|
+
return this.peekTag() === tag ? this.read() : undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Throws unless everything was consumed. */
|
|
102
|
+
end(): void {
|
|
103
|
+
if (this.hasMore) throw new DerError("trailing data");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Read the children of a constructed TLV. */
|
|
108
|
+
export function children(tlv: Tlv): Tlv[] {
|
|
109
|
+
const reader = new DerReader(tlv.value);
|
|
110
|
+
const out: Tlv[] = [];
|
|
111
|
+
while (reader.hasMore) out.push(reader.read());
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function parseBoolean(tlv: Tlv): boolean {
|
|
116
|
+
if (tlv.tag !== Tag.BOOLEAN || tlv.value.length !== 1) throw new DerError("bad BOOLEAN");
|
|
117
|
+
const b = tlv.value[0];
|
|
118
|
+
if (b === 0x00) return false;
|
|
119
|
+
if (b === 0xff) return true;
|
|
120
|
+
throw new DerError("BOOLEAN is not DER");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** A non-negative INTEGER as bigint (DER minimal, no sign bit). */
|
|
124
|
+
export function parseUnsignedInteger(tlv: Tlv): bigint {
|
|
125
|
+
if (tlv.tag !== Tag.INTEGER) throw new DerError("expected INTEGER");
|
|
126
|
+
const v = tlv.value;
|
|
127
|
+
if (v.length === 0) throw new DerError("empty INTEGER");
|
|
128
|
+
if (v.length > 1 && ((v[0] === 0x00 && (v[1] as number) < 0x80) || (v[0] === 0xff && (v[1] as number) >= 0x80))) {
|
|
129
|
+
throw new DerError("INTEGER is not minimally encoded");
|
|
130
|
+
}
|
|
131
|
+
if ((v[0] as number) & 0x80) throw new DerError("INTEGER is negative");
|
|
132
|
+
return BigInt("0x" + v.toString("hex"));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Dotted form of an OBJECT IDENTIFIER's content bytes. */
|
|
136
|
+
export function oidString(content: Uint8Array): string {
|
|
137
|
+
if (content.length === 0) throw new DerError("empty OID");
|
|
138
|
+
const parts: bigint[] = [];
|
|
139
|
+
let acc = 0n;
|
|
140
|
+
for (const b of content) {
|
|
141
|
+
acc = (acc << 7n) | BigInt(b & 0x7f);
|
|
142
|
+
if ((b & 0x80) === 0) {
|
|
143
|
+
parts.push(acc);
|
|
144
|
+
acc = 0n;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if ((content[content.length - 1] as number) & 0x80) throw new DerError("truncated OID");
|
|
148
|
+
const first = parts[0] as bigint;
|
|
149
|
+
const head = first < 40n ? [0n, first] : first < 80n ? [1n, first - 40n] : [2n, first - 80n];
|
|
150
|
+
return [...head, ...parts.slice(1)].join(".");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Parse a UTCTime or GeneralizedTime in the RFC 5280 profile (UTC, seconds, trailing Z) to epoch milliseconds. */
|
|
154
|
+
export function parseTime(tlv: Tlv): number {
|
|
155
|
+
const text = tlv.value.toString("latin1");
|
|
156
|
+
let m: RegExpExecArray | null;
|
|
157
|
+
let year: number;
|
|
158
|
+
if (tlv.tag === Tag.UTC_TIME) {
|
|
159
|
+
m = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(text);
|
|
160
|
+
if (m === null) throw new DerError("bad UTCTime");
|
|
161
|
+
const yy = Number(m[1]);
|
|
162
|
+
year = yy >= 50 ? 1900 + yy : 2000 + yy;
|
|
163
|
+
} else if (tlv.tag === Tag.GENERALIZED_TIME) {
|
|
164
|
+
m = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(text);
|
|
165
|
+
if (m === null) throw new DerError("bad GeneralizedTime");
|
|
166
|
+
year = Number(m[1]);
|
|
167
|
+
} else {
|
|
168
|
+
throw new DerError("expected a time");
|
|
169
|
+
}
|
|
170
|
+
const month = Number(m[2]);
|
|
171
|
+
const day = Number(m[3]);
|
|
172
|
+
const hour = Number(m[4]);
|
|
173
|
+
const minute = Number(m[5]);
|
|
174
|
+
const second = Number(m[6]);
|
|
175
|
+
const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
|
|
176
|
+
const dim = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month - 1];
|
|
177
|
+
if (month < 1 || month > 12 || day < 1 || day > (dim as number) || hour > 23 || minute > 59 || second > 59) {
|
|
178
|
+
throw new DerError("time out of range");
|
|
179
|
+
}
|
|
180
|
+
return Date.UTC(year, month - 1, day, hour, minute, second);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** The content octets of a BIT STRING and its unused-bit count. */
|
|
184
|
+
export function parseBitString(tlv: Tlv): { bytes: Buffer; unusedBits: number } {
|
|
185
|
+
if (tlv.tag !== Tag.BIT_STRING || tlv.value.length < 1) throw new DerError("bad BIT STRING");
|
|
186
|
+
const unusedBits = tlv.value[0] as number;
|
|
187
|
+
if (unusedBits > 7 || (tlv.value.length === 1 && unusedBits !== 0)) throw new DerError("bad BIT STRING padding");
|
|
188
|
+
const bytes = tlv.value.subarray(1);
|
|
189
|
+
if (unusedBits > 0 && ((bytes[bytes.length - 1] as number) & ((1 << unusedBits) - 1)) !== 0) {
|
|
190
|
+
throw new DerError("BIT STRING padding bits are not zero");
|
|
191
|
+
}
|
|
192
|
+
return { bytes, unusedBits };
|
|
193
|
+
}
|