- Class Test

This section provides a tutorial example on how to write a simple program to test the class.

SSL sockets created with the default SSL socket factories will not have any certificates associated to authenticate them. In order to associate certificates with SSL sockets, we need to use the SSLContext class to create SSL socket factories. Here is a sample program,

 - Copyright (c) 2014,, All Rights Reserved.
public class SslContextTest {
   public static void main(String[] args) {
      PrintStream out = System.out;
      out.println("\nTesting socket factory with SSLContext:");
      try {
      	 // SSLContext protocols: TLS, SSL, SSLv3
         SSLContext sc = SSLContext.getInstance("SSLv3");
         System.out.println("\nSSLContext class: "+sc.getClass());
         System.out.println("   Protocol: "+sc.getProtocol());
         System.out.println("   Provider: "+sc.getProvider());

      	 // SSLContext algorithms: SunX509
         KeyManagerFactory kmf 
            = KeyManagerFactory.getInstance("SunX509");
         System.out.println("\nKeyManagerFactory class: "
         System.out.println("   Algorithm: "+kmf.getAlgorithm());
         System.out.println("   Provider: "+kmf.getProvider());

      	 // KeyStore types: JKS
         String ksName = "herong.jks";
         char ksPass[] = "HerongJKS".toCharArray();
         char ctPass[] = "My1stKey".toCharArray();
         KeyStore ks = KeyStore.getInstance("JKS");
         ks.load(new FileInputStream(ksName), ksPass);
         System.out.println("\nKeyStore class: "+ks.getClass());
         System.out.println("   Type: "+ks.getType());
         System.out.println("   Provider: "+ks.getProvider());
         System.out.println("   Size: "+ks.size());

         // Generating KeyManager list
         KeyManager[] kmList = kmf.getKeyManagers();
         System.out.println("\nKeyManager class: "
         System.out.println("   # of key manager: " +kmList.length);

         // Generating SSLServerSocketFactory
         sc.init(kmList, null, null);
         SSLServerSocketFactory ssf = sc.getServerSocketFactory();
         System.out.println("\nSSLServerSocketFactory class: "
         // Genearting SSLServerSocket
         SSLServerSocket ss
            = (SSLServerSocket) ssf.createServerSocket();
         System.out.println("\nSSLServerSocket class: "
         System.out.println("   String: "+ss.toString());

         // Generating SSLSocketFactory
         sc.init(kmList, null, null);
         SSLSocketFactory sf = sc.getSocketFactory();
         System.out.println("\nSSLSocketFactory class: "

         // Genearting SSLSocket
         SSLSocket s
            = (SSLSocket) sf.createSocket();
         System.out.println("\nSSLSocket class: "+s.getClass());
         System.out.println("   String: "+s.toString());
      } catch (Exception e) {

Of course, to run this program, you need to have the Java key store file, herong.jks, ready. It contains a self-signed pair of private and public keys. Read other chapters on Java key store files.

If you run this program with JDK 1.8, you will get:

>java SslContextTest

Testing socket factory with SSLContext:

SSLContext class: class
   Protocol: SSLv3
   Provider: SunJSSE version 1.8

KeyManagerFactory class: class
   Algorithm: SunX509
   Provider: SunJSSE version 1.8

KeyStore class: class
   Type: JKS
   Provider: SUN version 1.8
   Size: 3

KeyManager class: class
   # of key manager: 1

SSLServerSocketFactory class: class

SSLServerSocket class: class
   String: [SSL: ServerSocket[unbound]]

SSLSocketFactory class: class

SSLSocket class: class
   String: 1ed40e0[SSL_NULL_WITH_NULL_NULL: Socket[unconnected]]

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

 What Is SSL (Secure Socket Layer)?

 SSL Specification Overview

 JSSE - Java Implementation of SSL and TLS - Default SSL Socket Factory Test - Class Test

 SSL Socket Communication Testing Programs

 SSL Client Authentication

 HTTPS (Hypertext Transfer Protocol Secure)

 Outdated Tutorials


 PDF Printing Version