2016-08-17 06:01:20 +08:00
|
|
|
import ko from 'ko';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-09-03 22:37:20 +08:00
|
|
|
import { isArray, arrayLength, pString, addComputablesTo } from 'Common/Utils';
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
import { AccountUserStore } from 'Stores/User/Account';
|
2016-09-13 04:50:21 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
import { showScreenPopup } from 'Knoin/Knoin';
|
2016-09-13 04:50:21 +08:00
|
|
|
|
2021-01-26 05:00:13 +08:00
|
|
|
import { MessageOpenPgpPopupView } from 'View/Popup/MessageOpenPgp';
|
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
export const PgpUserStore = new class {
|
2016-08-17 06:01:20 +08:00
|
|
|
constructor() {
|
|
|
|
this.openpgp = null;
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-01-22 19:23:20 +08:00
|
|
|
this.openpgpkeys = ko.observableArray();
|
2016-08-17 06:01:20 +08:00
|
|
|
this.openpgpKeyring = null;
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2021-09-03 22:37:20 +08:00
|
|
|
addComputablesTo(this, {
|
|
|
|
openpgpkeysPublic: () => this.openpgpkeys.filter(item => item && !item.isPrivate),
|
|
|
|
openpgpkeysPrivate: () => this.openpgpkeys.filter(item => item && item.isPrivate)
|
|
|
|
});
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
isSupported() {
|
|
|
|
return !!this.openpgp;
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findKeyByHex(keys, hash) {
|
2020-07-22 16:43:19 +08:00
|
|
|
return keys.find(item => hash && item && (hash === item.id || item.ids.includes(hash)));
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findPublicKeyByHex(hash) {
|
|
|
|
return this.findKeyByHex(this.openpgpkeysPublic(), hash);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findPrivateKeyByHex(hash) {
|
|
|
|
return this.findKeyByHex(this.openpgpkeysPrivate(), hash);
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findPublicKeysByEmail(email) {
|
2020-07-23 22:06:16 +08:00
|
|
|
return this.openpgpkeysPublic().map(item => {
|
|
|
|
const key = item && item.emails.includes(email) ? item : null;
|
|
|
|
return key ? key.getNativeKeys() : [null];
|
2020-10-03 05:54:15 +08:00
|
|
|
}).flat().filter(v => v);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findPublicKeysBySigningKeyIds(signingKeyIds) {
|
2020-07-23 22:06:16 +08:00
|
|
|
return signingKeyIds.map(id => {
|
|
|
|
const key = id && id.toHex ? this.findPublicKeyByHex(id.toHex()) : null;
|
|
|
|
return key ? key.getNativeKeys() : [null];
|
2020-10-03 05:54:15 +08:00
|
|
|
}).flat().filter(v => v);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
findPrivateKeysByEncryptionKeyIds(encryptionKeyIds, recipients, returnWrapKeys) {
|
2021-03-16 23:49:14 +08:00
|
|
|
let result = isArray(encryptionKeyIds)
|
2020-07-23 22:06:16 +08:00
|
|
|
? encryptionKeyIds.map(id => {
|
|
|
|
const key = id && id.toHex ? this.findPrivateKeyByHex(id.toHex()) : null;
|
|
|
|
return key ? (returnWrapKeys ? [key] : key.getNativeKeys()) : [null];
|
2020-10-03 05:54:15 +08:00
|
|
|
}).flat().filter(v => v)
|
2019-07-05 03:19:24 +08:00
|
|
|
: [];
|
|
|
|
|
2021-07-22 03:34:17 +08:00
|
|
|
if (!result.length && arrayLength(recipients)) {
|
2020-07-23 22:06:16 +08:00
|
|
|
result = recipients.map(sEmail => {
|
|
|
|
const keys = sEmail ? this.findAllPrivateKeysByEmailNotNative(sEmail) : null;
|
|
|
|
return keys
|
|
|
|
? returnWrapKeys
|
|
|
|
? keys
|
|
|
|
: keys.map(key => key.getNativeKeys()).flat()
|
|
|
|
: [null];
|
2020-10-03 05:54:15 +08:00
|
|
|
}).flat().validUnique(key => key.id);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2015-02-03 07:58:58 +08:00
|
|
|
}
|
2015-02-01 23:44:44 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {string} email
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findPublicKeyByEmailNotNative(email) {
|
2020-07-22 16:43:19 +08:00
|
|
|
return this.openpgpkeysPublic().find(item => item && item.emails.includes(email)) || null;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {string} email
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findPrivateKeyByEmailNotNative(email) {
|
2020-07-22 16:43:19 +08:00
|
|
|
return this.openpgpkeysPrivate().find(item => item && item.emails.includes(email)) || null;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} email
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findAllPublicKeysByEmailNotNative(email) {
|
2020-07-21 03:39:00 +08:00
|
|
|
return this.openpgpkeysPublic().filter(item => item && item.emails.includes(email)) || null;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} email
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findAllPrivateKeysByEmailNotNative(email) {
|
2020-07-21 03:39:00 +08:00
|
|
|
return this.openpgpkeysPrivate().filter(item => item && item.emails.includes(email)) || null;
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} email
|
|
|
|
* @param {string=} password
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findPrivateKeyByEmail(email, password) {
|
|
|
|
let privateKey = null;
|
2020-07-22 16:43:19 +08:00
|
|
|
const key = this.openpgpkeysPrivate().find(item => item && item.emails.includes(email));
|
2016-08-17 06:01:20 +08:00
|
|
|
|
2019-07-05 03:19:24 +08:00
|
|
|
if (key) {
|
|
|
|
try {
|
2016-08-17 06:01:20 +08:00
|
|
|
privateKey = key.getNativeKeys()[0] || null;
|
2019-07-05 03:19:24 +08:00
|
|
|
if (privateKey) {
|
2016-08-17 06:01:20 +08:00
|
|
|
privateKey.decrypt(pString(password));
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {
|
2016-08-17 06:01:20 +08:00
|
|
|
privateKey = null;
|
2015-02-22 06:00:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return privateKey;
|
|
|
|
}
|
2016-06-30 08:02:45 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
/**
|
|
|
|
* @param {string=} password
|
|
|
|
* @returns {?}
|
|
|
|
*/
|
|
|
|
findSelfPrivateKey(password) {
|
2021-03-11 05:41:35 +08:00
|
|
|
return this.findPrivateKeyByEmail(AccountUserStore.email(), password);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2016-08-10 02:58:34 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
decryptMessage(message, recipients, fCallback) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (message && message.getEncryptionKeyIds) {
|
2016-08-17 06:01:20 +08:00
|
|
|
const privateKeys = this.findPrivateKeysByEncryptionKeyIds(message.getEncryptionKeyIds(), recipients, true);
|
2020-07-28 18:35:41 +08:00
|
|
|
if (privateKeys && privateKeys.length) {
|
2021-01-26 05:00:13 +08:00
|
|
|
showScreenPopup(MessageOpenPgpPopupView, [
|
2019-07-05 03:19:24 +08:00
|
|
|
(decryptedKey) => {
|
|
|
|
if (decryptedKey) {
|
|
|
|
message.decrypt(decryptedKey).then(
|
|
|
|
(decryptedMessage) => {
|
|
|
|
let privateKey = null;
|
|
|
|
if (decryptedMessage) {
|
|
|
|
privateKey = this.findPrivateKeyByHex(decryptedKey.primaryKey.keyid.toHex());
|
|
|
|
if (privateKey) {
|
|
|
|
this.verifyMessage(decryptedMessage, (oValidKey, aSigningKeyIds) => {
|
|
|
|
fCallback(privateKey, decryptedMessage, oValidKey || null, aSigningKeyIds || null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fCallback(privateKey, decryptedMessage);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fCallback(privateKey, decryptedMessage);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
fCallback(null, null);
|
2016-08-17 06:01:20 +08:00
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
);
|
|
|
|
} else {
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(null, null);
|
2019-07-05 03:19:24 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
privateKeys
|
|
|
|
]);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(null, null);
|
2015-06-23 05:33:27 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
return false;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-05-24 01:33:01 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
verifyMessage(message, fCallback) {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (message && message.getSigningKeyIds) {
|
2016-08-17 06:01:20 +08:00
|
|
|
const signingKeyIds = message.getSigningKeyIds();
|
2020-07-28 18:35:41 +08:00
|
|
|
if (signingKeyIds && signingKeyIds.length) {
|
2016-08-17 06:01:20 +08:00
|
|
|
const publicKeys = this.findPublicKeysBySigningKeyIds(signingKeyIds);
|
2020-07-28 18:35:41 +08:00
|
|
|
if (publicKeys && publicKeys.length) {
|
2019-07-05 03:19:24 +08:00
|
|
|
try {
|
|
|
|
const result = message.verify(publicKeys),
|
2021-03-16 23:49:14 +08:00
|
|
|
valid = (isArray(result) ? result : []).find(item => item && item.valid && item.keyid);
|
2019-07-05 03:19:24 +08:00
|
|
|
|
|
|
|
if (valid && valid.keyid && valid.keyid && valid.keyid.toHex) {
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(this.findPublicKeyByHex(valid.keyid.toHex()));
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-05 03:19:24 +08:00
|
|
|
} catch (e) {
|
2020-08-12 06:25:36 +08:00
|
|
|
console.log(e);
|
2015-10-14 00:36:43 +08:00
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(null, signingKeyIds);
|
|
|
|
return false;
|
|
|
|
}
|
2015-06-23 05:33:27 +08:00
|
|
|
}
|
2016-05-05 08:14:38 +08:00
|
|
|
|
2016-08-17 06:01:20 +08:00
|
|
|
fCallback(null);
|
|
|
|
return false;
|
2016-06-30 08:02:45 +08:00
|
|
|
}
|
2016-05-05 08:14:38 +08:00
|
|
|
|
2021-03-11 05:41:35 +08:00
|
|
|
};
|