From 1fd2c0f6f4001533df9f2acff15606533aa8d532 Mon Sep 17 00:00:00 2001 From: Msjoinder Date: Sat, 7 Jun 2014 14:11:53 -0500 Subject: [PATCH 1/4] API accepts single public key string instead of array --- src/openpgp.js | 12 ++++++++++++ src/worker/async_proxy.js | 9 +++++++++ src/worker/worker.js | 9 +++++++++ test/worker/api.js | 19 +++++++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/src/openpgp.js b/src/openpgp.js index e826620e..b6a4b7d9 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -83,6 +83,10 @@ function encryptMessage(keys, text, callback) { * @static */ function signAndEncryptMessage(publicKeys, privateKey, text, callback) { + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } + if (useWorker(callback)) { asyncProxy.signAndEncryptMessage(publicKeys, privateKey, text, callback); return; @@ -131,6 +135,10 @@ function decryptMessage(privateKey, msg, callback) { * @static */ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) { + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } + if (useWorker(callback)) { asyncProxy.decryptAndVerifyMessage(privateKey, publicKeys, msg, callback); return; @@ -179,6 +187,10 @@ function signClearMessage(privateKeys, text, callback) { * @static */ function verifyClearSignedMessage(publicKeys, msg, callback) { + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } + if (useWorker(callback)) { asyncProxy.verifyClearSignedMessage(publicKeys, msg, callback); return; diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 6580b0bc..1d9af298 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -120,6 +120,9 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { * @param {Function} callback receives encrypted ASCII armored message */ AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, text, callback) { + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } publicKeys = publicKeys.map(function(key) { return key.toPacketlist(); }); @@ -160,6 +163,9 @@ AsyncProxy.prototype.decryptMessage = function(privateKey, message, callback) { */ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, message, callback) { privateKey = privateKey.toPacketlist(); + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } publicKeys = publicKeys.map(function(key) { return key.toPacketlist(); }); @@ -205,6 +211,9 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { * @param {Function} callback receives cleartext with status of verified signatures */ AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, callback) { + if (typeof publicKeys === 'string') { + publicKeys = [publicKeys]; + } publicKeys = publicKeys.map(function(key) { return key.toPacketlist(); }); diff --git a/src/worker/worker.js b/src/worker/worker.js index b93ee4d5..844072da 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -47,6 +47,9 @@ onmessage = function (event) { break; case 'sign-and-encrypt-message': try { + if (typeof msg.publicKeys === 'string') { + 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 +71,9 @@ onmessage = function (event) { case 'decrypt-and-verify-message': try { msg.privateKey = packetlistCloneToKey(msg.privateKey); + if (typeof msg.publicKeys === 'string') { + 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 +93,9 @@ onmessage = function (event) { break; case 'verify-clear-signed-message': try { + if (typeof msg.publicKeys === 'string') { + 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); diff --git a/test/worker/api.js b/test/worker/api.js index 3c87cfa2..43efaea3 100644 --- a/test/worker/api.js +++ b/test/worker/api.js @@ -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; From 4bca02e635b79e3dd6a4758ddb78b29a7e8ae0e6 Mon Sep 17 00:00:00 2001 From: Msjoinder Date: Sat, 7 Jun 2014 14:36:04 -0500 Subject: [PATCH 2/4] include other key arrays --- src/openpgp.js | 8 ++++++++ src/worker/async_proxy.js | 6 ++++++ src/worker/worker.js | 3 +++ 3 files changed, 17 insertions(+) diff --git a/src/openpgp.js b/src/openpgp.js index b6a4b7d9..4538f982 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -59,6 +59,10 @@ function initWorker(path) { * @static */ function encryptMessage(keys, text, callback) { + if (typeof keys === 'string') { + keys = [keys]; + } + if (useWorker(callback)) { asyncProxy.encryptMessage(keys, text, callback); return; @@ -165,6 +169,10 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) { * @static */ function signClearMessage(privateKeys, text, callback) { + if (typeof privateKeys === 'string') { + privateKeys = [privateKeys]; + } + if (useWorker(callback)) { asyncProxy.signClearMessage(privateKeys, text, callback); return; diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index 1d9af298..cfa615b8 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -101,6 +101,9 @@ AsyncProxy.prototype.terminate = function() { * @param {Function} callback receives encrypted ASCII armored message */ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { + if (typeof keys === 'string') { + keys = [keys]; + } keys = keys.map(function(key) { return key.toPacketlist(); }); @@ -193,6 +196,9 @@ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, * @param {Function} callback receives ASCII armored message */ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { + if (typeof privateKeys === 'string') { + privateKeys = [privateKeys]; + } privateKeys = privateKeys.map(function(key) { return key.toPacketlist(); }); diff --git a/src/worker/worker.js b/src/worker/worker.js index 844072da..b1636e83 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -38,6 +38,9 @@ onmessage = function (event) { break; case 'encrypt-message': try { + if (typeof msg.keys === 'string') { + msg.keys = [msg.keys]; + } msg.keys = msg.keys.map(packetlistCloneToKey); data = window.openpgp.encryptMessage(msg.keys, msg.text); } catch (e) { From 74ef7455f5c34c3bb243f7e71d4fca8902c11f2d Mon Sep 17 00:00:00 2001 From: Msjoinder Date: Sat, 7 Jun 2014 15:09:55 -0500 Subject: [PATCH 3/4] use .length to test --- src/openpgp.js | 10 +++++----- src/worker/async_proxy.js | 10 +++++----- src/worker/worker.js | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index 4538f982..1dec924b 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -59,7 +59,7 @@ function initWorker(path) { * @static */ function encryptMessage(keys, text, callback) { - if (typeof keys === 'string') { + if (!keys.length) { keys = [keys]; } @@ -87,7 +87,7 @@ function encryptMessage(keys, text, callback) { * @static */ function signAndEncryptMessage(publicKeys, privateKey, text, callback) { - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } @@ -139,7 +139,7 @@ function decryptMessage(privateKey, msg, callback) { * @static */ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) { - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } @@ -169,7 +169,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) { * @static */ function signClearMessage(privateKeys, text, callback) { - if (typeof privateKeys === 'string') { + if (!privateKeys.length) { privateKeys = [privateKeys]; } @@ -195,7 +195,7 @@ function signClearMessage(privateKeys, text, callback) { * @static */ function verifyClearSignedMessage(publicKeys, msg, callback) { - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index cfa615b8..b7c01788 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -101,7 +101,7 @@ AsyncProxy.prototype.terminate = function() { * @param {Function} callback receives encrypted ASCII armored message */ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { - if (typeof keys === 'string') { + if (!keys.length) { keys = [keys]; } keys = keys.map(function(key) { @@ -123,7 +123,7 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { * @param {Function} callback receives encrypted ASCII armored message */ AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, text, callback) { - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } publicKeys = publicKeys.map(function(key) { @@ -166,7 +166,7 @@ AsyncProxy.prototype.decryptMessage = function(privateKey, message, callback) { */ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, message, callback) { privateKey = privateKey.toPacketlist(); - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } publicKeys = publicKeys.map(function(key) { @@ -196,7 +196,7 @@ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, * @param {Function} callback receives ASCII armored message */ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { - if (typeof privateKeys === 'string') { + if (!privateKeys.length) { privateKeys = [privateKeys]; } privateKeys = privateKeys.map(function(key) { @@ -217,7 +217,7 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { * @param {Function} callback receives cleartext with status of verified signatures */ AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, callback) { - if (typeof publicKeys === 'string') { + if (!publicKeys.length) { publicKeys = [publicKeys]; } publicKeys = publicKeys.map(function(key) { diff --git a/src/worker/worker.js b/src/worker/worker.js index b1636e83..cfcde8be 100644 --- a/src/worker/worker.js +++ b/src/worker/worker.js @@ -38,7 +38,7 @@ onmessage = function (event) { break; case 'encrypt-message': try { - if (typeof msg.keys === 'string') { + if (!msg.keys.length) { msg.keys = [msg.keys]; } msg.keys = msg.keys.map(packetlistCloneToKey); @@ -50,7 +50,7 @@ onmessage = function (event) { break; case 'sign-and-encrypt-message': try { - if (typeof msg.publicKeys === 'string') { + if (!msg.publicKeys.length) { msg.publicKeys = [msg.publicKeys]; } msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey); @@ -74,7 +74,7 @@ onmessage = function (event) { case 'decrypt-and-verify-message': try { msg.privateKey = packetlistCloneToKey(msg.privateKey); - if (typeof msg.publicKeys === 'string') { + if (!msg.publicKeys.length) { msg.publicKeys = [msg.publicKeys]; } msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey); @@ -96,7 +96,7 @@ onmessage = function (event) { break; case 'verify-clear-signed-message': try { - if (typeof msg.publicKeys === 'string') { + if (!msg.publicKeys.length) { msg.publicKeys = [msg.publicKeys]; } msg.publicKeys = msg.publicKeys.map(packetlistCloneToKey); From 016490656f9226dad9cd85ed1f51f7ec731f7edf Mon Sep 17 00:00:00 2001 From: Msjoinder Date: Sun, 22 Jun 2014 20:42:30 +0000 Subject: [PATCH 4/4] update function documentation --- src/openpgp.js | 20 ++++++++++---------- src/worker/async_proxy.js | 32 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/openpgp.js b/src/openpgp.js index 1dec924b..41701386 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -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,7 +52,7 @@ function initWorker(path) { /** * Encrypts message text with keys - * @param {Array} keys array of keys, used to encrypt the message + * @param {(Array|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 @@ -79,7 +79,7 @@ function encryptMessage(keys, text, callback) { /** * Signs message text and encrypts it - * @param {Array} publicKeys array of keys, used to encrypt the message + * @param {(Array|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 @@ -130,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} publicKeys public keys to verify signatures + * @param {(Array|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}>}} @@ -162,7 +162,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg, callback) { /** * Signs a cleartext message - * @param {Array} privateKeys private key with decrypted secret key data to sign cleartext + * @param {(Array|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 @@ -187,7 +187,7 @@ function signClearMessage(privateKeys, text, callback) { /** * Verifies signatures of cleartext signed message - * @param {Array} publicKeys public keys to verify signatures + * @param {(Array|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}>}} diff --git a/src/worker/async_proxy.js b/src/worker/async_proxy.js index b7c01788..bdb5aab4 100644 --- a/src/worker/async_proxy.js +++ b/src/worker/async_proxy.js @@ -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,7 +96,7 @@ AsyncProxy.prototype.terminate = function() { /** * Encrypts message text with keys - * @param {Array} keys array of keys, used to encrypt the message + * @param {(Array|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 */ @@ -108,7 +108,7 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { return key.toPacketlist(); }); this.worker.postMessage({ - event: 'encrypt-message', + event: 'encrypt-message', keys: keys, text: text }); @@ -117,7 +117,7 @@ AsyncProxy.prototype.encryptMessage = function(keys, text, callback) { /** * Signs message text and encrypts it - * @param {Array} publicKeys array of keys, used to encrypt the message + * @param {(Array|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 @@ -131,7 +131,7 @@ AsyncProxy.prototype.signAndEncryptMessage = function(publicKeys, privateKey, te }); privateKey = privateKey.toPacketlist(); this.worker.postMessage({ - event: 'sign-and-encrypt-message', + event: 'sign-and-encrypt-message', publicKeys: publicKeys, privateKey: privateKey, text: text @@ -149,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 }); @@ -159,7 +159,7 @@ 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} publicKeys public keys to verify signatures + * @param {(Array|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 @@ -173,7 +173,7 @@ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, return key.toPacketlist(); }); this.worker.postMessage({ - event: 'decrypt-and-verify-message', + event: 'decrypt-and-verify-message', privateKey: privateKey, publicKeys: publicKeys, message: message @@ -191,7 +191,7 @@ AsyncProxy.prototype.decryptAndVerifyMessage = function(privateKey, publicKeys, /** * Signs a cleartext message - * @param {Array} privateKeys private key with decrypted secret key data to sign cleartext + * @param {(Array|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 */ @@ -203,7 +203,7 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { return key.toPacketlist(); }); this.worker.postMessage({ - event: 'sign-clear-message', + event: 'sign-clear-message', privateKeys: privateKeys, text: text }); @@ -212,7 +212,7 @@ AsyncProxy.prototype.signClearMessage = function(privateKeys, text, callback) { /** * Verifies signatures of cleartext signed message - * @param {Array} publicKeys public keys to verify signatures + * @param {(Array|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 */ @@ -224,7 +224,7 @@ AsyncProxy.prototype.verifyClearSignedMessage = function(publicKeys, message, ca return key.toPacketlist(); }); this.worker.postMessage({ - event: 'verify-clear-signed-message', + event: 'verify-clear-signed-message', publicKeys: publicKeys, message: message }); @@ -251,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) {