This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
krpairing.ts
245 lines (215 loc) · 6.43 KB
/
krpairing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import * as qr from 'qr-image';
import * as platform from 'platform';
import {stringify, parse } from './krjson';
import { from_base64, to_base64, randombytes_buf, crypto_box_keypair, crypto_hash_sha256, crypto_box_seal_open, crypto_box_open_easy, crypto_box_easy, to_base64_url} from './crypto';
import {receive, send, createQueues} from './krtransport';
import $ from 'jquery';
import {get, set} from './storage';
import bytesToUuid from './uuid';
import { crypto_box_NONCEBYTES, KeyPair } from 'libsodium-wrappers-sumo';
import { Type, Transform } from 'class-transformer';
import { TransformationType } from 'class-transformer/TransformOperationExecutor';
import textEncoding from 'text-encoding';
import { Request, MeRequest } from './protocol';
import { browser } from './browser';
export const VERSION = "2.4.4";
const CIPHERTEXT = 0;
const WRAPPED_PUBLIC_KEY = 2;
export class Pairing {
@Transform(v => {
let ret: KeyPair = {
keyType: v.keyType,
privateKey: from_base64(v.privateKey),
publicKey: from_base64(v.publicKey),
};
return ret;
})
keyPair: KeyPair;
@Transform(v => from_base64(v))
workstationDeviceIdentifier: Uint8Array;
creationTimestamp: number;
@Transform(v => v && from_base64(v))
enclavePublicKey?: Uint8Array;
workstationName: string;
snsEndpointArn?: string;
trackingId?: string;
@Transform(v => v && from_base64(v))
enclaveDeviceIdentifier?: Uint8Array;
enclaveEmail?: string;
version: string;
u2fAccounts: Array<string> = [];
static async loadOrGenerate() : Promise<Pairing> {
return await get('pairing')
.then(data => {
if (!!data) {
return data;
}
throw 'no pairing';
})
.then(pairingJson => parse(Pairing, pairingJson))
.then(pairing => {
if (!pairing.isPaired() && (Date.now() - pairing.creationTimestamp > 60*1000) ) {
console.log("Keypair expired - generating a new one");
return Pairing.generateAndSave();
}
return pairing;
})
.catch(e => {
console.info('Failed to load pairing: ' + e);
return Pairing.generateAndSave();
});
}
static async getDeviceIdentifier() : Promise<Uint8Array> {
return get('workstationDeviceIdentifier')
.then(async d => {
if (!d) {
d = await to_base64(await randombytes_buf(16));
await set('workstationDeviceIdentifier', d);
}
return d;
})
.then(from_base64);
}
static async generateAndSave() : Promise<Pairing> {
let pairing = new Pairing();
pairing.creationTimestamp = Date.now();
pairing.version = VERSION;
pairing.workstationDeviceIdentifier = await Pairing.getDeviceIdentifier();
pairing.workstationName = platform.name + " " + platform.os.family;
pairing.keyPair = await crypto_box_keypair();
await pairing.save();
createQueues(pairing);
return pairing;
}
static async deleteAndRegenerate() : Promise<Pairing> {
return set('pairing', null).then(_ => {
return Pairing.generateAndSave()
});
}
async save() {
return set('pairing', await stringify(this));
}
toQR() {
return new PairingQR(this.keyPair.publicKey, this.workstationName, this.version, this.workstationDeviceIdentifier);
}
async uuid() {
let h = await crypto_hash_sha256(this.keyPair.publicKey);
return bytesToUuid(h.slice(0, 16)).toUpperCase();
}
isPaired() : boolean {
return !!this.enclaveDeviceIdentifier;
}
async sendQueueName() {
return await this.uuid();
}
async recvQueueName() {
return await this.uuid() + '-responder';
}
async recv(onMessage: Function) {
let msgs = await receive(this);
if (msgs.Messages && msgs.Messages.length > 0) {
for (var i = 0; i < msgs.Messages.length; i++) {
onMessage(
this,
from_base64(msgs.Messages[i].Body)
)
}
}
}
// Ignore serialized values
@Transform(v => 0)
pollUntil = 0;
@Transform(v => false)
polling = false;
async pollFor(ms: number, onMessage: Function) {
this.pollUntil = Math.max(this.pollUntil, Date.now() + ms);
if (this.polling) {
return;
}
this.polling = true;
while (Date.now() < this.pollUntil) {
await this.recv(onMessage);
}
this.polling = false;
}
async decrypt(c: Uint8Array) {
if (c.length == 0) {
throw new Error('0-length ciphertext');
}
let header = c[0];
let body = c.slice(1);
if (header == WRAPPED_PUBLIC_KEY) {
if (this.enclaveDeviceIdentifier) {
throw new Error('Already paired');
}
let unwrappedKey = await crypto_box_seal_open(body, this.keyPair.publicKey, this.keyPair.privateKey);
if (!unwrappedKey) {
throw new Error('Unwrap key failed');
}
if (this.enclavePublicKey && unwrappedKey != this.enclavePublicKey) {
throw new Error('Already sent confirmation for a different key');
}
this.enclavePublicKey = unwrappedKey;
await this.save();
let meRequest = await Request.make();
meRequest.me_request = new MeRequest();
meRequest.me_request.u2f_only = true;
stringify(meRequest)
this.encryptAndSend(stringify(meRequest));
return null;
} else if (header == CIPHERTEXT) {
if (body.length < crypto_box_NONCEBYTES) {
throw new Error('Ciphertext shorter than nonce');
}
if (!this.enclavePublicKey) {
throw 'Not yet paired';
}
let plaintext = await crypto_box_open_easy(
body.slice(crypto_box_NONCEBYTES),
body.slice(0, crypto_box_NONCEBYTES),
this.enclavePublicKey,
this.keyPair.privateKey,
);
if (!plaintext) {
throw new Error('Decryption failed');
}
return new textEncoding.TextDecoder().decode(plaintext);
}
throw new Error('Invalid message header');
}
async encryptAndSend(message: string) {
if (!this.enclavePublicKey) {
throw 'Not yet paired';
}
let nonce = await randombytes_buf(crypto_box_NONCEBYTES);
let ciphertext = await crypto_box_easy(message, nonce,
this.enclavePublicKey, this.keyPair.privateKey);
let finalCiphertext = new Uint8Array([CIPHERTEXT, ...nonce, ...ciphertext]);
return this.send(finalCiphertext);
}
send(msg: Uint8Array) {
let self = this;
to_base64(msg).then(s => send(self, s));
}
}
export class PairingQR {
@Transform(v => from_base64(v))
pk: Uint8Array;
n: string;
v: string;
@Transform(v => from_base64(v))
d: Uint8Array;
b: string;
constructor(pk: Uint8Array, n: string, v: string, d: Uint8Array) {
this.pk = pk;
this.n = n;
this.v = v;
this.d = d;
this.b = browser();
}
async render() {
let payload = await stringify(this);
let url = 'https://get.krypt.co/#' + await to_base64_url(payload);
return qr.imageSync(url, { type: 'svg', ec_level: 'L' });
}
}