@tide-ota/protocol 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +7 -2
- package/dist/bzip2.d.ts +14 -0
- package/dist/bzip2.js +307 -0
- package/dist/hash.d.ts +21 -0
- package/dist/hash.js +54 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +9 -0
- package/dist/manifest.d.ts +38 -0
- package/dist/manifest.js +186 -0
- package/dist/multipart.d.ts +34 -0
- package/dist/multipart.js +213 -0
- package/dist/patch.d.ts +35 -0
- package/dist/patch.js +201 -0
- package/dist/reply.d.ts +46 -0
- package/dist/reply.js +215 -0
- package/dist/s3.d.ts +117 -0
- package/dist/s3.js +411 -0
- package/dist/sfv.d.ts +12 -0
- package/dist/sfv.js +146 -0
- package/dist/signature.d.ts +32 -0
- package/dist/signature.js +366 -0
- package/package.json +50 -3
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { ProtocolError, base64, decodeBase64, sha256Hex } from "./hash.js";
|
|
2
|
+
import { encodeSfvDictionary, parseSfvDictionary } from "./sfv.js";
|
|
3
|
+
const ALG = "rsa-v1_5-sha256";
|
|
4
|
+
const RSA = { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" };
|
|
5
|
+
/** Returns a signer over a private RSASSA-PKCS1-v1_5 SHA-256 key, the only key whose digest matches the header. */
|
|
6
|
+
export function cryptoKeySigner(key, keyId) {
|
|
7
|
+
checkKey(key, "private", "sign");
|
|
8
|
+
return {
|
|
9
|
+
keyId,
|
|
10
|
+
sign: async (bytes) => new Uint8Array(await crypto.subtle.sign(RSA, key, bytes)),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/** Returns the expo-signature header value for `bytes`. The key ID is checked before the signer runs. */
|
|
14
|
+
export async function expoSignature(bytes, signer) {
|
|
15
|
+
const keyId = signer.keyId;
|
|
16
|
+
if (typeof keyId !== "string" || !/^[\x20-\x7E]+$/.test(keyId)) {
|
|
17
|
+
throw new ProtocolError("keyId", "must be non-empty printable ASCII");
|
|
18
|
+
}
|
|
19
|
+
const sig = await signer.sign(bytes);
|
|
20
|
+
if (!(sig instanceof Uint8Array) || sig.length === 0) {
|
|
21
|
+
throw new ProtocolError("signer", "must resolve non-empty bytes");
|
|
22
|
+
}
|
|
23
|
+
return encodeSfvDictionary({ sig: base64(sig), keyid: keyId, alg: ALG });
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Checks `header` over `bytes` as expo-updates does, but stricter: a non-string `keyid` or `alg` is malformed,
|
|
27
|
+
* where the client would apply the default. `key` must come from trusted configuration, never from the response.
|
|
28
|
+
*/
|
|
29
|
+
export async function verifyExpoSignature(bytes, header, key, keyId) {
|
|
30
|
+
checkKey(key, "public", "verify");
|
|
31
|
+
const { sig, keyId: id } = parseExpoSignature(header);
|
|
32
|
+
if (id !== keyId)
|
|
33
|
+
throw new ProtocolError("signature.keyid", `must be ${keyId}`);
|
|
34
|
+
if (!(await crypto.subtle.verify(RSA, key, sig, bytes))) {
|
|
35
|
+
throw new ProtocolError("signature.sig", "does not match the bytes and key");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Returns the signature bytes and key ID of an expo-signature header, checked as `verifyExpoSignature` does. */
|
|
39
|
+
export function parseExpoSignature(header) {
|
|
40
|
+
const members = parseSfvDictionary(header, "signature");
|
|
41
|
+
const member = (name, fallback) => {
|
|
42
|
+
const m = members.get(name);
|
|
43
|
+
const value = m === undefined ? fallback : m.kind === "string" ? m.text : undefined;
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
throw new ProtocolError("signature", `${name} must be a string`);
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
// iOS reads the whole dictionary as [String: String], so any other member fails there.
|
|
49
|
+
for (const [name, { kind }] of members) {
|
|
50
|
+
if (kind !== "string" && kind !== "token") {
|
|
51
|
+
throw new ProtocolError("signature", `${name} must be a string or token`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const sigText = member("sig");
|
|
55
|
+
const alg = member("alg", ALG);
|
|
56
|
+
const keyId = member("keyid", "root");
|
|
57
|
+
const sig = decodeBase64(sigText);
|
|
58
|
+
if (sig === undefined)
|
|
59
|
+
throw new ProtocolError("signature", "sig must be padded base64");
|
|
60
|
+
if (alg !== ALG)
|
|
61
|
+
throw new ProtocolError("signature.alg", `must be ${ALG}`);
|
|
62
|
+
return { sig, keyId };
|
|
63
|
+
}
|
|
64
|
+
/** Returns the verify key in the first CERTIFICATE block of `pem`. Dates, usages and the chain are not checked. */
|
|
65
|
+
export async function certificatePublicKey(pem) {
|
|
66
|
+
const block = /-----BEGIN CERTIFICATE-----([^-]*)-----END CERTIFICATE-----/.exec(pem)?.[1];
|
|
67
|
+
if (block === undefined)
|
|
68
|
+
throw new ProtocolError("certificate", "has no CERTIFICATE block");
|
|
69
|
+
const der = decodeBase64(block.replace(/[\r\n]/g, ""));
|
|
70
|
+
if (der === undefined)
|
|
71
|
+
throw new ProtocolError("certificate", "must be canonical base64");
|
|
72
|
+
return rsaKey(der, parseCertificate(der).spki);
|
|
73
|
+
}
|
|
74
|
+
const SHA256_WITH_RSA = "1.2.840.113549.1.1.11";
|
|
75
|
+
const KEY_USAGE = "2.5.29.15";
|
|
76
|
+
const EXT_KEY_USAGE = "2.5.29.37";
|
|
77
|
+
const CODE_SIGNING = "1.3.6.1.5.5.7.3.3";
|
|
78
|
+
// Expo's project-scoped certificates; Tide's replies carry no project ID or scope key to check it against.
|
|
79
|
+
const EXPO_PROJECT_INFORMATION = "1.2.840.113556.1.8000.2554.43437.254.128.102.157.7894389.20439.2.1";
|
|
80
|
+
/**
|
|
81
|
+
* Returns `pem` as the one self-signed certificate stock expo-updates trusts for direct signing at `now`: X.509 v3,
|
|
82
|
+
* valid at `now`, sha256WithRSAEncryption, Key Usage digitalSignature, Extended Key Usage codeSigning, no Expo
|
|
83
|
+
* project information, and an RSA key of 2048 to 4096 bits.
|
|
84
|
+
*/
|
|
85
|
+
export async function codeSigningCertificate(pem, now) {
|
|
86
|
+
// ASCII, with each delimiter alone on its line: iOS decodes nothing else.
|
|
87
|
+
const block = /^(?:[\t\n\r ]*\n)?-----BEGIN CERTIFICATE-----\r?\n((?:[A-Za-z0-9+/=]*\r?\n)*)-----END CERTIFICATE-----(?:\r?\n[\t\n\r ]*)?$/.exec(pem)?.[1];
|
|
88
|
+
if (block === undefined) {
|
|
89
|
+
throw new ProtocolError("certificate", "must be exactly one PEM CERTIFICATE block");
|
|
90
|
+
}
|
|
91
|
+
const der = decodeBase64(block.replace(/\r?\n/g, ""));
|
|
92
|
+
if (der === undefined)
|
|
93
|
+
throw new ProtocolError("certificate", "must be canonical base64");
|
|
94
|
+
const cert = parseCertificate(der);
|
|
95
|
+
const fail = (message) => new ProtocolError("certificate", message);
|
|
96
|
+
if (cert.version === undefined || !equal(der, cert.version, V3)) {
|
|
97
|
+
throw fail("must be X.509 v3");
|
|
98
|
+
}
|
|
99
|
+
if (!equal(der, cert.innerAlgorithm, bytesOf(der, cert.outerAlgorithm))) {
|
|
100
|
+
throw fail("must name the same signature algorithm in the TBSCertificate and outside it");
|
|
101
|
+
}
|
|
102
|
+
const algorithm = sequence(der, cert.outerAlgorithm);
|
|
103
|
+
if (algorithm[0]?.tag !== OID ||
|
|
104
|
+
oid(der, algorithm[0]) !== SHA256_WITH_RSA ||
|
|
105
|
+
algorithm.length > 2 ||
|
|
106
|
+
(algorithm[1] !== undefined && !equal(der, algorithm[1], NULL))) {
|
|
107
|
+
throw fail("must be signed with sha256WithRSAEncryption");
|
|
108
|
+
}
|
|
109
|
+
if (!equal(der, cert.issuer, bytesOf(der, cert.subject))) {
|
|
110
|
+
throw fail("must be self-issued");
|
|
111
|
+
}
|
|
112
|
+
const validity = sequence(der, cert.validity);
|
|
113
|
+
const [notBefore, notAfter] = validity.map((t) => time(der, t));
|
|
114
|
+
if (notBefore === undefined || notAfter === undefined || validity.length !== 2) {
|
|
115
|
+
throw fail("has a malformed validity");
|
|
116
|
+
}
|
|
117
|
+
if (now.getTime() < notBefore)
|
|
118
|
+
throw fail("is not valid yet");
|
|
119
|
+
if (now.getTime() > notAfter)
|
|
120
|
+
throw fail("has expired");
|
|
121
|
+
const extensions = extensionsOf(der, cert.extensions);
|
|
122
|
+
if (!digitalSignature(extensions.get(KEY_USAGE))) {
|
|
123
|
+
throw fail("must have Key Usage digitalSignature");
|
|
124
|
+
}
|
|
125
|
+
if (!extendedKeyUsages(extensions.get(EXT_KEY_USAGE)).includes(CODE_SIGNING)) {
|
|
126
|
+
throw fail("must have Extended Key Usage codeSigning");
|
|
127
|
+
}
|
|
128
|
+
if (extensions.has(EXPO_PROJECT_INFORMATION)) {
|
|
129
|
+
throw fail("must not carry Expo project information");
|
|
130
|
+
}
|
|
131
|
+
const key = await rsaKey(der, cert.spki);
|
|
132
|
+
const { modulusLength: bits } = key.algorithm;
|
|
133
|
+
if (bits === undefined || bits < 2048 || bits > 4096)
|
|
134
|
+
throw fail("must hold an RSA key of 2048 to 4096 bits");
|
|
135
|
+
const signature = der.subarray(cert.signature.body, cert.signature.end);
|
|
136
|
+
if (signature[0] !== 0 ||
|
|
137
|
+
!(await crypto.subtle.verify(RSA, key, signature.slice(1), der.slice(cert.tbs.start, cert.tbs.end)))) {
|
|
138
|
+
throw fail("has a self-signature that does not verify");
|
|
139
|
+
}
|
|
140
|
+
return { key, sha256: await sha256Hex(der) };
|
|
141
|
+
}
|
|
142
|
+
function checkKey(key, type, usage) {
|
|
143
|
+
const algorithm = key.algorithm;
|
|
144
|
+
if (key.type !== type ||
|
|
145
|
+
algorithm.name !== RSA.name ||
|
|
146
|
+
algorithm.hash?.name !== RSA.hash ||
|
|
147
|
+
!key.usages.includes(usage)) {
|
|
148
|
+
throw new ProtocolError("key", `must be a ${type} ${RSA.name} ${RSA.hash} key for ${usage}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const SEQUENCE = 0x30;
|
|
152
|
+
const INTEGER = 0x02;
|
|
153
|
+
const BIT_STRING = 0x03;
|
|
154
|
+
const OCTET_STRING = 0x04;
|
|
155
|
+
const OID = 0x06;
|
|
156
|
+
const BOOLEAN = 0x01;
|
|
157
|
+
const VERSION = 0xa0;
|
|
158
|
+
const EXTENSIONS = 0xa3;
|
|
159
|
+
const V3 = new Uint8Array([VERSION, 3, INTEGER, 1, 2]);
|
|
160
|
+
const NULL = new Uint8Array([0x05, 0]);
|
|
161
|
+
// serial, signature, issuer, validity, subject, subjectPublicKeyInfo
|
|
162
|
+
const TBS_TAGS = [INTEGER, SEQUENCE, SEQUENCE, SEQUENCE, SEQUENCE, SEQUENCE];
|
|
163
|
+
// issuerUniqueID, subjectUniqueID, extensions: each optional, in this order.
|
|
164
|
+
const TBS_TAIL = [0x81, 0x82, EXTENSIONS];
|
|
165
|
+
/** Returns the fields of an X.509 certificate's DER. */
|
|
166
|
+
function parseCertificate(der) {
|
|
167
|
+
const cert = readTlv(der, 0, der.length);
|
|
168
|
+
if (cert.end !== der.length)
|
|
169
|
+
throw new ProtocolError("certificate", "has bytes after the DER");
|
|
170
|
+
const envelope = sequence(der, cert);
|
|
171
|
+
const [tbsTlv, outerAlgorithm, signature] = envelope;
|
|
172
|
+
if (!hasTags(envelope, [SEQUENCE, SEQUENCE, BIT_STRING]) ||
|
|
173
|
+
envelope.length !== 3 ||
|
|
174
|
+
tbsTlv === undefined ||
|
|
175
|
+
outerAlgorithm === undefined ||
|
|
176
|
+
signature === undefined) {
|
|
177
|
+
throw new ProtocolError("certificate", "must be exactly SEQUENCE { tbs, algorithm, signature }");
|
|
178
|
+
}
|
|
179
|
+
const tbs = sequence(der, tbsTlv);
|
|
180
|
+
const version = tbs[0]?.tag === VERSION ? tbs[0] : undefined;
|
|
181
|
+
const fields = version === undefined ? tbs : tbs.slice(1);
|
|
182
|
+
const [, innerAlgorithm, issuer, validity, subject, spki] = fields;
|
|
183
|
+
const tail = fields.slice(6).map((f) => f.tag);
|
|
184
|
+
if (innerAlgorithm === undefined ||
|
|
185
|
+
issuer === undefined ||
|
|
186
|
+
validity === undefined ||
|
|
187
|
+
subject === undefined ||
|
|
188
|
+
spki === undefined ||
|
|
189
|
+
!hasTags(fields, TBS_TAGS) ||
|
|
190
|
+
tail.some((tag, i) => TBS_TAIL.indexOf(tag) <= TBS_TAIL.indexOf(tail[i - 1] ?? -1))) {
|
|
191
|
+
throw new ProtocolError("certificate", "has a malformed TBSCertificate");
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
tbs: tbsTlv,
|
|
195
|
+
version,
|
|
196
|
+
innerAlgorithm,
|
|
197
|
+
issuer,
|
|
198
|
+
validity,
|
|
199
|
+
subject,
|
|
200
|
+
spki,
|
|
201
|
+
extensions: fields.slice(6).find((f) => f.tag === EXTENSIONS),
|
|
202
|
+
outerAlgorithm,
|
|
203
|
+
signature,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
/** Imports the SubjectPublicKeyInfo at `spki` as an RSASSA-PKCS1-v1_5 SHA-256 verify key. */
|
|
207
|
+
async function rsaKey(der, spki) {
|
|
208
|
+
try {
|
|
209
|
+
return await crypto.subtle.importKey("spki", der.slice(spki.start, spki.end), RSA, false, [
|
|
210
|
+
"verify",
|
|
211
|
+
]);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
throw new ProtocolError("certificate", "must hold an RSA public key");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Returns each extension's value (the DER inside its OCTET STRING) by dotted OID; a repeated OID is refused. */
|
|
218
|
+
function extensionsOf(der, wrapper) {
|
|
219
|
+
const out = new Map();
|
|
220
|
+
if (wrapper === undefined)
|
|
221
|
+
return out;
|
|
222
|
+
const malformed = () => new ProtocolError("certificate", "has a malformed extension");
|
|
223
|
+
const inner = readTlv(der, wrapper.body, wrapper.end);
|
|
224
|
+
if (inner.end !== wrapper.end)
|
|
225
|
+
throw malformed();
|
|
226
|
+
for (const extension of sequence(der, inner)) {
|
|
227
|
+
const items = sequence(der, extension);
|
|
228
|
+
const [id, second, third] = items;
|
|
229
|
+
const value = items.length === 3 ? third : second;
|
|
230
|
+
if (id?.tag !== OID ||
|
|
231
|
+
value?.tag !== OCTET_STRING ||
|
|
232
|
+
items.length > 3 ||
|
|
233
|
+
(items.length === 3 && second?.tag !== BOOLEAN)) {
|
|
234
|
+
throw malformed();
|
|
235
|
+
}
|
|
236
|
+
const name = oid(der, id);
|
|
237
|
+
// RFC 5280 4.2: a certificate must not include more than one instance of an extension.
|
|
238
|
+
if (out.has(name))
|
|
239
|
+
throw new ProtocolError("certificate", "repeats an extension");
|
|
240
|
+
out.set(name, der.subarray(value.body, value.end));
|
|
241
|
+
}
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
/** True when a Key Usage value asserts digitalSignature, bit 0: the top bit after the unused-bit count. */
|
|
245
|
+
function digitalSignature(value) {
|
|
246
|
+
if (value === undefined)
|
|
247
|
+
return false;
|
|
248
|
+
const bits = readTlv(value, 0, value.length);
|
|
249
|
+
// Apple's parser refuses the whole certificate for an unused-bit count over 7.
|
|
250
|
+
if (bits.tag === BIT_STRING && (value[bits.body] ?? 0) > 7) {
|
|
251
|
+
throw new ProtocolError("certificate", "has a malformed extension");
|
|
252
|
+
}
|
|
253
|
+
return (bits.tag === BIT_STRING &&
|
|
254
|
+
bits.end === value.length &&
|
|
255
|
+
bits.end - bits.body >= 2 &&
|
|
256
|
+
((value[bits.body + 1] ?? 0) & 0x80) !== 0);
|
|
257
|
+
}
|
|
258
|
+
/** Returns the OIDs in an Extended Key Usage value, or none when it is absent. */
|
|
259
|
+
function extendedKeyUsages(value) {
|
|
260
|
+
if (value === undefined)
|
|
261
|
+
return [];
|
|
262
|
+
const outer = readTlv(value, 0, value.length);
|
|
263
|
+
if (outer.end !== value.length)
|
|
264
|
+
throw new ProtocolError("certificate", "has a malformed extension");
|
|
265
|
+
// Apple's parser refuses the whole certificate for any entry that is not an OID.
|
|
266
|
+
return sequence(value, outer).map((t) => {
|
|
267
|
+
if (t.tag !== OID)
|
|
268
|
+
throw new ProtocolError("certificate", "has a malformed extension");
|
|
269
|
+
return oid(value, t);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
/** Returns the dotted form of the OBJECT IDENTIFIER at `tlv`. */
|
|
273
|
+
function oid(der, tlv) {
|
|
274
|
+
const arcs = [];
|
|
275
|
+
let arc = 0;
|
|
276
|
+
for (let at = tlv.body; at < tlv.end; at++) {
|
|
277
|
+
const byte = der[at] ?? 0;
|
|
278
|
+
if (arc === 0 && byte === 0x80)
|
|
279
|
+
throw new ProtocolError("certificate", "has a malformed OID");
|
|
280
|
+
arc = arc * 128 + (byte & 0x7f);
|
|
281
|
+
if (arc > Number.MAX_SAFE_INTEGER / 128) {
|
|
282
|
+
throw new ProtocolError("certificate", "has a malformed OID");
|
|
283
|
+
}
|
|
284
|
+
if ((byte & 0x80) === 0) {
|
|
285
|
+
arcs.push(arc);
|
|
286
|
+
arc = 0;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
const [first] = arcs;
|
|
290
|
+
if (first === undefined || arc !== 0)
|
|
291
|
+
throw new ProtocolError("certificate", "has a malformed OID");
|
|
292
|
+
const top = Math.min(2, Math.floor(first / 40));
|
|
293
|
+
return [top, first - top * 40, ...arcs.slice(1)].join(".");
|
|
294
|
+
}
|
|
295
|
+
/** Returns a UTCTime or GeneralizedTime in `Z` form as epoch milliseconds, or undefined. */
|
|
296
|
+
function time(der, tlv) {
|
|
297
|
+
const text = String.fromCharCode(...der.subarray(tlv.body, tlv.end));
|
|
298
|
+
const m = tlv.tag === 0x17
|
|
299
|
+
? /^(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z$/.exec(text)
|
|
300
|
+
: tlv.tag === 0x18
|
|
301
|
+
? /^(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)Z$/.exec(text)
|
|
302
|
+
: null;
|
|
303
|
+
if (m === null)
|
|
304
|
+
return undefined;
|
|
305
|
+
const [y, mo, d, h, mi, s] = m.slice(1).map(Number);
|
|
306
|
+
// RFC 5280 4.1.2.5.1: a two-digit year of 50 or more is 19YY.
|
|
307
|
+
const year = tlv.tag === 0x17 ? (y >= 50 ? 1900 : 2000) + y : y;
|
|
308
|
+
const date = new Date(Date.UTC(year, mo - 1, d, h, mi, s));
|
|
309
|
+
const same = date.getUTCFullYear() === year &&
|
|
310
|
+
date.getUTCMonth() === mo - 1 &&
|
|
311
|
+
date.getUTCDate() === d &&
|
|
312
|
+
date.getUTCHours() === h &&
|
|
313
|
+
date.getUTCMinutes() === mi &&
|
|
314
|
+
date.getUTCSeconds() === s;
|
|
315
|
+
return same ? date.getTime() : undefined;
|
|
316
|
+
}
|
|
317
|
+
/** True when the TLV at `tlv` is exactly `bytes`. */
|
|
318
|
+
function equal(der, tlv, bytes) {
|
|
319
|
+
const own = bytesOf(der, tlv);
|
|
320
|
+
return own.length === bytes.length && own.every((b, i) => b === bytes[i]);
|
|
321
|
+
}
|
|
322
|
+
/** Returns the whole TLV at `tlv`, header included. */
|
|
323
|
+
const bytesOf = (der, tlv) => der.subarray(tlv.start, tlv.end);
|
|
324
|
+
/** Returns the TLVs that exactly fill SEQUENCE `parent`. */
|
|
325
|
+
function sequence(der, parent) {
|
|
326
|
+
if (parent?.tag !== SEQUENCE)
|
|
327
|
+
throw new ProtocolError("certificate", "expected a DER SEQUENCE");
|
|
328
|
+
const items = [];
|
|
329
|
+
for (let at = parent.body; at < parent.end;) {
|
|
330
|
+
const item = readTlv(der, at, parent.end);
|
|
331
|
+
items.push(item);
|
|
332
|
+
at = item.end;
|
|
333
|
+
}
|
|
334
|
+
return items;
|
|
335
|
+
}
|
|
336
|
+
function hasTags(items, tags) {
|
|
337
|
+
return tags.every((tag, i) => items[i]?.tag === tag);
|
|
338
|
+
}
|
|
339
|
+
/** Reads one DER TLV inside `limit`: a low tag number and a minimal definite length of at most 4 octets. */
|
|
340
|
+
function readTlv(der, start, limit) {
|
|
341
|
+
const byte = (at) => {
|
|
342
|
+
if (at >= limit)
|
|
343
|
+
throw new ProtocolError("certificate", "has truncated DER");
|
|
344
|
+
return der[at] ?? 0;
|
|
345
|
+
};
|
|
346
|
+
const tag = byte(start);
|
|
347
|
+
if ((tag & 0x1f) === 0x1f)
|
|
348
|
+
throw new ProtocolError("certificate", "has a high DER tag number");
|
|
349
|
+
let length = byte(start + 1);
|
|
350
|
+
let body = start + 2;
|
|
351
|
+
if (length >= 0x80) {
|
|
352
|
+
const octets = length & 0x7f;
|
|
353
|
+
if (octets === 0 || octets > 4 || byte(body) === 0) {
|
|
354
|
+
throw new ProtocolError("certificate", "has a DER length that is not minimal and definite");
|
|
355
|
+
}
|
|
356
|
+
length = 0;
|
|
357
|
+
for (let i = 0; i < octets; i++)
|
|
358
|
+
length = length * 256 + byte(body++);
|
|
359
|
+
if (length < 0x80) {
|
|
360
|
+
throw new ProtocolError("certificate", "has a DER length that is not minimal and definite");
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (body + length > limit)
|
|
364
|
+
throw new ProtocolError("certificate", "has truncated DER");
|
|
365
|
+
return { tag, start, body, end: body + length };
|
|
366
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tide-ota/protocol",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Expo Updates protocol v1 on web-standard APIs: manifests, multipart replies, code signing and bsdiff patches.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"expo-updates",
|
|
7
|
+
"expo-updates-protocol",
|
|
8
|
+
"manifest",
|
|
9
|
+
"code-signing",
|
|
10
|
+
"multipart",
|
|
11
|
+
"bsdiff",
|
|
12
|
+
"bspatch",
|
|
13
|
+
"web-crypto"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/HashimTheArab/tide/blob/main/docs/design.md",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/HashimTheArab/tide/issues"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/HashimTheArab/tide.git",
|
|
23
|
+
"directory": "packages/protocol"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"type": "module",
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=24.11"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@cloudflare/vitest-plugin": "1.8.3",
|
|
42
|
+
"@types/node": "^26.6.6",
|
|
43
|
+
"fast-check": "4.10.2",
|
|
44
|
+
"typescript": "7.0.2",
|
|
45
|
+
"vitest": "^5.1.0"
|
|
46
|
+
},
|
|
47
|
+
"sideEffects": false,
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
50
|
+
"typecheck": "tsc --noEmit -p tsconfig.web.json && tsc --noEmit -p tsconfig.node.json",
|
|
51
|
+
"test": "vitest run"
|
|
52
|
+
}
|
|
6
53
|
}
|