From 516802f79648a8583826dbec24346c725279d414 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Wed, 14 Dec 2022 11:51:20 +0000 Subject: [PATCH 1/2] make: Add Patch Trichrome script * inspired form calyx and https://github.com/danielfullmer/robotnix/blob/master/modules/apps/chromium-trichrome-patcher.py --- core/app_prebuilt_internal.mk | 1 + core/config.mk | 2 ++ core/definitions.mk | 5 ++++ tools/chromium_trichrome_patcher.py | 36 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100755 tools/chromium_trichrome_patcher.py diff --git a/core/app_prebuilt_internal.mk b/core/app_prebuilt_internal.mk index ab574b3fa4..238d3e7925 100644 --- a/core/app_prebuilt_internal.mk +++ b/core/app_prebuilt_internal.mk @@ -245,6 +245,7 @@ ifeq ($(module_run_appcompat),true) $(call appcompat-header, aapt2) $(run-appcompat) endif # module_run_appcompat + $(patch-trichrome) $(sign-package) # No need for align-package because sign-package takes care of alignment else # LOCAL_CERTIFICATE == PRESIGNED diff --git a/core/config.mk b/core/config.mk index c9efa116fc..a27d842881 100644 --- a/core/config.mk +++ b/core/config.mk @@ -669,6 +669,8 @@ JETIFIER := prebuilts/sdk/tools/jetifier/jetifier-standalone/bin/jetifier-standa EXTRACT_KERNEL := build/make/tools/extract_kernel.py +PATCH_TRICHROME := build/make/tools/chromium_trichrome_patcher.py + # Path to tools.jar HOST_JDK_TOOLS_JAR := $(ANDROID_JAVA8_HOME)/lib/tools.jar diff --git a/core/definitions.mk b/core/definitions.mk index 89c2e276a3..c6af6df9cb 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -3342,3 +3342,8 @@ define use_soong_sdk_libraries $(foreach l,$(1),$(if $(filter $(l),$(SOONG_SDK_VARIANT_MODULES)),\ $(l).sdk,$(l))) endef + +# Patch Trichrome to add cert digest at buildtime +define patch-trichrome +$(hide) $(PATCH_TRICHROME) $@ $(PRIVATE_CERTIFICATE) +endef diff --git a/tools/chromium_trichrome_patcher.py b/tools/chromium_trichrome_patcher.py new file mode 100755 index 0000000000..06cdea0448 --- /dev/null +++ b/tools/chromium_trichrome_patcher.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import os +import subprocess +import sys +import zipfile + +infilename, sign_key = sys.argv[1:] + +def ExtractFingerprint(cert): + cmd = ['openssl', 'x509', '-sha256', '-fingerprint', '-noout', '-in', cert] + proc = subprocess.run(cmd, stdout=subprocess.PIPE) + return proc.stdout.decode('utf-8').split('=')[1].replace(':', '') + +def patch_trichrome(infilename, sign_key): + orig_certdigest = "32a2fc74d731105859e5a85df16d95f102d85b22099b8064c5d8915c61dad1e0" + new_certdigest = ExtractFingerprint(sign_key).lower().rstrip() + + with zipfile.ZipFile(infilename, 'r') as zin, zipfile.ZipFile(infilename + ".patched", 'w') as zout: + for info in zin.infolist(): + data = zin.read(info.filename) + if info.filename == 'AndroidManifest.xml': + # Make sure we can find the certdigest + data.rindex(orig_certdigest.encode('utf-16-le')) + # Replace it + data = data.replace(orig_certdigest.encode('utf-16-le'), new_certdigest.encode('utf-16-le')) + zout.writestr(info, data) + + # Delete the original file + os.remove(infilename) + + # Rename the output file to the original file name + os.rename(infilename + ".patched", infilename) + +if "Browser_" in infilename or "BrowserWebView_" in infilename: + patch_trichrome(infilename, sign_key) -- GitLab From 92b2bc5d41a42d302f0561585ebd33b62b8e06e0 Mon Sep 17 00:00:00 2001 From: althafvly Date: Wed, 21 Dec 2022 15:43:03 +0530 Subject: [PATCH 2/2] make: Catch exception for no certdigest - Fixes build for non-trichrome browser apks --- tools/chromium_trichrome_patcher.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/chromium_trichrome_patcher.py b/tools/chromium_trichrome_patcher.py index 06cdea0448..bda96b3fa6 100755 --- a/tools/chromium_trichrome_patcher.py +++ b/tools/chromium_trichrome_patcher.py @@ -21,7 +21,10 @@ def patch_trichrome(infilename, sign_key): data = zin.read(info.filename) if info.filename == 'AndroidManifest.xml': # Make sure we can find the certdigest - data.rindex(orig_certdigest.encode('utf-16-le')) + try: + data.rindex(orig_certdigest.encode('utf-16-le')) + except: + pass # Replace it data = data.replace(orig_certdigest.encode('utf-16-le'), new_certdigest.encode('utf-16-le')) zout.writestr(info, data) -- GitLab