Java SSLServerSocket create

Description

Java SSLServerSocket create


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.AlgorithmConstraints;
import java.security.AlgorithmParameters;
import java.security.CryptoPrimitive;
import java.security.Key;
import java.security.interfaces.RSAKey;
import java.util.Date;
import java.util.Set;

import javax.net.ssl.ExtendedSSLSession;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

class SimpleConstraints implements AlgorithmConstraints {
   public boolean permits(Set<CryptoPrimitive> primitives, String algorithm, AlgorithmParameters parameters) {
      return permits(primitives, algorithm, null, parameters);
   }/*from   w  w  w  .  j  a  v  a2s .  c  o  m*/

   public boolean permits(Set<CryptoPrimitive> primitives, Key key) {
      return permits(primitives, null, key, null);
   }

   public boolean permits(Set<CryptoPrimitive> primitives, String algorithm, Key key, AlgorithmParameters parameters) {
      if (algorithm == null)
         algorithm = key.getAlgorithm();

      if (algorithm.indexOf("RSA") == -1)
         return false;

      if (key != null) {
         RSAKey rsaKey = (RSAKey) key;
         int size = rsaKey.getModulus().bitLength();
         if (size < 2048)
            return false;
      }

      return true;
   }
}

//EchoServer
public class Main {

   public static void main(String[] arstring) {
      try {
         SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
         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();
         }
      } catch (Exception exception) {
         exception.printStackTrace();
      }
   }

}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

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

public class Main {
   public static void main(String[] args) {
      try {/*  ww w  . j a va  2 s.  c  o  m*/
         SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
         SSLServerSocket sss = (SSLServerSocket) factory.createServerSocket(6789);
         SSLSocket ss = (SSLSocket) sss.accept();
         BufferedReader in = new BufferedReader(new InputStreamReader(ss.getInputStream()));
         PrintWriter out = new PrintWriter(ss.getOutputStream(), true);

         String line = null;
         while ((line = in.readLine()) != null) {
            System.out.println("received<-- " + line);
            out.println(line);
            System.out.println("sent    --> " + line);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}



PreviousNext

Related