encrypt/decrypt/sign/verify will always return promises
Note: publicKeyEncryptedSessionKey uses promises, symEncryptedSessionKey does not
This commit is contained in:
parent
1a714cec73
commit
21ae66c604
|
@ -330,6 +330,7 @@ module.exports = {
|
||||||
// Custom silencers:
|
// Custom silencers:
|
||||||
"camelcase": 0,
|
"camelcase": 0,
|
||||||
"no-debugger": 0,
|
"no-debugger": 0,
|
||||||
|
"require-await": 0,
|
||||||
"no-multi-assign": 0,
|
"no-multi-assign": 0,
|
||||||
"no-underscore-dangle": 0,
|
"no-underscore-dangle": 0,
|
||||||
"one-var-declaration-per-line": 0,
|
"one-var-declaration-per-line": 0,
|
||||||
|
|
|
@ -72,9 +72,9 @@ export default {
|
||||||
* @param {module:type/mpi} data Data to be encrypted as MPI
|
* @param {module:type/mpi} data Data to be encrypted as MPI
|
||||||
* @return {Array<module:type/mpi|module:type/oid|module:type/kdf|module:type/ecdh_symkey>} encrypted session key parameters
|
* @return {Array<module:type/mpi|module:type/oid|module:type/kdf|module:type/ecdh_symkey>} encrypted session key parameters
|
||||||
*/
|
*/
|
||||||
publicKeyEncrypt: function(algo, publicParams, data, fingerprint) {
|
publicKeyEncrypt: async function(algo, publicParams, data, fingerprint) {
|
||||||
var types = this.getEncSessionKeyParamTypes(algo);
|
var types = this.getEncSessionKeyParamTypes(algo);
|
||||||
var result = (async function() {
|
return (async function() {
|
||||||
var m;
|
var m;
|
||||||
switch (algo) {
|
switch (algo) {
|
||||||
case 'rsa_encrypt':
|
case 'rsa_encrypt':
|
||||||
|
@ -107,8 +107,6 @@ export default {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}());
|
}());
|
||||||
|
|
||||||
return result;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -120,9 +118,9 @@ export default {
|
||||||
* @return {module:type/mpi} returns a big integer containing the decrypted data; otherwise null
|
* @return {module:type/mpi} returns a big integer containing the decrypted data; otherwise null
|
||||||
*/
|
*/
|
||||||
|
|
||||||
publicKeyDecrypt: function(algo, keyIntegers, dataIntegers, fingerprint) {
|
publicKeyDecrypt: async function(algo, keyIntegers, dataIntegers, fingerprint) {
|
||||||
var p;
|
var p;
|
||||||
var bn = (function() {
|
return new type_mpi(await (async function() {
|
||||||
switch (algo) {
|
switch (algo) {
|
||||||
case 'rsa_encrypt_sign':
|
case 'rsa_encrypt_sign':
|
||||||
case 'rsa_encrypt':
|
case 'rsa_encrypt':
|
||||||
|
@ -157,10 +155,7 @@ export default {
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}());
|
}()));
|
||||||
|
|
||||||
var result = new type_mpi(bn);
|
|
||||||
return result;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/** Returns the types comprising the private key of an algorithm
|
/** Returns the types comprising the private key of an algorithm
|
||||||
|
|
|
@ -105,7 +105,7 @@ async function encrypt(oid, cipher_algo, hash_algo, m, Q, fingerprint) {
|
||||||
* @param {String} fingerprint Recipient fingerprint
|
* @param {String} fingerprint Recipient fingerprint
|
||||||
* @return {Uint8Array} Value derived from session
|
* @return {Uint8Array} Value derived from session
|
||||||
*/
|
*/
|
||||||
function decrypt(oid, cipher_algo, hash_algo, V, C, d, fingerprint) {
|
async function decrypt(oid, cipher_algo, hash_algo, V, C, d, fingerprint) {
|
||||||
fingerprint = util.hex2Uint8Array(fingerprint);
|
fingerprint = util.hex2Uint8Array(fingerprint);
|
||||||
const param = buildEcdhParam(enums.publicKey.ecdh, oid, cipher_algo, hash_algo, fingerprint);
|
const param = buildEcdhParam(enums.publicKey.ecdh, oid, cipher_algo, hash_algo, fingerprint);
|
||||||
const curve = curves.get(oid);
|
const curve = curves.get(oid);
|
||||||
|
|
|
@ -64,8 +64,7 @@ export default {
|
||||||
const s = msg_MPIs[1].toBigInteger();
|
const s = msg_MPIs[1].toBigInteger();
|
||||||
m = data;
|
m = data;
|
||||||
const Q = publickey_MPIs[1].toBigInteger();
|
const Q = publickey_MPIs[1].toBigInteger();
|
||||||
const result = await ecdsa.verify(curve.oid, hash_algo, {r: r, s: s}, m, Q);
|
return ecdsa.verify(curve.oid, hash_algo, {r: r, s: s}, m, Q);
|
||||||
return result;
|
|
||||||
default:
|
default:
|
||||||
throw new Error('Invalid signature algorithm.');
|
throw new Error('Invalid signature algorithm.');
|
||||||
}
|
}
|
||||||
|
|
111
src/message.js
111
src/message.js
|
@ -92,9 +92,8 @@ Message.prototype.getSigningKeyIds = function() {
|
||||||
* @param {String} password (optional) password used to decrypt
|
* @param {String} password (optional) password used to decrypt
|
||||||
* @return {Message} new message with decrypted content
|
* @return {Message} new message with decrypted content
|
||||||
*/
|
*/
|
||||||
Message.prototype.decrypt = function(privateKey, sessionKey, password) {
|
Message.prototype.decrypt = async function(privateKey, sessionKey, password) {
|
||||||
return Promise.resolve().then(() => {
|
const keyObj = sessionKey || await this.decryptSessionKey(privateKey, password);
|
||||||
const keyObj = sessionKey || this.decryptSessionKey(privateKey, password);
|
|
||||||
if (!keyObj || !util.isUint8Array(keyObj.data) || !util.isString(keyObj.algorithm)) {
|
if (!keyObj || !util.isUint8Array(keyObj.data) || !util.isString(keyObj.algorithm)) {
|
||||||
throw new Error('Invalid session key for decryption.');
|
throw new Error('Invalid session key for decryption.');
|
||||||
}
|
}
|
||||||
|
@ -110,12 +109,11 @@ Message.prototype.decrypt = function(privateKey, sessionKey, password) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const symEncryptedPacket = symEncryptedPacketlist[0];
|
const symEncryptedPacket = symEncryptedPacketlist[0];
|
||||||
return symEncryptedPacket.decrypt(keyObj.algorithm, keyObj.data).then(() => {
|
await symEncryptedPacket.decrypt(keyObj.algorithm, keyObj.data);
|
||||||
const resultMsg = new Message(symEncryptedPacket.packets);
|
const resultMsg = new Message(symEncryptedPacket.packets);
|
||||||
symEncryptedPacket.packets = new packet.List(); // remove packets after decryption
|
symEncryptedPacket.packets = new packet.List(); // remove packets after decryption
|
||||||
|
|
||||||
return resultMsg;
|
return resultMsg;
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,26 +124,23 @@ Message.prototype.decrypt = function(privateKey, sessionKey, password) {
|
||||||
* { data:Uint8Array, algorithm:String }
|
* { data:Uint8Array, algorithm:String }
|
||||||
*/
|
*/
|
||||||
Message.prototype.decryptSessionKey = function(privateKey, password) {
|
Message.prototype.decryptSessionKey = function(privateKey, password) {
|
||||||
var keyPacket;
|
var keyPacket, results, error;
|
||||||
|
return Promise.resolve().then(async () => {
|
||||||
if (password) {
|
if (password) {
|
||||||
var symEncryptedSessionKeyPacketlist = this.packets.filterByTag(enums.packet.symEncryptedSessionKey);
|
var symESKeyPacketlist = this.packets.filterByTag(enums.packet.symEncryptedSessionKey);
|
||||||
var symLength = symEncryptedSessionKeyPacketlist.length;
|
// FIXME need a circuit breaker here
|
||||||
for (var i = 0; i < symLength; i++) {
|
if (!symESKeyPacketlist) {
|
||||||
keyPacket = symEncryptedSessionKeyPacketlist[i];
|
|
||||||
try {
|
|
||||||
keyPacket.decrypt(password);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
catch(err) {
|
|
||||||
if (i === (symLength - 1)) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!keyPacket) {
|
|
||||||
throw new Error('No symmetrically encrypted session key packet found.');
|
throw new Error('No symmetrically encrypted session key packet found.');
|
||||||
}
|
}
|
||||||
|
results = await Promise.all(symESKeyPacketlist.map(async function(packet) {
|
||||||
|
try {
|
||||||
|
await packet.decrypt(password);
|
||||||
|
return packet;
|
||||||
|
} catch (err) {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
keyPacket = results.find(result => result !== undefined);
|
||||||
|
|
||||||
} else if (privateKey) {
|
} else if (privateKey) {
|
||||||
var encryptionKeyIds = this.getEncryptionKeyIds();
|
var encryptionKeyIds = this.getEncryptionKeyIds();
|
||||||
|
@ -158,24 +153,35 @@ Message.prototype.decryptSessionKey = function(privateKey, password) {
|
||||||
throw new Error('Private key is not decrypted.');
|
throw new Error('Private key is not decrypted.');
|
||||||
}
|
}
|
||||||
var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey);
|
var pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey);
|
||||||
for (var j = 0; j < pkESKeyPacketlist.length; j++) {
|
if (!pkESKeyPacketlist) {
|
||||||
if (pkESKeyPacketlist[j].publicKeyId.equals(privateKeyPacket.getKeyId())) {
|
throw new Error('No public key encrypted session key packet found.');
|
||||||
keyPacket = pkESKeyPacketlist[j];
|
}
|
||||||
keyPacket.decrypt(privateKeyPacket);
|
// FIXME need a circuit breaker here
|
||||||
break;
|
results = await Promise.all(pkESKeyPacketlist.map(async function(packet) {
|
||||||
|
if (packet.publicKeyId.equals(privateKeyPacket.getKeyId())) {
|
||||||
|
try {
|
||||||
|
await packet.decrypt(privateKeyPacket)
|
||||||
|
return packet;
|
||||||
|
} catch (err) {
|
||||||
|
error = err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
|
keyPacket = results.find(result => result !== undefined);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error('No key or password specified.');
|
throw new Error('No key or password specified.');
|
||||||
}
|
}
|
||||||
|
}).then(() => {
|
||||||
if (keyPacket) {
|
if (keyPacket) {
|
||||||
return {
|
return {
|
||||||
data: keyPacket.sessionKey,
|
data: keyPacket.sessionKey,
|
||||||
algorithm: keyPacket.sessionKeyAlgorithm
|
algorithm: keyPacket.sessionKeyAlgorithm
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
throw new Error('Session key decryption failed.');
|
||||||
}
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -218,7 +224,15 @@ Message.prototype.getText = function() {
|
||||||
*/
|
*/
|
||||||
Message.prototype.encrypt = function(keys, passwords, sessionKey) {
|
Message.prototype.encrypt = function(keys, passwords, sessionKey) {
|
||||||
let symAlgo, msg, symEncryptedPacket;
|
let symAlgo, msg, symEncryptedPacket;
|
||||||
return Promise.resolve().then(() => {
|
return Promise.resolve().then(async () => {
|
||||||
|
if (keys) {
|
||||||
|
symAlgo = enums.read(enums.symmetric, keyModule.getPreferredSymAlgo(keys));
|
||||||
|
} else if (passwords) {
|
||||||
|
symAlgo = enums.read(enums.symmetric, config.encryption_cipher);
|
||||||
|
} else {
|
||||||
|
throw new Error('No keys or passwords');
|
||||||
|
}
|
||||||
|
|
||||||
if (sessionKey) {
|
if (sessionKey) {
|
||||||
if (!util.isUint8Array(sessionKey.data) || !util.isString(sessionKey.algorithm)) {
|
if (!util.isUint8Array(sessionKey.data) || !util.isString(sessionKey.algorithm)) {
|
||||||
throw new Error('Invalid session key for encryption.');
|
throw new Error('Invalid session key for encryption.');
|
||||||
|
@ -237,7 +251,7 @@ Message.prototype.encrypt = function(keys, passwords, sessionKey) {
|
||||||
sessionKey = crypto.generateSessionKey(symAlgo);
|
sessionKey = crypto.generateSessionKey(symAlgo);
|
||||||
}
|
}
|
||||||
|
|
||||||
msg = encryptSessionKey(sessionKey, symAlgo, keys, passwords);
|
msg = await encryptSessionKey(sessionKey, symAlgo, keys, passwords);
|
||||||
|
|
||||||
if (config.aead_protect) {
|
if (config.aead_protect) {
|
||||||
symEncryptedPacket = new packet.SymEncryptedAEADProtected();
|
symEncryptedPacket = new packet.SymEncryptedAEADProtected();
|
||||||
|
@ -272,38 +286,41 @@ Message.prototype.encrypt = function(keys, passwords, sessionKey) {
|
||||||
* @return {Message} new message with encrypted content
|
* @return {Message} new message with encrypted content
|
||||||
*/
|
*/
|
||||||
export function encryptSessionKey(sessionKey, symAlgo, publicKeys, passwords) {
|
export function encryptSessionKey(sessionKey, symAlgo, publicKeys, passwords) {
|
||||||
var packetlist = new packet.List();
|
var results, packetlist = new packet.List();
|
||||||
|
|
||||||
|
return Promise.resolve().then(async () => {
|
||||||
if (publicKeys) {
|
if (publicKeys) {
|
||||||
publicKeys.forEach(function(key) {
|
results = await Promise.all(publicKeys.map(async function(key) {
|
||||||
var encryptionKeyPacket = key.getEncryptionKeyPacket();
|
var encryptionKeyPacket = key.getEncryptionKeyPacket();
|
||||||
if (encryptionKeyPacket) {
|
if (!encryptionKeyPacket) {
|
||||||
|
throw new Error('Could not find valid key packet for encryption in key ' + key.primaryKey.getKeyId().toHex());
|
||||||
|
}
|
||||||
var pkESKeyPacket = new packet.PublicKeyEncryptedSessionKey();
|
var pkESKeyPacket = new packet.PublicKeyEncryptedSessionKey();
|
||||||
pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId();
|
pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId();
|
||||||
pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm;
|
pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm;
|
||||||
pkESKeyPacket.sessionKey = sessionKey;
|
pkESKeyPacket.sessionKey = sessionKey;
|
||||||
pkESKeyPacket.sessionKeyAlgorithm = symAlgo;
|
pkESKeyPacket.sessionKeyAlgorithm = symAlgo;
|
||||||
pkESKeyPacket.encrypt(encryptionKeyPacket);
|
await pkESKeyPacket.encrypt(encryptionKeyPacket);
|
||||||
delete pkESKeyPacket.sessionKey; // delete plaintext session key after encryption
|
delete pkESKeyPacket.sessionKey; // delete plaintext session key after encryption
|
||||||
packetlist.push(pkESKeyPacket);
|
return pkESKeyPacket;
|
||||||
} else {
|
}));
|
||||||
throw new Error('Could not find valid key packet for encryption in key ' + key.primaryKey.getKeyId().toHex());
|
packetlist.concat(results);
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (passwords) {
|
if (passwords) {
|
||||||
passwords.forEach(function(password) {
|
results = await Promise.all(passwords.map(async function(password) {
|
||||||
var symEncryptedSessionKeyPacket = new packet.SymEncryptedSessionKey();
|
var symEncryptedSessionKeyPacket = new packet.SymEncryptedSessionKey();
|
||||||
symEncryptedSessionKeyPacket.sessionKey = sessionKey;
|
symEncryptedSessionKeyPacket.sessionKey = sessionKey;
|
||||||
symEncryptedSessionKeyPacket.sessionKeyAlgorithm = symAlgo;
|
symEncryptedSessionKeyPacket.sessionKeyAlgorithm = symAlgo;
|
||||||
symEncryptedSessionKeyPacket.encrypt(password);
|
await symEncryptedSessionKeyPacket.encrypt(password);
|
||||||
delete symEncryptedSessionKeyPacket.sessionKey; // delete plaintext session key after encryption
|
delete symEncryptedSessionKeyPacket.sessionKey; // delete plaintext session key after encryption
|
||||||
packetlist.push(symEncryptedSessionKeyPacket);
|
return symEncryptedSessionKeyPacket;
|
||||||
});
|
}));
|
||||||
|
packetlist.concat(results);
|
||||||
}
|
}
|
||||||
|
}).then(() => {
|
||||||
return new Message(packetlist);
|
return new Message(packetlist);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -365,13 +382,11 @@ Message.prototype.sign = async function(privateKeys=[], signature=null) {
|
||||||
|
|
||||||
packetlist.push(literalDataPacket);
|
packetlist.push(literalDataPacket);
|
||||||
|
|
||||||
// FIXME does the order matter here? It used to be n-1..0
|
await Promise.all(privateKeys.reverse().map(async function(privateKey) {
|
||||||
await Promise.all(privateKeys.map(async function(privateKey) {
|
|
||||||
var signingKeyPacket = privateKey.getSigningKeyPacket();
|
var signingKeyPacket = privateKey.getSigningKeyPacket();
|
||||||
var signaturePacket = new packet.Signature();
|
var signaturePacket = new packet.Signature();
|
||||||
signaturePacket.signatureType = signatureType;
|
signaturePacket.signatureType = signatureType;
|
||||||
signaturePacket.hashAlgorithm = config.prefer_hash_algorithm;
|
signaturePacket.hashAlgorithm = config.prefer_hash_algorithm;
|
||||||
// FIXME FIXME were we just signing with the last key?
|
|
||||||
signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
|
signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
|
||||||
if (!signingKeyPacket.isDecrypted) {
|
if (!signingKeyPacket.isDecrypted) {
|
||||||
throw new Error('Private key is not decrypted.');
|
throw new Error('Private key is not decrypted.');
|
||||||
|
|
|
@ -394,10 +394,8 @@ export function encryptSessionKey({ data, algorithm, publicKeys, passwords }) {
|
||||||
return asyncProxy.delegate('encryptSessionKey', { data, algorithm, publicKeys, passwords });
|
return asyncProxy.delegate('encryptSessionKey', { data, algorithm, publicKeys, passwords });
|
||||||
}
|
}
|
||||||
|
|
||||||
return execute(() => ({
|
return execute(async () => ({
|
||||||
|
message: await messageLib.encryptSessionKey(data, algorithm, publicKeys, passwords)
|
||||||
message: messageLib.encryptSessionKey(data, algorithm, publicKeys, passwords)
|
|
||||||
|
|
||||||
}), 'Error encrypting session key');
|
}), 'Error encrypting session key');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -149,6 +149,20 @@ Packetlist.prototype.forEach = function (callback) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array containing return values of callback
|
||||||
|
* on each element
|
||||||
|
*/
|
||||||
|
Packetlist.prototype.map = function (callback) {
|
||||||
|
var packetArray = [];
|
||||||
|
|
||||||
|
for (var i = 0; i < this.length; i++) {
|
||||||
|
packetArray.push(callback(this[i], i, this));
|
||||||
|
}
|
||||||
|
|
||||||
|
return packetArray;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Traverses packet tree and returns first matching packet
|
* Traverses packet tree and returns first matching packet
|
||||||
* @param {module:enums.packet} type The packet type
|
* @param {module:enums.packet} type The packet type
|
||||||
|
|
|
@ -132,12 +132,12 @@ PublicKeyEncryptedSessionKey.prototype.encrypt = async function (key) {
|
||||||
* Private key with secMPIs unlocked
|
* Private key with secMPIs unlocked
|
||||||
* @return {String} The unencrypted session key
|
* @return {String} The unencrypted session key
|
||||||
*/
|
*/
|
||||||
PublicKeyEncryptedSessionKey.prototype.decrypt = function (key) {
|
PublicKeyEncryptedSessionKey.prototype.decrypt = async function (key) {
|
||||||
var result = crypto.publicKeyDecrypt(
|
var result = (await crypto.publicKeyDecrypt(
|
||||||
this.publicKeyAlgorithm,
|
this.publicKeyAlgorithm,
|
||||||
key.params,
|
key.params,
|
||||||
this.encrypted,
|
this.encrypted,
|
||||||
key.fingerprint).toBytes();
|
key.fingerprint)).toBytes();
|
||||||
|
|
||||||
var checksum;
|
var checksum;
|
||||||
var decoded;
|
var decoded;
|
||||||
|
@ -155,8 +155,7 @@ PublicKeyEncryptedSessionKey.prototype.decrypt = function (key) {
|
||||||
throw new Error('Checksum mismatch');
|
throw new Error('Checksum mismatch');
|
||||||
} else {
|
} else {
|
||||||
this.sessionKey = key;
|
this.sessionKey = key;
|
||||||
this.sessionKeyAlgorithm =
|
this.sessionKeyAlgorithm = enums.read(enums.symmetric, decoded.charCodeAt(0));
|
||||||
enums.read(enums.symmetric, decoded.charCodeAt(0));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -133,7 +133,7 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm
|
||||||
this.packets.read(decrypted.subarray(0, decrypted.length - 22));
|
this.packets.read(decrypted.subarray(0, decrypted.length - 22));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve();
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -122,9 +122,7 @@ SymEncryptedSessionKey.prototype.decrypt = function(passphrase) {
|
||||||
var decrypted = crypto.cfb.normalDecrypt(
|
var decrypted = crypto.cfb.normalDecrypt(
|
||||||
algo, key, this.encrypted, null);
|
algo, key, this.encrypted, null);
|
||||||
|
|
||||||
this.sessionKeyAlgorithm = enums.read(enums.symmetric,
|
this.sessionKeyAlgorithm = enums.read(enums.symmetric, decrypted[0]);
|
||||||
decrypted[0]);
|
|
||||||
|
|
||||||
this.sessionKey = decrypted.subarray(1,decrypted.length);
|
this.sessionKey = decrypted.subarray(1,decrypted.length);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -147,8 +145,7 @@ SymEncryptedSessionKey.prototype.encrypt = function(passphrase) {
|
||||||
}
|
}
|
||||||
private_key = util.concatUint8Array([algo_enum, this.sessionKey]);
|
private_key = util.concatUint8Array([algo_enum, this.sessionKey]);
|
||||||
|
|
||||||
this.encrypted = crypto.cfb.normalEncrypt(
|
this.encrypted = crypto.cfb.normalEncrypt(algo, key, private_key, null);
|
||||||
algo, key, private_key, null);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -372,7 +372,8 @@ describe('API functional testing', function() {
|
||||||
"rsa_encrypt_sign", RSApubMPIs, RSAUnencryptedData
|
"rsa_encrypt_sign", RSApubMPIs, RSAUnencryptedData
|
||||||
).then(RSAEncryptedData => {
|
).then(RSAEncryptedData => {
|
||||||
|
|
||||||
var data = openpgp.crypto.publicKeyDecrypt("rsa_encrypt_sign", RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData).write();
|
openpgp.crypto.publicKeyDecrypt("rsa_encrypt_sign", RSApubMPIs.concat(RSAsecMPIs), RSAEncryptedData).then(data => {
|
||||||
|
data = data.write();
|
||||||
data = util.Uint8Array2str(data.subarray(2, data.length));
|
data = util.Uint8Array2str(data.subarray(2, data.length));
|
||||||
|
|
||||||
var result = openpgp.crypto.pkcs1.eme.decode(data, RSApubMPIs[0].byteLength());
|
var result = openpgp.crypto.pkcs1.eme.decode(data, RSApubMPIs[0].byteLength());
|
||||||
|
@ -380,6 +381,7 @@ describe('API functional testing', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Asymmetric using Elgamal with eme_pkcs1 padding', function (done) {
|
it('Asymmetric using Elgamal with eme_pkcs1 padding', function (done) {
|
||||||
var symmKey = util.Uint8Array2str(openpgp.crypto.generateSessionKey('aes256'));
|
var symmKey = util.Uint8Array2str(openpgp.crypto.generateSessionKey('aes256'));
|
||||||
|
@ -389,7 +391,8 @@ describe('API functional testing', function() {
|
||||||
"elgamal", ElgamalpubMPIs, ElgamalUnencryptedData
|
"elgamal", ElgamalpubMPIs, ElgamalUnencryptedData
|
||||||
).then(ElgamalEncryptedData => {
|
).then(ElgamalEncryptedData => {
|
||||||
|
|
||||||
var data = openpgp.crypto.publicKeyDecrypt("elgamal", ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData).write();
|
var data = openpgp.crypto.publicKeyDecrypt("elgamal", ElgamalpubMPIs.concat(ElgamalsecMPIs), ElgamalEncryptedData).then(data => {
|
||||||
|
data = data.write();
|
||||||
data = util.Uint8Array2str(data.subarray(2, data.length));
|
data = util.Uint8Array2str(data.subarray(2, data.length));
|
||||||
|
|
||||||
var result = openpgp.crypto.pkcs1.eme.decode(data, ElgamalpubMPIs[0].byteLength());
|
var result = openpgp.crypto.pkcs1.eme.decode(data, ElgamalpubMPIs[0].byteLength());
|
||||||
|
@ -399,3 +402,4 @@ describe('API functional testing', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -198,13 +198,14 @@ describe("Packet", function() {
|
||||||
|
|
||||||
msg2.read(msg.write());
|
msg2.read(msg.write());
|
||||||
|
|
||||||
msg2[0].decrypt({ params: mpi });
|
msg2[0].decrypt({ params: mpi }).then(() => {
|
||||||
|
|
||||||
expect(stringify(msg2[0].sessionKey)).to.equal(stringify(enc.sessionKey));
|
expect(stringify(msg2[0].sessionKey)).to.equal(stringify(enc.sessionKey));
|
||||||
expect(msg2[0].sessionKeyAlgorithm).to.equal(enc.sessionKeyAlgorithm);
|
expect(msg2[0].sessionKeyAlgorithm).to.equal(enc.sessionKeyAlgorithm);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Secret key packet (reading, unencrypted)', function(done) {
|
it('Secret key packet (reading, unencrypted)', function(done) {
|
||||||
var armored_key =
|
var armored_key =
|
||||||
|
@ -243,12 +244,13 @@ describe("Packet", function() {
|
||||||
|
|
||||||
enc.encrypt(key).then(() => {
|
enc.encrypt(key).then(() => {
|
||||||
|
|
||||||
enc.decrypt(key);
|
enc.decrypt(key).then(() => {
|
||||||
|
|
||||||
expect(stringify(enc.sessionKey)).to.equal(stringify(secret));
|
expect(stringify(enc.sessionKey)).to.equal(stringify(secret));
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Public key encrypted packet (reading, GPG)', function(done) {
|
it('Public key encrypted packet (reading, GPG)', function(done) {
|
||||||
var armored_key =
|
var armored_key =
|
||||||
|
@ -305,7 +307,7 @@ describe("Packet", function() {
|
||||||
var msg = new openpgp.packet.List();
|
var msg = new openpgp.packet.List();
|
||||||
msg.read(openpgp.armor.decode(armored_msg).data);
|
msg.read(openpgp.armor.decode(armored_msg).data);
|
||||||
|
|
||||||
msg[0].decrypt(key);
|
msg[0].decrypt(key).then(() => {
|
||||||
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
||||||
|
|
||||||
var text = stringify(msg[1].packets[0].packets[0].data);
|
var text = stringify(msg[1].packets[0].packets[0].data);
|
||||||
|
@ -313,6 +315,7 @@ describe("Packet", function() {
|
||||||
expect(text).to.equal('Hello world!');
|
expect(text).to.equal('Hello world!');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Sym encrypted session key reading/writing', function(done) {
|
it('Sym encrypted session key reading/writing', function(done) {
|
||||||
var passphrase = 'hello',
|
var passphrase = 'hello',
|
||||||
|
@ -335,7 +338,6 @@ describe("Packet", function() {
|
||||||
enc.packets.push(literal);
|
enc.packets.push(literal);
|
||||||
enc.encrypt(algo, key);
|
enc.encrypt(algo, key);
|
||||||
|
|
||||||
|
|
||||||
var msg2 = new openpgp.packet.List();
|
var msg2 = new openpgp.packet.List();
|
||||||
msg2.read(msg.write());
|
msg2.read(msg.write());
|
||||||
|
|
||||||
|
@ -368,7 +370,7 @@ describe("Packet", function() {
|
||||||
var msg = new openpgp.packet.List();
|
var msg = new openpgp.packet.List();
|
||||||
msg.read(openpgp.armor.decode(armored_msg).data);
|
msg.read(openpgp.armor.decode(armored_msg).data);
|
||||||
|
|
||||||
msg[0].decrypt(key);
|
msg[0].decrypt(key).then(() => {
|
||||||
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
||||||
|
|
||||||
var text = stringify(msg[1].packets[0].packets[0].data);
|
var text = stringify(msg[1].packets[0].packets[0].data);
|
||||||
|
@ -376,6 +378,7 @@ describe("Packet", function() {
|
||||||
expect(text).to.equal('Hello world!');
|
expect(text).to.equal('Hello world!');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Secret key reading with signature verification.', function() {
|
it('Secret key reading with signature verification.', function() {
|
||||||
var key = new openpgp.packet.List();
|
var key = new openpgp.packet.List();
|
||||||
|
@ -418,7 +421,7 @@ describe("Packet", function() {
|
||||||
var msg = new openpgp.packet.List();
|
var msg = new openpgp.packet.List();
|
||||||
msg.read(openpgp.armor.decode(armored_msg).data);
|
msg.read(openpgp.armor.decode(armored_msg).data);
|
||||||
|
|
||||||
msg[0].decrypt(key[3]);
|
msg[0].decrypt(key[3]).then(() => {
|
||||||
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
msg[1].decrypt(msg[0].sessionKeyAlgorithm, msg[0].sessionKey);
|
||||||
|
|
||||||
var payload = msg[1].packets[0].packets;
|
var payload = msg[1].packets[0].packets;
|
||||||
|
@ -427,6 +430,7 @@ describe("Packet", function() {
|
||||||
key[0], payload[1]
|
key[0], payload[1]
|
||||||
)).to.eventually.be.true.notify(done);
|
)).to.eventually.be.true.notify(done);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Writing and encryption of a secret key packet.', function() {
|
it('Writing and encryption of a secret key packet.', function() {
|
||||||
var key = new openpgp.packet.List();
|
var key = new openpgp.packet.List();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user