自从从 Android 13 迁移到 Android 14 以来,我无法再从 Android 连接到包含其自己的证书的本地盒子。 这是我的代码下面:
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);
在Android 13上它可以工作,但在Android 14上我得到SSLHandshakeException SSL例程:OPENSSL_internal:KEY_USAGE_BIT_INCORRECT
问题似乎出在自签名证书上。 在网上查看时,我看到现在需要将证书导入应用程序用户证书,但我无法执行此操作,因为我无权访问该证书的私钥,因为它属于本地框。 有人找到 Android 14 的解决方法吗? 感谢您的帮助,我已经寻找解决方案一周了!