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.
Files changed (204) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/README.md +123 -14
  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 +191 -54
  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 +42 -1
  166. package/dist/electron/session-service.js +226 -23
  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 +218 -59
  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 +230 -26
  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,411 @@
1
+ import { children, contextTag, DerError, DerReader, oidString, Tag, type Tlv } from "./der.ts";
2
+
3
+ /**
4
+ * Distinguished-name comparison, GeneralNames and name constraints, ported from BoringSSL's pki library:
5
+ * verify_name_match.cc (NormalizeName, VerifyNameInSubtree, FindEmailAddressesInName), general_names.cc,
6
+ * ip_util.cc and name_constraints.cc. Chromium's Cast certificate verification runs on that library
7
+ * (components/media_router/common/providers/cast/certificate/cast_cert_validator.cc builds a bssl::CertPathBuilder).
8
+ */
9
+
10
+ const OID_EMAIL_ADDRESS = "1.2.840.113549.1.9.1";
11
+
12
+ /** A normalized RDN sequence: one comparison key per RDN, in order. */
13
+ export type NormalizedName = string[];
14
+
15
+ const NORMALIZABLE = new Set<number>([Tag.PRINTABLE_STRING, Tag.UTF8_STRING, Tag.IA5_STRING, Tag.UNIVERSAL_STRING, Tag.BMP_STRING]);
16
+
17
+ /** ValueAsStringUnsafe: the attribute value as text, for the string types a Name may use. */
18
+ function valueAsString(tag: number, value: Buffer): string | undefined {
19
+ switch (tag) {
20
+ case Tag.PRINTABLE_STRING:
21
+ case Tag.IA5_STRING:
22
+ case Tag.T61_STRING:
23
+ return value.toString("latin1");
24
+ case Tag.UTF8_STRING:
25
+ return value.toString("utf8");
26
+ case Tag.BMP_STRING: {
27
+ if (value.length % 2 !== 0) return undefined;
28
+ let s = "";
29
+ for (let i = 0; i < value.length; i += 2) s += String.fromCharCode(value.readUInt16BE(i));
30
+ return s;
31
+ }
32
+ case Tag.UNIVERSAL_STRING: {
33
+ if (value.length % 4 !== 0) return undefined;
34
+ let s = "";
35
+ for (let i = 0; i < value.length; i += 4) {
36
+ const cp = value.readUInt32BE(i);
37
+ if (cp > 0x10ffff || (cp >= 0xd800 && cp <= 0xdfff)) return undefined;
38
+ s += String.fromCodePoint(cp);
39
+ }
40
+ return s;
41
+ }
42
+ default:
43
+ return undefined;
44
+ }
45
+ }
46
+
47
+ /** NormalizeDirectoryString: trim and collapse spaces, fold ASCII case, enforce the charset of the string type. */
48
+ function normalizeDirectoryString(input: string, enforce: "none" | "printable" | "ascii"): string | undefined {
49
+ let out = "";
50
+ let i = 0;
51
+ while (i < input.length && input[i] === " ") i++;
52
+ for (; i < input.length; i++) {
53
+ const c = input.charCodeAt(i);
54
+ if (c === 0x20) {
55
+ const next = input[i + 1];
56
+ if (next !== undefined && next !== " ") out += " ";
57
+ } else if (c >= 0x41 && c <= 0x5a) {
58
+ out += String.fromCharCode(c + 0x20);
59
+ } else {
60
+ if (enforce === "printable" && !((c >= 0x61 && c <= 0x7a) || (c >= 0x27 && c <= 0x3a) || c === 0x3d || c === 0x3f)) return undefined;
61
+ if (enforce === "ascii" && c > 0x7f) return undefined;
62
+ out += input[i];
63
+ }
64
+ }
65
+ return out;
66
+ }
67
+
68
+ interface Ava {
69
+ oid: string;
70
+ tag: number;
71
+ value: Buffer;
72
+ }
73
+
74
+ function readRdns(nameContent: Buffer): Ava[][] {
75
+ const reader = new DerReader(nameContent);
76
+ const rdns: Ava[][] = [];
77
+ while (reader.hasMore) {
78
+ const set = reader.expect(Tag.SET);
79
+ const avas: Ava[] = [];
80
+ const inner = new DerReader(set.value);
81
+ while (inner.hasMore) {
82
+ const seq = new DerReader(inner.expect(Tag.SEQUENCE).value);
83
+ const oid = oidString(seq.expect(Tag.OID).value);
84
+ const value = seq.read();
85
+ seq.end();
86
+ avas.push({ oid, tag: value.tag, value: value.value });
87
+ }
88
+ if (avas.length === 0) throw new DerError("empty RelativeDistinguishedName");
89
+ rdns.push(avas);
90
+ }
91
+ return rdns;
92
+ }
93
+
94
+ /** NormalizeName. Throws {@link DerError} when a value cannot be normalized. */
95
+ export function normalizeName(nameContent: Buffer): NormalizedName {
96
+ return readRdns(nameContent).map((avas) =>
97
+ avas
98
+ .map((ava) => {
99
+ if (NORMALIZABLE.has(ava.tag)) {
100
+ const text = valueAsString(ava.tag, ava.value);
101
+ const enforce = ava.tag === Tag.PRINTABLE_STRING ? "printable" : ava.tag === Tag.IA5_STRING ? "ascii" : "none";
102
+ const normalized = text === undefined ? undefined : normalizeDirectoryString(text, enforce);
103
+ if (normalized === undefined) throw new DerError("attribute value cannot be normalized");
104
+ return `${ava.oid}=S:${normalized}`;
105
+ }
106
+ return `${ava.oid}=${ava.tag}:${ava.value.toString("hex")}`;
107
+ })
108
+ .sort()
109
+ .join("+"),
110
+ );
111
+ }
112
+
113
+ export function namesEqual(a: NormalizedName, b: NormalizedName): boolean {
114
+ return a.length === b.length && a.every((rdn, i) => rdn === b[i]);
115
+ }
116
+
117
+ /** VerifyNameInSubtree: `name` equals `parent` or extends it with more RDNs. */
118
+ export function nameInSubtree(name: NormalizedName, parent: NormalizedName): boolean {
119
+ return name.length >= parent.length && parent.every((rdn, i) => rdn === name[i]);
120
+ }
121
+
122
+ /** FindEmailAddressesInName. Returns undefined if the name is not parseable. */
123
+ export function findEmailAddresses(nameContent: Buffer): string[] | undefined {
124
+ try {
125
+ const out: string[] = [];
126
+ for (const rdn of readRdns(nameContent)) {
127
+ for (const ava of rdn) {
128
+ if (ava.oid !== OID_EMAIL_ADDRESS) continue;
129
+ const text = valueAsString(ava.tag, ava.value);
130
+ if (text === undefined) return undefined;
131
+ out.push(text);
132
+ }
133
+ }
134
+ return out;
135
+ } catch {
136
+ return undefined;
137
+ }
138
+ }
139
+
140
+ // --- GeneralNames ---------------------------------------------------------------------------------------------
141
+
142
+ export const GeneralNameType = {
143
+ OTHER_NAME: 1 << 0,
144
+ RFC822_NAME: 1 << 1,
145
+ DNS_NAME: 1 << 2,
146
+ X400_ADDRESS: 1 << 3,
147
+ DIRECTORY_NAME: 1 << 4,
148
+ EDI_PARTY_NAME: 1 << 5,
149
+ URI: 1 << 6,
150
+ IP_ADDRESS: 1 << 7,
151
+ REGISTERED_ID: 1 << 8,
152
+ } as const;
153
+ const ALL_NAME_TYPES = 0x1ff;
154
+ /** The name types the constraint matcher implements; a constraint of any other type only binds when critical. */
155
+ const SUPPORTED_NAME_TYPES =
156
+ GeneralNameType.RFC822_NAME | GeneralNameType.DNS_NAME | GeneralNameType.DIRECTORY_NAME | GeneralNameType.IP_ADDRESS;
157
+
158
+ export interface GeneralNames {
159
+ presentTypes: number;
160
+ rfc822: string[];
161
+ dns: string[];
162
+ directory: NormalizedName[];
163
+ ipAddresses: Buffer[];
164
+ /** Constraint form: address and netmask. */
165
+ ipRanges: { address: Buffer; mask: Buffer }[];
166
+ }
167
+
168
+ function emptyGeneralNames(): GeneralNames {
169
+ return { presentTypes: 0, rfc822: [], dns: [], directory: [], ipAddresses: [], ipRanges: [] };
170
+ }
171
+
172
+ const isAscii = (b: Buffer): boolean => b.every((x) => x < 0x80);
173
+
174
+ function isValidNetmask(mask: Buffer): boolean {
175
+ if (mask.length !== 4 && mask.length !== 16) return false;
176
+ for (let i = 0; i < mask.length; i++) {
177
+ const b = mask[i] as number;
178
+ if (b !== 0xff) {
179
+ const inv = ~b & 0xff;
180
+ if ((inv & (inv + 1)) !== 0) return false;
181
+ for (let j = i + 1; j < mask.length; j++) if (mask[j] !== 0) return false;
182
+ return true;
183
+ }
184
+ }
185
+ return true;
186
+ }
187
+
188
+ /** ParseGeneralName. Throws {@link DerError} for malformed or unknown general names. */
189
+ function parseGeneralName(tlv: Tlv, into: GeneralNames, ipForm: "address" | "constraint"): void {
190
+ const { tag, value } = tlv;
191
+ if (tag === contextTag(0, true)) {
192
+ into.presentTypes |= GeneralNameType.OTHER_NAME;
193
+ } else if (tag === contextTag(1, false)) {
194
+ if (!isAscii(value)) throw new DerError("rfc822Name is not ASCII");
195
+ into.rfc822.push(value.toString("latin1"));
196
+ into.presentTypes |= GeneralNameType.RFC822_NAME;
197
+ } else if (tag === contextTag(2, false)) {
198
+ if (!isAscii(value)) throw new DerError("dNSName is not ASCII");
199
+ into.dns.push(value.toString("latin1"));
200
+ into.presentTypes |= GeneralNameType.DNS_NAME;
201
+ } else if (tag === contextTag(3, true)) {
202
+ into.presentTypes |= GeneralNameType.X400_ADDRESS;
203
+ } else if (tag === contextTag(4, true)) {
204
+ const reader = new DerReader(value);
205
+ const name = reader.expect(Tag.SEQUENCE);
206
+ reader.end();
207
+ into.directory.push(normalizeName(name.value));
208
+ into.presentTypes |= GeneralNameType.DIRECTORY_NAME;
209
+ } else if (tag === contextTag(5, true)) {
210
+ into.presentTypes |= GeneralNameType.EDI_PARTY_NAME;
211
+ } else if (tag === contextTag(6, false)) {
212
+ if (!isAscii(value)) throw new DerError("uniformResourceIdentifier is not ASCII");
213
+ into.presentTypes |= GeneralNameType.URI;
214
+ } else if (tag === contextTag(7, false)) {
215
+ if (ipForm === "address") {
216
+ if (value.length !== 4 && value.length !== 16) throw new DerError("bad iPAddress");
217
+ into.ipAddresses.push(value);
218
+ } else {
219
+ if (value.length !== 8 && value.length !== 32) throw new DerError("bad iPAddress constraint");
220
+ const half = value.length / 2;
221
+ const mask = value.subarray(half);
222
+ if (!isValidNetmask(mask)) throw new DerError("bad netmask");
223
+ into.ipRanges.push({ address: value.subarray(0, half), mask });
224
+ }
225
+ into.presentTypes |= GeneralNameType.IP_ADDRESS;
226
+ } else if (tag === contextTag(8, false)) {
227
+ into.presentTypes |= GeneralNameType.REGISTERED_ID;
228
+ } else {
229
+ throw new DerError("unknown GeneralName type");
230
+ }
231
+ }
232
+
233
+ /** GeneralNames::Create from an extension value (a SEQUENCE OF GeneralName, non-empty). */
234
+ export function parseGeneralNames(extnValue: Buffer): GeneralNames {
235
+ const reader = new DerReader(extnValue);
236
+ const seq = reader.expect(Tag.SEQUENCE);
237
+ reader.end();
238
+ const out = emptyGeneralNames();
239
+ const items = children(seq);
240
+ if (items.length === 0) throw new DerError("GeneralNames is empty");
241
+ for (const item of items) parseGeneralName(item, out, "address");
242
+ return out;
243
+ }
244
+
245
+ // --- NameConstraints ------------------------------------------------------------------------------------------
246
+
247
+ function parseSubtrees(value: Buffer): GeneralNames {
248
+ const out = emptyGeneralNames();
249
+ const reader = new DerReader(value);
250
+ if (!reader.hasMore) throw new DerError("empty subtrees");
251
+ while (reader.hasMore) {
252
+ const subtree = new DerReader(reader.expect(Tag.SEQUENCE).value);
253
+ parseGeneralName(subtree.read(), out, "constraint");
254
+ // minimum and maximum are not allowed to be present (RFC 5280 4.2.1.10); BoringSSL rejects trailing fields.
255
+ subtree.end();
256
+ }
257
+ return out;
258
+ }
259
+
260
+ function dnsNameMatches(nameIn: string, constraintIn: string, wildcard: "partial" | "full"): boolean {
261
+ let name = nameIn;
262
+ let constraint = constraintIn;
263
+ if (constraint === "") return true;
264
+ if (name.endsWith(".")) name = name.slice(0, -1);
265
+ if (constraint.endsWith(".")) constraint = constraint.slice(0, -1);
266
+ if (wildcard === "partial" && name.length > 2 && name.startsWith("*.")) {
267
+ const dot = constraint.indexOf(".");
268
+ if (dot !== -1 && name.slice(2).toLowerCase() === constraint.slice(dot + 1).toLowerCase()) return true;
269
+ }
270
+ if (!name.toLowerCase().endsWith(constraint.toLowerCase())) return false;
271
+ if (name.length === constraint.length) return true;
272
+ if (constraint.startsWith(".")) constraint = constraint.slice(1);
273
+ return name.length > constraint.length && name[name.length - constraint.length - 1] === ".";
274
+ }
275
+
276
+ const isAlnum = (c: string): boolean => /^[0-9A-Za-z]$/.test(c);
277
+ const allowedLocalPart = (s: string): boolean => s !== "" && [...s].every((c) => isAlnum(c) || "!#$%&'*+-/=?^_`{|}~.".includes(c));
278
+ const allowedDomain = (s: string): boolean => s !== "" && [...s].every((c) => isAlnum(c) || c === "-" || c === ".");
279
+
280
+ function rfc822NameMatches(local: string, domain: string, constraint: string, kind: "permitted" | "excluded", caseInsensitiveLocal: boolean): boolean {
281
+ const errorValue = kind === "excluded";
282
+ const parts = constraint.split("@");
283
+ let constraintLocal = "";
284
+ let constraintDomain: string;
285
+ if (parts.length === 1) {
286
+ constraintDomain = parts[0] as string;
287
+ } else if (parts.length === 2) {
288
+ constraintLocal = parts[0] as string;
289
+ if (!allowedLocalPart(constraintLocal)) return errorValue;
290
+ constraintDomain = parts[1] as string;
291
+ } else {
292
+ return errorValue;
293
+ }
294
+ if (!allowedDomain(constraintDomain)) return errorValue;
295
+ if (constraintLocal !== "") {
296
+ const localEqual = caseInsensitiveLocal ? local.toLowerCase() === constraintLocal.toLowerCase() : local === constraintLocal;
297
+ return localEqual && domain.toLowerCase() === constraintDomain.toLowerCase();
298
+ }
299
+ if (constraintDomain.startsWith(".")) return domain.toLowerCase().endsWith(constraintDomain.toLowerCase());
300
+ return domain.toLowerCase() === constraintDomain.toLowerCase();
301
+ }
302
+
303
+ function ipMatches(addr: Buffer, other: Buffer, mask: Buffer): boolean {
304
+ if (addr.length !== other.length || addr.length !== mask.length) return false;
305
+ for (let i = 0; i < addr.length; i++) if (((addr[i] as number) & (mask[i] as number)) !== ((other[i] as number) & (mask[i] as number))) return false;
306
+ return true;
307
+ }
308
+
309
+ const MAX_NAME_CONSTRAINT_CHECKS = 1 << 20;
310
+
311
+ /** The result of checking a certificate against name constraints. */
312
+ export class NameConstraints {
313
+ private readonly permitted: GeneralNames;
314
+ private readonly excluded: GeneralNames;
315
+ private readonly constrainedTypes: number;
316
+
317
+ private constructor(permitted: GeneralNames, excluded: GeneralNames, critical: boolean) {
318
+ this.permitted = permitted;
319
+ this.excluded = excluded;
320
+ const mask = critical ? ALL_NAME_TYPES : SUPPORTED_NAME_TYPES;
321
+ this.constrainedTypes = (permitted.presentTypes & mask) | (excluded.presentTypes & mask);
322
+ }
323
+
324
+ /** NameConstraints::Create. Throws {@link DerError} when the extension is malformed. */
325
+ static parse(extnValue: Buffer, critical: boolean): NameConstraints {
326
+ const reader = new DerReader(extnValue);
327
+ const seq = new DerReader(reader.expect(Tag.SEQUENCE).value);
328
+ reader.end();
329
+ let permitted = emptyGeneralNames();
330
+ let excluded = emptyGeneralNames();
331
+ const p = seq.optional(contextTag(0, true));
332
+ if (p !== undefined) permitted = parseSubtrees(p.value);
333
+ const e = seq.optional(contextTag(1, true));
334
+ if (e !== undefined) excluded = parseSubtrees(e.value);
335
+ if (p === undefined && e === undefined) throw new DerError("NameConstraints has no subtrees");
336
+ seq.end();
337
+ return new NameConstraints(permitted, excluded, critical);
338
+ }
339
+
340
+ /**
341
+ * IsPermittedCert. Returns false when the certificate's names violate the constraints. `subjectContent` is the
342
+ * subject Name's content octets (for email addresses); `subjectAltNames` is undefined when the extension is absent.
343
+ */
344
+ isPermittedCert(subject: NormalizedName, subjectContent: Buffer, subjectAltNames: GeneralNames | undefined): boolean {
345
+ let nameCount: number;
346
+ let constraintCount = 0;
347
+ if (subjectAltNames !== undefined) {
348
+ nameCount = subjectAltNames.rfc822.length + subjectAltNames.dns.length + subjectAltNames.directory.length + subjectAltNames.ipAddresses.length;
349
+ constraintCount =
350
+ this.excluded.rfc822.length + this.permitted.rfc822.length + this.excluded.dns.length + this.permitted.dns.length +
351
+ this.excluded.directory.length + this.permitted.directory.length + this.excluded.ipRanges.length + this.permitted.ipRanges.length;
352
+ } else {
353
+ constraintCount += this.excluded.directory.length + this.permitted.directory.length;
354
+ nameCount = subjectContent.length;
355
+ }
356
+ if (constraintCount * nameCount > MAX_NAME_CONSTRAINT_CHECKS) return false;
357
+
358
+ let subjectEmails: string[] = [];
359
+ if (subjectAltNames === undefined && (this.constrainedTypes & GeneralNameType.RFC822_NAME) !== 0) {
360
+ const found = findEmailAddresses(subjectContent);
361
+ if (found === undefined) return false;
362
+ subjectEmails = found;
363
+ }
364
+
365
+ if (subjectAltNames !== undefined) {
366
+ if ((this.constrainedTypes & subjectAltNames.presentTypes & ~SUPPORTED_NAME_TYPES) !== 0) return false;
367
+ if ((this.constrainedTypes & GeneralNameType.RFC822_NAME) !== 0) {
368
+ for (const name of subjectAltNames.rfc822) if (!this.isPermittedRfc822(name, false)) return false;
369
+ }
370
+ for (const name of subjectAltNames.dns) if (!this.isPermittedDns(name)) return false;
371
+ for (const name of subjectAltNames.directory) if (!this.isPermittedDirectory(name)) return false;
372
+ for (const ip of subjectAltNames.ipAddresses) if (!this.isPermittedIp(ip)) return false;
373
+ }
374
+
375
+ for (const email of subjectEmails) if (!this.isPermittedRfc822(email, true)) return false;
376
+
377
+ // With a SAN and an empty subject the subject DN is not checked (RFC 5280 4.2.1.10).
378
+ if (subjectAltNames !== undefined && subject.length === 0) return true;
379
+ return this.isPermittedDirectory(subject);
380
+ }
381
+
382
+ private isPermittedRfc822(name: string, caseInsensitiveExcludeLocal: boolean): boolean {
383
+ const parts = name.split("@");
384
+ if (parts.length !== 2) return false;
385
+ const [local, domain] = parts as [string, string];
386
+ if (!allowedLocalPart(local) || !allowedDomain(domain)) return false;
387
+ for (const excluded of this.excluded.rfc822) {
388
+ if (rfc822NameMatches(local, domain, excluded, "excluded", caseInsensitiveExcludeLocal)) return false;
389
+ }
390
+ if ((this.permitted.presentTypes & GeneralNameType.RFC822_NAME) === 0) return true;
391
+ return this.permitted.rfc822.some((p) => rfc822NameMatches(local, domain, p, "permitted", false));
392
+ }
393
+
394
+ private isPermittedDns(name: string): boolean {
395
+ for (const excluded of this.excluded.dns) if (dnsNameMatches(name, excluded, "partial")) return false;
396
+ if ((this.permitted.presentTypes & GeneralNameType.DNS_NAME) === 0) return true;
397
+ return this.permitted.dns.some((p) => dnsNameMatches(name, p, "full"));
398
+ }
399
+
400
+ private isPermittedDirectory(name: NormalizedName): boolean {
401
+ for (const excluded of this.excluded.directory) if (nameInSubtree(name, excluded)) return false;
402
+ if ((this.permitted.presentTypes & GeneralNameType.DIRECTORY_NAME) === 0) return true;
403
+ return this.permitted.directory.some((p) => nameInSubtree(name, p));
404
+ }
405
+
406
+ private isPermittedIp(ip: Buffer): boolean {
407
+ for (const excluded of this.excluded.ipRanges) if (ipMatches(ip, excluded.address, excluded.mask)) return false;
408
+ if ((this.permitted.presentTypes & GeneralNameType.IP_ADDRESS) === 0) return true;
409
+ return this.permitted.ipRanges.some((p) => ipMatches(ip, p.address, p.mask));
410
+ }
411
+ }