-
Notifications
You must be signed in to change notification settings - Fork 0
/
smail.js
339 lines (299 loc) · 8.78 KB
/
smail.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
var privKey, pubKey;
var toPubKey;
openpgp.init();
function loadKeyFile()
{
var fr = new FileReader(), file, input = document.getElementById("keyfile");
if (!input.files)
{
alert("No files!");
return false;
}
if (!input.files[0])
{
alert("No file selected!");
return false;
}
file = input.files[0];
fr.onload = function(e) {
// FIXME
alert("Key loaded");
localStorage.setItem("key", e.target.result);
}
fr.readAsText(file);
return false;
}
function generateKey(name, email, bits)
{
var keys;
if (window.crypto.getRandomValues)
{
keys = openpgp.generate_key_pair(1, bits, name + " <" + email + ">");
// document.getElementById("privatekey").textContent = keys["privateKeyArmored"];
// document.getElementById("publickey").textContent = keys["publicKeyArmored"];
openpgp.keyring.importPrivateKey(keys["privateKeyArmored"]);
openpgp.keyring.importPublicKey(keys["publicKeyArmored"]);
openpgp.keyring.store();
// document.location = 'data:Application/octet-stream,' + encodeURIComponent(keys["privateKeyArmored"]);
return true;
}
else
{
alert("Sorry, your browser does not support key generation!");
}
return false;
}
function rsaDecrypt(privkey, message)
{
var msg = openpgp.read_message(message)[0];
var pk = openpgp.read_privateKey(privkey)[0];
var keymat = {key: pk, keymaterial: pk.privateKeyPacket};
return msg.decrypt(keymat, msg.sessionKeys[0]);
}
function rsaEncrypt(pubkey, message, sign)
{
return openpgp.write_encrypted_message(pubkey, message);
}
function symmetricDecrypt(key, message)
{
return openpgp_crypto_symmetricDecrypt(10, key, message, true);
}
function symmetricEncrypt(key, message)
{
return openpgp_crypto_symmetricEncrypt(openpgp_crypto_getPrefixRandom(10), 10, key, message, true);
}
function determineEncStatus()
{
var email = document.getElementById("toemail").value;
document.getElementById("emailstatus").textContent = "";
$.ajax({
url: "api_handler.php",
data: {method : "get_public_key", email : email}
}).done(function (data) {
if (data != '')
{
toPubKey = openpgp.read_publicKey(data);
document.getElementById("emailstatus").textContent = "Public key found, message will be secured";
$('#emailstatus').removeClass('notencrypted');
$('#emailstatus').removeClass('encrypted');
$('#emailstatus').addClass('encrypted');
}
else
{
$('#emailstatus').removeClass('encrypted');
$('#emailstatus').removeClass('notencrypted');
$('#emailstatus').addClass('notencrypted');
document.getElementById("emailstatus").textContent = "No public key found, message will NOT be secured";
}
});
}
function showMessage(id)
{
$.ajax({
url: "api_handler.php",
data: {method: "get_message", id: id}
}).done(function (data) {
$(".mailbox").hide();
var messagejson = JSON.parse(data);
$('#single_mail_selector').each(function(){
$(this).val(id);
});
document.getElementById("message_subject").textContent = messagejson.subject;
document.getElementById("message_from").textContent = messagejson.from;
document.getElementById("message_date").textContent = messagejson.timestamp;
document.getElementById("message_content").textContent = rsaDecrypt(openpgp.keyring.exportPrivateKey(0).armored, messagejson.message);
document.getElementById("messagebox").style.display = "block";
});
}
function getPreview(message) {
return rsaDecrypt(openpgp.keyring.exportPrivateKey(0).armored, message);
}
function sendMessage()
{
var message, email, subject, encmessage, post = "false";
message = document.getElementById("message").value;
email = document.getElementById("toemail").value;
subject = document.getElementById("subject").value;
if (toPubKey)
{
encmessage = rsaEncrypt(toPubKey, message, false);
post = "true";
}
else
{
encmessage = message;
}
$.ajax({
url: "api_handler.php?method=send",
type: "POST",
data: {email: email, subject: subject, message: encmessage, post: post}
}).done(function (data) {
document.getElementById("composeform").reset();
document.getElementById("composeform").style.display = 'none';
document.getElementById("send_complete").style.display = 'block';
});
}
function logOff()
{
localStorage.removeItem("privatekeys");
localStorage.removeItem("publickeys");
$.ajax({
url: "api_handler.php",
data: {method : "logoff"}
}).done(function (data) {
document.location.reload();
});
}
function validateSmailEmail()
{
var email = $("#createemail").val() + '@' + emaildomain;
document.getElementById("createemailcheck").textContent = "";
$.ajax({
url: "api_handler.php",
data: {method : "user_exists", email : email}
}).done(function (data) {
if (data == 'false')
document.getElementById("createemailcheck").textContent = email + " is available!";
else
document.getElementById("createemailcheck").textContent = "Sorry, " + email + " is not available";
});
}
function createUser()
{
var email = $("#createemail").val() + '@' + emaildomain;
var name = $("#createname").val();
var bits = (keysize == null) ? 1024 : keysize;
if (generateKey(name, email, bits))
{
var pubkey = openpgp.keyring.getPublicKeyForAddress(email)[0].armored;
var privkey = openpgp.keyring.getPrivateKeyForAddress(email)[0].armored;
var encprivkey = escape(symmetricEncrypt($("#createpassword").val(), privkey));
postToUrl('dologin.php', {method: 'create_user', pubkey: pubkey, email: email, name: name, encprivkey: encprivkey});
}
}
function postToUrl(path, params, method) {
method = method || "post"; // Set method to post by default if not specified.
// The rest of this code assumes you are not using a library.
// It can be made less wordy if you use one.
var form = document.createElement("form");
form.setAttribute("method", method);
form.setAttribute("action", path);
for(var key in params) {
if(params.hasOwnProperty(key)) {
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
function login()
{
var email = document.getElementById("loginemail").value;
if (email.indexOf("@") == -1)
email = email + '@' + emaildomain;
$.ajax({
url: "api_handler.php",
data: {method : "get_enc_privkey", email : email}
}).done(function (data) {
if (data != '')
{
var privkey = '';
try
{
privkey = symmetricDecrypt($("#loginpassword").val(), unescape(data));
}
catch(err)
{
// Do nothing
}
if (privkey.match('-----BEGIN PGP'))
{
openpgp.keyring.importPrivateKey(privkey);
openpgp.keyring.store();
$.ajax({
url: "api_handler.php",
data: {method : "get_enc_nonce", email : email}
}).done(function (data) {
if (data != '')
{
var nonce = rsaDecrypt(openpgp.keyring.exportPrivateKey(0).armored, data);
postToUrl('dologin.php', {method: 'login', nonce: nonce});
}
});
}
else
{
alert("Sorry, incorrect username/password");
}
}
else
{
alert("Sorry, incorrect username/password");
}
});
}
function push_state(state, title, url) {
if (url != window.location.hash)
history.pushState(state, title, url);
}
function goto_compose()
{
var state = {view: "#compose"};
$("#content").load("compose.php");
push_state(state, "sMail", '#compose');
}
function goto_messages(dir)
{
var state = {view: "#inbox"};
$("#content").load("mailbox.php");
push_state(state, "sMail", '#inbox');
}
function goto_message(id)
{
if (isInt(id) == false) {
return;
}
var state = {view: "#inbox/" + id};
$("#content").load("message.php");
push_state(state, "sMail", "#inbox/" + id);
showMessage(id);
}
function toggle_msg_highlight(id)
{
$("#"+id).toggleClass("msg_highlight");
}
function delete_selected_mail()
{
$(".mail_selector:checked").each(function() {
$.ajax({
url: "api_handler.php",
data: {method: "delete_message", id: $(this).val()}
}).done(function (data) {
goto_messages('#inbox');
});
});
}
function loadFileDiv(id, file) {
$(id).load(file);
}
function hideShowByID(hide, show) {
hideDiv(hide);
showDiv(show);
}
function hideDiv(devid) {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(devid).style.display = 'none';
}
}
function showDiv(devid) {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(devid).style.display = 'block';
}
}
function isInt(n) {
return typeof n === 'number' && n % 1 == 0;
}