Bug 1628097 - Auto attach own pub key to signed messages. Use better attachment name. Use configured sign/encrypt prefs. r=mkmelin DONTBUILD
authorKai Engert <kaie@kuix.de>
Wed, 08 Apr 2020 18:38:35 +0200
changeset 38849 bf32f930607721e6fdc9264e7ecdee5941adc472
parent 38848 dc4ab600181bbb9a679dfed7a399b80fa500eded
child 38850 89f992fc5fe99d11e99fe4d8dde040bdac301bd5
push id401
push userclokep@gmail.com
push dateMon, 01 Jun 2020 20:41:59 +0000
reviewersmkmelin
bugs1628097
Bug 1628097 - Auto attach own pub key to signed messages. Use better attachment name. Use configured sign/encrypt prefs. r=mkmelin DONTBUILD Differential Revision: https://phabricator.services.mozilla.com/D70213
mail/components/compose/content/MsgComposeCommands.js
mail/extensions/openpgp/content/modules/mimeEncrypt.jsm
mail/extensions/openpgp/content/ui/enigmailMsgComposeOverlay.js
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -105,21 +105,32 @@ var gManualAttachmentReminder;
 var gDisableAttachmentReminder;
 var gComposeType;
 var gLanguageObserver;
 var gBodyFromArgs;
 
 var gSMFields = null;
 var gSelectedTechnologyIsPGP = false;
 
+// The initial flags store the value we used at composer open time.
+// Some flags might be automatically changed as a consequence of other
+// changes. When reverting automatic actions, the initial flags help
+// us know what value we should use for restoring.
+
 var gSendSigned = false;
+var gSendSignedInitial = false;
+
 var gAttachMyPublicPGPKey = false;
+var gAttachMyPublicPGPKeyInitial = false;
 
 var gSendEncrypted = false;
+var gSendEncryptedInitial = false;
+
 var gOptionalEncryption = false; // Only encrypt if possible. Ignored if !gSendEncrypted.
+var gOptionalEncryptionInitial = false;
 
 var gUserTouchedSendEncrypted = false;
 var gUserTouchedSendSigned = false;
 var gUserTouchedAttachMyPubKey = false;
 
 var gIsRelatedToEncryptedOriginal = false;
 var gIsRelatedToSignedOriginal = false;
 
@@ -1566,44 +1577,73 @@ function isSmimeEncryptionConfigured() {
 
 function isPgpConfigured() {
   return !!gCurrentIdentity.getUnicharAttribute("openpgp_key_id");
 }
 
 function toggleGlobalSignMessage() {
   gSendSigned = !gSendSigned;
   gUserTouchedSendSigned = true;
+
+  if (!gUserTouchedAttachMyPubKey) {
+    if (gSendSigned) {
+      gAttachMyPublicPGPKey = true;
+    } else {
+      gAttachMyPublicPGPKey = gAttachMyPublicPGPKeyInitial;
+    }
+  }
+
   setEncSigStatusUI();
 }
 
 function setGlobalEncryptMessage(mode) {
   let oldSendEnc = gSendEncrypted;
   let oldOptEnc = gOptionalEncryption;
 
+  let enableSig = false;
+
   switch (mode) {
     case 0:
       gSendEncrypted = false;
       gOptionalEncryption = false;
       break;
     case 1:
       gSendEncrypted = true;
+      enableSig = true;
       gOptionalEncryption = true;
       break;
     case 2:
       gSendEncrypted = true;
+      enableSig = true;
       gOptionalEncryption = false;
       break;
     default:
       return;
   }
 
   if (oldSendEnc != gSendEncrypted || oldOptEnc != gOptionalEncryption) {
     gUserTouchedSendEncrypted = true;
   }
 
+  if (!gUserTouchedSendSigned) {
+    if (enableSig) {
+      gSendSigned = true;
+    } else {
+      gSendSigned = gSendSignedInitial;
+    }
+  }
+
+  if (!gUserTouchedAttachMyPubKey) {
+    if (gSendSigned) {
+      gAttachMyPublicPGPKey = true;
+    } else {
+      gAttachMyPublicPGPKey = gAttachMyPublicPGPKeyInitial;
+    }
+  }
+
   setEncSigStatusUI();
 }
 
 function toggleAttachMyPublicKey() {
   gAttachMyPublicPGPKey = !gAttachMyPublicPGPKey;
   gUserTouchedAttachMyPubKey = true;
 }
 
@@ -3647,28 +3687,73 @@ function ComposeLoad() {
   gMsgCompose.compFields.composeSecure = null;
   gSMFields = Cc[
     "@mozilla.org/messengercompose/composesecure;1"
   ].createInstance(Ci.nsIMsgComposeSecure);
   if (gSMFields) {
     gMsgCompose.compFields.composeSecure = gSMFields;
   }
 
-  // TODO: call code to get default settings for gSendEncrypted etc.
+  let configuredSMIME =
+    isSmimeSigningConfigured() || isSmimeEncryptionConfigured();
+  let configuredOpenPGP = false;
+
+  if (MailConstants.MOZ_OPENPGP && BondOpenPGP.allDependenciesLoaded()) {
+    configuredOpenPGP = isPgpConfigured();
+  }
+
+  gSelectedTechnologyIsPGP = false;
+
+  if (configuredOpenPGP) {
+    if (!configuredSMIME) {
+      gSelectedTechnologyIsPGP = true;
+    } else {
+      // both are configured
+      let techPref = gCurrentIdentity.getIntAttribute("e2etechpref");
+      gSelectedTechnologyIsPGP = techPref != 1;
+
+      // TODO: if !techPref, we might set another flag, and
+      // decide dynamically which one to use, based on the
+      // availability of recipient keys etc.
+    }
+  }
+
+  if (configuredOpenPGP || configuredSMIME) {
+    gSendEncrypted = gCurrentIdentity.getIntAttribute("encryptionpolicy") > 0;
+    gOptionalEncryption = false;
+    gSendSigned = gCurrentIdentity.getBoolAttribute("sign_mail");
+  }
+
+  gSendSignedInitial = gSendSigned;
+  gAttachMyPublicPGPKeyInitial = gAttachMyPublicPGPKey;
+  gSendEncryptedInitial = gSendEncrypted;
+  gOptionalEncryptionInitial = gOptionalEncryption;
+
+  // automatic changes after this line
+
+  if (gSendSigned && gSelectedTechnologyIsPGP) {
+    gAttachMyPublicPGPKey = true;
+  }
+
   if (
     gEncryptedURIService &&
     gEncryptedURIService.isEncrypted(gMsgCompose.originalMsgURI)
   ) {
     gIsRelatedToEncryptedOriginal = true;
   }
 
   if (gIsRelatedToEncryptedOriginal) {
     gSendEncrypted = true;
   }
 
+  if (gSMFields && !gSelectedTechnologyIsPGP) {
+    gSMFields.requireEncryptMessage = gSendEncrypted;
+    gSMFields.signMessage = gSendSigned;
+  }
+
   setEncSigStatusUI();
 }
 
 function ComposeUnload() {
   // Send notification that the window is going away completely.
   document
     .getElementById("msgcomposeWindow")
     .dispatchEvent(
--- a/mail/extensions/openpgp/content/modules/mimeEncrypt.jsm
+++ b/mail/extensions/openpgp/content/modules/mimeEncrypt.jsm
@@ -451,19 +451,19 @@ PgpMimeEncrypt.prototype = {
 
   signedHeaders2() {
     LOCAL_DEBUG("mimeEncrypt.js: signedHeaders2\n");
 
     this.writeOut(
       "\r\n--" +
         this.cryptoBoundary +
         "\r\n" +
-        'Content-Type: application/pgp-signature; name="signature.asc"\r\n' +
+        'Content-Type: application/pgp-signature; name="OpenPGP_signature.asc"\r\n' +
         "Content-Description: OpenPGP digital signature\r\n" +
-        'Content-Disposition: attachment; filename="signature.asc"\r\n\r\n'
+        'Content-Disposition: attachment; filename="OpenPGP_signature"\r\n\r\n'
     );
   },
 
   finishCryptoHeaders() {
     EnigmailLog.DEBUG("mimeEncrypt.js: finishCryptoHeaders\n");
 
     this.writeOut("\r\n--" + this.cryptoBoundary + "--\r\n");
   },
--- a/mail/extensions/openpgp/content/ui/enigmailMsgComposeOverlay.js
+++ b/mail/extensions/openpgp/content/ui/enigmailMsgComposeOverlay.js
@@ -7,17 +7,17 @@
 "use strict";
 
 /*globally available Thunderbird variables/object/functions: */
 /*global gMsgCompose: false, getCurrentIdentity: false, gNotification: false */
 /*global UpdateAttachmentBucket: false, gContentChanged: true */
 /*global AddAttachments: false, AddAttachment: false, ChangeAttachmentBucketVisibility: false, GetResourceFromUri: false */
 /*global Recipients2CompFields: false, Attachments2CompFields: false, DetermineConvertibility: false, gWindowLocked: false */
 /*global CommandUpdate_MsgCompose: false, gSMFields: false, setSecuritySettings: false, getCurrentAccountKey: false */
-/*global Sendlater3Composing: false */
+/*global Sendlater3Composing: false, gCurrentIdentity: false */
 /*global gSendEncrypted: true, gOptionalEncryption: true, gSendSigned: true, gSelectedTechnologyIsPGP: true */
 /*global gIsRelatedToEncryptedOriginal: true, gIsRelatedToSignedOriginal: true, gAttachMyPublicPGPKey: true */
 
 /* import-globals-from ../BondOpenPGP.jsm */
 
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var { MailServices } = ChromeUtils.import(
   "resource:///modules/MailServices.jsm"
@@ -204,55 +204,29 @@ Enigmail.msg = {
     /*
     let numCerts = EnigmailFuncs.getNumOfX509Certs();
     this.addrOnChangeTimeout = Math.max((numCerts - 250) * 2, 250);
     EnigmailLog.DEBUG(`enigmailMsgComposeOverlay.js: composeStartup: numCerts=${numCerts}; setting timeout to ${this.addrOnChangeTimeout}\n`);
     */
 
     Enigmail.msg.msgComposeReset(false); // false => not closing => call setIdentityDefaults()
 
-    // TODO this migration code needs to move to a better place, possibly configure.jsm
+    // TODO implement migration code for old prefs, possibly in configure.jsm
     // Use a new pref identityEnigmailPrefsMigrated, default false.
     // Only if we're doing this for the first time for an identity,
     // try to read old prefs and if found, store as new prefs,
     // then set identityEnigmailPrefsMigrated=true
-
-    if (
-      Enigmail.msg.wasEnigmailAddOnInstalled() &&
-      Enigmail.msg.wasEnigmailEnabledForIdentity() &&
-      this.identity.getIntAttribute("mimePreferOpenPGP") > 0
-    ) {
-      // migrate old enigmail prefs
-      gSendEncrypted =
-        this.identity.getIntAttribute("defaultEncryptionPolicy") > 0;
-      gOptionalEncryption =
-        this.identity.getIntAttribute("autoSendEncrypted") > 0;
-      gSendSigned = this.identity.getIntAttribute("defaultSigningPolicy") > 0;
-      gSelectedTechnologyIsPGP = true;
-    } else if (Enigmail.msg.isSmimeEnabled()) {
-      gSendEncrypted = this.identity.getIntAttribute("encryptionpolicy") > 0;
-      gOptionalEncryption = false;
-      gSendSigned = this.identity.getBoolAttribute("sign_mail");
-    } else {
-      // if the user didn't yet configure s/mime, use PGP mode.
-      gSendEncrypted = false;
-      gOptionalEncryption = false;
-      gSendSigned = false;
-      gSelectedTechnologyIsPGP = true;
-    }
-    // TODO: If already migrated, set variables using new pres
-
-    if (gIsRelatedToEncryptedOriginal) {
-      gSendEncrypted = true;
-    }
-
-    if (!gSelectedTechnologyIsPGP) {
-      gSMFields.requireEncryptMessage = gSendEncrypted;
-      gSMFields.signMessage = gSendSigned;
-    }
+    // Enigmail account prefs that need to be handled:
+    // - defaultEncryptionPolicy
+    // - autoSendEncrypted
+    // - defaultSigningPolicy
+    // possible values to check:
+    // - Enigmail.msg.wasEnigmailAddOnInstalled()
+    // - Enigmail.msg.wasEnigmailEnabledForIdentity
+    // - this.identity.getIntAttribute("mimePreferOpenPGP") > 0
 
     Enigmail.msg.composeOpen();
     //Enigmail.msg.processFinalState();
     Enigmail.msg.updateStatusBar();
     Enigmail.msg.initialSendFlags();
 
     //Enigmail.msg.setFinalSendMode('final-pgpmimeYes');
   },
@@ -914,23 +888,16 @@ Enigmail.msg = {
     this.dirty = 0;
     this.processed = null;
     this.timeoutId = null;
 
     this.modifiedAttach = null;
     //this.sendMode = 0;
     //this.sendModeDirty = false;
 
-    // here ???
-    gSendSigned = false;
-    gSendEncrypted = false;
-    gOptionalEncryption = false;
-    gIsRelatedToEncryptedOriginal = false;
-    gIsRelatedToSignedOriginal = false;
-
     this.statusEncryptedStr = "???";
     this.statusSignedStr = "???";
     //this.statusPGPMimeStr = "???";
     //this.statusInlinePGPStr = "???";
     this.statusAttachOwnKey = "???";
     this.identity = null;
     this.sendProcess = false;
     this.trustAllKeys = false;
@@ -1045,17 +1012,21 @@ Enigmail.msg = {
       this.attachOwnKeyObj.attachedKey != id
     ) {
       // remove attached key if user ID changed
       this.removeAttachedKey();
     }
 
     if (!this.attachOwnKeyObj.attachedKey) {
       let hex = "0x" + id;
-      var attachedObj = this.extractAndAttachKey([hex], true);
+      var attachedObj = this.extractAndAttachKey(
+        [hex],
+        gCurrentIdentity.email,
+        true
+      );
       if (attachedObj) {
         this.attachOwnKeyObj.attachedObj = attachedObj;
         this.attachOwnKeyObj.attachedKey = hex;
       }
     }
   },
 
   attachKey() {
@@ -1076,23 +1047,23 @@ Enigmail.msg = {
       "dialog,modal,centerscreen,resizable",
       inputObj,
       resultObj
     );
     try {
       if (resultObj.cancelled) {
         return;
       }
-      this.extractAndAttachKey(resultObj.userList, true);
+      this.extractAndAttachKey(resultObj.userList, null, true);
     } catch (ex) {
       // cancel pressed -> do nothing
     }
   },
 
-  extractAndAttachKey(uidArray, warnOnError) {
+  extractAndAttachKey(uidArray, emailForFilename, warnOnError) {
     EnigmailLog.DEBUG(
       "enigmailMsgComposeOverlay.js: Enigmail.msg.extractAndAttachKey: \n"
     );
     var enigmailSvc = EnigmailCore.getService(window);
     if (!enigmailSvc) {
       return null;
     }
 
@@ -1142,16 +1113,19 @@ Enigmail.msg = {
     if (
       uidArray.length == 1 &&
       uidArray[0].search(/^(0x)?[a-fA-F0-9]+$/) === 0
     ) {
       keyAttachment.name = uidArray[0].substr(-16, 16) + ".asc";
       if (keyAttachment.name.search(/^0x/) < 0) {
         keyAttachment.name = "0x" + keyAttachment.name;
       }
+      // let normalizedEmail = emailForFilename.replace(" ", "_");
+      // emailForFilename is currently unused
+      keyAttachment.name = "OpenPGP_" + keyAttachment.name;
     } else {
       keyAttachment.name = "pgpkeys.asc";
     }
     keyAttachment.temporary = true;
     keyAttachment.contentType = "application/pgp-keys";
 
     // add attachment to msg
     this.addAttachment(keyAttachment);