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