#89 Use OpenPGP.js v5.1.0 (still in progress)

This commit is contained in:
the-djmaze 2022-01-27 16:00:52 +01:00
parent dfd255545a
commit ff7e41ad08
130 changed files with 1871 additions and 44411 deletions

View file

@ -279,9 +279,9 @@ class ComposePopupView extends AbstractViewPopup {
currentIdentity: value => {
this.canPgpSign(false);
value && PgpUserStore.hasKeyForSigning(value.email()).then(result => {
value && PgpUserStore.getKeyForSigning(value.email()).then(result => {
console.log({canPgpSign:result});
this.canPgpSign(result)
this.canPgpSign(!!result)
});
},
@ -458,11 +458,10 @@ class ComposePopupView extends AbstractViewPopup {
if ('openpgp' == sign) {
let privateKey;
try {
const keys = PgpUserStore.openpgpKeyring.privateKeys.getForAddress(this.currentIdentity().email());
const keys = PgpUserStore.getOpenPGPPrivateKeyFor(this.currentIdentity().email());
if (keys[0]) {
keys[0].decrypt(window.prompt('Password', ''));
privateKey = keys[0];
cfg.privateKeys = [privateKey];
cfg.privateKey = privateKey = keys[0];
}
} catch (e) {
console.error(e);
@ -478,7 +477,7 @@ class ComposePopupView extends AbstractViewPopup {
// error 'sign and encrypt must be same engine';
} else if ('openpgp' == encrypt) {
this.allRecipients().forEach(recEmail => {
cfg.publicKeys = cfg.publicKeys.concat(PgpUserStore.openpgpKeyring.publicKeys.getForAddress(recEmail));
cfg.publicKeys = cfg.publicKeys.concat(PgpUserStore.getOpenPGPPublicKeyFor(recEmail));
});
pgpPromise = openpgp.encrypt(cfg);
} else if ('openpgp' == sign) {
@ -1502,7 +1501,8 @@ class ComposePopupView extends AbstractViewPopup {
allRecipients() {
const email = new EmailModel();
return [
// this.currentIdentity.email(),
// From/sender is also recipient (Sent mailbox)
this.currentIdentity().email(),
this.to(),
this.cc(),
this.bcc()

View file

@ -1,121 +0,0 @@
import ko from 'ko';
import { pString } from 'Common/Utils';
import { Scope } from 'Common/Enums';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
class MessageOpenPgpPopupView extends AbstractViewPopup {
constructor() {
super('MessageOpenPgp');
this.addObservables({
notification: '',
selectedKey: null,
password: '',
submitRequest: false
});
this.privateKeys = ko.observableArray();
this.resultCallback = null;
decorateKoCommands(this, {
doCommand: self => !self.submitRequest()
});
}
doCommand() {
this.submitRequest(true);
setTimeout(() => {
let privateKey = null;
try {
if (this.resultCallback && this.selectedKey()) {
const privateKeys = this.selectedKey().getNativeKeys();
privateKey = privateKeys && privateKeys[0] ? privateKeys[0] : null;
if (privateKey) {
try {
if (!privateKey.decrypt(pString(this.password()))) {
console.log('Error: Private key cannot be decrypted');
privateKey = null;
}
} catch (e) {
console.log(e);
privateKey = null;
}
} else {
console.log('Error: Private key cannot be found');
}
}
} catch (e) {
console.log(e);
privateKey = null;
}
this.submitRequest(false);
this.cancelCommand();
this.resultCallback(privateKey);
}, 100);
}
clearPopup() {
this.notification('');
this.password('');
this.selectedKey(false);
this.submitRequest(false);
this.resultCallback = null;
this.privateKeys([]);
}
onBuild(oDom) {
// shortcuts.add('tab', 'shift', Scope.MessageOpenPgp, () => {
shortcuts.add('tab', '', Scope.MessageOpenPgp, () => {
let btn = this.querySelector('.inputPassword');
if (btn.matches(':focus')) {
btn = this.querySelector('.buttonDo');
}
btn.focus();
return false;
});
const self = this;
oDom.addEventListener('click', event => {
const el = event.target.closestWithin('.key-list__item', oDom);
if (el) {
oDom.querySelectorAll('.key-list__item .key-list__item__radio').forEach(node =>
node.textContent = el === node ? '⦿' : '○'
);
self.selectedKey(ko.dataFor(el));
// this.querySelector('.inputPassword').focus();
}
});
}
onHideWithDelay() {
this.clearPopup();
}
onShow(fCallback, privateKeys) {
this.clearPopup();
this.resultCallback = fCallback;
this.privateKeys(privateKeys);
if (this.viewModelDom) {
const el = this.querySelector('.key-list__item');
el && el.click();
}
}
}
export { MessageOpenPgpPopupView, MessageOpenPgpPopupView as default };

View file

@ -1,105 +0,0 @@
import { pInt } from 'Common/Utils';
import { PgpUserStore } from 'Stores/User/Pgp';
import { IdentityUserStore } from 'Stores/User/Identity';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
class NewOpenPgpKeyPopupView extends AbstractViewPopup {
constructor() {
super('NewOpenPgpKey');
this.identities = IdentityUserStore;
this.addObservables({
email: '',
emailError: false,
name: '',
password: '',
keyBitLength: 4096,
submitRequest: false,
submitError: ''
});
this.email.subscribe(() => this.emailError(false));
decorateKoCommands(this, {
generateOpenPgpKeyCommand: 1
});
}
generateOpenPgpKeyCommand() {
const userId = {},
openpgpKeyring = PgpUserStore.openpgpKeyring;
this.emailError(!this.email().trim());
if (!openpgpKeyring || this.emailError()) {
return false;
}
userId.email = this.email();
if (this.name()) {
userId.name = this.name();
}
this.submitRequest(true);
this.submitError('');
setTimeout(() => {
try {
openpgp
.generateKey({
userIds: [userId],
numBits: pInt(this.keyBitLength()),
passphrase: this.password().trim()
})
.then((keyPair) => {
this.submitRequest(false);
if (keyPair && keyPair.privateKeyArmored) {
openpgpKeyring.privateKeys.importKey(keyPair.privateKeyArmored);
openpgpKeyring.publicKeys.importKey(keyPair.publicKeyArmored);
openpgpKeyring.store();
PgpUserStore.reloadOpenPgpKeys();
PgpUserStore.gnupgImportKey(keyPair.privateKeyArmored);
this.cancelCommand();
}
})
.catch((e) => {
this.submitRequest(false);
this.showError(e);
});
} catch (e) {
this.submitRequest(false);
this.showError(e);
}
}, 100);
return true;
}
showError(e) {
console.log(e);
if (e && e.message) {
this.submitError(e.message);
}
}
onShow() {
this.name(IdentityUserStore()[0].name());
this.password('');
this.email(IdentityUserStore()[0].email());
this.emailError(false);
this.keyBitLength(4096);
this.submitError('');
}
}
export { NewOpenPgpKeyPopupView, NewOpenPgpKeyPopupView as default };

View file

@ -0,0 +1,102 @@
//import { pInt } from 'Common/Utils';
import { PgpUserStore } from 'Stores/User/Pgp';
import { IdentityUserStore } from 'Stores/User/Identity';
import { decorateKoCommands } from 'Knoin/Knoin';
import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { Capa } from 'Common/Enums';
import { Settings } from 'Common/Globals';
export class OpenPgpGeneratePopupView extends AbstractViewPopup {
constructor() {
super('OpenPgpGenerate');
this.identities = IdentityUserStore;
this.addObservables({
email: '',
emailError: false,
name: '',
password: '',
keyType: 'ECC',
submitRequest: false,
submitError: '',
saveGnuPG: true,
saveServer: true
});
this.canGnuPG = Settings.capa(Capa.GnuPG);
this.email.subscribe(() => this.emailError(false));
decorateKoCommands(this, {
generateOpenPgpKeyCommand: 1
});
}
generateOpenPgpKeyCommand() {
const type = this.keyType().toLowerCase(),
userId = {
name: this.name(),
email: this.email()
},
cfg = {
type: type,
userIDs: [userId],
passphrase: this.password().trim()
// format: 'armored' // output key format, defaults to 'armored' (other options: 'binary' or 'object')
}
/*
if ('ecc' === type) {
cfg.curve = 'curve25519';
} else {
cfg.rsaBits = pInt(this.keyBitLength());
}
*/
this.emailError(!this.email().trim());
if (this.emailError()) {
return false;
}
this.submitRequest(true);
this.submitError('');
openpgp.generateKey(cfg).then(keyPair => {
if (keyPair) {
keyPair.onServer = !!this.saveServer();
keyPair.inGnuPG = !!this.saveGnuPG();
keyPair.uid = userId;
PgpUserStore.storeKeyPair(keyPair, ()=>{
this.submitRequest(false);
this.cancelCommand();
});
}
})
.catch((e) => {
this.submitRequest(false);
this.showError(e);
});
return true;
}
showError(e) {
console.log(e);
if (e && e.message) {
this.submitError(e.message);
}
}
onShow() {
this.name(''/*IdentityUserStore()[0].name()*/);
this.password('');
this.email(''/*IdentityUserStore()[0].email()*/);
this.emailError(false);
this.submitError('');
}
}

View file

@ -6,9 +6,9 @@ import { AbstractViewPopup } from 'Knoin/AbstractViews';
import { Capa } from 'Common/Enums';
import { Settings } from 'Common/Globals';
class AddOpenPgpKeyPopupView extends AbstractViewPopup {
export class OpenPgpImportPopupView extends AbstractViewPopup {
constructor() {
super('AddOpenPgpKey');
super('OpenPgpImport');
this.addObservables({
key: '',
@ -16,11 +16,10 @@ class AddOpenPgpKeyPopupView extends AbstractViewPopup {
keyErrorMessage: '',
saveGnuPG: true,
saveOpenPGP: true
saveServer: true
});
this.canGnuPG = Settings.capa(Capa.GnuPG);
this.canOpenPGP = Settings.capa(Capa.OpenPGP);
this.key.subscribe(() => {
this.keyError(false);
@ -50,8 +49,7 @@ class AddOpenPgpKeyPopupView extends AbstractViewPopup {
count = 30,
done = false;
// eslint-disable-next-line max-len
const reg = /[-]{3,6}BEGIN[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}[\s\S]+?[-]{3,6}END[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}/gi,
keyring = PgpUserStore.openpgpKeyring;
const reg = /[-]{3,6}BEGIN[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}[\s\S]+?[-]{3,6}END[\s]PGP[\s](PRIVATE|PUBLIC)[\s]KEY[\s]BLOCK[-]{3,6}/gi;
do {
match = reg.exec(keyTrimmed);
@ -61,14 +59,15 @@ class AddOpenPgpKeyPopupView extends AbstractViewPopup {
if (this.saveGnuPG()) {
PgpUserStore.gnupgImportKey(this.key());
}
/*
if (this.canOpenPGP && this.saveOpenPGP()) {
if ('PRIVATE' === match[1]) {
err = keyring.privateKeys.importKey(match[0]);
err = PgpUserStore.openpgpKeyring.privateKeys.importKey(match[0]);
} else if ('PUBLIC' === match[1]) {
err = keyring.publicKeys.importKey(match[0]);
err = PgpUserStore.openpgpKeyring.publicKeys.importKey(match[0]);
}
}
*/
if (err) {
this.keyError(true);
this.keyErrorMessage(err && err[0] ? '' + err[0] : '');
@ -83,12 +82,6 @@ class AddOpenPgpKeyPopupView extends AbstractViewPopup {
}
} while (!done);
if (this.canOpenPGP && this.saveOpenPGP()) {
keyring.store();
}
// PgpUserStore.reloadOpenPgpKeys();
if (this.keyError()) {
return false;
}
@ -103,5 +96,3 @@ class AddOpenPgpKeyPopupView extends AbstractViewPopup {
this.keyErrorMessage('');
}
}
export { AddOpenPgpKeyPopupView, AddOpenPgpKeyPopupView as default };

View file

@ -182,7 +182,7 @@ export class MailMessageView extends AbstractViewRight {
pgpSigned: () => currentMessage() && !!currentMessage().pgpSigned(),
pgpEncrypted: () => currentMessage()
&& currentMessage().isPgpEncrypted(),
&& !!(currentMessage().pgpEncrypted() || currentMessage().isPgpEncrypted()),
pgpSupported: () => currentMessage() && PgpUserStore.isSupported(),
messageListOrViewLoading:
@ -625,7 +625,8 @@ export class MailMessageView extends AbstractViewRight {
}
pgpDecrypt(self) {
if (self.pgpEncrypted()) {
const pgpInfo = self.pgpEncrypted();
if (pgpInfo) {
const message = self.message();
if (window.mailvelope) {
/**
@ -657,10 +658,37 @@ export class MailMessageView extends AbstractViewRight {
});
}
/*
else {
// TODO: which key to decrypt, use pgpInfo.KeyIds
PgpUserStore.getKeyForDecrypting(message.email()).then(result => {
console.log({canPgpSign:result});
this.canPgpSign(!!result)
});
else if (window.openpgp) {
decryptMessage(message, recipients, fCallback)
}
else if (Settings.capa(Capa.GnuPG)) {
message.
let params = {
Folder: message.folder,
Uid: message.uid,
PartId: message.pgpEncrypted().PartId,
KeyId: '',
Passphrase: prompt("Passphrase", ''),
Data: '' // optional
}
rl.app.Remote.post('GnupgDecrypt', null, params)
.then(data => {
// TODO
console.dir(data);
})
.catch(error => {
// TODO
console.dir(error);
});
}
*/
}