Merge pull request #232 from msjoinder/single_public_key

API accepts a single public key or an array
This commit is contained in:
Tankred Hase 2014-06-28 16:44:32 +02:00
commit 68a5739354
4 changed files with 92 additions and 26 deletions

View File

@ -1,23 +1,23 @@
// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
/**
* @fileoverview The openpgp base module should provide all of the functionality
* to consume the openpgp.js library. All additional classes are documented
* @fileoverview The openpgp base module should provide all of the functionality
* to consume the openpgp.js library. All additional classes are documented
* for extending and developing on top of the base library.
*/
@ -52,13 +52,17 @@ function initWorker(path) {
/**
* Encrypts message text with keys
* @param {Array<module:key~Key>} keys array of keys, used to encrypt the message
* @param {(Array<module:key~Key>|module:key~Key)} keys array of keys or single key, used to encrypt the message
* @param {String} text message as native JavaScript string
* @param {function} callback (optional) callback(error, result) for async style
* @return {String} encrypted ASCII armored message
* @static
*/
function encryptMessage(keys, text, callback) {
if (!keys.length) {
keys = [keys];
}
if (useWorker(callback)) {
asyncProxy.encryptMessage(keys, text, callback);
return;
@ -75,7 +79,7 @@ function encryptMessage(keys, text, callback) {
/**
* Signs message text and encrypts it
* @param {Array<module:key~Key>} publicKeys array of keys, used to encrypt the message
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key, used to encrypt the message
* @param {module:key~Key} privateKey private key with decrypted secret key data for signing
* @param {String} text message as native JavaScript string
* @param {function} callback (optional) callback(error, result) for async style
@ -83,6 +87,10 @@ function encryptMessage(keys, text, callback) {
* @static
*/
function signAndEncryptMessage(publicKeys, privateKey, text, callback) {
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text, callback);
return;
@ -122,7 +130,7 @@ function decryptMessage(privateKey, msg, callback) {
/**
* Decrypts message and verifies signatures
* @param {module:key~Key} privateKey private key with decrypted secret key data
* @param {Array<module:key~Key>} publicKeys public keys to verify signatures
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key, to verify signatures
* @param {module:message~Message} msg the message object with signed and encrypted data
* @param {function} callback (optional) callback(error, result) for async style
* @return {{text: String, signatures: Array<{keyid: module:type/keyid, valid: Boolean}>}}
@ -131,6 +139,10 @@ function decryptMessage(privateKey, msg, callback) {
* @static
*/
function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) {
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg, callback);
return;
@ -150,13 +162,17 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) {
/**
* Signs a cleartext message
* @param {Array<module:key~Key>} privateKeys private key with decrypted secret key data to sign cleartext
* @param {(Array<module:key~Key>|module:key~Key)} privateKeys array of keys or single key with decrypted secret key data to sign cleartext
* @param {String} text cleartext
* @param {function} callback (optional) callback(error, result) for async style
* @return {String} ASCII armored message
* @static
*/
function signClearMessage(privateKeys, text, callback) {
if (!privateKeys.length) {
privateKeys = [privateKeys];
}
if (useWorker(callback)) {
asyncProxy.signClearMessage(privateKeys, text, callback);
return;
@ -171,7 +187,7 @@ function signClearMessage(privateKeys, text, callback) {
/**
* Verifies signatures of cleartext signed message
* @param {Array<module:key~Key>} publicKeys public keys to verify signatures
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key, to verify signatures
* @param {module:cleartext~CleartextMessage} msg cleartext message object with signatures
* @param {function} callback (optional) callback(error, result) for async style
* @return {{text: String, signatures: Array<{keyid: module:type/keyid, valid: Boolean}>}}
@ -179,6 +195,10 @@ function signClearMessage(privateKeys, text, callback) {
* @static
*/
function verifyClearSignedMessage(publicKeys, msg, callback) {
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
if (useWorker(callback)) {
asyncProxy.verifyClearSignedMessage(publicKeys, msg, callback);
return;

View File

@ -1,16 +1,16 @@
// GPG4Browsers - An OpenPGP implementation in javascript
// Copyright (C) 2011 Recurity Labs GmbH
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
@ -53,7 +53,7 @@ function AsyncProxy(path) {
* Message handling
*/
AsyncProxy.prototype.onMessage = function(event) {
var msg = event.data;
var msg = event.data;
switch (msg.event) {
case 'method-return':
this.tasks.shift()(msg.err ? new Error(msg.err) : null, msg.data);
@ -96,16 +96,19 @@ AsyncProxy.prototype.terminate = function() {
/**
* Encrypts message text with keys
* @param {Array<module:key~Key>} keys array of keys, used to encrypt the message
* @param {(Array<module:key~Key>|module:key~Key)} keys array of keys or single key, used to encrypt the message
* @param {String} text message as native JavaScript string
* @param {Function} callback receives encrypted ASCII armored message
*/
AsyncProxy.prototype.encryptMessage = function(keys, text, callback) {
if (!keys.length) {
keys = [keys];
}
keys = keys.map(function(key) {
return key.toPacketlist();
});
this.worker.postMessage({
event: 'encrypt-message',
event: 'encrypt-message',
keys: keys,
text: text
});
@ -114,18 +117,21 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) {
/**
* Signs message text and encrypts it
* @param {Array<module:key~Key>} publicKeys array of keys, used to encrypt the message
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key, used to encrypt the message
* @param {module:key~Key} privateKey private key with decrypted secret key data for signing
* @param {String} text message as native JavaScript string
* @param {Function} callback receives encrypted ASCII armored message
*/
AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, text, callback) {
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});
privateKey = privateKey.toPacketlist();
this.worker.postMessage({
event: 'sign-and-encrypt-message',
event: 'sign-and-encrypt-message',
publicKeys: publicKeys,
privateKey: privateKey,
text: text
@ -143,7 +149,7 @@ AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, te
AsyncProxy.prototype.decryptMessage = function(privateKey, message, callback) {
privateKey = privateKey.toPacketlist();
this.worker.postMessage({
event: 'decrypt-message',
event: 'decrypt-message',
privateKey: privateKey,
message: message
});
@ -153,18 +159,21 @@ AsyncProxy.prototype.decryptMessage = function(privateKey, message, callback) {
/**
* Decrypts message and verifies signatures
* @param {module:key~Key} privateKey private key with decrypted secret key data
* @param {Array<module:key~Key>} publicKeys public keys to verify signatures
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key to verify signatures
* @param {module:message~Message} message the message object with signed and encrypted data
* @param {Function} callback receives decrypted message as as native JavaScript string
* with verified signatures or null if no literal data found
*/
AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, message, callback) {
privateKey = privateKey.toPacketlist();
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});
this.worker.postMessage({
event: 'decrypt-and-verify-message',
event: 'decrypt-and-verify-message',
privateKey: privateKey,
publicKeys: publicKeys,
message: message
@ -182,16 +191,19 @@ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys,
/**
* Signs a cleartext message
* @param {Array<module:key~Key>} privateKeys private key with decrypted secret key data to sign cleartext
* @param {(Array<module:key~Key>|module:key~Key)} privateKeys array of keys or single key, with decrypted secret key data to sign cleartext
* @param {String} text cleartext
* @param {Function} callback receives ASCII armored message
*/
AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) {
if (!privateKeys.length) {
privateKeys = [privateKeys];
}
privateKeys = privateKeys.map(function(key) {
return key.toPacketlist();
});
this.worker.postMessage({
event: 'sign-clear-message',
event: 'sign-clear-message',
privateKeys: privateKeys,
text: text
});
@ -200,16 +212,19 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) {
/**
* Verifies signatures of cleartext signed message
* @param {Array<module:key~Key>} publicKeys public keys to verify signatures
* @param {(Array<module:key~Key>|module:key~Key)} publicKeys array of keys or single key, to verify signatures
* @param {module:cleartext~CleartextMessage} message cleartext message object with signatures
* @param {Function} callback receives cleartext with status of verified signatures
*/
AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, callback) {
if (!publicKeys.length) {
publicKeys = [publicKeys];
}
publicKeys = publicKeys.map(function(key) {
return key.toPacketlist();
});
this.worker.postMessage({
event: 'verify-clear-signed-message',
event: 'verify-clear-signed-message',
publicKeys: publicKeys,
message: message
});
@ -236,7 +251,7 @@ AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, ca
*/
AsyncProxy.prototype.generateKeyPair = function(options, callback) {
this.worker.postMessage({
event: 'generate-key-pair',
event: 'generate-key-pair',
options: options
});
this.tasks.push(function(err, data) {

View File

@ -38,6 +38,9 @@ onmessage = function (event) {
break;
case 'encrypt-message':
try {
if (!msg.keys.length) {
msg.keys = [msg.keys];
}
msg.keys = msg.keys.map(packetlistCloneToKey);
data = window.openpgp.encryptMessage(msg.keys, msg.text);
} catch (e) {
@ -47,6 +50,9 @@ onmessage = function (event) {
break;
case 'sign-and-encrypt-message':
try {
if (!msg.publicKeys.length) {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
msg.privateKey = packetlistCloneToKey(msg.privateKey);
data = window.openpgp.signAndEncryptMessage(msg.publicKeys, msg.privateKey, msg.text);
@ -68,6 +74,9 @@ onmessage = function (event) {
case 'decrypt-and-verify-message':
try {
msg.privateKey = packetlistCloneToKey(msg.privateKey);
if (!msg.publicKeys.length) {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
msg.message = packetlistCloneToMessage(msg.message.packets);
data = window.openpgp.decryptAndVerifyMessage(msg.privateKey, msg.publicKeys, msg.message);
@ -87,6 +96,9 @@ onmessage = function (event) {
break;
case 'verify-clear-signed-message':
try {
if (!msg.publicKeys.length) {
msg.publicKeys = [msg.publicKeys];
}
msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey);
var packetlist = window.openpgp.packet.List.fromStructuredClone(msg.message.packets);
msg.message = new window.openpgp.cleartext.CleartextMessage(msg.message.text, packetlist);

View File

@ -194,6 +194,17 @@ describe('High level API', function() {
});
});
it('RSA: encryptMessage one key async', function (done) {
openpgp.encryptMessage(pubKeyRSA, plaintext, function(err, data) {
expect(err).to.not.exist;
expect(data).to.exist;
expect(data).to.match(/^-----BEGIN PGP MESSAGE/);
var msg = openpgp.message.readArmored(data);
expect(msg).to.be.an.instanceof(openpgp.message.Message);
done();
});
});
it('RSA: encryptMessage sync', function () {
var msg = openpgp.encryptMessage([pubKeyRSA], plaintext);
expect(msg).to.exist;
@ -202,6 +213,14 @@ describe('High level API', function() {
expect(msg).to.be.an.instanceof(openpgp.message.Message);
});
it('RSA: encryptMessage one key sync', function () {
var msg = openpgp.encryptMessage(pubKeyRSA, plaintext);
expect(msg).to.exist;
expect(msg).to.match(/^-----BEGIN PGP MESSAGE/);
msg = openpgp.message.readArmored(msg);
expect(msg).to.be.an.instanceof(openpgp.message.Message);
});
it('ELG: encryptMessage async', function (done) {
openpgp.encryptMessage([pubKeyDE], plaintext, function(err, data) {
expect(err).to.not.exist;