diff options
author | Sergio Giro <sgiro@google.com> | 2016-02-01 18:54:35 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-02-01 18:54:35 +0000 |
commit | 3e75bd6b407dd472c834a50f16aae54cca67ea9c (patch) | |
tree | b5eb091b97b2aade28e5b45a15352125a4a776d7 /bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java | |
parent | 9218edabd1ef9852bc2f13115dcadc81b442dd6c (diff) | |
parent | c1040cb5656c3299f1c2d0fe0bd7c44b10466aaf (diff) | |
download | android_external_bouncycastle-3e75bd6b407dd472c834a50f16aae54cca67ea9c.tar.gz android_external_bouncycastle-3e75bd6b407dd472c834a50f16aae54cca67ea9c.tar.bz2 android_external_bouncycastle-3e75bd6b407dd472c834a50f16aae54cca67ea9c.zip |
Merge "Restoring the contents of aosp after"
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java | 113 |
1 files changed, 0 insertions, 113 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java b/bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java deleted file mode 100644 index d55bc73..0000000 --- a/bcprov/src/main/java/org/bouncycastle/crypto/tls/test/HTTPSServerThread.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.bouncycastle.crypto.tls.test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.security.KeyStore; - -import javax.net.ssl.KeyManagerFactory; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLServerSocket; -import javax.net.ssl.SSLServerSocketFactory; -import javax.net.ssl.SSLSession; -import javax.net.ssl.SSLSocket; -import javax.net.ssl.TrustManagerFactory; - -public class HTTPSServerThread - extends Thread -{ - private static final int PORT_NO = 8003; - private static final char[] SERVER_PASSWORD = "serverPassword".toCharArray(); - private static final char[] TRUST_STORE_PASSWORD = "trustPassword".toCharArray(); - - /** - * Read a HTTP request - */ - private void readRequest( - InputStream in) - throws IOException - { - int ch = 0; - int lastCh = 0; - while ((ch = in.read()) >= 0 && (ch != '\n' && lastCh != '\n')) - { - if (ch != '\r') - { - lastCh = ch; - } - } - } - - /** - * Send a response - */ - private void sendResponse( - OutputStream out) - { - PrintWriter pWrt = new PrintWriter(new OutputStreamWriter(out)); - pWrt.print("HTTP/1.1 200 OK\r\n"); - pWrt.print("Content-Type: text/html\r\n"); - pWrt.print("\r\n"); - pWrt.print("<html>\r\n"); - pWrt.print("<body>\r\n"); - pWrt.print("Hello World!\r\n"); - pWrt.print("</body>\r\n"); - pWrt.print("</html>\r\n"); - pWrt.flush(); - } - - SSLContext createSSLContext() - throws Exception - { - KeyManagerFactory mgrFact = KeyManagerFactory.getInstance("SunX509"); - KeyStore serverStore = KeyStore.getInstance("JKS"); - - serverStore.load(new ByteArrayInputStream(KeyStores.server), SERVER_PASSWORD); - - mgrFact.init(serverStore, SERVER_PASSWORD); - - // set up a trust manager so we can recognize the server - TrustManagerFactory trustFact = TrustManagerFactory.getInstance("SunX509"); - KeyStore trustStore = KeyStore.getInstance("JKS"); - - trustStore.load(new ByteArrayInputStream(KeyStores.trustStore), TRUST_STORE_PASSWORD); - - trustFact.init(trustStore); - - // create a context and set up a socket factory - SSLContext sslContext = SSLContext.getInstance("TLS"); - - sslContext.init(mgrFact.getKeyManagers(), trustFact.getTrustManagers(), null); - - return sslContext; - } - - public void run() - { - try - { - SSLContext sslContext = createSSLContext(); - SSLServerSocketFactory fact = sslContext.getServerSocketFactory(); - - SSLServerSocket sSock = (SSLServerSocket)fact.createServerSocket(PORT_NO); - SSLSocket sslSock = (SSLSocket)sSock.accept(); - - sslSock.startHandshake(); - - readRequest(sslSock.getInputStream()); - - SSLSession session = sslSock.getSession(); - - sendResponse(sslSock.getOutputStream()); - - sslSock.close(); - } - catch (Exception e) - { - throw new RuntimeException(e); - } - } -} |