(function () { 'use strict'; var _ = require('_'), ko = require('ko'), $ = require('$'), kn = require('Knoin/Knoin'), Translator = require('Common/Translator'), Utils = require('Common/Utils') ; /** * @constructor */ function PgpUserStore() { this.capaOpenPGP = ko.observable(false); this.openpgp = null; this.openpgpkeys = ko.observableArray([]); this.openpgpKeyring = null; this.openpgpkeysPublic = this.openpgpkeys.filter(function (oItem) { return !!(oItem && !oItem.isPrivate); }); this.openpgpkeysPrivate = this.openpgpkeys.filter(function (oItem) { return !!(oItem && oItem.isPrivate); }); } /** * @return {boolean} */ PgpUserStore.prototype.isSupported = function () { return !!this.openpgp; }; PgpUserStore.prototype.findPublicKeyByHex = function (sHash) { return _.find(this.openpgpkeysPublic(), function (oItem) { return sHash && oItem && sHash === oItem.id; }); }; PgpUserStore.prototype.findPrivateKeyByHex = function (sHash) { return _.find(this.openpgpkeysPrivate(), function (oItem) { return sHash && oItem && sHash === oItem.id; }); }; PgpUserStore.prototype.findPublicKeysByEmail = function (sEmail) { return _.compact(_.flatten(_.map(this.openpgpkeysPublic(), function (oItem) { var oKey = oItem && -1 !== oItem.emails.indexOf(sEmail) ? oItem : null; return oKey ? oKey.getNativeKeys() : [null]; }), true)); }; PgpUserStore.prototype.findPublicKeysBySigningKeyIds = function (aSigningKeyIds) { var self = this; return _.compact(_.flatten(_.map(aSigningKeyIds, function (oId) { var oKey = oId && oId.toHex ? self.findPublicKeyByHex(oId.toHex()) : null; return oKey ? oKey.getNativeKeys() : [null]; }), true)); }; PgpUserStore.prototype.findPrivateKeysByEncryptionKeyIds = function (aEncryptionKeyIds, aRecipients, bReturnWrapKeys) { var self = this, aResult = []; aResult = Utils.isArray(aEncryptionKeyIds) ? _.compact(_.flatten(_.map(aEncryptionKeyIds, function (oId) { var oKey = oId && oId.toHex ? self.findPrivateKeyByHex(oId.toHex()) : null; return oKey ? (bReturnWrapKeys ? [oKey] : oKey.getNativeKeys()) : [null]; }), true)) : []; if (0 === aResult.length && Utils.isNonEmptyArray(aRecipients)) { aResult = _.compact(_.flatten(_.map(aRecipients, function (sEmail) { var oKey = sEmail ? self.findPrivateKeyByEmailNotNative(sEmail) : null; return oKey ? (bReturnWrapKeys ? [oKey] : oKey.getNativeKeys()) : [null]; }), true)); } return aResult; }; /** * @param {string} sEmail * @return {?} */ PgpUserStore.prototype.findPublicKeyByEmailNotNative = function (sEmail) { return _.find(this.openpgpkeysPublic(), function (oItem) { return oItem && -1 !== oItem.emails.indexOf(sEmail); }) || null; }; /** * @param {string} sEmail * @return {?} */ PgpUserStore.prototype.findPrivateKeyByEmailNotNative = function (sEmail) { return _.find(this.openpgpkeysPrivate(), function (oItem) { return oItem && -1 !== oItem.emails.indexOf(sEmail); }) || null; }; /** * @param {string} sEmail * @param {string=} sPassword * @return {?} */ PgpUserStore.prototype.findPrivateKeyByEmail = function (sEmail, sPassword) { var oPrivateKeys = [], oPrivateKey = null, oKey = _.find(this.openpgpkeysPrivate(), function (oItem) { return oItem && -1 !== oItem.emails.indexOf(sEmail); }) ; if (oKey) { oPrivateKeys = oKey.getNativeKeys(); oPrivateKey = oPrivateKeys[0] || null; try { if (oPrivateKey) { oPrivateKey.decrypt(Utils.pString(sPassword)); } } catch (e) { oPrivateKey = null; } } return oPrivateKey; }; /** * @param {string=} sPassword * @return {?} */ PgpUserStore.prototype.findSelfPrivateKey = function (sPassword) { return this.findPrivateKeyByEmail(require('Stores/User/Account').email(), sPassword); }; PgpUserStore.prototype.decryptMessage = function (oMessage, aRecipients, fCallback) { var self = this, aPrivateKeys = []; if (oMessage && oMessage.getEncryptionKeyIds) { aPrivateKeys = this.findPrivateKeysByEncryptionKeyIds(oMessage.getEncryptionKeyIds(), aRecipients, true); if (aPrivateKeys && 0 < aPrivateKeys.length) { kn.showScreenPopup(require('View/Popup/MessageOpenPgp'), [function (oDecriptedKey) { if (oDecriptedKey) { var oPrivateKey = null, oDecryptedMessage = null; try { oDecryptedMessage = oMessage.decrypt(oDecriptedKey); } catch (e) { oDecryptedMessage = null; } if (oDecryptedMessage) { oPrivateKey = self.findPrivateKeyByHex(oDecriptedKey.primaryKey.keyid.toHex()); if (oPrivateKey) { self.verifyMessage(oDecryptedMessage, function (oValidKey, aSigningKeyIds) { fCallback(oPrivateKey, oDecryptedMessage, oValidKey || null, aSigningKeyIds || null); }); } else { fCallback(oPrivateKey, oDecryptedMessage); } } else { fCallback(oPrivateKey, oDecryptedMessage); } } else { fCallback(null, null); } }, aPrivateKeys]); return false; } } fCallback(null, null); return false; }; PgpUserStore.prototype.verifyMessage = function (oMessage, fCallback) { var oValid = null, aResult = [], aPublicKeys = [], aSigningKeyIds = []; if (oMessage && oMessage.getSigningKeyIds) { aSigningKeyIds = oMessage.getSigningKeyIds(); if (aSigningKeyIds && 0 < aSigningKeyIds.length) { aPublicKeys = this.findPublicKeysBySigningKeyIds(aSigningKeyIds); if (aPublicKeys && 0 < aPublicKeys.length) { try { aResult = oMessage.verify(aPublicKeys); oValid = _.find(_.isArray(aResult) ? aResult : [], function (oItem) { return oItem && oItem.valid && oItem.keyid; }); if (oValid && oValid.keyid && oValid.keyid && oValid.keyid.toHex) { fCallback(this.findPublicKeyByHex(oValid.keyid.toHex())); return true; } } catch (e) { Utils.log(e); } } fCallback(null, aSigningKeyIds); return false; } } fCallback(null); return false; }; /** * @param {*} mDom */ PgpUserStore.prototype.controlsHelper = function (mDom, oVerControl, bSuccess, sTitle, sText) { if (bSuccess) { mDom.removeClass('error').addClass('success').attr('title', sTitle); oVerControl.removeClass('error').addClass('success').attr('title', sTitle); } else { mDom.removeClass('success').addClass('error').attr('title', sTitle); oVerControl.removeClass('success').addClass('error').attr('title', sTitle); } if (!Utils.isUnd(sText)) { mDom.text(Utils.trim(sText.replace(/(\u200C|\u0002)/g, ''))); } }; /** * @param {*} mDom * @param {MessageModel} oRainLoopMessage */ PgpUserStore.prototype.initMessageBodyControls = function (mDom, oRainLoopMessage) { if (mDom && !mDom.hasClass('inited')) { mDom.addClass('inited'); var self = this, bEncrypted = mDom.hasClass('encrypted'), bSigned = mDom.hasClass('signed'), oVerControl = null, aRecipients = oRainLoopMessage ? oRainLoopMessage.getEmails(['from', 'to', 'cc']) : [], sData = '' ; if (bEncrypted || bSigned) { sData = mDom.text(); mDom.data('openpgp-original', sData); if (bEncrypted) { oVerControl = $('