Merge pull request #84 from whiteout-io/dev/WO-437

Dev/wo 437
This commit is contained in:
Tankred Hase 2014-07-02 15:22:27 +02:00
commit 22fc2b29fe
5 changed files with 37 additions and 14 deletions

View File

@ -26,6 +26,17 @@ define(function(require) {
keypair = keypair || {}; keypair = keypair || {};
// extract public key from private key block if missing in key file
if (!$scope.key.publicKeyArmored || $scope.key.publicKeyArmored.indexOf('-----BEGIN PGP PUBLIC KEY BLOCK-----') < 0) {
try {
$scope.key.publicKeyArmored = pgp.extractPublicKey($scope.key.privateKeyArmored);
} catch (e) {
$scope.onError(new Error('Error parsing public key from private key!'));
return;
}
}
// parse keypair params
var privKeyParams, pubKeyParams; var privKeyParams, pubKeyParams;
try { try {
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored); privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
@ -93,14 +104,11 @@ define(function(require) {
reader.onload = function(e) { reader.onload = function(e) {
var rawKeys = e.target.result, var rawKeys = e.target.result,
privKeyPrexix = '-----BEGIN PGP PRIVATE KEY BLOCK-----', index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----'),
pubKeyPrefix = '-----BEGIN PGP PUBLIC KEY BLOCK-----',
index = rawKeys.indexOf(privKeyPrexix),
errMsg = 'Keyfile must be formatted like so:\n' + pubKeyPrefix + '\n ... \n' + privKeyPrexix,
keyParts; keyParts;
if (index === -1) { if (index === -1) {
scope.onError(new Error(errMsg)); scope.onError(new Error('Error parsing private PGP key block!'));
return; return;
} }
@ -109,11 +117,6 @@ define(function(require) {
privateKeyArmored: rawKeys.substring(index, rawKeys.length).trim() privateKeyArmored: rawKeys.substring(index, rawKeys.length).trim()
}; };
if (keyParts.publicKeyArmored.indexOf(pubKeyPrefix) < 0 || keyParts.privateKeyArmored.indexOf(privKeyPrexix) < 0) {
scope.onError(new Error(errMsg));
return;
}
scope.$apply(function() { scope.$apply(function() {
scope.key = keyParts; scope.key = keyParts;
}); });

View File

@ -133,6 +133,17 @@ define(function(require) {
}; };
}; };
/**
* Extract a public key from a private key
* @param {String} privateKeyArmored The private PGP key block
* @return {String} The publick PGP key block
*/
PGP.prototype.extractPublicKey = function(privateKeyArmored) {
var privkey = openpgp.key.readArmored(privateKeyArmored).keys[0];
var pubkey = privkey.toPublic();
return pubkey.armor();
};
/** /**
* Import the user's key pair * Import the user's key pair
*/ */

View File

@ -26,7 +26,8 @@ define(function(require) {
keyId = '9FEB47936E712926'; keyId = '9FEB47936E712926';
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO); emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
appController._crypto = pgpMock = sinon.createStubInstance(PGP); appController._pgp = pgpMock = sinon.createStubInstance(PGP);
pgpMock.extractPublicKey.returns('publicKeyArmored');
emailDaoMock._account = { emailDaoMock._account = {
emailAddress: emailAddress, emailAddress: emailAddress,

View File

@ -262,6 +262,14 @@ define(function(require) {
}); });
}); });
describe('extractPublicKey', function() {
it('should work', function() {
var pk = pgp.extractPublicKey(privkey);
expect(pk).to.exist;
expect(pk).to.contain('-----BEGIN PGP PUBLIC KEY BLOCK-----');
});
});
describe('Encrypt and sign', function() { describe('Encrypt and sign', function() {
it('should fail', function(done) { it('should fail', function(done) {
var input = null; var input = null;

View File

@ -26,8 +26,8 @@ define(function(require) {
origInvitation = appController._invitationDao; origInvitation = appController._invitationDao;
appController._invitationDao = invitationMock = sinon.createStubInstance(InvitationDAO); appController._invitationDao = invitationMock = sinon.createStubInstance(InvitationDAO);
origCrypto = appController._crypto; origCrypto = appController._pgp;
appController._crypto = cryptoMock = sinon.createStubInstance(PGP); appController._pgp = cryptoMock = sinon.createStubInstance(PGP);
origOutbox = appController._outboxBo; origOutbox = appController._outboxBo;
appController._outboxBo = outboxMock = sinon.createStubInstance(OutboxBO); appController._outboxBo = outboxMock = sinon.createStubInstance(OutboxBO);
@ -51,7 +51,7 @@ define(function(require) {
afterEach(function() { afterEach(function() {
appController._keychain = origKeychain; appController._keychain = origKeychain;
appController._invitationDao = origInvitation; appController._invitationDao = origInvitation;
appController._crypto = origCrypto; appController._pgp = origCrypto;
appController._outboxBo = origOutbox; appController._outboxBo = origOutbox;
appController._emailDao = origEmailDao; appController._emailDao = origEmailDao;
}); });