Communication with server that support ssl in java -


i have question sslconnectionin java , write below code client side ( application have connect server, have server support ssl )but error” javax.net.ssl.sslexception: connection has been shutdown: javax.net.ssl.sslhandshakeexception: sun.security.validator.validatorexception: pkix path building failed: sun.security.provider.certpath.suncertpathbuilderexception: unable find valid certification path requested target”

how can resolve problem ?

  public static void main(string[] args)  {               bufferedreader in = new bufferedreader(                  new inputstreamreader(system.in));               printstream out = system.out;               sslsocketfactory f =                   (sslsocketfactory) sslsocketfactory.getdefault();               try {                  sslsocket c =                    (sslsocket) f.createsocket("192.168.10.38", 7701);                    printsocketinfo(c);                    system.out.println("end printsocketinfo");                    c.starthandshake();                    system.out.println("handshake ok");                     bufferedwriter w = new bufferedwriter(                     new outputstreamwriter(c.getoutputstream()));                    bufferedreader r = new bufferedreader(                     new inputstreamreader(c.getinputstream()));                    string m = null;                 // string m=                  while ((m=r.readline())!= null) {                      system.out.println("11111");                     out.println(m);                     m = in.readline();                     system.out.println("m is:  "+ m);                     w.write(m,0,m.length());                     w.newline();                     w.flush();                  }                  w.close();                  r.close();                  c.close();               } catch (ioexception e) {                  system.err.println(e.tostring());               }     }       private static void printsocketinfo(sslsocket s) {           system.out.println("socket class: "+s.getclass());           system.out.println("   remote address = "              +s.getinetaddress().tostring());           system.out.println("   remote port = "+s.getport());           system.out.println("   local socket address = "              +s.getlocalsocketaddress().tostring());           system.out.println("   local address = "              +s.getlocaladdress().tostring());           system.out.println("   local port = "+s.getlocalport());           system.out.println("   need client authentication = "              +s.getneedclientauth());           sslsession ss = s.getsession();           system.out.println("   cipher suite = "+ss.getciphersuite());           system.out.println("   protocol = "+ss.getprotocol());        } 

i have certificate file, how have use certificate?

best regards

is self signed certificate

if yes have 2 options first option :

import certificate authority certificate in global java certificate trust store. store located @

%java installation%/jre/lib/security/cacerts 

to import can use keytool command comes java installation

keytool -import -alias keyname -file yourcertificate.crt -keystore cacerts 

advantage:

  • no code modification needed.
  • simple deploy

disadvantage:

  • cacert file overwritten in next java update. have import certificate again.
  • requires administrative privileges (both on linux , windows)

second option :

if want bypass certificate validation follow java: overriding function disable ssl certificate check

else create new trust store program

keytool -import -alias keyname -file yourcertificate.crt -keystore yourtruststore 

this command ask password 2 times. enter password want , input "yes" questions file created @ current directory name "yourtruststore"

now need use trust store in program

sslsocketfactory sslfactory = null; inputstream truststore = null; keystore keystore = null; truststore = new fileinputstream("<your trust store absolute path>"); keystore = keystore.getinstance(keystore.getdefaulttype()); keystore.load(truststore, "<your trust store password>".tochararray()); trustmanagerfactory tmf = trustmanagerfactory.getinstance(trustmanagerfactory.getdefaultalgorithm()); tmf.init(keystore); sslcontext ctx = sslcontext.getinstance("tls"); ctx.init(null, tmf.gettrustmanagers(), null); sslfactory = ctx.getsocketfactory(); 

you can use socket factory open new sockets


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -