Java implementation of SSL mutual authentication

Java implementation of SSL mutual authentication

SSL protocol handshake agreement into a two-way one-way authentication and certification, including mutual authentication with a variety of implementations, the following describes how Java implementation of SSL mutual authentication.

Simulation scenarios:
Server-side and Client-side communications, the need for identity verification and authorization, that only accept messages Client Server and Client Server can only accept messages.

Technical realization:
JSSE ( the Java  Security the Socket Extension)
is the Sun in order to solve secure communications on the Internet and the introduction of solutions. It implements the SSL and the TSL (Transport Layer Security) protocol. JSSE includes the data encryption, server authentication, client authentication, and message integrity techniques. By using JSSE, developers can TCP / IP protocol to transfer data between the client and secure server.


In order to achieve message authentication.

Server requires:
1) KeyStore: where to save the server's private key
2) Trust KeyStore: which authority certificates saved the client
the same, Client needs:
1) KeyStore: which hold the client's private key
2) Trust KeyStore: which authority certificates stored server-side


Here I recommend the use of Java comes with keytool command to generate such information file. Of course, the current generation of very popular open source SSL certificate as well as for OpenSSL. OpenSSL using C language written across systems. But we may consider the convenience of java program used to generate the certificate at a later process, or use the built-in JDK keytool.
1) generation server private key, and imported to the server KeyStore file
keytool -genkey -alias serverkey -keystore kserver.keystore
process, they are required to complete, according to the needs of their own settings on the line
keystore password: 123456
Name and Last Name: jin
organizational unit name: none
organization name: none
city or region name: BJ
state or province name: BJ
country Code: CN
serverkey private key password, and do not fill the same keystore password. We should be careful here, directly to Enter on the line, do not change your password. Otherwise, in the back of the program and can not be applied directly to the private key error.



You can generate kserver.keystore file
server.keystore to the server is used, which holds its own private key

2) According to the private key, export the server certificate
the keytool -export -alias -file serverkey -keystore kserver.keystore server.crt
server.crt end of that Certificate Services

3) The server certificate into the client's Trust KeyStore in
the keytool -import -alias -file serverkey server.crt -keystore tclient.keystore
tclient.keystore to the client is used, which holds a trusted certificate

Using the same method, the client generates a private key, the client certificate, and introduced into the service side of the Trust KeyStore
. 1) the keytool -alias -genkey ClientKey -keystore kclient.keystore
2) the keytool -export -alias ClientKey -keystore kclient.keystore - client.crt File
3) the keytool -import -alias -file client.crt -keystore tserver.keystore ClientKey

Thus, the resulting file into two
server save: kserver.keystore tserver.keystore
client save: kclient.keystore tclient.kyestore



The following is by Java Socket communication program to validate our generated certificate is available.

Client:

Client code

  1. package examples.ssl;


  2. import java.io.BufferedInputStream;

  3. import java.io.BufferedOutputStream;

  4. import java.io.FileInputStream;

  5. import java.io.IOException;

  6. import java.io.InputStream;

  7. import java.io.OutputStream;

  8. import java.security.KeyStore;


  9. import javax.net.ssl.KeyManagerFactory;

  10. import javax.net.ssl.SSLContext;

  11. import javax.net.ssl.SSLSocket;

  12. import javax.net.ssl.TrustManagerFactory;


  13. /**

  14. * SSL Client

  15. *

  16. */

  17. public class SSLClient {


  18. private static final String DEFAULT_HOST = “127.0.0.1”;

  19. private static final int DEFAULT_PORT = 7777;


  20. private static final String CLIENT_KEY_STORE_PASSWORD = “123456”;

  21. private static final String CLIENT_TRUST_KEY_STORE_PASSWORD = “123456”;


  22. private SSLSocket sslSocket;


  23. /**

  24. * Start the client program

  25. *

  26. * @param args

  27. */

  28. public static void main(String[] args) {

  29. SSLClient client = new SSLClient();

  30. init();

  31. process();

  32. }


  33. /**

  34. * Connected via ssl socket with the server, and sends a message

  35. */

  36. public void process() {

  37. if (sslSocket == null) {

  38. out.println(“ERROR”);

  39. return;

  40. }

  41. try {

  42. InputStream input = sslSocket.getInputStream();

  43. OutputStream output = sslSocket.getOutputStream();


  44. BufferedInputStream bis = new BufferedInputStream(input);

  45. BufferedOutputStream bos = new BufferedOutputStream(output);


  46. write(“Client Message”.getBytes());

  47. flush();


  48. byte[] buffer = new byte[20];

  49. read(buffer);

  50. out.println(new String(buffer));


  51. close();

  52. } catch (IOException e) {

  53. out.println(e);

  54. }

  55. }


  56. /**

  57. * <ul>

  58. * <Li> Key ssl connection: </ li>

  59. * <Li> Initialization SSLSocket </ li>

  60. * <Li> Import private client KeyStore, import KeyStore (the certificate server) client trusted </ li>

  61. * </ul>

  62. */

  63. public void init() {

  64. try {

  65. SSLContext ctx = SSLContext.getInstance(“SSL”);


  66. KeyManagerFactory kmf = KeyManagerFactory.getInstance(“SunX509”);

  67. TrustManagerFactory tmf = TrustManagerFactory.getInstance(“SunX509”);


  68. KeyStore ks = KeyStore.getInstance(“JKS”);

  69. KeyStore tks = KeyStore.getInstance(“JKS”);


  70. load(new FileInputStream(“E://kclient.keystore”), CLIENT_KEY_STORE_PASSWORD.toCharArray());

  71. load(new FileInputStream(“E://tclient.keystore”), CLIENT_TRUST_KEY_STORE_PASSWORD.toCharArray());


  72. init(ks, CLIENT_KEY_STORE_PASSWORD.toCharArray());

  73. init(tks);


  74. init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);


  75. sslSocket = (SSLSocket) ctx.getSocketFactory().createSocket(DEFAULT_HOST, DEFAULT_PORT);

  76. } catch (Exception e) {

  77. out.println(e);

  78. }

  79. }


  80. }


Service-Terminal:

Java code

  1. package examples.ssl;


  2. import java.io.BufferedInputStream;

  3. import java.io.BufferedOutputStream;

  4. import java.io.FileInputStream;

  5. import java.io.InputStream;

  6. import java.io.OutputStream;

  7. import java.net.Socket;

  8. import java.security.KeyStore;


  9. import javax.net.ssl.KeyManagerFactory;

  10. import javax.net.ssl.SSLContext;

  11. import javax.net.ssl.SSLServerSocket;

  12. import javax.net.ssl.TrustManagerFactory;


  13. /***********************************************************************************************************************

  14. * <ul>

  15. * <Li> 1) to generate private server </ li>

  16. * <li>keytool -genkey -alias serverkey -keystore kserver.keystore</li>

  17. * <Li> 2) according to the private key, the server certificate around </ li>

  18. * <li>keytool -exoport -alias serverkey -keystore kserver.keystore -file server.crt</li>

  19. * <Li> 3) the certificate is added to the client trusted in the keystore </ li>

  20. * <li>keytool -import -alias serverkey -file server.crt -keystore tclient.keystore</li>

  21. * </ul>

  22. **********************************************************************************************************************/


  23. /**

  24. * SSL Server

  25. *

  26. */

  27. public class SSLServer {


  28. private static final int DEFAULT_PORT = 7777;


  29. private static final String SERVER_KEY_STORE_PASSWORD = “123456”;

  30. private static final String SERVER_TRUST_KEY_STORE_PASSWORD = “123456”;


  31. private SSLServerSocket serverSocket;


  32. /**

  33. * starting program

  34. *

  35. * @param args

  36. */

  37. public static void main(String[] args) {

  38. SSLServer server = new SSLServer();

  39. init();

  40. start();

  41. }


  42. /**

  43. * <ul>

  44. * <li>听SSL Server Socket</li>

  45. * <Li> Since the demonstration program is not Socket monitor, a simple form of single-threaded, and only accept messages of the client, the client and returns the specified message </ li>

  46. * </ul>

  47. */

  48. public void start() {

  49. if (serverSocket == null) {

  50. out.println(“ERROR”);

  51. return;

  52. }

  53. while (true) {

  54. try {

  55. Socket s = serverSocket.accept();

  56. InputStream input = s.getInputStream();

  57. OutputStream output = s.getOutputStream();


  58. BufferedInputStream bis = new BufferedInputStream(input);

  59. BufferedOutputStream bos = new BufferedOutputStream(output);


  60. byte[] buffer = new byte[20];

  61. read(buffer);

  62. out.println(new String(buffer));


  63. write(“Server Echo”.getBytes());

  64. flush();


  65. close();

  66. } catch (Exception e) {

  67. out.println(e);

  68. }

  69. }

  70. }


  71. /**

  72. * <ul>

  73. * <Li> Key ssl connection: </ li>

  74. * <Li> Initialization SSLServerSocket </ li>

  75. * <Li> Importing server private key KeyStore, import KeyStore (the client's certificate) </ li> Trusted server

  76. * </ul>

  77. */

  78. public void init() {

  79. try {

  80. SSLContext ctx = SSLContext.getInstance(“SSL”);


  81. KeyManagerFactory kmf = KeyManagerFactory.getInstance(“SunX509”);

  82. TrustManagerFactory tmf = TrustManagerFactory.getInstance(“SunX509”);


  83. KeyStore ks = KeyStore.getInstance(“JKS”);

  84. KeyStore tks = KeyStore.getInstance(“JKS”);


  85. load(new FileInputStream(“E://kserver.keystore”), SERVER_KEY_STORE_PASSWORD.toCharArray());

  86. load(new FileInputStream(“E://tserver.keystore”), SERVER_TRUST_KEY_STORE_PASSWORD.toCharArray());


  87. init(ks, SERVER_KEY_STORE_PASSWORD.toCharArray());

  88. init(tks);


  89. init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);


  90. serverSocket = (SSLServerSocket) ctx.getServerSocketFactory().createServerSocket(DEFAULT_PORT);

  91. setNeedClientAuth(true);

  92. } catch (Exception e) {

  93. printStackTrace ();

  94. }

  95. }

  96. }


Guess you like

Origin blog.51cto.com/14588847/2468096