1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 12:22:22 -05:00

[WO-437] Allow users to import only the private PGP key block

This commit is contained in:
Tankred Hase 2014-07-01 20:58:34 +02:00
parent f20dbede2c
commit b20c6977ca
4 changed files with 34 additions and 11 deletions

View File

@ -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;
});

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
*/

View File

@ -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,

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() {
it('should fail', function(done) {
var input = null;