Also PGP sign with private keys that have no passphrase #429

This commit is contained in:
the-djmaze 2022-08-08 09:53:19 +02:00
parent 7a85a75ca9
commit 977868c1fa

View file

@ -18,9 +18,22 @@ const
key.emails.includes(query) || query == key.id || query == key.fingerprint key.emails.includes(query) || query == key.id || query == key.fingerprint
), ),
askPassphrase = async (privateKey, btnTxt = 'LABEL_SIGN') => askPassphrase = async (privateKey, btnTxt) =>
await AskPopupView.password('OpenPGP.js key<br>' + privateKey.id + ' ' + privateKey.emails[0], 'OPENPGP/'+btnTxt), await AskPopupView.password('OpenPGP.js key<br>' + privateKey.id + ' ' + privateKey.emails[0], 'OPENPGP/'+btnTxt),
decryptKey = async (privateKey, btnTxt = 'LABEL_SIGN') => {
if (privateKey.key.isDecrypted()) {
return privateKey.key;
}
const passphrase = await askPassphrase(privateKey, btnTxt);
if (null !== passphrase) {
return await openpgp.decryptKey({
privateKey: privateKey.key,
passphrase
});
}
},
/** /**
* OpenPGP.js v5 removed the localStorage (keyring) * OpenPGP.js v5 removed the localStorage (keyring)
* This should be compatible with the old OpenPGP.js v2 * This should be compatible with the old OpenPGP.js v2
@ -178,18 +191,7 @@ export const OpenPGPUserStore = new class {
} }
} }
if (privateKey) try { if (privateKey) try {
let decryptedKey; const decryptedKey = await decryptKey(privateKey, 'BUTTON_DECRYPT');
if (privateKey.key.isDecrypted()) {
decryptedKey = privateKey;
} else {
const passphrase = await askPassphrase(privateKey, 'BUTTON_DECRYPT');
if (null !== passphrase) {
decryptedKey = await openpgp.decryptKey({
privateKey: privateKey.key,
passphrase
});
}
}
if (decryptedKey) { if (decryptedKey) {
const publicKey = findOpenPGPKey(this.publicKeys, sender/*, sign*/); const publicKey = findOpenPGPKey(this.publicKeys, sender/*, sign*/);
return await openpgp.decrypt({ return await openpgp.decrypt({
@ -250,18 +252,14 @@ export const OpenPGPUserStore = new class {
* https://docs.openpgpjs.org/global.html#sign * https://docs.openpgpjs.org/global.html#sign
*/ */
async sign(text, privateKey, detached) { async sign(text, privateKey, detached) {
const passphrase = await askPassphrase(privateKey); const signingKey = await decryptKey(privateKey);
if (null !== passphrase) { if (signingKey) {
privateKey = await openpgp.decryptKey({
privateKey: privateKey.key,
passphrase
});
const message = detached const message = detached
? await openpgp.createMessage({ text: text }) ? await openpgp.createMessage({ text: text })
: await openpgp.createCleartextMessage({ text: text }); : await openpgp.createCleartextMessage({ text: text });
return await openpgp.sign({ return await openpgp.sign({
message: message, message: message,
signingKeys: privateKey, signingKeys: signingKey,
detached: !!detached detached: !!detached
}); });
} }
@ -276,14 +274,10 @@ export const OpenPGPUserStore = new class {
recipients = recipients.map(email => this.publicKeys().find(key => key.emails.includes(email))).filter(key => key); recipients = recipients.map(email => this.publicKeys().find(key => key.emails.includes(email))).filter(key => key);
if (count === recipients.length) { if (count === recipients.length) {
if (signPrivateKey) { if (signPrivateKey) {
const passphrase = await askPassphrase(signPrivateKey); signPrivateKey = await decryptKey(signPrivateKey);
if (null === passphrase) { if (!signPrivateKey) {
return; return;
} }
signPrivateKey = await openpgp.decryptKey({
privateKey: signPrivateKey.key,
passphrase
});
} }
return await openpgp.encrypt({ return await openpgp.encrypt({
message: await openpgp.createMessage({ text: text }), message: await openpgp.createMessage({ text: text }),