c8359d
# HG changeset patch
c8359d
# User Kai Engert <kaie@kuix.de>
c8359d
# Date 1666897160 -7200
c8359d
#      Thu Oct 27 20:59:20 2022 +0200
c8359d
# Node ID af0b1f5e4c7710f824c6141103e516ca60bc78aa
c8359d
# Parent  adfbf6378df82c8b2e087427a48ddc5cbe13aadd
c8359d
Bug 1791195 - Add RNP security rules to obsolete our patches to RNP. r=mkmelin,o.nickolay
c8359d
c8359d
diff --git a/comm/mail/extensions/openpgp/content/modules/RNP.jsm b/comm/mail/extensions/openpgp/content/modules/RNP.jsm
c8359d
--- a/comm/mail/extensions/openpgp/content/modules/RNP.jsm
c8359d
+++ b/comm/mail/extensions/openpgp/content/modules/RNP.jsm
c8359d
@@ -1863,12 +1863,12 @@ var RNP = {
c8359d
 
c8359d
     if (keyBlockStr.length > RNP.maxImportKeyBlockSize) {
c8359d
       throw new Error("rejecting big keyblock");
c8359d
     }
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     let pubKey;
c8359d
     if (!this.importToFFI(tempFFI, keyBlockStr, true, false, permissive)) {
c8359d
@@ -1892,12 +1892,12 @@ var RNP = {
c8359d
 
c8359d
     if (keyBlockStr.length > RNP.maxImportKeyBlockSize) {
c8359d
       throw new Error("rejecting big keyblock");
c8359d
     }
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     let keyList = null;
c8359d
     if (!this.importToFFI(tempFFI, keyBlockStr, pubkey, seckey, permissive)) {
c8359d
@@ -1929,12 +1929,12 @@ var RNP = {
c8359d
   async mergePublicKeyBlocks(fingerprint, ...keyBlocks) {
c8359d
     if (keyBlocks.some(b => b.length > RNP.maxImportKeyBlockSize)) {
c8359d
       throw new Error("keyBlock too big");
c8359d
     }
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     const pubkey = true;
c8359d
     const seckey = false;
c8359d
@@ -2067,12 +2067,12 @@ var RNP = {
c8359d
     let result = {};
c8359d
     result.exitCode = -1;
c8359d
     result.importedKeys = [];
c8359d
     result.errorMsg = "";
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     // TODO: check result
c8359d
     if (this.importToFFI(tempFFI, keyBlockStr, pubkey, seckey, permissive)) {
c8359d
@@ -3115,12 +3115,12 @@ var RNP = {
c8359d
    *
c8359d
    */
c8359d
   export_pubkey_strip_sigs_uids(expKey, keepUserIDs, out_binary) {
c8359d
     let expKeyId = this.getKeyIDFromHandle(expKey);
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     let exportFlags =
c8359d
       RNPLib.RNP_KEY_EXPORT_SUBKEYS | RNPLib.RNP_KEY_EXPORT_PUBLIC;
c8359d
@@ -3399,12 +3399,12 @@ var RNP = {
c8359d
       ))
c8359d
     ) {
c8359d
       throw new Error("rnp_output_to_armor failed:" + rv);
c8359d
     }
c8359d
 
c8359d
-    let tempFFI = new RNPLib.rnp_ffi_t();
c8359d
-    if (RNPLib.rnp_ffi_create(tempFFI.address(), "GPG", "GPG")) {
c8359d
+    let tempFFI = RNPLib.prepare_ffi();
c8359d
+    if (!tempFFI) {
c8359d
       throw new Error("Couldn't initialize librnp.");
c8359d
     }
c8359d
 
c8359d
     let internalPassword = await OpenPGPMasterpass.retrieveOpenPGPPassword();
c8359d
 
c8359d
diff --git a/comm/mail/extensions/openpgp/content/modules/RNPLib.jsm b/mail/extensions/openpgp/content/modules/RNPLib/comm.jsm
c8359d
--- a/comm/mail/extensions/openpgp/content/modules/RNPLib.jsm
c8359d
+++ b/comm/mail/extensions/openpgp/content/modules/RNPLib.jsm
c8359d
@@ -13,11 +13,11 @@ XPCOMUtils.defineLazyModuleGetters(this,
c8359d
   OpenPGPMasterpass: "chrome://openpgp/content/modules/masterpass.jsm",
c8359d
   Services: "resource://gre/modules/Services.jsm",
c8359d
   setTimeout: "resource://gre/modules/Timer.jsm",
c8359d
 });
c8359d
 
c8359d
-const MIN_RNP_VERSION = [0, 16, 0];
c8359d
+const MIN_RNP_VERSION = [0, 16, 2];
c8359d
 
c8359d
 var systemOS = Services.appinfo.OS.toLowerCase();
c8359d
 var abi = ctypes.default_abi;
c8359d
 
c8359d
 // Open librnp. Determine the path to the chrome directory and look for it
c8359d
@@ -149,10 +149,12 @@ function enableRNPLibJS() {
c8359d
   // this must be delayed until after "librnp" is initialized
c8359d
 
c8359d
   RNPLib = {
c8359d
     path: librnpPath,
c8359d
 
c8359d
+    // Handle to the RNP library and primary key data store.
c8359d
+    // Kept at null if init fails.
c8359d
     ffi: null,
c8359d
 
c8359d
     // returns rnp_input_t, destroy using rnp_input_destroy
c8359d
     async createInputFromPath(path) {
c8359d
       // IOUtils.read always returns an array.
c8359d
@@ -265,13 +267,204 @@ function enableRNPLibJS() {
c8359d
       const min_version = this.rnp_version_for(...MIN_RNP_VERSION);
c8359d
       const this_version = this.rnp_version();
c8359d
       return Boolean(this_version >= min_version);
c8359d
     },
c8359d
 
c8359d
+    /**
c8359d
+     * Prepare an RNP library handle, and in addition set all the
c8359d
+     * application's preferences for library behavior.
c8359d
+     *
c8359d
+     * Other application code should NOT call rnp_ffi_create directly,
c8359d
+     * but obtain an RNP library handle from this function.
c8359d
+     */
c8359d
+    prepare_ffi() {
c8359d
+      let ffi = new rnp_ffi_t();
c8359d
+      if (this._rnp_ffi_create(ffi.address(), "GPG", "GPG")) {
c8359d
+        return null;
c8359d
+      }
c8359d
+
c8359d
+      // Treat MD5 as insecure.
c8359d
+      if (
c8359d
+        this.rnp_add_security_rule(
c8359d
+          ffi,
c8359d
+          this.RNP_FEATURE_HASH_ALG,
c8359d
+          this.RNP_ALGNAME_MD5,
c8359d
+          this.RNP_SECURITY_OVERRIDE,
c8359d
+          0,
c8359d
+          this.RNP_SECURITY_INSECURE
c8359d
+        )
c8359d
+      ) {
c8359d
+        return null;
c8359d
+      }
c8359d
+
c8359d
+      // Use RNP's default rule for SHA1 used with data signatures,
c8359d
+      // and use our override to allow it for key signatures.
c8359d
+      if (
c8359d
+        this.rnp_add_security_rule(
c8359d
+          ffi,
c8359d
+          this.RNP_FEATURE_HASH_ALG,
c8359d
+          this.RNP_ALGNAME_SHA1,
c8359d
+          this.RNP_SECURITY_VERIFY_KEY | this.RNP_SECURITY_OVERRIDE,
c8359d
+          0,
c8359d
+          this.RNP_SECURITY_DEFAULT
c8359d
+        )
c8359d
+      ) {
c8359d
+        return null;
c8359d
+      }
c8359d
+
c8359d
+      /*
c8359d
+      // Security rules API does not yet support PK and SYMM algs.
c8359d
+      //
c8359d
+      // If a hash algorithm is already disabled at build time,
c8359d
+      // and an attempt is made to set a security rule for that
c8359d
+      // algorithm, then RNP returns a failure.
c8359d
+      //
c8359d
+      // Ideally, RNP should allow these calls (regardless of build time
c8359d
+      // settings) to define an application security rule, that is
c8359d
+      // independent of the configuration used for building the
c8359d
+      // RNP library.
c8359d
+
c8359d
+      if (
c8359d
+        this.rnp_add_security_rule(
c8359d
+          ffi,
c8359d
+          this.RNP_FEATURE_HASH_ALG,
c8359d
+          this.RNP_ALGNAME_SM3,
c8359d
+          this.RNP_SECURITY_OVERRIDE,
c8359d
+          0,
c8359d
+          this.RNP_SECURITY_PROHIBITED
c8359d
+        )
c8359d
+      ) {
c8359d
+        return null;
c8359d
+      }
c8359d
+
c8359d
+      if (
c8359d
+        this.rnp_add_security_rule(
c8359d
+          ffi,
c8359d
+          this.RNP_FEATURE_PK_ALG,
c8359d
+          this.RNP_ALGNAME_SM2,
c8359d
+          this.RNP_SECURITY_OVERRIDE,
c8359d
+          0,
c8359d
+          this.RNP_SECURITY_PROHIBITED
c8359d
+        )
c8359d
+      ) {
c8359d
+        return null;
c8359d
+      }
c8359d
+
c8359d
+      if (
c8359d
+        this.rnp_add_security_rule(
c8359d
+          ffi,
c8359d
+          this.RNP_FEATURE_SYMM_ALG,
c8359d
+          this.RNP_ALGNAME_SM4,
c8359d
+          this.RNP_SECURITY_OVERRIDE,
c8359d
+          0,
c8359d
+          this.RNP_SECURITY_PROHIBITED
c8359d
+        )
c8359d
+      ) {
c8359d
+        return null;
c8359d
+      }
c8359d
+      */
c8359d
+
c8359d
+      return ffi;
c8359d
+    },
c8359d
+
c8359d
+    /**
c8359d
+     * Test the correctness of security rules, in particular, test
c8359d
+     * if the given hash algorithm is allowed at the given time.
c8359d
+     *
c8359d
+     * This is an application consistency test. If the behavior isn't
c8359d
+     * according to the expectation, the function throws an error.
c8359d
+     *
c8359d
+     * @param {string} hashAlg - Test this hash algorithm
c8359d
+     * @param {time_t} time - Test status at this timestamp
c8359d
+     * @param {boolean} keySigAllowed - Test if using the hash algorithm
c8359d
+     *  is allowed for signatures found inside OpenPGP keys.
c8359d
+     * @param {boolean} dataSigAllowed - Test if using the hash algorithm
c8359d
+     *  is allowed for signatures on data.
c8359d
+     */
c8359d
+    _confirmSecurityRule(hashAlg, time, keySigAllowed, dataSigAllowed) {
c8359d
+      let level = new ctypes.uint32_t();
c8359d
+      let flag = new ctypes.uint32_t();
c8359d
+
c8359d
+      flag.value = this.RNP_SECURITY_VERIFY_DATA;
c8359d
+      let testDataSuccess = false;
c8359d
+      if (
c8359d
+        !RNPLib.rnp_get_security_rule(
c8359d
+          this.ffi,
c8359d
+          this.RNP_FEATURE_HASH_ALG,
c8359d
+          hashAlg,
c8359d
+          time,
c8359d
+          flag.address(),
c8359d
+          null,
c8359d
+          level.address()
c8359d
+        )
c8359d
+      ) {
c8359d
+        if (dataSigAllowed) {
c8359d
+          testDataSuccess = level.value == RNPLib.RNP_SECURITY_DEFAULT;
c8359d
+        } else {
c8359d
+          testDataSuccess = level.value < RNPLib.RNP_SECURITY_DEFAULT;
c8359d
+        }
c8359d
+      }
c8359d
+
c8359d
+      if (!testDataSuccess) {
c8359d
+        throw new Error("security configuration for data signatures failed");
c8359d
+      }
c8359d
+
c8359d
+      flag.value = this.RNP_SECURITY_VERIFY_KEY;
c8359d
+      let testKeySuccess = false;
c8359d
+      if (
c8359d
+        !RNPLib.rnp_get_security_rule(
c8359d
+          this.ffi,
c8359d
+          this.RNP_FEATURE_HASH_ALG,
c8359d
+          hashAlg,
c8359d
+          time,
c8359d
+          flag.address(),
c8359d
+          null,
c8359d
+          level.address()
c8359d
+        )
c8359d
+      ) {
c8359d
+        if (keySigAllowed) {
c8359d
+          testKeySuccess = level.value == RNPLib.RNP_SECURITY_DEFAULT;
c8359d
+        } else {
c8359d
+          testKeySuccess = level.value < RNPLib.RNP_SECURITY_DEFAULT;
c8359d
+        }
c8359d
+      }
c8359d
+
c8359d
+      if (!testKeySuccess) {
c8359d
+        throw new Error("security configuration for key signatures failed");
c8359d
+      }
c8359d
+    },
c8359d
+
c8359d
+    /**
c8359d
+     * Perform tests that the RNP library behaves according to the
c8359d
+     * defined security rules.
c8359d
+     * If a problem is found, the function throws an error.
c8359d
+     */
c8359d
+    _sanityCheckSecurityRules() {
c8359d
+      let time_t_now = Math.round(Date.now() / 1000);
c8359d
+      let ten_years_in_seconds = 10 * 365 * 24 * 60 * 60;
c8359d
+      let ten_years_future = time_t_now + ten_years_in_seconds;
c8359d
+
c8359d
+      this._confirmSecurityRule(this.RNP_ALGNAME_MD5, time_t_now, false, false);
c8359d
+      this._confirmSecurityRule(
c8359d
+        this.RNP_ALGNAME_MD5,
c8359d
+        ten_years_future,
c8359d
+        false,
c8359d
+        false
c8359d
+      );
c8359d
+
c8359d
+      this._confirmSecurityRule(this.RNP_ALGNAME_SHA1, time_t_now, true, false);
c8359d
+      this._confirmSecurityRule(
c8359d
+        this.RNP_ALGNAME_SHA1,
c8359d
+        ten_years_future,
c8359d
+        true,
c8359d
+        false
c8359d
+      );
c8359d
+    },
c8359d
+
c8359d
     async init() {
c8359d
-      this.ffi = new rnp_ffi_t();
c8359d
-      if (this.rnp_ffi_create(this.ffi.address(), "GPG", "GPG")) {
c8359d
+      this.ffi = this.prepare_ffi();
c8359d
+      if (!this.ffi) {
c8359d
         throw new Error("Couldn't initialize librnp.");
c8359d
       }
c8359d
 
c8359d
       this.rnp_ffi_set_log_fd(this.ffi, 2); // stderr
c8359d
 
c8359d
@@ -286,10 +479,18 @@ function enableRNPLibJS() {
c8359d
         null
c8359d
       );
c8359d
 
c8359d
       let { pubRingPath, secRingPath } = this.getFilenames();
c8359d
 
c8359d
+      try {
c8359d
+        this._sanityCheckSecurityRules();
c8359d
+      } catch (e) {
c8359d
+        // Disable all RNP operation
c8359d
+        this.ffi = null;
c8359d
+        throw e;
c8359d
+      }
c8359d
+
c8359d
       await this.loadWithFallback(pubRingPath, this.RNP_LOAD_SAVE_PUBLIC_KEYS);
c8359d
       await this.loadWithFallback(secRingPath, this.RNP_LOAD_SAVE_SECRET_KEYS);
c8359d
 
c8359d
       let pubnum = new ctypes.size_t();
c8359d
       this.rnp_get_public_key_count(this.ffi, pubnum.address());
c8359d
@@ -481,10 +682,14 @@ function enableRNPLibJS() {
c8359d
      * @param {string} path - The file path to save to.
c8359d
      * @param {number} keyRingFlag - RNP_LOAD_SAVE_PUBLIC_KEYS or
c8359d
      *   RNP_LOAD_SAVE_SECRET_KEYS.
c8359d
      */
c8359d
     async saveKeyRing(path, keyRingFlag) {
c8359d
+      if (!this.ffi) {
c8359d
+        return;
c8359d
+      }
c8359d
+
c8359d
       let oldPath = path + ".old";
c8359d
 
c8359d
       // Ignore failure, oldPath might not exist yet.
c8359d
       await IOUtils.copy(path, oldPath).catch(() => {});
c8359d
 
c8359d
@@ -540,10 +745,13 @@ function enableRNPLibJS() {
c8359d
         tmpPath: path + ".tmp-new",
c8359d
       });
c8359d
     },
c8359d
 
c8359d
     async saveKeys() {
c8359d
+      if (!this.ffi) {
c8359d
+        return;
c8359d
+      }
c8359d
       let { pubRingPath, secRingPath } = this.getFilenames();
c8359d
 
c8359d
       let saveThem = async () => {
c8359d
         await this.saveKeyRing(pubRingPath, this.RNP_LOAD_SAVE_PUBLIC_KEYS);
c8359d
         await this.saveKeyRing(secRingPath, this.RNP_LOAD_SAVE_SECRET_KEYS);
c8359d
@@ -600,11 +808,13 @@ function enableRNPLibJS() {
c8359d
       abi,
c8359d
       ctypes.char.ptr
c8359d
     ),
c8359d
 
c8359d
     // Get a RNP library handle.
c8359d
-    rnp_ffi_create: librnp.declare(
c8359d
+    // Mark with leading underscore, to clarify that this function
c8359d
+    // shouldn't be called directly - you should call prepare_ffi().
c8359d
+    _rnp_ffi_create: librnp.declare(
c8359d
       "rnp_ffi_create",
c8359d
       abi,
c8359d
       rnp_result_t,
c8359d
       rnp_ffi_t.ptr,
c8359d
       ctypes.char.ptr,
c8359d
@@ -1713,10 +1923,22 @@ function enableRNPLibJS() {
c8359d
       ctypes.uint32_t.ptr,
c8359d
       ctypes.uint64_t.ptr,
c8359d
       ctypes.uint32_t.ptr
c8359d
     ),
c8359d
 
c8359d
+    rnp_add_security_rule: librnp.declare(
c8359d
+      "rnp_add_security_rule",
c8359d
+      abi,
c8359d
+      rnp_result_t,
c8359d
+      rnp_ffi_t,
c8359d
+      ctypes.char.ptr,
c8359d
+      ctypes.char.ptr,
c8359d
+      ctypes.uint32_t,
c8359d
+      ctypes.uint64_t,
c8359d
+      ctypes.uint32_t
c8359d
+    ),
c8359d
+
c8359d
     rnp_result_t,
c8359d
     rnp_ffi_t,
c8359d
     rnp_password_cb_t,
c8359d
     rnp_input_t,
c8359d
     rnp_output_t,
c8359d
@@ -1748,11 +1970,26 @@ function enableRNPLibJS() {
c8359d
 
c8359d
     RNP_KEY_SIGNATURE_NON_SELF_SIG: 4,
c8359d
 
c8359d
     RNP_SUCCESS: 0x00000000,
c8359d
 
c8359d
+    RNP_FEATURE_SYMM_ALG: "symmetric algorithm",
c8359d
     RNP_FEATURE_HASH_ALG: "hash algorithm",
c8359d
+    RNP_FEATURE_PK_ALG: "public key algorithm",
c8359d
+    RNP_ALGNAME_MD5: "MD5",
c8359d
+    RNP_ALGNAME_SHA1: "SHA1",
c8359d
+    RNP_ALGNAME_SM2: "SM2",
c8359d
+    RNP_ALGNAME_SM3: "SM3",
c8359d
+    RNP_ALGNAME_SM4: "SM4",
c8359d
+
c8359d
+    RNP_SECURITY_OVERRIDE: 1,
c8359d
+    RNP_SECURITY_VERIFY_KEY: 2,
c8359d
+    RNP_SECURITY_VERIFY_DATA: 4,
c8359d
+    RNP_SECURITY_REMOVE_ALL: 65536,
c8359d
+
c8359d
+    RNP_SECURITY_PROHIBITED: 0,
c8359d
+    RNP_SECURITY_INSECURE: 1,
c8359d
     RNP_SECURITY_DEFAULT: 2,
c8359d
 
c8359d
     /* Common error codes */
c8359d
     RNP_ERROR_GENERIC: 0x10000000, // 268435456
c8359d
     RNP_ERROR_BAD_FORMAT: 0x10000001, // 268435457