ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/rzahajsseexmplserver.htm

158 lines
6.4 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: SSL server using an SSLContext object" />
<meta name="DC.Relation" scheme="URI" content="codedisclaimer.htm" />
<meta name="DC.Relation" scheme="URI" content="rzahajsseexmpls.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzahajsseexmplserver" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: SSL server using an SSLContext object</title>
</head>
<body id="rzahajsseexmplserver"><a name="rzahajsseexmplserver"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: SSL server using an SSLContext object</h1>
<div><div class="section"><p><strong>Note:</strong> Read the <a href="codedisclaimer.htm">Code
example disclaimer</a> for important legal information.</p>
<pre>//////////////////////////////////////////////////////////////////////////////////
//
// The following server program utilizes an SSLContext object that it
// initializes with a previously created keystore file.
//
// The keystore file has the following name and keystore password:
// File name: /home/keystore.file
// Password: password
//
// The example program needs the keystore file in order to create an
// IbmISeriesKeyStore object. The KeyStore object must specify MY_SERVER_APP as
// the application identifier.
//
// To create the keystore file, you can use the following Qshell command:
//
// java com.ibm.as400.SSLConfiguration -create -keystore /home/keystore.file
// -storepass password -appid MY_SERVER_APP
//
// Command syntax:
// java -Djava.version=1.4 JavaSslServer
//
// Note that "-Djava.version=1.4" is unnecessary when you have configured
// J2SDK version 1. to be used by default.
//
//////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import javax.net.ssl.*;
/**
* Java SSL Server Program using Application ID.
*/
public class JavaSslServer {
/**
* JavaSslServer main method.
*
* @param args the command line arguments (not used)
*/
public static void main(String args[]) {
/*
* Set up to catch any exceptions thrown.
*/
try {
/*
* Allocate and initialize a KeyStore object.
*/
Char[] password = "password".toCharArray();
KeyStore ks = KeyStore.getInstance("IbmISeriesKeyStore");
FileInputStream fis = new FileInputStream("/home/keystore.file"
Ks.load(fis, password);
/*
* Allocate and initialize a KeyManagerFactory.
*/
KeyManagerFactory kmf =
KeyManagerFactory.getInstance("IbmISeriesX509");
Kmf.init(ks, password);
/*
* Allocate and initialize a TrustManagerFactory.
*/
TrustManagerFactory tmf =
TrustManagerFactory.getInstance("IbmISeriesX509");
tmf.init(ks);
/*
* Allocate and initialize an SSLContext.
*/
SSLContext c =
SSLContext.getInstance("SSL", "IbmISeriesSslProvider");
C.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
/*
* Get the an SSLServerSocketFactory from the SSLContext.
*/
SSLServerSocketFactory sf = c.getSSLServerSocketFactory();
/*
* Create an SSLServerSocket.
*/
SSLServerSocket ss =
(SSLServerSocket) sf.createServerSocket(13333);
/*
* Perform an accept() to create an SSLSocket.
*/
SSLSocket s = (SSLSocket) ss.accept();
/*
* Receive a message from the client using the secure session.
*/
InputStream is = s.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead = is.read(buffer);
if (bytesRead == -1)
throw new IOException("Unexpected End-of-file Received");
String received = new String(buffer, 0, bytesRead);
/*
* Write results to screen.
*/
System.out.println("Read " + received.length() + " bytes...");
System.out.println(received);
/*
* Echo the message back to the client using the secure session.
*/
OutputStream os = s.getOutputStream();
os.write(received.getBytes());
/*
* Write results to screen.
*/
System.out.println("Wrote " + received.length() + " bytes...");
System.out.println(received);
} catch (Exception e) {
System.out.println("Unexpected exception caught: " +
e.getMessage());
e.printStackTrace();
}
}
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzahajsseexmpls.htm" title="The JSSE examples show how a client and a server can use the native iSeries JSSE provider to create a context that enables secure communications.">Examples: IBM Java Secure Sockets Extension</a></div>
</div>
<div class="linklist"><strong>Collected links</strong><br />
<div><a href="codedisclaimer.htm">Code example disclaimer</a></div></div>
</div>
</body>
</html>