From c94f2ceaca7253170da2a098e8e804ca991b1ed5 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 14 Aug 2021 10:22:38 +0200 Subject: [PATCH] Refactoring --- .../eu/faircode/email/ConnectionHelper.java | 30 ------------------- .../java/eu/faircode/email/EmailProvider.java | 2 +- .../java/eu/faircode/email/EmailService.java | 2 +- .../eu/faircode/email/EntityCertificate.java | 29 ++++++++++++++++++ 4 files changed, 31 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ConnectionHelper.java b/app/src/main/java/eu/faircode/email/ConnectionHelper.java index 14b6499177..1fb2d68a23 100644 --- a/app/src/main/java/eu/faircode/email/ConnectionHelper.java +++ b/app/src/main/java/eu/faircode/email/ConnectionHelper.java @@ -29,7 +29,6 @@ import android.net.NetworkInfo; import android.os.Build; import android.provider.Settings; import android.telephony.TelephonyManager; -import android.text.TextUtils; import androidx.annotation.Nullable; import androidx.preference.PreferenceManager; @@ -457,33 +456,4 @@ public class ConnectionHelper { return Settings.Global.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } - - static boolean matches(String server, List names) { - for (String name : names) - if (matches(server, name)) { - Log.i("Trusted server=" + server + " name=" + name); - return true; - } - return false; - } - - private static boolean matches(String server, String name) { - if (name.startsWith("*.")) { - // Wildcard certificate - String domain = name.substring(2); - if (TextUtils.isEmpty(domain)) - return false; - - int dot = server.indexOf("."); - if (dot < 0) - return false; - - String cdomain = server.substring(dot + 1); - if (TextUtils.isEmpty(cdomain)) - return false; - - return domain.equalsIgnoreCase(cdomain); - } else - return server.equalsIgnoreCase(name); - } } diff --git a/app/src/main/java/eu/faircode/email/EmailProvider.java b/app/src/main/java/eu/faircode/email/EmailProvider.java index 7c5c206d65..9c6791929c 100644 --- a/app/src/main/java/eu/faircode/email/EmailProvider.java +++ b/app/src/main/java/eu/faircode/email/EmailProvider.java @@ -870,7 +870,7 @@ public class EmailProvider implements Parcelable { List names = EntityCertificate.getDnsNames((X509Certificate) cert); EntityLog.log(context, "Certificate " + address + " " + TextUtils.join(",", names)); - if (ConnectionHelper.matches(host, names)) { + if (EntityCertificate.matches(host, names)) { EntityLog.log(context, "Trusted " + address); return true; } diff --git a/app/src/main/java/eu/faircode/email/EmailService.java b/app/src/main/java/eu/faircode/email/EmailService.java index 1a05f34a46..e72f1e6a6c 100644 --- a/app/src/main/java/eu/faircode/email/EmailService.java +++ b/app/src/main/java/eu/faircode/email/EmailService.java @@ -849,7 +849,7 @@ public class EmailService implements AutoCloseable { // Check host name List names = EntityCertificate.getDnsNames(certificate); - if (ConnectionHelper.matches(server, names)) + if (EntityCertificate.matches(server, names)) return; String error = server + " not in certificate: " + TextUtils.join(",", names); diff --git a/app/src/main/java/eu/faircode/email/EntityCertificate.java b/app/src/main/java/eu/faircode/email/EntityCertificate.java index da3763a25f..a684b230f7 100644 --- a/app/src/main/java/eu/faircode/email/EntityCertificate.java +++ b/app/src/main/java/eu/faircode/email/EntityCertificate.java @@ -19,6 +19,7 @@ package eu.faircode.email; Copyright 2018-2021 by Marcel Bokhorst (M66B) */ +import android.text.TextUtils; import android.util.Base64; import androidx.annotation.NonNull; @@ -215,6 +216,34 @@ public class EntityCertificate { return result; } + static boolean matches(String server, List names) { + for (String name : names) + if (matches(server, name)) { + Log.i("Trusted server=" + server + " name=" + name); + return true; + } + return false; + } + + private static boolean matches(String server, String name) { + if (name.startsWith("*.")) { + // Wildcard certificate + String domain = name.substring(2); + if (TextUtils.isEmpty(domain)) + return false; + + int dot = server.indexOf("."); + if (dot < 0) + return false; + + String cdomain = server.substring(dot + 1); + if (TextUtils.isEmpty(cdomain)) + return false; + + return domain.equalsIgnoreCase(cdomain); + } else + return server.equalsIgnoreCase(name); + } public JSONObject toJSON() throws JSONException { JSONObject json = new JSONObject();