Test Client Program for Client Authentication

This section provides a tutorial example on how to write a test program as the SSL socket communication client with both server and client authentications.

Of course, I also revised SslSocketClient.java:

/* SslSocketClientRevised.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
import java.io.*;
import java.net.*;
import java.security.*;
import javax.net.ssl.*;
public class SslSocketClientRevised {
   public static void main(String[] args) {
      if (args.length<3) {
            "   java SslReverseEchoerRevised ksName ksPass ctPass");
      String ksName = args[0];
      char[] ksPass = args[1].toCharArray();
      char[] ctPass = args[2].toCharArray();
      System.setProperty("javax.net.ssl.trustStore", args[0]);
      BufferedReader in = new BufferedReader(
         new InputStreamReader(System.in));
      PrintStream out = System.out;
      try {
         KeyStore ks = KeyStore.getInstance("JKS");
         ks.load(new FileInputStream(ksName), ksPass);
         KeyManagerFactory kmf = 
         kmf.init(ks, ctPass);
         SSLContext sc = SSLContext.getInstance("SSL");
         sc.init(kmf.getKeyManagers(), null, null);
         SSLSocketFactory f = sc.getSocketFactory();
         SSLSocket c =
           (SSLSocket) f.createSocket("localhost", 8888);
         BufferedWriter w = new BufferedWriter(
            new OutputStreamWriter(c.getOutputStream()));
         BufferedReader r = new BufferedReader(
            new InputStreamReader(c.getInputStream()));
         String m = null;
         while ((m=r.readLine())!= null) {
            m = in.readLine();
      } catch (Exception e) {
   private static void printSocketInfo(SSLSocket s) {
      System.out.println("Socket class: "+s.getClass());
      System.out.println("   Remote address = "
      System.out.println("   Remote port = "+s.getPort());
      System.out.println("   Local socket address = "
      System.out.println("   Local address = "
      System.out.println("   Local port = "+s.getLocalPort());
      System.out.println("   Need client authentication = "
      SSLSession ss = s.getSession();
      try {
         System.out.println("Session class: "+ss.getClass());
         System.out.println("   Cipher suite = "
         System.out.println("   Protocol = "+ss.getProtocol());
         System.out.println("   PeerPrincipal = "
         System.out.println("   LocalPrincipal = "
         System.out.println("   PeerPrincipal = "
      } catch (Exception e) {

The changes are minor:

Last update: 2014.

Table of Contents

 About This JDK Tutorial Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 Downloading and Installing JDK 1.6.2 on Windows

 Java Date-Time API

 Date, Time and Calendar Classes

 Date and Time Object and String Conversion

 Number Object and Numeric String Conversion

 Locales, Localization Methods and Resource Bundles

 Calling and Importing Classes Defined in Unnamed Packages

 HashSet, Vector, HashMap and Collection Classes

 Character Set Encoding Classes and Methods

 Character Set Encoding Maps

 Encoding Conversion Programs for Encoded Text Files

 Socket Network Communication

 Datagram Network Communication

 DOM (Document Object Model) - API for XML Files

 SAX (Simple API for XML)

 DTD (Document Type Definition) - XML Validation

 XSD (XML Schema Definition) - XML Validation

 XSL (Extensible Stylesheet Language)

 Message Digest Algorithm Implementations in JDK

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Digital Signature Algorithm and Sample Program

 "keytool" Commands and "keystore" Files

 KeyStore and Certificate Classes

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 The SSL (Secure Socket Layer) Protocol

 SSL Socket Communication Testing Programs

SSL Client Authentication

 What Is SSL Client Authentication?

 Preparing Certificates for Client Authentication

 Test Server Program for Client Authentication

Test Client Program for Client Authentication

 SSL Client Authentication Test Result

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version