forked from SiTLar/pyt-vanilla-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private_module.js
355 lines (338 loc) · 11.8 KB
/
private_module.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"use strict";
/*
var cfg = {"srvurl":"http://moimosk.ru/cgi/secret?","authurl": gConfig.serverURL,"sk":16 };
*/
var CryptoPrivate = function(cfg){
this.cfg = cfg;
this.storage = window.sessionStorage;
if(typeof cfg.authurl === "undefined" ) throw new Error("authurl is not defined");
var sSymKeys = this.storage.getItem("crypto_keys");
if (sSymKeys != "undefined")if (sSymKeys) var gSymKeys = JSON.parse(this.storage.getItem("crypto_keys"));
if(!this.gSymKeys)this.loadKeys(gSymKeys);
var c_login = this.storage.getItem("crypto_login")
if(c_login){
c_login = JSON.parse(c_login);
this.password = atob(c_login.password);
this.username =c_login.username;
}
}
CryptoPrivate.prototype = {
constructor: CryptoPrivate,
gSymKeys: undefined,
decipher: undefined,
cfg: undefined,
username: undefined,
password: undefined,
constants:{"kidlength":btoa(openpgp.crypto.hash.sha256("1")).length},
pubCache: {},
pubQ: {},
decrypt: function(data){
var idL = this.cfg.encId.length;
if (data.slice(0,idL) != this.cfg.encId) return {"error":"0"};
if(typeof this.decipher === "undefined") return {"error":"4"};
var key = this.decipher[data.slice(idL, this.constants.kidlength+idL)];
if(typeof key === "undefined") return {"error":"3"};
return StringView.makeFromBase64(openpgp.crypto.cfb.decrypt("aes256", atob(key), atob(data.slice(idL+this.constants.kidlength) ))).toString();
},
encrypt: function(id, data){
var init = openpgp.crypto.getPrefixRandom("aes256");
var key_idx = Math.floor(Math.random()*this.cfg.sk);
if (typeof this.gSymKeys[id] === "undifined") return {"error":"1"};
var sym_key = atob(this.gSymKeys[id].aKeys[key_idx].key);
/*var init = new Uint8Array(16);
window.crypto.getRandomValues(init);
*/
return this.cfg.encId +this.gSymKeys[id].aKeys[key_idx].id+btoa(openpgp.crypto.cfb.encrypt( init,"aes256", new StringView(data).toBase64(), sym_key));
},
setPassword: function (pass){
this.password = openpgp.crypto.hash.sha256(pass);
caller.storage.setItem("crypto_login", JSON.stringify({"password":btoa(this.password), "username":gMe.users.username}));
},
makeToken: function(){
var caller = this;
return new Promise(function(resolve,reject){
var token = caller.storage.getItem("crypto_write_token");
if(token) return resolve( token);
caller.requestToken().then(function(){
token = caller.storage.getItem("crypto_write_token");
return resolve( token);
}, reject);
} );
},
logout: function(){
caller.storage.removeItem("key_pub");
caller.storage.removeItem("key_private");
caller.storage.removeItem("crypto_write_token");
caller.storage.removeItem("crypto_keys");
this.username = undefined;
this.password = undefined;
this.decipher = new Object();
this.gSymKeys = new Object();
},
register: function(){
var caller = this;
var init = openpgp.crypto.getPrefixRandom("aes256");
var keyPuA = caller.storage.getItem("key_pub");
var keyPrA = caller.storage.getItem("key_private");
var my_passphrase = ""; // Key Pairs is always protected with a caller.password to safety.
return new Promise(function(resolve,reject){
if((typeof caller.password === "undefined")|| (typeof caller.username === "undefined")){
reject();
return;
}
if (!keyPuA)openpgp.generateKeyPair({numBits: 1024, userId: caller.username, passphrase: my_passphrase,unlocked: true }).then (function (a){
caller.storage.setItem("key_pub", a.publicKeyArmored);
caller.storage.setItem("key_private", a.privateKeyArmored);
keyPuA = a.publicKeyArmored;
keyPrA = a.privateKeyArmored;
go();
});else go();
function go(){
var oReqS = new XMLHttpRequest();
oReqS.open("POST", caller.cfg.srvurl+"register");
oReqS.onload = function(){
if(oReqS.status < 400){
var msgEnc = openpgp.message.readArmored(oReqS.response);
var dkey = openpgp.key.readArmored(keyPrA);
openpgp.decryptMessage(dkey.keys[0],msgEnc)
.then(function(msg){
caller.storage.setItem("crypto_write_token",msg);
caller.update().then( resolve());
});
}else reject();
}
oReqS.setRequestHeader("X-Authentication-Token", gConfig.token);
oReqS.setRequestHeader("Content-type","application/json");
oReqS.send(JSON.stringify({"d":keyPuA}));
}
});
},
initPrivate: function(grName){
var caller = this;
var arrKeys = new Array(caller.cfg.sk);
var id;
var grIdRaw = "";
for(var idx = 0; idx < caller.cfg.sk; idx++){
var key = openpgp.crypto.generateSessionKey("blowfish");
id = btoa(openpgp.crypto.hash.sha256(key));
arrKeys[idx]= {"id":id, "key":btoa(key)};
grIdRaw += key;
}
id = caller.username+"-"+btoa(openpgp.crypto.hash.sha256(grIdRaw));
var priv = new Object();
priv.aKeys = arrKeys;
priv.name = new StringView(grName).toBase64();
priv.id = id;
if (typeof caller.gSymKeys === "undefined") caller.gSymKeys = new Object();
caller.gSymKeys[id] = priv;
if (typeof caller.decipher === "undefined") caller.decipher = new Object();
priv.aKeys.forEach(function(key){
caller.decipher[key.id] = key.key;
});
return new Promise(function(resolve,reject){
caller.update().then(resolve,reject);
});
},
getUserPub: function(victim){
var caller = this;
return new Promise(function(resolve,reject){
var oReq = new XMLHttpRequest();
oReq.open("GET", caller.cfg.srvurl+"user/"+victim);
oReq.onload = function(){
if(oReq.status < 400){
resolve(oReq.response);
}else reject(oReq.status);
}
oReq.send();
});
},
readMsg: function(msgEnc){
var caller = this;
return new Promise(function(resolve,reject){
var keyPrA = caller.storage.getItem("key_private");
if(!keyPrA) {
getUserPriv().then(function(){caller.readMsg.then(resolve,reject)},reject);
return;
}
var dkey = openpgp.key.readArmored(keyPrA);
openpgp.decryptMessage(dkey.keys[0],openpgp.message.readArmored(msgEnc)).then(function(msg){resolve(msg);}, reject);
});
},
genMsg: function(victim,data){
var caller = this;
return new Promise(function(resolve,reject){
caller.getUserPub(victim).then(function(arKey){
var key = openpgp.key.readArmored(arKey).keys[0];
openpgp.encryptMessage(key,data).then(resolve);
}, reject);
});
},
addKeys: function(keys){
if (!keys||(keys == "null")) return;
var caller = this;
if (typeof caller.gSymKeys === "undefined") caller.gSymKeys = new Object();
console.log(typeof caller.gSymKeys[keys.id]);
if((typeof caller.gSymKeys[keys.id]) != "undefined" )return;
caller.gSymKeys[keys.id] = keys;
if (typeof caller.decipher === "undefined") caller.decipher = new Object();
keys.aKeys.forEach(function(key){ caller.decipher[key.id] = key.key; });
caller.update();
},
loadKeys: function(keys){
if (!keys||(keys == "null")) return;
var caller = this;
caller.gSymKeys = keys;
if (typeof caller.decipher === "undefined") caller.decipher = new Object();
for(var pid in caller.gSymKeys)
caller.gSymKeys[pid].aKeys.forEach(function(key){ caller.decipher[key.id] = key.key; });
caller.storage.setItem("crypto_keys",JSON.stringify(caller.gSymKeys));
caller.ready = 1;
},
getUserPriv: function(){
var caller = this;
return new Promise(function(resolve,reject){
if((typeof caller.password === "undefined")|| (typeof caller.username === "undefined")){
reject(-2);
return;
}
var oReq = new XMLHttpRequest();
oReq.open("GET", caller.cfg.srvurl+"data" );
oReq.onload = function(){
if(oReq.status < 400){
var secret;
try {
secret = openpgp.crypto.cfb.decrypt("aes256",caller.password , atob(oReq.response));
} catch(e){
console.log(e);
reject(-1);
return;
}
secret = JSON.parse(secret);
caller.storage.setItem("key_private", secret.prkey);
caller.loadKeys(secret.symkeys);
resolve();
}else reject(oReq.status);
}
oReq.setRequestHeader("X-Authentication-User",caller.username);
oReq.send();
});
},
sign: function(data){
var caller = this;
return new Promise(function(resolve,reject){
var keyPrA = caller.storage.getItem("key_private");
if(!keyPrA) {
caller.getUserPriv().then(function(){caller.requestToken( resolve,reject)},reject);
return;
}
var sign = new openpgp.packet.Signature();
sign.signatureType = openpgp.enums.signature.text;
sign.hashAlgorithm = openpgp.config.prefer_hash_algorithm;
var pLit = new openpgp.packet.Literal();
pLit.setText( new StringView(data).toBase64());
keyPrA = openpgp.key.readArmored(keyPrA);
var pSignKey = keyPrA.keys[0].getSigningKeyPacket();
sign.publicKeyAlgorithm = pSignKey.algorithm;
sign.sign(pSignKey,pLit);
resolve(btoa(sign.write()));
});
},
verify: function(data, txtSign, username){
var caller = this;
return new Promise(function(resolve,reject){
var keyPub;
function gotUser(){
var sign = new openpgp.packet.Signature();
var binSign = atob(txtSign);
var pLit = new openpgp.packet.Literal();
pLit.setText( new StringView(data).toBase64());
sign.read(binSign,0,binSign.length);
var pKey = keyPub.keys[0].getKeyPacket([sign.issuerKeyId]);
if(!pKey)return reject();
if (!sign.verify(pKey, pLit))return reject();
resolve();
}
if (caller.pubCache[username]){
keyPub = caller.pubCache[username];
gotUser();
} else if(caller.pubQ[username]) caller.pubQ[username].then(
function(){
keyPub = caller.pubCache[username];
gotUser();
},reject);
else{
caller.pubQ[username] = new Promise(function(resolveP, rejectP){
caller.getUserPub(username).then(function(res){
keyPub = openpgp.key.readArmored(res);
caller.pubCache[username] = keyPub;
gotUser();
resolve();
resolveP();
},function(){
reject();
rejectP();
});
});
}
});
},
mkOwnToken: function(data){
if (typeof this.password === "undefined") return null;
return btoa(openpgp.crypto.hash.sha256(this.password+data));
},
requestToken: function(){
var caller = this;
return new Promise(function(resolve,reject){
var keyPrA = caller.storage.getItem("key_private");
if(!keyPrA) {
caller.getUserPriv().then(function(){caller.requestToken( resolve,reject)},reject);
return;
}
var oReq = new XMLHttpRequest();
oReq.open("GET", caller.cfg.srvurl+"token" );
oReq.onload = function(){
if(oReq.status < 400){
var msgEnc = openpgp.message.readArmored(oReq.response);
var dkey = openpgp.key.readArmored(keyPrA);
openpgp.decryptMessage(dkey.keys[0],msgEnc
).then(function(msg){
caller.storage.setItem("crypto_write_token",msg);
resolve();
},reject);
}else reject(oReq.status);
}
oReq.setRequestHeader("X-Authentication-User",caller.username);
oReq.send();
});
},
update: function(){
var caller = this;
var keyPrA = caller.storage.getItem("key_private");
return new Promise(function(resolve,reject){
if((typeof caller.password === "undefined")|| (typeof caller.username === "undefined")){
reject();
return;
}
var init = openpgp.crypto.getPrefixRandom("aes256");
var oReq = new XMLHttpRequest();
oReq.open("POST", caller.cfg.srvurl+"update" );
oReq.onload = function(){
if(oReq.status < 400){
resolve();
}else reject();
}
var token = caller.storage.getItem("crypto_write_token");
if(!token ) {
caller.requestToken().then(doReq,reject);
}else doReq();
function doReq(){
token = caller.storage.getItem("crypto_write_token");
oReq.setRequestHeader("X-Authentication-Token",token);
oReq.setRequestHeader("X-Authentication-User",caller.username);
oReq.setRequestHeader("Content-type","application/json");
oReq.send(JSON.stringify({"d": btoa(openpgp.crypto.cfb.encrypt( init,"aes256", JSON.stringify({"prkey":keyPrA,"symkeys":caller.gSymKeys }),caller.password ))}));
caller.storage.setItem("crypto_keys",JSON.stringify(caller.gSymKeys) );
}
});
}
}