001: package org.bouncycastle.crypto.tls.test;
002:
003: import javax.net.ssl.KeyManagerFactory;
004: import javax.net.ssl.SSLContext;
005: import javax.net.ssl.SSLServerSocket;
006: import javax.net.ssl.SSLServerSocketFactory;
007: import javax.net.ssl.SSLSession;
008: import javax.net.ssl.SSLSocket;
009: import javax.net.ssl.TrustManagerFactory;
010: import java.io.ByteArrayInputStream;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.io.OutputStream;
014: import java.io.OutputStreamWriter;
015: import java.io.PrintWriter;
016: import java.security.KeyStore;
017:
018: public class HTTPSServerThread extends Thread {
019: private static final int PORT_NO = 8003;
020: private static final char[] SERVER_PASSWORD = "serverPassword"
021: .toCharArray();
022: private static final char[] TRUST_STORE_PASSWORD = "trustPassword"
023: .toCharArray();
024:
025: /**
026: * Read a HTTP request
027: */
028: private void readRequest(InputStream in) throws IOException {
029: int ch = 0;
030: int lastCh = 0;
031: while ((ch = in.read()) >= 0 && (ch != '\n' && lastCh != '\n')) {
032: if (ch != '\r') {
033: lastCh = ch;
034: }
035: }
036: }
037:
038: /**
039: * Send a response
040: */
041: private void sendResponse(OutputStream out) {
042: PrintWriter pWrt = new PrintWriter(new OutputStreamWriter(out));
043: pWrt.print("HTTP/1.1 200 OK\r\n");
044: pWrt.print("Content-Type: text/html\r\n");
045: pWrt.print("\r\n");
046: pWrt.print("<html>\r\n");
047: pWrt.print("<body>\r\n");
048: pWrt.print("Hello World!\r\n");
049: pWrt.print("</body>\r\n");
050: pWrt.print("</html>\r\n");
051: pWrt.flush();
052: }
053:
054: SSLContext createSSLContext() throws Exception {
055: KeyManagerFactory mgrFact = KeyManagerFactory
056: .getInstance("SunX509");
057: KeyStore serverStore = KeyStore.getInstance("JKS");
058:
059: serverStore.load(new ByteArrayInputStream(KeyStores.server),
060: SERVER_PASSWORD);
061:
062: mgrFact.init(serverStore, SERVER_PASSWORD);
063:
064: // set up a trust manager so we can recognize the server
065: TrustManagerFactory trustFact = TrustManagerFactory
066: .getInstance("SunX509");
067: KeyStore trustStore = KeyStore.getInstance("JKS");
068:
069: trustStore.load(new ByteArrayInputStream(KeyStores.trustStore),
070: TRUST_STORE_PASSWORD);
071:
072: trustFact.init(trustStore);
073:
074: // create a context and set up a socket factory
075: SSLContext sslContext = SSLContext.getInstance("TLS");
076:
077: sslContext.init(mgrFact.getKeyManagers(), trustFact
078: .getTrustManagers(), null);
079:
080: return sslContext;
081: }
082:
083: public void run() {
084: try {
085: SSLContext sslContext = createSSLContext();
086: SSLServerSocketFactory fact = sslContext
087: .getServerSocketFactory();
088:
089: SSLServerSocket sSock = (SSLServerSocket) fact
090: .createServerSocket(PORT_NO);
091: SSLSocket sslSock = (SSLSocket) sSock.accept();
092:
093: sslSock.startHandshake();
094:
095: readRequest(sslSock.getInputStream());
096:
097: SSLSession session = sslSock.getSession();
098:
099: sendResponse(sslSock.getOutputStream());
100:
101: sslSock.close();
102: } catch (Exception e) {
103: throw new RuntimeException(e);
104: }
105: }
106: }
|