Example usage for javax.net.ssl SSLServerSocket accept

List of usage examples for javax.net.ssl SSLServerSocket accept

Introduction

In this page you can find the example usage for javax.net.ssl SSLServerSocket accept.

Prototype

public Socket accept() throws IOException 

Source Link

Document

Listens for a connection to be made to this socket and accepts it.

Usage

From source file:SecureServer.java

public static void main(String[] args) throws Exception {
    ServerSocketFactory ssf = SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(98999);
    Socket sock = ss.accept();
    ss.close();/*  ww  w .  ja  v a2s  .c  om*/
    OutputStream rawOut = sock.getOutputStream();
    PrintWriter out = new PrintWriter(new OutputStreamWriter(rawOut));
    out.println(new java.util.Date().toString());
    out.flush();
    sock.close();

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(443);
    ss.setNeedClientAuth(true);/*  w w w  .  ja va 2  s  . c  o  m*/
    while (true) {
        Socket s = ss.accept();

        SSLSession session = ((SSLSocket) s).getSession();
        Certificate[] cchain = session.getPeerCertificates();
        for (int j = 0; j < cchain.length; j++) {
            System.out.println(((X509Certificate) cchain[j]).getSubjectDN());
        }
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;
        while ((info = in.readLine()) != null) {
            System.out.println("now got " + info);
            if (info.equals(""))
                break;
        }

        out.println("HTTP/1.0 200 OK\nMIME_version:1.0");
        out.println("Content_Type:text/html");
        String c = "<html> <head></head><body> <h1> Hi,</h1></Body></html>";
        out.println("Content_Length:" + c.length());
        out.println("");
        out.println(c);
        out.close();
        s.close();
        in.close();
    }
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket ss = (SSLServerSocket) ssf.createServerSocket(8080);
    ss.setNeedClientAuth(true);/*  ww w  .  j av  a  2s  .c om*/

    while (true) {
        try {
            Socket s = ss.accept();
            OutputStream out = s.getOutputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            String line = null;
            while (((line = in.readLine()) != null) && (!("".equals(line)))) {
                System.out.println(line);
            }
            System.out.println("");

            StringBuffer buffer = new StringBuffer();
            buffer.append("<HTML>\n");
            buffer.append("<HEAD><TITLE>HTTPS Server</TITLE></HEAD>\n");
            buffer.append("<BODY>\n");
            buffer.append("<H1>Success!</H1>\n");
            buffer.append("</BODY>\n");
            buffer.append("</HTML>\n");

            String string = buffer.toString();
            byte[] data = string.getBytes();
            out.write("HTTP/1.0 200 OK\n".getBytes());
            out.write(new String("Content-Length: " + data.length + "\n").getBytes());
            out.write("Content-Type: text/html\n\n".getBytes());
            out.write(data);
            out.flush();

            out.close();
            in.close();
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:Test.java

public static void main(String[] arstring) throws Exception {
        SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory
                .getDefault();/*  w  w  w.  ja v a2  s  . c o  m*/
        SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(9999);
        System.out.println("Waiting for a client ...");
        SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();

        SSLParameters parameters = sslSocket.getSSLParameters();
        parameters.setAlgorithmConstraints(new SimpleConstraints());

        AlgorithmConstraints constraints = parameters.getAlgorithmConstraints();
        System.out.println("Constraint: " + constraints);

        String endPoint = parameters.getEndpointIdentificationAlgorithm();
        System.out.println("End Point: " + endPoint);

        System.out.println("Local Supported Signature Algorithms");
        if (sslSocket.getSession() instanceof ExtendedSSLSession) {
            ExtendedSSLSession extendedSSLSession = (ExtendedSSLSession) sslSocket.getSession();
            String alogrithms[] = extendedSSLSession.getLocalSupportedSignatureAlgorithms();
            for (String algorithm : alogrithms) {
                System.out.println("Algortihm: " + algorithm);
            }
        }

        System.out.println("Peer Supported Signature Algorithms");
        if (sslSocket.getSession() instanceof ExtendedSSLSession) {
            String alogrithms[] = ((ExtendedSSLSession) sslSocket.getSession())
                    .getPeerSupportedSignatureAlgorithms();
            for (String algorithm : alogrithms) {
                System.out.println("Algortihm: " + algorithm);
            }
        }

        InputStream inputstream = sslSocket.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader = new BufferedReader(inputstreamreader);

        SSLSession session = sslSocket.getHandshakeSession();
        if (session != null) {
            System.out.println("Last accessed: " + new Date(session.getLastAccessedTime()));
        }

        String string = null;
        while ((string = bufferedreader.readLine()) != null) {
            System.out.println(string);
            System.out.flush();
        }

    }

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(8080);
    String[] suites = serverSocket.getSupportedCipherSuites();
    for (int i = 0; i < suites.length; i++) {
        System.out.println(suites[i]);
    }/*from w  w w .  j av  a 2s  .c  o m*/
    serverSocket.setEnabledCipherSuites(suites);
    String[] protocols = serverSocket.getSupportedProtocols();
    for (int i = 0; i < protocols.length; i++) {
        System.out.println(protocols[i]);
    }
    SSLSocket socket = (SSLSocket) serverSocket.accept();
    socket.startHandshake();
    System.out.println(socket.getRemoteSocketAddress());
}

From source file:MainClass.java

public static void main(String[] args) {
    int port = Integer.parseInt(args[0]);

    try {//from w  ww. j  a v a  2  s.c  o  m
        System.out.println("Locating server socket factory for SSL...");
        SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();

        System.out.println("Creating a server socket on port " + port);
        SSLServerSocket serverSocket = (SSLServerSocket) factory.createServerSocket(port);

        String[] suites = serverSocket.getSupportedCipherSuites();
        System.out.println("Support cipher suites are:");
        for (int i = 0; i < suites.length; i++) {
            System.out.println(suites[i]);
        }
        serverSocket.setEnabledCipherSuites(suites);

        System.out.println("Support protocols are:");
        String[] protocols = serverSocket.getSupportedProtocols();
        for (int i = 0; i < protocols.length; i++) {
            System.out.println(protocols[i]);
        }

        System.out.println("Waiting for client...");
        SSLSocket socket = (SSLSocket) serverSocket.accept();

        System.out.println("Starting handshake...");
        socket.startHandshake();

        System.out.println("Just connected to " + socket.getRemoteSocketAddress());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.globus.gsi.jsse.SSLConfiguratorTest.java

private SSLServerSocket startServer(SSLConfigurator config)
        throws GlobusSSLConfigurationException, IOException {
    SSLServerSocketFactory sslserversocketfactory = config.createServerFactory();
    final SSLServerSocket sslserversocket = (SSLServerSocket) sslserversocketfactory.createServerSocket(9991);

    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(new Runnable() {
        /**/*from ww w  .ja  v a 2  s.c  om*/
         * When an object implementing interface <code>Runnable</code> is
         * used to create a thread, starting the thread causes the object's
         * <code>run</code> method to be called in that separately executing
         * thread.
         * <p/>
         * The general contract of the method <code>run</code> is that it
         * may take any action whatsoever.
         *
         * @see Thread#run()
         */
        public void run() {
            latch.countDown();
            try {
                SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
                InputStream inputstream = sslsocket.getInputStream();
                InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
                String line;
                while ((line = bufferedreader.readLine()) != null) {
                    builder.append(line);
                }
                assertEquals(builder.toString().trim(), "hello");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    return sslserversocket;
}

From source file:servidor.Servidor.java

public static void main(String[] arstring) {
    try {/*ww w. j  av a 2 s.  c om*/
        SSLServer server = new SSLServer();
        // Server needs some key material.  We'll use an OpenSSL/PKCS8 style key (possibly encrypted).
        String certificateChain = "/lib/server.crt";
        String privateKey = "/lib/server.key";
        char[] password = "clave".toCharArray();
        KeyMaterial km = new KeyMaterial(certificateChain, privateKey, password);

        server.setKeyMaterial(km);

        // These settings have to do with how we'll treat client certificates that are presented
        // to us.  If the client doesn't present any client certificate, then these are ignored.
        server.setCheckHostname(false); // default setting is "false" for SSLServer
        server.setCheckExpiry(true); // default setting is "true" for SSLServer
        server.setCheckCRL(true); // default setting is "true" for SSLServer

        // This server trusts all client certificates presented (usually people won't present
        // client certs, but if they do, we'll give them a socket at the very least).
        server.addTrustMaterial(TrustMaterial.TRUST_ALL);
        SSLServerSocket ss = (SSLServerSocket) server.createServerSocket(7443);
        SSLSocket s = (SSLSocket) ss.accept();
        PrintWriter writer = new PrintWriter(s.getOutputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
        System.out.println(reader.readLine());
    } catch (Exception e) {
    }

}

From source file:test.integ.be.fedict.commons.eid.client.SSLTest.java

@Test
public void testMutualSSL() throws Exception {

    Security.addProvider(new BeIDProvider());

    final KeyPair serverKeyPair = generateKeyPair();
    final PrivateKey serverPrivateKey = serverKeyPair.getPrivate();
    final DateTime notBefore = new DateTime();
    final DateTime notAfter = notBefore.plusDays(1);
    final X509Certificate serverCertificate = generateCACertificate(serverKeyPair, "CN=Test", notBefore,
            notAfter);//from w  w w.j a v  a2s  .  c  o  m

    final KeyManager keyManager = new ServerTestX509KeyManager(serverPrivateKey, serverCertificate);
    final TrustManager trustManager = new ServerTestX509TrustManager();
    final SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(new KeyManager[] { keyManager }, new TrustManager[] { trustManager }, new SecureRandom());

    final SSLServerSocketFactory sslServerSocketFactory = sslContext.getServerSocketFactory();

    final int serverPort = 8443;
    final SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory
            .createServerSocket(serverPort);

    sslServerSocket.setNeedClientAuth(true);

    final TestRunnable testRunnable = new TestRunnable(serverPort);
    final Thread thread = new Thread(testRunnable);
    thread.start();

    SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
    LOG.debug("server accepted");
    InputStream inputStream = sslSocket.getInputStream();
    int result = inputStream.read();
    LOG.debug("result: " + result);
    assertEquals(12, result);
    SSLSession sslSession = sslSocket.getSession();
    sslSession.invalidate();
    sslSocket = (SSLSocket) sslServerSocket.accept();
    inputStream = sslSocket.getInputStream();
    result = inputStream.read();
    LOG.debug("result: " + result);
    assertEquals(34, result);
}