added keychain dao and started impl

This commit is contained in:
Tankred Hase 2013-05-23 23:45:26 +02:00
parent eaddd674b9
commit dbb25162fa
4 changed files with 120 additions and 3 deletions

View File

@ -36,6 +36,8 @@ app.crypto.Crypto = function(window, util) {
// derive PBKDF2 from password in web worker thread
this.deriveKey(args.password, self.keySize, function(pbkdf2) {
// TODO: rm keystore logix and check args.storedKeypair
// fetch user's encrypted secret key from keychain/storage
var storedKeypair = keyStore.read(storageId);
@ -52,14 +54,12 @@ app.crypto.Crypto = function(window, util) {
function generateKeypair(pbkdf2) {
// generate RSA keypair in web worker
rsa.generateKeypair(self.rsaKeySize, function(err) {
rsa.generateKeypair(self.rsaKeySize, function(err, keypair) {
if (err) {
callback(err);
return;
}
var keypair = rsa.exportKeys();
// encrypt keypair
var iv = util.random(self.ivSize);
var encryptedKeys = aes.encrypt(JSON.stringify(keypair), pbkdf2, iv);
@ -73,6 +73,7 @@ app.crypto.Crypto = function(window, util) {
};
keyStore.persist(storageId, newStoredKeypair);
// TODO: return generated keypair for storage in keychain dao
callback();
});
}

View File

@ -0,0 +1,71 @@
/**
* A high-level Data-Access Api for handling Keypair synchronization
* between the cloud service and the device's local storage
*/
app.dao.KeychainDAO = function(jsonDao, cloudstorage) {
'use strict';
/**
* Get an array of public keys by looking in local storage and
* fetching missing keys from the cloud service.
* @param ids [Array] the key ids as [{_id, userId}]
* @return [PublicKeyCollection] The requiested public keys
*/
this.getPublicKeys = function(ids, callback) {
};
/**
* Gets the local user's key either from local storage
* or syncronizes from the cloud. The private key is encrypted.
* If no key pair exists, null is returned.
* return [Object] The user's key pair {publicKey, privateKey}
*/
this.getUserKeyPair = function(userId, callback) {
// try to read public key from local storage
jsonDao.read(userId + '_publickey', function(pubkey) {
if (!pubkey) {
// no public key in storage
// TODO: fetch from cloud
// TODO: persist in local storage
callback({
errMsg: 'Not implemented yet!'
});
} else {
// public key found
// get corresponding private key
fetchEncryptedPrivateKey(pubkey);
}
});
function fetchEncryptedPrivateKey(publicKey) {
// try to read private key from local storage
jsonDao.read(userId + '_privatekey_' + publicKey._id, function(privkey) {
if (!privkey) {
// no private key in storage
// TODO: fetch from cloud
// TODO: persist in local storage
callback({
errMsg: 'Not implemented yet!'
});
} else {
// private key found
callback(null, {
publicKey: publicKey,
privateKey: privkey
});
}
});
}
};
/**
* Checks to see if the user's key pair is stored both
* locally and in the cloud and persist arccordingly
* @param [Object] The user's key pair {publicKey, privateKey}
*/
this.putUserKeyPair = function(keypair, callback) {
};
};

View File

@ -49,6 +49,7 @@
<script src="../../src/js/dao/lawnchair-dao.js"></script>
<script src="../../src/js/dao/devicestorage.js"></script>
<script src="../../src/js/dao/cloudstorage-dao.js"></script>
<script src="../../src/js/dao/keychain-dao.js"></script>
<script src="../../src/js/dao/email-dao.js"></script>
<!-- tests -->
@ -60,6 +61,7 @@
<script src="localstorage-dao-test.js"></script>
<script src="lawnchair-dao-test.js"></script>
<script src="devicestorage-test.js"></script>
<script src="keychain-dao-test.js"></script>
<script src="email-dao-test.js"></script>
</body>

View File

@ -0,0 +1,43 @@
module("Keychain DAO");
var keychaindao_test = {
user: 'keychaindao_test@example.com',
password: 'Password',
keySize: 128,
ivSize: 128,
rsaKeySize: 1024
};
asyncTest("Init", 1, function() {
// init dependencies
var util = new cryptoLib.Util(window, uuid);
var jsonDao = new app.dao.LawnchairDAO(window);
var crypto = new app.crypto.Crypto(window, util);
// cloud storage stub
var cloudstorageStub = {
syncPrivateKey: function(emailAdress, storedKey, callback) {
callback();
},
putPublicKey: function(pk, callback) {
callback();
}
};
keychaindao_test.keychainDao = new app.dao.KeychainDAO(jsonDao, cloudstorageStub);
ok(keychaindao_test.keychainDao);
start();
});
asyncTest("Get User Keypair", 2, function() {
keychaindao_test.keychainDao.getUserKeyPair(keychaindao_test.user, function(err, keypair) {
ok(!err);
ok(keypair && keypair.publicKey && keypair.privateKey);
start();
});
});
// asyncTest("Get Public Keys", 1, function() {
// });