对TLS 1.2版Android客户端/服务器

编程入门 行业动态 更新时间:2024-10-27 23:27:16
本文介绍了对TLS 1.2版Android客户端/服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想我创建一个服务器和Android客户端之间的TLS 1.2版的通信。 我建立了任何问题,一个TLS 1.0连接,但我不能让1.2版。 这是服务器code:

I'm trying my to create TLS v1.2 communication between a server and android client. I established a TLS v1.0 connection with any problem, but I cannot get v1.2. This is server code:

char[] passphrase = "myComplexPass1".toCharArray(); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(new FileInputStream("cacerts"), passphrase); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); keyManagerFactory.init(keystore, passphrase); SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); SSLContext sslContext.init(keyManagers, null, null); SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory(); SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port); sslServerSocket.setEnabledProtocols(new String [] { "TLSv1", "TLSv1.1", "TLSv1.2" }); sslServerSocket.setUseClientMode(false); sslServerSocket.setWantClientAuth(false); sslServerSocket.setNeedClientAuth(false); sslSocket = (SSLSocket)sslServerSocket.accept();

而这是客户端code:

while this is client code:

char[] passphrase = "myComplexPass1".toCharArray(); KeyStore keystore = KeyStore.getInstance("BKS"); keystore.load(this.getApplicationContext().getResources().openRawResource(R.raw.jb), passphrase); KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); keyManagerFactory.init(keystore, passphrase); SSLContext sslContext = SSLContext.getInstance("TLSv1.2"); Log.d("Context Protocol",sslContext.getProtocol());//this prints correctly TLS v1.2! KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); TrustManager[] trustManagers = 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) { } } }; sslContext.init(keyManagers, trustManagers, new SecureRandom()); SSLSocketFactory sslSocketFactory = (SSLSocketFactory) sslContext.getSocketFactory(); SSLSocket skt = (SSLSocket) sslSocketFactory.createSocket(HOST, PORT); skt.setKeepAlive(true);

客户端code,写在我的电脑上运行JRE7一个Java客户端,完美的作品,我用的getProtocol(服务器端)TLSv1.2用正确的密码,通过tlsv1.2支持见。 在Android上同code进行tlsv1.0连接! 我真的不uderstand。 在Java客户端JRE7工作,在Android上只有tlsv1.0 任何建议?

Client code, written in a java client running on JRE7 on my pc, perfectly works and I see with getProtocol (server-side) TLSv1.2 with a correct cipher, supported by tlsv1.2. Same code on android make a tlsv1.0 connection! I really don't uderstand. On Java client JRE7 works, on android ONLY tlsv1.0 Any suggestion?

这是我的第一个问题,我搜索了很多。也许我的格式不正确:(

It's my first question, I searched a lot. Probably my formatting is not correct :(

推荐答案

有点晚了要回答这个问题,但也许别人会需要一个答案。

Kind of late to be answering this, but maybe someone else will need an answer.

我遇到了同样的问题。无论你提供TLSv1.2到SSLContext.init()方法,我已经尝试了一些Android的版本不启用TLS 1.2。您必须使用setEnabledProtocols()就像你做你的服务器套接字启用您的客户端套接字。

I have run into the same issue. No matter whether you provide TLSv1.2 to the SSLContext.init() method, some Android versions that I've tried do not enable TLS 1.2. You must enable that on your client socket using setEnabledProtocols() just as you do for your server socket. For me, I did this in a custom SSLSocketFactory I created:

public class MySSLSocketFactory extends SSLSocketFactory throws NoSuchAlgorithmException { private SSLContext mSSLContext; public MySSLSocketFactory(KeyManager km) { ... mSSLContext = SSLContext.getInstance("TLSv1.2"); ... mSSLContext.init(new KeyManager[] {km}, null, null); ... } @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { SSLSocket s = (SSLSocket)mSSLContext.getSocketFactory().createSocket(socket, host, port, autoClose); s.setEnabledProtocols(new String[] {"TLSv1.2"} ); return s; } ... }

更多推荐

对TLS 1.2版Android客户端/服务器

本文发布于:2023-11-05 07:14:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:客户端   服务器   TLS   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!