@dotrino/vault 0.3.0 → 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.
- package/package.json +2 -2
- package/src/enroll.js +250 -0
- package/src/index.js +35 -120
- package/src/service.js +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dotrino/vault",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Usa ESTE dispositivo (navegador) como bóveda/CA del ecosistema Dotrino: atiende enrolamientos por el proxy y firma certificados de delegación a tus máquinas. Incluye el cliente de SERVICIO (Node): un proyecto se enrola una vez y jala sus credenciales del vault en vez del .env (`import '@dotrino/vault/config'`).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"pairing"
|
|
43
43
|
],
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@dotrino/identity": ">=0.
|
|
45
|
+
"@dotrino/identity": ">=0.23.0",
|
|
46
46
|
"@dotrino/proxy-client": ">=0.6.0"
|
|
47
47
|
},
|
|
48
48
|
"license": "MIT",
|
package/src/enroll.js
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* enroll.js — núcleo del LADO BÓVEDA del emparejamiento endurecido.
|
|
3
|
+
*
|
|
4
|
+
* Fuente ÚNICA del flujo `vault.enroll` → `vault.enroll.challenge` → `vault.enrolled`
|
|
5
|
+
* y de la revocación firmada. Lo consumen los tres sitios que hacen de bóveda:
|
|
6
|
+
* · el daemon del PC (`dotrino-vault/src/vault.js`)
|
|
7
|
+
* · «este dispositivo es bóveda» (`lib/src/index.js#startDeviceVault`)
|
|
8
|
+
* · la copia vendorizada del iframe de identidad (`dotrino-identity/vault/vendor/vault/`)
|
|
9
|
+
*
|
|
10
|
+
* Módulo PURO: sin `node:*`, sin red, sin disco. Recibe la identidad (que firma), un
|
|
11
|
+
* transporte (`send`/`sendByPubkey`) y callbacks de log/auditoría. Así el binario Node
|
|
12
|
+
* lo embebe al compilar (SEA), el navegador lo importa y el iframe lo vendoriza sin
|
|
13
|
+
* bundler.
|
|
14
|
+
*
|
|
15
|
+
* EL CÓDIGO DE APROBACIÓN, en detalle (esto es lo que hace seguro el emparejamiento):
|
|
16
|
+
* 1. El DISPOSITIVO genera un código aleatorio de 6 dígitos, lo MUESTRA en su pantalla
|
|
17
|
+
* y manda solo su COMPROMISO `SHA-256(code‖dpub‖sn)` dentro del `data` firmado.
|
|
18
|
+
* El código en sí NUNCA viaja.
|
|
19
|
+
* 2. La bóveda no conoce el código: lo aprende cuando un humano lo TIPEA al aprobar.
|
|
20
|
+
* 3. Al aprobar, la bóveda RECOMPUTA el compromiso con el código tipeado y solo firma
|
|
21
|
+
* el cert si coincide → aprobar exige haber ido a leer el código del dispositivo.
|
|
22
|
+
* 4. La bóveda ECHA el código junto al cert; el dispositivo lo acepta solo si es el
|
|
23
|
+
* suyo → una bóveda falsa (que nunca vio el código) no puede enrolarlo.
|
|
24
|
+
*
|
|
25
|
+
* Qué cierra y qué NO (sin exagerar): cierra que se emita un cert sin que quien aprueba
|
|
26
|
+
* tenga el código del dispositivo — antes se firmaba igual y la defensa vivía solo en el
|
|
27
|
+
* cliente honesto, así que un cliente malicioso se quedaba con un cert válido. NO cierra
|
|
28
|
+
* el phishing en el que alguien le DICTA el código al dueño por otro canal: contra eso
|
|
29
|
+
* está la copy de advertencia y que el dueño reconozca el `deviceId` (residual A1/A2 de
|
|
30
|
+
* `docs/pairing-protocol.md`).
|
|
31
|
+
*/
|
|
32
|
+
import { verifyDeviceSig, pubkeyId, commitCode } from '@dotrino/identity/capabilities'
|
|
33
|
+
|
|
34
|
+
/** Un token de emparejamiento vale 5 min. */
|
|
35
|
+
export const PAIRING_TTL_MS = 5 * 60 * 1000
|
|
36
|
+
/** Ventana anti-replay del ENROLL (±5 min), mismo criterio que el identify del proxy. */
|
|
37
|
+
export const FRESH_WINDOW_MS = 5 * 60 * 1000
|
|
38
|
+
/** Vida por defecto del cert de un dispositivo (tope duro de `MAX_DELEGATION_MS`). */
|
|
39
|
+
export const DEVICE_TTL_MS = 30 * 24 * 60 * 60 * 1000
|
|
40
|
+
|
|
41
|
+
export const MSG_ENROLL = 'vault.enroll'
|
|
42
|
+
export const MSG_ENROLL_CHALLENGE = 'vault.enroll.challenge'
|
|
43
|
+
export const MSG_ENROLLED = 'vault.enrolled'
|
|
44
|
+
export const MSG_REVOKED = 'vault.revoked'
|
|
45
|
+
export const MSG_ERROR = 'vault.error'
|
|
46
|
+
|
|
47
|
+
/** Token aleatorio de 128 bits en hex. */
|
|
48
|
+
export function randToken () {
|
|
49
|
+
const b = crypto.getRandomValues(new Uint8Array(16))
|
|
50
|
+
return [...b].map((x) => x.toString(16).padStart(2, '0')).join('')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** deviceId legible (p. ej. `C440-AC0E`) a partir de una pubkey JWK. */
|
|
54
|
+
export async function deviceIdOf (pub) {
|
|
55
|
+
const id = (await pubkeyId(pub)).slice(0, 8).toUpperCase()
|
|
56
|
+
return id.slice(0, 4) + '-' + id.slice(4, 8)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Crea el «mostrador» de emparejamiento de una bóveda.
|
|
61
|
+
*
|
|
62
|
+
* @param {Object} opts
|
|
63
|
+
* @param {Object} opts.identity firma: `signData`, `signDelegation`, `listDelegations`, `revokeDelegation`.
|
|
64
|
+
* @param {string} opts.iss pubkey de la maestra de ESTA bóveda (va en el QR).
|
|
65
|
+
* @param {string} opts.proxy URL del proxy (va en el QR).
|
|
66
|
+
* @param {(to:string, obj:object)=>void} opts.send responder por el token de la conexión.
|
|
67
|
+
* @param {(pub:string, obj:object)=>void} opts.sendByPubkey dirigir por pubkey (cola offline 24 h).
|
|
68
|
+
* @param {(op:string, info?:object)=>void} [opts.audit]
|
|
69
|
+
* @param {(...a:any[])=>void} [opts.log]
|
|
70
|
+
* @param {(c:{deviceId:string, scope:any, label:string})=>void} [opts.onChallenge] un dispositivo espera aprobación.
|
|
71
|
+
* @param {()=>void} [opts.onPendingChange]
|
|
72
|
+
* @param {string[]} [opts.defaultScope]
|
|
73
|
+
* @param {number} [opts.defaultTtlMs]
|
|
74
|
+
*/
|
|
75
|
+
export function createEnrollDesk ({
|
|
76
|
+
identity, iss, proxy, send, sendByPubkey,
|
|
77
|
+
audit = () => {}, log = () => {},
|
|
78
|
+
onChallenge = () => {}, onPendingChange = () => {},
|
|
79
|
+
defaultScope = ['vault:sign'], defaultTtlMs = DEVICE_TTL_MS
|
|
80
|
+
} = {}) {
|
|
81
|
+
if (!identity) throw new Error('createEnrollDesk: falta identity')
|
|
82
|
+
if (!iss) throw new Error('createEnrollDesk: falta iss (pubkey de la maestra)')
|
|
83
|
+
|
|
84
|
+
// token -> { token, exp, scope, ttlMs, label, sn, state, dpub?, deviceId?, commit?, from? }
|
|
85
|
+
// state: 'AWAITING_ENROLL' -> 'PENDING_CONFIRM'
|
|
86
|
+
const pending = new Map()
|
|
87
|
+
|
|
88
|
+
const fire = (fn, arg) => { try { fn(arg) } catch (_) {} }
|
|
89
|
+
const reply = (to, obj) => { try { send(to, obj) } catch (e) { log('[vault] no se pudo responder:', e.message) } }
|
|
90
|
+
const isFresh = (d) => typeof d?.ts === 'number' && Math.abs(Date.now() - d.ts) <= FRESH_WINDOW_MS
|
|
91
|
+
|
|
92
|
+
/** Inicia un emparejamiento: token + nonce de sesión. NO firma nada todavía. */
|
|
93
|
+
function startPairing ({ scope = defaultScope, ttlMs = defaultTtlMs, label = '' } = {}) {
|
|
94
|
+
pending.clear() // uno a la vez: una sesión nueva supersede a la anterior
|
|
95
|
+
const token = randToken()
|
|
96
|
+
const sn = randToken()
|
|
97
|
+
pending.set(token, { token, exp: Date.now() + PAIRING_TTL_MS, scope, ttlMs, label, sn, state: 'AWAITING_ENROLL' })
|
|
98
|
+
return { token, qr: { v: 2, iss, proxy, token, sn }, expiresInMs: PAIRING_TTL_MS }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function stopPairing (token) { pending.delete(token) }
|
|
102
|
+
|
|
103
|
+
function listPending () {
|
|
104
|
+
return [...pending.values()]
|
|
105
|
+
.filter((p) => p.state === 'PENDING_CONFIRM')
|
|
106
|
+
.map((p) => ({ deviceId: p.deviceId, label: p.label || '', scope: p.scope }))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function findPending (deviceId) {
|
|
110
|
+
for (const p of pending.values()) {
|
|
111
|
+
if (p.state === 'PENDING_CONFIRM' && p.deviceId === deviceId) return p
|
|
112
|
+
}
|
|
113
|
+
return null
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* ENROLL: el dispositivo prueba posesión de `D` firmando el sobre y deja el
|
|
118
|
+
* COMPROMISO de su código. Todavía NO se firma ningún cert.
|
|
119
|
+
*/
|
|
120
|
+
async function handleEnroll (from, p) {
|
|
121
|
+
const d = p?.data
|
|
122
|
+
if (!d || typeof d.dpub !== 'string' || typeof p.signature !== 'string') {
|
|
123
|
+
return reply(from, { type: MSG_ERROR, error: 'enroll inválido' })
|
|
124
|
+
}
|
|
125
|
+
const pend = pending.get(d.token)
|
|
126
|
+
if (!pend || pend.state === 'DONE' || Date.now() > pend.exp) {
|
|
127
|
+
return reply(from, { type: MSG_ERROR, error: 'token de emparejamiento inválido o expirado' })
|
|
128
|
+
}
|
|
129
|
+
if (d.sn !== pend.sn) return reply(from, { type: MSG_ERROR, error: 'sesión inválida' })
|
|
130
|
+
if (!isFresh(d)) {
|
|
131
|
+
audit('rejected', { what: 'enroll', reason: 'stale' })
|
|
132
|
+
return reply(from, { type: MSG_ERROR, error: 'petición vencida: ts fuera de la ventana ±5 min (posible replay, o el reloj del dispositivo está desfasado)' })
|
|
133
|
+
}
|
|
134
|
+
// PRUEBA DE POSESIÓN: la firma de `data` debe verificar contra `dpub`.
|
|
135
|
+
if (!(await verifyDeviceSig({ publickey: d.dpub, data: d, signature: p.signature }))) {
|
|
136
|
+
audit('rejected', { what: 'enroll', reason: 'bad-device-signature' })
|
|
137
|
+
return reply(from, { type: MSG_ERROR, error: 'firma de dispositivo inválida' })
|
|
138
|
+
}
|
|
139
|
+
// El COMPROMISO del código es obligatorio: sin él no se puede comprobar al aprobar
|
|
140
|
+
// y volveríamos a emitir certs a ciegas. Un cliente viejo cae acá con un mensaje claro.
|
|
141
|
+
if (typeof d.commit !== 'string' || !/^[0-9a-f]{64}$/.test(d.commit)) {
|
|
142
|
+
audit('rejected', { what: 'enroll', reason: 'no-commit' })
|
|
143
|
+
return reply(from, { type: MSG_ERROR, error: 'este dispositivo usa una versión antigua del emparejamiento (no envía el compromiso del código). Actualízalo y vuelve a intentarlo.' })
|
|
144
|
+
}
|
|
145
|
+
// Un solo dispositivo a la vez esperando su código (así aprobar no es ambiguo).
|
|
146
|
+
if (pend.state === 'PENDING_CONFIRM' && pend.dpub && pend.dpub !== d.dpub) {
|
|
147
|
+
return reply(from, { type: MSG_ERROR, error: 'ya hay un dispositivo usando este emparejamiento' })
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const deviceId = await deviceIdOf(d.dpub)
|
|
151
|
+
pend.state = 'PENDING_CONFIRM'
|
|
152
|
+
pend.dpub = d.dpub
|
|
153
|
+
pend.deviceId = deviceId
|
|
154
|
+
pend.commit = d.commit
|
|
155
|
+
pend.from = from // la bóveda NO conoce el código: lo aprende cuando lo tipeas
|
|
156
|
+
if (d.label) pend.label = String(d.label).slice(0, 60)
|
|
157
|
+
|
|
158
|
+
reply(from, { type: MSG_ENROLL_CHALLENGE, deviceId })
|
|
159
|
+
fire(onChallenge, { deviceId, scope: pend.scope, label: pend.label || '' })
|
|
160
|
+
fire(onPendingChange)
|
|
161
|
+
return { deviceId }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Aprueba TIPEANDO el código que muestra el dispositivo. Recompone el compromiso
|
|
166
|
+
* `SHA-256(code‖dpub‖sn)` y solo firma el cert si coincide con el que llegó en el
|
|
167
|
+
* ENROLL — es decir, solo si de verdad fuiste a leer el código del dispositivo.
|
|
168
|
+
*
|
|
169
|
+
* @param {string} code
|
|
170
|
+
* @param {{deviceId?: string}} [opts] cuál aprobar cuando hay varios pendientes.
|
|
171
|
+
*/
|
|
172
|
+
async function approve (code, { deviceId } = {}) {
|
|
173
|
+
code = String(code || '').trim()
|
|
174
|
+
if (!code) throw new Error('falta el código (los dígitos que muestra el dispositivo)')
|
|
175
|
+
|
|
176
|
+
let pend
|
|
177
|
+
if (deviceId) {
|
|
178
|
+
pend = findPending(deviceId)
|
|
179
|
+
if (!pend) throw new Error('no hay ninguna máquina esperando aprobación con ese identificador')
|
|
180
|
+
} else {
|
|
181
|
+
const waiting = [...pending.values()].filter((p) => p.state === 'PENDING_CONFIRM' && p.dpub)
|
|
182
|
+
if (waiting.length === 0) throw new Error('no hay ningún dispositivo esperando aprobación')
|
|
183
|
+
if (waiting.length > 1) throw new Error('hay más de un emparejamiento en curso; reinícialo con dotrino-vault pair')
|
|
184
|
+
pend = waiting[0]
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// COMPROBACIÓN DEL CÓDIGO — antes de firmar nada.
|
|
188
|
+
const expected = await commitCode({ code, dpub: pend.dpub, sn: pend.sn })
|
|
189
|
+
if (expected !== pend.commit) {
|
|
190
|
+
audit('rejected', { what: 'approve', device: pend.deviceId, reason: 'bad-code' })
|
|
191
|
+
log('[vault] código incorrecto para %s: no se emitió ningún certificado', pend.deviceId)
|
|
192
|
+
throw new Error('el código no coincide con el que muestra el dispositivo: no se emitió ningún certificado. Vuelve a mirarlo y prueba otra vez.')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const { cert } = await identity.signDelegation(pend.dpub, pend.scope, { ttlMs: pend.ttlMs, label: pend.label })
|
|
196
|
+
audit('enroll', { device: pend.deviceId, label: pend.label || '', scope: pend.scope })
|
|
197
|
+
// Echamos el código tipeado junto al cert: el DISPOSITIVO acepta solo si coincide
|
|
198
|
+
// con el que generó → una bóveda falsa (que no lo conoce) no puede enrolarlo.
|
|
199
|
+
reply(pend.from, { type: MSG_ENROLLED, code, cert, iss })
|
|
200
|
+
pend.state = 'DONE'
|
|
201
|
+
pending.delete(pend.token)
|
|
202
|
+
fire(onPendingChange)
|
|
203
|
+
log('[vault] dispositivo aprobado: %s', pend.deviceId)
|
|
204
|
+
return { ok: true, deviceId: pend.deviceId, cert }
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Rechaza un enrolamiento pendiente. */
|
|
208
|
+
function reject (deviceId) {
|
|
209
|
+
const pend = deviceId
|
|
210
|
+
? findPending(deviceId)
|
|
211
|
+
: [...pending.values()].find((p) => p.state === 'PENDING_CONFIRM')
|
|
212
|
+
if (!pend) return { ok: false }
|
|
213
|
+
reply(pend.from, { type: MSG_ERROR, error: 'emparejamiento rechazado' })
|
|
214
|
+
pending.delete(pend.token)
|
|
215
|
+
audit('reject', { device: pend.deviceId })
|
|
216
|
+
fire(onPendingChange)
|
|
217
|
+
log('[vault] dispositivo rechazado: %s', pend.deviceId)
|
|
218
|
+
return { ok: true, deviceId: pend.deviceId }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Emite un REVOKED FIRMADO por la maestra para que el dispositivo se autoborre. El
|
|
223
|
+
* borrado remoto SOLO se dispara con esta firma (nunca con un error cualquiera →
|
|
224
|
+
* cierra el wipe-DoS). Va por `sendByPubkey`: si está apagado, el proxy lo encola 24 h.
|
|
225
|
+
*/
|
|
226
|
+
async function emitRevoke (dpub, nonce) {
|
|
227
|
+
const body = { op: 'revoke', sub: dpub, nonce, iat: Date.now(), exp: Date.now() + DEVICE_TTL_MS }
|
|
228
|
+
const { signature } = await identity.signData(body)
|
|
229
|
+
try { sendByPubkey(dpub, { type: MSG_REVOKED, body, signature }) }
|
|
230
|
+
catch (e) { log('[vault] no se pudo emitir revoke:', e.message) }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Revoca una delegación por `nonce` y avisa al dispositivo para que se autoborre. */
|
|
234
|
+
async function revoke (nonce) {
|
|
235
|
+
audit('revoke', { nonce })
|
|
236
|
+
const { issued } = await identity.listDelegations()
|
|
237
|
+
const dele = (issued || []).find((d) => d.nonce === nonce)
|
|
238
|
+
const res = await identity.revokeDelegation(nonce)
|
|
239
|
+
if (dele?.sub) await emitRevoke(dele.sub, nonce)
|
|
240
|
+
return res
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
startPairing, stopPairing, handleEnroll, approve, reject,
|
|
245
|
+
listPending, findPending, emitRevoke, revoke,
|
|
246
|
+
get pendingCount () { return pending.size }
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export default { createEnrollDesk, deviceIdOf, randToken }
|
package/src/index.js
CHANGED
|
@@ -17,37 +17,29 @@
|
|
|
17
17
|
* → una bóveda falsa (que nunca vio el código) no puede enrolar el dispositivo, y
|
|
18
18
|
* aprobar "a ciegas" (sin ir a leer el código del dispositivo) tampoco enrola nada.
|
|
19
19
|
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
20
|
+
* El flujo de enrolamiento en sí (incluida la comprobación del código antes de firmar) vive
|
|
21
|
+
* en `./enroll.js`, COMPARTIDO con el daemon del PC y con la copia vendorizada del iframe:
|
|
22
|
+
* un solo sitio donde se decide a quién se le emite un certificado.
|
|
23
|
+
*
|
|
24
|
+
* Cripto 100% de `@dotrino/identity/capabilities`; firma con la identidad P
|
|
25
|
+
* (`identity.signDelegation`). Transporte: `@dotrino/proxy-client` (import perezoso).
|
|
26
|
+
* No reimplementa nada del ecosistema.
|
|
23
27
|
*/
|
|
24
|
-
import {
|
|
28
|
+
import { verifyChain } from '@dotrino/identity/capabilities'
|
|
29
|
+
import { createEnrollDesk, deviceIdOf, DEVICE_TTL_MS, FRESH_WINDOW_MS } from './enroll.js'
|
|
25
30
|
|
|
26
31
|
const SIGN_SCOPE = 'vault:sign'
|
|
27
|
-
const PAIRING_TTL_MS = 5 * 60 * 1000 // un emparejamiento (token) vale 5 min
|
|
28
|
-
const DEVICE_TTL_MS = 30 * 24 * 60 * 60 * 1000 // vida de un cert de dispositivo (30 días)
|
|
29
32
|
const SELFCERT_TTL_MS = 24 * 60 * 60 * 1000 // el self-cert P←P se regenera cada 24 h
|
|
30
|
-
const FRESH_WINDOW_MS = 5 * 60 * 1000 // ventana anti-replay del enroll (±5 min)
|
|
31
33
|
|
|
32
34
|
const MSG = {
|
|
33
35
|
ENROLL: 'vault.enroll',
|
|
34
|
-
ENROLL_CHALLENGE: 'vault.enroll.challenge',
|
|
35
|
-
ENROLLED: 'vault.enrolled',
|
|
36
36
|
DEVICES: 'vault.devices',
|
|
37
37
|
DEVICES_RESULT: 'vault.devices.result',
|
|
38
|
-
REVOKED: 'vault.revoked',
|
|
39
38
|
ERROR: 'vault.error'
|
|
40
39
|
}
|
|
41
40
|
|
|
42
|
-
function randToken () {
|
|
43
|
-
const b = crypto.getRandomValues(new Uint8Array(16))
|
|
44
|
-
return [...b].map((x) => x.toString(16).padStart(2, '0')).join('')
|
|
45
|
-
}
|
|
46
|
-
|
|
47
41
|
/** deviceId legible (p. ej. `C440-AC0E`) desde una pubkey JWK. */
|
|
48
|
-
export
|
|
49
|
-
return pubkeyId(pub).then((id) => id.slice(0, 8).toUpperCase().replace(/(.{4})(.{4})/, '$1-$2'))
|
|
50
|
-
}
|
|
42
|
+
export { deviceIdOf }
|
|
51
43
|
|
|
52
44
|
/**
|
|
53
45
|
* Levanta la bóveda de este dispositivo: se conecta al proxy identificado como P y
|
|
@@ -94,49 +86,21 @@ export async function startDeviceVault (identity, { proxyUrl } = {}) {
|
|
|
94
86
|
|
|
95
87
|
const send = (to, obj) => { try { client.send(to, obj) } catch (_) {} }
|
|
96
88
|
|
|
97
|
-
// token -> { exp, sn, scope, ttlMs, label, state, dpub?, deviceId?, from? }
|
|
98
|
-
const pending = new Map()
|
|
99
89
|
let _onPendingChange = () => {}
|
|
100
90
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
// PRUEBA DE POSESIÓN: la firma de `data` debe verificar contra `dpub`.
|
|
115
|
-
const ok = await verifyDeviceSig({ publickey: d.dpub, data: d, signature: p.signature })
|
|
116
|
-
if (!ok) return send(from, { type: MSG.ERROR, error: 'firma de dispositivo inválida' })
|
|
117
|
-
// Un solo dispositivo a la vez esperando su código (así `approve` no es ambiguo).
|
|
118
|
-
if (pend.state === 'PENDING_CONFIRM' && pend.dpub && pend.dpub !== d.dpub) {
|
|
119
|
-
return send(from, { type: MSG.ERROR, error: 'ya hay un dispositivo usando este emparejamiento' })
|
|
120
|
-
}
|
|
121
|
-
const deviceId = await deviceIdOf(d.dpub)
|
|
122
|
-
pend.state = 'PENDING_CONFIRM'
|
|
123
|
-
pend.dpub = d.dpub
|
|
124
|
-
pend.deviceId = deviceId
|
|
125
|
-
pend.from = from // esta bóveda NO conoce el código (no viaja): el dispositivo lo MUESTRA
|
|
126
|
-
if (d.label) pend.label = String(d.label).slice(0, 60)
|
|
127
|
-
_onPendingChange()
|
|
128
|
-
send(from, { type: MSG.ENROLL_CHALLENGE, deviceId })
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Emite un REVOKED FIRMADO por la maestra a la máquina revocada para que se
|
|
132
|
-
// auto-borre. Va por `sendByPubkey` → si está offline, el proxy lo encola 24 h; y
|
|
133
|
-
// si reaparece más tarde, `handleDevices` lo re-emite en su siguiente consulta.
|
|
134
|
-
// El auto-borrado remoto SOLO se dispara con esta firma (no con un error cualquiera).
|
|
135
|
-
async function emitRevoke (dpub, nonce) {
|
|
136
|
-
const body = { op: 'revoke', sub: dpub, nonce, iat: Date.now(), exp: Date.now() + DEVICE_TTL_MS }
|
|
137
|
-
const { signature } = await identity.signData(body)
|
|
138
|
-
try { client.sendByPubkey(dpub, { type: MSG.REVOKED, body, signature }) } catch (_) {}
|
|
139
|
-
}
|
|
91
|
+
// ENROLL / aprobación / revocación: núcleo COMPARTIDO con el daemon del PC y con la
|
|
92
|
+
// copia vendorizada del iframe (`lib/src/enroll.js`). Un solo sitio donde vive el
|
|
93
|
+
// flujo → y por lo tanto un solo sitio donde se comprueba el código antes de firmar.
|
|
94
|
+
const desk = createEnrollDesk({
|
|
95
|
+
identity,
|
|
96
|
+
iss,
|
|
97
|
+
proxy,
|
|
98
|
+
send,
|
|
99
|
+
sendByPubkey: (pub, obj) => { try { client.sendByPubkey(pub, obj) } catch (_) {} },
|
|
100
|
+
defaultScope: [SIGN_SCOPE],
|
|
101
|
+
defaultTtlMs: DEVICE_TTL_MS,
|
|
102
|
+
onPendingChange: () => _onPendingChange()
|
|
103
|
+
})
|
|
140
104
|
|
|
141
105
|
// Consulta de revocaciones (igual que `vault.devices` del daemon): responde la lista
|
|
142
106
|
// de dispositivos enrolados + revocados para que el dispositivo refresque su set. Y si
|
|
@@ -155,62 +119,15 @@ export async function startDeviceVault (identity, { proxyUrl } = {}) {
|
|
|
155
119
|
send(from, { type: MSG.DEVICES_RESULT, devices, revoked: (revoked || []).map((r) => r.nonce || r) })
|
|
156
120
|
// ¿el que consulta es una máquina revocada que reapareció? → re-emite el REVOKED firmado.
|
|
157
121
|
const mine = (issued || []).find((x) => x.sub === chk.device && x.revokedAt)
|
|
158
|
-
if (mine) emitRevoke(chk.device, mine.nonce)
|
|
122
|
+
if (mine) desk.emitRevoke(chk.device, mine.nonce)
|
|
159
123
|
}
|
|
160
124
|
|
|
161
125
|
client.on('message', (_from, p) => {
|
|
162
126
|
if (!p || typeof p !== 'object') return
|
|
163
|
-
if (p.type === MSG.ENROLL) handleEnroll(_from, p).catch(() => {})
|
|
127
|
+
if (p.type === MSG.ENROLL) desk.handleEnroll(_from, p).catch(() => {})
|
|
164
128
|
else if (p.type === MSG.DEVICES) handleDevices(_from, p).catch(() => {})
|
|
165
129
|
})
|
|
166
130
|
|
|
167
|
-
/**
|
|
168
|
-
* Abre un emparejamiento: devuelve el QR/JSON v2 que el dispositivo consume para
|
|
169
|
-
* enrolarse. `scope`/`ttlMs`/`label` fijan lo que otorgará el cert al aprobar.
|
|
170
|
-
*/
|
|
171
|
-
function startPairing ({ scope = [SIGN_SCOPE], ttlMs = DEVICE_TTL_MS, label = '' } = {}) {
|
|
172
|
-
pending.clear()
|
|
173
|
-
const token = randToken()
|
|
174
|
-
const sn = randToken()
|
|
175
|
-
pending.set(token, { token, exp: Date.now() + PAIRING_TTL_MS, sn, scope, ttlMs, label, state: 'AWAITING_ENROLL' })
|
|
176
|
-
return { qr: { v: 2, iss, proxy, token, sn }, expiresInMs: PAIRING_TTL_MS }
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function listPending () {
|
|
180
|
-
return [...pending.values()]
|
|
181
|
-
.filter((p) => p.state === 'PENDING_CONFIRM')
|
|
182
|
-
.map((p) => ({ deviceId: p.deviceId, label: p.label }))
|
|
183
|
-
}
|
|
184
|
-
function findPending (deviceId) {
|
|
185
|
-
for (const [, p] of pending) if (p.state === 'PENDING_CONFIRM' && p.deviceId === deviceId) return p
|
|
186
|
-
return null
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Aprueba una máquina pendiente TIPEANDO el código que ella muestra. Esta bóveda NO
|
|
191
|
-
* conoce/valida el código: firma el cert y ECHA el código tipeado; la máquina lo acepta
|
|
192
|
-
* solo si coincide con el que generó. (Modelo `dotrino-vault#approveDevice`.)
|
|
193
|
-
*/
|
|
194
|
-
async function approve (deviceId, code) {
|
|
195
|
-
const pend = findPending(deviceId)
|
|
196
|
-
if (!pend || !pend.dpub) throw new Error('no hay ninguna máquina esperando aprobación')
|
|
197
|
-
code = String(code || '').trim()
|
|
198
|
-
if (!code) throw new Error('escribe el código que muestra la máquina')
|
|
199
|
-
const { cert } = await identity.signDelegation(pend.dpub, pend.scope, { ttlMs: pend.ttlMs, label: pend.label })
|
|
200
|
-
send(pend.from, { type: MSG.ENROLLED, code, cert, iss })
|
|
201
|
-
pending.delete(pend.token)
|
|
202
|
-
_onPendingChange()
|
|
203
|
-
return { ok: true, deviceId }
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function reject (deviceId) {
|
|
207
|
-
const pend = findPending(deviceId)
|
|
208
|
-
if (!pend) return
|
|
209
|
-
send(pend.from, { type: MSG.ERROR, error: 'emparejamiento rechazado' })
|
|
210
|
-
pending.delete(pend.token)
|
|
211
|
-
_onPendingChange()
|
|
212
|
-
}
|
|
213
|
-
|
|
214
131
|
/**
|
|
215
132
|
* Máquinas enroladas bajo esta identidad (P), vigentes, con scope de firma y label
|
|
216
133
|
* propio (excluye navegadores enrolados con label 'cli', que no atienden peticiones).
|
|
@@ -228,20 +145,18 @@ export async function startDeviceVault (identity, { proxyUrl } = {}) {
|
|
|
228
145
|
return Promise.all([...bySub.values()].map(async (x) => ({ ...x, deviceId: await deviceIdOf(x.sub) })))
|
|
229
146
|
}
|
|
230
147
|
|
|
231
|
-
async function revoke (nonce) {
|
|
232
|
-
// Deja el registro persistente (revokedAt en la delegación) y AVISA a la máquina
|
|
233
|
-
// con un REVOKED firmado para que se auto-borre (ahora si está online, o al
|
|
234
|
-
// reaparecer vía handleDevices). Ver emitRevoke.
|
|
235
|
-
const { issued } = await identity.listDelegations()
|
|
236
|
-
const dele = (issued || []).find((d) => d.nonce === nonce)
|
|
237
|
-
const res = await identity.revokeDelegation(nonce)
|
|
238
|
-
if (dele?.sub) await emitRevoke(dele.sub, nonce)
|
|
239
|
-
return res
|
|
240
|
-
}
|
|
241
|
-
|
|
242
148
|
return {
|
|
243
149
|
iss, proxy, client,
|
|
244
|
-
startPairing
|
|
150
|
+
startPairing: desk.startPairing,
|
|
151
|
+
// Aprueba TIPEANDO el código que muestra la máquina: el núcleo compartido recompone
|
|
152
|
+
// el compromiso `SHA-256(code‖dpub‖sn)` y solo firma el cert si coincide.
|
|
153
|
+
approve: (deviceId, code) => desk.approve(code, { deviceId }),
|
|
154
|
+
reject: (deviceId) => desk.reject(deviceId),
|
|
155
|
+
listPending: desk.listPending,
|
|
156
|
+
listMachines,
|
|
157
|
+
// Revoca y AVISA a la máquina con un REVOKED firmado para que se auto-borre (ahora si
|
|
158
|
+
// está online, o al reaparecer vía handleDevices).
|
|
159
|
+
revoke: (nonce) => desk.revoke(nonce),
|
|
245
160
|
getSelfCert,
|
|
246
161
|
onPendingChange (fn) { _onPendingChange = fn || (() => {}) },
|
|
247
162
|
close () { try { client.close() } catch (_) {} }
|
package/src/service.js
CHANGED
|
@@ -22,7 +22,7 @@ import fs from 'node:fs'
|
|
|
22
22
|
import path from 'node:path'
|
|
23
23
|
import {
|
|
24
24
|
makeDeviceKey, signWithDevice, verifyDelegation, verifyDeviceSig,
|
|
25
|
-
makePairingCode, pubkeyId
|
|
25
|
+
makePairingCode, commitCode, pubkeyId
|
|
26
26
|
} from '@dotrino/identity/capabilities'
|
|
27
27
|
import { MSG, secretsScope, isValidSecretsNs } from './protocol.js'
|
|
28
28
|
import { makeEphemeralKey, openSealed } from './sealed.js'
|
|
@@ -144,7 +144,10 @@ export async function enrollService ({ qr, ns, dir, label, onCode, approveTimeou
|
|
|
144
144
|
// Código ALEATORIO generado AQUÍ: el vault no lo conoce; solo puede echarlo
|
|
145
145
|
// de vuelta si el dueño lo tipeó (= tiene esta pantalla a la vista).
|
|
146
146
|
const code = makePairingCode()
|
|
147
|
-
|
|
147
|
+
// El COMPROMISO del código (nunca el código): la bóveda lo recompone con lo que
|
|
148
|
+
// tipeas y solo entonces firma el cert → aprobar exige haber leído esta pantalla.
|
|
149
|
+
const commit = await commitCode({ code, dpub: device.publickey, sn: qr.sn })
|
|
150
|
+
const data = { op: 'enroll', dpub: device.publickey, token: qr.token, sn: qr.sn, commit, label, ts: Date.now() }
|
|
148
151
|
const { signature } = await signWithDevice({ privateJwk: device.privateJwk, data })
|
|
149
152
|
|
|
150
153
|
const enrolled = new Promise((resolve, reject) => {
|