c8359d
diff -up comm/third_party/moz.build.D161379.diff comm/third_party/moz.build
c8359d
--- comm/third_party/moz.build.D161379.diff	2022-10-14 21:45:15.000000000 +0200
c8359d
+++ comm/third_party/moz.build	2022-11-10 11:49:44.194016978 +0100
c8359d
@@ -11,9 +11,11 @@ if CONFIG["TB_LIBOTR_PREBUILT"]:
c8359d
 
c8359d
 if CONFIG["MZLA_LIBRNP"]:
c8359d
     DIRS += [
c8359d
-        "botan",
c8359d
         "bzip2",
c8359d
         "json-c",
c8359d
         "rnp",
c8359d
         "zlib",
c8359d
     ]
c8359d
+    if CONFIG["MZLA_LIBRNP_BACKEND"] == "botan":
c8359d
+        DIRS += [ "botan" ]
c8359d
+
c8359d
diff -up comm/third_party/openpgp.configure.D161379.diff comm/third_party/openpgp.configure
c8359d
--- comm/third_party/openpgp.configure.D161379.diff	2022-11-10 11:49:37.605024129 +0100
c8359d
+++ comm/third_party/openpgp.configure	2022-11-10 11:49:44.194016978 +0100
c8359d
@@ -199,16 +199,136 @@ with only_when(in_tree_librnp):
c8359d
         set_config("MZLA_BZIP2_CFLAGS", bzip2_flags.cflags)
c8359d
         set_config("MZLA_BZIP2_LIBS", bzip2_flags.ldflags)
c8359d
 
c8359d
-    # BOTAN --with-system-botan
c8359d
-    system_lib_option(
c8359d
-        "--with-system-botan",
c8359d
-        help="Use system Botan for librnp (located with pkgconfig)",
c8359d
-    )
c8359d
-
c8359d
-    botan_pkg = pkg_check_modules(
c8359d
-        "MZLA_BOTAN", "botan-2 >= 2.8.0", when="--with-system-botan"
c8359d
-    )
c8359d
-    set_config("MZLA_SYSTEM_BOTAN", depends_if(botan_pkg)(lambda _: True))
c8359d
+    # librnp crypto backend selection
c8359d
+    option("--with-librnp-backend",
c8359d
+           help="Build librnp with the selected backend: {botan, openssl}",
c8359d
+           default="botan")
c8359d
+
c8359d
+    @depends("--with-librnp-backend")
c8359d
+    def librnp_backend(backend):
c8359d
+        allowed = ("botan", "openssl")
c8359d
+        if backend[0] in allowed:
c8359d
+            return backend[0]
c8359d
+        else:
c8359d
+            die(f"Unsupported librnp backend {backend[0]}.")
c8359d
+
c8359d
+    set_config("MZLA_LIBRNP_BACKEND", librnp_backend)
c8359d
+
c8359d
+    @depends(librnp_backend)
c8359d
+    def rnp_botan(backend):
c8359d
+        return backend == "botan"
c8359d
+
c8359d
+    @depends(librnp_backend)
c8359d
+    def rnp_openssl(backend):
c8359d
+        return backend == "openssl"
c8359d
+
c8359d
+    # Botan backend (--with-system-botan)
c8359d
+    with only_when(rnp_botan):
c8359d
+        system_lib_option(
c8359d
+            "--with-system-botan",
c8359d
+            help="Use system Botan for librnp (located with pkgconfig)",
c8359d
+        )
c8359d
+
c8359d
+        botan_pkg = pkg_check_modules(
c8359d
+            "MZLA_BOTAN", "botan-2 >= 2.8.0", when="--with-system-botan"
c8359d
+        )
c8359d
+        set_config("MZLA_SYSTEM_BOTAN", depends_if(botan_pkg)(lambda _: True))
c8359d
+
c8359d
+
c8359d
+    # OpenSSL backend
c8359d
+    with only_when(rnp_openssl):
c8359d
+        option(
c8359d
+            "--with-openssl",
c8359d
+            nargs=1,
c8359d
+            help="OpenSSL library prefix (when not found by pkgconfig)"
c8359d
+        )
c8359d
+        openssl_pkg = pkg_check_modules(
c8359d
+            "MZLA_LIBRNP_OPENSSL",
c8359d
+            "openssl > 1.1.1",
c8359d
+            allow_missing=True,
c8359d
+            config=False
c8359d
+        )
c8359d
+        @depends_if("--with-openssl", openssl_pkg)
c8359d
+        @imports(_from="os.path", _import="isdir")
c8359d
+        @imports(_from="os.path", _import="join")
c8359d
+        def openssl_flags(openssl_prefix, openssl_pkg):
c8359d
+            if openssl_prefix:
c8359d
+                openssl_prefix = openssl_prefix[0]
c8359d
+                include = join(openssl_prefix, "include")
c8359d
+                lib = join(openssl_prefix, "lib")
c8359d
+                if not isdir(lib):
c8359d
+                    lib = join(openssl_prefix, "lib64")
c8359d
+                if isdir(include) and isdir(lib):
c8359d
+                    log.info(f"Using OpenSSL at {openssl_prefix}.")
c8359d
+                    return namespace(
c8359d
+                        cflags=(f"-I{include}",),
c8359d
+                        ldflags=(f"-L{lib}", "-lssl", "-lcrypto"),
c8359d
+                    )
c8359d
+            if openssl_pkg:
c8359d
+                return namespace(
c8359d
+                    cflags=openssl_pkg.cflags,
c8359d
+                    ldflags=openssl_pkg.libs,
c8359d
+                )
c8359d
+        set_config("MZLA_LIBRNP_OPENSSL_CFLAGS", openssl_flags.cflags)
c8359d
+        set_config("MZLA_LIBRNP_OPENSSL_LIBS", openssl_flags.ldflags)
c8359d
+
c8359d
+
c8359d
+        @depends(c_compiler, openssl_flags)
c8359d
+        @imports(_from="textwrap", _import="dedent")
c8359d
+        def openssl_version(compiler, openssl_flags):
c8359d
+            log.info("Checking for OpenSSL >= 1.1.1")
c8359d
+            if openssl_flags is None:
c8359d
+                die("OpenSSL not found. Must be locatable with pkg-config or use --with-openssl.")
c8359d
+
c8359d
+            def ossl_hexver(hex_str):
c8359d
+                # See opensshlv.h for description of OPENSSL_VERSION_NUMBER
c8359d
+                MIN_OSSL_VER = 0x1010100f  # Version 1.1.1
c8359d
+                ver_as_int = int(hex_str[:-1], 16)
c8359d
+                ossl_major = (ver_as_int & 0xf0000000) >> 28
c8359d
+                ossl_minor = (ver_as_int & 0x0ff00000) >> 20
c8359d
+                ossl_fix = (ver_as_int & 0x000ff000) >> 12
c8359d
+                ossl_patch = chr(96 + (ver_as_int & 0x00000ff0) >> 4)  # as a letter a-z
c8359d
+                ver_as_str = f"{ossl_major}.{ossl_minor}.{ossl_fix}{ossl_patch}"
c8359d
+                if ver_as_int < MIN_OSSL_VER:
c8359d
+                    die(f"OpenSSL version {ver_as_str} is too old.")
c8359d
+                return ver_as_str
c8359d
+
c8359d
+            check = dedent(
c8359d
+                """\
c8359d
+            #include <openssl/opensslv.h>
c8359d
+            #ifdef OPENSSL_VERSION_STR
c8359d
+            OPENSSL_VERSION_STR
c8359d
+            #elif defined(OPENSSL_VERSION_NUMBER)
c8359d
+            OPENSSL_VERSION_NUMBER
c8359d
+            #else
c8359d
+            #error Unable to determine OpenSSL version.
c8359d
+            #endif
c8359d
+                """
c8359d
+            )
c8359d
+            result = try_preprocess(
c8359d
+                compiler.wrapper
c8359d
+                + [compiler.compiler]
c8359d
+                + compiler.flags
c8359d
+                + list(openssl_flags.cflags),
c8359d
+                "C",
c8359d
+                check
c8359d
+            )
c8359d
+            if result:
c8359d
+                openssl_ver = result.splitlines()[-1]
c8359d
+                if openssl_ver.startswith("0x"):
c8359d
+                    # OpenSSL 1.x.x - like 0x1010107fL
c8359d
+                    openssl_ver = ossl_hexver(openssl_ver)
c8359d
+                else:
c8359d
+                    # OpenSSL 3.x.x - quoted version like "3.0.7"
c8359d
+                    openssl_ver = openssl_ver.replace('"', "")
c8359d
+                    major_version = openssl_ver.split(".")[0]
c8359d
+                    if major_version != "3":
c8359d
+                        die("Unrecognized OpenSSL version {openssl_version} found. Require >= 1.1.1 or 3.x.x")
c8359d
+
c8359d
+                log.info(f"Found OpenSSL {openssl_ver}.")
c8359d
+                return openssl_ver
c8359d
+
c8359d
+        set_config("MZLA_LIBRNP_OPENSSL_VERSION", openssl_version)
c8359d
 
c8359d
     # Checks for building librnp itself
c8359d
     # =================================
c8359d
diff -up comm/third_party/rnp/moz.build.D161379.diff comm/third_party/rnp/moz.build
c8359d
--- comm/third_party/rnp/moz.build.D161379.diff	2022-11-10 11:49:43.682017534 +0100
c8359d
+++ comm/third_party/rnp/moz.build	2022-11-10 11:51:22.878909880 +0100
c8359d
@@ -36,17 +36,53 @@ if CONFIG["CC_TYPE"] == "clang-cl":
c8359d
         "/EHs",
c8359d
     ]
c8359d
 
c8359d
+LOCAL_INCLUDES = [
c8359d
+    "include",
c8359d
+    "src",
c8359d
+    "src/common",
c8359d
+    "src/lib",
c8359d
+]
c8359d
+
c8359d
+IQuote(
c8359d
+    "{}/src/lib".format(OBJDIR),
c8359d
+    "{}/src/lib".format(SRCDIR),
c8359d
+)
c8359d
+
c8359d
+# Set up defines for src/lib/config.h
c8359d
 rnp_defines = {
c8359d
     "HAVE_BZLIB_H": True,
c8359d
     "HAVE_ZLIB_H": True,
c8359d
-    "CRYPTO_BACKEND_OPENSSL": True,
c8359d
-    "ENABLE_AEAD": True,
c8359d
-    "ENABLE_TWOFISH": True,
c8359d
-    "ENABLE_BRAINPOOL": True,
c8359d
     "ENABLE_IDEA": True,
c8359d
     "PACKAGE_BUGREPORT": '"https://bugzilla.mozilla.org/enter_bug.cgi?product=Thunderbird"',
c8359d
     "PACKAGE_STRING": '"rnp {}"'.format(CONFIG["MZLA_LIBRNP_FULL_VERSION"])
c8359d
 }
c8359d
+if CONFIG["MZLA_LIBRNP_BACKEND"] == "botan":
c8359d
+    LOCAL_INCLUDES += ["!../botan/build/include"]
c8359d
+    if CONFIG["MZLA_SYSTEM_BOTAN"]:
c8359d
+        CXXFLAGS += CONFIG["MZLA_BOTAN_CFLAGS"]
c8359d
+
c8359d
+    rnp_defines.update({
c8359d
+        "CRYPTO_BACKEND_BOTAN": True,
c8359d
+        "ENABLE_AEAD": True,
c8359d
+        "ENABLE_TWOFISH": True,
c8359d
+        "ENABLE_BRAINPOOL": True,
c8359d
+    })
c8359d
+elif CONFIG["MZLA_LIBRNP_BACKEND"] == "openssl":
c8359d
+    CXXFLAGS += CONFIG["MZLA_LIBRNP_OPENSSL_CFLAGS"]
c8359d
+    OS_LIBS += CONFIG["MZLA_LIBRNP_OPENSSL_LIBS"]
c8359d
+
c8359d
+    rnp_defines.update({
c8359d
+        "CRYPTO_BACKEND_OPENSSL": True,
c8359d
+        # Not supported with RNP+OpenSSL https://github.com/rnpgp/rnp/issues/1642
c8359d
+        "ENABLE_AEAD": False,
c8359d
+        # Not supported by OpenSSL https://github.com/openssl/openssl/issues/2046
c8359d
+        "ENABLE_TWOFISH": False,
c8359d
+        # Supported, but not with RHEL's OpenSSL, disabled for now;
c8359d
+        "ENABLE_BRAINPOOL": False,
c8359d
+    })
c8359d
+    if CONFIG["MZLA_LIBRNP_OPENSSL_VERSION"][0] == "3":
c8359d
+        rnp_defines["CRYPTO_BACKEND_OPENSSL3"] = True
c8359d
+
c8359d
 GeneratedFile(
c8359d
     "src/lib/config.h",
c8359d
     script="/comm/python/rocbuild/process_cmake_define_files.py",
c8359d
@@ -57,23 +93,6 @@ GeneratedFile(
c8359d
     ],
c8359d
 )
c8359d
 
c8359d
-LOCAL_INCLUDES = [
c8359d
-    "include",
c8359d
-    "src",
c8359d
-    "src/common",
c8359d
-    "src/lib",
c8359d
-]
c8359d
-
c8359d
-IQuote(
c8359d
-    "{}/src/lib".format(OBJDIR),
c8359d
-    "{}/src/lib".format(SRCDIR),
c8359d
-)
c8359d
-
c8359d
-if CONFIG["MZLA_SYSTEM_BOTAN"]:
c8359d
-    CXXFLAGS += CONFIG["MZLA_BOTAN_CFLAGS"]
c8359d
-else:
c8359d
-    LOCAL_INCLUDES += ["!../botan/build/include"]
c8359d
-
c8359d
 if CONFIG["MOZ_SYSTEM_ZLIB"]:
c8359d
     CXXFLAGS += CONFIG["MOZ_ZLIB_CFLAGS"]
c8359d
 else:
c8359d
@@ -109,29 +128,16 @@ SOURCES += [
c8359d
     "src/common/time-utils.cpp",
c8359d
     "src/lib/crypto.cpp",
c8359d
     "src/lib/crypto/backend_version.cpp",
c8359d
-    "src/lib/crypto/bn.cpp",
c8359d
     "src/lib/crypto/cipher.cpp",
c8359d
-    "src/lib/crypto/cipher_botan.cpp",
c8359d
-    "src/lib/crypto/dsa.cpp",
c8359d
-    "src/lib/crypto/ec.cpp",
c8359d
     "src/lib/crypto/ec_curves.cpp",
c8359d
-    "src/lib/crypto/ecdh.cpp",
c8359d
     "src/lib/crypto/ecdh_utils.cpp",
c8359d
-    "src/lib/crypto/ecdsa.cpp",
c8359d
-    "src/lib/crypto/eddsa.cpp",
c8359d
-    "src/lib/crypto/elgamal.cpp",
c8359d
-    "src/lib/crypto/hash.cpp",
c8359d
     "src/lib/crypto/hash_common.cpp",
c8359d
     "src/lib/crypto/hash_sha1cd.cpp",
c8359d
-    "src/lib/crypto/mem.cpp",
c8359d
     "src/lib/crypto/mpi.cpp",
c8359d
-    "src/lib/crypto/rng.cpp",
c8359d
-    "src/lib/crypto/rsa.cpp",
c8359d
     "src/lib/crypto/s2k.cpp",
c8359d
     "src/lib/crypto/sha1cd/sha1.c",
c8359d
     "src/lib/crypto/sha1cd/ubc_check.c",
c8359d
     "src/lib/crypto/signatures.cpp",
c8359d
-    "src/lib/crypto/symmetric.cpp",
c8359d
     "src/lib/fingerprint.cpp",
c8359d
     "src/lib/generate-key.cpp",
c8359d
     "src/lib/json-utils.cpp",
c8359d
@@ -159,4 +165,40 @@ SOURCES += [
c8359d
     "src/librepgp/stream-write.cpp",
c8359d
 ]
c8359d
 
c8359d
+if CONFIG["MZLA_LIBRNP_BACKEND"] == "botan":
c8359d
+    SOURCES += [
c8359d
+        "src/lib/crypto/bn.cpp",
c8359d
+        "src/lib/crypto/cipher_botan.cpp",
c8359d
+        "src/lib/crypto/dsa.cpp",
c8359d
+        "src/lib/crypto/ec.cpp",
c8359d
+        "src/lib/crypto/ecdh.cpp",
c8359d
+        "src/lib/crypto/ecdsa.cpp",
c8359d
+        "src/lib/crypto/eddsa.cpp",
c8359d
+        "src/lib/crypto/elgamal.cpp",
c8359d
+        "src/lib/crypto/hash.cpp",
c8359d
+        "src/lib/crypto/mem.cpp",
c8359d
+        "src/lib/crypto/rng.cpp",
c8359d
+        "src/lib/crypto/rsa.cpp",
c8359d
+        "src/lib/crypto/symmetric.cpp",
c8359d
+    ]
c8359d
+if CONFIG["MZLA_LIBRNP_BACKEND"] == "openssl":
c8359d
+    SOURCES += [
c8359d
+        "src/lib/crypto/bn_ossl.cpp",
c8359d
+        "src/lib/crypto/cipher_ossl.cpp",
c8359d
+        "src/lib/crypto/dl_ossl.cpp",
c8359d
+        "src/lib/crypto/dsa_ossl.cpp",
c8359d
+        "src/lib/crypto/ec_ossl.cpp",
c8359d
+        "src/lib/crypto/ecdh_ossl.cpp",
c8359d
+        "src/lib/crypto/ecdsa_ossl.cpp",
c8359d
+        "src/lib/crypto/eddsa_ossl.cpp",
c8359d
+        "src/lib/crypto/elgamal_ossl.cpp",
c8359d
+        "src/lib/crypto/hash_crc24.cpp",
c8359d
+        "src/lib/crypto/hash_ossl.cpp",
c8359d
+        "src/lib/crypto/mem_ossl.cpp",
c8359d
+        "src/lib/crypto/rng_ossl.cpp",
c8359d
+        "src/lib/crypto/rsa_ossl.cpp",
c8359d
+        "src/lib/crypto/s2k_ossl.cpp",
c8359d
+        "src/lib/crypto/symmetric_ossl.cpp",
c8359d
+    ]
c8359d
+
c8359d
 DIRS += ["src/rnp", "src/rnpkeys"]