Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Java security: Java security, Part 1: Crypto basics

Brad Rubin (BradRubin@BradRubin.com), Principal, Brad Rubin & Associates Inc.
Brad Rubin is principal of Brad Rubin & Associates Inc., a computer-security consulting company specializing in wireless network and Java application security and education. Brad spent 14 years with IBM in Rochester, MN, working on all facets of the AS/400 hardware and software development, starting with its first release. He was a key player in IBM's move to embrace the Java platform, and was lead architect of IBM's largest Java application, a business application framework product called SanFrancisco (now part of WebSphere). He was also chief technology officer for the Data Storage Division of Imation Corp., as well as the leader of its R&D organization.

Brad has degrees in Computer and Electrical Engineering, and a Doctorate in Computer Science from the University of Wisconsin, Madison. He currently teaches the Senior Design course in Electrical and Computer Engineering at the University of Minnesota, and will develop and teach the university's Computer Security course in Fall 2002.

Summary:  The Java platform, both its base language features and library extensions, provides an excellent base for writing secure applications. In this tutorial, the first of two parts on Java security, Brad Rubin guides you through the basics of cryptography and how it is implemented in the Java programming language, using plenty of code examples to illustrate the concepts.

Date:  19 Jul 2002
Level:  Introductory PDF:  A4 and Letter (112 KB | 32 pages)Get Adobe® Reader®

Activity:  45346 views
Comments:  

SSL/TLS: Securing C/S communication

Overview

In this section, we'll examine the building blocks of the Secure Sockets Layer (and its replacement, Transport Layer Security), the protocol used to authenticate the server to the client. We'll offer a few code examples as illustrations.

What is Secure Sockets Layer/Transport Layer Security?

Secure Sockets Layer (SSL) and its replacement, Transport Layer Security (TLS), is a protocol for establishing a secure communications channel between a client and a server. It is also used to authenticate the server to the client and, less commonly, used to authenticate the client to the server. It is usually seen in a browser application, where the lock at the bottom of the browser window indicates SSL/TLS is in effect.

TLS 1.0 is the same as SSL 3.1.

SSL/TLS uses a hybrid of three of the cryptographic building blocks already discussed in this tutorial, but all of this is transparent to the user. Here is a simplified version of the protocol:

  • When a request is made to a site using SSL/TLS (usually with an https:// URL), a certificate is sent from the server to the client. The client verifies the identify of the server from this certificate using the installed public CA certificates, then checks that the IP name (machine name) matches the machine that the client is connected to.

  • The client generates some random info that can be used to generate a private key for the conversation, known as a session key, and encrypts it with the server's public key and sends it to the server. The server decrypts the message with its private key and uses the random info to derive the same private session key as the client. The RSA public key algorithm is usually used for this phase.

  • The client and server then communicate using the private session key and a private key algorithm, usually RC4. A message-authentication code, using yet another key, is used to ensure the integrity of the message.

SSL/TLS code sample

In this example, we write an HTTPS daemon process using an SSL server socket that returns an HTML stream when a browser connects to it. This example also shows how to generate a machine certificate in a special keystore to support the SSL deployment.

In Java programming, the only thing that needs to be done is to use an SSL Server Socket Factory instead of a Socket Factory, using lines like the following:

SSLServerSocketFacctory sslf = 
  (SSLServerSocketFactor)SSLServerSocketFactory.getDefault();
ServerSocket serverSocket = sslf.createServerSocket(PORT);

The complete code example is listed below:

import java.io.*;
import java.net.*;
import javax.net.ssl.*;
//
// Example of an HTTPS server to illustrate SSL certificate and socket
public class HTTPSServerExample {

  public static void main(String[] args) throws IOException {

    //
    // create an SSL socket using the factory and pick port 8080
    SSLServerSocketFactory sslsf =
      (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
    ServerSocket ss = sslsf.createServerSocket(8080);
    //
    // loop forever
    while (true) {
      try {
        //
        // block waiting for client connection
        Socket s = ss.accept();
        System.out.println( "Client connection made" );
        // get client request
        BufferedReader in = new BufferedReader(
          new InputStreamReader(s.getInputStream()));
        System.out.println(in.readLine());
        //
        // make an HTML response
        PrintWriter out = new PrintWriter( s.getOutputStream() );
        out.println("<HTML><HEAD>
<TITLE>HTTPS Server Example</TITLE>
" +
                    "</HEAD><BODY>
<H1>Hello World!</H1>
</BODY></HTML>
\n");
        //
        // Close the stream and socket
        out.close();
        s.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
  }
}


HTTPS server sample execution

In this example, we create an HTTPS server daemon that waits for a client browser connection and returns "Hello, World!". The browser connects to this daemon via https://localhost:8080.

We first create a machine certificate. The name must match the machine name of the computer where the daemon runs; in this case, localhost. In addition, we cannot use the same .keystore we have used in the past. We must create a separate keystore just for the machine certificate. In this case, it has the name sslKeyStore.

D:\IBM>keytool -genkey -v -keyalg RSA -alias MachineCert 
  -keystore sslKeyStore
Enter keystore password:  password
What is your first and last name?
  [Unknown]:  localhost
What is the name of your organizational unit?
  [Unknown]:  Security
What is the name of your organization?
  [Unknown]:  Company, Inc.
What is the name of your City or Locality?
  [Unknown]:  Machine Cert City
What is the name of your State or Province?
  [Unknown]:  MN
What is the two-letter country code for this unit?
  [Unknown]:  US
Is CN=localhost, OU=Security, O="Company, Inc.", L=Machine Cert City, 
ST=MN, C=US correct?
  [no]:  y

Generating 1,024 bit RSA key pair and self-signed certificate (MD5WithRSA)
   for: CN=localhost, OU=Security, O="Company, Inc.", L=Machine Cert City,
ST=MN, C=US
Enter key password for <MachineCert>
        (RETURN if same as keystore password):
[Saving sslKeyStore]

Then, we start the server daemon process specifying the special keystore and its password:

D:\IBM>java -Djavax.net.ssl.keyStore=sslKeyStore
 -Djavax.net.ssl.keyStorePassword=password HTTPSServerExample

After waiting a few seconds, fire up a browser and point it to https://localhost:8080 and you should be prompted on whether or not to trust the certificate. Selecting "yes" should display "Hello World!", and clicking on the lock in Internet Explorer will give the certificate details.

9 of 12 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=132295
TutorialTitle=Java security: Java security, Part 1: Crypto basics
publish-date=07192002
author1-email=BradRubin@BradRubin.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).