From 0cb5214b949aa7971d852ef3aacec1b6309f7bdb Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Sun, 19 May 2013 01:33:59 +0200 Subject: [PATCH] minor fixes to email dao and crypto --- src/js/crypto/crypto.js | 17 +++++++++++++---- src/js/dao/email-dao.js | 5 +++-- src/js/model/account-model.js | 1 + test/unit/crypto-test.js | 6 ++++-- test/unit/email-dao-test.js | 8 +++++--- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/js/crypto/crypto.js b/src/js/crypto/crypto.js index 6766072..b418734 100644 --- a/src/js/crypto/crypto.js +++ b/src/js/crypto/crypto.js @@ -15,9 +15,18 @@ app.crypto.Crypto = function(window, util) { this.init = function(args, callback) { var self = this; + // valdiate input + if (!args.emailAddress || !args.keySize || !args.rsaKeySize) { + callback({ + errMsg: 'Crypto init failed. Not all args set!' + }); + return; + } + this.emailAddress = args.emailAddress; this.keySize = args.keySize; this.ivSize = args.keySize; + this.rsaKeySize = args.rsaKeySize; // derive PBKDF2 from password in web worker thread this.deriveKey(args.password, args.keySize, function(pbkdf2) { @@ -40,7 +49,7 @@ app.crypto.Crypto = function(window, util) { function generateKeypair(keyStore, storageId, pbkdf2) { // generate RSA keypair in web worker - rsa.generateKeypair(rsa_test.keySize, function(err) { + rsa.generateKeypair(self.rsaKeySize, function(err) { if (err) { callback(err); return; @@ -56,8 +65,8 @@ app.crypto.Crypto = function(window, util) { var newStoredKeypair = { _id: keypair._id, userId: args.emailAddress, - encryptedKeys: encryptedKeys, - keyIV: iv + encryptedKey: encryptedKeys, + iv: iv }; keyStore.persist(storageId, newStoredKeypair); @@ -69,7 +78,7 @@ app.crypto.Crypto = function(window, util) { var keypairJson, keypair; // try to decrypt with pbkdf2 try { - keypairJson = aes.decrypt(storedKeypair.encryptedKeys, pbkdf2, storedKeypair.keyIV); + keypairJson = aes.decrypt(storedKeypair.encryptedKey, pbkdf2, storedKeypair.iv); keypair = JSON.parse(keypairJson); } catch (ex) { callback({ diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index c141687..17e277b 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -2,7 +2,7 @@ * A high-level Data-Access Api for handling Email synchronization * between the cloud service and the device's local storage */ -app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) { +app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, util) { 'use strict'; /** @@ -30,7 +30,8 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) { crypto.init({ emailAddress: account.get('emailAddress'), password: password, - keySize: account.get('symKeySize') + keySize: account.get('symKeySize'), + rsaKeySize: account.get('asymKeySize') }, function(err) { if (err) { callback(err); diff --git a/src/js/model/account-model.js b/src/js/model/account-model.js index e148444..68f1008 100644 --- a/src/js/model/account-model.js +++ b/src/js/model/account-model.js @@ -7,6 +7,7 @@ emailAddress: null, symKeySize: null, symIvSize: null, + ssymKeySize: null, folders: null }, diff --git a/test/unit/crypto-test.js b/test/unit/crypto-test.js index ca74c37..855ae53 100644 --- a/test/unit/crypto-test.js +++ b/test/unit/crypto-test.js @@ -4,7 +4,8 @@ var crypto_test = { user: 'crypto_test@example.com', password: 'Password', keySize: 128, - ivSize: 128 + ivSize: 128, + rsaKeySize: 1024 }; asyncTest("Init", 2, function() { @@ -16,7 +17,8 @@ asyncTest("Init", 2, function() { crypto_test.crypto.init({ emailAddress: crypto_test.user, password: crypto_test.password, - keySize: crypto_test.keySize + keySize: crypto_test.keySize, + rsaKeySize: crypto_test.rsaKeySize }, function(err) { ok(!err, 'Init crypto'); diff --git a/test/unit/email-dao-test.js b/test/unit/email-dao-test.js index c0018d7..23f3cd6 100644 --- a/test/unit/email-dao-test.js +++ b/test/unit/email-dao-test.js @@ -4,7 +4,8 @@ var emaildao_test = { user: 'test@atlasdev.onmicrosoft.com', password: 'Xoza76645', keySize: 128, - ivSize: 128 + ivSize: 128, + rsaKeySize: 1024 }; asyncTest("Init", 3, function() { @@ -22,7 +23,7 @@ asyncTest("Init", 3, function() { callback(); } }; - emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub); + emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub, util); // generate test data emaildao_test.list = new TestData().getEmailCollection(100); @@ -30,7 +31,8 @@ asyncTest("Init", 3, function() { var account = new app.model.Account({ emailAddress: emaildao_test.user, symKeySize: emaildao_test.keySize, - symIvSize: emaildao_test.ivSize + symIvSize: emaildao_test.ivSize, + asymKeySize: emaildao_test.rsaKeySize }); emaildao_test.emailDao.init(account, emaildao_test.password, function(err) {