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