mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-06 22:47:11 -04:00
Clean up really bad promise code in PGP ops
This commit is contained in:
parent
f1be229b87
commit
057fb20ea4
1 changed files with 344 additions and 367 deletions
|
@ -42,11 +42,10 @@ var PGP = {
|
|||
runEncrypt: function (plaintext, args) {
|
||||
var publicKey = args[0];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var publicKeys = openpgp.key.readArmored(publicKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Cannot read public key: " + err);
|
||||
throw "Cannot read public key: " + err;
|
||||
}
|
||||
|
||||
var options = {
|
||||
|
@ -54,13 +53,12 @@ var PGP = {
|
|||
publicKeys: publicKeys,
|
||||
};
|
||||
|
||||
openpgp.encrypt(options)
|
||||
return openpgp.encrypt(options)
|
||||
.then(function(ciphertext) {
|
||||
resolve(ciphertext.data);
|
||||
return ciphertext.data;
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not encrypt text: " + err);
|
||||
});
|
||||
throw "Could not encrypt text: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -76,17 +74,16 @@ var PGP = {
|
|||
var privateKey = args[0],
|
||||
password = args[1];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
privateKey = openpgp.key.readArmored(privateKey).keys[0];
|
||||
} catch (err) {
|
||||
return reject("Cannot read private key: " + err);
|
||||
throw "Cannot read private key: " + err;
|
||||
}
|
||||
|
||||
try {
|
||||
var message = openpgp.message.readArmored(input);
|
||||
} catch (err) {
|
||||
return reject("Cannot read message: " + err);
|
||||
throw "Cannot read message: " + err;
|
||||
}
|
||||
|
||||
var options = {
|
||||
|
@ -98,16 +95,15 @@ var PGP = {
|
|||
privateKey.decrypt(password);
|
||||
}
|
||||
if (privateKey.primaryKey.encrypted !== null) {
|
||||
return reject("Could not decrypt private key.");
|
||||
throw "Could not decrypt private key.";
|
||||
}
|
||||
|
||||
openpgp.decrypt(options)
|
||||
return openpgp.decrypt(options)
|
||||
.then(function(plaintext) {
|
||||
resolve(plaintext.data);
|
||||
return plaintext.data;
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not decrypt message: " + err);
|
||||
});
|
||||
throw "Could not decrypt message: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -124,24 +120,23 @@ var PGP = {
|
|||
privateKey = args[1],
|
||||
password = args[2];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var publicKeys = openpgp.key.readArmored(publicKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read public key: " + err);
|
||||
throw "Could not read public key: " + err;
|
||||
}
|
||||
|
||||
try {
|
||||
var privateKeys = openpgp.key.readArmored(privateKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read private key: " + err);
|
||||
throw "Could not read private key: " + err;
|
||||
}
|
||||
|
||||
if (password) {
|
||||
privateKeys[0].decrypt(password);
|
||||
}
|
||||
if (privateKeys[0].primaryKey.encrypted !== null) {
|
||||
return reject("Could not decrypt private key.");
|
||||
throw "Could not decrypt private key.";
|
||||
}
|
||||
|
||||
var options = {
|
||||
|
@ -150,13 +145,12 @@ var PGP = {
|
|||
privateKeys: privateKeys,
|
||||
};
|
||||
|
||||
openpgp.encrypt(options)
|
||||
return openpgp.encrypt(options)
|
||||
.then(function(signedData) {
|
||||
resolve(signedData.data);
|
||||
return signedData.data;
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not sign input: " + err);
|
||||
});
|
||||
throw "Could not sign input: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -173,30 +167,29 @@ var PGP = {
|
|||
privateKey = args[1],
|
||||
password = args[2];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var publicKeys = openpgp.key.readArmored(publicKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read public key: " + err);
|
||||
throw "Could not read public key: " + err;
|
||||
}
|
||||
|
||||
try {
|
||||
var privateKeys = openpgp.key.readArmored(privateKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read private key: " + err);
|
||||
throw "Could not read private key: " + err;
|
||||
}
|
||||
|
||||
if (password) {
|
||||
privateKeys[0].decrypt(password);
|
||||
}
|
||||
if (privateKeys[0].primaryKey.encrypted !== null) {
|
||||
return reject("Could not decrypt private key.");
|
||||
throw "Could not decrypt private key.";
|
||||
}
|
||||
|
||||
try {
|
||||
var message = openpgp.message.readArmored(input);
|
||||
} catch (err) {
|
||||
return reject("Could not read encrypted message: " + err);
|
||||
throw "Could not read encrypted message: " + err;
|
||||
}
|
||||
|
||||
var packetContainingAlgorithms = message.packets.filterByTag(
|
||||
|
@ -213,7 +206,7 @@ var PGP = {
|
|||
message: "",
|
||||
};
|
||||
|
||||
openpgp.decrypt({
|
||||
return openpgp.decrypt({
|
||||
message: message,
|
||||
publicKeys: publicKeys,
|
||||
privateKey: privateKeys[0],
|
||||
|
@ -225,7 +218,7 @@ var PGP = {
|
|||
verification.keyID = decrypted.signatures[0].keyid.toHex();
|
||||
}
|
||||
|
||||
resolve([
|
||||
return [
|
||||
"Verified: " + verification.verified,
|
||||
"Key ID: " + verification.keyID,
|
||||
"Encrypted for: " + verification.recipient,
|
||||
|
@ -236,12 +229,11 @@ var PGP = {
|
|||
verification.sessionAlgorithm,
|
||||
"\n",
|
||||
decrypted.data,
|
||||
].join("\n"));
|
||||
].join("\n");
|
||||
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not decrypt and verify message: " + err);
|
||||
});
|
||||
throw "Could not decrypt and verify message: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -257,18 +249,17 @@ var PGP = {
|
|||
var privateKey = args[0],
|
||||
password = args[1];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var privateKeys = openpgp.key.readArmored(privateKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read private key: " + err);
|
||||
throw "Could not read private key: " + err;
|
||||
}
|
||||
|
||||
if (password) {
|
||||
privateKeys[0].decrypt(password);
|
||||
}
|
||||
if (privateKeys[0].primaryKey.encrypted !== null) {
|
||||
return reject("Could not decrypt private key.");
|
||||
throw "Could not decrypt private key.";
|
||||
}
|
||||
|
||||
var bytes = openpgp.util.str2Uint8Array(input);
|
||||
|
@ -308,8 +299,7 @@ var PGP = {
|
|||
bytes: rawSignatureBytes,
|
||||
}];
|
||||
|
||||
resolve(Utils.displayFilesAsHTML(files));
|
||||
});
|
||||
return Utils.displayFilesAsHTML(files);
|
||||
},
|
||||
|
||||
|
||||
|
@ -324,11 +314,10 @@ var PGP = {
|
|||
var publicKey = args[0],
|
||||
armouredSignature = args[1];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var publicKeys = openpgp.key.readArmored(publicKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read public key: " + err);
|
||||
throw "Could not read public key: " + err;
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -337,7 +326,7 @@ var PGP = {
|
|||
armouredSignature
|
||||
);
|
||||
} catch (err) {
|
||||
return reject("Could not read armoured signature or message: " + err);
|
||||
throw "Could not read armoured signature or message: " + err;
|
||||
}
|
||||
|
||||
var packetContainingSignature = message.packets.filterByTag(
|
||||
|
@ -353,7 +342,7 @@ var PGP = {
|
|||
message: "",
|
||||
};
|
||||
|
||||
Promise.resolve(message.verify(publicKeys))
|
||||
return Promise.resolve(message.verify(publicKeys))
|
||||
.then(function(signatures) {
|
||||
|
||||
if (signatures && signatures.length) {
|
||||
|
@ -361,7 +350,7 @@ var PGP = {
|
|||
verification.keyID = signatures[0].keyid.toHex();
|
||||
}
|
||||
|
||||
resolve([
|
||||
return [
|
||||
"Verified: " + verification.verified,
|
||||
"Key ID: " + verification.keyID,
|
||||
"Signed on: " + verification.date,
|
||||
|
@ -369,12 +358,11 @@ var PGP = {
|
|||
"Signed with: " + verification.pkAlgorithm,
|
||||
"\n",
|
||||
input,
|
||||
].join("\n"));
|
||||
].join("\n");
|
||||
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not verify message: " + err);
|
||||
});
|
||||
throw "Could not verify message: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -390,19 +378,17 @@ var PGP = {
|
|||
var privateKey = args[0],
|
||||
password = args[1];
|
||||
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var privateKeys = openpgp.key.readArmored(privateKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read private key: " + err);
|
||||
throw "Could not read private key: " + err;
|
||||
}
|
||||
|
||||
if (password) {
|
||||
privateKeys[0].decrypt(password);
|
||||
}
|
||||
if (privateKeys[0].primaryKey.encrypted !== null) {
|
||||
return reject("Could not decrypt private key.");
|
||||
throw "Could not decrypt private key.";
|
||||
}
|
||||
|
||||
var options = {
|
||||
|
@ -410,13 +396,12 @@ var PGP = {
|
|||
privateKeys: privateKeys,
|
||||
};
|
||||
|
||||
openpgp.sign(options)
|
||||
return openpgp.sign(options)
|
||||
.then(function(signedData) {
|
||||
resolve(signedData.data);
|
||||
return signedData.data;
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not clearsign input: " + err);
|
||||
});
|
||||
throw "Could not clearsign input: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -431,18 +416,16 @@ var PGP = {
|
|||
runVerifyCleartext: function (input, args) {
|
||||
var publicKey = args[0];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
|
||||
try {
|
||||
var publicKeys = openpgp.key.readArmored(publicKey).keys;
|
||||
} catch (err) {
|
||||
return reject("Could not read public key: " + err);
|
||||
throw "Could not read public key: " + err;
|
||||
}
|
||||
|
||||
try {
|
||||
var message = openpgp.cleartext.readArmored(input);
|
||||
} catch (err) {
|
||||
return reject("Could not read input message: " + err);
|
||||
throw "Could not read input message: " + err;
|
||||
}
|
||||
|
||||
var packetContainingSignature = message.packets.filterByTag(
|
||||
|
@ -458,19 +441,18 @@ var PGP = {
|
|||
message: message.text,
|
||||
};
|
||||
|
||||
openpgp.verify({
|
||||
return openpgp.verify({
|
||||
message: message,
|
||||
publicKeys: publicKeys,
|
||||
})
|
||||
.then(function(verifiedData) {
|
||||
|
||||
if (verifiedData.signatures) {
|
||||
// valid is either true or null, casting required.
|
||||
verification.verified = !!verifiedData.signatures[0].valid;
|
||||
verification.keyID = verifiedData.signatures[0].keyid.toHex();
|
||||
}
|
||||
|
||||
resolve([
|
||||
return [
|
||||
"Verified: " + verification.verified,
|
||||
"Key ID: " + verification.keyID,
|
||||
"Signed on: " + verification.date,
|
||||
|
@ -478,11 +460,10 @@ var PGP = {
|
|||
"Signed with: " + verification.pkAlgorithm,
|
||||
"\n",
|
||||
verification.message,
|
||||
].join("\n"));
|
||||
].join("\n");
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not verify message: " + err);
|
||||
});
|
||||
throw "Could not verify message: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -500,7 +481,6 @@ var PGP = {
|
|||
name = args[2],
|
||||
email = args[3];
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
var options = {
|
||||
numBits: keySize,
|
||||
userIds: [{name: name, email: email}],
|
||||
|
@ -510,17 +490,16 @@ var PGP = {
|
|||
options.passphrase = password;
|
||||
}
|
||||
|
||||
openpgp.generateKey(options)
|
||||
throw openpgp.generateKey(options)
|
||||
.then(function(key) {
|
||||
var output = [
|
||||
key.publicKeyArmored,
|
||||
key.privateKeyArmored,
|
||||
].join(""); // Preceding and trailing newlines are already generated.
|
||||
resolve(output);
|
||||
return output;
|
||||
})
|
||||
.catch(function(err) {
|
||||
reject("Could not generate key pair: " + err);
|
||||
});
|
||||
throw "Could not generate key pair: " + err;
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -533,11 +512,10 @@ var PGP = {
|
|||
* @returns {HTML} - HTML file display of message, armoured signature, and bytes signature
|
||||
*/
|
||||
runDetachClearsig: function (input, args) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
try {
|
||||
var message = openpgp.cleartext.readArmored(input);
|
||||
} catch (err) {
|
||||
return reject("Could not read input message: " + err);
|
||||
throw "Could not read input message: " + err;
|
||||
}
|
||||
|
||||
var cleartext = message.getText();
|
||||
|
@ -576,8 +554,7 @@ var PGP = {
|
|||
bytes: rawSignatureBytes,
|
||||
}];
|
||||
|
||||
resolve(Utils.displayFilesAsHTML(files));
|
||||
});
|
||||
return Utils.displayFilesAsHTML(files);
|
||||
},
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue