Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
 package gov.nist.core.net;
 
 
extended implementation of a network layer that allows to define a private java keystores/truststores

Author(s):
f.reif
Version:
1.2
Since:
1.2
 
 public class SslNetworkLayer implements NetworkLayer {
 
 
 
     public SslNetworkLayer(
             String trustStoreFile,
             String keyStoreFile,
             char[] keyStorePassword,
             String keyStoreTypethrows GeneralSecurityExceptionFileNotFoundExceptionIOException
     {
         SSLContext sslContext;
         sslContext = SSLContext.getInstance("TLS");
         String algorithm = KeyManagerFactory.getDefaultAlgorithm();
         TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(algorithm);
         KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(algorithm);
         SecureRandom secureRandom   = new SecureRandom();
         secureRandom.nextInt();
         KeyStore keyStore = KeyStore.getInstance(
              keyStoreType != null ? keyStoreType : KeyStore.getDefaultType());
         KeyStore trustStore = KeyStore.getInstance(
              keyStoreType != null ? keyStoreType : KeyStore.getDefaultType());
         keyStore.load(new FileInputStream(keyStoreFile), keyStorePassword);
         trustStore.load(new FileInputStream(trustStoreFile), keyStorePassword);
         tmFactory.init(trustStore);
         kmFactory.init(keyStorekeyStorePassword);
         sslContext.init(kmFactory.getKeyManagers(), tmFactory.getTrustManagers(), secureRandom);
          = sslContext.getServerSocketFactory();
          = sslContext.getSocketFactory();
     }
 
     public ServerSocket createServerSocket(int portint backlog,
             InetAddress bindAddressthrows IOException {
         return new ServerSocket(portbacklogbindAddress);
     }
 
     public Socket createSocket(InetAddress addressint port)
             throws IOException {
         return new Socket(addressport);
     }
 
        return new DatagramSocket();
    }
    public DatagramSocket createDatagramSocket(int portInetAddress laddr)
            throws SocketException {
        return new DatagramSocket(portladdr);
    }
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
    public SSLServerSocket createSSLServerSocket(int portint backlog,
            InetAddress bindAddressthrows IOException {
                portbacklogbindAddress);
    }
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
    public SSLSocket createSSLSocket(InetAddress addressint port)
            throws IOException {
        return (SSLSocket.createSocket(addressport);
    }
    /* Added by Daniel J. Martinez Manzano <dani@dif.um.es> */
    public SSLSocket createSSLSocket(InetAddress addressint port,
            InetAddress myAddressthrows IOException {
        return (SSLSocket.createSocket(addressport,
                myAddress, 0);
    }
    public Socket createSocket(InetAddress addressint port,
            InetAddress myAddressthrows IOException {
        if (myAddress != null)
            return new Socket(addressportmyAddress, 0);
        else
            return new Socket(addressport);
    }

    
Creates a new Socket, binds it to myAddress:myPort and connects it to address:port.

Parameters:
address the InetAddress that we'd like to connect to.
port the port that we'd like to connect to
myAddress the address that we are supposed to bind on or null for the "any" address.
myPort the port that we are supposed to bind on or 0 for a random one.
Returns:
a new Socket, bound on myAddress:myPort and connected to address:port.
Throws:
java.io.IOException if binding or connecting the socket fail for a reason (exception relayed from the correspoonding Socket methods)
    public Socket createSocket(InetAddress addressint port,
                    InetAddress myAddressint myPort)
        throws IOException
    {
        if (myAddress != null)
            return new Socket(addressportmyAddressmyPort);
        else if (port != 0)
        {
            //myAddress is null (i.e. any)  but we have a port number
            Socket sock = new Socket();
            sock.bind(new InetSocketAddress(port));
            sock.connect(new InetSocketAddress(addressport));
            return sock;
        }
        else
            return new Socket(addressport);
    }
New to GrepCode? Check out our FAQ X