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