@witbitz/sdk 0.0.0-stage → 4.3.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 +20 -2
- package/a2aCore.js +200 -0
- package/a2aShared.js +27 -0
- package/accountErase.js +223 -0
- package/accountIndexDoc.js +122 -0
- package/accountOwner.js +59 -0
- package/accountRecord.js +90 -0
- package/accountScope.js +114 -0
- package/accountSeal.js +42 -0
- package/accountStores.js +173 -0
- package/acctV2App.js +619 -0
- package/acctV2Check.js +29 -0
- package/acctV2Client.js +382 -0
- package/acctV2Keys.js +43 -0
- package/acctV2Legacy.js +14 -0
- package/acctV2Switch.js +80 -0
- package/acctV2UiCore.js +857 -0
- package/acctV2WebAuthn.js +61 -0
- package/acctV2Wire.js +59 -0
- package/address.js +42 -0
- package/attestedBox.js +30 -0
- package/attestedRooms.js +78 -0
- package/awsKey.js +98 -0
- package/backupRecord.js +106 -0
- package/backupVault.js +97 -0
- package/blocks.js +115 -0
- package/blocksPush.js +64 -0
- package/browseSeal.js +103 -0
- package/cbor.js +225 -0
- package/changePush.js +75 -0
- package/chatLock.js +149 -0
- package/chatLockSw.js +13 -0
- package/cloudDeviceBuilds.js +11 -0
- package/cloudDevicePins.js +33 -0
- package/cloudDeviceSeal.js +53 -0
- package/codeBrowse.js +224 -0
- package/codeCloud.js +652 -0
- package/codeCloudKeys.js +112 -0
- package/codeCommands.js +167 -0
- package/codeComputers.js +388 -0
- package/codeEgress.js +123 -0
- package/codeFilesRoutes.js +17 -0
- package/codeMkdir.js +8 -0
- package/codeModes.js +57 -0
- package/codeOrphanKeys.js +42 -0
- package/codeOutputs.js +141 -0
- package/codeRelay.js +505 -0
- package/codeSentinel.js +86 -0
- package/codeTransport.js +295 -0
- package/codeUndo.js +48 -0
- package/compress.js +24 -0
- package/config.js +11 -0
- package/connCatalog.js +164 -0
- package/connDisp.js +76 -0
- package/connOtherWays.js +74 -0
- package/connPending.js +16 -0
- package/connWrap.js +53 -0
- package/connectionEntry.js +89 -0
- package/connections.js +562 -0
- package/credits.js +30 -0
- package/csAttest.js +112 -0
- package/decrypt.html +384 -0
- package/deviceAttest.js +521 -0
- package/deviceCall.js +54 -0
- package/deviceKeyCore.js +41 -0
- package/deviceSeal.js +59 -0
- package/e2ee.js +323 -0
- package/eat.js +103 -0
- package/emailFork.js +275 -0
- package/emailSignIn.js +81 -0
- package/emptySpaces.js +111 -0
- package/encSource.js +55 -0
- package/enclaveAccept.js +214 -0
- package/enclaveClaim.js +241 -0
- package/enclaveRoute.js +102 -0
- package/enclaveSnapshotMerge.js +44 -0
- package/entrySig.js +165 -0
- package/env.js +73 -0
- package/eraseFile.js +58 -0
- package/followImage.js +86 -0
- package/healNames.js +45 -0
- package/idb.js +97 -0
- package/idbSeal.js +119 -0
- package/indexGone.js +12 -0
- package/invite.js +51 -0
- package/inviteEnroll.js +67 -0
- package/inviteSend.js +68 -0
- package/jobAgents.js +87 -0
- package/keptMerge.js +84 -0
- package/l1-worker-src.js +2 -0
- package/l1-worker.js +831 -0
- package/laneConfig.js +57 -0
- package/laneLinkCore.js +177 -0
- package/ledgerView.js +64 -0
- package/manifest.json +220 -0
- package/mcpRecommended.js +25 -0
- package/memberChange.js +123 -0
- package/mkseal.js +101 -0
- package/nativePush.js +153 -0
- package/nitroVerify.js +220 -0
- package/noble-p256.js +27 -0
- package/notice.js +116 -0
- package/notifKey.js +58 -0
- package/offlineBundle.js +62 -0
- package/ohttp.js +177 -0
- package/opencodeWire.js +282 -0
- package/oprf.js +70 -0
- package/ownAgentLink.js +96 -0
- package/ownerPolicy.js +117 -0
- package/package.json +254 -4
- package/pollVerdict.js +33 -0
- package/prefetchPoll.js +60 -0
- package/protectedLocal.js +410 -0
- package/pushCore.js +42 -0
- package/recovery.js +107 -0
- package/regions.js +42 -0
- package/removedMessage.js +11 -0
- package/renameRefusal.js +9 -0
- package/replyQuote.js +34 -0
- package/reqEnvelope.js +126 -0
- package/roomDefaultPrefs.js +76 -0
- package/roomDelete.js +109 -0
- package/roomName.js +177 -0
- package/roomPass.js +256 -0
- package/roomRoster.js +289 -0
- package/roomSettings.js +61 -0
- package/roomState.js +58 -0
- package/roomStreamClient.js +115 -0
- package/rosterCount.js +49 -0
- package/rosterHash.js +13 -0
- package/rowSchema.js +113 -0
- package/safeMedia.js +116 -0
- package/safeMediaPins.js +17 -0
- package/schedulesDoc.js +78 -0
- package/sdkA2A.js +404 -0
- package/sdkAccount.js +470 -0
- package/sdkAccountDoc.js +81 -0
- package/sdkAccountSections.js +223 -0
- package/sdkAgui.js +141 -0
- package/sdkAttest.js +34 -0
- package/sdkBackup.js +480 -0
- package/sdkBackupV2.js +630 -0
- package/sdkBrowserPrivate.js +28 -0
- package/sdkBuilds.js +208 -0
- package/sdkCloudDevice.js +215 -0
- package/sdkComputers.js +480 -0
- package/sdkDictation.js +142 -0
- package/sdkErase.js +283 -0
- package/sdkExports.js +69 -0
- package/sdkInvitations.js +34 -0
- package/sdkKeptAgent.js +188 -0
- package/sdkLaneLink.js +205 -0
- package/sdkPlan.js +94 -0
- package/sdkProtect.js +228 -0
- package/sdkRedact.js +36 -0
- package/sdkRekey.js +307 -0
- package/sdkReport.js +68 -0
- package/sdkSafeMedia.js +255 -0
- package/sdkSchedules.js +39 -0
- package/sdkSpeech.js +54 -0
- package/sdkTurn.js +117 -0
- package/sdkVault.js +124 -0
- package/sealShare.js +18 -0
- package/sealedNames.js +64 -0
- package/sealedReceipt.js +17 -0
- package/sentinelRelease.js +154 -0
- package/shamir.js +85 -0
- package/sharedViews.js +38 -0
- package/sheetChrome.js +450 -0
- package/siri.js +171 -0
- package/spaceClient.js +1418 -0
- package/spaceConfig.js +81 -0
- package/spaceEmailGate.js +813 -0
- package/spaceExport.js +125 -0
- package/spaceFork.js +216 -0
- package/spaceLock.js +358 -0
- package/spacePasskey.js +180 -0
- package/speechOut.js +100 -0
- package/splitKeyClient.js +90 -0
- package/store.js +587 -0
- package/streamHint.js +36 -0
- package/sttCloud.js +220 -0
- package/sttWorklet.js +38 -0
- package/sw-notif-core.js +538 -0
- package/tempList.js +48 -0
- package/tinfoilVerifier.js +78 -0
- package/toolBooks.js +31 -0
- package/toolRules.js +52 -0
- package/turnCerts.js +254 -0
- package/turnFeed.js +58 -0
- package/turnVerdict.js +36 -0
- package/usageReceipts.js +176 -0
- package/usageSummary.js +90 -0
- package/util.js +181 -0
- package/vault-core.js +1958 -0
- package/vault-kit.mjs +354 -0
- package/vault-screens.js +263 -0
- package/vaultAtRest.js +177 -0
- package/vaultCoreLock.js +111 -0
- package/vaultCorePush.js +140 -0
- package/vaultDeviceKey.js +95 -0
- package/vaultEnclaveHandoff.js +114 -0
- package/vaultKeep.js +281 -0
- package/vaultSealedStore.js +77 -0
- package/vaultWorkerHost.js +98 -0
- package/vcWorker.js +34 -0
- package/voiceAttest.js +50 -0
- package/voiceCert.js +51 -0
- package/witbitz-admin.d.ts +135 -0
- package/witbitz-admin.js +529 -0
- package/witbitz-admin.mjs +1459 -0
- package/witbitz-sdk.d.ts +1106 -0
- package/witbitz-sdk.js +3835 -0
- package/workerClient.js +16 -0
- package/workerLiveness.js +21 -0
- package/x509.js +218 -0
package/workerClient.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// workerClient.js — the Spaces app's room worker, as an SDK client (docs/sdk-convergence.md, phase 1).
|
|
2
|
+
//
|
|
3
|
+
// The SDK's Space calls `client.op(room, mk, …)`. The app confines mk to a worker (l1.js → l1-worker.js; in the tenant app
|
|
4
|
+
// the worker is hoisted into the vault), whose facade takes the SAME ops and arguments minus mk — the worker inserts its
|
|
5
|
+
// own. So the adapter only drops the mk argument, and says it is `keyless`: a Space over it never holds the key, and
|
|
6
|
+
// refuses what would need it in the page (see witbitz-sdk.js, KEYLESS).
|
|
7
|
+
//
|
|
8
|
+
// const space = new Space(workerClient(l1), { room, mk: null, poll: false })
|
|
9
|
+
export const WORKER_OPS = ['poll', 'pollSealed', 'turn', 'state', 'data', 'title', 'decide', 'pending', 'subscribe', 'unsubscribe',
|
|
10
|
+
'mirror', 'toolResult', 'getConfig', 'setConfig', 'blob', 'toolBook', 'toolRules', 'react', 'safeCopy', 'mark', 'leave', 'carry', 'purge', 'mailPurge', 'intakeReg', 'intakePush', 'intake', 'intakeAck', 'importChat', 'importWhatsApp']
|
|
11
|
+
|
|
12
|
+
export function workerClient (l1) {
|
|
13
|
+
const c = { keyless: true }
|
|
14
|
+
for (const op of WORKER_OPS) if (typeof l1[op] === 'function') c[op] = (room, _mk, ...rest) => l1[op](room, ...rest)
|
|
15
|
+
return c
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// workerLiveness.js — IS THE ROOM WORKER STILL THERE? iOS reclaims a Web Worker under memory pressure and leaves a dead
|
|
2
|
+
// handle that swallows every postMessage: rooms stop opening, sync stops, and nothing says why. Ping it; a missed reply
|
|
3
|
+
// within the grace means it is gone, and the owner restarts it. The vault page did this for its hosted worker first
|
|
4
|
+
// (vault-loader.js); the regular app's l1.js uses this module for its own (docs/convergence-review-2026-10-17.md).
|
|
5
|
+
//
|
|
6
|
+
// createLiveness({ ping(n), restart(), setTimeout, graceMs, isHidden }) → { tick(), pong(n) }
|
|
7
|
+
// tick() — send a ping (call it on an interval and when the page returns to the foreground); skipped while hidden
|
|
8
|
+
// pong(n) — the worker answered ping n
|
|
9
|
+
// One restart per silence: a restart counts every ping sent so far as answered, so older pings' timers do not fire again.
|
|
10
|
+
export function createLiveness ({ ping, restart, setTimeout: st = globalThis.setTimeout, graceMs = 5000, isHidden = () => false }) {
|
|
11
|
+
let sent = 0, answered = 0
|
|
12
|
+
return {
|
|
13
|
+
tick () {
|
|
14
|
+
if (isHidden()) return
|
|
15
|
+
const n = ++sent
|
|
16
|
+
try { ping(n) } catch { /* a dead handle may throw; the timer below decides */ }
|
|
17
|
+
st(() => { if (answered < n) { answered = sent; try { restart() } catch { /* the next tick tries again */ } } }, graceMs)
|
|
18
|
+
},
|
|
19
|
+
pong (n) { answered = Math.max(answered, n | 0) },
|
|
20
|
+
}
|
|
21
|
+
}
|
package/x509.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// spaces/public/x509.js — just enough DER/X.509 to verify an ECDSA certificate chain in a browser.
|
|
2
|
+
//
|
|
3
|
+
// ★ WHY THIS FILE EXISTS AT ALL. WebCrypto can verify a *signature*, but it cannot verify a *certificate chain* —
|
|
4
|
+
// there is no browser API for "is this cert signed by that one, and is it in date". So a page that wants to check an
|
|
5
|
+
// AWS Nitro attestation document has to walk the chain itself. That is the single biggest reason browser-side
|
|
6
|
+
// attestation verification gets waved at and never shipped, and it is why this was built before the enclave.
|
|
7
|
+
//
|
|
8
|
+
// SCOPE, deliberately narrow: ECDSA (P-256/P-384) certificates, which is all the Nitro PKI issues. No RSA, no name
|
|
9
|
+
// constraints, no CRL/OCSP, no policy processing, no wildcard/SAN matching (there is no hostname here to match).
|
|
10
|
+
// Issuer/subject are compared as raw DER bytes — stricter than RFC 5280 name matching, and the strictness is the point.
|
|
11
|
+
|
|
12
|
+
const TAG = { INT: 0x02, BITSTRING: 0x03, OID: 0x06, SEQ: 0x30, UTCTIME: 0x17, GENTIME: 0x18 }
|
|
13
|
+
|
|
14
|
+
// ecdsa-with-SHA256 / SHA384 / SHA512
|
|
15
|
+
const SIG_OIDS = new Map([
|
|
16
|
+
['1.2.840.10045.4.3.2', { hash: 'SHA-256' }],
|
|
17
|
+
['1.2.840.10045.4.3.3', { hash: 'SHA-384' }],
|
|
18
|
+
['1.2.840.10045.4.3.4', { hash: 'SHA-512' }],
|
|
19
|
+
])
|
|
20
|
+
// The curve OID lives in the SPKI; we read it so the caller can import the key with the right namedCurve.
|
|
21
|
+
const CURVE_OIDS = new Map([
|
|
22
|
+
['1.2.840.10045.3.1.7', { curve: 'P-256', size: 32 }],
|
|
23
|
+
['1.3.132.0.34', { curve: 'P-384', size: 48 }],
|
|
24
|
+
['1.3.132.0.35', { curve: 'P-521', size: 66 }],
|
|
25
|
+
])
|
|
26
|
+
|
|
27
|
+
/** Read one DER TLV at `off`. → { tag, start, contentStart, contentEnd } where [start, contentEnd) is the full element. */
|
|
28
|
+
function readTLV(b, off) {
|
|
29
|
+
if (off + 2 > b.length) throw new Error('der: truncated tag')
|
|
30
|
+
const tag = b[off]
|
|
31
|
+
if ((tag & 0x1f) === 0x1f) throw new Error('der: multi-byte tags not supported')
|
|
32
|
+
let p = off + 1
|
|
33
|
+
let len = b[p++]
|
|
34
|
+
if (len & 0x80) {
|
|
35
|
+
const n = len & 0x7f
|
|
36
|
+
if (n === 0) throw new Error('der: indefinite length is not valid DER')
|
|
37
|
+
if (n > 4) throw new Error('der: length too large')
|
|
38
|
+
if (p + n > b.length) throw new Error('der: truncated length')
|
|
39
|
+
len = 0
|
|
40
|
+
for (let i = 0; i < n; i++) len = len * 256 + b[p++]
|
|
41
|
+
}
|
|
42
|
+
const contentEnd = p + len
|
|
43
|
+
if (contentEnd > b.length) throw new Error('der: truncated content')
|
|
44
|
+
return { tag, start: off, contentStart: p, contentEnd }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function expect(b, off, tag, what) {
|
|
48
|
+
const t = readTLV(b, off)
|
|
49
|
+
if (t.tag !== tag) throw new Error(`der: expected ${what} (tag 0x${tag.toString(16)}), got 0x${t.tag.toString(16)}`)
|
|
50
|
+
return t
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Walk the direct children of a constructed element. */
|
|
54
|
+
function children(b, t) {
|
|
55
|
+
const out = []
|
|
56
|
+
let p = t.contentStart
|
|
57
|
+
while (p < t.contentEnd) { const c = readTLV(b, p); out.push(c); p = c.contentEnd }
|
|
58
|
+
return out
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function oidString(b, t) {
|
|
62
|
+
const c = b.subarray(t.contentStart, t.contentEnd)
|
|
63
|
+
if (!c.length) throw new Error('der: empty OID')
|
|
64
|
+
const parts = [Math.floor(c[0] / 40), c[0] % 40]
|
|
65
|
+
let v = 0
|
|
66
|
+
for (let i = 1; i < c.length; i++) {
|
|
67
|
+
// Each arc is base-128, high bit = "more bytes follow".
|
|
68
|
+
v = v * 128 + (c[i] & 0x7f)
|
|
69
|
+
if (!(c[i] & 0x80)) { parts.push(v); v = 0 }
|
|
70
|
+
}
|
|
71
|
+
return parts.join('.')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** DER times: UTCTime is 2-digit-year (the 2050 pivot per RFC 5280), GeneralizedTime is 4. */
|
|
75
|
+
function parseTime(b, t) {
|
|
76
|
+
const s = String.fromCharCode(...b.subarray(t.contentStart, t.contentEnd))
|
|
77
|
+
let m
|
|
78
|
+
if (t.tag === TAG.UTCTIME) {
|
|
79
|
+
m = /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(s)
|
|
80
|
+
if (!m) throw new Error('der: bad UTCTime')
|
|
81
|
+
const yy = Number(m[1])
|
|
82
|
+
return Date.UTC(yy >= 50 ? 1900 + yy : 2000 + yy, Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]))
|
|
83
|
+
}
|
|
84
|
+
if (t.tag === TAG.GENTIME) {
|
|
85
|
+
m = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})Z$/.exec(s)
|
|
86
|
+
if (!m) throw new Error('der: bad GeneralizedTime')
|
|
87
|
+
return Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]), Number(m[4]), Number(m[5]), Number(m[6]))
|
|
88
|
+
}
|
|
89
|
+
throw new Error('der: unsupported time tag')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** An ECDSA signature in X.509 is DER SEQUENCE{INTEGER r, INTEGER s}; WebCrypto wants raw r‖s, each left-padded to
|
|
93
|
+
* the curve size. (COSE, by contrast, already carries the raw form — hence this conversion lives here only.) */
|
|
94
|
+
export function derSigToRaw(der, size) {
|
|
95
|
+
const b = der instanceof Uint8Array ? der : new Uint8Array(der)
|
|
96
|
+
const seq = expect(b, 0, TAG.SEQ, 'signature SEQUENCE')
|
|
97
|
+
const [rT, sT] = children(b, seq)
|
|
98
|
+
if (!rT || !sT || rT.tag !== TAG.INT || sT.tag !== TAG.INT) throw new Error('der: bad ECDSA signature')
|
|
99
|
+
const out = new Uint8Array(size * 2)
|
|
100
|
+
for (const [i, t] of [[0, rT], [1, sT]]) {
|
|
101
|
+
let v = b.subarray(t.contentStart, t.contentEnd)
|
|
102
|
+
while (v.length > 1 && v[0] === 0x00) v = v.subarray(1) // strip the sign byte DER adds to keep INTEGERs positive
|
|
103
|
+
if (v.length > size) throw new Error('der: ECDSA integer too large for curve')
|
|
104
|
+
out.set(v, i * size + (size - v.length))
|
|
105
|
+
}
|
|
106
|
+
return out
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parse an X.509 certificate.
|
|
111
|
+
* → { tbs, sig, sigHash, spki, curve, size, issuer, subject, notBefore, notAfter, isCA }
|
|
112
|
+
* `tbs` is the raw DER of tbsCertificate INCLUDING its header — that is precisely the byte range a CA signs, and
|
|
113
|
+
* re-serialising it instead of slicing it is how verifiers acquire signature-validity bugs.
|
|
114
|
+
*/
|
|
115
|
+
export function parseCertificate(der) {
|
|
116
|
+
const b = der instanceof Uint8Array ? der : new Uint8Array(der)
|
|
117
|
+
const cert = expect(b, 0, TAG.SEQ, 'Certificate')
|
|
118
|
+
const [tbsT, algT, sigT] = children(b, cert)
|
|
119
|
+
if (!tbsT || !algT || !sigT) throw new Error('der: Certificate needs 3 fields')
|
|
120
|
+
if (sigT.tag !== TAG.BITSTRING) throw new Error('der: signatureValue must be a BIT STRING')
|
|
121
|
+
|
|
122
|
+
const sigAlgOid = oidString(b, expect(b, algT.contentStart, TAG.OID, 'signature algorithm'))
|
|
123
|
+
const sigAlg = SIG_OIDS.get(sigAlgOid)
|
|
124
|
+
if (!sigAlg) throw new Error(`der: unsupported signature algorithm ${sigAlgOid}`)
|
|
125
|
+
|
|
126
|
+
// BIT STRING content begins with an "unused bits" count, which must be 0 for a signature.
|
|
127
|
+
if (b[sigT.contentStart] !== 0x00) throw new Error('der: signature BIT STRING has unused bits')
|
|
128
|
+
const sigDer = b.subarray(sigT.contentStart + 1, sigT.contentEnd)
|
|
129
|
+
|
|
130
|
+
const kids = children(b, tbsT)
|
|
131
|
+
const base = kids[0] && kids[0].tag === 0xa0 ? 1 : 0 // [0] EXPLICIT version — absent means v1
|
|
132
|
+
const [serialT, innerAlgT, issuerT, validityT, subjectT, spkiT] = kids.slice(base, base + 6)
|
|
133
|
+
if (!serialT || !innerAlgT || !issuerT || !validityT || !subjectT || !spkiT) throw new Error('der: malformed tbsCertificate')
|
|
134
|
+
|
|
135
|
+
// RFC 5280 §4.1.1.2: the inner and outer algorithm identifiers must match. If they don't, one of them is decoration
|
|
136
|
+
// and a verifier can be steered to check the wrong thing.
|
|
137
|
+
const innerOid = oidString(b, expect(b, innerAlgT.contentStart, TAG.OID, 'inner signature algorithm'))
|
|
138
|
+
if (innerOid !== sigAlgOid) throw new Error('der: signature algorithm mismatch between tbs and certificate')
|
|
139
|
+
|
|
140
|
+
const [nbT, naT] = children(b, validityT)
|
|
141
|
+
if (!nbT || !naT) throw new Error('der: malformed validity')
|
|
142
|
+
|
|
143
|
+
// SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING }
|
|
144
|
+
const [spkiAlgT] = children(b, spkiT)
|
|
145
|
+
const spkiAlgKids = children(b, spkiAlgT)
|
|
146
|
+
if (spkiAlgKids.length < 2) throw new Error('der: SPKI algorithm needs a curve parameter')
|
|
147
|
+
const keyOid = oidString(b, spkiAlgKids[0])
|
|
148
|
+
if (keyOid !== '1.2.840.10045.2.1') throw new Error(`der: not an EC public key (${keyOid})`)
|
|
149
|
+
const curveOid = oidString(b, spkiAlgKids[1])
|
|
150
|
+
const curve = CURVE_OIDS.get(curveOid)
|
|
151
|
+
if (!curve) throw new Error(`der: unsupported curve ${curveOid}`)
|
|
152
|
+
|
|
153
|
+
// basicConstraints cA — read from the extensions, if present ([3] EXPLICIT).
|
|
154
|
+
let isCA = false
|
|
155
|
+
for (const k of kids.slice(base + 6)) {
|
|
156
|
+
if (k.tag !== 0xa3) continue
|
|
157
|
+
for (const ext of children(b, children(b, k)[0] || k)) {
|
|
158
|
+
const extKids = children(b, ext)
|
|
159
|
+
if (!extKids.length || oidString(b, extKids[0]) !== '2.5.29.19') continue
|
|
160
|
+
const octet = extKids[extKids.length - 1]
|
|
161
|
+
const bcSeq = readTLV(b, octet.contentStart)
|
|
162
|
+
const bcKids = children(b, bcSeq)
|
|
163
|
+
isCA = !!(bcKids[0] && bcKids[0].tag === 0x01 && b[bcKids[0].contentStart] !== 0x00)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
tbs: b.subarray(tbsT.start, tbsT.contentEnd),
|
|
169
|
+
sig: derSigToRaw(sigDer, curve.size),
|
|
170
|
+
sigHash: sigAlg.hash,
|
|
171
|
+
spki: b.slice(spkiT.start, spkiT.contentEnd), // copy: this is handed to WebCrypto, which wants its own buffer
|
|
172
|
+
curve: curve.curve,
|
|
173
|
+
size: curve.size,
|
|
174
|
+
issuer: b.subarray(issuerT.start, issuerT.contentEnd),
|
|
175
|
+
subject: b.subarray(subjectT.start, subjectT.contentEnd),
|
|
176
|
+
notBefore: parseTime(b, nbT),
|
|
177
|
+
notAfter: parseTime(b, naT),
|
|
178
|
+
isCA,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function bytesEqual(a, b) {
|
|
183
|
+
if (!a || !b || a.length !== b.length) return false
|
|
184
|
+
let d = 0
|
|
185
|
+
for (let i = 0; i < a.length; i++) d |= a[i] ^ b[i]
|
|
186
|
+
return d === 0
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function importSpki(cert) {
|
|
190
|
+
return crypto.subtle.importKey('spki', cert.spki, { name: 'ECDSA', namedCurve: cert.curve }, false, ['verify'])
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Verify a certificate chain ordered ANCHOR-FIRST: chain[0] is the trust anchor and is never itself verified — trust
|
|
195
|
+
* has to start somewhere, and it starts with a key the *verifier* pinned, not one the document supplied.
|
|
196
|
+
* Each subsequent certificate must be signed by its predecessor, in date, and issued to it by name.
|
|
197
|
+
* → the parsed leaf, so the caller can verify a payload signature under it.
|
|
198
|
+
*/
|
|
199
|
+
export async function verifyChain(chainDer, { now = Date.now() } = {}) {
|
|
200
|
+
if (!Array.isArray(chainDer) || chainDer.length < 2) throw new Error('chain: need an anchor and at least one certificate')
|
|
201
|
+
const certs = chainDer.map((d, i) => {
|
|
202
|
+
try { return parseCertificate(d) } catch (e) { throw new Error(`chain: certificate ${i} unparseable: ${e.message}`) }
|
|
203
|
+
})
|
|
204
|
+
for (let i = 1; i < certs.length; i++) {
|
|
205
|
+
const child = certs[i]
|
|
206
|
+
const parent = certs[i - 1]
|
|
207
|
+
if (!bytesEqual(child.issuer, parent.subject)) throw new Error(`chain: certificate ${i} was not issued by certificate ${i - 1}`)
|
|
208
|
+
// Every cert that signs another must actually be a CA. Without this, a leaf can mint further certificates.
|
|
209
|
+
if (i > 1 && !parent.isCA) throw new Error(`chain: certificate ${i - 1} signs but is not a CA`)
|
|
210
|
+
if (now < child.notBefore) throw new Error(`chain: certificate ${i} is not yet valid`)
|
|
211
|
+
if (now > child.notAfter) throw new Error(`chain: certificate ${i} has expired`)
|
|
212
|
+
const ok = await crypto.subtle.verify({ name: 'ECDSA', hash: child.sigHash }, await importSpki(parent), child.sig, child.tbs)
|
|
213
|
+
if (!ok) throw new Error(`chain: certificate ${i} has a bad signature`)
|
|
214
|
+
}
|
|
215
|
+
return certs[certs.length - 1]
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export { importSpki }
|