Writing a client-server application that uses ssl / tls using java without beeing able to use keytool -


what i'm planning writing client-server application uses ssl (tls) connection exchange data.

as client downloadable , can not guarantee access keystore i'm looking way import certificates @ runtime.

what need:

  • a way import server's public key/certificate client application
  • a way import server's private key/certificate server application

what found out far:

// load server's public crt (pem), exported https website certificatefactory cf = certificatefactory.getinstance("x509"); x509certificate cert = (x509certificate)cf.generatecertificate(new fileinputstream("c:\\certificate.crt"));  // load pkcs12 key generated openssl out of server.crt , server.key (private) (if private key stored in pkcs file, not solution need ship application) keystore ks = keystore.getinstance("pkcs12"); ks.load(new fileinputstream("c:\\certificate.pkcs"), "password".tochararray()); ks.setcertificateentry("alias", cert);  trustmanagerfactory tmf = trustmanagerfactory.getinstance("sunx509"); tmf.init(ks);  keymanagerfactory kmf = keymanagerfactory.getinstance("sunx509"); kmf.init(ks, "password".tochararray());  // create sslcontext establish secure connection sslcontext ctx = sslcontext.getinstance("tls"); ctx.init(kmf.getkeymanagers(), tmf.gettrustmanagers(), null); 

this not work me i'm getting error:

java.security.keystoreexception: trustedcertentry not supported @ ks.setcertificateentry("alias", cert);

also, think pkcs12 used store private keys not want.

i'm new java , i'm stuck problem now.

thanks in advance,

kazuo

sun's implementation of #pkcs12 not allow store trusted certificates if not part of chain of private key.
if need use #pkcs12 have switch different provider e.g. bouncy castle supports this.
if not have requirement on keystore type can switch jks java's keystore , allows set trusted certificates (i.e. not part of private key).
jks can use default provider i.e. sun.
update:
code have change follows:

//create temp keystore server certificate    keystore kstemp = keystore.getinstance("jks");     kstemp.load(null, null);//initialize   kstemp.setcertificateentry("alias", cert);    bytearrayoutputstream bout = new bytearrayoutputstream();   // save temp keystore ks.store(bout, password);    //now create keystore used jsse    keystore store = keystore.getinstance("jks");    store.load(new bytearrayinputstream(bout.tobytearray()), password);   

now use keystore store in code has server's trusted certificate , not private key.
comments in code noticed have pkcs12 created using openssl?
if have p12 can not use "jks" keymanager.
have use pkcs12 , load pkcs12 use in kmf.
have use 2 types in app


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

c# - SharpSVN - How to get the previous revision? -

php cli reading files and how to fix it? -