Diffstat (limited to 'frontend/beta/js/Clipperz/PM/Crypto.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/Clipperz/PM/Crypto.js | 91 |
1 files changed, 59 insertions, 32 deletions
diff --git a/frontend/beta/js/Clipperz/PM/Crypto.js b/frontend/beta/js/Clipperz/PM/Crypto.js index ad16ff0..e1e87ec 100644 --- a/frontend/beta/js/Clipperz/PM/Crypto.js +++ b/frontend/beta/js/Clipperz/PM/Crypto.js | |||
@@ -37,49 +37,49 @@ MochiKit.Base.update(Clipperz.PM.Crypto, { | |||
37 | //------------------------------------------------------------------------- | 37 | //------------------------------------------------------------------------- |
38 | 38 | ||
39 | 'toString': function () { | 39 | 'toString': function () { |
40 | return this.__repr__(); | 40 | return this.__repr__(); |
41 | }, | 41 | }, |
42 | 42 | ||
43 | //------------------------------------------------------------------------- | 43 | //------------------------------------------------------------------------- |
44 | 44 | ||
45 | 'communicationProtocol': { | 45 | 'communicationProtocol': { |
46 | 'currentVersion': '0.2', | 46 | 'currentVersion': '0.2', |
47 | 'versions': { | 47 | 'versions': { |
48 | '0.1': Clipperz.PM.Connection.SRP['1.0'],//Clipperz.Crypto.SRP.versions['1.0'].Connection, | 48 | '0.1': Clipperz.PM.Connection.SRP['1.0'],//Clipperz.Crypto.SRP.versions['1.0'].Connection, |
49 | '0.2': Clipperz.PM.Connection.SRP['1.1']//Clipperz.Crypto.SRP.versions['1.1'].Connection, | 49 | '0.2': Clipperz.PM.Connection.SRP['1.1']//Clipperz.Crypto.SRP.versions['1.1'].Connection, |
50 | }, | 50 | }, |
51 | 'fallbackVersions': { | 51 | 'fallbackVersions': { |
52 | 'current':'0.1', | 52 | 'current':'0.1', |
53 | '0.2': '0.1', | 53 | '0.2': '0.1', |
54 | '0.1': null | 54 | '0.1': null |
55 | } | 55 | } |
56 | }, | 56 | }, |
57 | 57 | ||
58 | //------------------------------------------------------------------------- | 58 | //------------------------------------------------------------------------- |
59 | 59 | ||
60 | 'encryptingFunctions': { | 60 | 'encryptingFunctions': { |
61 | 'currentVersion': '0.3', | 61 | 'currentVersion': '0.4', |
62 | 'versions': { | 62 | 'versions': { |
63 | 63 | ||
64 | //##################################################################### | 64 | //##################################################################### |
65 | 65 | ||
66 | '0.1': { | 66 | '0.1': { |
67 | 'encrypt': function(aKey, aValue) { | 67 | 'encrypt': function(aKey, aValue) { |
68 | return Clipperz.Crypto.Base.encryptUsingSecretKey(aKey, Clipperz.Base.serializeJSON(aValue)); | 68 | return Clipperz.Crypto.Base.encryptUsingSecretKey(aKey, Clipperz.Base.serializeJSON(aValue)); |
69 | }, | 69 | }, |
70 | 70 | ||
71 | 'deferredEncrypt': function(aKey, aValue) { | 71 | 'deferredEncrypt': function(aKey, aValue) { |
72 | var deferredResult; | 72 | var deferredResult; |
73 | 73 | ||
74 | deferredResult = new MochiKit.Async.Deferred(); | 74 | deferredResult = new MochiKit.Async.Deferred(); |
75 | deferredResult.addCallback(Clipperz.PM.Crypto.encryptingFunctions.versions['0.1'].encrypt, aKey, aValue); | 75 | deferredResult.addCallback(Clipperz.PM.Crypto.encryptingFunctions.versions['0.1'].encrypt, aKey, aValue); |
76 | deferredResult.callback(); | 76 | deferredResult.callback(); |
77 | 77 | ||
78 | return deferredResult; | 78 | return deferredResult; |
79 | }, | 79 | }, |
80 | 80 | ||
81 | 'decrypt': function(aKey, aValue) { | 81 | 'decrypt': function(aKey, aValue) { |
82 | var result; | 82 | var result; |
83 | 83 | ||
84 | if (aValue != null) { | 84 | if (aValue != null) { |
85 | result = Clipperz.Base.evalJSON(Clipperz.Crypto.Base.decryptUsingSecretKey(aKey, aValue)); | 85 | result = Clipperz.Base.evalJSON(Clipperz.Crypto.Base.decryptUsingSecretKey(aKey, aValue)); |
@@ -274,167 +274,194 @@ MochiKit.Base.update(Clipperz.PM.Crypto, { | |||
274 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | 274 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); |
275 | value = new Clipperz.ByteArray().appendBase64String(aValue); | 275 | value = new Clipperz.ByteArray().appendBase64String(aValue); |
276 | 276 | ||
277 | decryptedData = Clipperz.Crypto.AES.decrypt(key, value); | 277 | decryptedData = Clipperz.Crypto.AES.decrypt(key, value); |
278 | 278 | ||
279 | value = decryptedData.asString(); | 279 | value = decryptedData.asString(); |
280 | try { | 280 | try { |
281 | result = Clipperz.Base.evalJSON(value); | 281 | result = Clipperz.Base.evalJSON(value); |
282 | } catch (exception) { | 282 | } catch (exception) { |
283 | MochiKit.Logging.logError("Error while decrypting data"); | 283 | MochiKit.Logging.logError("Error while decrypting data"); |
284 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; | 284 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; |
285 | } | 285 | } |
286 | } else { | 286 | } else { |
287 | result = null; | 287 | result = null; |
288 | } | 288 | } |
289 | 289 | ||
290 | return result; | 290 | return result; |
291 | }, | 291 | }, |
292 | 292 | ||
293 | 'deferredDecrypt': function(aKey, aValue) { | 293 | 'deferredDecrypt': function(aKey, aValue) { |
294 | var deferredResult; | 294 | var deferredResult; |
295 | // var now; | 295 | // var now; |
296 | 296 | ||
297 | deferredResult = new MochiKit.Async.Deferred(); | 297 | deferredResult = new MochiKit.Async.Deferred(); |
298 | now = new Date; | 298 | // now = new Date; |
299 | 299 | ||
300 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1: " + res); return res;}); | 300 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1: " + res); return res;}); |
301 | if (aValue != null) { | 301 | if (aValue != null) { |
302 | var key, value; | 302 | var key, value; |
303 | var decryptedData; | 303 | var decryptedData; |
304 | var decryptedValue; | 304 | var decryptedValue; |
305 | 305 | ||
306 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | 306 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); |
307 | //MochiKit.Logging.logDebug("[" + (new Date() - now) + "] computed key"); | 307 | //MochiKit.Logging.logDebug("[" + (new Date() - now) + "] computed key"); |
308 | value = new Clipperz.ByteArray().appendBase64String(aValue); | 308 | value = new Clipperz.ByteArray().appendBase64String(aValue); |
309 | //MochiKit.Logging.logDebug("[" + (new Date() - now) + "] appendedBase64String"); | 309 | //MochiKit.Logging.logDebug("[" + (new Date() - now) + "] appendedBase64String"); |
310 | 310 | ||
311 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1.1: " /* + res*/); return res;}); | 311 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 1.1: " /* + res*/); return res;}); |
312 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value); | 312 | deferredResult.addCallback(Clipperz.Crypto.AES.deferredDecrypt, key, value); |
313 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 2: " /* + res*/); return res;}); | 313 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 2: " /* + res*/); return res;}); |
314 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); | 314 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); |
315 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 3: " /* + res*/); return res;}); | 315 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 3: " /* + res*/); return res;}); |
316 | deferredResult.addCallback(function(aResult) { | 316 | deferredResult.addCallback(function(aResult) { |
317 | return aResult.asString(); | 317 | return aResult.asString(); |
318 | }); | 318 | }); |
319 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 4: " /* + res*/); return res;}); | 319 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 4: " /* + res*/); return res;}); |
320 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); | 320 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); |
321 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 5: " /* + res*/); return res;}); | 321 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 5: " /* + res*/); return res;}); |
322 | deferredResult.addCallback(Clipperz.Base.evalJSON); | 322 | deferredResult.addCallback(Clipperz.Base.evalJSON); |
323 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 6: " /* + res*/); return res;}); | 323 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 6: " /* + res*/); return res;}); |
324 | deferredResult.addErrback(function(anError) { | 324 | deferredResult.addErrback(function(anError) { |
325 | MochiKit.Logging.logError("Error while decrypting data"); | 325 | MochiKit.Logging.logError("Error while decrypting data"); |
326 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; | 326 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; |
327 | }) | 327 | }) |
328 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 7: " /* + res*/); return res;}); | 328 | //deferredResult.addBoth(function(res) {MochiKit.Logging.logDebug("[" + (new Date() - now) + "] Clipperz.PM.Crypto.deferredDecrypt - 7: " /* + res*/); return res;}); |
329 | } else { | 329 | } else { |
330 | deferredResult.addCallback(function() { | 330 | deferredResult.addCallback(function() { |
331 | return null; | 331 | return null; |
332 | }); | 332 | }); |
333 | } | 333 | } |
334 | deferredResult.callback(); | 334 | deferredResult.callback(); |
335 | 335 | ||
336 | return deferredResult; | 336 | return deferredResult; |
337 | }, | 337 | }, |
338 | 338 | ||
339 | 'hash': Clipperz.Crypto.SHA.sha_d256 | 339 | 'hash': Clipperz.Crypto.SHA.sha_d256 |
340 | }, | 340 | }, |
341 | 341 | ||
342 | //##################################################################### | 342 | //##################################################################### |
343 | /* | 343 | |
344 | '0.4': { | 344 | '0.4': { |
345 | 'encrypt': function(aKey, aValue, aNonce) { | 345 | 'encrypt': function(aKey, aValue, aNonce) { |
346 | var result; | 346 | var result; |
347 | varkey, value; | 347 | varkey, value; |
348 | var data; | 348 | var data; |
349 | var dataToEncrypt; | 349 | var dataToEncrypt; |
350 | var encryptedData; | 350 | var encryptedData; |
351 | 351 | ||
352 | //MochiKit.Logging.logDebug(">>> [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt"); | ||
353 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | 352 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); |
354 | //MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 1"); | ||
355 | value = Clipperz.Base.serializeJSON(aValue); | 353 | value = Clipperz.Base.serializeJSON(aValue); |
356 | //MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 2"); | ||
357 | / * | ||
358 | //MochiKit.Logging.logDebug("--> encrypt.fullSize: " + value.length); | ||
359 | value = value.replace(/":{"label":"/g, '":{l:"'); | ||
360 | value = value.replace(/":{"key":"/g, '":{k:"'); | ||
361 | value = value.replace(/":{"notes":"/g, '":{n:"'); | ||
362 | value = value.replace(/":{"record":"/g, '":{r:"'); | ||
363 | value = value.replace(/", "label":"/g, '",l:"'); | ||
364 | value = value.replace(/", "favicon":"/g,'",f:"'); | ||
365 | //MochiKit.Logging.logDebug("<-- encrypt.compressed: " + value.length); | ||
366 | * / | ||
367 | data = new Clipperz.ByteArray(value); | 354 | data = new Clipperz.ByteArray(value); |
368 | //MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 3"); | 355 | encryptedData = Clipperz.Crypto.AES_2.encrypt(key, data, aNonce); |
369 | encryptedData = Clipperz.Crypto.AES.encrypt(key, data, aNonce); | ||
370 | //MochiKit.Logging.logDebug("--- [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt - 4"); | ||
371 | result = encryptedData.toBase64String(); | 356 | result = encryptedData.toBase64String(); |
372 | //MochiKit.Logging.logDebug("<<< [" + (new Date()).valueOf() + "] Clipperz.PM.Crypto.versions[0.3].encrypt"); | ||
373 | 357 | ||
374 | return result; | 358 | return result; |
375 | }, | 359 | }, |
360 | |||
361 | 'deferredEncrypt': function(aKey, aValue, aNonce) { | ||
362 | var deferredResult; | ||
363 | varkey, value; | ||
364 | var data; | ||
365 | var dataToEncrypt; | ||
366 | var encryptedData; | ||
367 | |||
368 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | ||
369 | value = Clipperz.Base.serializeJSON(aValue); | ||
370 | data = new Clipperz.ByteArray(value); | ||
371 | |||
372 | deferredResult = new MochiKit.Async.Deferred() | ||
373 | deferredResult.addCallback(Clipperz.Crypto.AES_2.deferredEncrypt, key, data, aNonce); | ||
374 | deferredResult.addCallback(function(aResult) { | ||
375 | return aResult.toBase64String(); | ||
376 | }) | ||
377 | deferredResult.callback(); | ||
378 | |||
379 | return deferredResult; | ||
380 | }, | ||
376 | 381 | ||
377 | 'decrypt': function(aKey, aValue) { | 382 | 'decrypt': function(aKey, aValue) { |
378 | var result; | 383 | var result; |
379 | 384 | ||
380 | if (aValue != null) { | 385 | if (aValue != null) { |
381 | var key, value; | 386 | var key, value; |
382 | var decryptedData; | 387 | var decryptedData; |
383 | var decryptedValue; | 388 | var decryptedValue; |
384 | 389 | ||
385 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | 390 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); |
386 | value = new Clipperz.ByteArray().appendBase64String(aValue); | 391 | value = new Clipperz.ByteArray().appendBase64String(aValue); |
387 | 392 | ||
388 | decryptedData = Clipperz.Crypto.AES.decrypt(key, value); | 393 | decryptedData = Clipperz.Crypto.AES_2.decrypt(key, value); |
389 | 394 | ||
390 | value = decryptedData.asString(); | 395 | value = decryptedData.asString(); |
391 | / * | ||
392 | value = value.replace(/":{l:"/g,'":{"label":"'); | ||
393 | value = value.replace(/":{k:"/g,'":{"key":"'); | ||
394 | value = value.replace(/":{n:"/g,'":{"notes":"'); | ||
395 | value = value.replace(/":{r:"/g,'":{"record":"'); | ||
396 | value = value.replace(/",l:"/g, '", "label":"'); | ||
397 | value = value.replace(/",f:"/g, '", "favicon":"'); | ||
398 | * / | ||
399 | try { | 396 | try { |
400 | result = Clipperz.Base.evalJSON(value); | 397 | result = Clipperz.Base.evalJSON(value); |
401 | } catch (exception) { | 398 | } catch (exception) { |
402 | MochiKit.Logging.logError("Error while decrypting data"); | 399 | MochiKit.Logging.logError("Error while decrypting data"); |
403 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; | 400 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; |
404 | } | 401 | } |
405 | |||
406 | |||
407 | } else { | 402 | } else { |
408 | result = null; | 403 | result = null; |
409 | } | 404 | } |
410 | 405 | ||
411 | return result; | 406 | return result; |
412 | }, | 407 | }, |
413 | 408 | ||
409 | 'deferredDecrypt': function(aKey, aValue) { | ||
410 | var deferredResult; | ||
411 | |||
412 | deferredResult = new MochiKit.Async.Deferred(); | ||
413 | if (aValue != null) { | ||
414 | var key, value; | ||
415 | var decryptedData; | ||
416 | var decryptedValue; | ||
417 | |||
418 | key = Clipperz.Crypto.SHA.sha_d256(new Clipperz.ByteArray(aKey)); | ||
419 | value = new Clipperz.ByteArray().appendBase64String(aValue); | ||
420 | deferredResult.addCallback(Clipperz.Crypto.AES_2.deferredDecrypt, key, value); | ||
421 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); | ||
422 | deferredResult.addCallback(function(aResult) { | ||
423 | return aResult.asString(); | ||
424 | }); | ||
425 | deferredResult.addCallback(MochiKit.Async.wait, 0.1); | ||
426 | deferredResult.addCallback(Clipperz.Base.evalJSON); | ||
427 | deferredResult.addErrback(function(anError) { | ||
428 | MochiKit.Logging.logError("Error while decrypting data"); | ||
429 | throw Clipperz.Crypto.Base.exception.CorruptedMessage; | ||
430 | }) | ||
431 | } else { | ||
432 | deferredResult.addCallback(function() { | ||
433 | return null; | ||
434 | }); | ||
435 | } | ||
436 | deferredResult.callback(); | ||
437 | |||
438 | return deferredResult; | ||
439 | }, | ||
440 | |||
414 | 'hash': Clipperz.Crypto.SHA.sha_d256 | 441 | 'hash': Clipperz.Crypto.SHA.sha_d256 |
415 | }, | 442 | }, |
416 | */ | 443 | |
417 | //##################################################################### | 444 | //##################################################################### |
418 | __syntaxFix__: "syntax fix" | 445 | __syntaxFix__: "syntax fix" |
419 | } | 446 | } |
420 | }, | 447 | }, |
421 | 448 | ||
422 | //------------------------------------------------------------------------- | 449 | //------------------------------------------------------------------------- |
423 | 450 | ||
424 | 'encrypt': function(aKey, aValue, aVersion) { | 451 | 'encrypt': function(aKey, aValue, aVersion) { |
425 | return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].encrypt(aKey, aValue); | 452 | return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].encrypt(aKey, aValue); |
426 | }, | 453 | }, |
427 | 454 | ||
428 | 'deferredEncrypt': function(aKey, aValue, aVersion) { | 455 | 'deferredEncrypt': function(aKey, aValue, aVersion) { |
429 | return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].deferredEncrypt(aKey, aValue); | 456 | return Clipperz.PM.Crypto.encryptingFunctions.versions[aVersion].deferredEncrypt(aKey, aValue); |
430 | }, | 457 | }, |
431 | 458 | ||
432 | 'encryptWithCurrentVersion': function(aKey, aValue) { | 459 | 'encryptWithCurrentVersion': function(aKey, aValue) { |
433 | return Clipperz.PM.Crypto.encrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion); | 460 | return Clipperz.PM.Crypto.encrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion); |
434 | }, | 461 | }, |
435 | 462 | ||
436 | 'deferredEncryptWithCurrentVersion': function(aKey, aValue) { | 463 | 'deferredEncryptWithCurrentVersion': function(aKey, aValue) { |
437 | return Clipperz.PM.Crypto.deferredEncrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion); | 464 | return Clipperz.PM.Crypto.deferredEncrypt(aKey, aValue, Clipperz.PM.Crypto.encryptingFunctions.currentVersion); |
438 | }, | 465 | }, |
439 | 466 | ||
440 | //......................................................................... | 467 | //......................................................................... |