Refactor pgp service to use promises

This commit is contained in:
Tankred Hase 2014-11-29 16:28:51 +01:00
parent f9f086d622
commit 01e0529854
1 changed files with 230 additions and 203 deletions

View File

@ -10,7 +10,9 @@ var util = openpgp.util,
/**
* High level crypto api that handles all calls to OpenPGP.js
*/
function PGP() {
function PGP($q) {
this._q = $q;
openpgp.config.prefer_hash_algorithm = openpgp.enums.hash.sha256;
openpgp.initWorker(config.workerPath + '/openpgp.worker.min.js');
}
@ -18,29 +20,31 @@ function PGP() {
/**
* Generate a key pair for the user
*/
PGP.prototype.generateKeys = function(options, callback) {
PGP.prototype.generateKeys = function(options) {
var userId, passphrase;
if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) {
callback(new Error('Crypto init failed. Not all options set!'));
return;
return this._q(function(resolve, reject) {
reject(new Error('Crypto init failed. Not all options set!'));
});
}
// generate keypair
userId = 'Whiteout User <' + options.emailAddress + '>';
passphrase = (options.passphrase) ? options.passphrase : undefined;
openpgp.generateKeyPair({
return openpgp.generateKeyPair({
keyType: 1, // (keytype 1=RSA)
numBits: options.keySize,
userId: userId,
passphrase: passphrase
}).then(function(keys) {
callback(null, {
return {
keyId: keys.key.primaryKey.getKeyId().toHex().toUpperCase(),
privateKeyArmored: keys.privateKeyArmored,
publicKeyArmored: keys.publicKeyArmored
};
});
}).catch(callback);
};
/**
@ -142,12 +146,14 @@ PGP.prototype.extractPublicKey = function(privateKeyArmored) {
/**
* Import the user's key pair
*/
PGP.prototype.importKeys = function(options, callback) {
var pubKeyId, privKeyId, self = this;
PGP.prototype.importKeys = function(options) {
var self = this;
return self._q(function(resolve, reject) {
var pubKeyId, privKeyId;
// check options
if (!options.privateKeyArmored || !options.publicKeyArmored) {
callback(new Error('Importing keys failed. Not all options set!'));
reject(new Error('Importing keys failed. Not all options set!'));
return;
}
@ -158,66 +164,71 @@ PGP.prototype.importKeys = function(options, callback) {
// read armored keys
try {
this._publicKey = openpgp.key.readArmored(options.publicKeyArmored).keys[0];
this._privateKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
self._publicKey = openpgp.key.readArmored(options.publicKeyArmored).keys[0];
self._privateKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
} catch (e) {
resetKeys();
callback(new Error('Importing keys failed. Parsing error!'));
reject(new Error('Importing keys failed. Parsing error!'));
return;
}
// decrypt private key with passphrase
if (!this._privateKey.decrypt(options.passphrase)) {
if (!self._privateKey.decrypt(options.passphrase)) {
resetKeys();
callback(new Error('Incorrect passphrase!'));
reject(new Error('Incorrect passphrase!'));
return;
}
// check if keys have the same id
pubKeyId = this._publicKey.primaryKey.getKeyId().toHex();
privKeyId = this._privateKey.primaryKey.getKeyId().toHex();
pubKeyId = self._publicKey.primaryKey.getKeyId().toHex();
privKeyId = self._privateKey.primaryKey.getKeyId().toHex();
if (!pubKeyId || !privKeyId || pubKeyId !== privKeyId) {
resetKeys();
callback(new Error('Key IDs dont match!'));
reject(new Error('Key IDs dont match!'));
return;
}
callback();
resolve();
});
};
/**
* Export the user's key pair
*/
PGP.prototype.exportKeys = function(callback) {
if (!this._publicKey || !this._privateKey) {
callback(new Error('Could not export keys!'));
PGP.prototype.exportKeys = function() {
var self = this;
return self._q(function(resolve, reject) {
if (!self._publicKey || !self._privateKey) {
reject(new Error('Could not export keys!'));
return;
}
callback(null, {
keyId: this._publicKey.primaryKey.getKeyId().toHex().toUpperCase(),
privateKeyArmored: this._privateKey.armor(),
publicKeyArmored: this._publicKey.armor()
resolve({
keyId: self._publicKey.primaryKey.getKeyId().toHex().toUpperCase(),
privateKeyArmored: self._privateKey.armor(),
publicKeyArmored: self._publicKey.armor()
});
});
};
/**
* Change the passphrase of an ascii armored private key.
*/
PGP.prototype.changePassphrase = function(options, callback) {
PGP.prototype.changePassphrase = function(options) {
return this._q(function(resolve, reject) {
var privKey, packets, newPassphrase, newKeyArmored;
// set undefined instead of empty string as passphrase
newPassphrase = (options.newPassphrase) ? options.newPassphrase : undefined;
if (!options.privateKeyArmored) {
callback(new Error('Private key must be specified to change passphrase!'));
reject(new Error('Private key must be specified to change passphrase!'));
return;
}
if (options.oldPassphrase === newPassphrase ||
(!options.oldPassphrase && !newPassphrase)) {
callback(new Error('New and old passphrase are the same!'));
reject(new Error('New and old passphrase are the same!'));
return;
}
@ -225,13 +236,13 @@ PGP.prototype.changePassphrase = function(options, callback) {
try {
privKey = openpgp.key.readArmored(options.privateKeyArmored).keys[0];
} catch (e) {
callback(new Error('Importing key failed. Parsing error!'));
reject(new Error('Importing key failed. Parsing error!'));
return;
}
// decrypt private key with passphrase
if (!privKey.decrypt(options.oldPassphrase)) {
callback(new Error('Old passphrase incorrect!'));
reject(new Error('Old passphrase incorrect!'));
return;
}
@ -243,31 +254,33 @@ PGP.prototype.changePassphrase = function(options, callback) {
}
newKeyArmored = privKey.armor();
} catch (e) {
callback(new Error('Setting new passphrase failed!'));
reject(new Error('Setting new passphrase failed!'));
return;
}
// check if new passphrase really works
if (!privKey.decrypt(newPassphrase)) {
callback(new Error('Decrypting key with new passphrase failed!'));
reject(new Error('Decrypting key with new passphrase failed!'));
return;
}
callback(null, newKeyArmored);
resolve(newKeyArmored);
});
};
/**
* Encrypt and sign a pgp message for a list of receivers
*/
PGP.prototype.encrypt = function(plaintext, publicKeysArmored, callback) {
PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
var self = this;
return self._q(function(resolve, reject) {
var publicKeys;
// check keys
if (!this._privateKey) {
callback(new Error('Error encrypting. Keys must be set!'));
if (!self._privateKey) {
reject(new Error('Error encrypting. Keys must be set!'));
return;
}
// parse armored public keys
try {
if (publicKeysArmored && publicKeysArmored.length) {
@ -277,17 +290,20 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored, callback) {
});
}
} catch (err) {
callback(new Error('Error encrypting plaintext!'));
reject(new Error('Error encrypting plaintext!'));
return;
}
resolve(publicKeys);
}).then(function(publicKeys) {
if (publicKeys) {
// encrypt and sign the plaintext
openpgp.signAndEncryptMessage(publicKeys, this._privateKey, plaintext).then(callback.bind(null, null)).catch(callback);
return openpgp.signAndEncryptMessage(publicKeys, self._privateKey, plaintext);
} else {
// if no public keys are available encrypt for myself
openpgp.signAndEncryptMessage([this._publicKey], this._privateKey, plaintext).then(callback.bind(null, null)).catch(callback);
return openpgp.signAndEncryptMessage([self._publicKey], self._privateKey, plaintext);
}
});
};
/**
@ -296,15 +312,16 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored, callback) {
* @param {String} publicKeyArmored The public key used to sign the message
* @param {Function} callback(error, plaintext, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
*/
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored, callback) {
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
var self = this;
return self._q(function(resolve, reject) {
var publicKeys, message;
// check keys
if (!this._privateKey) {
callback(new Error('Error decrypting. Keys must be set!'));
if (!self._privateKey) {
reject(new Error('Error decrypting. Keys must be set!'));
return;
}
// read keys and ciphertext message
try {
if (publicKeyArmored) {
@ -312,19 +329,25 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored, callback) {
publicKeys = openpgp.key.readArmored(publicKeyArmored).keys;
} else {
// use own public key to know if signatures are available
publicKeys = [this._publicKey];
publicKeys = [self._publicKey];
}
message = openpgp.message.readArmored(ciphertext);
} catch (err) {
callback(new Error('Error parsing encrypted PGP message!'));
reject(new Error('Error parsing encrypted PGP message!'));
return;
}
resolve(publicKeys, message);
}).then(function(publicKeys, message) {
// decrypt and verify pgp message
openpgp.decryptAndVerifyMessage(this._privateKey, publicKeys, message).then(function(decrypted) {
return openpgp.decryptAndVerifyMessage(self._privateKey, publicKeys, message);
}).then(function(decrypted) {
// return decrypted plaintext
callback(null, decrypted.text, checkSignatureValidity(decrypted.signatures));
}).catch(callback);
return {
decrypted: decrypted.text,
signaturesValid: checkSignatureValidity(decrypted.signatures)
};
});
};
/**
@ -333,16 +356,16 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored, callback) {
* @param {String} publicKeyArmored The public key used to signed the message
* @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
*/
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored, callback) {
var publicKeys,
message;
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) {
var self = this;
return self._q(function(resolve, reject) {
var publicKeys, message;
// check keys
if (!this._privateKey) {
callback(new Error('Error verifying signed PGP message. Keys must be set!'));
if (!self._privateKey) {
reject(new Error('Error verifying signed PGP message. Keys must be set!'));
return;
}
// read keys and ciphertext message
try {
if (publicKeyArmored) {
@ -350,17 +373,20 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
publicKeys = openpgp.key.readArmored(publicKeyArmored).keys;
} else {
// use own public key to know if signatures are available
publicKeys = [this._publicKey];
publicKeys = [self._publicKey];
}
message = openpgp.cleartext.readArmored(clearSignedText);
} catch (err) {
callback(new Error('Error verifying signed PGP message!'));
reject(new Error('Error verifying signed PGP message!'));
return;
}
resolve(publicKeys, message);
openpgp.verifyClearSignedMessage(publicKeys, message).then(function(result) {
callback(null, checkSignatureValidity(result.signatures));
}).catch(callback);
}).then(function(publicKeys, message) {
return openpgp.verifyClearSignedMessage(publicKeys, message);
}).then(function(result) {
return checkSignatureValidity(result.signatures);
});
};
/**
@ -370,15 +396,16 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
* @param {String} publicKeyArmored The public key used to signed the message
* @param {Function} callback(error, signaturesValid) signaturesValid is undefined in case there are no signature, null in case there are signatures but the wrong public key or no key was used to verify, true if the signature was successfully verified, or false if the signataure verification failed.
*/
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored, callback) {
var publicKeys;
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) {
var self = this;
return self._q(function(resolve, reject) {
var publicKeys, signatures;
// check keys
if (!this._privateKey) {
callback(new Error('Error verifying signed PGP message. Keys must be set!'));
if (!self._privateKey) {
reject(new Error('Error verifying signed PGP message. Keys must be set!'));
return;
}
// read keys and ciphertext message
try {
if (publicKeyArmored) {
@ -386,23 +413,23 @@ PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArm
publicKeys = openpgp.key.readArmored(publicKeyArmored).keys;
} else {
// use own public key to know if signatures are available
publicKeys = [this._publicKey];
publicKeys = [self._publicKey];
}
} catch (err) {
callback(new Error('Error verifying signed PGP message!'));
reject(new Error('Error verifying signed PGP message!'));
return;
}
var signatures;
// check signatures
try {
var msg = openpgp.message.readSignedContent(message, pgpSignature);
signatures = msg.verify(publicKeys);
} catch (err) {
callback(new Error('Error verifying signed PGP message!'));
reject(new Error('Error verifying signed PGP message!'));
return;
}
callback(null, checkSignatureValidity(signatures));
resolve(checkSignatureValidity(signatures));
});
};
/**