Protocol/cipher debugging

This commit is contained in:
M66B 2022-02-22 19:19:26 +01:00
parent fc6b36a5e8
commit 3bf3e16954
2 changed files with 69 additions and 43 deletions

View File

@ -23,7 +23,6 @@ import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@ -42,7 +41,6 @@ import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.util.Pair;
import android.view.LayoutInflater;
@ -70,7 +68,6 @@ import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.Group;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.preference.PreferenceManager;
import java.io.File;
@ -79,7 +76,6 @@ import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
@ -88,9 +84,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import io.requery.android.database.sqlite.SQLiteDatabase;
public class FragmentOptionsMisc extends FragmentBase implements SharedPreferences.OnSharedPreferenceChangeListener {
@ -1165,45 +1158,10 @@ public class FragmentOptionsMisc extends FragmentBase implements SharedPreferenc
btnCiphers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SpannableStringBuilder ssb = new SpannableStringBuilderEx();
try {
SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
List<String> protocols = new ArrayList<>();
protocols.addAll(Arrays.asList(socket.getEnabledProtocols()));
List<String> ciphers = new ArrayList<>();
ciphers.addAll(Arrays.asList(socket.getEnabledCipherSuites()));
for (String p : socket.getSupportedProtocols()) {
boolean enabled = protocols.contains(p);
int start = ssb.length();
ssb.append(p);
if (!enabled)
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), 0);
ssb.append("\r\n");
}
ssb.append("\r\n");
for (String c : socket.getSupportedCipherSuites()) {
boolean enabled = ciphers.contains(c);
int start = ssb.length();
ssb.append(c);
if (!enabled)
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), 0);
ssb.append("\r\n");
}
ssb.append("\r\n");
} catch (IOException ex) {
ssb.append(ex.toString());
}
ssb.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), 0, ssb.length(), 0);
new AlertDialog.Builder(getContext())
.setIcon(R.drawable.twotone_info_24)
.setTitle(R.string.title_advanced_ciphers)
.setMessage(ssb)
.setMessage(Log.getCiphers())
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

View File

@ -39,6 +39,7 @@ import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.sqlite.SQLiteFullException;
import android.graphics.Point;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.LinkProperties;
import android.net.Network;
@ -55,7 +56,11 @@ import android.os.PowerManager;
import android.os.RemoteException;
import android.os.TransactionTooLargeException;
import android.provider.Settings;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.util.Printer;
import android.view.Display;
import android.view.InflateException;
@ -104,10 +109,12 @@ import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.security.cert.CertPathValidatorException;
@ -137,8 +144,11 @@ import javax.mail.Part;
import javax.mail.StoreClosedException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
import io.requery.android.database.CursorWindowAllocationException;
@ -2283,6 +2293,9 @@ public class Log {
size += write(os, "VPN active=" + ConnectionHelper.vpnActive(context) + "\r\n");
size += write(os, "Data saving=" + ConnectionHelper.isDataSaving(context) + "\r\n");
size += write(os, "Airplane=" + ConnectionHelper.airplaneMode(context) + "\r\n");
size += write(os, "\r\n");
size += write(os, getCiphers().toString());
}
db.attachment().setDownloaded(attachment.id, size);
@ -2682,6 +2695,61 @@ public class Log {
db.attachment().setDownloaded(attachment.id, target.length());
}
static SpannableStringBuilder getCiphers() {
SpannableStringBuilder ssb = new SpannableStringBuilderEx();
for (String protocol : new String[]{"SSL", "TLS"}) {
try {
int begin = ssb.length();
ssb.append(protocol).append("\n\n");
ssb.setSpan(new StyleSpan(Typeface.BOLD), begin, ssb.length(), 0);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
SSLContext sslContext = SSLContext.getInstance(protocol);
sslContext.init(null, tmf.getTrustManagers(), null);
SSLSocket socket = (SSLSocket) sslContext.getSocketFactory().createSocket();
List<String> protocols = new ArrayList<>();
protocols.addAll(Arrays.asList(socket.getEnabledProtocols()));
for (String p : socket.getSupportedProtocols()) {
boolean enabled = protocols.contains(p);
int start = ssb.length();
ssb.append(p);
if (!enabled)
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), 0);
ssb.append("\r\n");
}
ssb.append("\r\n");
List<String> ciphers = new ArrayList<>();
ciphers.addAll(Arrays.asList(socket.getEnabledCipherSuites()));
for (String c : socket.getSupportedCipherSuites()) {
boolean enabled = ciphers.contains(c);
if (!enabled)
ssb.append('(');
int start = ssb.length();
ssb.append(c);
if (!enabled) {
ssb.setSpan(new StrikethroughSpan(), start, ssb.length(), 0);
ssb.append(')');
}
ssb.append("\r\n");
}
ssb.append("\r\n");
} catch (Throwable ex) {
ssb.append(ex.toString());
}
}
ssb.setSpan(new RelativeSizeSpan(HtmlHelper.FONT_SMALL), 0, ssb.length(), 0);
return ssb;
}
private static int write(OutputStream os, String text) throws IOException {
byte[] bytes = text.getBytes();
os.write(bytes);