Using SSL with Axis
In this article, we review how to use SSL with Axis and address the combination of SSL server authentication and BASIC-AUTH. SSL is based on a public key cryptography system, also called an asymmetrical key cryptography system, in which separate keys are used for encryption and decryption. In the case of server authentication, the server has a private key to decrypt messages from the client, and the client has the server's public key for encrypting messages that it sends after the session is established.
In the case of secret key encryption, more effort is required: Storage of the secret key by each communications partner, as well as the (initial) distribution of the secret key to those partners, must be secured. This can be a daunting and error-prone task.
You can create private and public keys using the keytool program provided with the JDK. If you add the -genkey option, the keytool command generates a private key and its public key (the command is broken here because of printing constraints, but it actually appears on a single line):
keytool -genkey -keyalg RSA sigalg MD5withRSA -keysize 1024 [ccc] -alias SkatesTown -dname "CN=Purchase Order Service, [ccc] OU=Purchase Order Department, O=SkatesTown, [ccc]L=..., S=NY, C=US" -keypass wsbookexample -storepass [ccc] wsbookexample keystore SkatesTown.ks -storetype JKS
The options are summarized in Table 1. The generated private key and related information are stored in a keystore file, and keystore file information may also be specified in the command (for example, keystore and storetype).
The keyalg and keysize options specify the specification of the private key. dname specifies an identification (X.500 Distinguished Name) for the key, and alias indicates an alias for the key (unique within the keystore). In addition, a password for accessing the key may be specified with keypass (by default, it is the same as the keystore password).
A public key corresponding to the private key is also generated with this command and is included in a certificate, which will be published to the client. Note that the certificate itself is described in more detail in the PKI section. The certificate must be signed in order to demonstrate integrity. The signature algorithm is specified by the sigalg option. Although we would like to specify who signed (or will sign) the certificate, that information cannot be specified with keytool. In this case, the certificate is signed by the private key that is generated, making it a self-signed certificate. A self-signed certificate is not practical for real use but is sufficiently useful for the purpose of experimenting with SSL.
Table 1: Options for the keytool Command
Option |
Value |
Meaning |
-keyalg |
RSA |
Format of the private key is RSA |
-keysize |
1024 |
Key size is 1024 bits |
-alias |
SkatesTown |
Key alias is SkatesTown |
-dname |
CN=Purchase Order Service, ... |
Identification of the key is CN=Purchase Order Service, ... |
-keypass |
wsbookexample |
Password for the private key is wsbookexample |
-sigalg |
MD5withRSA |
Method for signing certificate is MD5 with RSA |
-storepass |
wsbookexample |
Password for the keystore file is wsbookexample |
-keystore |
SkatesTown.ks |
Keystore filename is SkatesTown.ks |
-keystoretype |
JKS |
Keystore filetype is Java Key Store (JKS) |
The generated certificate can be extracted with the following keytool command:
keytool -export -alias SkatesTown -file SkatesTown.cer -keystore [ccc] SkatesTown.ks -storepass wsbookexample
The extracted certificate is stored in SkatesTown.cer. Next we import the server certificate to a client keystore using the following command:
keytool -import -trustcacerts -alias SkatesTown -file SkatesTown.cer [ccc] keystore SkateboardWarehouse.ks -storepass wsbookexample [ccc] -storetype JKS
The client uses the imported certificate to trust the server that owns that certificate. When a client establishes a session, the server sends a server certificate to the client. If the certificate is a member of the certificates included in the client keystore, the client trusts the server and so proceeds to the session.
We provide a browser interface for keytool within our example navigator. Visit /ch5/ex2/index.jsp. If you specify some parameters, keytool commands are automatically generated and executed (see Figure 1). You can create keystore files, export certificates, and import them to other keystore files.
Figure 1 Example navigator GUI for keytool.
Let's examine the SSL configuration for a Web server (Tomcat, in this case). In the <tomcat-home>/conf/server.xml file, you need to add the Connector section shown in Listing 1.
Listing 1: Tomcat Configuration for SSL
<Connector className="org.apache.tomcat.service.PoolTcpConnector"> <Parameter name="handler" value="org.apache.tomcat.service.http.HttpConnectionHandler"/> <Parameter name="port" value="8443"/> <Parameter name="socketFactory" value="org.apache.tomcat.net.SSLSocketFactory"/> <Parameter name="keystore" value="c:\ws-book\SkatesTown.ks" /> <Parameter name="keypass" value="wsbookexample"/> <Parameter name="clientAuth" value="false"/> </Connector>
HTTPS settings are specified in the Parameter elements. The port parameter indicates a port number for SSL connections. socketFactory specifies the Java factory class that will create SSL socket objects. With keystore and keypass, Tomcat can get a private key for SSL session. Note that Tomcat assumes that (and hence requires) the server's keystore password is identical to the private key password. Finally, clientAuth specifies whether client authentication is performed. Note that you have to modify <java_home>/jre/security/java.security to run Tomcat with SSL.
For the client, you must set up Java system properties that are required when invoking SSL. Listing 2 shows a modified POSubmission program. As you can see, the keystore type (storetype), keystore filename (keystore), and keystore password (storepass) are fed to the constructor, and the parameters are set for system properties. We will review these properties in the section "Java Secure Socket Extension." Although we specify the system parameters programmatically, you can specify them with the D option of the java command. In that case, you do not have to change the program at all.
Listing 2: POSubmission That Performs Basic Authentication and SSL
package ch5.ex3; import java.io.StringBufferInputStream; import java.io.StringWriter; import org.apache.axis.encoding.SerializationContext; import org.apache.axis.message.SOAPEnvelope; import org.apache.axis.message.SOAPBodyElement; import org.apache.axis.client.ServiceClient; import org.apache.axis.client.Transport; import org.apache.axis.transport.http.HTTPTransport; import org.apache.axis.Message; import org.apache.axis.MessageContext; import org.apache.axis.encoding.ServiceDescription; final public class DoOrder { final String url; final Strint storetype; final Strint keystore; final String storepass; final String uid; final String password; public DoOrder(String url, Strint storetype, Strint keystore, String storepass, String uid, String password) { this.url = url; this.storetype = storetype; this.keystore = keystore; this.storepass = storepass; this.uid = uid; this.password = password; } public synchronized static String invoke(String xml) throws [ccc] Exception { String[][] props = { { "javax.net.ssl.trustStore", keystore, }, { "javax.net.ssl.keyStore", keystore, }, { "javax.net.ssl.keyStorePassword", storepass, }, { "javax.net.ssl.keyStoreType", storetype, }, }; for (int i = 0; i < props.length; i++) System.getProperties().setProperty(props[i][0], [ccc] props[i][1]); ServiceClient client = [ccc] new ServiceClient(new HTTPTransport(url, "PO")); client.set(MessageContext.USERID, uid); client.set(MessageContext.PASSWORD, password); client.setRequestMessage(new Message(new [ccc] StringBufferInputStream(xml), true)); client.invoke(); Message outMsg = [ccc] client.getMessageContext().getResponseMessage(); ServiceDescription svc = [ccc] new ServiceDescription("doOrder", false); client.getMessageContext().setServiceDescription(svc); SOAPEnvelope envelope = outMsg.getAsSOAPEnvelope(); SOAPBodyElement body = envelope.getFirstBody(); StringWriter writer = new StringWriter(); SerializationContext ctx = [ccc] new SerializationContext(writer, [ccc] client.getMessageContext()); body.output(ctx); return writer.toString(); } }
You can execute POSubmission with BASIC-AUTH and SSL via /ch5/ex3/index.jsp in the example navigator. Specify some values in the page such as the keystore file, keystore password, and so on if you want, and then click the Submit PO button (see Figure 2).
Figure 2 Example navigator GUI for basic authentication and SSL.