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