integrated new crypto into email dao unit test

This commit is contained in:
Tankred Hase 2013-05-18 22:33:10 +02:00
parent 2c1458b663
commit afe8c1329b
5 changed files with 51 additions and 32 deletions

View File

@ -84,6 +84,19 @@ app.crypto.Crypto = function(window, util) {
} }
}; };
/**
* Return a Public Key object containing the Public Key PEM
*/
this.getPublicKey = function() {
var keypair = rsa.exportKeys();
return {
_id: keypair._id,
userId: this.emailAddress,
publicKey: keypair.pubkeyPem
};
};
/** /**
* Do PBKDF2 key derivation in a WebWorker thread * Do PBKDF2 key derivation in a WebWorker thread
*/ */

View File

@ -2,11 +2,9 @@
* A high-level Data-Access Api for handling Email synchronization * A high-level Data-Access Api for handling Email synchronization
* between the cloud service and the device's local storage * between the cloud service and the device's local storage
*/ */
app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto, util) { app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
'use strict'; 'use strict';
var keypair; // the user's keypair
/** /**
* Inits all dependencies * Inits all dependencies
*/ */
@ -29,30 +27,27 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
}); });
function initCrypto() { function initCrypto() {
crypto.init(account.get('emailAddress'), password, account.get('symKeySize'), account.get('symIvSize'), function(err) { crypto.init({
emailAddress: account.get('emailAddress'),
password: password,
keySize: account.get('symKeySize')
}, function(err) {
if (err) { if (err) {
callback(err); callback(err);
return; return;
} }
initNaclCrypto(); publishPublicKey();
}); });
} }
function initNaclCrypto() { function publishPublicKey() {
// derive keypair from user's secret key // get public key from crypto
crypto.deriveKeyPair(naclCrypto, function(generated) { var pubkey = crypto.getPublicKey();
keypair = generated;
//publish public key to cloud service //publish public key to cloud service
var pubkey = new app.model.PublicKey({ cloudstorage.putPublicKey(pubkey, function(err) {
_id: keypair.id, callback(err);
userId: account.get('emailAddress'),
publicKey: keypair.boxPk
});
cloudstorage.putPublicKey(pubkey.toJSON(), function(err) {
callback(err);
});
}); });
} }
}; };
@ -85,7 +80,12 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
if (!folder) { if (!folder) {
// get items from storage // get items from storage
devicestorage.listItems('email_' + folderName, offset, num, function(decryptedList) { devicestorage.listItems('email_' + folderName, offset, num, function(err, decryptedList) {
if (err) {
callback(err);
return;
}
// parse to backbone model collection // parse to backbone model collection
collection = new app.model.EmailCollection(decryptedList); collection = new app.model.EmailCollection(decryptedList);
@ -98,13 +98,13 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage, naclCrypto,
self.account.get('folders').add(folder); self.account.get('folders').add(folder);
} }
callback(collection); callback(null, collection);
}); });
} else { } else {
// read items from memory // read items from memory
collection = folder.get('items'); collection = folder.get('items');
callback(collection); callback(null, collection);
} }
}; };

View File

@ -24,6 +24,12 @@ asyncTest("Init", 2, function() {
}); });
}); });
test("Get Public Key PEM", 2, function() {
var pk = crypto_test.crypto.getPublicKey();
ok(pk._id && pk.userId, 'Key ID: ' + pk._id);
ok(pk.publicKey.indexOf('-----BEGIN PUBLIC KEY-----') === 0, pk.publicKey);
});
asyncTest("PBKDF2 (Async/Worker)", 1, function() { asyncTest("PBKDF2 (Async/Worker)", 1, function() {
crypto_test.crypto.deriveKey(crypto_test.password, crypto_test.keySize, function(key) { crypto_test.crypto.deriveKey(crypto_test.password, crypto_test.keySize, function(key) {
equal(crypto_test.util.base642Str(key).length * 8, crypto_test.keySize, 'Keysize ' + crypto_test.keySize); equal(crypto_test.util.base642Str(key).length * 8, crypto_test.keySize, 'Keysize ' + crypto_test.keySize);

View File

@ -7,12 +7,11 @@ var emaildao_test = {
ivSize: 128 ivSize: 128
}; };
asyncTest("Init", 2, function() { asyncTest("Init", 3, function() {
// init dependencies // init dependencies
var util = new app.crypto.Util(window, uuid); var util = new app.crypto.Util(window, uuid);
var jsonDao = new app.dao.LawnchairDAO(window); var jsonDao = new app.dao.LawnchairDAO(window);
emaildao_test.crypto = new app.crypto.Crypto(window, util); emaildao_test.crypto = new app.crypto.Crypto(window, util);
var naclCrypto = new app.crypto.NaclCrypto(nacl, util);
emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null); emaildao_test.storage = new app.dao.DeviceStorage(util, emaildao_test.crypto, jsonDao, null);
// cloud storage stub // cloud storage stub
var cloudstorageStub = { var cloudstorageStub = {
@ -23,7 +22,7 @@ asyncTest("Init", 2, function() {
callback(); callback();
} }
}; };
emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub, naclCrypto); emaildao_test.emailDao = new app.dao.EmailDAO(_, emaildao_test.crypto, emaildao_test.storage, cloudstorageStub);
// generate test data // generate test data
emaildao_test.list = new TestData().getEmailCollection(100); emaildao_test.list = new TestData().getEmailCollection(100);
@ -34,7 +33,8 @@ asyncTest("Init", 2, function() {
symIvSize: emaildao_test.ivSize symIvSize: emaildao_test.ivSize
}); });
emaildao_test.emailDao.init(account, emaildao_test.password, function() { emaildao_test.emailDao.init(account, emaildao_test.password, function(err) {
ok(!err);
equal(emaildao_test.emailDao.account.get('emailAddress'), emaildao_test.user, 'Email DAO Account'); equal(emaildao_test.emailDao.account.get('emailAddress'), emaildao_test.user, 'Email DAO Account');
// clear db before tests // clear db before tests
@ -46,8 +46,9 @@ asyncTest("Init", 2, function() {
}); });
}); });
asyncTest("Persist test emails", 2, function() { asyncTest("Persist test emails", 3, function() {
emaildao_test.crypto.aesEncryptListForUser(emaildao_test.list.toJSON(), function(encryptedList) { emaildao_test.crypto.encryptListForUser(emaildao_test.list.toJSON(), null, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list'); equal(encryptedList.length, emaildao_test.list.length, 'Encrypt list');
// add sent date to encrypted items // add sent date to encrypted items
@ -63,8 +64,10 @@ asyncTest("Persist test emails", 2, function() {
}); });
}); });
asyncTest("List Email models", 1, function() { asyncTest("List Email models", 2, function() {
emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(collection) { emaildao_test.emailDao.listItems('inbox', 0, emaildao_test.list.length, function(err, collection) {
ok(!err);
var gotten = collection.toJSON(), var gotten = collection.toJSON(),
reference = emaildao_test.list.toJSON(); reference = emaildao_test.list.toJSON();

View File

@ -25,7 +25,6 @@
<script src="../lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script> <script src="../lib/lawnchair/lawnchair-adapter-webkit-sqlite-git.js"></script>
<script src="../lib/forge/forge.rsa.bundle.js"></script> <script src="../lib/forge/forge.rsa.bundle.js"></script>
<script src="../lib/nacl.js"></script>
<script src="../lib/uuid.js"></script> <script src="../lib/uuid.js"></script>
<script src="../js/app-config.js"></script> <script src="../js/app-config.js"></script>
@ -43,7 +42,6 @@
<script src="../js/crypto/pbkdf2.js"></script> <script src="../js/crypto/pbkdf2.js"></script>
<script src="../js/crypto/aes-cbc.js"></script> <script src="../js/crypto/aes-cbc.js"></script>
<script src="../js/crypto/rsa.js"></script> <script src="../js/crypto/rsa.js"></script>
<script src="../js/crypto/nacl-crypto.js"></script>
<script src="../js/crypto/crypto-batch.js"></script> <script src="../js/crypto/crypto-batch.js"></script>
<script src="../js/crypto/crypto.js"></script> <script src="../js/crypto/crypto.js"></script>
@ -59,7 +57,6 @@
<script src="forge-test.js"></script> <script src="forge-test.js"></script>
<script src="aes-test.js"></script> <script src="aes-test.js"></script>
<script src="rsa-test.js"></script> <script src="rsa-test.js"></script>
<script src="nacl-crypto-test.js"></script>
<script src="crypto-test.js"></script> <script src="crypto-test.js"></script>
<script src="localstorage-dao-test.js"></script> <script src="localstorage-dao-test.js"></script>
<script src="lawnchair-dao-test.js"></script> <script src="lawnchair-dao-test.js"></script>