From 18ec54bf4bf22768c93b819b64d250dc03082ac3 Mon Sep 17 00:00:00 2001 From: Daniel Huigens Date: Thu, 25 Mar 2021 16:55:45 +0100 Subject: [PATCH] Fetch Streams ponyfill on demand in lightweight build --- README.md | 18 +- package-lock.json | 6 +- package.json | 2 +- src/cleartext.js | 3 +- src/crypto/hash/index.js | 2 +- src/crypto/mode/cfb.js | 2 +- src/encoding/armor.js | 2 +- src/encoding/base64.js | 2 +- src/index.js | 3 +- src/message.js | 17 +- src/openpgp.js | 2 +- src/packet/aead_encrypted_data.js | 2 +- src/packet/compressed_data.js | 26 ++- src/packet/literal_data.js | 2 +- src/packet/one_pass_signature.js | 2 +- src/packet/packet.js | 2 +- src/packet/packetlist.js | 2 +- src/packet/signature.js | 2 +- .../sym_encrypted_integrity_protected_data.js | 2 +- src/packet/symmetrically_encrypted_data.js | 2 +- src/util.js | 2 +- test/general/brainpool.js | 10 +- test/general/config.js | 10 +- test/general/ecc_nist.js | 10 +- test/general/ecc_secp256k1.js | 4 +- test/general/key.js | 38 ++-- test/general/openpgp.js | 190 +++++++++--------- test/general/packet.js | 4 +- test/general/signature.js | 53 +++-- test/general/streaming.js | 181 +++++++++++------ test/general/x25519.js | 10 +- test/security/unsigned_subpackets.js | 2 +- test/typescript/definitions.ts | 14 +- test/worker/worker_example.js | 2 +- 34 files changed, 359 insertions(+), 272 deletions(-) diff --git a/README.md b/README.md index 6d7443f1..c07d5bf8 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ Encryption will use the algorithm specified in config.preferredSymmetricAlgorith ```js (async () => { - const message = openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01])); + const message = await openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01])); const encrypted = await openpgp.encrypt({ message, // input as Message object passwords: ['secret stuff'], // multiple passwords possible @@ -216,7 +216,7 @@ const openpgp = require('openpgp'); // use as CommonJS, AMD, ES6 module or via w await privateKey.decrypt(passphrase); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText('Hello, World!'), // input as Message object + message: await openpgp.Message.fromText('Hello, World!'), // input as Message object publicKeys: publicKey, // for encryption privateKeys: privateKey // for signing (optional) }); @@ -257,7 +257,7 @@ Encrypt with multiple public keys: const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored }); await privateKey.decrypt(passphrase) - const message = openpgp.Message.fromText(message); + const message = await openpgp.Message.fromText(message); const encrypted = await openpgp.encrypt({ message:, // input as Message object publicKeys, // for encryption @@ -274,7 +274,7 @@ It's possible to change that behaviour by enabling compression through the confi ```js (async () => { - const message = openpgp.Message.fromBinary(new Uint8Array([0x01, 0x02, 0x03])); // or .fromText('string') + const message = await openpgp.Message.fromBinary(new Uint8Array([0x01, 0x02, 0x03])); // or .fromText('string') const encrypted = await openpgp.encrypt({ message, passwords: ['secret stuff'], // multiple passwords possible @@ -306,7 +306,7 @@ Where the value can be any of: } }); - const message = openpgp.Message.fromBinary(readableStream); + const message = await openpgp.Message.fromBinary(readableStream); const encrypted = await openpgp.encrypt({ message, // input as Message object passwords: ['secret stuff'], // multiple passwords possible @@ -361,7 +361,7 @@ its [Reader class](https://openpgpjs.org/web-stream-tools/Reader.html). }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(readableStream), // input as Message object + message: await openpgp.Message.fromText(readableStream), // input as Message object publicKeys: publicKey, // for encryption privateKeys: privateKey // for signing (optional) }); @@ -457,7 +457,7 @@ Using the private key: const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored }); await privateKey.decrypt(passphrase); - const unsignedMessage = openpgp.CleartextMessage.fromText('Hello, World!'); + const unsignedMessage = await openpgp.CleartextMessage.fromText('Hello, World!'); const cleartextMessage = await openpgp.sign({ message: unsignedMessage, // CleartextMessage or Message object privateKeys: privateKey // for signing @@ -497,7 +497,7 @@ Using the private key: const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored }); await privateKey.decrypt(passphrase); - const cleartextMessage = openpgp.CleartextMessage.fromText('Hello, World!'); + const cleartextMessage = await openpgp.CleartextMessage.fromText('Hello, World!'); const detachedSignature = await openpgp.sign({ message: cleartextMessage, // CleartextMessage or Message object privateKeys: privateKey, // for signing @@ -544,7 +544,7 @@ Using the private key: const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored }); await privateKey.decrypt(passphrase); - const message = openpgp.Message.fromBinary(readableStream); // or .fromText(readableStream: ReadableStream) + const message = await openpgp.Message.fromBinary(readableStream); // or .fromText(readableStream: ReadableStream) const signatureArmored = await openpgp.sign({ message, privateKeys: privateKey // for signing diff --git a/package-lock.json b/package-lock.json index 71d14217..243a008b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -261,9 +261,9 @@ "dev": true }, "@openpgp/web-stream-tools": { - "version": "0.0.2", - "resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.2.tgz", - "integrity": "sha512-6RDYscGNmPEB/blocMH6bbyu4x0SXZKVHe1n1jHTonGyDJSMhPub99G+zFzRfXVjWamim2mXUPa7gfyK3tKJ7g==", + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/@openpgp/web-stream-tools/-/web-stream-tools-0.0.3.tgz", + "integrity": "sha512-AEcTCwFZSl6LMpFdDbOX1fv+9GwHpkF3h3DZagdFLPNE2yqx6ol6Rb/OnuMiKnpoGm09smCmNb+gHpcqp3Oz0Q==", "dev": true, "requires": { "@mattiasbuelens/web-streams-adapter": "0.1.0-alpha.5", diff --git a/package.json b/package.json index bbed1532..ae80fe13 100644 --- a/package.json +++ b/package.json @@ -58,7 +58,7 @@ "@openpgp/pako": "^1.0.11", "@openpgp/seek-bzip": "^1.0.5-git", "@openpgp/tweetnacl": "^1.0.3", - "@openpgp/web-stream-tools": "0.0.2", + "@openpgp/web-stream-tools": "0.0.3", "@rollup/plugin-commonjs": "^11.1.0", "@rollup/plugin-node-resolve": "^7.1.3", "@rollup/plugin-replace": "^2.3.2", diff --git a/src/cleartext.js b/src/cleartext.js index 51d8b78a..04074b3d 100644 --- a/src/cleartext.js +++ b/src/cleartext.js @@ -124,8 +124,9 @@ export class CleartextMessage { * Creates a new CleartextMessage object from text * @param {String} text * @static + * @async */ - static fromText(text) { + static async fromText(text) { return new CleartextMessage(text); } } diff --git a/src/crypto/hash/index.js b/src/crypto/hash/index.js index facd1c7c..b636a016 100644 --- a/src/crypto/hash/index.js +++ b/src/crypto/hash/index.js @@ -12,7 +12,7 @@ import sha224 from 'hash.js/lib/hash/sha/224'; import sha384 from 'hash.js/lib/hash/sha/384'; import sha512 from 'hash.js/lib/hash/sha/512'; import { ripemd160 } from 'hash.js/lib/hash/ripemd'; -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import md5 from './md5'; import util from '../../util'; import defaultConfig from '../../config'; diff --git a/src/crypto/mode/cfb.js b/src/crypto/mode/cfb.js index ba1a3036..c1162e99 100644 --- a/src/crypto/mode/cfb.js +++ b/src/crypto/mode/cfb.js @@ -24,7 +24,7 @@ import { AES_CFB } from '@openpgp/asmcrypto.js/dist_es8/aes/cfb'; -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import * as cipher from '../cipher'; import util from '../../util'; diff --git a/src/encoding/armor.js b/src/encoding/armor.js index e19bf05d..09359ed8 100644 --- a/src/encoding/armor.js +++ b/src/encoding/armor.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import * as base64 from './base64.js'; import enums from '../enums.js'; import util from '../util'; diff --git a/src/encoding/base64.js b/src/encoding/base64.js index 20df84ac..dfea632d 100644 --- a/src/encoding/base64.js +++ b/src/encoding/base64.js @@ -16,7 +16,7 @@ * @private */ -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import util from '../util'; const Buffer = util.getNodeBuffer(); diff --git a/src/index.js b/src/index.js index 9104f262..821e92e0 100644 --- a/src/index.js +++ b/src/index.js @@ -21,7 +21,8 @@ export { CleartextMessage, readCleartextMessage } from './cleartext'; export * from './packet'; -export { default as stream } from '@openpgp/web-stream-tools'; +import * as streamMod from '@openpgp/web-stream-tools'; // eslint-disable-line +export const stream = streamMod; export * from './encoding/armor'; diff --git a/src/message.js b/src/message.js index 12c26957..b1ada838 100644 --- a/src/message.js +++ b/src/message.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import { armor, unarmor } from './encoding/armor'; import KeyID from './type/keyid'; import defaultConfig from './config'; @@ -653,9 +653,13 @@ export class Message { * @param {'utf8'|'binary'|'text'|'mime'} [type='utf8'] - Data packet type * @returns {Message} New message object. * @static + * @async */ - static fromText(text, filename, date = new Date(), type = 'utf8') { + static async fromText(text, filename, date = new Date(), type = 'utf8') { const streamType = util.isStream(text); + if (streamType) { + await stream.loadStreamsPonyfill(); + } if (streamType === 'node') { text = stream.nodeToWeb(text); } @@ -680,12 +684,16 @@ export class Message { * @param {'utf8'|'binary'|'text'|'mime'} [type='binary'] - Data packet type * @returns {Message} New message object. * @static + * @async */ - static fromBinary(bytes, filename, date = new Date(), type = 'binary') { + static async fromBinary(bytes, filename, date = new Date(), type = 'binary') { const streamType = util.isStream(bytes); if (!util.isUint8Array(bytes) && !streamType) { throw new Error('Data must be in the form of a Uint8Array or Stream'); } + if (streamType) { + await stream.loadStreamsPonyfill(); + } if (streamType === 'node') { bytes = stream.nodeToWeb(bytes); } @@ -856,6 +864,9 @@ export async function readMessage({ armoredMessage, binaryMessage, config }) { throw new Error('readMessage: must pass options object containing `armoredMessage` or `binaryMessage`'); } const streamType = util.isStream(input); + if (streamType) { + await stream.loadStreamsPonyfill(); + } if (streamType === 'node') { input = stream.nodeToWeb(input); } diff --git a/src/openpgp.js b/src/openpgp.js index cd764a7c..713dcbdf 100644 --- a/src/openpgp.js +++ b/src/openpgp.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import { createReadableStreamWrapper } from '@mattiasbuelens/web-streams-adapter'; import { Message } from './message'; import { CleartextMessage } from './cleartext'; diff --git a/src/packet/aead_encrypted_data.js b/src/packet/aead_encrypted_data.js index 44f0ecbd..052529cc 100644 --- a/src/packet/aead_encrypted_data.js +++ b/src/packet/aead_encrypted_data.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import crypto from '../crypto'; import enums from '../enums'; import util from '../util'; diff --git a/src/packet/compressed_data.js b/src/packet/compressed_data.js index 2a943e0d..2ef36935 100644 --- a/src/packet/compressed_data.js +++ b/src/packet/compressed_data.js @@ -19,7 +19,7 @@ import { Deflate } from '@openpgp/pako/lib/deflate'; import { Inflate } from '@openpgp/pako/lib/inflate'; import { Z_SYNC_FLUSH, Z_FINISH } from '@openpgp/pako/lib/zlib/constants'; import { decode as BunzipDecode } from '@openpgp/seek-bzip'; -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import enums from '../enums'; import util from '../util'; import defaultConfig from '../config'; @@ -146,13 +146,19 @@ function uncompressed(data) { return data; } -function node_zlib(func, options = {}) { +function node_zlib(func, create, options = {}) { return function (data) { - const webStream = stream.nodeToWeb(stream.webToNode(data).pipe(func(options))); - if (stream.isStream(data) === 'array') { - return stream.fromAsync(() => stream.readToEnd(webStream)); + if (!util.isStream(data) || stream.isArrayStream(data)) { + return stream.fromAsync(() => stream.readToEnd(data).then(data => { + return new Promise((resolve, reject) => { + func(data, options, (err, result) => { + if (err) return reject(err); + resolve(result); + }); + }); + })); } - return webStream; + return stream.nodeToWeb(stream.webToNode(data).pipe(create(options))); }; } @@ -180,8 +186,8 @@ function bzip2(func) { } const compress_fns = nodeZlib ? { - zip: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.createDeflateRaw, { level })(compressed), - zlib: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.createDeflate, { level })(compressed) + zip: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.deflateRaw, nodeZlib.createDeflateRaw, { level })(compressed), + zlib: /*#__PURE__*/ (compressed, level) => node_zlib(nodeZlib.deflate, nodeZlib.createDeflate, { level })(compressed) } : { zip: /*#__PURE__*/ (compressed, level) => pako_zlib(Deflate, { raw: true, level })(compressed), zlib: /*#__PURE__*/ (compressed, level) => pako_zlib(Deflate, { level })(compressed) @@ -189,8 +195,8 @@ const compress_fns = nodeZlib ? { const decompress_fns = nodeZlib ? { uncompressed: uncompressed, - zip: /*#__PURE__*/ node_zlib(nodeZlib.createInflateRaw), - zlib: /*#__PURE__*/ node_zlib(nodeZlib.createInflate), + zip: /*#__PURE__*/ node_zlib(nodeZlib.inflateRaw, nodeZlib.createInflateRaw), + zlib: /*#__PURE__*/ node_zlib(nodeZlib.inflate, nodeZlib.createInflate), bzip2: /*#__PURE__*/ bzip2(BunzipDecode) } : { uncompressed: uncompressed, diff --git a/src/packet/literal_data.js b/src/packet/literal_data.js index d9dc1217..9534627b 100644 --- a/src/packet/literal_data.js +++ b/src/packet/literal_data.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import enums from '../enums'; import util from '../util'; diff --git a/src/packet/one_pass_signature.js b/src/packet/one_pass_signature.js index f4eac15b..e5ebbe4d 100644 --- a/src/packet/one_pass_signature.js +++ b/src/packet/one_pass_signature.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import SignaturePacket from './signature'; import KeyID from '../type/keyid'; import enums from '../enums'; diff --git a/src/packet/packet.js b/src/packet/packet.js index 084cfd9f..b99ee3aa 100644 --- a/src/packet/packet.js +++ b/src/packet/packet.js @@ -23,7 +23,7 @@ * @private */ -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import enums from '../enums'; import util from '../util'; diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index 30148cc1..742c1714 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -1,4 +1,4 @@ -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import { readPackets, supportsStreaming, writeTag, writeHeader, diff --git a/src/packet/signature.js b/src/packet/signature.js index 17a6219c..bf2f8a56 100644 --- a/src/packet/signature.js +++ b/src/packet/signature.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import { readSimpleLength, writeSimpleLength } from './packet'; import KeyID from '../type/keyid.js'; import crypto from '../crypto'; diff --git a/src/packet/sym_encrypted_integrity_protected_data.js b/src/packet/sym_encrypted_integrity_protected_data.js index 6794f1fc..b52aefe4 100644 --- a/src/packet/sym_encrypted_integrity_protected_data.js +++ b/src/packet/sym_encrypted_integrity_protected_data.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import crypto from '../crypto'; import enums from '../enums'; import util from '../util'; diff --git a/src/packet/symmetrically_encrypted_data.js b/src/packet/symmetrically_encrypted_data.js index 3df67adb..b532a346 100644 --- a/src/packet/symmetrically_encrypted_data.js +++ b/src/packet/symmetrically_encrypted_data.js @@ -15,7 +15,7 @@ // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import crypto from '../crypto'; import enums from '../enums'; import util from '../util'; diff --git a/src/util.js b/src/util.js index 9c153c55..4b3ea34e 100644 --- a/src/util.js +++ b/src/util.js @@ -23,7 +23,7 @@ * @private */ -import stream from '@openpgp/web-stream-tools'; +import * as stream from '@openpgp/web-stream-tools'; import { getBigInteger } from './biginteger'; const debugMode = globalThis.process && globalThis.process.env.NODE_ENV === 'development'; diff --git a/test/general/brainpool.js b/test/general/brainpool.js index 567e1f6f..39d4764f 100644 --- a/test/general/brainpool.js +++ b/test/general/brainpool.js @@ -210,7 +210,7 @@ EJ4QcD/oQ6x1M/8X/iKQCtxZP8RnlrbH7ExkNON5s5g= }); it('Sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); - const signed = await openpgp.sign({ privateKeys: [romeoPrivate], message: openpgp.CleartextMessage.fromText(data.romeo.message) }); + const signed = await openpgp.sign({ privateKeys: [romeoPrivate], message: await openpgp.CleartextMessage.fromText(data.romeo.message) }); const romeoPublic = await load_pub_key('romeo'); const msg = await openpgp.readCleartextMessage({ cleartextMessage: signed }); const result = await openpgp.verify({ publicKeys: [romeoPublic], message: msg }); @@ -260,7 +260,7 @@ EJ4QcD/oQ6x1M/8X/iKQCtxZP8RnlrbH7ExkNON5s5g= it('Encrypt and sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); const julietPublic = await load_pub_key('juliet'); - const encrypted = await openpgp.encrypt({ publicKeys: [julietPublic], privateKeys: [romeoPrivate], message: openpgp.Message.fromText(data.romeo.message) }); + const encrypted = await openpgp.encrypt({ publicKeys: [julietPublic], privateKeys: [romeoPrivate], message: await openpgp.Message.fromText(data.romeo.message) }); const message = await openpgp.readMessage({ armoredMessage: encrypted }); const romeoPublic = await load_pub_key('romeo'); @@ -290,21 +290,21 @@ function omnibus() { const bye = secondKey.key; const pubBye = bye.toPublic(); - const cleartextMessage = await openpgp.sign({ message: openpgp.CleartextMessage.fromText(testData), privateKeys: hi }); + const cleartextMessage = await openpgp.sign({ message: await openpgp.CleartextMessage.fromText(testData), privateKeys: hi }); await openpgp.verify({ message: await openpgp.readCleartextMessage({ cleartextMessage }), publicKeys: pubHi }).then(output => expect(output.signatures[0].valid).to.be.true); // Verifying detached signature await openpgp.verify({ - message: openpgp.Message.fromText(util.removeTrailingSpaces(testData)), + message: await openpgp.Message.fromText(util.removeTrailingSpaces(testData)), publicKeys: pubHi, signature: (await openpgp.readCleartextMessage({ cleartextMessage })).signature }).then(output => expect(output.signatures[0].valid).to.be.true); // Encrypting and signing const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(testData2), + message: await openpgp.Message.fromText(testData2), publicKeys: [pubBye], privateKeys: [hi] }); diff --git a/test/general/config.js b/test/general/config.js index b80e508c..5af1fca0 100644 --- a/test/general/config.js +++ b/test/general/config.js @@ -149,7 +149,7 @@ module.exports = () => describe('Custom configuration', function() { try { const passwords = ['12345678']; - const message = openpgp.Message.fromText("test"); + const message = await openpgp.Message.fromText("test"); const armored = await openpgp.encrypt({ message, passwords }); const encrypted = await openpgp.readMessage({ armoredMessage: armored }); @@ -225,7 +225,7 @@ module.exports = () => describe('Custom configuration', function() { const { privateKeyArmored } = await openpgp.generateKey({ userIDs }); const key = await openpgp.readKey({ armoredKey: privateKeyArmored }); - const message = openpgp.Message.fromText("test"); + const message = await openpgp.Message.fromText("test"); const opt = { message, privateKeys: key, @@ -235,7 +235,7 @@ module.exports = () => describe('Custom configuration', function() { opt.detached = true; await expect(openpgp.sign(opt)).to.be.rejectedWith(/Insecure hash algorithm/); - const clearText = openpgp.CleartextMessage.fromText("test"); + const clearText = await openpgp.CleartextMessage.fromText("test"); const opt2 = { message: clearText, privateKeys: key, @@ -255,7 +255,7 @@ module.exports = () => describe('Custom configuration', function() { const config = { rejectMessageHashAlgorithms: new Set([openpgp.enums.hash.sha256, openpgp.enums.hash.sha512]) }; - const message = openpgp.Message.fromText("test"); + const message = await openpgp.Message.fromText("test"); const signed = await openpgp.sign({ message, privateKeys: key }); const opt = { message: await openpgp.readMessage({ armoredMessage: signed }), @@ -274,7 +274,7 @@ module.exports = () => describe('Custom configuration', function() { const { signatures: [sig2] } = await openpgp.verify(opt2); await expect(sig2.error).to.match(/Insecure message hash algorithm/); - const cleartext = openpgp.CleartextMessage.fromText("test"); + const cleartext = await openpgp.CleartextMessage.fromText("test"); const signedCleartext = await openpgp.sign({ message: cleartext, privateKeys: key }); const opt3 = { message: await openpgp.readCleartextMessage({ cleartextMessage: signedCleartext }), diff --git a/test/general/ecc_nist.js b/test/general/ecc_nist.js index 20629d04..3ff735c1 100644 --- a/test/general/ecc_nist.js +++ b/test/general/ecc_nist.js @@ -20,21 +20,21 @@ module.exports = () => describe('Elliptic Curve Cryptography for NIST P-256,P-38 const bye = secondKey.key; const pubBye = bye.toPublic(); - const cleartextMessage = await openpgp.sign({ message: openpgp.CleartextMessage.fromText(testData), privateKeys: hi }); + const cleartextMessage = await openpgp.sign({ message: await openpgp.CleartextMessage.fromText(testData), privateKeys: hi }); await openpgp.verify({ message: await openpgp.readCleartextMessage({ cleartextMessage }), publicKeys: pubHi }).then(output => expect(output.signatures[0].valid).to.be.true); // Verifying detached signature await openpgp.verify({ - message: openpgp.Message.fromText(util.removeTrailingSpaces(testData)), + message: await openpgp.Message.fromText(util.removeTrailingSpaces(testData)), publicKeys: pubHi, signature: (await openpgp.readCleartextMessage({ cleartextMessage })).signature }).then(output => expect(output.signatures[0].valid).to.be.true); // Encrypting and signing const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(testData2), + message: await openpgp.Message.fromText(testData2), publicKeys: [pubBye], privateKeys: [hi] }); @@ -56,7 +56,7 @@ module.exports = () => describe('Elliptic Curve Cryptography for NIST P-256,P-38 const testData = input.createSomeMessage(); const options = { userIDs: { name: "Hi", email: "hi@hel.lo" }, curve: "p256" }; const firstKey = await openpgp.generateKey(options); - const signature = await openpgp.sign({ message: openpgp.CleartextMessage.fromText(testData), privateKeys: firstKey.key }); + const signature = await openpgp.sign({ message: await openpgp.CleartextMessage.fromText(testData), privateKeys: firstKey.key }); const msg = await openpgp.readCleartextMessage({ cleartextMessage: signature }); const result = await openpgp.verify({ message: msg, publicKeys: firstKey.key.toPublic() }); expect(result.signatures[0].valid).to.be.true; @@ -69,7 +69,7 @@ module.exports = () => describe('Elliptic Curve Cryptography for NIST P-256,P-38 options = { userIDs: { name: "Bye", email: "bye@good.bye" }, curve: "p256" }; const secondKey = await openpgp.generateKey(options); const encrypted = await openpgp.encrypt( - { message: openpgp.Message.fromText(testData), + { message: await openpgp.Message.fromText(testData), publicKeys: [secondKey.key.toPublic()], privateKeys: [firstKey.key] } ); diff --git a/test/general/ecc_secp256k1.js b/test/general/ecc_secp256k1.js index 4147f846..5ad7f7fd 100644 --- a/test/general/ecc_secp256k1.js +++ b/test/general/ecc_secp256k1.js @@ -184,7 +184,7 @@ module.exports = () => describe('Elliptic Curve Cryptography for secp256k1 curve }); it('Sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); - const signed = await openpgp.sign({ privateKeys: [romeoPrivate], message: openpgp.CleartextMessage.fromText(data.romeo.message) }); + const signed = await openpgp.sign({ privateKeys: [romeoPrivate], message: await openpgp.CleartextMessage.fromText(data.romeo.message) }); const romeoPublic = await load_pub_key('romeo'); const msg = await openpgp.readCleartextMessage({ cleartextMessage: signed }); const result = await openpgp.verify({ publicKeys: [romeoPublic], message: msg }); @@ -208,7 +208,7 @@ module.exports = () => describe('Elliptic Curve Cryptography for secp256k1 curve it('Encrypt and sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); const julietPublic = await load_pub_key('juliet'); - const encrypted = await openpgp.encrypt({ publicKeys: [julietPublic], privateKeys: [romeoPrivate], message: openpgp.Message.fromText(data.romeo.message) }); + const encrypted = await openpgp.encrypt({ publicKeys: [julietPublic], privateKeys: [romeoPrivate], message: await openpgp.Message.fromText(data.romeo.message) }); const message = await openpgp.readMessage({ armoredMessage: encrypted }); const romeoPublic = await load_pub_key('romeo'); diff --git a/test/general/key.js b/test/general/key.js index 16b424a9..6c9f74e4 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -2166,7 +2166,7 @@ function versionSpecificTests() { const opt = { userIDs: { name: 'test', email: 'a@b.com' }, passphrase: '123' }; const { key } = await openpgp.generateKey(opt); return openpgp.encrypt({ - message: openpgp.Message.fromText('hello'), + message: await openpgp.Message.fromText('hello'), publicKeys: key }).then(async armoredMessage => openpgp.decrypt({ message: await openpgp.readMessage({ armoredMessage }), @@ -2581,12 +2581,12 @@ function versionSpecificTests() { const userID = { name: 'test', email: 'a@b.com' }; const key = await openpgp.readKey({ armoredKey: key_without_subkey }); const opt = { privateKey: key, userIDs: [userID] }; - return openpgp.reformatKey(opt).then(function(newKey) { + return openpgp.reformatKey(opt).then(async function(newKey) { newKey = newKey.key; expect(newKey.users.length).to.equal(1); expect(newKey.users[0].userID.userID).to.equal('test '); expect(newKey.isDecrypted()).to.be.true; - return openpgp.sign({ message: openpgp.CleartextMessage.fromText('hello'), privateKeys: newKey, armor: true }).then(async function(signed) { + return openpgp.sign({ message: await openpgp.CleartextMessage.fromText('hello'), privateKeys: newKey, armor: true }).then(async function(signed) { return openpgp.verify( { message: await openpgp.readCleartextMessage({ cleartextMessage: signed }), publicKeys: newKey.toPublic() } ).then(async function(verified) { @@ -2628,9 +2628,9 @@ function versionSpecificTests() { key = key.key; opt.privateKey = key; opt.userIDs = userID2; - return openpgp.reformatKey(opt).then(function(newKey) { + return openpgp.reformatKey(opt).then(async function(newKey) { newKey = newKey.key; - return openpgp.encrypt({ message: openpgp.Message.fromText('hello'), publicKeys: newKey.toPublic(), privateKeys: newKey, armor: true }).then(async function(encrypted) { + return openpgp.encrypt({ message: await openpgp.Message.fromText('hello'), publicKeys: newKey.toPublic(), privateKeys: newKey, armor: true }).then(async function(encrypted) { return openpgp.decrypt({ message: await openpgp.readMessage({ armoredMessage: encrypted }), privateKeys: newKey, publicKeys: newKey.toPublic() }).then(function(decrypted) { expect(decrypted.data).to.equal('hello'); expect(decrypted.signatures[0].valid).to.be.true; @@ -3016,7 +3016,7 @@ module.exports = () => describe('Key', function() { expect(key.primaryKey.isDummy()).to.be.false; key.primaryKey.makeDummy(); expect(key.primaryKey.isDummy()).to.be.true; - await expect(openpgp.sign({ message: openpgp.Message.fromText('test'), privateKeys: [key], config: { minRSABits: 1024 } })).to.be.fulfilled; + await expect(openpgp.sign({ message: await openpgp.Message.fromText('test'), privateKeys: [key], config: { minRSABits: 1024 } })).to.be.fulfilled; }); it('makeDummy() - should work for encrypted keys', async function() { @@ -3423,10 +3423,10 @@ VYGdb3eNlV8CfoEC expect(sessionKey.algorithm).to.equal('aes128'); const config = { minRSABits: 1024 }; await openpgp.encrypt({ - message: openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIDs: { name: 'Test User', email: 'b@c.com' }, armor: false, config + message: await openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIDs: { name: 'Test User', email: 'b@c.com' }, armor: false, config }); await expect(openpgp.encrypt({ - message: openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIDs: { name: 'Test User', email: 'c@c.com' }, armor: false, config + message: await openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIDs: { name: 'Test User', email: 'c@c.com' }, armor: false, config })).to.be.rejectedWith('Could not find user that matches that user ID'); }); @@ -3435,7 +3435,7 @@ VYGdb3eNlV8CfoEC expect(publicKey.users.length).to.equal(0); const privateKey = await openpgp.readKey({ armoredKey: uidlessKey }); await privateKey.decrypt('correct horse battery staple'); - await expect(openpgp.encrypt({ message: openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false })).to.be.rejectedWith('Could not find primary user'); + await expect(openpgp.encrypt({ message: await openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false })).to.be.rejectedWith('Could not find primary user'); }); it('Sign - specific user', async function() { @@ -3453,17 +3453,17 @@ VYGdb3eNlV8CfoEC privateKey.users[1].selfCertifications[0].preferredHashAlgorithms = [openpgp.enums.hash.sha512]; const config = { minRSABits: 1024 }; const signed = await openpgp.sign({ - message: openpgp.Message.fromText('hello'), privateKeys: privateKey, fromUserIDs: { name: 'Test McTestington', email: 'test@example.com' }, armor: false, config + message: await openpgp.Message.fromText('hello'), privateKeys: privateKey, fromUserIDs: { name: 'Test McTestington', email: 'test@example.com' }, armor: false, config }); const signature = await openpgp.readMessage({ binaryMessage: signed }); expect(signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText('hello'), passwords: 'test', privateKeys: privateKey, fromUserIDs: { name: 'Test McTestington', email: 'test@example.com' }, armor: false, config + message: await openpgp.Message.fromText('hello'), passwords: 'test', privateKeys: privateKey, fromUserIDs: { name: 'Test McTestington', email: 'test@example.com' }, armor: false, config }); const { signatures } = await openpgp.decrypt({ message: await openpgp.readMessage({ binaryMessage: encrypted }), passwords: 'test' }); expect(signatures[0].signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); await expect(openpgp.encrypt({ - message: openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserIDs: { name: 'Not Test McTestington', email: 'test@example.com' }, armor: false, config + message: await openpgp.Message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserIDs: { name: 'Not Test McTestington', email: 'test@example.com' }, armor: false, config })).to.be.rejectedWith('Could not find user that matches that user ID'); }); @@ -3499,7 +3499,7 @@ VYGdb3eNlV8CfoEC it('Reject encryption with revoked primary user', async function() { const key = await openpgp.readKey({ armoredKey: pub_revoked_subkeys }); - return openpgp.encrypt({ publicKeys: [key], message: openpgp.Message.fromText('random data') }).then(() => { + return openpgp.encrypt({ publicKeys: [key], message: await openpgp.Message.fromText('random data') }).then(() => { throw new Error('encryptSessionKey should not encrypt with revoked public key'); }).catch(function(error) { expect(error.message).to.equal('Error encrypting message: Primary user is revoked'); @@ -3510,7 +3510,7 @@ VYGdb3eNlV8CfoEC const key = await openpgp.readKey({ armoredKey: pub_revoked_subkeys }); key.revocationSignatures = []; key.users[0].revocationSignatures = []; - return openpgp.encrypt({ publicKeys: [key], message: openpgp.Message.fromText('random data'), date: new Date(1386842743000) }).then(() => { + return openpgp.encrypt({ publicKeys: [key], message: await openpgp.Message.fromText('random data'), date: new Date(1386842743000) }).then(() => { throw new Error('encryptSessionKey should not encrypt with revoked public key'); }).catch(function(error) { expect(error.message).to.equal('Error encrypting message: Could not find valid encryption key packet in key ' + key.getKeyID().toHex() + ': Subkey is revoked'); @@ -3519,7 +3519,7 @@ VYGdb3eNlV8CfoEC it('Reject encryption with key revoked with appended revocation cert', async function() { const key = await openpgp.readKey({ armoredKey: pub_revoked_with_cert }); - return openpgp.encrypt({ publicKeys: [key], message: openpgp.Message.fromText('random data') }).then(() => { + return openpgp.encrypt({ publicKeys: [key], message: await openpgp.Message.fromText('random data') }).then(() => { throw new Error('encryptSessionKey should not encrypt with revoked public key'); }).catch(function(error) { expect(error.message).to.equal('Error encrypting message: Primary key is revoked'); @@ -3734,7 +3734,7 @@ VYGdb3eNlV8CfoEC expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('eddsa'); await subKey.verify(newPrivateKey.primaryKey); expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey); - const signed = await openpgp.sign({ message: openpgp.Message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false }); + const signed = await openpgp.sign({ message: await openpgp.Message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false }); const message = await openpgp.readMessage({ binaryMessage: signed }); const { signatures } = await openpgp.verify({ message, publicKeys: [newPrivateKey.toPublic()] }); expect(signatures).to.exist; @@ -3756,7 +3756,7 @@ VYGdb3eNlV8CfoEC const publicKey = newPrivateKey.toPublic(); await subKey.verify(newPrivateKey.primaryKey); expect(await newPrivateKey.getEncryptionKey()).to.be.equal(subKey); - const encrypted = await openpgp.encrypt({ message: openpgp.Message.fromText(vData), publicKeys: publicKey, armor:false }); + const encrypted = await openpgp.encrypt({ message: await openpgp.Message.fromText(vData), publicKeys: publicKey, armor:false }); expect(encrypted).to.be.exist; const message = await openpgp.readMessage({ binaryMessage: encrypted }); const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey); @@ -3780,7 +3780,7 @@ VYGdb3eNlV8CfoEC expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('rsaEncryptSign'); await subKey.verify(newPrivateKey.primaryKey); expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey); - const signed = await openpgp.sign({ message: openpgp.Message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false }); + const signed = await openpgp.sign({ message: await openpgp.Message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false }); const message = await openpgp.readMessage({ binaryMessage: signed }); const { signatures } = await openpgp.verify({ message, publicKeys: [newPrivateKey.toPublic()] }); expect(signatures).to.exist; @@ -3800,7 +3800,7 @@ VYGdb3eNlV8CfoEC const publicKey = newPrivateKey.toPublic(); const vData = 'the data to encrypted!'; expect(await newPrivateKey.getEncryptionKey()).to.be.equal(subKey); - const encrypted = await openpgp.encrypt({ message: openpgp.Message.fromText(vData), publicKeys: publicKey, armor:false }); + const encrypted = await openpgp.encrypt({ message: await openpgp.Message.fromText(vData), publicKeys: publicKey, armor:false }); expect(encrypted).to.be.exist; const message = await openpgp.readMessage({ binaryMessage: encrypted }); const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey); diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 521265bc..d9b28f5a 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -864,14 +864,14 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const commentStringVal = openpgp.config.commentString; try { - const encryptedDefault = await openpgp.encrypt({ publicKeys:publicKey, message:openpgp.Message.fromText(plaintext) }); + const encryptedDefault = await openpgp.encrypt({ publicKeys:publicKey, message:await openpgp.Message.fromText(plaintext) }); expect(encryptedDefault).to.exist; expect(encryptedDefault).not.to.match(/^Version:/); expect(encryptedDefault).not.to.match(/^Comment:/); openpgp.config.showComment = true; openpgp.config.commentString = 'different'; - const encryptedWithComment = await openpgp.encrypt({ publicKeys:publicKey, message:openpgp.Message.fromText(plaintext) }); + const encryptedWithComment = await openpgp.encrypt({ publicKeys:publicKey, message:await openpgp.Message.fromText(plaintext) }); expect(encryptedWithComment).to.exist; expect(encryptedWithComment).not.to.match(/^Version:/); expect(encryptedWithComment).to.match(/Comment: different/); @@ -1019,7 +1019,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('Calling decrypt with not decrypted key leads to exception', async function() { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }; const decOpt = { @@ -1147,7 +1147,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with pgp key pair', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }); const decryptedSessionKeys = await openpgp.decryptSessionKeys({ @@ -1164,7 +1164,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with pgp key pair -- trailing spaces', async function () { const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }); const decryptedSessionKeys = await openpgp.decryptSessionKeys({ @@ -1180,7 +1180,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('roundtrip workflow: encrypt, decryptSessionKeys, decrypt with password', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: password1 }); const decryptedSessionKeys = await openpgp.decryptSessionKeys({ @@ -1196,7 +1196,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('roundtrip workflow: encrypt with multiple passwords, decryptSessionKeys, decrypt with multiple passwords', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: [password1, password2] }); const decryptedSessionKeys = await openpgp.decryptSessionKeys({ @@ -1212,7 +1212,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('roundtrip workflow: encrypt twice with one password, decryptSessionKeys, only one session key', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: [password1, password1] }); const decryptedSessionKeys = await openpgp.decryptSessionKeys({ @@ -1250,9 +1250,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { privateKey = decryptedPrivateKey; }); - it('should encrypt then decrypt', function () { + it('should encrypt then decrypt', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }; const decOpt = { @@ -1274,7 +1274,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { await privKeyDE.decrypt(passphrase); const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }; const decOpt = { @@ -1291,9 +1291,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt then decrypt with wildcard', function () { + it('should encrypt then decrypt with wildcard', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, wildcard: true }; @@ -1316,7 +1316,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { await privKeyDE.decrypt(passphrase); const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, wildcard: true }; @@ -1339,7 +1339,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { publicKeys: publicKey }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), sessionKey }); expect(encrypted).to.match(/^-----BEGIN PGP MESSAGE/); @@ -1358,7 +1358,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { algorithm: 'aes256' }; const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), sessionKey: sessionKey, publicKeys: publicKey }; @@ -1381,7 +1381,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { algorithm: 'aes128' }; const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), sessionKey: sessionKey, publicKeys: publicKey }; @@ -1398,9 +1398,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt/sign and decrypt/verify', function () { + it('should encrypt/sign and decrypt/verify', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, privateKeys: privateKey }; @@ -1421,9 +1421,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt/sign and decrypt/verify (no AEAD support)', function () { + it('should encrypt/sign and decrypt/verify (no AEAD support)', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKeyNoAEAD, privateKeys: privateKey }; @@ -1454,7 +1454,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const newPrivateKey = await openpgp.readKey({ armoredKey: newKey.privateKeyArmored }); const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: newPublicKey, privateKeys: newPrivateKey }; @@ -1484,11 +1484,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const newPrivateKey = await openpgp.readKey({ armoredKey: newKey.privateKeyArmored }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: newPublicKey }); const signed = await openpgp.sign({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), privateKeys: newPrivateKey, detached: true }); @@ -1507,9 +1507,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { expect(decrypted.signatures[0].signature.packets.length).to.equal(1); }); - it('should encrypt/sign and decrypt/verify with null string input', function () { + it('should encrypt/sign and decrypt/verify with null string input', async function () { const encOpt = { - message: openpgp.Message.fromText(''), + message: await openpgp.Message.fromText(''), publicKeys: publicKey, privateKeys: privateKey }; @@ -1531,11 +1531,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should encrypt/sign and decrypt/verify with detached signatures', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }); const signed = await openpgp.sign({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), privateKeys: privateKey, detached: true }); @@ -1565,13 +1565,13 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const pubKeyDE = await openpgp.readKey({ armoredKey: pub_key_de }); const signOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), privateKeys: privKeyDE, detached: true }; const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, privateKeys: privateKey }; @@ -1606,13 +1606,13 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to encrypt and decrypt/verify with detached signature as input for encryption with wrong public key', async function () { const signOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), privateKeys: privateKey, detached: true }; const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }; @@ -1639,7 +1639,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to verify decrypted data with wrong public pgp key', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, privateKeys: privateKey }; @@ -1662,7 +1662,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to verify decrypted null string with wrong public pgp key', async function () { const encOpt = { - message: openpgp.Message.fromText(''), + message: await openpgp.Message.fromText(''), publicKeys: publicKey, privateKeys: privateKey }; @@ -1685,7 +1685,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should successfully decrypt signed message without public keys to verify', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, privateKeys: privateKey }; @@ -1707,11 +1707,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to verify decrypted data with wrong public pgp key with detached signatures', async function () { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey }); const signed = await openpgp.sign({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), privateKeys: privateKey, detached: true }); @@ -1740,7 +1740,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const pubKeyDE = await openpgp.readKey({ armoredKey: pub_key_de }); const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: publicKey, privateKeys: [privateKey, privKeyDE] }; @@ -1773,7 +1773,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to decrypt modified message', async function() { const { privateKeyArmored } = await openpgp.generateKey({ curve: 'curve25519', userIDs: [{ email: 'test@email.com' }] }); const key = await openpgp.readKey({ armoredKey: privateKeyArmored }); - const data = await openpgp.encrypt({ message: openpgp.Message.fromBinary(new Uint8Array(500)), publicKeys: [key.toPublic()] }); + const data = await openpgp.encrypt({ message: await openpgp.Message.fromBinary(new Uint8Array(500)), publicKeys: [key.toPublic()] }); let badSumEncrypted = data.replace(/\n=[a-zA-Z0-9/+]{4}/, '\n=aaaa'); if (badSumEncrypted === data) { // checksum was already =aaaa badSumEncrypted = data.replace(/\n=[a-zA-Z0-9/+]{4}/, '\n=bbbb'); @@ -1826,7 +1826,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to decrypt unarmored message with garbage data appended', async function() { const { key } = await openpgp.generateKey({ userIDs: {} }); - const message = await openpgp.encrypt({ message: openpgp.Message.fromText('test'), publicKeys: key, privateKeys: key, armor: false }); + const message = await openpgp.encrypt({ message: await openpgp.Message.fromText('test'), publicKeys: key, privateKeys: key, armor: false }); const encrypted = util.concat([message, new Uint8Array([11])]); await expect((async () => { await openpgp.decrypt({ message: await openpgp.readMessage({ binaryMessage: encrypted }), privateKeys: key, publicKeys: key }); @@ -1848,7 +1848,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { await openpgp.encrypt({ publicKeys: pubKeyDE, privateKeys: privKeyDE, - message: openpgp.Message.fromText(plaintext) + message: await openpgp.Message.fromText(plaintext) }).then(async function (encrypted) { return openpgp.decrypt({ privateKeys: privKeyDE, @@ -1937,9 +1937,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { describe('AES encrypt, decrypt', function() { - it('should encrypt and decrypt with one password', function () { + it('should encrypt and decrypt with one password', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: password1 }; const decOpt = { @@ -1954,9 +1954,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt and decrypt with two passwords', function () { + it('should encrypt and decrypt with two passwords', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: [password1, password2] }; const decOpt = { @@ -1971,9 +1971,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt and decrypt with password and not ascii armor', function () { + it('should encrypt and decrypt with password and not ascii armor', async function () { const encOpt = { - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: password1, armor: false }; @@ -1989,9 +1989,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt and decrypt with binary data', function () { + it('should encrypt and decrypt with binary data', async function () { const encOpt = { - message: openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01])), + message: await openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01])), passwords: password1, armor: false }; @@ -2011,9 +2011,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { describe('Encrypt, decrypt with compression', function() { withCompression(function (modifyCompressionEncryptOptions, verifyCompressionDecrypted) { - it('should encrypt and decrypt with one password', function () { + it('should encrypt and decrypt with one password', async function () { const encOpt = modifyCompressionEncryptOptions({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), passwords: password1 }); const decOpt = { @@ -2046,7 +2046,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { } }); const encrypted = await openpgp.encrypt(modifyCompressionEncryptOptions({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] })); expect(openpgp.stream.isStream(encrypted)).to.equal(useNativeStream ? 'web' : 'ponyfill'); @@ -2087,8 +2087,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { privateKey = decryptedPrivateKey; }); - it('should sign and verify cleartext message', function () { - const message = openpgp.CleartextMessage.fromText(plaintext); + it('should sign and verify cleartext message', async function () { + const message = await openpgp.CleartextMessage.fromText(plaintext); const signOpt = { message, privateKeys: privateKey @@ -2117,7 +2117,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const privKeyDE = await openpgp.readKey({ armoredKey: priv_key_de }); await privKeyDE.decrypt(passphrase); - const message = openpgp.CleartextMessage.fromText(plaintext); + const message = await openpgp.CleartextMessage.fromText(plaintext); const signOpt = { message, privateKeys: [privateKey, privKeyDE] @@ -2146,8 +2146,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { } }); - it('should sign and verify data with detached signatures', function () { - const message = openpgp.Message.fromText(plaintext); + it('should sign and verify data with detached signatures', async function () { + const message = await openpgp.Message.fromText(plaintext); const signOpt = { message, privateKeys: privateKey, @@ -2170,7 +2170,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); it('should sign and fail to verify cleartext message with wrong public pgp key', async function () { - const message = openpgp.CleartextMessage.fromText(plaintext); + const message = await openpgp.CleartextMessage.fromText(plaintext); const signOpt = { message, privateKeys: privateKey @@ -2192,7 +2192,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); it('should sign and fail to verify data with wrong public pgp key with detached signature', async function () { - const message = openpgp.Message.fromText(plaintext); + const message = await openpgp.Message.fromText(plaintext); const signOpt = { message, privateKeys: privateKey, @@ -2215,8 +2215,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign and verify data and not armor', function () { - const message = openpgp.Message.fromText(plaintext); + it('should sign and verify data and not armor', async function () { + const message = await openpgp.Message.fromText(plaintext); const signOpt = { message, privateKeys: privateKey, @@ -2237,9 +2237,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign and verify data and not armor with detached signatures', function () { + it('should sign and verify data and not armor with detached signatures', async function () { const start = util.normalizeDate(); - const message = openpgp.Message.fromText(plaintext); + const message = await openpgp.Message.fromText(plaintext); const signOpt = { message, privateKeys: privateKey, @@ -2264,8 +2264,8 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign and verify data with a date in the past', function () { - const message = openpgp.Message.fromText(plaintext); + it('should sign and verify data with a date in the past', async function () { + const message = await openpgp.Message.fromText(plaintext); const past = new Date(2000); const signOpt = { message, @@ -2302,11 +2302,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign and verify binary data with a date in the future', function () { + it('should sign and verify binary data with a date in the future', async function () { const future = new Date(2040, 5, 5, 5, 5, 5, 0); const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const signOpt = { - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privateKey_2038_2045, detached: true, date: future, @@ -2318,7 +2318,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { format: 'binary' }; return openpgp.sign(signOpt).then(async function (signed) { - verifyOpt.message = openpgp.Message.fromBinary(data); + verifyOpt.message = await openpgp.Message.fromBinary(data); verifyOpt.signature = await openpgp.readSignature({ binarySignature: signed }); return openpgp.verify(verifyOpt); }).then(async function (verified) { @@ -2331,10 +2331,10 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign and verify binary data without one-pass signature', function () { + it('should sign and verify binary data without one-pass signature', async function () { const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const signOpt = { - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privateKey, armor: false }; @@ -2359,10 +2359,10 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should streaming sign and verify binary data without one-pass signature', function () { + it('should streaming sign and verify binary data without one-pass signature', async function () { const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const signOpt = { - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privateKey, armor: false, streaming: 'web' @@ -2392,10 +2392,10 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt and decrypt data with a date in the future', function () { + it('should encrypt and decrypt data with a date in the future', async function () { const future = new Date(2040, 5, 5, 5, 5, 5, 0); const encryptOpt = { - message: openpgp.Message.fromText(plaintext, undefined, future), + message: await openpgp.Message.fromText(plaintext, undefined, future), publicKeys: publicKey_2038_2045, date: future, armor: false @@ -2412,11 +2412,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should encrypt and decrypt binary data with a date in the past', function () { + it('should encrypt and decrypt binary data with a date in the past', async function () { const past = new Date(2005, 5, 5, 5, 5, 5, 0); const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const encryptOpt = { - message: openpgp.Message.fromBinary(data, undefined, past), + message: await openpgp.Message.fromBinary(data, undefined, past), publicKeys: publicKey_2000_2008, date: past, armor: false @@ -2433,10 +2433,10 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign, encrypt and decrypt, verify data with a date in the past', function () { + it('should sign, encrypt and decrypt, verify data with a date in the past', async function () { const past = new Date(2005, 5, 5, 5, 5, 5, 0); const encryptOpt = { - message: openpgp.Message.fromText(plaintext, undefined, past), + message: await openpgp.Message.fromText(plaintext, undefined, past), publicKeys: publicKey_2000_2008, privateKeys: privateKey_2000_2008, date: past, @@ -2460,11 +2460,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign, encrypt and decrypt, verify binary data with a date in the future', function () { + it('should sign, encrypt and decrypt, verify binary data with a date in the future', async function () { const future = new Date(2040, 5, 5, 5, 5, 5, 0); const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const encryptOpt = { - message: openpgp.Message.fromBinary(data, undefined, future), + message: await openpgp.Message.fromBinary(data, undefined, future), publicKeys: publicKey_2038_2045, privateKeys: privateKey_2038_2045, date: future, @@ -2489,11 +2489,11 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { }); }); - it('should sign, encrypt and decrypt, verify mime data with a date in the future', function () { + it('should sign, encrypt and decrypt, verify mime data with a date in the future', async function () { const future = new Date(2040, 5, 5, 5, 5, 5, 0); const data = new Uint8Array([3, 14, 15, 92, 65, 35, 59]); const encryptOpt = { - message: openpgp.Message.fromBinary(data, undefined, future, 'mime'), + message: await openpgp.Message.fromBinary(data, undefined, future, 'mime'), publicKeys: publicKey_2038_2045, privateKeys: privateKey_2038_2045, date: future, @@ -2521,9 +2521,9 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { it('should fail to encrypt with revoked key', function() { return openpgp.revokeKey({ key: privateKey - }).then(function(revKey) { + }).then(async function(revKey) { return openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: revKey.publicKey }).then(function() { throw new Error('Should not encrypt with revoked key'); @@ -2537,10 +2537,10 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const pubKeyDE = await openpgp.readKey({ armoredKey: pub_key_de }); const privKeyDE = await openpgp.readKey({ armoredKey: priv_key_de }); await privKeyDE.decrypt(passphrase); - return privKeyDE.subKeys[0].revoke(privKeyDE.primaryKey).then(function(revSubKey) { + return privKeyDE.subKeys[0].revoke(privKeyDE.primaryKey).then(async function(revSubKey) { pubKeyDE.subKeys[0] = revSubKey; return openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: pubKeyDE, config: { rejectPublicKeyAlgorithms: new Set() } }).then(function() { @@ -2556,7 +2556,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { const privKeyDE = await openpgp.readKey({ armoredKey: priv_key_de }); await privKeyDE.decrypt(passphrase); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: pubKeyDE, config: { rejectPublicKeyAlgorithms: new Set() } }); @@ -2578,7 +2578,7 @@ module.exports = () => describe('OpenPGP.js public api tests', function() { // validation will not check the decryption subkey and will succeed await privKeyDE.decrypt(passphrase); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(plaintext), + message: await openpgp.Message.fromText(plaintext), publicKeys: pubKeyDE, config: { rejectPublicKeyAlgorithms: new Set() } }); @@ -2690,7 +2690,7 @@ amnR6g== }); it('should normalize newlines in encrypted text message', async function() { - const message = openpgp.Message.fromText('"BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\r\nUID:123\r\nDTSTART:20191211T121212Z\r\nDTEND:20191212T121212Z\r\nEND:VEVENT\nEND:VCALENDAR"'); + const message = await openpgp.Message.fromText('"BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\r\nUID:123\r\nDTSTART:20191211T121212Z\r\nDTEND:20191212T121212Z\r\nEND:VEVENT\nEND:VCALENDAR"'); const encrypted = await openpgp.encrypt({ passwords: 'test', message @@ -2710,7 +2710,7 @@ amnR6g== it(`sign/verify with ${curve}`, async function() { const plaintext = 'short message'; const key = (await openpgp.generateKey({ curve, userIDs: { name: 'Alice', email: 'info@alice.com' } })).key; - const signed = await openpgp.sign({ privateKeys:[key], message: openpgp.CleartextMessage.fromText(plaintext) }); + const signed = await openpgp.sign({ privateKeys:[key], message: await openpgp.CleartextMessage.fromText(plaintext) }); const verified = await openpgp.verify({ publicKeys:[key], message: await openpgp.readCleartextMessage({ cleartextMessage: signed }) }); expect(verified.signatures[0].valid).to.be.true; }); @@ -2823,9 +2823,9 @@ bsZgJWVlAa5eil6J9ePX2xbo1vVAkLQdzE9+1jL+l7PRIZuVBQ== describe('Errors', function() { - it('Error message should contain the original error message', function() { + it('Error message should contain the original error message', async function() { return openpgp.encrypt({ - message: openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01])), + message: await openpgp.Message.fromBinary(new Uint8Array([0x01, 0x01, 0x01])), passwords: null }).then(function() { throw new Error('Error expected.'); @@ -2858,7 +2858,7 @@ bsZgJWVlAa5eil6J9ePX2xbo1vVAkLQdzE9+1jL+l7PRIZuVBQ== for (let i = 0; i < encryptionKeyIDs.length; i++) { m = await openpgp.readMessage({ armoredMessage: await openpgp.encrypt({ - message: openpgp.Message.fromText("Hello World\n"), + message: await openpgp.Message.fromText("Hello World\n"), publicKeys: primaryKey, encryptionKeyIDs: [encryptionKeyIDs[i]] }) @@ -2876,7 +2876,7 @@ bsZgJWVlAa5eil6J9ePX2xbo1vVAkLQdzE9+1jL+l7PRIZuVBQ== for (let i = 0; i < signingKeyIDs.length; i++) { s = await openpgp.readSignature({ armoredSignature: await openpgp.sign({ - message: openpgp.Message.fromText("Hello World\n"), + message: await openpgp.Message.fromText("Hello World\n"), privateKeys: primaryKey, signingKeyIDs: [signingKeyIDs[i]], detached: true @@ -2890,7 +2890,7 @@ bsZgJWVlAa5eil6J9ePX2xbo1vVAkLQdzE9+1jL+l7PRIZuVBQ== it('Encrypt and sign with specific encryption/signing key ids', async function () { const primaryKey = await getPrimaryKey(); - const plaintextMessage = openpgp.Message.fromText("Hello World\n"); + const plaintextMessage = await openpgp.Message.fromText("Hello World\n"); const checkEncryptedPackets = (encryptionKeyIDs, pKESKList) => { pKESKList.forEach(({ publicKeyID }, i) => { diff --git a/test/general/packet.js b/test/general/packet.js index 0076f4a4..401a4947 100644 --- a/test/general/packet.js +++ b/test/general/packet.js @@ -753,7 +753,7 @@ module.exports = () => describe("Packet", function() { await Promise.all([ signature.verify(keyPacket, openpgp.enums.signature.binary, literal), - openpgp.stream.pipe(literal.getBytes(), new openpgp.stream.WritableStream()) + openpgp.stream.readToEnd(literal.getBytes()) ]); }); }); @@ -911,7 +911,7 @@ V+HOQJQxXJkVRYa3QrFUehiMzTeqqMdgC6ZqJy7+ await Promise.all([ signed2[1].verify(key, openpgp.enums.signature.text, signed2[0]), - openpgp.stream.pipe(signed2[0].getBytes(), new openpgp.stream.WritableStream()) + openpgp.stream.readToEnd(signed2[0].getBytes()) ]); }); }); diff --git a/test/general/signature.js b/test/general/signature.js index 9a036da8..d171d595 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -882,7 +882,7 @@ hUhMKMuiM3pRwdIyDOItkUWQmjEEw7/XmhgInkXsCw== expect(msg.signatures[0].valid).to.be.true; expect(msg.signatures[0].signature.packets.length).to.equal(1); await expect(openpgp.sign({ - message: openpgp.Message.fromText('test'), privateKeys: [priv_key_gnupg_ext], config: { rejectPublicKeyAlgorithms: new Set() } + message: await openpgp.Message.fromText('test'), privateKeys: [priv_key_gnupg_ext], config: { rejectPublicKeyAlgorithms: new Set() } })).to.eventually.be.rejectedWith(/Cannot sign with a gnu-dummy key/); await expect(openpgp.reformatKey({ userIDs: { name: 'test' }, privateKey: priv_key_gnupg_ext })).to.eventually.be.rejectedWith(/Cannot reformat a gnu-dummy primary key/); await expect(openpgp.reformatKey({ userIDs: { name: 'test' }, privateKey: priv_key_gnupg_ext_2, passphrase: 'test' })).to.eventually.be.rejectedWith(/Cannot reformat a gnu-dummy primary key/); @@ -898,7 +898,7 @@ hUhMKMuiM3pRwdIyDOItkUWQmjEEw7/XmhgInkXsCw== it('Supports signing with GnuPG stripped-key extension', async function() { const priv_key_gnupg_ext = await openpgp.readKey({ armoredKey: flowcrypt_stripped_key }); await priv_key_gnupg_ext.decrypt('FlowCrypt'); - const sig = await openpgp.sign({ message: openpgp.Message.fromText('test'), privateKeys: [priv_key_gnupg_ext], date: new Date('2018-12-17T03:24:00') }); + const sig = await openpgp.sign({ message: await openpgp.Message.fromText('test'), privateKeys: [priv_key_gnupg_ext], date: new Date('2018-12-17T03:24:00') }); expect(sig).to.match(/-----END PGP MESSAGE-----\n$/); }); @@ -939,7 +939,7 @@ hUhMKMuiM3pRwdIyDOItkUWQmjEEw7/XmhgInkXsCw== const sMsg = await openpgp.readMessage({ armoredMessage: signedArmor }); const pub_key = await openpgp.readKey({ armoredKey: pub_key_arm2 }); const verified = await sMsg.verify([pub_key]); - openpgp.stream.pipe(sMsg.getLiteralData(), new openpgp.stream.WritableStream()); + openpgp.stream.readToEnd(sMsg.getLiteralData()); expect(verified).to.exist; expect(verified).to.have.length(1); expect(await verified[0].verified).to.be.true; @@ -1083,7 +1083,7 @@ hUhMKMuiM3pRwdIyDOItkUWQmjEEw7/XmhgInkXsCw== it('Verify latin-1 signed message', async function() { const latin1Binary = util.hexToUint8Array('48e46c6cf62057e86c74'); - const message = openpgp.Message.fromBinary(latin1Binary); + const message = await openpgp.Message.fromBinary(latin1Binary); message.appendSignature(`-----BEGIN PGP SIGNATURE----- @@ -1201,12 +1201,25 @@ yYDnCgA= const plaintext = 'space: \nspace and tab: \t\nno trailing space\n \ntab:\t\ntab and space:\t '; const message = await openpgp.readMessage({ - armoredMessage: new openpgp.stream.ReadableStream({ + armoredMessage: globalThis.ReadableStream ? new globalThis.ReadableStream({ async pull(controller) { await new Promise(setTimeout); controller.enqueue(armoredMessage.shift()); if (!armoredMessage.length) controller.close(); } + }) : new (require('stream').Readable)({ + encoding: 'utf8', + async read() { + while (true) { + await new Promise(setTimeout); + if (!msg_armor.length) { + return this.push(null); + } + if (!this.push(msg_armor.shift())) { + break; + } + } + } }) }); const pubKey = await openpgp.readKey({ armoredKey: pub_key_arm2 }); @@ -1309,7 +1322,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { const message = await openpgp.readCleartextMessage({ cleartextMessage: signed }); return openpgp.verify({ publicKeys:[pubKey], message, config }); @@ -1329,7 +1342,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { const message = await openpgp.readCleartextMessage({ cleartextMessage: signed }); return openpgp.verify({ publicKeys:[pubKey], message, config }); @@ -1349,7 +1362,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.CleartextMessage.fromText(plaintext), config }).then(async function(signed) { const message = await openpgp.readCleartextMessage({ cleartextMessage: signed }); return openpgp.verify({ publicKeys:[pubKey], message, config }); @@ -1369,7 +1382,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.Message.fromBinary(plaintext), config }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.Message.fromBinary(plaintext), config }).then(async function(signed) { const message = await openpgp.readMessage({ armoredMessage: signed }); return openpgp.verify({ publicKeys:[pubKey], message, format: 'binary', config }); @@ -1389,7 +1402,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.Message.fromBinary(plaintext), armor:false, config }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.Message.fromBinary(plaintext), armor:false, config }).then(async function(signed) { const message = await openpgp.readMessage({ binaryMessage: signed }); return openpgp.verify({ publicKeys:[pubKey], message, format: 'binary', config }); @@ -1409,9 +1422,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.Message.fromText(plaintext), detached: true, config }).then(async function(armoredSignature) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.Message.fromText(plaintext), detached: true, config }).then(async function(armoredSignature) { const signature = await openpgp.readSignature({ armoredSignature }); - return openpgp.verify({ publicKeys:[pubKey], message: openpgp.Message.fromBinary(util.encodeUTF8(plaintext)), signature, config }); + return openpgp.verify({ publicKeys:[pubKey], message: await openpgp.Message.fromBinary(util.encodeUTF8(plaintext)), signature, config }); }).then(function({ data, signatures }) { expect(data).to.equal(plaintext); expect(signatures).to.have.length(1); @@ -1428,9 +1441,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message:openpgp.Message.fromBinary(binaryPlaintext), detached: true, config }).then(async function(armoredSignature) { + return openpgp.sign({ privateKeys:[privKey], message:await openpgp.Message.fromBinary(binaryPlaintext), detached: true, config }).then(async function(armoredSignature) { const signature = await openpgp.readSignature({ armoredSignature }); - return openpgp.verify({ publicKeys:[pubKey], message: openpgp.Message.fromText(plaintext), signature, config }); + return openpgp.verify({ publicKeys:[pubKey], message: await openpgp.Message.fromText(plaintext), signature, config }); }).then(function({ data, signatures }) { expect(data).to.equal(plaintext); expect(signatures).to.have.length(1); @@ -1446,9 +1459,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA await privKey.decrypt('hello world'); const config = { minRSABits: 1024 }; - return openpgp.sign({ privateKeys:[privKey], message: openpgp.Message.fromText(plaintext), detached: true, config }).then(async function(armoredSignature) { + return openpgp.sign({ privateKeys:[privKey], message: await openpgp.Message.fromText(plaintext), detached: true, config }).then(async function(armoredSignature) { const signature = await openpgp.readSignature({ armoredSignature }); - return openpgp.encrypt({ message: openpgp.Message.fromBinary(util.encodeUTF8(plaintext)), publicKeys: [pubKey], signature, config }); + return openpgp.encrypt({ message: await openpgp.Message.fromBinary(util.encodeUTF8(plaintext)), publicKeys: [pubKey], signature, config }); }).then(async armoredMessage => { const message = await openpgp.readMessage({ armoredMessage }); @@ -1565,7 +1578,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA const publicKeyArmored = '-----BEGIN PGP PUBLIC KEY BLOCK-----\r\nVersion: OpenPGP.js v.1.20131116\r\nComment: Whiteout Mail - https://whiteout.io\r\n\r\nxsBNBFKODs4BB/9iOF4THsjQMY+WEpT7ShgKxj4bHzRRaQkqczS4nZvP0U3g\r\nqeqCnbpagyeKXA+bhWFQW4GmXtgAoeD5PXs6AZYrw3tWNxLKu2Oe6Tp9K/XI\r\nxTMQ2wl4qZKDXHvuPsJ7cmgaWqpPyXtxA4zHHS3WrkI/6VzHAcI/y6x4szSB\r\nKgSuhI3hjh3s7TybUC1U6AfoQGx/S7e3WwlCOrK8GTClirN/2mCPRC5wuIft\r\nnkoMfA6jK8d2OPrJ63shy5cgwHOjQg/xuk46dNS7tkvGmbaa+X0PgqSKB+Hf\r\nYPPNS/ylg911DH9qa8BqYU2QpNh9jUKXSF+HbaOM+plWkCSAL7czV+R3ABEB\r\nAAHNLVdoaXRlb3V0IFVzZXIgPHNhZmV3aXRobWUudGVzdHVzZXJAZ21haWwu\r\nY29tPsLAXAQQAQgAEAUCUo4O2gkQ1/uT/N+/wjwAAN2cB/9gFRmAfvEQ2qz+\r\nWubmT2EsSSnjPMxzG4uyykFoa+TaZCWo2Xa2tQghmU103kEkQb1OEjRjpgwJ\r\nYX9Kghnl8DByM686L5AXnRyHP78qRJCLXSXl0AGicboUDp5sovaa4rswQceH\r\nvcdWgZ/mgHTRoiQeJddy9k+H6MPFiyFaVcFwegVsmpc+dCcC8yT+qh8ZIbyG\r\nRJU60PmKKN7LUusP+8DbSv39zCGJCBlVVKyA4MzdF5uM+sqTdXbKzOrT5DGd\r\nCZaox4s+w16Sq1rHzZKFWfQPfKLDB9pyA0ufCVRA3AF6BUi7G3ZqhZiHNhMP\r\nNvE45V/hS1PbZcfPVoUjE2qc1Ix1\r\n=7Wpe\r\n-----END PGP PUBLIC KEY BLOCK-----'; const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored }); - const message = openpgp.Message.fromText(content); + const message = await openpgp.Message.fromText(content); await message.appendSignature(detachedSig); const { data, signatures } = await openpgp.verify({ publicKeys:[publicKey], message, config: { minRSABits: 1024 } }); expect(data).to.equal(content); @@ -1576,7 +1589,7 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA }); it('Detached signature signing and verification', async function() { - const message = openpgp.Message.fromText('hello'); + const message = await openpgp.Message.fromText('hello'); const pubKey2 = await openpgp.readKey({ armoredKey: pub_key_arm2 }); const privKey2 = await openpgp.readKey({ armoredKey: priv_key_arm2 }); await privKey2.decrypt('hello world'); @@ -1593,9 +1606,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA it('Sign message with key without password', function() { const opt = { userIDs: { name:'test', email:'a@b.com' }, passphrase: null }; - return openpgp.generateKey(opt).then(function(gen) { + return openpgp.generateKey(opt).then(async function(gen) { const key = gen.key; - const message = openpgp.Message.fromText('hello world'); + const message = await openpgp.Message.fromText('hello world'); return message.sign([key]); }); }); diff --git a/test/general/streaming.js b/test/general/streaming.js index c8a82e4d..07a25106 100644 --- a/test/general/streaming.js +++ b/test/general/streaming.js @@ -1,5 +1,4 @@ /* eslint-disable max-lines */ -/* globals tryTests: true */ const openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../..'); const random = require('../../src/crypto/random'); @@ -16,6 +15,7 @@ const { stream } = openpgp; const useNativeStream = (() => { try { new global.ReadableStream(); return true; } catch (e) { return false; } })(); // eslint-disable-line no-new const ReadableStream = useNativeStream ? global.ReadableStream : openpgp.stream.ReadableStream; +const NodeReadableStream = useNativeStream ? undefined : require('stream').Readable; const pub_key = [ '-----BEGIN PGP PUBLIC KEY BLOCK-----', @@ -182,15 +182,21 @@ let dataArrived; function tests() { it('Encrypt small message', async function() { dataArrived(); // Do not wait until data arrived. - const data = new ReadableStream({ - async start(controller) { + const data = ReadableStream ? new ReadableStream({ + start(controller) { controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('world')); controller.close(); } + }) : new NodeReadableStream({ + read() { + this.push(util.stringToUint8Array('hello ')); + this.push(util.stringToUint8Array('world')); + this.push(null); + } }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); const msgAsciiArmored = await openpgp.stream.readToEnd(encrypted); @@ -204,7 +210,7 @@ function tests() { it('Encrypt larger message', async function() { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); const reader = openpgp.stream.getReader(encrypted); @@ -223,7 +229,7 @@ function tests() { it('Input stream should be canceled when canceling encrypted stream', async function() { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); const reader = openpgp.stream.getReader(encrypted); @@ -236,7 +242,7 @@ function tests() { it('Sign: Input stream should be canceled when canceling encrypted stream', async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, config: { minRSABits: 1024 } }); @@ -252,7 +258,7 @@ function tests() { const aeadProtectValue = openpgp.config.aeadProtect; openpgp.config.aeadProtect = false; const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'], armor: false }); @@ -280,7 +286,7 @@ function tests() { openpgp.config.allowUnauthenticatedStream = true; try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'], armor: false }); @@ -310,7 +316,7 @@ function tests() { openpgp.config.allowUnauthenticatedStream = true; try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), publicKeys: pubKey, privateKeys: privKey, armor: false, @@ -343,7 +349,7 @@ function tests() { await priv.decrypt(xPass); try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), publicKeys: pub, privateKeys: priv, armor: false @@ -375,7 +381,7 @@ function tests() { await priv.decrypt(brainpoolPass); try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), publicKeys: pub, privateKeys: priv, armor: false @@ -406,7 +412,7 @@ function tests() { openpgp.config.allowUnauthenticatedStream = true; try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data, 'msg.bin'), + message: await openpgp.Message.fromBinary(data, 'msg.bin'), passwords: ['test'] }); expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); @@ -443,7 +449,7 @@ function tests() { openpgp.config.allowUnauthenticatedStream = true; try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), publicKeys: pubKey, privateKeys: privKey, config: { minRSABits: 1024 } @@ -481,7 +487,7 @@ function tests() { openpgp.config.allowUnauthenticatedStream = true; try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), publicKeys: pubKey, privateKeys: privKey, config: { minRSABits: 1024 } @@ -516,7 +522,7 @@ function tests() { it('Sign/verify: Detect armor checksum error', async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, config: { minRSABits: 1024 } }); @@ -572,7 +578,7 @@ function tests() { it('Sign/verify: Input stream should be canceled when canceling verified stream', async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, config: { minRSABits: 1024 } }); @@ -598,7 +604,7 @@ function tests() { it("Don't pull entire input stream when we're not pulling encrypted stream", async function() { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); @@ -612,7 +618,7 @@ function tests() { it("Sign: Don't pull entire input stream when we're not pulling signed stream", async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, config: { minRSABits: 1024 } }); @@ -627,7 +633,7 @@ function tests() { it("Sign/verify: Don't pull entire input stream when we're not pulling verified stream", async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, config: { minRSABits: 1024 } }); @@ -643,20 +649,26 @@ function tests() { expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); dataArrived(); await new Promise(resolve => setTimeout(resolve, 3000)); - expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 100); + expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 250); }); it('Detached sign small message', async function() { dataArrived(); // Do not wait until data arrived. - const data = new ReadableStream({ - async start(controller) { + const data = ReadableStream ? new ReadableStream({ + start(controller) { controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('world')); controller.close(); } + }) : new NodeReadableStream({ + read() { + this.push(util.stringToUint8Array('hello ')); + this.push(util.stringToUint8Array('world')); + this.push(null); + } }); const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, detached: true, streaming: expectedType, @@ -668,7 +680,7 @@ function tests() { const verified = await openpgp.verify({ signature, publicKeys: pubKey, - message: openpgp.Message.fromText('hello world'), + message: await openpgp.Message.fromText('hello world'), config: { minRSABits: 1024 } }); expect(verified.data).to.equal('hello world'); @@ -678,15 +690,21 @@ function tests() { it('Detached sign small message (not streaming)', async function() { dataArrived(); // Do not wait until data arrived. - const data = new ReadableStream({ - async start(controller) { + const data = ReadableStream ? new ReadableStream({ + start(controller) { controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('world')); controller.close(); } + }) : new NodeReadableStream({ + read() { + this.push(util.stringToUint8Array('hello ')); + this.push(util.stringToUint8Array('world')); + this.push(null); + } }); const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, detached: true, streaming: false, @@ -698,7 +716,7 @@ function tests() { const verified = await openpgp.verify({ signature, publicKeys: pubKey, - message: openpgp.Message.fromText('hello world'), + message: await openpgp.Message.fromText('hello world'), config: { minRSABits: 1024 } }); expect(verified.data).to.equal('hello world'); @@ -708,18 +726,24 @@ function tests() { it('Detached sign small message using brainpool curve keys', async function() { dataArrived(); // Do not wait until data arrived. - const data = new ReadableStream({ - async start(controller) { + const data = ReadableStream ? new ReadableStream({ + start(controller) { controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('world')); controller.close(); } + }) : new NodeReadableStream({ + read() { + this.push(util.stringToUint8Array('hello ')); + this.push(util.stringToUint8Array('world')); + this.push(null); + } }); const priv = await openpgp.readKey({ armoredKey: brainpoolPriv }); const pub = await openpgp.readKey({ armoredKey: brainpoolPub }); await priv.decrypt(brainpoolPass); const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: priv, detached: true, streaming: expectedType @@ -730,7 +754,7 @@ function tests() { const verified = await openpgp.verify({ signature, publicKeys: pub, - message: openpgp.Message.fromText('hello world') + message: await openpgp.Message.fromText('hello world') }); expect(verified.data).to.equal('hello world'); expect(verified.signatures).to.exist.and.have.length(1); @@ -739,18 +763,24 @@ function tests() { it('Detached sign small message using x25519 curve keys', async function() { dataArrived(); // Do not wait until data arrived. - const data = new ReadableStream({ + const data = ReadableStream ? new ReadableStream({ async start(controller) { controller.enqueue(util.stringToUint8Array('hello ')); controller.enqueue(util.stringToUint8Array('world')); controller.close(); } + }) : new NodeReadableStream({ + read() { + this.push(util.stringToUint8Array('hello ')); + this.push(util.stringToUint8Array('world')); + this.push(null); + } }); const priv = await openpgp.readKey({ armoredKey: xPriv }); const pub = await openpgp.readKey({ armoredKey: xPub }); await priv.decrypt(xPass); const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: priv, detached: true, streaming: expectedType @@ -761,7 +791,7 @@ function tests() { const verified = await openpgp.verify({ signature, publicKeys: pub, - message: openpgp.Message.fromText('hello world') + message: await openpgp.Message.fromText('hello world') }); expect(verified.data).to.equal('hello world'); expect(verified.signatures).to.exist.and.have.length(1); @@ -770,7 +800,7 @@ function tests() { it("Detached sign is expected to pull entire input stream when we're not pulling signed stream", async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, detached: true, config: { minRSABits: 1024 } @@ -780,12 +810,12 @@ function tests() { expect((await reader.readBytes(30)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\n'); dataArrived(); await new Promise(resolve => setTimeout(resolve, 3000)); - expect(i).to.be.greaterThan(100); + expect(i).to.equal(expectedType === 'web' ? 100 : 500); }); it('Detached sign: Input stream should be canceled when canceling signed stream', async function() { const signed = await openpgp.sign({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), privateKeys: privKey, detached: true, config: { minRSABits: 1024 } @@ -816,7 +846,7 @@ function tests() { it('Encrypt and decrypt larger message roundtrip (AEAD)', async function() { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'], armor: false }); @@ -840,7 +870,7 @@ function tests() { const plaintext = []; let i = 0; - const data = new ReadableStream({ + const data = ReadableStream ? new ReadableStream({ async pull(controller) { await new Promise(resolve => setTimeout(resolve, 10)); if (i++ < 10) { @@ -851,9 +881,23 @@ function tests() { controller.close(); } } + }) : new NodeReadableStream({ + encoding: 'utf8', + async read() { + while (true) { + await new Promise(resolve => setTimeout(resolve, 10)); + if (i++ < 10) { + const randomData = input.createSomeMessage(); + plaintext.push(randomData); + if (!this.push(randomData)) break; + } else { + return this.push(null); + } + } + } }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(data), + message: await openpgp.Message.fromText(data), streaming: expectedType, passwords: ['test'] }); @@ -883,7 +927,7 @@ function tests() { } try { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); expect(openpgp.stream.isStream(encrypted)).to.equal(expectedType); @@ -898,7 +942,7 @@ function tests() { expect(await reader.readBytes(1024)).to.deep.equal(plaintext[0]); dataArrived(); await new Promise(resolve => setTimeout(resolve, 3000)); - expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 100); + expect(i).to.be.lessThan(expectedType === 'web' ? 50 : 300); } finally { if (util.detectNode()) { coresStub.restore(); @@ -910,7 +954,7 @@ function tests() { it('Input stream should be canceled when canceling decrypted stream (AEAD)', async function() { const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'] }); @@ -926,6 +970,7 @@ function tests() { dataArrived(); reader.releaseLock(); await openpgp.stream.cancel(decrypted.data, new Error('canceled by test')); + await new Promise(setTimeout); expect(canceled).to.be.true; }); }); @@ -949,10 +994,11 @@ module.exports = () => describe('Streaming', function() { plaintext = []; i = 0; canceled = false; - data = new ReadableStream({ + data = ReadableStream ? new ReadableStream({ async pull(controller) { await new Promise(setTimeout); - if (test === currentTest && i++ < 100) { + if (test === currentTest && i < (expectedType === 'web' ? 100 : 500)) { + i++; if (i === 4) await dataArrivedPromise; const randomBytes = await random.getRandomBytes(1024); controller.enqueue(randomBytes); @@ -964,23 +1010,32 @@ module.exports = () => describe('Streaming', function() { cancel() { canceled = true; } + }, new ByteLengthQueuingStrategy({ + highWaterMark: 1024 + })) : new NodeReadableStream({ + highWaterMark: 1024, + async read() { + while (true) { + await new Promise(setTimeout); + if (test === currentTest && i < (expectedType === 'web' ? 100 : 500)) { + i++; + if (i === 4) await dataArrivedPromise; + const randomBytes = await random.getRandomBytes(1024); + plaintext.push(randomBytes); + if (!this.push(randomBytes)) break; + } else { + return this.push(null); + } + } + }, + destroy() { + canceled = true; + } }); + expectedType = ReadableStream ? 'web' : 'node'; }); - tryTests('WhatWG Streams', tests, { - if: true, - beforeEach: function() { - expectedType = useNativeStream ? 'web' : 'ponyfill'; - } - }); - - tryTests('Node Streams', tests, { - if: util.detectNode(), - beforeEach: function() { - data = openpgp.stream.webToNode(data); - expectedType = 'node'; - } - }); + tests(); if (util.detectNode()) { const fs = require('fs'); @@ -990,7 +1045,7 @@ module.exports = () => describe('Streaming', function() { const plaintext = fs.readFileSync(__filename.replace('streaming.js', 'openpgp.js'), 'utf8'); // eslint-disable-line no-sync const data = fs.createReadStream(__filename.replace('streaming.js', 'openpgp.js'), { encoding: 'utf8' }); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromText(data), + message: await openpgp.Message.fromText(data), passwords: ['test'] }); expect(openpgp.stream.isStream(encrypted)).to.equal('node'); @@ -1009,7 +1064,7 @@ module.exports = () => describe('Streaming', function() { const plaintext = fs.readFileSync(__filename.replace('streaming.js', 'openpgp.js')); // eslint-disable-line no-sync const data = fs.createReadStream(__filename.replace('streaming.js', 'openpgp.js')); const encrypted = await openpgp.encrypt({ - message: openpgp.Message.fromBinary(data), + message: await openpgp.Message.fromBinary(data), passwords: ['test'], armor: false }); diff --git a/test/general/x25519.js b/test/general/x25519.js index b3976aea..5501484e 100644 --- a/test/general/x25519.js +++ b/test/general/x25519.js @@ -174,7 +174,7 @@ module.exports = () => (openpgp.config.ci ? describe.skip : describe)('X25519 Cr const name = 'light'; const randomData = input.createSomeMessage(); const priv = await load_priv_key(name); - const signed = await openpgp.sign({ privateKeys: [priv], message: openpgp.CleartextMessage.fromText(randomData) }); + const signed = await openpgp.sign({ privateKeys: [priv], message: await openpgp.CleartextMessage.fromText(randomData) }); const pub = await load_pub_key(name); const msg = await openpgp.readCleartextMessage({ cleartextMessage: signed }); const result = await openpgp.verify({ publicKeys: [pub], message: msg }); @@ -201,7 +201,7 @@ module.exports = () => (openpgp.config.ci ? describe.skip : describe)('X25519 Cr const nightPublic = await load_pub_key('night'); const lightPrivate = await load_priv_key('light'); const randomData = input.createSomeMessage(); - const encrypted = await openpgp.encrypt({ publicKeys: [nightPublic], privateKeys: [lightPrivate], message: openpgp.Message.fromText(randomData) }); + const encrypted = await openpgp.encrypt({ publicKeys: [nightPublic], privateKeys: [lightPrivate], message: await openpgp.Message.fromText(randomData) }); const message = await openpgp.readMessage({ armoredMessage: encrypted }); const lightPublic = await load_pub_key('light'); @@ -447,7 +447,7 @@ function omnibus() { }), // Signing message openpgp.sign( - { message: openpgp.CleartextMessage.fromText('Hi, this is me, Hi!'), privateKeys: hi } + { message: await openpgp.CleartextMessage.fromText('Hi, this is me, Hi!'), privateKeys: hi } ).then(async signed => { const msg = await openpgp.readCleartextMessage({ cleartextMessage: signed }); // Verifying signed message @@ -457,7 +457,7 @@ function omnibus() { ).then(output => expect(output.signatures[0].valid).to.be.true), // Verifying detached signature openpgp.verify({ - message: openpgp.Message.fromText('Hi, this is me, Hi!'), + message: await openpgp.Message.fromText('Hi, this is me, Hi!'), publicKeys: hi.toPublic(), signature: msg.signature }).then(output => expect(output.signatures[0].valid).to.be.true) @@ -466,7 +466,7 @@ function omnibus() { // Encrypting and signing openpgp.encrypt( { - message: openpgp.Message.fromText('Hi, Hi wrote this but only Bye can read it!'), + message: await openpgp.Message.fromText('Hi, Hi wrote this but only Bye can read it!'), publicKeys: [bye.toPublic()], privateKeys: [hi] } diff --git a/test/security/unsigned_subpackets.js b/test/security/unsigned_subpackets.js index 57467352..457e767e 100644 --- a/test/security/unsigned_subpackets.js +++ b/test/security/unsigned_subpackets.js @@ -58,7 +58,7 @@ async function makeKeyValid() { async function encryptFails(k) { try { await openpgp.encrypt({ - message: message.fromText('Hello', 'hello.txt'), + message: await Message.fromText('Hello', 'hello.txt'), publicKeys: k }); return false; diff --git a/test/typescript/definitions.ts b/test/typescript/definitions.ts index 5281bf48..706942d4 100644 --- a/test/typescript/definitions.ts +++ b/test/typescript/definitions.ts @@ -25,7 +25,7 @@ import { expect } from 'chai'; // Encrypt text message (armored) const text = 'hello'; - const textMessage = Message.fromText('hello'); + const textMessage = await Message.fromText('hello'); const encryptedArmor: string = await encrypt({ publicKeys, message: textMessage }); expect(encryptedArmor).to.include('-----BEGIN PGP MESSAGE-----'); @@ -33,7 +33,7 @@ import { expect } from 'chai'; const binary = new Uint8Array(2); binary[0] = 1; binary[1] = 2; - const binaryMessage = Message.fromBinary(binary); + const binaryMessage = await Message.fromBinary(binary); const encryptedBinary: Uint8Array = await encrypt({ publicKeys, message: binaryMessage, armor: false }); expect(encryptedBinary).to.be.instanceOf(Uint8Array); @@ -54,7 +54,7 @@ import { expect } from 'chai'; expect(encryptedMessage).to.be.instanceOf(Message); // Sign cleartext message (armored) - const cleartextMessage = CleartextMessage.fromText('hello'); + const cleartextMessage = await CleartextMessage.fromText('hello'); const clearSignedArmor = await sign({ privateKeys, message: cleartextMessage }); expect(clearSignedArmor).to.include('-----BEGIN PGP SIGNED MESSAGE-----'); @@ -80,12 +80,12 @@ import { expect } from 'chai'; // // Detached - sign cleartext message (armored) // import { Message, sign } from 'openpgp'; - // const message = Message.fromText(util.removeTrailingSpaces(text)); + // const message = await Message.fromText(util.removeTrailingSpaces(text)); // const signed = await sign({ privateKeys, message, detached: true }); // console.log(signed); // String // // Detached - sign binary message (unarmored) - // const message = Message.fromText(text); + // const message = await Message.fromText(text); // const signed = await sign({ privateKeys, message, detached: true, armor: false }); // console.log(signed); // Uint8Array @@ -99,7 +99,7 @@ import { expect } from 'chai'; // // Streaming - encrypt text message on Node.js (armored) // const data = fs.createReadStream(filename, { encoding: 'utf8' }); - // const message = Message.fromText(data); + // const message = await Message.fromText(data); // const encrypted = await encrypt({ publicKeys, message }); // encrypted.on('data', chunk => { // console.log(chunk); // String @@ -107,7 +107,7 @@ import { expect } from 'chai'; // // Streaming - encrypt binary message on Node.js (unarmored) // const data = fs.createReadStream(filename); - // const message = Message.fromBinary(data); + // const message = await Message.fromBinary(data); // const encrypted = await encrypt({ publicKeys, message, armor: false }); // encrypted.pipe(targetStream); diff --git a/test/worker/worker_example.js b/test/worker/worker_example.js index 5ea48bb1..9b1e776f 100644 --- a/test/worker/worker_example.js +++ b/test/worker/worker_example.js @@ -48,7 +48,7 @@ onmessage = async function({ data: { action, message }, ports: [port] }) { const privateKey = await openpgp.readKey({ armoredKey: privateKeyArmored }); await privateKey.decrypt('test'); const data = await openpgp.encrypt({ - message: openpgp.Message.fromText(message), + message: await openpgp.Message.fromText(message), publicKeys: publicKey, privateKeys: privateKey });