Ejemplo n.º 1
0
 /**
  * 存储ca证书成JKS格式
  *
  * @param cert
  * @param alias
  * @param password
  * @param out
  * @throws KeyStoreException
  * @throws NoSuchAlgorithmException
  * @throws CertificateException
  * @throws IOException
  */
 public static void storeCACert(Certificate cert, String alias, String password, OutputStream out)
     throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
   KeyStore ks = KeyStore.getInstance("JKS");
   ks.load(null, null);
   ks.setCertificateEntry(alias, cert);
   // store keystore
   ks.store(out, HttpClientUtil.str2CharArray(password));
 }
Ejemplo n.º 2
0
 /**
  * 获取SSLContext
  *
  * @param trustFile
  * @param trustPasswd
  * @param keyFile
  * @param keyPasswd
  * @return
  * @throws NoSuchAlgorithmException
  * @throws KeyStoreException
  * @throws IOException
  * @throws CertificateException
  * @throws UnrecoverableKeyException
  * @throws KeyManagementException
  */
 public static SSLContext getSSLContext(
     FileInputStream trustFileInputStream,
     String trustPasswd,
     FileInputStream keyFileInputStream,
     String keyPasswd)
     throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException,
         UnrecoverableKeyException, KeyManagementException {
   // ca
   TrustManagerFactory tmf = TrustManagerFactory.getInstance(HttpClientUtil.SunX509);
   KeyStore trustKeyStore = KeyStore.getInstance(HttpClientUtil.JKS);
   trustKeyStore.load(trustFileInputStream, HttpClientUtil.str2CharArray(trustPasswd));
   tmf.init(trustKeyStore);
   final char[] kp = HttpClientUtil.str2CharArray(keyPasswd);
   KeyManagerFactory kmf = KeyManagerFactory.getInstance(HttpClientUtil.SunX509);
   KeyStore ks = KeyStore.getInstance(HttpClientUtil.PKCS12);
   ks.load(keyFileInputStream, kp);
   kmf.init(ks, kp);
   SecureRandom rand = new SecureRandom();
   SSLContext ctx = SSLContext.getInstance(HttpClientUtil.TLS);
   ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), rand);
   return ctx;
 }