From d01e7abd0ebffb19bc753296c45306ba3e1c014f Mon Sep 17 00:00:00 2001
From: morhaviv <61060270+morhaviv@users.noreply.github.com>
Date: Sun, 15 Nov 2026 21:41:53 +0200
Subject: [PATCH] feat(settings): optional PKCS#12 client certificate
management
Offer an app-managed certificate for existing HTTPS accounts while
retaining system KeyChain fallback. Guard against absent accounts and
clear the password entry after import.
Assisted-by: ChatGPT:GPT-6
---
app/src/main/AndroidManifest.xml | 4 +
.../ManualClientCertificateActivity.java | 219 ++++++++++++++++++
.../android/ui/activity/SettingsActivity.java | 12 +
app/src/main/res/values/strings.xml | 13 ++
app/src/main/res/xml/preferences.xml | 4 +
5 files changed, 252 insertions(+)
create mode 100644 app/src/main/java/com/owncloud/android/ui/activity/ManualClientCertificateActivity.java
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 8014d33ba07c..7443f879285a 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -380,6 +380,10 @@
android:enableOnBackInvokedCallback="false"
android:exported="false"
android:theme="@style/PreferenceTheme" />
+
selectCertificate());
+ layout.addView(importButton);
+
+ removeButton = new Button(this);
+ removeButton.setText(R.string.mtls_remove);
+ removeButton.setOnClickListener(view -> new MaterialAlertDialogBuilder(this)
+ .setTitle(R.string.mtls_remove)
+ .setNegativeButton(android.R.string.cancel, null)
+ .setPositiveButton(R.string.mtls_remove, (dialog, which) -> removeCertificate())
+ .show());
+ layout.addView(removeButton);
+
+ ViewCompat.setOnApplyWindowInsetsListener(root, (view, insets) -> {
+ Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
+ view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
+ return insets;
+ });
+ setContentView(root);
+ setSupportActionBar(toolbar);
+ ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ actionBar.setTitle(R.string.mtls_title);
+ actionBar.setDisplayHomeAsUpEnabled(true);
+ }
+ ViewCompat.requestApplyInsets(root);
+ updateStatus();
+ }
+
+ @Override
+ public boolean onSupportNavigateUp() {
+ finish();
+ return true;
+ }
+
+ private void updateStatus() {
+ boolean configured = ManualClientCertificateStore.isConfigured(this, host, port);
+ status.setText(configured ? R.string.mtls_status_imported : R.string.mtls_status_keychain);
+ removeButton.setEnabled(configured);
+ }
+
+ private void selectCertificate() {
+ Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
+ intent.setType("*/*");
+ startActivityForResult(intent, REQUEST_CERTIFICATE);
+ }
+
+ @Override
+ protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
+ super.onActivityResult(requestCode, resultCode, data);
+ if (requestCode != REQUEST_CERTIFICATE || resultCode != Activity.RESULT_OK ||
+ data == null || data.getData() == null) {
+ return;
+ }
+ Uri selected = data.getData();
+ EditText passwordInput = new EditText(this);
+ passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
+ passwordInput.setHint(R.string.mtls_password);
+ new MaterialAlertDialogBuilder(this)
+ .setTitle(R.string.mtls_password)
+ .setView(passwordInput)
+ .setNegativeButton(android.R.string.cancel, null)
+ .setPositiveButton(R.string.mtls_import, (dialog, which) -> {
+ char[] password = passwordInput.getText().toString().toCharArray();
+ passwordInput.getText().clear();
+ new Thread(() -> importCertificate(selected, password)).start();
+ })
+ .show();
+ }
+
+ private void importCertificate(Uri selected, char[] password) {
+ byte[] contents = null;
+ boolean success = false;
+ try (InputStream input = getContentResolver().openInputStream(selected)) {
+ if (input == null) {
+ throw new IllegalArgumentException("Certificate file could not be opened");
+ }
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] buffer = new byte[8192];
+ int length;
+ while ((length = input.read(buffer)) != -1) {
+ if (output.size() + length > MAX_IMPORT_BYTES) {
+ throw new IllegalArgumentException("Certificate file is too large");
+ }
+ output.write(buffer, 0, length);
+ }
+ contents = output.toByteArray();
+ ManualClientCertificateStore.importCertificate(getApplicationContext(), host, port,
+ contents, password);
+ success = true;
+ } catch (Exception ignored) {
+ // Report a generic message; never expose certificate or filesystem details in the UI.
+ } finally {
+ Arrays.fill(password, '\0');
+ if (contents != null) {
+ Arrays.fill(contents, (byte) 0);
+ }
+ }
+ boolean imported = success;
+ runOnUiThread(() -> {
+ Toast.makeText(this, imported ? R.string.mtls_import_success : R.string.mtls_import_failed,
+ Toast.LENGTH_LONG).show();
+ updateStatus();
+ });
+ }
+
+ private void removeCertificate() {
+ new Thread(() -> {
+ try {
+ ManualClientCertificateStore.remove(getApplicationContext(), host, port);
+ runOnUiThread(() -> {
+ updateStatus();
+ Toast.makeText(this, R.string.mtls_remove_success, Toast.LENGTH_SHORT).show();
+ });
+ } catch (Exception ignored) {
+ runOnUiThread(() -> Toast.makeText(this, R.string.mtls_remove_failed,
+ Toast.LENGTH_LONG).show());
+ }
+ }).start();
+ }
+}
diff --git a/app/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java b/app/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java
index 92a192af45c9..6a4401993ba3 100644
--- a/app/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java
+++ b/app/src/main/java/com/owncloud/android/ui/activity/SettingsActivity.java
@@ -385,6 +385,8 @@ private void setupMoreCategory() {
setupRenewE2ECertificate(preferenceCategoryMore);
+ setupManualClientCertificatePreference();
+
setupHelpPreference(preferenceCategoryMore);
setupRecommendPreference(preferenceCategoryMore);
@@ -396,6 +398,16 @@ private void setupMoreCategory() {
loadExternalSettingLinks(preferenceCategoryMore);
}
+ private void setupManualClientCertificatePreference() {
+ Preference preference = findPreference("manual_client_certificate");
+ if (preference != null) {
+ preference.setOnPreferenceClickListener(clicked -> {
+ startActivity(new Intent(this, ManualClientCertificateActivity.class));
+ return true;
+ });
+ }
+ }
+
private void setupImprintPreference(PreferenceCategory preferenceCategoryMore) {
boolean imprintEnabled = getResources().getBoolean(R.bool.imprint_enabled);
Preference pImprint = findPreference("imprint");
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index f3d6f60bf3e1..958e431d31fe 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1647,4 +1647,17 @@
Random button
Server not allowed
Run sync now
+ Client certificate
+ Import a PKCS#12 certificate for this server
+ A manually imported certificate will be used for connections to %1$s:%2$d only. The private key is encrypted in app storage.
+ Import PKCS#12
+ PKCS#12 password
+ Remove client certificate
+ A manual client certificate is configured.
+ No manual certificate. Android KeyChain is used.
+ Client certificate imported. Reconnect to use it.
+ Import failed. Check the certificate and password.
+ Manual certificate removed
+ Could not remove manual certificate
+ An existing HTTPS account is required.
diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml
index aa73c18f2f11..5d0467f0b2ef 100644
--- a/app/src/main/res/xml/preferences.xml
+++ b/app/src/main/res/xml/preferences.xml
@@ -120,6 +120,10 @@
android:title="@string/prefs_renew_e2e_certificate"
android:key="renew_e2e_certificate" />
+