Example #1
0
  /**
   * Connect to the underlying secure socket transport. Perform the SSL handshake and then proceded
   * to the underlying HTTP protocol connect semantics.
   *
   * @return SSL/TCP stream connection
   * @exception IOException is thrown if the connection cannot be opened
   */
  protected StreamConnection connect() throws IOException {
    String httpsTunnel;
    com.sun.midp.io.j2me.socket.Protocol tcpConnection;
    OutputStream tcpOutputStream;
    InputStream tcpInputStream;
    X509Certificate serverCert;

    verifyPermissionCheck();

    /*
     * To save memory for applications the do not use HTTPS,
     * the public keys of the certificate authorities may not
     * have been loaded yet.
     */
    WebPublicKeyStore.loadCertificateAuthorities();

    // Open socket connection
    tcpConnection = new com.sun.midp.io.j2me.socket.Protocol();

    // check to see if a protocol is specified for the tunnel
    httpsTunnel = Configuration.getProperty("com.sun.midp.io.http.proxy");
    if (httpsTunnel != null) {
      // Make the connection to the ssl tunnel
      tcpConnection.openPrim(classSecurityToken, "//" + httpsTunnel);

      // Do not delay request since this delays the response.
      tcpConnection.setSocketOption(SocketConnection.DELAY, 0);

      tcpOutputStream = tcpConnection.openOutputStream();
      tcpInputStream = tcpConnection.openInputStream();

      // Do the handshake with the ssl tunnel
      try {
        doTunnelHandshake(tcpOutputStream, tcpInputStream);
      } catch (IOException ioe) {
        String temp = ioe.getMessage();

        tcpConnection.close();
        tcpOutputStream.close();
        tcpInputStream.close();

        if (temp.indexOf(" 500 ") > -1) {
          throw new ConnectionNotFoundException(temp);
        }

        throw ioe;
      }
    } else {
      tcpConnection.openPrim(classSecurityToken, "//" + hostAndPort);

      // Do not delay request since this delays the response.
      tcpConnection.setSocketOption(SocketConnection.DELAY, 0);

      tcpOutputStream = tcpConnection.openOutputStream();
      tcpInputStream = tcpConnection.openInputStream();
    }

    tcpConnection.close();

    try {
      // Get the SSLStreamConnection
      sslConnection = new SSLStreamConnection(url.host, url.port, tcpInputStream, tcpOutputStream);
    } catch (Exception e) {
      try {
        tcpInputStream.close();
      } finally {
        try {
          tcpOutputStream.close();
        } finally {
          if (e instanceof IOException) {
            throw (IOException) e;
          } else {
            throw (RuntimeException) e;
          }
        }
      }
    }

    try {
      serverCert = sslConnection.getServerCertificate();

      /*
       * if the subject alternate name is a DNS name,
       * then use that instead of the common name for a
       * site name match
       */
      if (serverCert.getSubjectAltNameType() == X509Certificate.TYPE_DNS_NAME) {
        if (!checkSiteName(url.host, (String) serverCert.getSubjectAltName())) {
          throw new CertificateException(
              "Subject alternative name did not match site name",
              serverCert,
              CertificateException.SITENAME_MISMATCH);
        }
      } else {
        String cname = getCommonName(serverCert.getSubject());
        if (cname == null) {
          throw new CertificateException(
              "Common name missing from subject name",
              serverCert,
              CertificateException.SITENAME_MISMATCH);
        }

        if (!checkSiteName(url.host, cname)) {
          throw new CertificateException(serverCert, CertificateException.SITENAME_MISMATCH);
        }
      }

      return sslConnection;
    } catch (Exception e) {
      try {
        sslConnection.close();
      } finally {
        if (e instanceof IOException) {
          throw (IOException) e;
        } else {
          throw (RuntimeException) e;
        }
      }
    }
  }