Java Code Examples for javax.net.ssl.SSLSocket#getInputStream()

The following examples show how to use javax.net.ssl.SSLSocket#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SSLconnection.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Constructor to setup and establish a connection.
 *
 * @param host as String describing the Service Access Point location i.e. hostname.
 * @param port as String describing the Service Access Point location i.e. TCP port.
 * @throws java.net.ConnectException in case of unrecoverable communication failures.
 * @throws java.io.IOException in case of continuous communication I/O failures.
 * @throws java.net.UnknownHostException in case of continuous communication I/O failures.
 */
public SSLconnection(String host, int port) throws ConnectException, IOException, UnknownHostException {
    logger.debug("SSLconnection({},{}) called.", host, port);
    logger.info("Starting {} bridge connection.", VeluxBindingConstants.BINDING_ID);
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("SSL");
        ctx.init(null, trustAllCerts, null);
    } catch (Exception e) {
        throw new IOException("create of an empty trust store failed.");
    }
    logger.trace("SSLconnection(): creating socket...");
    socket = (SSLSocket) ctx.getSocketFactory().createSocket(host, port);
    logger.trace("SSLconnection(): starting SSL handshake...");
    socket.startHandshake();
    dOut = new DataOutputStream(socket.getOutputStream());
    dIn = new DataInputStream(socket.getInputStream());
    logger.trace("SSLconnection() finished.");
}
 
Example 2
Source File: AnonCipherWithWantClientAuth.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void runServerApplication(SSLSocket socket) throws Exception {
    String ciphers[] = {
            "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA",
            "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5",
            "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA" };
    socket.setEnabledCipherSuites(ciphers);
    socket.setWantClientAuth(true);

    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslIS.read();
    sslOS.write(85);
    sslOS.flush();
}
 
Example 3
Source File: GenericBlockCipher.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void doServerSide() throws Exception {
    SSLServerSocketFactory sslssf =
        (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    SSLServerSocket sslServerSocket =
        (SSLServerSocket) sslssf.createServerSocket(serverPort);

    serverPort = sslServerSocket.getLocalPort();

    /*
     * Signal Client, we're ready for his connect.
     */
    serverReady = true;

    SSLSocket sslSocket = (SSLSocket) sslServerSocket.accept();
    InputStream sslIS = sslSocket.getInputStream();
    OutputStream sslOS = sslSocket.getOutputStream();

    sslIS.read();
    sslOS.write('A');
    sslOS.flush();

    sslSocket.close();
}
 
Example 4
Source File: HandshakeHashCloneExhaustion.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void runServerApplication(SSLSocket socket) throws Exception {
    socket.setNeedClientAuth(true);
    socket.setEnabledProtocols(protocol);
    socket.setEnabledCipherSuites(ciphersuite);

    // here comes the test logic
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslIS.read();
    sslOS.write(85);
    sslOS.flush();
}
 
Example 5
Source File: SSLSocketTemplate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected void runClientApplication(SSLSocket socket) throws Exception {
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslOS.write(280);
    sslOS.flush();
    sslIS.read();
}
 
Example 6
Source File: HandshakeHashCloneExhaustion.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void runClientApplication(SSLSocket socket) throws Exception {
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslOS.write(280);
    sslOS.flush();
    sslIS.read();
}
 
Example 7
Source File: HandshakeHashCloneExhaustion.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void runServerApplication(SSLSocket socket) throws Exception {
    socket.setNeedClientAuth(true);
    socket.setEnabledProtocols(protocol);
    socket.setEnabledCipherSuites(ciphersuite);

    // here comes the test logic
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslIS.read();
    sslOS.write(85);
    sslOS.flush();
}
 
Example 8
Source File: SSLSessionNulls.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void runServerApplication(SSLSocket socket) throws Exception {
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslIS.read();
    sslOS.write(85);
    sslOS.flush();
}
 
Example 9
Source File: ServerIdentityTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void runServerApplication(SSLSocket socket) throws Exception {
    InputStream sslIS = socket.getInputStream();
    sslIS.read();
    BufferedWriter bw = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream()));
    bw.write("HTTP/1.1 200 OK\r\n\r\n\r\n");
    bw.flush();
    socket.getSession().invalidate();
}
 
Example 10
Source File: SSLSender.java    From fluency with Apache License 2.0 5 votes vote down vote up
@Override
protected void recvResponse(SSLSocket sslSocket, ByteBuffer buffer)
        throws IOException
{
    InputStream inputStream = sslSocket.getInputStream();
    byte[] tempBuf = new byte[buffer.remaining()];
    int read = inputStream.read(tempBuf);
    buffer.put(tempBuf, 0, read);
}
 
Example 11
Source File: SSLSocketTemplate.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void runClientApplication(SSLSocket socket) throws Exception {
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslOS.write(280);
    sslOS.flush();
    sslIS.read();
}
 
Example 12
Source File: ECCurvesconstraints.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void doServerSide() throws Exception {
    SSLContext context = generateSSLContext(false);
    SSLServerSocketFactory sslssf = context.getServerSocketFactory();
    SSLServerSocket sslServerSocket =
        (SSLServerSocket)sslssf.createServerSocket(serverPort);
    serverPort = sslServerSocket.getLocalPort();

    /*
     * Signal Client, we're ready for his connect.
     */
    serverReady = true;

    SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
    try {
        sslSocket.setSoTimeout(5000);
        sslSocket.setSoLinger(true, 5);

        InputStream sslIS = sslSocket.getInputStream();
        OutputStream sslOS = sslSocket.getOutputStream();

        sslIS.read();
        sslOS.write('A');
        sslOS.flush();

        throw new Exception("EC curve secp224k1 should be disabled");
    } catch (SSLHandshakeException she) {
        // expected exception: no cipher suites in common
        System.out.println("Expected exception: " + she);
    } finally {
        sslSocket.close();
        sslServerSocket.close();
    }
}
 
Example 13
Source File: Connection.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
public void promoteToClientSSL() {
    SSLSocketFactory f = (SSLSocketFactory) SSLSocketFactory.getDefault();
    try {
        socket = (SSLSocket) f.createSocket(socket, null, socket.getPort(), false);
        in = socket.getInputStream();
        out = socket.getOutputStream();
    } catch (IOException ex) {

    }

}
 
Example 14
Source File: SSLSocketTemplate.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected void runServerApplication(SSLSocket socket) throws Exception {
    // here comes the test logic
    InputStream sslIS = socket.getInputStream();
    OutputStream sslOS = socket.getOutputStream();

    sslIS.read();
    sslOS.write(85);
    sslOS.flush();
}
 
Example 15
Source File: ECCurvesconstraints.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
void doClientSide() throws Exception {

        /*
         * Wait for server to get started.
         */
        while (!serverReady) {
            Thread.sleep(50);
        }

        SSLContext context = generateSSLContext(true);
        SSLSocketFactory sslsf = context.getSocketFactory();

        SSLSocket sslSocket =
            (SSLSocket)sslsf.createSocket("localhost", serverPort);

        try {
            sslSocket.setSoTimeout(5000);
            sslSocket.setSoLinger(true, 5);

            InputStream sslIS = sslSocket.getInputStream();
            OutputStream sslOS = sslSocket.getOutputStream();

            sslOS.write('B');
            sslOS.flush();
            sslIS.read();

            throw new Exception("EC curve secp224k1 should be disabled");
        } catch (SSLHandshakeException she) {
            // expected exception: Received fatal alert
            System.out.println("Expected exception: " + she);
        } finally {
            sslSocket.close();
        }
    }
 
Example 16
Source File: Connection.java    From IoTgo_Android_App with MIT License 4 votes vote down vote up
/**
 * Create an {@code SSLSocket} and perform the TLS handshake and certificate
 * validation.
 */
private void upgradeToTls(TunnelRequest tunnelRequest) throws IOException {
  Platform platform = Platform.get();

  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  if (requiresTunnel()) {
    makeTunnel(tunnelRequest);
  }

  // Create the wrapper over connected socket.
  socket = route.address.sslSocketFactory
      .createSocket(socket, route.address.uriHost, route.address.uriPort, true /* autoClose */);
  SSLSocket sslSocket = (SSLSocket) socket;
  if (route.modernTls) {
    platform.enableTlsExtensions(sslSocket, route.address.uriHost);
  } else {
    platform.supportTlsIntolerantServer(sslSocket);
  }

  boolean useNpn = route.modernTls && route.address.transports.contains("spdy/3");
  if (useNpn) {
    platform.setNpnProtocols(sslSocket, NPN_PROTOCOLS);
  }

  // Force handshake. This can throw!
  sslSocket.startHandshake();

  // Verify that the socket's certificates are acceptable for the target host.
  if (!route.address.hostnameVerifier.verify(route.address.uriHost, sslSocket.getSession())) {
    throw new IOException("Hostname '" + route.address.uriHost + "' was not verified");
  }

  out = sslSocket.getOutputStream();
  in = sslSocket.getInputStream();
  streamWrapper();

  byte[] selectedProtocol;
  if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
    if (Arrays.equals(selectedProtocol, SPDY3)) {
      sslSocket.setSoTimeout(0); // SPDY timeouts are set per-stream.
      spdyConnection = new SpdyConnection.Builder(route.address.getUriHost(), true, in, out)
          .build();
      spdyConnection.sendConnectionHeader();
    } else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
      throw new IOException(
          "Unexpected NPN transport " + new String(selectedProtocol, "ISO-8859-1"));
    }
  }
}
 
Example 17
Source File: Connection.java    From android-discourse with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@code SSLSocket} and perform the TLS handshake and certificate
 * validation.
 */
private void upgradeToTls(TunnelRequest tunnelRequest) throws IOException {
    Platform platform = Platform.get();

    // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
    if (requiresTunnel()) {
        makeTunnel(tunnelRequest);
    }

    // Create the wrapper over connected socket.
    socket = route.address.sslSocketFactory.createSocket(socket, route.address.uriHost, route.address.uriPort, true /* autoClose */);
    SSLSocket sslSocket = (SSLSocket) socket;
    if (route.modernTls) {
        platform.enableTlsExtensions(sslSocket, route.address.uriHost);
    } else {
        platform.supportTlsIntolerantServer(sslSocket);
    }

    boolean useNpn = route.modernTls && route.address.transports.contains("spdy/3");
    if (useNpn) {
        platform.setNpnProtocols(sslSocket, NPN_PROTOCOLS);
    }

    // Force handshake. This can throw!
    sslSocket.startHandshake();

    // Verify that the socket's certificates are acceptable for the target host.
    if (!route.address.hostnameVerifier.verify(route.address.uriHost, sslSocket.getSession())) {
        throw new IOException("Hostname '" + route.address.uriHost + "' was not verified");
    }

    out = sslSocket.getOutputStream();
    in = sslSocket.getInputStream();

    byte[] selectedProtocol;
    if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
        if (Arrays.equals(selectedProtocol, SPDY3)) {
            sslSocket.setSoTimeout(0); // SPDY timeouts are set per-stream.
            spdyConnection = new SpdyConnection.Builder(route.address.getUriHost(), true, in, out).build();
            spdyConnection.sendConnectionHeader();
        } else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
            throw new IOException("Unexpected NPN transport " + new String(selectedProtocol, "ISO-8859-1"));
        }
    }
}
 
Example 18
Source File: TestSsl.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testRenegotiateWorks() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    Assume.assumeTrue("SSL renegotiation has to be supported for this test",
            TesterSupport.isRenegotiationSupported(getTomcatInstance()));

    File appDir = new File(getBuildDirectory(), "webapps/examples");
    // app dir is relative to server home
    tomcat.addWebapp(null, "/examples", appDir.getAbsolutePath());

    TesterSupport.initSsl(tomcat);

    tomcat.start();

    SSLContext sslCtx = SSLContext.getInstance("TLS");
    sslCtx.init(null, TesterSupport.getTrustManagers(), null);
    SSLSocketFactory socketFactory = 
            new TesterSupport.NoSSLv2SocketFactory(sslCtx.getSocketFactory());
    SSLSocket socket = (SSLSocket) socketFactory.createSocket("localhost",
            getPort());

    OutputStream os = socket.getOutputStream();

    os.write("GET /examples/servlets/servlet/HelloWorldExample HTTP/1.1\n".getBytes());
    os.flush();

    socket.startHandshake();

    try {
        os.write("Host: localhost\n\n".getBytes());
    } catch (IOException ex) {
        ex.printStackTrace();
        fail("Re-negotiation failed");
    }

    InputStream is = socket.getInputStream();
    Reader r = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(r);
    String line = br.readLine();
    while (line != null) {
        // For testing System.out.println(line);
        line = br.readLine();
    }
}
 
Example 19
Source File: Connection.java    From cordova-amazon-fireos with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@code SSLSocket} and perform the TLS handshake and certificate
 * validation.
 */
private void upgradeToTls(TunnelRequest tunnelRequest) throws IOException {
  Platform platform = Platform.get();

  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  if (requiresTunnel()) {
    makeTunnel(tunnelRequest);
  }

  // Create the wrapper over connected socket.
  socket = route.address.sslSocketFactory
      .createSocket(socket, route.address.uriHost, route.address.uriPort, true /* autoClose */);
  SSLSocket sslSocket = (SSLSocket) socket;
  if (route.modernTls) {
    platform.enableTlsExtensions(sslSocket, route.address.uriHost);
  } else {
    platform.supportTlsIntolerantServer(sslSocket);
  }

  boolean useNpn = route.modernTls && route.address.transports.contains("spdy/3");
  if (useNpn) {
    platform.setNpnProtocols(sslSocket, NPN_PROTOCOLS);
  }

  // Force handshake. This can throw!
  sslSocket.startHandshake();

  // Verify that the socket's certificates are acceptable for the target host.
  if (!route.address.hostnameVerifier.verify(route.address.uriHost, sslSocket.getSession())) {
    throw new IOException("Hostname '" + route.address.uriHost + "' was not verified");
  }

  out = sslSocket.getOutputStream();
  in = sslSocket.getInputStream();
  streamWrapper();

  byte[] selectedProtocol;
  if (useNpn && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
    if (Arrays.equals(selectedProtocol, SPDY3)) {
      sslSocket.setSoTimeout(0); // SPDY timeouts are set per-stream.
      spdyConnection = new SpdyConnection.Builder(route.address.getUriHost(), true, in, out)
          .build();
      spdyConnection.sendConnectionHeader();
    } else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
      throw new IOException(
          "Unexpected NPN transport " + new String(selectedProtocol, "ISO-8859-1"));
    }
  }
}
 
Example 20
Source File: Connection.java    From cordova-android-chromeview with Apache License 2.0 4 votes vote down vote up
/**
 * Create an {@code SSLSocket} and perform the TLS handshake and certificate
 * validation.
 */
private void upgradeToTls(TunnelRequest tunnelRequest) throws IOException {
  Platform platform = Platform.get();

  // Make an SSL Tunnel on the first message pair of each SSL + proxy connection.
  if (requiresTunnel()) {
    makeTunnel(tunnelRequest);
  }

  // Create the wrapper over connected socket.
  socket = route.address.sslSocketFactory
      .createSocket(socket, route.address.uriHost, route.address.uriPort, true /* autoClose */);
  SSLSocket sslSocket = (SSLSocket) socket;
  if (route.modernTls) {
    platform.enableTlsExtensions(sslSocket, route.address.uriHost);
  } else {
    platform.supportTlsIntolerantServer(sslSocket);
  }

  if (route.modernTls) {
    platform.setNpnProtocols(sslSocket, NPN_PROTOCOLS);
  }

  // Force handshake. This can throw!
  sslSocket.startHandshake();

  // Verify that the socket's certificates are acceptable for the target host.
  if (!route.address.hostnameVerifier.verify(route.address.uriHost, sslSocket.getSession())) {
    throw new IOException("Hostname '" + route.address.uriHost + "' was not verified");
  }

  out = sslSocket.getOutputStream();
  in = sslSocket.getInputStream();

  byte[] selectedProtocol;
  if (route.modernTls
      && (selectedProtocol = platform.getNpnSelectedProtocol(sslSocket)) != null) {
    if (Arrays.equals(selectedProtocol, SPDY3)) {
      sslSocket.setSoTimeout(0); // SPDY timeouts are set per-stream.
      spdyConnection = new SpdyConnection.Builder(route.address.getUriHost(), true, in, out)
          .build();
    } else if (!Arrays.equals(selectedProtocol, HTTP_11)) {
      throw new IOException(
          "Unexpected NPN transport " + new String(selectedProtocol, "ISO-8859-1"));
    }
  }
}