@freedomofpress/sigstore-browser 0.1.0 → 0.1.2

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 (133) hide show
  1. package/dist/cli.js +1 -1
  2. package/dist/cli.js.map +1 -1
  3. package/dist/index.d.ts +3 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +3 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/interfaces.d.ts +1 -15
  8. package/dist/interfaces.d.ts.map +1 -1
  9. package/dist/interfaces.js +2 -18
  10. package/dist/interfaces.js.map +1 -1
  11. package/dist/rfc3161/timestamp.d.ts +1 -1
  12. package/dist/rfc3161/timestamp.d.ts.map +1 -1
  13. package/dist/rfc3161/timestamp.js +3 -5
  14. package/dist/rfc3161/timestamp.js.map +1 -1
  15. package/dist/rfc3161/tstinfo.d.ts +1 -1
  16. package/dist/rfc3161/tstinfo.d.ts.map +1 -1
  17. package/dist/rfc3161/tstinfo.js +18 -4
  18. package/dist/rfc3161/tstinfo.js.map +1 -1
  19. package/dist/sigstore.d.ts +1 -1
  20. package/dist/sigstore.d.ts.map +1 -1
  21. package/dist/sigstore.js +6 -8
  22. package/dist/sigstore.js.map +1 -1
  23. package/dist/timestamp/tsa.d.ts +1 -1
  24. package/dist/timestamp/tsa.d.ts.map +1 -1
  25. package/dist/timestamp/tsa.js +4 -5
  26. package/dist/timestamp/tsa.js.map +1 -1
  27. package/dist/tlog/body.js +1 -1
  28. package/dist/tlog/body.js.map +1 -1
  29. package/dist/tlog/checkpoint.d.ts.map +1 -1
  30. package/dist/tlog/checkpoint.js +6 -7
  31. package/dist/tlog/checkpoint.js.map +1 -1
  32. package/dist/tlog/dsse.js +3 -3
  33. package/dist/tlog/dsse.js.map +1 -1
  34. package/dist/tlog/hashedrekord.js +1 -1
  35. package/dist/tlog/hashedrekord.js.map +1 -1
  36. package/dist/tlog/intoto.js +2 -2
  37. package/dist/tlog/intoto.js.map +1 -1
  38. package/dist/tlog/merkle.js +4 -4
  39. package/dist/tlog/merkle.js.map +1 -1
  40. package/dist/trust/tuf.d.ts +1 -1
  41. package/dist/trust/tuf.d.ts.map +1 -1
  42. package/dist/trust/tuf.js +4 -4
  43. package/dist/trust/tuf.js.map +1 -1
  44. package/dist/x509/cert.d.ts +1 -1
  45. package/dist/x509/cert.d.ts.map +1 -1
  46. package/dist/x509/cert.js +4 -7
  47. package/dist/x509/cert.js.map +1 -1
  48. package/dist/x509/chain.d.ts.map +1 -1
  49. package/dist/x509/chain.js +16 -5
  50. package/dist/x509/chain.js.map +1 -1
  51. package/dist/x509/ext.d.ts +1 -1
  52. package/dist/x509/ext.d.ts.map +1 -1
  53. package/dist/x509/ext.js +17 -3
  54. package/dist/x509/ext.js.map +1 -1
  55. package/dist/x509/sct.d.ts.map +1 -1
  56. package/dist/x509/sct.js +2 -4
  57. package/dist/x509/sct.js.map +1 -1
  58. package/package.json +8 -4
  59. package/src/cli.ts +6 -1
  60. package/src/index.ts +3 -0
  61. package/src/interfaces.ts +2 -17
  62. package/src/rfc3161/timestamp.ts +3 -5
  63. package/src/rfc3161/tstinfo.ts +3 -5
  64. package/src/sigstore.ts +17 -16
  65. package/src/timestamp/tsa.ts +5 -6
  66. package/src/tlog/body.ts +1 -1
  67. package/src/tlog/checkpoint.ts +13 -7
  68. package/src/tlog/dsse.ts +3 -3
  69. package/src/tlog/hashedrekord.ts +1 -1
  70. package/src/tlog/intoto.ts +2 -2
  71. package/src/tlog/merkle.ts +4 -4
  72. package/src/trust/tuf.ts +6 -6
  73. package/src/x509/cert.ts +11 -7
  74. package/src/x509/chain.ts +6 -6
  75. package/src/x509/ext.ts +2 -4
  76. package/src/x509/sct.ts +2 -4
  77. package/vite.config.playwright.ts +4 -16
  78. package/dist/asn1/error.d.ts +0 -5
  79. package/dist/asn1/error.d.ts.map +0 -1
  80. package/dist/asn1/error.js +0 -20
  81. package/dist/asn1/error.js.map +0 -1
  82. package/dist/asn1/index.d.ts +0 -2
  83. package/dist/asn1/index.d.ts.map +0 -1
  84. package/dist/asn1/index.js +0 -17
  85. package/dist/asn1/index.js.map +0 -1
  86. package/dist/asn1/length.d.ts +0 -4
  87. package/dist/asn1/length.d.ts.map +0 -1
  88. package/dist/asn1/length.js +0 -59
  89. package/dist/asn1/length.js.map +0 -1
  90. package/dist/asn1/obj.d.ts +0 -15
  91. package/dist/asn1/obj.d.ts.map +0 -1
  92. package/dist/asn1/obj.js +0 -153
  93. package/dist/asn1/obj.js.map +0 -1
  94. package/dist/asn1/parse.d.ts +0 -7
  95. package/dist/asn1/parse.d.ts.map +0 -1
  96. package/dist/asn1/parse.js +0 -118
  97. package/dist/asn1/parse.js.map +0 -1
  98. package/dist/asn1/tag.d.ts +0 -17
  99. package/dist/asn1/tag.d.ts.map +0 -1
  100. package/dist/asn1/tag.js +0 -86
  101. package/dist/asn1/tag.js.map +0 -1
  102. package/dist/canonicalize.d.ts +0 -2
  103. package/dist/canonicalize.d.ts.map +0 -1
  104. package/dist/canonicalize.js +0 -64
  105. package/dist/canonicalize.js.map +0 -1
  106. package/dist/crypto.d.ts +0 -5
  107. package/dist/crypto.d.ts.map +0 -1
  108. package/dist/crypto.js +0 -260
  109. package/dist/crypto.js.map +0 -1
  110. package/dist/encoding.d.ts +0 -17
  111. package/dist/encoding.d.ts.map +0 -1
  112. package/dist/encoding.js +0 -112
  113. package/dist/encoding.js.map +0 -1
  114. package/dist/pem.d.ts +0 -3
  115. package/dist/pem.d.ts.map +0 -1
  116. package/dist/pem.js +0 -41
  117. package/dist/pem.js.map +0 -1
  118. package/dist/stream.d.ts +0 -22
  119. package/dist/stream.d.ts.map +0 -1
  120. package/dist/stream.js +0 -114
  121. package/dist/stream.js.map +0 -1
  122. package/src/asn1/error.ts +0 -18
  123. package/src/asn1/index.ts +0 -16
  124. package/src/asn1/length.ts +0 -71
  125. package/src/asn1/obj.ts +0 -184
  126. package/src/asn1/parse.ts +0 -146
  127. package/src/asn1/tag.ts +0 -105
  128. package/src/canonicalize.ts +0 -61
  129. package/src/crypto.ts +0 -310
  130. package/src/encoding.ts +0 -141
  131. package/src/pem.ts +0 -48
  132. package/src/stream.ts +0 -141
  133. package/src/types/tuf-browser.d.ts +0 -13
package/src/crypto.ts DELETED
@@ -1,310 +0,0 @@
1
- import { ASN1Obj } from "./asn1/index.js";
2
- import {
3
- base64ToUint8Array,
4
- base64UrlToUint8Array,
5
- hexToUint8Array,
6
- toArrayBuffer,
7
- Uint8ArrayToHex,
8
- } from "./encoding.js";
9
- import { EcdsaTypes, HashAlgorithms, KeyTypes } from "./interfaces.js";
10
- import { toDER } from "./pem.js";
11
- import { p256, p384, p521 } from "@noble/curves/nist.js";
12
-
13
- function pkcs1ToSpki(pkcs1Bytes: Uint8Array): Uint8Array {
14
- const algorithmIdentifier = new Uint8Array([
15
- 0x30, 0x0d,
16
- 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01,
17
- 0x05, 0x00
18
- ]);
19
-
20
- const bitStringLength = pkcs1Bytes.length + 1;
21
- const totalContentLength = algorithmIdentifier.length + 1 + lengthBytes(bitStringLength).length + bitStringLength;
22
-
23
- const result = new Uint8Array(1 + lengthBytes(totalContentLength).length + totalContentLength);
24
- let offset = 0;
25
-
26
- result[offset++] = 0x30;
27
- const totalLengthBytes = lengthBytes(totalContentLength);
28
- result.set(totalLengthBytes, offset);
29
- offset += totalLengthBytes.length;
30
-
31
- result.set(algorithmIdentifier, offset);
32
- offset += algorithmIdentifier.length;
33
-
34
- result[offset++] = 0x03;
35
- const bitStringLengthBytes = lengthBytes(bitStringLength);
36
- result.set(bitStringLengthBytes, offset);
37
- offset += bitStringLengthBytes.length;
38
- result[offset++] = 0x00;
39
- result.set(pkcs1Bytes, offset);
40
-
41
- return result;
42
- }
43
-
44
- // Encodes a length value in DER (Distinguished Encoding Rules) format.
45
- // DER length encoding rules:
46
- // - Lengths 0-127: single byte containing the length
47
- // - Lengths 128-255: 0x81 followed by one byte containing the length
48
- // - Lengths 256-65535: 0x82 followed by two bytes containing the length (big-endian)
49
- // Used by pkcs1ToSpki() to construct valid ASN.1/DER structures.
50
- function lengthBytes(length: number): Uint8Array {
51
- if (length < 128) {
52
- return new Uint8Array([length]);
53
- } else if (length < 256) {
54
- return new Uint8Array([0x81, length]);
55
- } else {
56
- return new Uint8Array([0x82, (length >> 8) & 0xff, length & 0xff]);
57
- }
58
- }
59
-
60
- // Imports cryptographic public keys into the Web Crypto API format (CryptoKey).
61
- // This function is specific to the browser implementation and doesn't exist in sigstore-js.
62
- //
63
- // Why needed for browser:
64
- // - sigstore-js uses Node.js crypto.createPublicKey() which handles many key formats automatically
65
- // - Browsers only have Web Crypto API (crypto.subtle.importKey) which requires explicit format/algorithm
66
- // - This function bridges the gap by:
67
- // 1. Detecting key format (PEM, hex, base64, PKCS#1, SPKI)
68
- // 2. Converting PKCS#1 RSA keys to SPKI (Web Crypto only supports SPKI for RSA)
69
- // 3. Mapping Sigstore key types/schemes to Web Crypto algorithm parameters
70
- //
71
- // Supports:
72
- // - ECDSA (P-256, P-384, P-521) - Used by Fulcio, CT logs
73
- // - Ed25519 - Used by Rekor checkpoints, some CT logs
74
- // - RSA (PKCS#1, PSS) - Used by older CT logs and some Rekor instances
75
- //
76
- // Key format detection:
77
- // - PEM format (contains "BEGIN"): Parse with toDER()
78
- // - Hex string (all hex chars): Import as raw
79
- // - Base64 PKCS#1 (starts with 0x30 0x82...): Convert to SPKI
80
- // - Base64 SPKI: Import directly
81
- export async function importKey(
82
- keytype: string,
83
- scheme: string,
84
- key: string,
85
- ): Promise<CryptoKey> {
86
- class importParams {
87
- format: "raw" | "spki" = "spki";
88
- keyData: ArrayBuffer = new ArrayBuffer(0);
89
- algorithm: RsaHashedImportParams | EcKeyImportParams | Algorithm = { name: "ECDSA" };
90
- extractable: boolean = true;
91
- usage: Array<KeyUsage> = ["verify"];
92
- }
93
-
94
- const params = new importParams();
95
- if (key.includes("BEGIN")) {
96
- params.format = "spki";
97
- params.keyData = toArrayBuffer(toDER(key));
98
- } else if (/^[0-9A-Fa-f]+$/.test(key)) {
99
- params.format = "raw";
100
- params.keyData = toArrayBuffer(hexToUint8Array(key));
101
- } else {
102
- params.format = "spki";
103
- const keyBytes = base64ToUint8Array(key);
104
-
105
- if (keytype.toLowerCase().includes("pkcs1") &&
106
- keyBytes[0] === 0x30 && keyBytes[1] === 0x82 &&
107
- keyBytes[4] === 0x02 && keyBytes[5] === 0x82) {
108
- params.keyData = toArrayBuffer(pkcs1ToSpki(keyBytes));
109
- } else {
110
- params.keyData = toArrayBuffer(keyBytes);
111
- }
112
- }
113
-
114
- if (keytype.toLowerCase().includes("ecdsa")) {
115
- if (scheme.includes("256")) {
116
- params.algorithm = { name: "ECDSA", namedCurve: EcdsaTypes.P256 };
117
- } else if (scheme.includes("384")) {
118
- params.algorithm = { name: "ECDSA", namedCurve: EcdsaTypes.P384 };
119
- } else if (scheme.includes("521")) {
120
- params.algorithm = { name: "ECDSA", namedCurve: EcdsaTypes.P521 };
121
- } else {
122
- throw new Error("Cannot determine ECDSA key size.");
123
- }
124
- } else if (keytype.toLowerCase().includes("ed25519")) {
125
- params.algorithm = { name: "Ed25519" };
126
- } else if (keytype.toLowerCase().includes("rsa") || keytype.toLowerCase().includes("pkcs1")) {
127
- let hashName = HashAlgorithms.SHA256;
128
- // Normalize scheme to handle various formats: SHA256, SHA_256, SHA-256, etc.
129
- const normalizedScheme = scheme.toUpperCase().replace(/[-_]/g, "");
130
- if (normalizedScheme.includes("SHA256") || normalizedScheme.includes("256")) {
131
- hashName = HashAlgorithms.SHA256;
132
- } else if (normalizedScheme.includes("SHA384") || normalizedScheme.includes("384")) {
133
- hashName = HashAlgorithms.SHA384;
134
- } else if (normalizedScheme.includes("SHA512") || normalizedScheme.includes("512")) {
135
- hashName = HashAlgorithms.SHA512;
136
- }
137
-
138
- if (scheme.includes("PKCS1") || scheme.includes("RSA_PKCS1")) {
139
- params.algorithm = {
140
- name: "RSASSA-PKCS1-v1_5",
141
- hash: { name: hashName },
142
- };
143
- } else {
144
- params.algorithm = {
145
- name: "RSA-PSS",
146
- hash: { name: hashName },
147
- };
148
- }
149
- } else {
150
- throw new Error(`Unsupported ${keytype}`);
151
- }
152
-
153
- return await crypto.subtle.importKey(
154
- params.format,
155
- params.keyData,
156
- params.algorithm,
157
- params.extractable,
158
- params.usage,
159
- );
160
- }
161
-
162
- export async function verifySignature(
163
- key: CryptoKey,
164
- signed: Uint8Array,
165
- sig: Uint8Array,
166
- hash: string = "sha256",
167
- ): Promise<boolean> {
168
- const options: {
169
- name: string;
170
- hash?: {
171
- name: string;
172
- };
173
- } = {
174
- name: key.algorithm.name,
175
- };
176
-
177
- if (key.algorithm.name === KeyTypes.Ecdsa) {
178
- const namedCurve = (key.algorithm as EcKeyAlgorithm).namedCurve;
179
- let sig_size = 32;
180
-
181
- if (namedCurve === EcdsaTypes.P256) {
182
- sig_size = 32;
183
- } else if (namedCurve === EcdsaTypes.P384) {
184
- sig_size = 48;
185
- } else if (namedCurve === EcdsaTypes.P521) {
186
- sig_size = 66;
187
- }
188
-
189
- options.hash = { name: "" };
190
- if (hash.includes("256")) {
191
- options.hash.name = HashAlgorithms.SHA256;
192
- } else if (hash.includes("384")) {
193
- options.hash.name = HashAlgorithms.SHA384;
194
- } else if (hash.includes("512")) {
195
- options.hash.name = HashAlgorithms.SHA512;
196
- } else {
197
- throw new Error("Cannot determine hashing algorithm;");
198
- }
199
-
200
- let raw_signature: Uint8Array;
201
- try {
202
- const asn1_sig = ASN1Obj.parseBuffer(sig);
203
- const r = asn1_sig.subs[0].toInteger();
204
- const s = asn1_sig.subs[1].toInteger();
205
- const binr = hexToUint8Array(r.toString(16).padStart(sig_size * 2, "0"));
206
- const bins = hexToUint8Array(s.toString(16).padStart(sig_size * 2, "0"));
207
- raw_signature = new Uint8Array(binr.length + bins.length);
208
- raw_signature.set(binr, 0);
209
- raw_signature.set(bins, binr.length);
210
- } catch {
211
- return false;
212
- }
213
-
214
- return await crypto.subtle.verify(
215
- options,
216
- key,
217
- toArrayBuffer(raw_signature),
218
- toArrayBuffer(signed),
219
- );
220
- } else if (key.algorithm.name === KeyTypes.Ed25519) {
221
- // Ed25519 uses raw signatures (not DER-encoded like ECDSA)
222
- // Fulcio supports Ed25519, ECDSA, and RSA_PSS for signing certificates
223
- // Ed25519 is also used for checkpoint signatures in TLog configurations
224
- return await crypto.subtle.verify(
225
- key.algorithm.name,
226
- key,
227
- toArrayBuffer(sig),
228
- toArrayBuffer(signed),
229
- );
230
- } else if (key.algorithm.name === "RSA-PSS") {
231
- // Salt length must match the hash output size
232
- const hashAlg = (key.algorithm as RsaHashedKeyAlgorithm).hash.name;
233
- const saltLength = hashAlg === HashAlgorithms.SHA256 ? 32 :
234
- hashAlg === HashAlgorithms.SHA384 ? 48 :
235
- hashAlg === HashAlgorithms.SHA512 ? 64 : 32;
236
- return await crypto.subtle.verify(
237
- {
238
- name: "RSA-PSS",
239
- saltLength: saltLength,
240
- },
241
- key,
242
- toArrayBuffer(sig),
243
- toArrayBuffer(signed),
244
- );
245
- } else if (key.algorithm.name === "RSASSA-PKCS1-v1_5") {
246
- return await crypto.subtle.verify(
247
- key.algorithm.name,
248
- key,
249
- toArrayBuffer(sig),
250
- toArrayBuffer(signed),
251
- );
252
- } else {
253
- throw new Error("Unsupported key type!");
254
- }
255
- }
256
-
257
- // Verifies an ECDSA signature over a pre-computed digest.
258
- // WebCrypto's verify() always hashes the input first, so passing a digest would
259
- // result in double-hashing. We use @noble/curves for low-level ECDSA verification,
260
- // adapted from the same workaround used in sigstore-js's conformance CLI.
261
- // See: https://github.com/sigstore/sigstore-js/blob/main/packages/conformance/src/commands/verify-bundle.ts#L111
262
- export async function verifySignatureOverDigest(
263
- key: CryptoKey,
264
- digest: Uint8Array,
265
- sig: Uint8Array,
266
- ): Promise<boolean> {
267
- if (key.algorithm.name !== KeyTypes.Ecdsa) {
268
- throw new Error("verifySignatureOverDigest only supports ECDSA keys");
269
- }
270
-
271
- const namedCurve = (key.algorithm as EcKeyAlgorithm).namedCurve;
272
- let curve: typeof p256 | typeof p384 | typeof p521;
273
-
274
- if (namedCurve === EcdsaTypes.P256) {
275
- curve = p256;
276
- } else if (namedCurve === EcdsaTypes.P384) {
277
- curve = p384;
278
- } else if (namedCurve === EcdsaTypes.P521) {
279
- curve = p521;
280
- } else {
281
- throw new Error(`Unsupported curve: ${namedCurve}`);
282
- }
283
-
284
- // Export the public key to get x and y coordinates
285
- const jwk = await crypto.subtle.exportKey("jwk", key);
286
- if (!jwk.x || !jwk.y) {
287
- throw new Error("Invalid ECDSA public key: missing x or y coordinates");
288
- }
289
-
290
- // Convert base64url JWK coordinates to bytes
291
- const x = base64UrlToUint8Array(jwk.x);
292
- const y = base64UrlToUint8Array(jwk.y);
293
-
294
- // Combine x and y into uncompressed public key format (0x04 || x || y)
295
- const publicKey = new Uint8Array(1 + x.length + y.length);
296
- publicKey[0] = 0x04;
297
- publicKey.set(x, 1);
298
- publicKey.set(y, 1 + x.length);
299
-
300
- // Verify the DER-encoded signature over the digest
301
- // @noble/curves can parse DER signatures directly with format: 'der'
302
- // Options:
303
- // - format: 'der' to parse DER-encoded signatures
304
- // - prehash: false because we're passing a pre-computed digest, not the original message
305
- // - lowS: false to accept both high-S and low-S signatures (matches elliptic.js behavior)
306
- // elliptic.js accepts malleable signatures by default, while @noble/curves rejects them by default
307
- return curve.verify(sig, digest, publicKey, { format: 'der', prehash: false, lowS: false });
308
- }
309
-
310
- export { uint8ArrayEqual as bufferEqual } from "./encoding.js";
package/src/encoding.ts DELETED
@@ -1,141 +0,0 @@
1
- /*
2
- Copyright 2023 The Sigstore Authors.
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
- export function toArrayBuffer(view: ArrayBufferView): ArrayBuffer {
17
- const { byteLength } = view;
18
-
19
- if (byteLength === 0) {
20
- return new ArrayBuffer(0);
21
- }
22
-
23
- const { buffer, byteOffset } = view;
24
-
25
- if (
26
- buffer instanceof ArrayBuffer &&
27
- byteOffset === 0 &&
28
- byteLength === buffer.byteLength
29
- ) {
30
- return buffer;
31
- }
32
-
33
- const clone = new Uint8Array(byteLength);
34
- clone.set(new Uint8Array(buffer, byteOffset, byteLength));
35
- return clone.buffer;
36
- }
37
-
38
- export function base64ToUint8Array(base64: string): Uint8Array {
39
- const binaryString = atob(base64);
40
- const length = binaryString.length;
41
- const bytes = new Uint8Array(length);
42
-
43
- for (let i = 0; i < length; i++) {
44
- bytes[i] = binaryString.charCodeAt(i);
45
- }
46
-
47
- return bytes;
48
- }
49
-
50
- export function base64UrlToUint8Array(base64url: string): Uint8Array {
51
- let base64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
52
- while (base64.length % 4 !== 0) {
53
- base64 += '=';
54
- }
55
- return base64ToUint8Array(base64);
56
- }
57
-
58
- export function Uint8ArrayToBase64(uint8Array: Uint8Array): string {
59
- let binaryString = "";
60
-
61
- for (let i = 0; i < uint8Array.length; i++) {
62
- binaryString += String.fromCharCode(uint8Array[i]);
63
- }
64
-
65
- return btoa(binaryString);
66
- }
67
-
68
- export function hexToUint8Array(hex: string): Uint8Array {
69
- if (hex.length % 2 !== 0) {
70
- throw new Error("Hex string must have an even length");
71
- }
72
-
73
- const length = hex.length / 2;
74
- const uint8Array = new Uint8Array(length);
75
-
76
- for (let i = 0; i < length; i++) {
77
- uint8Array[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
78
- }
79
-
80
- return uint8Array;
81
- }
82
-
83
- export function Uint8ArrayToHex(uint8Array: Uint8Array): string {
84
- let hexString = "";
85
-
86
- for (let i = 0; i < uint8Array.length; i++) {
87
- let hex = uint8Array[i].toString(16);
88
- if (hex.length === 1) {
89
- hex = "0" + hex;
90
- }
91
- hexString += hex;
92
- }
93
-
94
- return hexString;
95
- }
96
-
97
- export function stringToUint8Array(str: string): Uint8Array {
98
- // Defaults to utf-8, but utf-8 is ascii compatible
99
- const encoder = new TextEncoder();
100
- return encoder.encode(str);
101
- }
102
-
103
- export function Uint8ArrayToString(uint8Array: Uint8Array): string {
104
- const decoder = new TextDecoder("utf-8");
105
- return decoder.decode(uint8Array);
106
- }
107
-
108
- export function readBigInt64BE(
109
- uint8Array: Uint8Array,
110
- offset?: number,
111
- ): bigint {
112
- if (offset === undefined) {
113
- offset = 0;
114
- }
115
- const hex = Uint8ArrayToHex(uint8Array.slice(offset, offset + 8));
116
- return BigInt(`0x${hex}`);
117
- }
118
-
119
- export function base64Encode(str: string): string {
120
- return Uint8ArrayToBase64(stringToUint8Array(str));
121
- }
122
-
123
- export function base64Decode(str: string): string {
124
- return Uint8ArrayToString(base64ToUint8Array(str));
125
- }
126
-
127
- /**
128
- * Constant-time equality comparison for Uint8Arrays
129
- * Prevents timing attacks by always comparing all bytes
130
- */
131
- export function uint8ArrayEqual(a: Uint8Array, b: Uint8Array): boolean {
132
- if (a.byteLength !== b.byteLength) {
133
- return false;
134
- }
135
-
136
- let result = 0;
137
- for (let i = 0; i < a.byteLength; i++) {
138
- result |= a[i] ^ b[i];
139
- }
140
- return result === 0;
141
- }
package/src/pem.ts DELETED
@@ -1,48 +0,0 @@
1
- /*
2
- Copyright 2023 The Sigstore Authors.
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
-
17
- import { base64ToUint8Array, Uint8ArrayToBase64 } from "./encoding.js";
18
-
19
- const PEM_HEADER = /-----BEGIN (.*)-----/;
20
- const PEM_FOOTER = /-----END (.*)-----/;
21
-
22
- export function toDER(certificate: string): Uint8Array {
23
- let der = "";
24
-
25
- certificate.split("\n").forEach((line) => {
26
- if (line.match(PEM_HEADER) || line.match(PEM_FOOTER)) {
27
- return;
28
- }
29
-
30
- der += line;
31
- });
32
-
33
- return base64ToUint8Array(der);
34
- }
35
-
36
- // Translates a DER-encoded buffer into a PEM-encoded string. Standard PEM
37
- // encoding dictates that each certificate should have a trailing newline after
38
- // the footer.
39
- export function fromDER(certificate: Uint8Array, type = "CERTIFICATE"): string {
40
- // Base64-encode the certificate.
41
- const der = Uint8ArrayToBase64(certificate);
42
- // Split the certificate into lines of 64 characters.
43
- const lines = der.match(/.{1,64}/g) || "";
44
-
45
- return [`-----BEGIN ${type}-----`, ...lines, `-----END ${type}-----`]
46
- .join("\n")
47
- .concat("\n");
48
- }
package/src/stream.ts DELETED
@@ -1,141 +0,0 @@
1
- /*
2
- Copyright 2023 The Sigstore Authors.
3
-
4
- Licensed under the Apache License, Version 2.0 (the "License");
5
- you may not use this file except in compliance with the License.
6
- You may obtain a copy of the License at
7
-
8
- http://www.apache.org/licenses/LICENSE-2.0
9
-
10
- Unless required by applicable law or agreed to in writing, software
11
- distributed under the License is distributed on an "AS IS" BASIS,
12
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- See the License for the specific language governing permissions and
14
- limitations under the License.
15
- */
16
- class StreamError extends Error {}
17
-
18
- export class ByteStream {
19
- private static BLOCK_SIZE = 1024;
20
-
21
- private buf: ArrayBuffer;
22
- private view: Uint8Array;
23
- private start = 0;
24
-
25
- constructor(buffer?: ArrayBuffer) {
26
- if (buffer) {
27
- this.buf = buffer;
28
- this.view = new Uint8Array(buffer);
29
- } else {
30
- this.buf = new ArrayBuffer(0);
31
- this.view = new Uint8Array(this.buf);
32
- }
33
- }
34
-
35
- get buffer(): Uint8Array {
36
- return this.view.subarray(0, this.start);
37
- }
38
-
39
- get length(): number {
40
- return this.view.byteLength;
41
- }
42
-
43
- get position(): number {
44
- return this.start;
45
- }
46
-
47
- public seek(position: number) {
48
- this.start = position;
49
- }
50
-
51
- // Returns a Buffer containing the specified number of bytes starting at the
52
- // given start position.
53
- public slice(start: number, len: number): Uint8Array {
54
- const end = start + len;
55
-
56
- if (end > this.length) {
57
- throw new StreamError("request past end of buffer");
58
- }
59
-
60
- return this.view.subarray(start, end);
61
- }
62
-
63
- public appendChar(char: number) {
64
- this.ensureCapacity(1);
65
- this.view[this.start] = char;
66
- this.start += 1;
67
- }
68
-
69
- public appendUint16(num: number) {
70
- this.ensureCapacity(2);
71
-
72
- const value = new Uint16Array([num]);
73
- const view = new Uint8Array(value.buffer);
74
-
75
- this.view[this.start] = view[1];
76
- this.view[this.start + 1] = view[0];
77
- this.start += 2;
78
- }
79
-
80
- public appendUint24(num: number) {
81
- this.ensureCapacity(3);
82
-
83
- const value = new Uint32Array([num]);
84
- const view = new Uint8Array(value.buffer);
85
-
86
- this.view[this.start] = view[2];
87
- this.view[this.start + 1] = view[1];
88
- this.view[this.start + 2] = view[0];
89
- this.start += 3;
90
- }
91
-
92
- public appendView(view: Uint8Array) {
93
- this.ensureCapacity(view.length);
94
- this.view.set(view, this.start);
95
- this.start += view.length;
96
- }
97
-
98
- public getBlock(size: number): Uint8Array {
99
- if (size <= 0) {
100
- return new Uint8Array(0);
101
- }
102
-
103
- if (this.start + size > this.view.length) {
104
- throw new Error("request past end of buffer");
105
- }
106
-
107
- const result = this.view.subarray(this.start, this.start + size);
108
- this.start += size;
109
-
110
- return result;
111
- }
112
-
113
- public getUint8(): number {
114
- return this.getBlock(1)[0];
115
- }
116
-
117
- public getUint16(): number {
118
- const block = this.getBlock(2);
119
-
120
- return (block[0] << 8) | block[1];
121
- }
122
-
123
- private ensureCapacity(size: number) {
124
- if (this.start + size > this.view.byteLength) {
125
- const blockSize =
126
- ByteStream.BLOCK_SIZE + (size > ByteStream.BLOCK_SIZE ? size : 0);
127
- this.realloc(this.view.byteLength + blockSize);
128
- }
129
- }
130
-
131
- private realloc(size: number) {
132
- const newArray = new ArrayBuffer(size);
133
- const newView = new Uint8Array(newArray);
134
-
135
- // Copy the old buffer into the new one
136
- newView.set(this.view);
137
-
138
- this.buf = newArray;
139
- this.view = newView;
140
- }
141
- }
@@ -1,13 +0,0 @@
1
- declare module 'tuf-browser/dist/tuf.js' {
2
- export class TUFClient {
3
- constructor(
4
- repositoryUrl: string,
5
- startingRoot: string,
6
- namespace: string,
7
- targetBaseUrl?: string
8
- );
9
-
10
- updateTUF(): Promise<void>;
11
- getTarget(targetPath: string): Promise<Uint8Array>;
12
- }
13
- }