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