Refactoring

pull/203/head
M66B 3 years ago
parent 823443fe69
commit c94f2ceaca

@ -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<String> 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);
}
}

@ -870,7 +870,7 @@ public class EmailProvider implements Parcelable {
List<String> 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;
}

@ -849,7 +849,7 @@ public class EmailService implements AutoCloseable {
// Check host name
List<String> names = EntityCertificate.getDnsNames(certificate);
if (ConnectionHelper.matches(server, names))
if (EntityCertificate.matches(server, names))
return;
String error = server + " not in certificate: " + TextUtils.join(",", names);

@ -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<String> 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();

Loading…
Cancel
Save