mirror of
https://github.com/moparisthebest/mail
synced 2024-11-29 20:32:15 -05:00
commit
22fc2b29fe
@ -26,6 +26,17 @@ define(function(require) {
|
||||
|
||||
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;
|
||||
try {
|
||||
privKeyParams = pgp.getKeyParams($scope.key.privateKeyArmored);
|
||||
@ -93,14 +104,11 @@ define(function(require) {
|
||||
|
||||
reader.onload = function(e) {
|
||||
var rawKeys = e.target.result,
|
||||
privKeyPrexix = '-----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,
|
||||
index = rawKeys.indexOf('-----BEGIN PGP PRIVATE KEY BLOCK-----'),
|
||||
keyParts;
|
||||
|
||||
if (index === -1) {
|
||||
scope.onError(new Error(errMsg));
|
||||
scope.onError(new Error('Error parsing private PGP key block!'));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -109,11 +117,6 @@ define(function(require) {
|
||||
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.key = keyParts;
|
||||
});
|
||||
|
@ -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
|
||||
*/
|
||||
|
@ -26,7 +26,8 @@ define(function(require) {
|
||||
|
||||
keyId = '9FEB47936E712926';
|
||||
emailDaoMock._keychain = keychainMock = sinon.createStubInstance(KeychainDAO);
|
||||
appController._crypto = pgpMock = sinon.createStubInstance(PGP);
|
||||
appController._pgp = pgpMock = sinon.createStubInstance(PGP);
|
||||
pgpMock.extractPublicKey.returns('publicKeyArmored');
|
||||
|
||||
emailDaoMock._account = {
|
||||
emailAddress: emailAddress,
|
||||
|
@ -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() {
|
||||
it('should fail', function(done) {
|
||||
var input = null;
|
||||
|
@ -26,8 +26,8 @@ define(function(require) {
|
||||
origInvitation = appController._invitationDao;
|
||||
appController._invitationDao = invitationMock = sinon.createStubInstance(InvitationDAO);
|
||||
|
||||
origCrypto = appController._crypto;
|
||||
appController._crypto = cryptoMock = sinon.createStubInstance(PGP);
|
||||
origCrypto = appController._pgp;
|
||||
appController._pgp = cryptoMock = sinon.createStubInstance(PGP);
|
||||
|
||||
origOutbox = appController._outboxBo;
|
||||
appController._outboxBo = outboxMock = sinon.createStubInstance(OutboxBO);
|
||||
@ -51,7 +51,7 @@ define(function(require) {
|
||||
afterEach(function() {
|
||||
appController._keychain = origKeychain;
|
||||
appController._invitationDao = origInvitation;
|
||||
appController._crypto = origCrypto;
|
||||
appController._pgp = origCrypto;
|
||||
appController._outboxBo = origOutbox;
|
||||
appController._emailDao = origEmailDao;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user