mindoodb 0.0.97 → 0.0.98
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/README.md +1 -0
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.d.ts +7 -0
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.js +17 -9
- package/dist/appendonlystores/network/ClientNetworkContentAddressedStore.js.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts +12 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.d.ts.map +1 -1
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js +19 -0
- package/dist/appendonlystores/network/ServerNetworkContentAddressedStore.js.map +1 -1
- package/dist/browser/index.mjs +70 -0
- package/dist/core/BaseMindooDB.d.ts +2 -0
- package/dist/core/BaseMindooDB.d.ts.map +1 -1
- package/dist/core/BaseMindooDB.js +73 -53
- package/dist/core/BaseMindooDB.js.map +1 -1
- package/dist/core/BaseMindooTenant.d.ts +33 -1
- package/dist/core/BaseMindooTenant.d.ts.map +1 -1
- package/dist/core/BaseMindooTenant.js +141 -41
- package/dist/core/BaseMindooTenant.js.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.d.ts +11 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.js +137 -92
- package/dist/core/BaseMindooTenantDirectory.js.map +1 -1
- package/dist/core/builtinDbInvariants.d.ts +26 -1
- package/dist/core/builtinDbInvariants.d.ts.map +1 -1
- package/dist/core/builtinDbInvariants.js +52 -2
- package/dist/core/builtinDbInvariants.js.map +1 -1
- package/dist/core/index.d.ts +2 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +3 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/joinrequests/crypto.d.ts +110 -0
- package/dist/core/joinrequests/crypto.d.ts.map +1 -0
- package/dist/core/joinrequests/crypto.js +340 -0
- package/dist/core/joinrequests/crypto.js.map +1 -0
- package/dist/core/joinrequests/index.d.ts +4 -0
- package/dist/core/joinrequests/index.d.ts.map +1 -0
- package/dist/core/joinrequests/index.js +29 -0
- package/dist/core/joinrequests/index.js.map +1 -0
- package/dist/core/joinrequests/invitations.d.ts +257 -0
- package/dist/core/joinrequests/invitations.d.ts.map +1 -0
- package/dist/core/joinrequests/invitations.js +703 -0
- package/dist/core/joinrequests/invitations.js.map +1 -0
- package/dist/core/joinrequests/types.d.ts +208 -0
- package/dist/core/joinrequests/types.d.ts.map +1 -0
- package/dist/core/joinrequests/types.js +43 -0
- package/dist/core/joinrequests/types.js.map +1 -0
- package/dist/core/recovery/PaperRescueDevice.d.ts +92 -0
- package/dist/core/recovery/PaperRescueDevice.d.ts.map +1 -0
- package/dist/core/recovery/PaperRescueDevice.js +182 -0
- package/dist/core/recovery/PaperRescueDevice.js.map +1 -0
- package/dist/core/recovery/RecoveryContactsDocument.d.ts +91 -0
- package/dist/core/recovery/RecoveryContactsDocument.d.ts.map +1 -0
- package/dist/core/recovery/RecoveryContactsDocument.js +92 -0
- package/dist/core/recovery/RecoveryContactsDocument.js.map +1 -0
- package/dist/core/recovery/RecoveryContactsManager.d.ts +95 -0
- package/dist/core/recovery/RecoveryContactsManager.d.ts.map +1 -0
- package/dist/core/recovery/RecoveryContactsManager.js +322 -0
- package/dist/core/recovery/RecoveryContactsManager.js.map +1 -0
- package/dist/core/recovery/index.d.ts +8 -0
- package/dist/core/recovery/index.d.ts.map +1 -0
- package/dist/core/recovery/index.js +41 -0
- package/dist/core/recovery/index.js.map +1 -0
- package/dist/core/recovery/recoverWithContacts.d.ts +36 -0
- package/dist/core/recovery/recoverWithContacts.d.ts.map +1 -0
- package/dist/core/recovery/recoverWithContacts.js +103 -0
- package/dist/core/recovery/recoverWithContacts.js.map +1 -0
- package/dist/core/recovery/recoveryPackage.d.ts +72 -0
- package/dist/core/recovery/recoveryPackage.d.ts.map +1 -0
- package/dist/core/recovery/recoveryPackage.js +181 -0
- package/dist/core/recovery/recoveryPackage.js.map +1 -0
- package/dist/core/recovery/rescueDevice.d.ts +51 -0
- package/dist/core/recovery/rescueDevice.d.ts.map +1 -0
- package/dist/core/recovery/rescueDevice.js +201 -0
- package/dist/core/recovery/rescueDevice.js.map +1 -0
- package/dist/core/recovery/shamir.d.ts +30 -0
- package/dist/core/recovery/shamir.d.ts.map +1 -0
- package/dist/core/recovery/shamir.js +133 -0
- package/dist/core/recovery/shamir.js.map +1 -0
- package/dist/core/types.d.ts +64 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/core/userkeys/UserKeyManager.d.ts +29 -0
- package/dist/core/userkeys/UserKeyManager.d.ts.map +1 -1
- package/dist/core/userkeys/UserKeyManager.js +76 -8
- package/dist/core/userkeys/UserKeyManager.js.map +1 -1
- package/dist/core/userkeys/generationSignature.d.ts +6 -0
- package/dist/core/userkeys/generationSignature.d.ts.map +1 -1
- package/dist/core/userkeys/generationSignature.js +1 -0
- package/dist/core/userkeys/generationSignature.js.map +1 -1
- package/dist/core/userkeys/resolveUserKeyDocument.d.ts.map +1 -1
- package/dist/core/userkeys/resolveUserKeyDocument.js +6 -6
- package/dist/core/userkeys/resolveUserKeyDocument.js.map +1 -1
- package/dist/core/userkeys/validateUserKeyDocument.d.ts +11 -1
- package/dist/core/userkeys/validateUserKeyDocument.d.ts.map +1 -1
- package/dist/core/userkeys/validateUserKeyDocument.js +18 -5
- package/dist/core/userkeys/validateUserKeyDocument.js.map +1 -1
- package/dist/node/server/MindooDBServer.d.ts +2 -0
- package/dist/node/server/MindooDBServer.d.ts.map +1 -1
- package/dist/node/server/MindooDBServer.js +46 -0
- package/dist/node/server/MindooDBServer.js.map +1 -1
- package/dist/node/server/StoreFactory.d.ts.map +1 -1
- package/dist/node/server/StoreFactory.js +4 -1
- package/dist/node/server/StoreFactory.js.map +1 -1
- package/dist/node/server/server.js +2 -0
- package/dist/node/server/server.js.map +1 -1
- package/dist/server/JoinRequestService.d.ts +108 -0
- package/dist/server/JoinRequestService.d.ts.map +1 -0
- package/dist/server/JoinRequestService.js +454 -0
- package/dist/server/JoinRequestService.js.map +1 -0
- package/dist/server/TenantManager.d.ts +50 -0
- package/dist/server/TenantManager.d.ts.map +1 -1
- package/dist/server/TenantManager.js +271 -33
- package/dist/server/TenantManager.js.map +1 -1
- package/dist/server/http/MindooDBServerCore.d.ts +18 -0
- package/dist/server/http/MindooDBServerCore.d.ts.map +1 -1
- package/dist/server/http/MindooDBServerCore.js +229 -137
- package/dist/server/http/MindooDBServerCore.js.map +1 -1
- package/dist/server/serverConfig.d.ts.map +1 -1
- package/dist/server/serverConfig.js +17 -0
- package/dist/server/serverConfig.js.map +1 -1
- package/dist/server/types.d.ts +15 -0
- package/dist/server/types.d.ts.map +1 -1
- package/dist/server/types.js +6 -0
- package/dist/server/types.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRescueDevice = generateRescueDevice;
|
|
4
|
+
exports.openRescueDevice = openRescueDevice;
|
|
5
|
+
exports.rescueDeviceUserId = rescueDeviceUserId;
|
|
6
|
+
const privateKeyEncryption_1 = require("../crypto/privateKeyEncryption");
|
|
7
|
+
const generationSignature_1 = require("../userkeys/generationSignature");
|
|
8
|
+
const fingerprint_1 = require("../userkeys/fingerprint");
|
|
9
|
+
const BLOB_VERSION = 1;
|
|
10
|
+
const BLOB_AAD = new TextEncoder().encode("mindoodb-rescue-device-v1");
|
|
11
|
+
const RSA_E = 65537n;
|
|
12
|
+
function toBuffer(bytes) {
|
|
13
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
14
|
+
}
|
|
15
|
+
function base64UrlToBytes(value) {
|
|
16
|
+
const normalized = value.replace(/-/g, "+").replace(/_/g, "/");
|
|
17
|
+
return (0, fingerprint_1.base64ToBytes)(normalized + "=".repeat((4 - (normalized.length % 4)) % 4));
|
|
18
|
+
}
|
|
19
|
+
function bytesToBase64Url(bytes) {
|
|
20
|
+
return (0, fingerprint_1.bytesToBase64)(bytes).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
21
|
+
}
|
|
22
|
+
function bytesToBigInt(bytes) {
|
|
23
|
+
let value = 0n;
|
|
24
|
+
for (const byte of bytes)
|
|
25
|
+
value = (value << 8n) | BigInt(byte);
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
/** Big-endian, minimal length (JWK Base64urlUInt). */
|
|
29
|
+
function bigIntToBytes(value) {
|
|
30
|
+
if (value === 0n)
|
|
31
|
+
return new Uint8Array([0]);
|
|
32
|
+
const hex = value.toString(16);
|
|
33
|
+
const padded = hex.length % 2 ? `0${hex}` : hex;
|
|
34
|
+
const out = new Uint8Array(padded.length / 2);
|
|
35
|
+
for (let i = 0; i < out.length; i++)
|
|
36
|
+
out[i] = parseInt(padded.slice(i * 2, i * 2 + 2), 16);
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
function modInverse(a, m) {
|
|
40
|
+
let [oldR, r] = [((a % m) + m) % m, m];
|
|
41
|
+
let [oldS, s] = [1n, 0n];
|
|
42
|
+
while (r !== 0n) {
|
|
43
|
+
const q = oldR / r;
|
|
44
|
+
[oldR, r] = [r, oldR - q * r];
|
|
45
|
+
[oldS, s] = [s, oldS - q * s];
|
|
46
|
+
}
|
|
47
|
+
if (oldR !== 1n)
|
|
48
|
+
throw new Error("not invertible");
|
|
49
|
+
return ((oldS % m) + m) % m;
|
|
50
|
+
}
|
|
51
|
+
async function aesKey(crypto, secret, usage) {
|
|
52
|
+
if (secret.length !== 32)
|
|
53
|
+
throw new Error("recovery secret must be 32 bytes");
|
|
54
|
+
return crypto.getSubtle().importKey("raw", toBuffer(secret), { name: "AES-GCM" }, false, [usage]);
|
|
55
|
+
}
|
|
56
|
+
/** Encode seed and primes: version | seed(32) | len(p) u16 | p | len(q) u16 | q. */
|
|
57
|
+
function encodeRescueKeys(seed, p, q) {
|
|
58
|
+
const out = new Uint8Array(1 + 32 + 2 + p.length + 2 + q.length);
|
|
59
|
+
let offset = 0;
|
|
60
|
+
out[offset++] = BLOB_VERSION;
|
|
61
|
+
out.set(seed, offset);
|
|
62
|
+
offset += 32;
|
|
63
|
+
for (const prime of [p, q]) {
|
|
64
|
+
out[offset++] = prime.length >> 8;
|
|
65
|
+
out[offset++] = prime.length & 0xff;
|
|
66
|
+
out.set(prime, offset);
|
|
67
|
+
offset += prime.length;
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
function decodeRescueKeys(bytes) {
|
|
72
|
+
if (bytes[0] !== BLOB_VERSION)
|
|
73
|
+
throw new Error("unknown rescue device format");
|
|
74
|
+
let offset = 1;
|
|
75
|
+
const seed = bytes.slice(offset, offset + 32);
|
|
76
|
+
offset += 32;
|
|
77
|
+
const primes = [];
|
|
78
|
+
for (let i = 0; i < 2; i++) {
|
|
79
|
+
const length = (bytes[offset] << 8) | bytes[offset + 1];
|
|
80
|
+
offset += 2;
|
|
81
|
+
primes.push(bytes.slice(offset, offset + length));
|
|
82
|
+
offset += length;
|
|
83
|
+
}
|
|
84
|
+
if (seed.length !== 32 || offset !== bytes.length)
|
|
85
|
+
throw new Error("damaged rescue device keys");
|
|
86
|
+
return { seed, p: primes[0], q: primes[1] };
|
|
87
|
+
}
|
|
88
|
+
/** Rebuild the RSA-OAEP key pair from its two primes. */
|
|
89
|
+
async function rsaFromPrimes(crypto, pBytes, qBytes) {
|
|
90
|
+
const p = bytesToBigInt(pBytes);
|
|
91
|
+
const q = bytesToBigInt(qBytes);
|
|
92
|
+
const n = p * q;
|
|
93
|
+
const d = modInverse(RSA_E, (p - 1n) * (q - 1n));
|
|
94
|
+
const jwk = {
|
|
95
|
+
kty: "RSA",
|
|
96
|
+
alg: "RSA-OAEP-256",
|
|
97
|
+
ext: true,
|
|
98
|
+
n: bytesToBase64Url(bigIntToBytes(n)),
|
|
99
|
+
e: bytesToBase64Url(bigIntToBytes(RSA_E)),
|
|
100
|
+
d: bytesToBase64Url(bigIntToBytes(d)),
|
|
101
|
+
p: bytesToBase64Url(bigIntToBytes(p)),
|
|
102
|
+
q: bytesToBase64Url(bigIntToBytes(q)),
|
|
103
|
+
dp: bytesToBase64Url(bigIntToBytes(d % (p - 1n))),
|
|
104
|
+
dq: bytesToBase64Url(bigIntToBytes(d % (q - 1n))),
|
|
105
|
+
qi: bytesToBase64Url(bigIntToBytes(modInverse(q, p))),
|
|
106
|
+
};
|
|
107
|
+
const subtle = crypto.getSubtle();
|
|
108
|
+
const algorithm = { name: "RSA-OAEP", hash: "SHA-256" };
|
|
109
|
+
const privateKey = await subtle.importKey("jwk", jwk, algorithm, true, ["decrypt"]);
|
|
110
|
+
const publicKey = await subtle.importKey("jwk", { kty: "RSA", alg: "RSA-OAEP-256", ext: true, n: jwk.n, e: jwk.e }, algorithm, true, ["encrypt"]);
|
|
111
|
+
return {
|
|
112
|
+
pkcs8: new Uint8Array(await subtle.exportKey("pkcs8", privateKey)),
|
|
113
|
+
publicKeyPem: (0, fingerprint_1.spkiToPem)(await subtle.exportKey("spki", publicKey)),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** Make a new rescue device and encrypt its private keys with a new secret. */
|
|
117
|
+
async function generateRescueDevice(crypto) {
|
|
118
|
+
const subtle = crypto.getSubtle();
|
|
119
|
+
const seed = crypto.getRandomValues(new Uint8Array(32));
|
|
120
|
+
const signing = await (0, generationSignature_1.ed25519FromSeed)(crypto, seed);
|
|
121
|
+
const pair = (await subtle.generateKey({
|
|
122
|
+
name: "RSA-OAEP",
|
|
123
|
+
modulusLength: 3072,
|
|
124
|
+
publicExponent: new Uint8Array([1, 0, 1]),
|
|
125
|
+
hash: "SHA-256",
|
|
126
|
+
}, true, ["encrypt", "decrypt"]));
|
|
127
|
+
const jwk = (await subtle.exportKey("jwk", pair.privateKey));
|
|
128
|
+
if (!jwk.p || !jwk.q)
|
|
129
|
+
throw new Error("RSA key export did not include the primes");
|
|
130
|
+
const p = base64UrlToBytes(jwk.p);
|
|
131
|
+
const q = base64UrlToBytes(jwk.q);
|
|
132
|
+
// Rebuild from the primes right away, so a key that would not come back on
|
|
133
|
+
// recovery is never certified.
|
|
134
|
+
const rebuilt = await rsaFromPrimes(crypto, p, q);
|
|
135
|
+
const secret = crypto.getRandomValues(new Uint8Array(32));
|
|
136
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
137
|
+
const ciphertext = new Uint8Array(await subtle.encrypt({ name: "AES-GCM", iv: toBuffer(iv), additionalData: toBuffer(BLOB_AAD), tagLength: 128 }, await aesKey(crypto, secret, "encrypt"), toBuffer(encodeRescueKeys(seed, p, q))));
|
|
138
|
+
const packed = new Uint8Array(12 + ciphertext.length);
|
|
139
|
+
packed.set(iv, 0);
|
|
140
|
+
packed.set(ciphertext, 12);
|
|
141
|
+
seed.fill(0);
|
|
142
|
+
return {
|
|
143
|
+
secret,
|
|
144
|
+
blob: (0, fingerprint_1.bytesToBase64)(packed),
|
|
145
|
+
signingPublicKey: signing.publicKeyPem,
|
|
146
|
+
encryptionPublicKey: rebuilt.publicKeyPem,
|
|
147
|
+
signingKeyFingerprint: await (0, fingerprint_1.fingerprintPublicKeyPem)(signing.publicKeyPem, subtle),
|
|
148
|
+
signingPkcs8: signing.pkcs8,
|
|
149
|
+
encryptionPkcs8: rebuilt.pkcs8,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Open a rescue blob with the recovery secret. Throws when the secret is wrong
|
|
154
|
+
* (AES-GCM authenticates it), which is how a bad share combination shows up.
|
|
155
|
+
*/
|
|
156
|
+
async function openRescueDevice(crypto, secret, blob) {
|
|
157
|
+
const packed = (0, fingerprint_1.base64ToBytes)(blob);
|
|
158
|
+
let plain;
|
|
159
|
+
try {
|
|
160
|
+
plain = new Uint8Array(await crypto.getSubtle().decrypt({
|
|
161
|
+
name: "AES-GCM",
|
|
162
|
+
iv: toBuffer(packed.slice(0, 12)),
|
|
163
|
+
additionalData: toBuffer(BLOB_AAD),
|
|
164
|
+
tagLength: 128,
|
|
165
|
+
}, await aesKey(crypto, secret, "decrypt"), toBuffer(packed.slice(12))));
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
throw new Error("The recovery shares do not open this rescue device.");
|
|
169
|
+
}
|
|
170
|
+
const { seed, p, q } = decodeRescueKeys(plain);
|
|
171
|
+
plain.fill(0);
|
|
172
|
+
const signing = await (0, generationSignature_1.ed25519FromSeed)(crypto, seed);
|
|
173
|
+
seed.fill(0);
|
|
174
|
+
const rsa = await rsaFromPrimes(crypto, p, q);
|
|
175
|
+
return {
|
|
176
|
+
signingPublicKey: signing.publicKeyPem,
|
|
177
|
+
encryptionPublicKey: rsa.publicKeyPem,
|
|
178
|
+
signingKeyFingerprint: await (0, fingerprint_1.fingerprintPublicKeyPem)(signing.publicKeyPem, crypto.getSubtle()),
|
|
179
|
+
signingPkcs8: signing.pkcs8,
|
|
180
|
+
encryptionPkcs8: rsa.pkcs8,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* A {@link PrivateUserId} for the rescue device, its private keys encrypted
|
|
185
|
+
* with `password` exactly like {@link BaseMindooTenantFactory.createUserId}
|
|
186
|
+
* does, so the regular discovery and bootstrap calls can act as it.
|
|
187
|
+
*/
|
|
188
|
+
async function rescueDeviceUserId(crypto, device, username, password) {
|
|
189
|
+
return {
|
|
190
|
+
username,
|
|
191
|
+
userSigningKeyPair: {
|
|
192
|
+
publicKey: device.signingPublicKey,
|
|
193
|
+
privateKey: await (0, privateKeyEncryption_1.encryptPrivateKey)(crypto, device.signingPkcs8, password, "signing"),
|
|
194
|
+
},
|
|
195
|
+
userEncryptionKeyPair: {
|
|
196
|
+
publicKey: device.encryptionPublicKey,
|
|
197
|
+
privateKey: await (0, privateKeyEncryption_1.encryptPrivateKey)(crypto, device.encryptionPkcs8, password, "encryption"),
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=rescueDevice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rescueDevice.js","sourceRoot":"","sources":["../../../src/core/recovery/rescueDevice.ts"],"names":[],"mappings":";;AAsKA,oDA2CC;AAMD,4CAmCC;AAOD,gDAiBC;AAjQD,yEAAmE;AACnE,yEAAkE;AAClE,yDAA2G;AAG3G,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,QAAQ,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,2BAA2B,CAAC,CAAC;AACvE,MAAM,KAAK,GAAG,MAAM,CAAC;AAwBrB,SAAS,QAAQ,CAAC,KAAiB;IACjC,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC,UAAU,CAAgB,CAAC;AAClG,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAa;IACrC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IAC/D,OAAO,IAAA,2BAAa,EAAC,UAAU,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACnF,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAiB;IACzC,OAAO,IAAA,2BAAa,EAAC,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;A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AG,MAAM,IAAA,qCAAe,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACpD,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACb,MAAM,GAAG,GAAG,MAAM,aAAa,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IAC9C,OAAO;QACL,gBAAgB,EAAE,OAAO,CAAC,YAAY;QACtC,mBAAmB,EAAE,GAAG,CAAC,YAAY;QACrC,qBAAqB,EAAE,MAAM,IAAA,qCAAuB,EAAC,OAAO,CAAC,YAAY,EAAE,MAAM,CAAC,SAAS,EAAE,CAAC;QAC9F,YAAY,EAAE,OAAO,CAAC,KAAK;QAC3B,eAAe,EAAE,GAAG,CAAC,KAAK;KAC3B,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACI,KAAK,UAAU,kBAAkB,CACtC,MAAqB,EACrB,MAA0B,EAC1B,QAAgB,EAChB,QAAgB;IAEhB,OAAO;QACL,QAAQ;QACR,kBAAkB,EAAE;YAClB,SAAS,EAAE,MAAM,CAAC,gBAAgB;YAClC,UAAU,EAAE,MAAM,IAAA,wCAAiB,EAAC,MAAM,EAAE,MAAM,CAAC,YAAY,EAAE,QAAQ,EAAE,SAAS,CAAC;SACtF;QACD,qBAAqB,EAAE;YACrB,SAAS,EAAE,MAAM,CAAC,mBAAmB;YACrC,UAAU,EAAE,MAAM,IAAA,wCAAiB,EAAC,MAAM,EAAE,MAAM,CAAC,eAAe,EAAE,QAAQ,EAAE,YAAY,CAAC;SAC5F;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shamir's secret sharing over GF(2^8) (docs/userkeys.md §12).
|
|
3
|
+
*
|
|
4
|
+
* Every byte of the secret is the constant term of its own random polynomial
|
|
5
|
+
* of degree `threshold - 1`; share `i` holds the polynomial values at one
|
|
6
|
+
* x-coordinate. Any `threshold` shares give the secret back by Lagrange
|
|
7
|
+
* interpolation at x = 0, fewer reveal nothing about it.
|
|
8
|
+
*
|
|
9
|
+
* Share layout: `[y_0, …, y_(len-1), x]` — the secret's length plus one byte,
|
|
10
|
+
* the x-coordinate last. This is the layout of the audited
|
|
11
|
+
* `shamir-secret-sharing` package (Privy), so shares are interchangeable with
|
|
12
|
+
* it; MindooDB carries its own copy to avoid a runtime dependency on every
|
|
13
|
+
* platform and to draw randomness from the {@link CryptoAdapter}.
|
|
14
|
+
*
|
|
15
|
+
* Arithmetic is in GF(2^8) with the AES reduction polynomial
|
|
16
|
+
* x^8 + x^4 + x^3 + x + 1 (0x11b), using log/exp tables over the generator 3.
|
|
17
|
+
*/
|
|
18
|
+
import type { CryptoAdapter } from "../crypto/CryptoAdapter";
|
|
19
|
+
/**
|
|
20
|
+
* Split `secret` into `shares` shares of which any `threshold` recover it.
|
|
21
|
+
* 2 <= threshold <= shares <= 255.
|
|
22
|
+
*/
|
|
23
|
+
export declare function splitSecret(crypto: CryptoAdapter, secret: Uint8Array, shares: number, threshold: number): Uint8Array[];
|
|
24
|
+
/**
|
|
25
|
+
* Recover the secret from shares made by {@link splitSecret}. Pass at least
|
|
26
|
+
* `threshold` shares; with fewer, the result is a wrong value, not an error —
|
|
27
|
+
* callers must authenticate it (MindooDB opens an AES-GCM blob with it).
|
|
28
|
+
*/
|
|
29
|
+
export declare function combineShares(shares: Uint8Array[]): Uint8Array;
|
|
30
|
+
//# sourceMappingURL=shamir.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shamir.d.ts","sourceRoot":"","sources":["../../../src/core/recovery/shamir.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AA0D7D;;;GAGG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,MAAM,EACd,SAAS,EAAE,MAAM,GAChB,UAAU,EAAE,CA0Bd;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CA4B9D"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.splitSecret = splitSecret;
|
|
4
|
+
exports.combineShares = combineShares;
|
|
5
|
+
const EXP = new Uint8Array(510);
|
|
6
|
+
const LOG = new Uint8Array(256);
|
|
7
|
+
(() => {
|
|
8
|
+
let x = 1;
|
|
9
|
+
for (let i = 0; i < 255; i++) {
|
|
10
|
+
EXP[i] = x;
|
|
11
|
+
LOG[x] = i;
|
|
12
|
+
// multiply by the generator 3: x * 2 xor x, reduced by 0x11b
|
|
13
|
+
let doubled = x << 1;
|
|
14
|
+
if (doubled & 0x100)
|
|
15
|
+
doubled ^= 0x11b;
|
|
16
|
+
x = doubled ^ x;
|
|
17
|
+
}
|
|
18
|
+
for (let i = 255; i < 510; i++)
|
|
19
|
+
EXP[i] = EXP[i - 255];
|
|
20
|
+
})();
|
|
21
|
+
function mul(a, b) {
|
|
22
|
+
if (a === 0 || b === 0)
|
|
23
|
+
return 0;
|
|
24
|
+
return EXP[LOG[a] + LOG[b]];
|
|
25
|
+
}
|
|
26
|
+
function div(a, b) {
|
|
27
|
+
if (b === 0)
|
|
28
|
+
throw new Error("division by zero in GF(256)");
|
|
29
|
+
if (a === 0)
|
|
30
|
+
return 0;
|
|
31
|
+
return EXP[(LOG[a] + 255 - LOG[b]) % 255];
|
|
32
|
+
}
|
|
33
|
+
/** Horner evaluation of `coefficients` (constant term first) at `x`. */
|
|
34
|
+
function evaluate(coefficients, x) {
|
|
35
|
+
let result = 0;
|
|
36
|
+
for (let i = coefficients.length - 1; i >= 0; i--) {
|
|
37
|
+
result = mul(result, x) ^ coefficients[i];
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
/** Distinct non-zero x-coordinates, picked at random. */
|
|
42
|
+
function randomXCoordinates(crypto, count) {
|
|
43
|
+
const pool = [];
|
|
44
|
+
for (let x = 1; x <= 255; x++)
|
|
45
|
+
pool.push(x);
|
|
46
|
+
// Fisher-Yates with rejection sampling, so every order is equally likely.
|
|
47
|
+
const byte = new Uint8Array(1);
|
|
48
|
+
for (let i = pool.length - 1; i > 0; i--) {
|
|
49
|
+
const bound = 256 - (256 % (i + 1));
|
|
50
|
+
let r;
|
|
51
|
+
do {
|
|
52
|
+
crypto.getRandomValues(byte);
|
|
53
|
+
r = byte[0];
|
|
54
|
+
} while (r >= bound);
|
|
55
|
+
const j = r % (i + 1);
|
|
56
|
+
const tmp = pool[i];
|
|
57
|
+
pool[i] = pool[j];
|
|
58
|
+
pool[j] = tmp;
|
|
59
|
+
}
|
|
60
|
+
return pool.slice(0, count);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Split `secret` into `shares` shares of which any `threshold` recover it.
|
|
64
|
+
* 2 <= threshold <= shares <= 255.
|
|
65
|
+
*/
|
|
66
|
+
function splitSecret(crypto, secret, shares, threshold) {
|
|
67
|
+
if (!(secret instanceof Uint8Array) || secret.length === 0) {
|
|
68
|
+
throw new Error("secret must be a non-empty Uint8Array");
|
|
69
|
+
}
|
|
70
|
+
if (!Number.isInteger(shares) || !Number.isInteger(threshold)) {
|
|
71
|
+
throw new Error("shares and threshold must be integers");
|
|
72
|
+
}
|
|
73
|
+
if (threshold < 2 || shares < threshold || shares > 255) {
|
|
74
|
+
throw new Error("need 2 <= threshold <= shares <= 255");
|
|
75
|
+
}
|
|
76
|
+
const xs = randomXCoordinates(crypto, shares);
|
|
77
|
+
const out = xs.map((x) => {
|
|
78
|
+
const share = new Uint8Array(secret.length + 1);
|
|
79
|
+
share[secret.length] = x;
|
|
80
|
+
return share;
|
|
81
|
+
});
|
|
82
|
+
const coefficients = new Uint8Array(threshold);
|
|
83
|
+
for (let byteIndex = 0; byteIndex < secret.length; byteIndex++) {
|
|
84
|
+
coefficients[0] = secret[byteIndex];
|
|
85
|
+
crypto.getRandomValues(coefficients.subarray(1));
|
|
86
|
+
for (let s = 0; s < shares; s++) {
|
|
87
|
+
out[s][byteIndex] = evaluate(coefficients, xs[s]);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
coefficients.fill(0);
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Recover the secret from shares made by {@link splitSecret}. Pass at least
|
|
95
|
+
* `threshold` shares; with fewer, the result is a wrong value, not an error —
|
|
96
|
+
* callers must authenticate it (MindooDB opens an AES-GCM blob with it).
|
|
97
|
+
*/
|
|
98
|
+
function combineShares(shares) {
|
|
99
|
+
if (shares.length < 2)
|
|
100
|
+
throw new Error("need at least two shares");
|
|
101
|
+
const length = shares[0].length;
|
|
102
|
+
if (length < 2)
|
|
103
|
+
throw new Error("share too short");
|
|
104
|
+
const xs = [];
|
|
105
|
+
for (const share of shares) {
|
|
106
|
+
if (share.length !== length)
|
|
107
|
+
throw new Error("shares have different lengths");
|
|
108
|
+
const x = share[length - 1];
|
|
109
|
+
if (x === 0)
|
|
110
|
+
throw new Error("invalid share x-coordinate");
|
|
111
|
+
if (xs.includes(x))
|
|
112
|
+
throw new Error("duplicate share");
|
|
113
|
+
xs.push(x);
|
|
114
|
+
}
|
|
115
|
+
const secret = new Uint8Array(length - 1);
|
|
116
|
+
for (let byteIndex = 0; byteIndex < length - 1; byteIndex++) {
|
|
117
|
+
let value = 0;
|
|
118
|
+
for (let i = 0; i < shares.length; i++) {
|
|
119
|
+
// Lagrange basis polynomial i at x = 0: prod_{j != i} x_j / (x_j - x_i);
|
|
120
|
+
// subtraction is xor in GF(2^8).
|
|
121
|
+
let basis = 1;
|
|
122
|
+
for (let j = 0; j < shares.length; j++) {
|
|
123
|
+
if (i === j)
|
|
124
|
+
continue;
|
|
125
|
+
basis = mul(basis, div(xs[j], xs[j] ^ xs[i]));
|
|
126
|
+
}
|
|
127
|
+
value ^= mul(shares[i][byteIndex], basis);
|
|
128
|
+
}
|
|
129
|
+
secret[byteIndex] = value;
|
|
130
|
+
}
|
|
131
|
+
return secret;
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=shamir.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shamir.js","sourceRoot":"","sources":["../../../src/core/recovery/shamir.ts"],"names":[],"mappings":";;AA+EA,kCA+BC;AAOD,sCA4BC;AA9HD,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAChC,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAChC,CAAC,GAAG,EAAE;IACJ,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7B,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACX,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACX,6DAA6D;QAC7D,IAAI,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC;QACrB,IAAI,OAAO,GAAG,KAAK;YAAE,OAAO,IAAI,KAAK,CAAC;QACtC,CAAC,GAAG,OAAO,GAAG,CAAC,CAAC;IAClB,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE;QAAE,GAAG,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAE,CAAC;AACzD,CAAC,CAAC,EAAE,CAAC;AAEL,SAAS,GAAG,CAAC,CAAS,EAAE,CAAS;IAC/B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IACjC,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAE,GAAG,GAAG,CAAC,CAAC,CAAE,CAAE,CAAC;AACjC,CAAC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 @@ export interface ConnectToServerOptions {
|
|
|
690
690
|
irohTicket?: string;
|
|
691
691
|
/** Byte-stream adapter used instead of HTTP. */
|
|
692
692
|
irohStreamIO?: import("./appendonlystores/network/IrohStreamIO").IrohStreamIO;
|
|
693
|
+
/**
|
|
694
|
+
* Authenticate as another key pair than the current user's device, e.g. an
|
|
695
|
+
* admin key for the admin-only `joinrequests` database (docs/invitations.md).
|
|
696
|
+
* The server resolves the principal from `signingPublicKey`.
|
|
697
|
+
*/
|
|
698
|
+
authAs?: {
|
|
699
|
+
signingPublicKey: string;
|
|
700
|
+
signingKey: CryptoKey;
|
|
701
|
+
encryptionKey: CryptoKey;
|
|
702
|
+
/** Name sent with the challenge; the server falls back to the key. */
|
|
703
|
+
username?: string;
|
|
704
|
+
};
|
|
693
705
|
}
|
|
694
706
|
/**
|
|
695
707
|
* A TenantFactory is a factory for creating and managing tenants.
|
|
@@ -960,7 +972,9 @@ export interface MindooTenant {
|
|
|
960
972
|
* @param encryptedData The entry's encrypted payload bytes
|
|
961
973
|
* @return True if the entry is authentic and intact, false otherwise
|
|
962
974
|
*/
|
|
963
|
-
verifyEntrySignature(entry: StoreEntryMetadata, encryptedData: Uint8Array
|
|
975
|
+
verifyEntrySignature(entry: StoreEntryMetadata, encryptedData: Uint8Array, options?: {
|
|
976
|
+
dbId?: string;
|
|
977
|
+
}): Promise<boolean>;
|
|
964
978
|
/**
|
|
965
979
|
* Method to open the directory for this tenant
|
|
966
980
|
*
|
|
@@ -1179,6 +1193,43 @@ export interface MindooTenant {
|
|
|
1179
1193
|
fingerprint: string;
|
|
1180
1194
|
certificate: import("./userkeys/deviceCertificate").DeviceCertificatePayload;
|
|
1181
1195
|
}>>;
|
|
1196
|
+
/**
|
|
1197
|
+
* Name recovery contacts (docs/userkeys.md §12): any two of them can later
|
|
1198
|
+
* help this person onto a new device. Needs device self-grant. Replaces an
|
|
1199
|
+
* earlier setup and withdraws its rescue device.
|
|
1200
|
+
*/
|
|
1201
|
+
setupRecoveryContacts?(contacts: string[], options?: import("./recovery/RecoveryContactsManager").SetupRecoveryContactsOptions): Promise<import("./recovery/RecoveryContactsDocument").RecoveryContactsSetup>;
|
|
1202
|
+
/** This person's current recovery contacts, or `null`. */
|
|
1203
|
+
getRecoveryContactsSetup?(): Promise<import("./recovery/RecoveryContactsDocument").RecoveryContactsSetup | null>;
|
|
1204
|
+
/** Remove the recovery contacts and withdraw the rescue device. */
|
|
1205
|
+
removeRecoveryContacts?(options?: {
|
|
1206
|
+
withdrawRescueDevices?: string[];
|
|
1207
|
+
}): Promise<number>;
|
|
1208
|
+
/** People who named this person as a recovery contact. */
|
|
1209
|
+
listRecoveryContactRequests?(): Promise<import("./recovery/RecoveryContactsManager").RecoveryContactRequest[]>;
|
|
1210
|
+
/** This contact's share for `person` (username or setup id), as a plaintext package. */
|
|
1211
|
+
createRecoveryHelperPackage?(person: string, options?: {
|
|
1212
|
+
serverUrl?: string;
|
|
1213
|
+
}): Promise<import("./recovery/recoveryPackage").RecoveryHelperPackage>;
|
|
1214
|
+
/** Whether this device holds the granted self-grant key (docs/userkeys.md §9.6). */
|
|
1215
|
+
canIssueDeviceCertificates?(): Promise<boolean>;
|
|
1216
|
+
/** Signing keys of every verified rescue device in the tenant, by person. */
|
|
1217
|
+
listRescueDeviceSigningKeys?(): Promise<Array<{
|
|
1218
|
+
username: string;
|
|
1219
|
+
signingPublicKey: string;
|
|
1220
|
+
}>>;
|
|
1221
|
+
/**
|
|
1222
|
+
* A new paper rescue device for a printed recovery sheet (docs/userkeys.md
|
|
1223
|
+
* §12.8): certified like any device, its secret goes onto the sheet. Earlier
|
|
1224
|
+
* paper rescue devices of this person are withdrawn. Needs device self-grant.
|
|
1225
|
+
*/
|
|
1226
|
+
createPaperRescueDevice?(options?: {
|
|
1227
|
+
label?: string;
|
|
1228
|
+
}): Promise<import("./recovery/PaperRescueDevice").PaperRescueSecret>;
|
|
1229
|
+
/** This person's paper rescue devices (public half). */
|
|
1230
|
+
listPaperRescueDevices?(): Promise<import("./recovery/PaperRescueDevice").PaperRescueRecord[]>;
|
|
1231
|
+
/** Withdraw every paper rescue device of this person. */
|
|
1232
|
+
removePaperRescueDevices?(): Promise<number>;
|
|
1182
1233
|
reconcileUserKeys?(options?: {
|
|
1183
1234
|
allowSelfCreate?: boolean;
|
|
1184
1235
|
}): Promise<import("./userkeys/types").UserKeyEnrollmentStatus>;
|
|
@@ -3323,6 +3374,18 @@ export interface MindooTenantDirectory {
|
|
|
3323
3374
|
* @returns True when the key may act as an administrator at `T`.
|
|
3324
3375
|
*/
|
|
3325
3376
|
isAdminAt?(signingKey: string, T: number): Promise<boolean>;
|
|
3377
|
+
/**
|
|
3378
|
+
* Whether the signer of a stored entry counts as a tenant admin for that
|
|
3379
|
+
* entry (docs/accesscontrol.md §6.13): the root always; a delegated admin key
|
|
3380
|
+
* when it was an admin at the entry's witness time (the receipt is
|
|
3381
|
+
* verified), so what it signed inside its window stays valid after a
|
|
3382
|
+
* deactivation and an entry stamped in a gap never counts. An entry without a
|
|
3383
|
+
* receipt counts only when this process signed it and the key is an admin now.
|
|
3384
|
+
*
|
|
3385
|
+
* @param entry Metadata of the entry, with its witness receipt fields.
|
|
3386
|
+
* @param dbid Database the entry belongs to (part of the signed receipt).
|
|
3387
|
+
*/
|
|
3388
|
+
isAdminSignedEntry?(entry: StoreEntryMetadata, dbid: string): Promise<boolean>;
|
|
3326
3389
|
/**
|
|
3327
3390
|
* The delegated admin keys at the directory head, with window, name and
|
|
3328
3391
|
* status at "now" (§6.13). The root admin key is not listed.
|