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);
};
/**
* Returns binary encoded message
* @returns {ReadableStream<Uint8Array>} binary message
*/
Message.prototype.write = function() {
return this.packets.write();
};
/**
* Returns ASCII armored text of message
* @returns {ReadableStream<String>} ASCII armor
*/
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)
* 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: 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)
* }
* @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 (detached) {
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 {
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);
}).then(async encrypted => {
if (armor) {
result.data = encrypted.message.armor();
} else {
result.message = encrypted.message;
}
result.data = armor ? encrypted.message.armor() : encrypted.message.write();
if (returnSessionKey) {
result.sessionKey = encrypted.sessionKey;
}
return convertStreams(result, streaming, armor ? ['signature', 'data'] : []);
return convertStreams(result, streaming, ['signature', 'data']);
}).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)
* message: Message (if `armor` was false)
* data: Uint8Array|ReadableStream<Uint8Array>|NodeStream (if `armor` was false)
* }
*
* Or, if `detached` was true:
*
* {
* 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
* @static
*/
export function sign({ message, privateKeys, armor = true, streaming = message && message.fromStream, detached = false, date = new Date(), fromUserIds = [] }) {
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);
if (asyncProxy) { // use web worker if available
return asyncProxy.delegate('sign', {
@ -444,25 +442,19 @@ export function sign({ message, privateKeys, armor = true, streaming = message &
return Promise.resolve().then(async function() {
if (detached) {
const signature = await message.signDetached(privateKeys, undefined, date, fromUserIds, message.fromStream);
result.signature = armor ? signature.armor() : signature;
if (message.packets) {
result.signature = stream.transformPair(message.packets.write(), async (readable, writable) => {
await Promise.all([
stream.pipe(result.signature, writable),
stream.readToEnd(readable).catch(() => {})
]);
});
}
result.signature = armor ? signature.armor() : signature.write();
result.signature = stream.transformPair(message.packets.write(), async (readable, writable) => {
await Promise.all([
stream.pipe(result.signature, writable),
stream.readToEnd(readable).catch(() => {})
]);
});
} else {
message = await message.sign(privateKeys, undefined, date, fromUserIds, message.fromStream);
if (armor) {
result.data = message.armor();
} else {
result.message = message;
}
result.data = armor ? message.armor() : message.write();
}
return convertStreams(result, streaming, armor ? ['signature', 'data'] : []);
}).catch(onError.bind(null, 'Error signing cleartext message'));
return convertStreams(result, streaming, ['signature', 'data']);
}).catch(onError.bind(null, 'Error signing message'));
}
/**
@ -504,7 +496,7 @@ export function verify({ message, publicKeys, streaming = message && message.fro
result.data = await convertStream(result.data, streaming);
if (!streaming) await prepareSignatures(result.signatures);
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 {ReadableStream<String>} ASCII armor
*/
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');
// 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];
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('aes128');
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false, returnSessionKey: true});
expect(encrypted.sessionKey.algorithm).to.equal('aes128');
});
it('Encrypt - primary user', async function() {
@ -3208,8 +3208,8 @@ VYGdb3eNlV8CfoEC
publicKey.users[0].selfCertifications[0].isPrimaryUserID = true;
// Set first user to prefer 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});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('aes128');
const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, armor: false, returnSessionKey: true});
expect(encrypted.sessionKey.algorithm).to.equal('aes128');
});
it('Encrypt - specific user', async function() {
@ -3220,8 +3220,8 @@ VYGdb3eNlV8CfoEC
publicKey.users[0].selfCertifications[0].isPrimaryUserID = true;
// Set second user to prefer aes128. We will select this user.
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});
expect(encrypted.message.packets[0].sessionKeyAlgorithm).to.equal('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, returnSessionKey: true});
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');
});
@ -3238,10 +3238,12 @@ VYGdb3eNlV8CfoEC
privateKey.users[0].userId.parse('Test User <b@c.com>');
// Set second user to prefer aes128. We will select this user.
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});
expect(signed.message.signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512);
const signed = await openpgp.sign({message: openpgp.message.fromText('hello'), privateKeys: privateKey, fromUserIds: {name: 'Test McTestington', email: 'test@example.com'}, armor: false});
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});
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');
});
@ -3433,12 +3435,13 @@ describe('addSubkey functionality testing', function(){
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.cleartext.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false});
const verified = await signed.message.verify([newPrivateKey.toPublic()]);
expect(verified).to.exist;
expect(verified.length).to.be.equal(1);
expect(await verified[0].keyid).to.be.equal(subKey.getKeyId());
expect(await verified[0].verified).to.be.true;
const signed = await openpgp.sign({message: openpgp.message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false});
const message = await openpgp.message.read(signed.data);
const { signatures } = await openpgp.verify({message, publicKeys: [newPrivateKey.toPublic()]});
expect(signatures).to.exist;
expect(signatures.length).to.be.equal(1);
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() {
@ -3455,12 +3458,13 @@ describe('addSubkey functionality testing', function(){
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});
expect(encrypted.message).to.be.exist;
const pkSessionKeys = encrypted.message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(encrypted.data).to.be.exist;
const message = await openpgp.message.read(encrypted.data);
const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(pkSessionKeys).to.exist;
expect(pkSessionKeys.length).to.be.equal(1);
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.data).to.be.equal(vData);
});
@ -3477,12 +3481,13 @@ describe('addSubkey functionality testing', function(){
expect(subKey.getAlgorithmInfo().algorithm).to.be.equal('rsa_encrypt_sign');
await subKey.verify(newPrivateKey.primaryKey);
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 verified = await signed.message.verify([newPrivateKey.toPublic()]);
expect(verified).to.exist;
expect(verified.length).to.be.equal(1);
expect(await verified[0].keyid).to.be.equal(subKey.getKeyId());
expect(await verified[0].verified).to.be.true;
const signed = await openpgp.sign({message: openpgp.message.fromText('the data to signed'), privateKeys: newPrivateKey, armor:false});
const message = await openpgp.message.read(signed.data);
const { signatures } = await openpgp.verify({message, publicKeys: [newPrivateKey.toPublic()]});
expect(signatures).to.exist;
expect(signatures.length).to.be.equal(1);
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() {
@ -3497,12 +3502,13 @@ describe('addSubkey functionality testing', function(){
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});
expect(encrypted.message).to.be.exist;
const pkSessionKeys = encrypted.message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(encrypted.data).to.be.exist;
const message = await openpgp.message.read(encrypted.data);
const pkSessionKeys = message.packets.filterByTag(openpgp.enums.packet.publicKeyEncryptedSessionKey);
expect(pkSessionKeys).to.exist;
expect(pkSessionKeys.length).to.be.equal(1);
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.data).to.be.equal(vData);
});

View File

@ -1928,8 +1928,8 @@ describe('OpenPGP.js public api tests', function() {
const decOpt = {
passwords: password1
};
return openpgp.encrypt(encOpt).then(function (encrypted) {
decOpt.message = encrypted.message;
return openpgp.encrypt(encOpt).then(async function (encrypted) {
decOpt.message = await openpgp.message.read(encrypted.data);
return openpgp.decrypt(decOpt);
}).then(function (decrypted) {
expect(decrypted.data).to.equal(plaintext);
@ -1948,8 +1948,14 @@ describe('OpenPGP.js public api tests', function() {
passwords: password1,
format: 'binary'
};
return openpgp.encrypt(encOpt).then(function (encrypted) {
decOpt.message = encrypted.message;
return openpgp.encrypt(encOpt).then(async function (encrypted) {
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);
}).then(function (decrypted) {
if (openpgp.getWorker()) {
@ -2041,7 +2047,7 @@ describe('OpenPGP.js public api tests', function() {
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 signOpt = {
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];
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 () {
const message = openpgp.cleartext.fromText(plaintext);
it('should sign and verify data with detached signatures', function () {
const message = openpgp.message.fromText(plaintext);
const signOpt = {
message,
privateKeys: privateKey.keys,
@ -2108,7 +2114,7 @@ describe('OpenPGP.js public api tests', function() {
verifyOpt.signature = await openpgp.signature.readArmored(signed.signature);
return openpgp.verify(verifyOpt);
}).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;
const signingKey = await privateKey.keys[0].getSigningKey();
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 signOpt = {
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 () {
const message = openpgp.cleartext.fromText(plaintext);
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 signOpt = {
message,
privateKeys: privateKey.keys,
@ -2152,7 +2158,7 @@ describe('OpenPGP.js public api tests', function() {
verifyOpt.signature = await openpgp.signature.readArmored(signed.signature);
return openpgp.verify(verifyOpt);
}).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;
const signingKey = await privateKey.keys[0].getSigningKey();
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 () {
const message = openpgp.cleartext.fromText(plaintext);
it('should sign and verify data and not armor', function () {
const message = openpgp.message.fromText(plaintext);
const signOpt = {
message,
privateKeys: privateKey.keys,
@ -2170,11 +2176,11 @@ describe('OpenPGP.js public api tests', function() {
const verifyOpt = {
publicKeys: publicKey.keys
};
return openpgp.sign(signOpt).then(function (signed) {
verifyOpt.message = signed.message;
return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.message = await openpgp.message.read(signed.data);
return openpgp.verify(verifyOpt);
}).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;
const signingKey = await privateKey.keys[0].getSigningKey();
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 message = openpgp.cleartext.fromText(plaintext);
const message = openpgp.message.fromText(plaintext);
const signOpt = {
message,
privateKeys: privateKey.keys,
@ -2195,11 +2201,11 @@ describe('OpenPGP.js public api tests', function() {
message,
publicKeys: publicKey.keys
};
return openpgp.sign(signOpt).then(function (signed) {
verifyOpt.signature = signed.signature;
return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt);
}).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.gte(+start);
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 () {
const message = openpgp.cleartext.fromText(plaintext);
it('should sign and verify data with a date in the past', function () {
const message = openpgp.message.fromText(plaintext);
const past = new Date(2000);
const signOpt = {
message,
@ -2224,11 +2230,11 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey_1337.keys,
date: past
};
return openpgp.sign(signOpt).then(function (signed) {
verifyOpt.signature = signed.signature;
return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt).then(async function (verified) {
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(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, past))
.to.be.not.null;
@ -2238,7 +2244,7 @@ describe('OpenPGP.js public api tests', function() {
return openpgp.verify(verifyOpt);
}).then(async function (verified) {
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(await signOpt.privateKeys[0].getSigningKey(verified.signatures[0].keyid, null))
.to.be.not.null;
@ -2261,9 +2267,9 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey_2038_2045.keys,
date: future
};
return openpgp.sign(signOpt).then(function (signed) {
return openpgp.sign(signOpt).then(async function (signed) {
verifyOpt.message = openpgp.message.fromBinary(data);
verifyOpt.signature = signed.signature;
verifyOpt.signature = await openpgp.signature.read(signed.signature);
return openpgp.verify(verifyOpt);
}).then(async function (verified) {
expect(+verified.signatures[0].signature.packets[0].created).to.equal(+future);
@ -2285,10 +2291,12 @@ describe('OpenPGP.js public api tests', function() {
const verifyOpt = {
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();
packets.push(signed.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.signature));
packets.push(message.packets.findPacket(openpgp.enums.packet.literal));
verifyOpt.message = new openpgp.message.Message(packets);
return openpgp.verify(verifyOpt);
}).then(async function (verified) {
@ -2312,10 +2320,13 @@ describe('OpenPGP.js public api tests', function() {
publicKeys: publicKey.keys,
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();
packets.push(signed.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.signature));
packets.push(message.packets.findPacket(openpgp.enums.packet.literal));
verifyOpt.message = new openpgp.message.Message(packets);
return openpgp.verify(verifyOpt);
}).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 encryptOpt = {
message: openpgp.message.fromText(plaintext, undefined, future),
@ -2336,14 +2347,10 @@ describe('OpenPGP.js public api tests', function() {
date: future,
armor: false
};
const decryptOpt = {
privateKeys: privateKey_2038_2045.keys,
date: future
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) {
decryptOpt.message = encrypted.message;
return encrypted.message.decrypt(decryptOpt.privateKeys);
return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
const message = await openpgp.message.read(encrypted.data);
return message.decrypt(privateKey_2038_2045.keys);
}).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1);
@ -2361,14 +2368,10 @@ describe('OpenPGP.js public api tests', function() {
date: past,
armor: false
};
const decryptOpt = {
privateKeys: privateKey_2000_2008.keys,
date: past
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) {
decryptOpt.message = encrypted.message;
return encrypted.message.decrypt(decryptOpt.privateKeys);
return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
const message = await openpgp.message.read(encrypted.data);
return message.decrypt(privateKey_2000_2008.keys);
}).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
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 encryptOpt = {
message: openpgp.message.fromText(plaintext, undefined, past),
@ -2387,8 +2390,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys);
return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1);
@ -2414,8 +2418,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys);
return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1);
@ -2442,8 +2447,9 @@ describe('OpenPGP.js public api tests', function() {
armor: false
};
return openpgp.encrypt(encryptOpt).then(function (encrypted) {
return encrypted.message.decrypt(encryptOpt.privateKeys);
return openpgp.encrypt(encryptOpt).then(async function (encrypted) {
const message = await openpgp.message.read(encrypted.data);
return message.decrypt(encryptOpt.privateKeys);
}).then(async function (packets) {
const literals = packets.packets.filterByTag(openpgp.enums.packet.literal);
expect(literals.length).to.equal(1);

View File

@ -1439,9 +1439,9 @@ hkJiXopCSWKSlQInL1devkJJUWJmTmZeugJYlpdLAagQJM0JpsCqIQZwKgAA
const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0];
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 });
}).then(function(cleartextSig) {

View File

@ -234,10 +234,11 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
armor: false
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
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({
passwords: ['test'],
@ -258,10 +259,11 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
armor: false
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
@ -286,11 +288,12 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
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.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
const decrypted = await openpgp.decrypt({
publicKeys: pubKey,
privateKeys: privKey,
@ -317,11 +320,12 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
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.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
const decrypted = await openpgp.decrypt({
publicKeys: pub,
privateKeys: priv,
@ -348,11 +352,12 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
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.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
const decrypted = await openpgp.decrypt({
publicKeys: pub,
privateKeys: priv,
@ -375,8 +380,9 @@ function tests() {
try {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
passwords: ['test']
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -412,6 +418,7 @@ function tests() {
publicKeys: pubKey,
privateKeys: privKey
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -447,6 +454,7 @@ function tests() {
publicKeys: pubKey,
privateKeys: privKey
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(openpgp.stream.transform(msgAsciiArmored, value => {
@ -478,6 +486,7 @@ function tests() {
message: openpgp.message.fromBinary(data),
privateKeys: privKey
});
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data;
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;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4;
try {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
armor: false
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
const message = await openpgp.message.read(encrypted.data);
const decrypted = await openpgp.decrypt({
passwords: ['test'],
message,
@ -551,8 +562,9 @@ function tests() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromText(data),
streaming: expectedType,
passwords: ['test'],
passwords: ['test']
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
@ -586,7 +598,7 @@ function tests() {
}
await writer.write(value);
}
} catch(e) {
} catch (e) {
await writer.abort(e);
}
});
@ -596,8 +608,8 @@ function tests() {
});
it('Input stream should be canceled when canceling decrypted stream (AEAD)', async function() {
let aead_protectValue = openpgp.config.aead_protect;
let aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
const aead_protectValue = openpgp.config.aead_protect;
const aead_chunk_size_byteValue = openpgp.config.aead_chunk_size_byte;
openpgp.config.aead_protect = true;
openpgp.config.aead_chunk_size_byte = 4;
try {
@ -631,6 +643,7 @@ function tests() {
message: openpgp.message.fromBinary(data),
privateKeys: privKey
});
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data;
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() {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
passwords: ['test']
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(encrypted.data);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/);
dataArrived();
@ -666,6 +681,8 @@ function tests() {
message: openpgp.message.fromBinary(data),
privateKeys: privKey
});
expect(util.isStream(signed.data)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.data);
expect(await reader.readBytes(1024)).to.match(/^-----BEGIN PGP MESSAGE-----\r\n/);
dataArrived();
@ -683,8 +700,9 @@ function tests() {
try {
const encrypted = await openpgp.encrypt({
message: openpgp.message.fromBinary(data),
passwords: ['test'],
passwords: ['test']
});
expect(util.isStream(encrypted.data)).to.equal(expectedType);
const msgAsciiArmored = encrypted.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
const decrypted = await openpgp.decrypt({
@ -710,6 +728,7 @@ function tests() {
message: openpgp.message.fromBinary(data),
privateKeys: privKey
});
expect(util.isStream(signed.data)).to.equal(expectedType);
const msgAsciiArmored = signed.data;
const message = await openpgp.message.readArmored(msgAsciiArmored);
const verified = await openpgp.verify({
@ -739,6 +758,7 @@ function tests() {
detached: true,
streaming: expectedType
});
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({
@ -765,9 +785,10 @@ function tests() {
privateKeys: privKey,
detached: true,
streaming: false,
armor: false
});
const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored);
expect(util.isStream(signed.signature)).to.be.false;
const signature = await openpgp.message.read(signed.signature);
const verified = await openpgp.verify({
signature,
publicKeys: pubKey,
@ -796,6 +817,7 @@ function tests() {
detached: true,
streaming: expectedType
});
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({
@ -826,6 +848,7 @@ function tests() {
detached: true,
streaming: expectedType
});
expect(util.isStream(signed.signature)).to.equal(expectedType);
const sigArmored = await openpgp.stream.readToEnd(signed.signature);
const signature = await openpgp.message.readArmored(sigArmored);
const verified = await openpgp.verify({
@ -844,6 +867,7 @@ function tests() {
privateKeys: privKey,
detached: true
});
expect(util.isStream(signed.signature)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.signature);
expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n');
dataArrived();
@ -857,6 +881,7 @@ function tests() {
privateKeys: privKey,
detached: true
});
expect(util.isStream(signed.signature)).to.equal(expectedType);
const reader = openpgp.stream.getReader(signed.signature);
expect((await reader.readBytes(31)).toString('utf8')).to.equal('-----BEGIN PGP SIGNATURE-----\r\n');
dataArrived();