since the migration from Android 13 to Android 14, I am no longer able to connect from Android to a local box containing its own certificate.
This is below my code :
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket soc = (SSLSocket) factory.createSocket();
String[] protocols = soc.getEnabledProtocols();
System.out.println("BEFORE Enabled protocols:");
for (String s : protocols) {
System.out.println(s);
}
store = KeyStore.getInstance(KeyStore.getDefaultType());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
tmf.init(store);
// context = SSLContext.getInstance("TLS");
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
context = SSLContext.getInstance("TLSv1");
context.init(null, trustAllCerts, new java.security.SecureRandom());
soc= (SSLSocket)context.getSocketFactory().createSocket();
protocols = soc.getEnabledProtocols();
System.out.println("After Enabled protocols:");
for (String s : protocols) {
System.out.println(s);
}
ArrayList enabled = new ArrayList();
String[] supported;
System.out.println("Enabled CipherSuites:");
supported = soc.getSupportedCipherSuites();
for (String s : supported) {
System.out.println(s);
enabled.add(s);
}
String[] newSupported = (String[]) enabled.toArray(new String[0]);
socket = (SSLSocket)context.getSocketFactory().createSocket(host, port);
((SSLSocket)socket).setEnabledCipherSuites(newSupported);
((SSLSocket) socket).setEnabledProtocols(new String[] {"TLSv1"} );
input = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
output = new DataOutputStream(socket.getOutputStream());
out = new PrintWriter(
new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream(), "UTF-8")), true);
On Android 13 it works, but on Android 14 I get SSLHandshakeException SSL routines:OPENSSL_internal:KEY_USAGE_BIT_INCORRECT
It seems the problem is with the self-signed certificate.
Looking on the web I saw that it is now necessary to import the certificates into the App User Certificate, but I am not able to do it because I do not have access to the private key of this certificate, because it belongs to the local box.
Has anyone found a workaround for Android 14?
Thank you for your help, I’ve been looking for a solution for a week!
chrismasb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.