package org.bouncycastle.tls.test;

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 java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCertificate;
import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCrypto;
import org.bouncycastle.tls.crypto.impl.jcajce.JcaTlsCryptoProvider;

public class HTTPSServerThread
    extends Thread
{
    private static final int PORT_NO = 12001;
    private static final char[] SERVER_PASSWORD = "serverPassword".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
    {
        JcaTlsCrypto crypto = new JcaTlsCryptoProvider().create(new SecureRandom());
        X509Certificate certificate = ((JcaTlsCertificate)TlsTestUtils.loadCertificateResource(crypto,
            "x509-server-rsa-sign.pem")).getX509Certificate();
        PrivateKey privateKey = TlsTestUtils.loadJcaPrivateKeyResource(crypto, "x509-server-key-rsa-sign.pem");

        KeyStore serverStore = KeyStore.getInstance("JKS");
        serverStore.load(null, null);
        serverStore.setKeyEntry("server", privateKey, SERVER_PASSWORD, new X509Certificate[]{ certificate });

        KeyManagerFactory mgrFact = TlsTestUtils.getSunX509KeyManagerFactory();
        mgrFact.init(serverStore, SERVER_PASSWORD);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(mgrFact.getKeyManagers(), null, 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.setUseClientMode(false);

            readRequest(sslSock.getInputStream());

//            SSLSession session =
            sslSock.getSession();

            sendResponse(sslSock.getOutputStream());

            sslSock.close();
            sSock.close();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
}

@@ -318,16 +318,41 @@
 ificate;
+%0Aimport java.util.Vector;
 %0A%0Aimport
@@ -532,16 +532,58 @@
 LSocket;
+%0Aimport javax.net.ssl.TrustManagerFactory;
 %0A%0Aimport
@@ -2450,24 +2450,121 @@
 ficate %7D);%0A%0A
+        KeyStore trustStore = KeyStore.getInstance(%22JKS%22);%0A        trustStore.load(null, null);%0A%0A
         KeyM
@@ -2680,24 +2680,147 @@
 PASSWORD);%0A%0A
+        TrustManagerFactory trustFact = TlsTestUtils.getSunX509TrustManagerFactory();%0A        trustFact.init(trustStore);%0A%0A
         SSLC
@@ -2920,20 +2920,44 @@
 gers(), 
-null
+trustFact.getTrustManagers()
 , null);
@@ -2987,24 +2987,565 @@
 ext;%0A    %7D%0A%0A
+    void disableRSAKeyExchange(SSLSocket s)%0A    %7B%0A        String%5B%5D cipherSuites = s.getEnabledCipherSuites();%0A%0A        Vector v = new Vector();%0A        for (String cipherSuite : cipherSuites)%0A        %7B%0A            if (!cipherSuite.regionMatches(true, 0, %22SSL_RSA_%22, 0, %22SSL_RSA_%22.length()) &&%0A                !cipherSuite.regionMatches(true, 0, %22TLS_RSA_%22, 0, %22TLS_RSA_%22.length()))%0A            %7B%0A                v.addElement(cipherSuite);%0A            %7D%0A        %7D%0A%0A        s.setEnabledCipherSuites((String%5B%5D)v.toArray(new String%5B0%5D));%0A    %7D%0A%0A
     public v
@@ -3860,24 +3860,68 @@
 k.accept();%0A
+            disableRSAKeyExchange(sslSock);%0A
             

Disable RSA key exchange in test