Return Uint8Array(Stream) instead of object when armor = false

This commit is contained in:
Daniel Huigens 2020-02-11 21:19:56 +01:00
parent 1f237e6a9c
commit 7225251af8
7 changed files with 187 additions and 142 deletions

View File

@ -721,12 +721,20 @@ Message.prototype.appendSignature = async function(detachedSignature) {
await this.packets.read(util.isUint8Array(detachedSignature) ? detachedSignature : (await armor.decode(detachedSignature)).data); await this.packets.read(util.isUint8Array(detachedSignature) ? detachedSignature : (await armor.decode(detachedSignature)).data);
}; };
/**
* Returns binary encoded message
* @returns {ReadableStream<Uint8Array>} binary message
*/
Message.prototype.write = function() {
return this.packets.write();
};
/** /**
* Returns ASCII armored text of message * Returns ASCII armored text of message
* @returns {ReadableStream<String>} ASCII armor * @returns {ReadableStream<String>} ASCII armor
*/ */
Message.prototype.armor = function() { Message.prototype.armor = function() {
return armor.encode(enums.armor.message, this.packets.write()); return armor.encode(enums.armor.message, this.write());
}; };
/** /**

View File

@ -303,9 +303,9 @@ export function encryptKey({ privateKey, passphrase }) {
* *
* { * {
* data: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default) * data: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default)
* message: Message, (if `armor` was false) * data: Uint8Array|ReadableStream<Uint8Array>|NodeStream, (if `armor` was false)
* signature: String|ReadableStream<String>|NodeStream, (if `detached` was true and `armor` was true) * signature: String|ReadableStream<String>|NodeStream, (if `detached` was true and `armor` was true)
* signature: Signature (if `detached` was true and `armor` was false) * signature: Uint8Array|ReadableStream<Uint8Array>|NodeStream (if `detached` was true and `armor` was false)
* sessionKey: { data, algorithm, aeadAlgorithm } (if `returnSessionKey` was true) * sessionKey: { data, algorithm, aeadAlgorithm } (if `returnSessionKey` was true)
* } * }
* @async * @async
@ -325,7 +325,7 @@ export function encrypt({ message, publicKeys, privateKeys, passwords, sessionKe
if (privateKeys.length || signature) { // sign the message only if private keys or signature is specified if (privateKeys.length || signature) { // sign the message only if private keys or signature is specified
if (detached) { if (detached) {
const detachedSignature = await message.signDetached(privateKeys, signature, date, fromUserIds, message.fromStream); const detachedSignature = await message.signDetached(privateKeys, signature, date, fromUserIds, message.fromStream);
result.signature = armor ? detachedSignature.armor() : detachedSignature; result.signature = armor ? detachedSignature.armor() : detachedSignature.write();
} else { } else {
message = await message.sign(privateKeys, signature, date, fromUserIds, message.fromStream); message = await message.sign(privateKeys, signature, date, fromUserIds, message.fromStream);
} }
@ -334,15 +334,11 @@ export function encrypt({ message, publicKeys, privateKeys, passwords, sessionKe
return message.encrypt(publicKeys, passwords, sessionKey, wildcard, date, toUserIds, streaming); return message.encrypt(publicKeys, passwords, sessionKey, wildcard, date, toUserIds, streaming);
}).then(async encrypted => { }).then(async encrypted => {
if (armor) { result.data = armor ? encrypted.message.armor() : encrypted.message.write();
result.data = encrypted.message.armor();
} else {
result.message = encrypted.message;
}
if (returnSessionKey) { if (returnSessionKey) {
result.sessionKey = encrypted.sessionKey; result.sessionKey = encrypted.sessionKey;
} }
return convertStreams(result, streaming, armor ? ['signature', 'data'] : []); return convertStreams(result, streaming, ['signature', 'data']);
}).catch(onError.bind(null, 'Error encrypting message')); }).catch(onError.bind(null, 'Error encrypting message'));
} }
@ -419,20 +415,22 @@ export function decrypt({ message, privateKeys, passwords, sessionKeys, publicKe
* *
* { * {
* data: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default) * data: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default)
* message: Message (if `armor` was false) * data: Uint8Array|ReadableStream<Uint8Array>|NodeStream (if `armor` was false)
* } * }
* *
* Or, if `detached` was true: * Or, if `detached` was true:
* *
* { * {
* signature: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default) * signature: String|ReadableStream<String>|NodeStream, (if `armor` was true, the default)
* signature: Signature (if `armor` was false) * signature: Uint8Array|ReadableStream<Uint8Array>|NodeStream (if `armor` was false)
* } * }
* @async * @async
* @static * @static
*/ */
export function sign({ message, privateKeys, armor = true, streaming = message && message.fromStream, detached = false, date = new Date(), fromUserIds = [] }) { export function sign({ message, privateKeys, armor = true, streaming = message && message.fromStream, detached = false, date = new Date(), fromUserIds = [] }) {
checkCleartextOrMessage(message); checkCleartextOrMessage(message);
if (message instanceof CleartextMessage && !armor) throw new Error("Can't sign non-armored cleartext message");
if (message instanceof CleartextMessage && detached) throw new Error("Can't sign detached cleartext message");
privateKeys = toArray(privateKeys); fromUserIds = toArray(fromUserIds); privateKeys = toArray(privateKeys); fromUserIds = toArray(fromUserIds);
if (asyncProxy) { // use web worker if available if (asyncProxy) { // use web worker if available
return asyncProxy.delegate('sign', { return asyncProxy.delegate('sign', {
@ -444,25 +442,19 @@ export function sign({ message, privateKeys, armor = true, streaming = message &
return Promise.resolve().then(async function() { return Promise.resolve().then(async function() {
if (detached) { if (detached) {
const signature = await message.signDetached(privateKeys, undefined, date, fromUserIds, message.fromStream); const signature = await message.signDetached(privateKeys, undefined, date, fromUserIds, message.fromStream);
result.signature = armor ? signature.armor() : signature; result.signature = armor ? signature.armor() : signature.write();
if (message.packets) {
result.signature = stream.transformPair(message.packets.write(), async (readable, writable) => { result.signature = stream.transformPair(message.packets.write(), async (readable, writable) => {
await Promise.all([ await Promise.all([
stream.pipe(result.signature, writable), stream.pipe(result.signature, writable),
stream.readToEnd(readable).catch(() => {}) stream.readToEnd(readable).catch(() => {})
]); ]);
}); });
}
} else { } else {
message = await message.sign(privateKeys, undefined, date, fromUserIds, message.fromStream); message = await message.sign(privateKeys, undefined, date, fromUserIds, message.fromStream);
if (armor) { result.data = armor ? message.armor() : message.write();
result.data = message.armor();
} else {
result.message = message;
} }
} return convertStreams(result, streaming, ['signature', 'data']);
return convertStreams(result, streaming, armor ? ['signature', 'data'] : []); }).catch(onError.bind(null, 'Error signing message'));
}).catch(onError.bind(null, 'Error signing cleartext message'));
} }
/** /**
@ -504,7 +496,7 @@ export function verify({ message, publicKeys, streaming = message && message.fro
result.data = await convertStream(result.data, streaming); result.data = await convertStream(result.data, streaming);
if (!streaming) await prepareSignatures(result.signatures); if (!streaming) await prepareSignatures(result.signatures);
return result; return result;
}).catch(onError.bind(null, 'Error verifying cleartext signed message')); }).catch(onError.bind(null, 'Error verifying signed message'));
} }

View File

@ -39,12 +39,20 @@ export function Signature(packetlist) {
} }
/**
* Returns binary encoded signature
* @returns {ReadableStream<Uint8Array>} binary signature
*/
Signature.prototype.write = function() {
return this.packets.write();
};
/** /**
* Returns ASCII armored text of signature * Returns ASCII armored text of signature
* @returns {ReadableStream<String>} ASCII armor * @returns {ReadableStream<String>} ASCII armor
*/ */
Signature.prototype.armor = function() { Signature.prototype.armor = function() {
return armor.encode(enums.armor.signature, this.packets.write()); return armor.encode(enums.armor.signature, this.write());
}; };
/** /**

View File

@ -3196,8 +3196,8 @@ VYGdb3eNlV8CfoEC
await privateKey.decrypt('hello world'); await privateKey.decrypt('hello world');
// Set second user to prefer aes128. We should select this user by default, since it was created later. // Set second user to prefer aes128. We should select this user by default, since it was created later.
publicKey.users[1].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128]; publicKey.users[1].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128];
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false}); const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false, returnSessionKey: true});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('aes128'); expect(encrypted.sessionKey.algorithm).to.equal('aes128');
}); });
it('Encrypt - primary user', async function() { it('Encrypt - primary user', async function() {
@ -3208,8 +3208,8 @@ VYGdb3eNlV8CfoEC
publicKey.users[0].selfCertifications[0].isPrimaryUserID = true; publicKey.users[0].selfCertifications[0].isPrimaryUserID = true;
// Set first user to prefer aes128. // Set first user to prefer aes128.
publicKey.users[0].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128]; publicKey.users[0].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128];
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false}); const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false, returnSessionKey: true});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('aes128'); expect(encrypted.sessionKey.algorithm).to.equal('aes128');
}); });
it('Encrypt - specific user', async function() { it('Encrypt - specific user', async function() {
@ -3220,8 +3220,8 @@ VYGdb3eNlV8CfoEC
publicKey.users[0].selfCertifications[0].isPrimaryUserID = true; publicKey.users[0].selfCertifications[0].isPrimaryUserID = true;
// Set second user to prefer aes128. We will select this user. // Set second user to prefer aes128. We will select this user.
publicKey.users[1].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128]; publicKey.users[1].selfCertifications[0].preferredSymmetricAlgorithms = [openpgp.enums.symmetric.aes128];
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIds: {name: 'Test User', email: 'b@c.com'}, armor: false}); const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIds: {name: 'Test User', email: 'b@c.com'}, armor: false, returnSessionKey: true});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('aes128'); expect(encrypted.sessionKey.algorithm).to.equal('aes128');
await expect(openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIds: {name: 'Test User', email: 'c@c.com'}, armor: false})).to.be.rejectedWith('Could not find user that matches that user ID'); await expect(openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, toUserIds: {name: 'Test User', email: 'c@c.com'}, armor: false})).to.be.rejectedWith('Could not find user that matches that user ID');
}); });
@ -3238,10 +3238,12 @@ VYGdb3eNlV8CfoEC
privateKey.users[0].userId.parse('Test User <b@c.com>'); privateKey.users[0].userId.parse('Test User <b@c.com>');
// Set second user to prefer aes128. We will select this user. // Set second user to prefer aes128. We will select this user.
privateKey.users[1].selfCertifications[0].preferredHashAlgorithms = [openpgp.enums.hash.sha512]; privateKey.users[1].selfCertifications[0].preferredHashAlgorithms = [openpgp.enums.hash.sha512];
const signed = await openpgp.sign({message: openpgp.cleartext.fromText('hello'), privateKeys: privateKey, fromUserIds: {name: 'Test McTestington', email: 'test@example.com'}, armor: false}); const signed = await openpgp.sign({message: openpgp.message.fromText('hello'), privateKeys: privateKey, fromUserIds: {name: 'Test McTestington', email: 'test@example.com'}, armor: false});
expect(signed.message.signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); const signature = await openpgp.message.read(signed.data);
expect(signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512);
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserIds: {name: 'Test McTestington', email: 'test@example.com'}, detached: true, armor: false}); const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserIds: {name: 'Test McTestington', email: 'test@example.com'}, detached: true, armor: false});
expect(encrypted.signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); const signature2 = await openpgp.message.read(encrypted.signature);
expect(signature2.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'}, detached: true, armor: false})).to.be.rejectedWith('Could not find user that matches that user ID'); await expect(openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserIds: {name: 'Not Test McTestington', email: 'test@example.com'}, detached: true, armor: false})).to.be.rejectedWith('Could not find user that matches that user ID');
}); });
@ -3433,12 +3435,13 @@ describe('addSubkey functionality testing', function(){
expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('eddsa'); expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('eddsa');
await subKey.verify(newPrivateKey.primaryKey); await subKey.verify(newPrivateKey.primaryKey);
expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey); expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey);
const signed = await openpgp.sign({message: openpgp.cleartext.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false}); const signed = await openpgp.sign({message: openpgp.message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false});
const verified = await signed.message.verify([newPrivateKey.toPublic()]); const message = await openpgp.message.read(signed.data);
expect(verified).to.exist; const { signatures } = await openpgp.verify({message, publicKeys: [newPrivateKey.toPublic()]});
expect(verified.length).to.be.equal(1); expect(signatures).to.exist;
expect(await verified[0].keyid).to.be.equal(subKey.getKeyId()); expect(signatures.length).to.be.equal(1);
expect(await verified[0].verified).to.be.true; expect(signatures[0].keyid.toHex()).to.be.equal(subKey.getKeyId().toHex());
expect(await signatures[0].verified).to.be.true;
}); });
it('encrypt/decrypt data with the new subkey correctly using curve25519', async function() { it('encrypt/decrypt data with the new subkey correctly using curve25519', async function() {
@ -3455,12 +3458,13 @@ describe('addSubkey functionality testing', function(){
await subKey.verify(newPrivateKey.primaryKey); await subKey.verify(newPrivateKey.primaryKey);
expect(await newPrivateKey.getEncryptionKey()).to.be.equal(subKey); 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: openpgp.message.fromText(vData), publicKeys: publicKey, armor:false});
expect(encrypted.message).to.be.exist; expect(encrypted.data).to.be.exist;
const pkSessionKeys = encrypted.message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey); const message = await openpgp.message.read(encrypted.data);
const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(pkSessionKeys).to.exist; expect(pkSessionKeys).to.exist;
expect(pkSessionKeys.length).to.be.equal(1); expect(pkSessionKeys.length).to.be.equal(1);
expect(pkSessionKeys[0].publicKeyId.toHex()).to.be.equals(subKey.keyPacket.getKeyId().toHex()); expect(pkSessionKeys[0].publicKeyId.toHex()).to.be.equals(subKey.keyPacket.getKeyId().toHex());
const decrypted = await openpgp.decrypt({message: encrypted.message, privateKeys: newPrivateKey}) const decrypted = await openpgp.decrypt({message, privateKeys: newPrivateKey})
expect(decrypted).to.exist; expect(decrypted).to.exist;
expect(decrypted.data).to.be.equal(vData); expect(decrypted.data).to.be.equal(vData);
}); });
@ -3477,12 +3481,13 @@ describe('addSubkey functionality testing', function(){
expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('rsa_encrypt_sign'); expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('rsa_encrypt_sign');
await subKey.verify(newPrivateKey.primaryKey); await subKey.verify(newPrivateKey.primaryKey);
expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey); expect(await newPrivateKey.getSigningKey()).to.be.equal(subKey);
const signed = await openpgp.sign({message: openpgp.cleartext.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false}); const signed = await openpgp.sign({message: openpgp.message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false});
const verified = await signed.message.verify([newPrivateKey.toPublic()]); const message = await openpgp.message.read(signed.data);
expect(verified).to.exist; const { signatures } = await openpgp.verify({message, publicKeys: [newPrivateKey.toPublic()]});
expect(verified.length).to.be.equal(1); expect(signatures).to.exist;
expect(await verified[0].keyid).to.be.equal(subKey.getKeyId()); expect(signatures.length).to.be.equal(1);
expect(await verified[0].verified).to.be.true; expect(signatures[0].keyid.toHex()).to.be.equal(subKey.getKeyId().toHex());
expect(await signatures[0].verified).to.be.true;
}); });
it('encrypt/decrypt data with the new subkey correctly using rsa', async function() { it('encrypt/decrypt data with the new subkey correctly using rsa', async function() {
@ -3497,12 +3502,13 @@ describe('addSubkey functionality testing', function(){
const vData = 'the data to encrypted!'; const vData = 'the data to encrypted!';
expect(await newPrivateKey.getEncryptionKey()).to.be.equal(subKey); 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: openpgp.message.fromText(vData), publicKeys: publicKey, armor:false});
expect(encrypted.message).to.be.exist; expect(encrypted.data).to.be.exist;
const pkSessionKeys = encrypted.message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey); const message = await openpgp.message.read(encrypted.data);
const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(pkSessionKeys).to.exist; expect(pkSessionKeys).to.exist;
expect(pkSessionKeys.length).to.be.equal(1); expect(pkSessionKeys.length).to.be.equal(1);
expect(pkSessionKeys[0].publicKeyId.toHex()).to.be.equals(subKey.keyPacket.getKeyId().toHex()); expect(pkSessionKeys[0].publicKeyId.toHex()).to.be.equals(subKey.keyPacket.getKeyId().toHex());
const decrypted = await openpgp.decrypt({message: encrypted.message, privateKeys: newPrivateKey}) const decrypted = await openpgp.decrypt({message, privateKeys: newPrivateKey})
expect(decrypted).to.exist; expect(decrypted).to.exist;
expect(decrypted.data).to.be.equal(vData); expect(decrypted.data).to.be.equal(vData);
}); });

View File

@ -1928,8 +1928,8 @@ describe('OpenPGP.js public api tests', function() {
const decOpt = { const decOpt = {
passwords: password1 passwords: password1
}; };
return openpgp.encrypt(encOpt).then(function (encrypted) { return openpgp.encrypt(encOpt).then(async function (encrypted) {
decOpt.message = encrypted.message; decOpt.message = await openpgp.message.read(encrypted.data);
return openpgp.decrypt(decOpt); return openpgp.decrypt(decOpt);
}).then(function (decrypted) { }).then(function (decrypted) {
expect(decrypted.data).to.equal(plaintext); expect(decrypted.data).to.equal(plaintext);
@ -1948,8 +1948,14 @@ describe('OpenPGP.js public api tests', function() {
passwords: password1, passwords: password1,
format: 'binary' format: 'binary'
}; };
return openpgp.encrypt(encOpt).then(function (encrypted) { return openpgp.encrypt(encOpt).then(async function (encrypted) {
decOpt.message = encrypted.message; decOpt.message = await openpgp.message.read(encrypted.data);
openpgp.config.zero_copy = false;
if (openpgp.getWorker()) {
openpgp.getWorker().workers.forEach(worker => {
worker.postMessage({ event: 'configure', config: openpgp.config });
});
}
return openpgp.decrypt(decOpt); return openpgp.decrypt(decOpt);
}).then(function (decrypted) { }).then(function (decrypted) {
if (openpgp.getWorker()) { if (openpgp.getWorker()) {
@ -2041,7 +2047,7 @@ describe('OpenPGP.js public api tests', function() {
privateKey = decryptedPrivateKey; privateKey = decryptedPrivateKey;
}); });
it('should sign and verify cleartext data', function () { it('should sign and verify cleartext message', function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.cleartext.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
@ -2063,7 +2069,7 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and verify cleartext data with multiple private keys', async function () { it('should sign and verify cleartext message with multiple private keys', async function () {
const privKeyDE = (await openpgp.key.readArmored(priv_key_de)).keys[0]; const privKeyDE = (await openpgp.key.readArmored(priv_key_de)).keys[0];
await privKeyDE.decrypt(passphrase); await privKeyDE.decrypt(passphrase);
@ -2093,8 +2099,8 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and verify cleartext data with detached signatures', function () { it('should sign and verify data with detached signatures', function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.message.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys, privateKeys: privateKey.keys,
@ -2108,7 +2114,7 @@ describe('OpenPGP.js public api tests', function() {
verifyOpt.signature = await openpgp.signature.readArmored(signed.signature); verifyOpt.signature = await openpgp.signature.readArmored(signed.signature);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(verified.signatures[0].valid).to.be.true; expect(verified.signatures[0].valid).to.be.true;
const signingKey = await privateKey.keys[0].getSigningKey(); const signingKey = await privateKey.keys[0].getSigningKey();
expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex()); expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex());
@ -2116,7 +2122,7 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and fail to verify cleartext data with wrong public pgp key', async function () { it('should sign and fail to verify cleartext message with wrong public pgp key', async function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.cleartext.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
@ -2137,8 +2143,8 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and fail to verify cleartext data with wrong public pgp key with detached signature', async function () { it('should sign and fail to verify data with wrong public pgp key with detached signature', async function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.message.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys, privateKeys: privateKey.keys,
@ -2152,7 +2158,7 @@ describe('OpenPGP.js public api tests', function() {
verifyOpt.signature = await openpgp.signature.readArmored(signed.signature); verifyOpt.signature = await openpgp.signature.readArmored(signed.signature);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(verified.signatures[0].valid).to.be.null; expect(verified.signatures[0].valid).to.be.null;
const signingKey = await privateKey.keys[0].getSigningKey(); const signingKey = await privateKey.keys[0].getSigningKey();
expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex()); expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex());
@ -2160,8 +2166,8 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and verify cleartext data and not armor', function () { it('should sign and verify data and not armor', function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.message.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys, privateKeys: privateKey.keys,
@ -2170,11 +2176,11 @@ describe('OpenPGP.js public api tests', function() {
const verifyOpt = { const verifyOpt = {
publicKeys: publicKey.keys publicKeys: publicKey.keys
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.message = signed.message; verifyOpt.message = await openpgp.message.read(signed.data);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(verified.signatures[0].valid).to.be.true; expect(verified.signatures[0].valid).to.be.true;
const signingKey = await privateKey.keys[0].getSigningKey(); const signingKey = await privateKey.keys[0].getSigningKey();
expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex()); expect(verified.signatures[0].keyid.toHex()).to.equal(signingKey.getKeyId().toHex());
@ -2182,9 +2188,9 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and verify cleartext data and not armor with detached signatures', function () { it('should sign and verify data and not armor with detached signatures', function () {
const start = openpgp.util.normalizeDate(); const start = openpgp.util.normalizeDate();
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.message.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys, privateKeys: privateKey.keys,
@ -2195,11 +2201,11 @@ describe('OpenPGP.js public api tests', function() {
message, message,
publicKeys: publicKey.keys publicKeys: publicKey.keys
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.signature = signed.signature; verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(+verified.signatures[0].signature.packets[0].created).to.be.lte(+openpgp.util.normalizeDate()); expect(+verified.signatures[0].signature.packets[0].created).to.be.lte(+openpgp.util.normalizeDate());
expect(+verified.signatures[0].signature.packets[0].created).to.be.gte(+start); expect(+verified.signatures[0].signature.packets[0].created).to.be.gte(+start);
expect(verified.signatures[0].valid).to.be.true; expect(verified.signatures[0].valid).to.be.true;
@ -2209,8 +2215,8 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign and verify cleartext data with a date in the past', function () { it('should sign and verify data with a date in the past', function () {
const message = openpgp.cleartext.fromText(plaintext); const message = openpgp.message.fromText(plaintext);
const past = new Date(2000); const past = new Date(2000);
const signOpt = { const signOpt = {
message, message,
@ -2224,11 +2230,11 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey_1337.keys, publicKeys: publicKey_1337.keys,
date: past date: past
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.signature = signed.signature; verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt).then(async function (verified) { return openpgp.verify(verifyOpt).then(async function (verified) {
expect(+verified.signatures[0].signature.packets[0].created).to.equal(+past); expect(+verified.signatures[0].signature.packets[0].created).to.equal(+past);
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(verified.signatures[0].valid).to.be.true; expect(verified.signatures[0].valid).to.be.true;
expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, past)) expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, past))
.to.be.not.null; .to.be.not.null;
@ -2238,7 +2244,7 @@ describe('OpenPGP.js public api tests', function() {
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(+verified.signatures[0].signature.packets[0].created).to.equal(+past); expect(+verified.signatures[0].signature.packets[0].created).to.equal(+past);
expect(verified.data).to.equal(plaintext.replace(/[ \t]+$/mg, '')); expect(verified.data).to.equal(plaintext);
expect(verified.signatures[0].valid).to.be.true; expect(verified.signatures[0].valid).to.be.true;
expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, null)) expect(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, null))
.to.be.not.null; .to.be.not.null;
@ -2261,9 +2267,9 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey_2038_2045.keys, publicKeys: publicKey_2038_2045.keys,
date: future date: future
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.message = openpgp.message.fromBinary(data); verifyOpt.message = openpgp.message.fromBinary(data);
verifyOpt.signature = signed.signature; verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
expect(+verified.signatures[0].signature.packets[0].created).to.equal(+future); expect(+verified.signatures[0].signature.packets[0].created).to.equal(+future);
@ -2285,10 +2291,12 @@ describe('OpenPGP.js public api tests', function() {
const verifyOpt = { const verifyOpt = {
publicKeys: publicKey.keys publicKeys: publicKey.keys
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
const message = await openpgp.message.read(signed.data);
message.packets.concat(await openpgp.stream.readToEnd(message.packets.stream, _ => _));
const packets = new openpgp.packet.List(); const packets = new openpgp.packet.List();
packets.push(signed.message.packets.findPacket(openpgp.enums.packet.signature)); packets.push(message.packets.findPacket(openpgp.enums.packet.signature));
packets.push(signed.message.packets.findPacket(openpgp.enums.packet.literal)); packets.push(message.packets.findPacket(openpgp.enums.packet.literal));
verifyOpt.message = new openpgp.message.Message(packets); verifyOpt.message = new openpgp.message.Message(packets);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
@ -2312,10 +2320,13 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey.keys, publicKeys: publicKey.keys,
streaming: 'web' streaming: 'web'
}; };
return openpgp.sign(signOpt).then(function (signed) { return openpgp.sign(signOpt).then(async function (signed) {
expect(openpgp.util.isStream(signed.data)).to.equal('web');
const message = await openpgp.message.read(signed.data);
message.packets.concat(await openpgp.stream.readToEnd(message.packets.stream, _ => _));
const packets = new openpgp.packet.List(); const packets = new openpgp.packet.List();
packets.push(signed.message.packets.findPacket(openpgp.enums.packet.signature)); packets.push(message.packets.findPacket(openpgp.enums.packet.signature));
packets.push(signed.message.packets.findPacket(openpgp.enums.packet.literal)); packets.push(message.packets.findPacket(openpgp.enums.packet.literal));
verifyOpt.message = new openpgp.message.Message(packets); verifyOpt.message = new openpgp.message.Message(packets);
return openpgp.verify(verifyOpt); return openpgp.verify(verifyOpt);
}).then(async function (verified) { }).then(async function (verified) {
@ -2328,7 +2339,7 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should encrypt and decrypt cleartext data with a date in the future', function () { it('should encrypt and decrypt data with a date in the future', function () {
const future = new Date(2040, 5, 5, 5, 5, 5, 0); const future = new Date(2040, 5, 5, 5, 5, 5, 0);
const encryptOpt = { const encryptOpt = {
message: openpgp.message.fromText(plaintext, undefined, future), message: openpgp.message.fromText(plaintext, undefined, future),
@ -2336,14 +2347,10 @@ describe('OpenPGP.js public api tests', function() {
date: future, date: future,
armor: false armor: false
}; };
const decryptOpt = {
privateKeys: privateKey_2038_2045.keys,
date: future
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) { return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
decryptOpt.message = encrypted.message; const message = await openpgp.message.read(encrypted.data);
return encrypted.message.decrypt(decryptOpt.privateKeys); return message.decrypt(privateKey_2038_2045.keys);
}).then(async function (packets) { }).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal); const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
@ -2361,14 +2368,10 @@ describe('OpenPGP.js public api tests', function() {
date: past, date: past,
armor: false armor: false
}; };
const decryptOpt = {
privateKeys: privateKey_2000_2008.keys,
date: past
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) { return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
decryptOpt.message = encrypted.message; const message = await openpgp.message.read(encrypted.data);
return encrypted.message.decrypt(decryptOpt.privateKeys); return message.decrypt(privateKey_2000_2008.keys);
}).then(async function (packets) { }).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal); const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
@ -2377,7 +2380,7 @@ describe('OpenPGP.js public api tests', function() {
}); });
}); });
it('should sign, encrypt and decrypt, verify cleartext data with a date in the past', function () { it('should sign, encrypt and decrypt, verify data with a date in the past', function () {
const past = new Date(2005, 5, 5, 5, 5, 5, 0); const past = new Date(2005, 5, 5, 5, 5, 5, 0);
const encryptOpt = { const encryptOpt = {
message: openpgp.message.fromText(plaintext, undefined, past), message: openpgp.message.fromText(plaintext, undefined, past),
@ -2387,8 +2390,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false armor: false
}; };
return openpgp.encrypt(encryptOpt).then(function (encrypted) { return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys); const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) { }).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal); const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
@ -2414,8 +2418,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false armor: false
}; };
return openpgp.encrypt(encryptOpt).then(function (encrypted) { return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys); const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) { }).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal); const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);
@ -2442,8 +2447,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false armor: false
}; };
return openpgp.encrypt(encryptOpt).then(function (encrypted) { return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys); const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) { }).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal); const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1); expect(literals.length).to.equal(1);

View File

@ -1439,9 +1439,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA
const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0];
await privKey.decrypt('hello world'); await privKey.decrypt('hello world');
return openpgp.sign({ privateKeys:[privKey], message: openpgp.message.fromBinary(plaintext), armor:false }).then(function(signed) { return openpgp.sign({ privateKeys:[privKey], message: openpgp.message.fromBinary(plaintext), armor:false }).then(async function(signed) {
const csMsg = signed.message; const csMsg = await openpgp.message.read(signed.data);
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); return openpgp.verify({ publicKeys:[pubKey], message:csMsg });
}).then(function(cleartextSig) { }).then(function(cleartextSig) {

View File

@ -234,10 +234,11 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test'],
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
setTimeout(dataArrived, 3000); // Do not wait until data arrived, but wait a bit to check that it doesn't arrive early. setTimeout(dataArrived, 3000); // Do not wait until data arrived, but wait a bit to check that it doesn't arrive early.
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
@ -258,10 +259,11 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test'],
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
message, message,
@ -286,11 +288,12 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
publicKeys: pubKey, publicKeys: pubKey,
privateKeys: privKey privateKeys: privKey,
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
publicKeys: pubKey, publicKeys: pubKey,
privateKeys: privKey, privateKeys: privKey,
@ -317,11 +320,12 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
publicKeys: pub, publicKeys: pub,
privateKeys: priv privateKeys: priv,
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
publicKeys: pub, publicKeys: pub,
privateKeys: priv, privateKeys: priv,
@ -348,11 +352,12 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
publicKeys: pub, publicKeys: pub,
privateKeys: priv privateKeys: priv,
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
publicKeys: pub, publicKeys: pub,
privateKeys: priv, privateKeys: priv,
@ -375,8 +380,9 @@ function tests() {
try { try {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test']
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => { const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -412,6 +418,7 @@ function tests() {
publicKeys: pubKey, publicKeys: pubKey,
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => { const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -447,6 +454,7 @@ function tests() {
publicKeys: pubKey, publicKeys: pubKey,
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => { const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -478,6 +486,7 @@ function tests() {
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data; const msgAsciiArmored = signed.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => { const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -504,14 +513,16 @@ function tests() {
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte; let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true; openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4; openpgp.config.aead_chunk_size_byte = 4;
try { try {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test'],
armor: false
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const message = await openpgp.message.read(encrypted.data);
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
passwords: ['test'], passwords: ['test'],
message, message,
@ -551,8 +562,9 @@ function tests() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromText(data), message: openpgp.message.fromText(data),
streaming: expectedType, streaming: expectedType,
passwords: ['test'], passwords: ['test']
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored); const message = await openpgp.message.readArmored(msgAsciiArmored);
@ -596,8 +608,8 @@ function tests() {
}); });
it('Input stream should be canceled when canceling decrypted stream (AEAD)', async function() { it('Input stream should be canceled when canceling decrypted stream (AEAD)', async function() {
let aead_protectValue = openpgp.config.aead_protect; const aead_protectValue = openpgp.config.aead_protect;
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte; const aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true; openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4; openpgp.config.aead_chunk_size_byte = 4;
try { try {
@ -631,6 +643,7 @@ function tests() {
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data; const msgAsciiArmored = signed.data;
const message = await openpgp.message.readArmored(msgAsciiArmored); const message = await openpgp.message.readArmored(msgAsciiArmored);
@ -652,8 +665,10 @@ function tests() {
it("Don't pull entire input stream when we're not pulling encrypted stream", async function() { it("Don't pull entire input stream when we're not pulling encrypted stream", async function() {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test']
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(encrypted.data); const reader = openpgp.stream.getReader(encrypted.data);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/);
dataArrived(); dataArrived();
@ -666,6 +681,8 @@ function tests() {
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(signed.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.data); const reader = openpgp.stream.getReader(signed.data);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/); expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/);
dataArrived(); dataArrived();
@ -683,8 +700,9 @@ function tests() {
try { try {
const encrypted = await openpgp.encrypt({ const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
passwords: ['test'], passwords: ['test']
}); });
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data; const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored); const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({ const decrypted = await openpgp.decrypt({
@ -710,6 +728,7 @@ function tests() {
message: openpgp.message.fromBinary(data), message: openpgp.message.fromBinary(data),
privateKeys: privKey privateKeys: privKey
}); });
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data; const msgAsciiArmored = signed.data;
const message = await openpgp.message.readArmored(msgAsciiArmored); const message = await openpgp.message.readArmored(msgAsciiArmored);
const verified = await openpgp.verify({ const verified = await openpgp.verify({
@ -739,6 +758,7 @@ function tests() {
detached: true, detached: true,
streaming: expectedType streaming: expectedType
}); });
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature); const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored); const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({ const verified = await openpgp.verify({
@ -765,9 +785,10 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
detached: true, detached: true,
streaming: false, streaming: false,
armor: false
}); });
const sigArmored = await openpgp.stream.readToEnd(signed.signature); expect(util.isStream(signed.signature)).to.be.false;
const signature = await openpgp.message.readArmored(sigArmored); const signature = await openpgp.message.read(signed.signature);
const verified = await openpgp.verify({ const verified = await openpgp.verify({
signature, signature,
publicKeys: pubKey, publicKeys: pubKey,
@ -796,6 +817,7 @@ function tests() {
detached: true, detached: true,
streaming: expectedType streaming: expectedType
}); });
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature); const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored); const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({ const verified = await openpgp.verify({
@ -826,6 +848,7 @@ function tests() {
detached: true, detached: true,
streaming: expectedType streaming: expectedType
}); });
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature); const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored); const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({ const verified = await openpgp.verify({
@ -844,6 +867,7 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
detached: true detached: true
}); });
expect(util.isStream(signed.signature)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.signature); const reader = openpgp.stream.getReader(signed.signature);
expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n'); expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n');
dataArrived(); dataArrived();
@ -857,6 +881,7 @@ function tests() {
privateKeys: privKey, privateKeys: privKey,
detached: true detached: true
}); });
expect(util.isStream(signed.signature)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.signature); const reader = openpgp.stream.getReader(signed.signature);
expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n'); expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n');
dataArrived(); dataArrived();