-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsigTool.js
255 lines (209 loc) · 6.93 KB
/
sigTool.js
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
246
247
248
249
250
251
252
253
254
255
/** sigTool -- signing key generation, storage, and usage
@flow strict
*/
/* global unescape, encodeURIComponent, HTMLInputElement, HTMLTextAreaElement */
import { asStr } from './messageBus.js';
const def = Object.freeze;
/*::
// SigningKey is the format we use to save the key pair
// with the secret key encrypted.
export type SigningKey = {
label: string,
secretKey: {
// ISSUE: opaque type for hex?
nonce: string,
cipherText: string,
},
pubKey: string
}
interface SigTool {
// Generate and save key.
generate({ label: string, password: string }): Promise<SigningKey>,
// Get stored key.
getKey(): Promise<SigningKey | null>,
// Decrypt private key and use it to sign message.
signMessage(message: Uint8Array, signingKey: SigningKey, password: string): string
}
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageArea
interface StorageArea {
get(key: string): Promise<{ [string]: mixed }>,
set(items: { [string]: mixed }): Promise<void>
}
// Toward a portable chrome/firefox API (WIP).
export type UserAgent = {
chrome: typeof chrome,
browser?: {
storage: {
local: StorageArea
}
}
}
*/
export function options(document /*: Document*/, ua /*: UserAgent*/, nacl /*: typeof nacl*/) {
function the/*:: <T>*/(x /*: ?T*/) /*: T*/ { if (!x) { throw new Error(x); } return x; }
const byId = id => the(document.getElementById(id));
function lose(doing, exc /*: Error */) {
byId('status').textContent = `failed ${doing}: ${exc.message}`;
console.log(exc);
}
document.addEventListener('DOMContentLoaded', () => {
const tool = sigTool(localStorage(ua), nacl);
tool.getKey()
.then(showPubKey)
.catch(oops => lose('get key', oops));
byId('save').addEventListener('click', () => {
byId('status').textContent = '';
tool.generate({
label: input(byId('label')).value,
password: input(byId('password')).value,
})
.then(showPubKey)
.catch(oops => lose('generate key', oops));
});
});
function showPubKey(maybeKey /*: SigningKey | null*/) {
if (!maybeKey) { return; }
const { label, pubKey } = maybeKey;
/* Assigning to params is the norm for DOM stuff. */
/* eslint-disable no-param-reassign */
input(byId('label')).value = label;
input(byId('pubKey')).value = pubKey;
}
}
/**
* Runtime check that this element is an input.
*/
export function input(elt /*: HTMLElement*/) /*: HTMLInputElement | HTMLTextAreaElement */ {
if (!(elt instanceof HTMLInputElement || elt instanceof HTMLTextAreaElement)) {
throw new TypeError(`not an input: ${elt.toString()}`);
}
return elt;
}
/**
* Normalize local storage API
*
* Produce promise-style API despite [chrome compatibility issues][1].
*
* [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities
*/
export function localStorage({ browser, chrome } /*: UserAgent*/) /*: StorageArea */{
return browser ? browser.storage.local : def({
set: items => asPromise(chrome, callback => chrome.storage.local.set(items, callback)),
get: key => asPromise(chrome, callback => chrome.storage.local.get(key, callback)),
});
}
export function sigTool(local /*: StorageArea */, nacl /*: typeof nacl*/) /*: SigTool */ {
function getKey() /*: Promise<SigningKey | null> */{
return local.get('signingKey').then(({ signingKey }) => chkKey(signingKey));
}
function chkKey(it /*: mixed*/) /*: SigningKey | null */ {
if (it === null) { return null; }
if (typeof it !== 'object') { return null; }
const { secretKey } = it;
if (!secretKey || typeof secretKey !== 'object') { return null; }
const { nonce } = secretKey;
if (typeof nonce !== 'string') { return null; }
const { cipherText } = secretKey;
if (typeof cipherText !== 'string') { return null; }
return {
label: asStr(it.label),
secretKey: { nonce, cipherText },
pubKey: asStr(it.pubKey),
};
}
function generate({ label, password }) {
const signingKey = encryptedKey(nacl.sign.keyPair(), { label, password });
return local.set({ signingKey }).then(() => signingKey);
}
function encryptedKey(keyPair, { label, password }) {
const sk = encryptWithNonce(keyPair.secretKey, passKey(password));
return {
label,
secretKey: {
nonce: b2h(sk.nonce),
cipherText: b2h(sk.cipherText),
},
pubKey: b2h(keyPair.publicKey),
};
}
/**
* Hash text password to get bytes for secretbox key.
*/
function passKey(password /*: string*/) /*: Uint8Array */{
return nacl.hash(utf8(password)).slice(0, nacl.secretbox.keyLength);
}
function encryptWithNonce(message /*: Uint8Array */, key) {
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
const cipherText = nacl.secretbox(message, nonce, key);
return { cipherText, nonce };
}
function signMessage(
message /*: Uint8Array */,
signingKey /*: SigningKey*/,
password /*: string*/,
) {
const nonce = h2b(signingKey.secretKey.nonce);
const box = h2b(signingKey.secretKey.cipherText);
const secretKey = nacl.secretbox.open(box, nonce, passKey(password));
if (secretKey === null) {
throw new Error('bad password');
}
return b2h(nacl.sign.detached(message, secretKey));
}
return def({ getKey, generate, signMessage });
}
/**
* Adapt callback-style API using Promises.
*
* Instead of obj.method(...arg, callback),
* use send(cb => obj.method(...arg, cb)) and get a promise.
*
* ref https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Chrome_incompatibilities
* https://github.com/mdn/webextensions-examples/issues/194
*
* @param calling: a function of the form (cb) => o.m(..., cb)
* @return A promise for the result passed to cb
*/
function asPromise/*:: <T>*/(
chrome /*: typeof chrome*/,
calling /*: (cb: (T) => void) => mixed */,
) /*: Promise<T> */{
function executor(resolve, reject) {
function callback(result /*: T*/) {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
return;
}
resolve(result);
}
calling(callback);
}
return new Promise(executor);
}
function utf8(s /*: string*/) /*: Uint8Array*/ {
const byteChars = unescape(encodeURIComponent(s));
return Uint8Array.from([...byteChars].map(ch => ch.charCodeAt(0)));
}
// ack: https://gist.github.com/tauzen/3d18825ae41ff3fc8981
function b2h(uint8arr /*: Uint8Array*/) /*: string */{
if (!uint8arr) {
return '';
}
let hexStr = '';
for (let i = 0; i < uint8arr.length; i += 1) {
let hex = (uint8arr[i] & 0xff).toString(16); // eslint-disable-line no-bitwise
hex = (hex.length === 1) ? `0${hex}` : hex;
hexStr += hex;
}
return hexStr;
}
function h2b(str) {
if (!str) {
return new Uint8Array([]);
}
const a = [];
for (let i = 0, len = str.length; i < len; i += 2) {
a.push(parseInt(str.substr(i, 2), 16));
}
return new Uint8Array(a);
}