java - Android Client / Server on TLS v1.2 -
i'm trying create tls v1.2 communication between server , android client. established tls v1.0 connection problem, cannot v1.2. 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();
while 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);
client code, written in java client running on jre7 on pc, works , see getprotocol (server-side) tlsv1.2 correct cipher, supported tlsv1.2. same code on android make tlsv1.0 connection! don't uderstand. on java client jre7 works, on android tlsv1.0 suggestion?
it's first question, searched lot. formatting not correct :(
kind of late answering this, maybe else need answer.
i have run same issue. no matter whether provide tlsv1.2 sslcontext.init()
method, android versions i've tried not enable tls 1.2. must enable on client socket using setenabledprotocols()
server socket. me, did in custom sslsocketfactory
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; } ... }
Comments
Post a Comment