@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 157 lines 5.52 KB
Raw
1 /**
2 * A minimal DER encoder/decoder — just enough ASN.1 for a PKCS#10
3 * certificate request (encode) and reading a certificate's serial,
4 * validity, issuer and subject alternative names (decode). Pure
5 * TypeScript; no native code, no dependency, works on workerd.
6 *
7 * @internal
8 */
9
10 export const concat = (...parts: ReadonlyArray<Uint8Array>): Uint8Array => {
11 const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
12 let offset = 0;
13 for (const part of parts) {
14 out.set(part, offset);
15 offset += part.length;
16 }
17 return out;
18 };
19
20 const encodeLength = (n: number): Uint8Array => {
21 if (n < 0x80) return Uint8Array.of(n);
22 if (n < 0x100) return Uint8Array.of(0x81, n);
23 if (n < 0x10000) return Uint8Array.of(0x82, n >>> 8, n & 0xff);
24 return Uint8Array.of(0x83, n >>> 16, (n >>> 8) & 0xff, n & 0xff);
25 };
26
27 /** Tag-length-value. */
28 export const tlv = (tag: number, body: Uint8Array): Uint8Array =>
29 concat(Uint8Array.of(tag), encodeLength(body.length), body);
30
31 export const sequence = (...items: ReadonlyArray<Uint8Array>) => tlv(0x30, concat(...items));
32 export const set = (...items: ReadonlyArray<Uint8Array>) => tlv(0x31, concat(...items));
33 export const octetString = (body: Uint8Array) => tlv(0x04, body);
34 export const bitString = (body: Uint8Array) => tlv(0x03, concat(Uint8Array.of(0), body));
35 export const nullValue = () => Uint8Array.of(0x05, 0x00);
36 export const ia5String = (text: string) => tlv(0x16, new TextEncoder().encode(text));
37 export const utf8String = (text: string) => tlv(0x0c, new TextEncoder().encode(text));
38 /** Context-specific constructed tag `[n]` (implicit or explicit, caller's choice of body). */
39 export const contextTag = (n: number, body: Uint8Array) => tlv(0xa0 | n, body);
40 /** Context-specific primitive tag `[n]` (e.g. GeneralName dNSName is `[2]`). */
41 export const contextPrimitive = (n: number, body: Uint8Array) => tlv(0x80 | n, body);
42
43 /** INTEGER from a non-negative number or big-endian magnitude bytes. */
44 export const integer = (value: number | Uint8Array): Uint8Array => {
45 if (typeof value === "number") {
46 if (value === 0) return tlv(0x02, Uint8Array.of(0));
47 const bytes: number[] = [];
48 let n = value;
49 while (n > 0) {
50 bytes.unshift(n & 0xff);
51 n = Math.floor(n / 256);
52 }
53 if (bytes[0]! & 0x80) bytes.unshift(0);
54 return tlv(0x02, Uint8Array.from(bytes));
55 }
56 let start = 0;
57 while (start < value.length - 1 && value[start] === 0) start++;
58 const magnitude = value.subarray(start);
59 return tlv(0x02, magnitude[0]! & 0x80 ? concat(Uint8Array.of(0), magnitude) : magnitude);
60 };
61
62 /** OBJECT IDENTIFIER from dotted notation. */
63 export const oid = (dotted: string): Uint8Array => {
64 const parts = dotted.split(".").map(Number);
65 const bytes: number[] = [parts[0]! * 40 + parts[1]!];
66 for (const part of parts.slice(2)) {
67 const stack: number[] = [part & 0x7f];
68 let n = Math.floor(part / 128);
69 while (n > 0) {
70 stack.unshift((n & 0x7f) | 0x80);
71 n = Math.floor(n / 128);
72 }
73 bytes.push(...stack);
74 }
75 return tlv(0x06, Uint8Array.from(bytes));
76 };
77
78 // =============================================================================
79 // Decoding
80 // =============================================================================
81
82 export interface Node {
83 readonly tag: number;
84 /** Offset of the first content byte. */
85 readonly start: number;
86 /** Offset one past the last content byte. */
87 readonly end: number;
88 readonly bytes: Uint8Array;
89 }
90
91 export const read = (bytes: Uint8Array, offset: number): Node => {
92 const tag = bytes[offset]!;
93 let cursor = offset + 1;
94 let length = bytes[cursor++]!;
95 if (length & 0x80) {
96 const count = length & 0x7f;
97 length = 0;
98 for (let i = 0; i < count; i++) length = length * 256 + bytes[cursor++]!;
99 }
100 return { tag, start: cursor, end: cursor + length, bytes };
101 };
102
103 /** The children of a constructed node, in order. */
104 export const children = (node: Node): Node[] => {
105 const out: Node[] = [];
106 let cursor = node.start;
107 while (cursor < node.end) {
108 const child = read(node.bytes, cursor);
109 out.push(child);
110 cursor = child.end;
111 }
112 return out;
113 };
114
115 export const content = (node: Node): Uint8Array => node.bytes.subarray(node.start, node.end);
116
117 export const decodeOid = (node: Node): string => {
118 const body = content(node);
119 const first = body[0]!;
120 const parts = [Math.floor(first / 40), first % 40];
121 let n = 0;
122 for (let i = 1; i < body.length; i++) {
123 n = n * 128 + (body[i]! & 0x7f);
124 if ((body[i]! & 0x80) === 0) {
125 parts.push(n);
126 n = 0;
127 }
128 }
129 // OIDs under joint-iso-itu-t(2) can have a first arc ≥ 2 with large second arcs.
130 if (first >= 80) {
131 parts[0] = 2;
132 parts[1] = first - 80;
133 }
134 return parts.join(".");
135 };
136
137 /** Parse UTCTime (`YYMMDDHHMMSSZ`) or GeneralizedTime (`YYYYMMDDHHMMSSZ`). */
138 export const decodeTime = (node: Node): Date => {
139 const text = new TextDecoder().decode(content(node));
140 const generalized = node.tag === 0x18;
141 const year = generalized
142 ? Number(text.slice(0, 4))
143 : (() => {
144 const yy = Number(text.slice(0, 2));
145 return yy < 50 ? 2000 + yy : 1900 + yy;
146 })();
147 const rest = generalized ? text.slice(4) : text.slice(2);
148 const month = Number(rest.slice(0, 2));
149 const day = Number(rest.slice(2, 4));
150 const hour = Number(rest.slice(4, 6));
151 const minute = Number(rest.slice(6, 8));
152 const second = Number(rest.slice(8, 10)) || 0;
153 return new Date(Date.UTC(year, month - 1, day, hour, minute, second));
154 };
155
156 export const toHex = (bytes: Uint8Array): string =>
157 Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");