@sendsafely/sendsafely 1.1.1 → 1.1.3

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.
@@ -1,287 +1,287 @@
1
- if(typeof window === 'undefined') {
2
- window = {};
3
- }
4
-
5
- window.crypto = {
6
- getRandomValues: function (buf) {
7
- for(var i = 0; i<buf.length; i++)
8
- {
9
- if((self.randomCounter%20) === 0) {
10
- reportProgress(self.randomCounter);
11
- }
12
- if((self.randomCounter % 512) === 0)
13
- {
14
- self.send({'cmd': 'randBuff', 'bytes': 64});
15
- }
16
- buf[i] = self.randomness[self.randomCounter++].charCodeAt();
17
- }
18
- }
19
- };
20
-
21
- self.randomCounter = 0;
22
-
23
- self.addEventListener('message', async function(e) {
24
- var data = e.data;
25
- switch (data.cmd) {
26
- case 'generate_key':
27
- debug("Starting to generate key..");
28
- self.randomCounter = 0;
29
- self.randomness = data.randomness;
30
-
31
- var bits = data.bits;
32
- var userStr = data.userStr;
33
-
34
-
35
- var options =
36
- {
37
- userIds: [userStr], // multiple user IDs
38
- numBits: 2048
39
- };
40
-
41
- openpgp.generateKey(options).then(function(key) {
42
- var privateKey = key.privateKeyArmored;
43
- var publicKey = key.publicKeyArmored;
44
-
45
- self.send({'cmd': 'key_generated', 'privateKey': privateKey, 'publicKey': publicKey});
46
- }, function(err) {
47
- debug("An Unknown Error Occurred While Generating the key");
48
- debug(err);
49
- });
50
- break;
51
- case 'convert_key':
52
- self.randomCounter = 0;
53
- self.randomness = data.randomness;
54
- var result = convertRawKey(data.rsaKeys, data.userStr);
55
- self.send({'cmd': 'key_converted', 'privateKey': result.privateKeyArmored, 'publicKey': result.publicKeyArmored});
56
- break;
57
- case 'encrypt_keycode':
58
- self.randomCounter = 0;
59
- self.randomness = data.randomness;
60
-
61
- debug(data.publicKey);
62
- var publicKeys = await openpgp.key.readArmored(data.publicKey);
63
-
64
- if(publicKeys.keys.length > 0) {
65
- var pubKey = publicKeys.keys[0];
66
-
67
- var options = {
68
- message: openpgp.message.fromText(data.keyCode),
69
- publicKeys: [pubKey]
70
- }
71
-
72
- openpgp.encrypt(options).then(function(encryptedMessage) {
73
- self.send({'cmd': 'keycode_encrypted', 'encryptedKeyCode': encryptedMessage.data});
74
- }, function(err) {
75
- debug('ERROR');
76
- debug(err);
77
- });
78
- }
79
- break;
80
- case 'decrypt_keycode':
81
- self.randomCounter = 0;
82
- self.randomness = data.randomness;
83
- var privKeys = await openpgp.key.readArmored(data.privateKey);
84
- privKey = privKeys.keys[0];
85
- var message = await openpgp.message.readArmored(data.keyCode);
86
-
87
- options =
88
- {
89
- message: message, // parse armored message
90
- privateKeys: [privKey] // for decryption
91
- };
92
-
93
- openpgp.decrypt(options).then(function(plaintext) {
94
- self.send({'cmd': 'keycode_decrypted', 'decryptedKeycode': plaintext.data});
95
- try {
96
- throw new Error("Hello");
97
- } catch(err) {
98
- sendError(err);
99
- }
100
- }, function(err) {
101
- debug('ERROR');
102
- debug(err);
103
- sendError(err);
104
- });
105
- break;
106
- case 'randBuff':
107
- self.randomness += data.randomness;
108
- default:
109
- ;
110
- };
111
- }, false);
112
-
113
- function convertRawKey(rsaKeys, userStr) {
114
-
115
- var secretKey = createSecretKey(rsaKeys.privateKey);
116
- var secretSubKey = createSecretSubKey(rsaKeys.privateSubKey);
117
-
118
- var key = wrapKeyObject(userStr, secretKey, secretSubKey);
119
-
120
- var result = {};
121
- result.privateKeyArmored = key.armor();
122
- result.publicKeyArmored = key.toPublic().armor();
123
-
124
- return result;
125
- }
126
-
127
- function wrapKeyObject(userId, secretKeyPacket, secretSubkeyPacket)
128
- {
129
- packetlist = new window.openpgp.packet.List();
130
-
131
- userIdPacket = new window.openpgp.packet.Userid();
132
- userIdPacket.read(userId);
133
-
134
- dataToSign = {};
135
- dataToSign.userid = userIdPacket;
136
- dataToSign.key = secretKeyPacket;
137
- signaturePacket = new window.openpgp.packet.Signature();
138
- signaturePacket.signatureType = window.openpgp.enums.signature.cert_generic;
139
- signaturePacket.publicKeyAlgorithm = window.openpgp.enums.publicKey.rsa_encrypt_sign;
140
- signaturePacket.hashAlgorithm = window.openpgp.config.prefer_hash_algorithm;
141
- signaturePacket.keyFlags = [window.openpgp.enums.keyFlags.certify_keys | window.openpgp.enums.keyFlags.sign_data];
142
- signaturePacket.preferredSymmetricAlgorithms = [];
143
- signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes256);
144
- signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes192);
145
- signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes128);
146
- signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.cast5);
147
- signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.tripledes);
148
- signaturePacket.preferredHashAlgorithms = [];
149
- signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha256);
150
- signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha1);
151
- signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha512);
152
- signaturePacket.preferredCompressionAlgorithms = [];
153
- signaturePacket.preferredCompressionAlgorithms.push(window.openpgp.enums.compression.zlib);
154
- signaturePacket.preferredCompressionAlgorithms.push(window.openpgp.enums.compression.zip);
155
- if (window.openpgp.config.integrity_protect) {
156
- signaturePacket.features = [];
157
- signaturePacket.features.push(1); // Modification Detection
158
- }
159
- signaturePacket.sign(secretKeyPacket, dataToSign);
160
-
161
- dataToSign = {};
162
- dataToSign.key = secretKeyPacket;
163
- dataToSign.bind = secretSubkeyPacket;
164
- subkeySignaturePacket = new window.openpgp.packet.Signature();
165
- subkeySignaturePacket.signatureType = window.openpgp.enums.signature.subkey_binding;
166
- subkeySignaturePacket.publicKeyAlgorithm = window.openpgp.enums.publicKey.rsa_encrypt_sign;
167
- subkeySignaturePacket.hashAlgorithm = window.openpgp.config.prefer_hash_algorithm;
168
- subkeySignaturePacket.keyFlags = [window.openpgp.enums.keyFlags.encrypt_communication | window.openpgp.enums.keyFlags.encrypt_storage];
169
- subkeySignaturePacket.sign(secretKeyPacket, dataToSign);
170
-
171
- packetlist.push(secretKeyPacket);
172
- packetlist.push(userIdPacket);
173
- packetlist.push(signaturePacket);
174
- packetlist.push(secretSubkeyPacket);
175
- packetlist.push(subkeySignaturePacket);
176
-
177
- return new window.openpgp.key.Key(packetlist);
178
- }
179
-
180
- function createSecretKey(key)
181
- {
182
- var mpiList = createMPIList(key);
183
- var packet = createSecretKeyPacketFromList(mpiList, window.openpgp.packet.SecretKey);
184
- return packet;
185
- }
186
-
187
- function createSecretSubKey(key)
188
- {
189
- var mpiList = createMPIList(key);
190
- var packet = createSecretKeyPacketFromList(mpiList, window.openpgp.packet.SecretSubkey);
191
- return packet;
192
- }
193
-
194
- function createSecretKeyPacketFromList(mpiList, PacketType)
195
- {
196
- var secretKeyPacket = new PacketType();
197
- secretKeyPacket.mpi = mpiList;
198
- secretKeyPacket.isDecrypted = true;
199
- secretKeyPacket.algorithm = window.openpgp.enums.read(window.openpgp.enums.publicKey, window.openpgp.enums.publicKey.rsa_encrypt_sign);
200
- return secretKeyPacket;
201
- }
202
-
203
- function createMPIList(privateKey)
204
- {
205
- var p = createMPI(privateKey.p.value, privateKey.p.radix);
206
- var q = createMPI(privateKey.q.value, privateKey.q.radix);
207
- var u = p.data.modInverse(q.data);
208
-
209
- var mpiList = [];
210
- mpiList.push(createMPI(privateKey.n.value, privateKey.n.radix)); // n
211
- mpiList.push(createMPI("10001", 16)); // e
212
- mpiList.push(createMPI(privateKey.d.value, privateKey.d.radix)); // d
213
- mpiList.push(p); // p
214
- mpiList.push(q); // q
215
- mpiList.push(createMPIFromBI(u));
216
-
217
- return mpiList;
218
- }
219
-
220
- function createMPI(value, radix)
221
- {
222
- var BigInteger = window.openpgp.crypto.publicKey.jsbn;
223
- var bn = new BigInteger(value, radix);
224
- return createMPIFromBI(bn);
225
- }
226
-
227
- function createMPIFromBI(bigIntegeger) {
228
- var mpi = new window.openpgp.MPI();
229
- mpi.fromBigInteger(bigIntegeger);
230
- return mpi;
231
- }
232
-
233
- function reportProgress(progress) {
234
- var TOTAL = 515;
235
- self.send({cmd: 'progress', progress: progress, total: TOTAL});
236
- }
237
-
238
- function sendError(err) {
239
- var stacktraceStr = "Stacktrace could not be extracted";
240
- if(err !== undefined && err.stack !== undefined) {
241
- stacktraceStr = err.stack;
242
- }
243
-
244
- var msg = err.message;
245
-
246
- self.send({cmd: 'error', stacktrace: stacktraceStr, message: msg});
247
- }
248
-
249
- function SecureRandom() {
250
- function nextBytes(byteArray) {
251
- for (var n = 0; n < byteArray.length; n++) {
252
- byteArray[n] = window.openpgp.crypto.random.getSecureRandomOctet();
253
- }
254
- }
255
- this.nextBytes = nextBytes;
256
- }
257
-
258
- function debug(msg) {
259
- self.log += msg + "\n";
260
- self.send({'cmd': 'debug', 'msg': msg});
261
- }
262
-
263
- function send(content) {
264
- if(self.postMessage != undefined) {
265
- self.postMessage(content);
266
- } else {
267
- postMessage(content);
268
- }
269
- }
270
-
271
- function execute(cmd, errMsg) {
272
- // wrap the sync cmd in a promise
273
- var promise = new Promise(function(resolve) {
274
- var result = cmd();
275
- resolve(result);
276
- });
277
-
278
- // handler error globally
279
- return promise.catch(onError.bind(null, errMsg));
280
- }
281
-
282
- function onError(message, error) {
283
- // log the stack trace
284
- console.error(error.stack);
285
- // rethrow new high level error for api users
286
- throw new Error(message);
1
+ if(typeof window === 'undefined') {
2
+ window = {};
3
+ }
4
+
5
+ window.crypto = {
6
+ getRandomValues: function (buf) {
7
+ for(var i = 0; i<buf.length; i++)
8
+ {
9
+ if((self.randomCounter%20) === 0) {
10
+ reportProgress(self.randomCounter);
11
+ }
12
+ if((self.randomCounter % 512) === 0)
13
+ {
14
+ self.send({'cmd': 'randBuff', 'bytes': 64});
15
+ }
16
+ buf[i] = self.randomness[self.randomCounter++].charCodeAt();
17
+ }
18
+ }
19
+ };
20
+
21
+ self.randomCounter = 0;
22
+
23
+ self.addEventListener('message', async function(e) {
24
+ var data = e.data;
25
+ switch (data.cmd) {
26
+ case 'generate_key':
27
+ debug("Starting to generate key..");
28
+ self.randomCounter = 0;
29
+ self.randomness = data.randomness;
30
+
31
+ var bits = data.bits;
32
+ var userStr = data.userStr;
33
+
34
+
35
+ var options =
36
+ {
37
+ userIds: [userStr], // multiple user IDs
38
+ numBits: 2048
39
+ };
40
+
41
+ openpgp.generateKey(options).then(function(key) {
42
+ var privateKey = key.privateKeyArmored;
43
+ var publicKey = key.publicKeyArmored;
44
+
45
+ self.send({'cmd': 'key_generated', 'privateKey': privateKey, 'publicKey': publicKey});
46
+ }, function(err) {
47
+ debug("An Unknown Error Occurred While Generating the key");
48
+ debug(err);
49
+ });
50
+ break;
51
+ case 'convert_key':
52
+ self.randomCounter = 0;
53
+ self.randomness = data.randomness;
54
+ var result = convertRawKey(data.rsaKeys, data.userStr);
55
+ self.send({'cmd': 'key_converted', 'privateKey': result.privateKeyArmored, 'publicKey': result.publicKeyArmored});
56
+ break;
57
+ case 'encrypt_keycode':
58
+ self.randomCounter = 0;
59
+ self.randomness = data.randomness;
60
+
61
+ debug(data.publicKey);
62
+ var publicKeys = await openpgp.key.readArmored(data.publicKey);
63
+
64
+ if(publicKeys.keys.length > 0) {
65
+ var pubKey = publicKeys.keys[0];
66
+
67
+ var options = {
68
+ message: openpgp.message.fromText(data.keyCode),
69
+ publicKeys: [pubKey]
70
+ }
71
+
72
+ openpgp.encrypt(options).then(function(encryptedMessage) {
73
+ self.send({'cmd': 'keycode_encrypted', 'encryptedKeyCode': encryptedMessage.data});
74
+ }, function(err) {
75
+ debug('ERROR');
76
+ debug(err);
77
+ });
78
+ }
79
+ break;
80
+ case 'decrypt_keycode':
81
+ self.randomCounter = 0;
82
+ self.randomness = data.randomness;
83
+ var privKeys = await openpgp.key.readArmored(data.privateKey);
84
+ privKey = privKeys.keys[0];
85
+ var message = await openpgp.message.readArmored(data.keyCode);
86
+
87
+ options =
88
+ {
89
+ message: message, // parse armored message
90
+ privateKeys: [privKey] // for decryption
91
+ };
92
+
93
+ openpgp.decrypt(options).then(function(plaintext) {
94
+ self.send({'cmd': 'keycode_decrypted', 'decryptedKeycode': plaintext.data});
95
+ try {
96
+ throw new Error("Hello");
97
+ } catch(err) {
98
+ sendError(err);
99
+ }
100
+ }, function(err) {
101
+ debug('ERROR');
102
+ debug(err);
103
+ sendError(err);
104
+ });
105
+ break;
106
+ case 'randBuff':
107
+ self.randomness += data.randomness;
108
+ default:
109
+ ;
110
+ };
111
+ }, false);
112
+
113
+ function convertRawKey(rsaKeys, userStr) {
114
+
115
+ var secretKey = createSecretKey(rsaKeys.privateKey);
116
+ var secretSubKey = createSecretSubKey(rsaKeys.privateSubKey);
117
+
118
+ var key = wrapKeyObject(userStr, secretKey, secretSubKey);
119
+
120
+ var result = {};
121
+ result.privateKeyArmored = key.armor();
122
+ result.publicKeyArmored = key.toPublic().armor();
123
+
124
+ return result;
125
+ }
126
+
127
+ function wrapKeyObject(userId, secretKeyPacket, secretSubkeyPacket)
128
+ {
129
+ packetlist = new window.openpgp.packet.List();
130
+
131
+ userIdPacket = new window.openpgp.packet.Userid();
132
+ userIdPacket.read(userId);
133
+
134
+ dataToSign = {};
135
+ dataToSign.userid = userIdPacket;
136
+ dataToSign.key = secretKeyPacket;
137
+ signaturePacket = new window.openpgp.packet.Signature();
138
+ signaturePacket.signatureType = window.openpgp.enums.signature.cert_generic;
139
+ signaturePacket.publicKeyAlgorithm = window.openpgp.enums.publicKey.rsa_encrypt_sign;
140
+ signaturePacket.hashAlgorithm = window.openpgp.config.prefer_hash_algorithm;
141
+ signaturePacket.keyFlags = [window.openpgp.enums.keyFlags.certify_keys | window.openpgp.enums.keyFlags.sign_data];
142
+ signaturePacket.preferredSymmetricAlgorithms = [];
143
+ signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes256);
144
+ signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes192);
145
+ signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.aes128);
146
+ signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.cast5);
147
+ signaturePacket.preferredSymmetricAlgorithms.push(window.openpgp.enums.symmetric.tripledes);
148
+ signaturePacket.preferredHashAlgorithms = [];
149
+ signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha256);
150
+ signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha1);
151
+ signaturePacket.preferredHashAlgorithms.push(window.openpgp.enums.hash.sha512);
152
+ signaturePacket.preferredCompressionAlgorithms = [];
153
+ signaturePacket.preferredCompressionAlgorithms.push(window.openpgp.enums.compression.zlib);
154
+ signaturePacket.preferredCompressionAlgorithms.push(window.openpgp.enums.compression.zip);
155
+ if (window.openpgp.config.integrity_protect) {
156
+ signaturePacket.features = [];
157
+ signaturePacket.features.push(1); // Modification Detection
158
+ }
159
+ signaturePacket.sign(secretKeyPacket, dataToSign);
160
+
161
+ dataToSign = {};
162
+ dataToSign.key = secretKeyPacket;
163
+ dataToSign.bind = secretSubkeyPacket;
164
+ subkeySignaturePacket = new window.openpgp.packet.Signature();
165
+ subkeySignaturePacket.signatureType = window.openpgp.enums.signature.subkey_binding;
166
+ subkeySignaturePacket.publicKeyAlgorithm = window.openpgp.enums.publicKey.rsa_encrypt_sign;
167
+ subkeySignaturePacket.hashAlgorithm = window.openpgp.config.prefer_hash_algorithm;
168
+ subkeySignaturePacket.keyFlags = [window.openpgp.enums.keyFlags.encrypt_communication | window.openpgp.enums.keyFlags.encrypt_storage];
169
+ subkeySignaturePacket.sign(secretKeyPacket, dataToSign);
170
+
171
+ packetlist.push(secretKeyPacket);
172
+ packetlist.push(userIdPacket);
173
+ packetlist.push(signaturePacket);
174
+ packetlist.push(secretSubkeyPacket);
175
+ packetlist.push(subkeySignaturePacket);
176
+
177
+ return new window.openpgp.key.Key(packetlist);
178
+ }
179
+
180
+ function createSecretKey(key)
181
+ {
182
+ var mpiList = createMPIList(key);
183
+ var packet = createSecretKeyPacketFromList(mpiList, window.openpgp.packet.SecretKey);
184
+ return packet;
185
+ }
186
+
187
+ function createSecretSubKey(key)
188
+ {
189
+ var mpiList = createMPIList(key);
190
+ var packet = createSecretKeyPacketFromList(mpiList, window.openpgp.packet.SecretSubkey);
191
+ return packet;
192
+ }
193
+
194
+ function createSecretKeyPacketFromList(mpiList, PacketType)
195
+ {
196
+ var secretKeyPacket = new PacketType();
197
+ secretKeyPacket.mpi = mpiList;
198
+ secretKeyPacket.isDecrypted = true;
199
+ secretKeyPacket.algorithm = window.openpgp.enums.read(window.openpgp.enums.publicKey, window.openpgp.enums.publicKey.rsa_encrypt_sign);
200
+ return secretKeyPacket;
201
+ }
202
+
203
+ function createMPIList(privateKey)
204
+ {
205
+ var p = createMPI(privateKey.p.value, privateKey.p.radix);
206
+ var q = createMPI(privateKey.q.value, privateKey.q.radix);
207
+ var u = p.data.modInverse(q.data);
208
+
209
+ var mpiList = [];
210
+ mpiList.push(createMPI(privateKey.n.value, privateKey.n.radix)); // n
211
+ mpiList.push(createMPI("10001", 16)); // e
212
+ mpiList.push(createMPI(privateKey.d.value, privateKey.d.radix)); // d
213
+ mpiList.push(p); // p
214
+ mpiList.push(q); // q
215
+ mpiList.push(createMPIFromBI(u));
216
+
217
+ return mpiList;
218
+ }
219
+
220
+ function createMPI(value, radix)
221
+ {
222
+ var BigInteger = window.openpgp.crypto.publicKey.jsbn;
223
+ var bn = new BigInteger(value, radix);
224
+ return createMPIFromBI(bn);
225
+ }
226
+
227
+ function createMPIFromBI(bigIntegeger) {
228
+ var mpi = new window.openpgp.MPI();
229
+ mpi.fromBigInteger(bigIntegeger);
230
+ return mpi;
231
+ }
232
+
233
+ function reportProgress(progress) {
234
+ var TOTAL = 515;
235
+ self.send({cmd: 'progress', progress: progress, total: TOTAL});
236
+ }
237
+
238
+ function sendError(err) {
239
+ var stacktraceStr = "Stacktrace could not be extracted";
240
+ if(err !== undefined && err.stack !== undefined) {
241
+ stacktraceStr = err.stack;
242
+ }
243
+
244
+ var msg = err.message;
245
+
246
+ self.send({cmd: 'error', stacktrace: stacktraceStr, message: msg});
247
+ }
248
+
249
+ function SecureRandom() {
250
+ function nextBytes(byteArray) {
251
+ for (var n = 0; n < byteArray.length; n++) {
252
+ byteArray[n] = window.openpgp.crypto.random.getSecureRandomOctet();
253
+ }
254
+ }
255
+ this.nextBytes = nextBytes;
256
+ }
257
+
258
+ function debug(msg) {
259
+ self.log += msg + "\n";
260
+ self.send({'cmd': 'debug', 'msg': msg});
261
+ }
262
+
263
+ function send(content) {
264
+ if(self.postMessage != undefined) {
265
+ self.postMessage(content);
266
+ } else {
267
+ postMessage(content);
268
+ }
269
+ }
270
+
271
+ function execute(cmd, errMsg) {
272
+ // wrap the sync cmd in a promise
273
+ var promise = new Promise(function(resolve) {
274
+ var result = cmd();
275
+ resolve(result);
276
+ });
277
+
278
+ // handler error globally
279
+ return promise.catch(onError.bind(null, errMsg));
280
+ }
281
+
282
+ function onError(message, error) {
283
+ // log the stack trace
284
+ console.error(error.stack);
285
+ // rethrow new high level error for api users
286
+ throw new Error(message);
287
287
  }