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