package com;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManagerFactory;
public class SSLClient {
private static final String SERVER_HOST = "127.0.0.1";
private static final int SERVER_PORT = 8443;
private static final String CLIENT_KEYSTORE_PWD = "12345678";
private static final String CLIENT_TRUST_KEYSTORE_PWD = "12345678";
SSLSocket clientSocket;
public SSLClient() {
try {
// Initialize SSLClient
// keyStore的load一定要用PKCS12,非JKS;trustKeyStore的load可以是PKCS12或者是JKS
KeyStore keyStore = KeyStore.getInstance("PKCS12");
KeyStore trustKeyStore = KeyStore.getInstance("PKCS12");
/**
ok:client.p12\server.p12 client.p12\ca.p12 服务端接收为:cl/ca
如果为:ca.p12/server.p12 ca.p12/ca.p12服务端接收为:ca/ca
如果为:server.p12/ca.p12 server.p12/server.p12 服务端接收为:svr/ca
*/
keyStore.load(new FileInputStream("./src/client.p12"),
CLIENT_KEYSTORE_PWD.toCharArray());
trustKeyStore.load(new FileInputStream("./src/server.p12"),
CLIENT_TRUST_KEYSTORE_PWD.toCharArray());
// Initialize KeyStore Factory
KeyManagerFactory keyManagerFactory = KeyManagerFactory
.getInstance("SunX509");
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance("SunX509");
keyManagerFactory.init(keyStore, CLIENT_KEYSTORE_PWD.toCharArray());
trustManagerFactory.init(trustKeyStore);
// Initialize SSLContext
SSLContext context = SSLContext.getInstance("SSL");
context.init(keyManagerFactory.getKeyManagers(),
trustManagerFactory.getTrustManagers(), null);
// Set up Client Socket
clientSocket = (SSLSocket) context.getSocketFactory().createSocket(
SERVER_HOST, SERVER_PORT);
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void Test() {
try {
if (clientSocket == null) {
System.out.println("NULL clientSocket");
return;
}
// Run Client Test
InputStream input = null;
OutputStream output = null;
input = clientSocket.getInputStream();
output = clientSocket.getOutputStream();
// Output Message To Server
BufferedOutputStream bufferedOutput = new BufferedOutputStream(
output);
// output to Server
bufferedOutput.write("Client Test Running".getBytes());
bufferedOutput.flush();
// Output To Client Console
System.out.println("==============INFO================");
System.out.println("HOST:"
+ clientSocket.getSession().getPeerHost());
System.out.println("POST:"
+ clientSocket.getSession().getPeerPort());
// 显示授权信息.
X509Certificate cert = (X509Certificate) clientSocket.getSession()
.getPeerCertificates()[0];// getLocalCertificates
String subject = cert.getSubjectDN().getName();
String issuer = cert.getIssuerDN().getName();
System.out.println("Subject certificate: " + subject);
System.out.println("Issuer certificate: " + issuer);
System.out.println("==============INFO================");
System.out.println(new String(StreamToByteArray(input)));
Thread.sleep(3000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public byte[] StreamToByteArray(InputStream inputStream) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int readIndex = inputStream.read(buffer);
bout.write(buffer, 0, readIndex);
bout.flush();
bout.close();
inputStream.close();
return bout.toByteArray();
}
public static void main(String[] args) {
new SSLClient().Test();
}
}