This discussion is archived
6 Replies Latest reply: May 15, 2010 7:38 PM by EJP RSS

[newbie]: SSLSocket for https via Proxy

843811 Newbie
Currently Being Moderated
Hi,

I have to go through a proxy here:
I use the code below, does this provides "Basic:" Authentication w/ Base64 encoding?

May problem is, that I have a VB app (not made by me) which connects fine to the host, but mine fails to connect ... and it has something to do with proxy

Thx in advance


LoCal

      System.setProperty("proxySet","true");
        System.setProperty("https.proxyHost", prxyhost);
        System.setProperty("https.proxyPort",prxyport);
        System.setProperty("proxyUser", prxyuid);
        System.setProperty("proxyPassword", prxypwd);

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
            
            SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
            Writer out = new OutputStreamWriter(socket.getOutputStream());
            out.write(daPost);
            out.flush();
  • 1. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    check out this url

    http://www.javaworld.com/javaworld/javatips/jw-javatip111-p2.html
  • 2. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    Thanks a lot. In the meantime I found "another way around" but the article is quite cool....maybe I'll change my code....

    Thx again,

    LoCal
  • 3. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    I get an error message 'untrusted server cert chain' when i try to make an https request thru the proxy.

    I'm doing exactly what is mentioned in the article.

    Is there a way to use the SSLContext along with the solution mentioned in the article?? That way I can override the TrustManager.

    Any help would be appreciated. Thanx.
  • 4. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    It looks like you have solved the exact problem I am trying to solve. Any help would be greately appreciated.

    I am trying to open SSLConnection from a client to the server via proxy. Here is my code that is supposed to open a Socket.
      
      Socket getConnection(String remoteHost, int remotePort) throws Exception
      {
        SSLSocket sslSocket;
        Socket socket = null;
        Proxy proxy = super.getProxy(remoteHost, remotePort); // This returns the proxy object
        InetSocketAddress inetSocketAddress = super.getProxyInetSocketAddress(proxy); // This returns proxy address
        
        char keystorepass[] = PASSWORD.toCharArray();
        KeyStore keyStore = KeyStore.getInstance(JAVA_KEYSTORE);
        
        keyStore.load(null, keystorepass);
        
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X509);
        keyManagerFactory.init(keyStore, keystorepass);
        
        TrustManager[] trustManager = new TrustManager[] {new MyX509TrustManager()};
        SSLContext sslContext = SSLContext.getInstance(PROTOCOL_LEVEL);
        
        sslContext.init(keyManagerFactory.getKeyManagers(), trustManager, null);
        
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        //InetSocketAddress inetSocketAddress = getProxyInetSocketAddress(host, port);
        
        if (super.isProxyDetected())
        {
          System.out.println("connecting with Proxy");
          //getSocket(host, port, false) Creates a socket with 
          sslSocket = (SSLSocket) sslSocketFactory.createSocket(new Socket(proxy), remoteHost, remotePort, true);
        }
        else
        {
          System.out.println("connecting without Proxy");
          sslSocket = (SSLSocket) sslSocketFactory.createSocket(remoteHost, remotePort);
        }
        
        System.out.println("SSLSocket:" + sslSocket);
        
        if (sslSocket != null)
        {
          socket = sslSocket;
          sslSocket.startHandshake();
        }
        
        System.out.println("Socket:" + socket);
        
        return socket;
      }
  • 5. Re: [newbie]: SSLSocket for https via Proxy
    843811 Newbie
    Currently Being Moderated
    cannot work! Please help me !!!!!!!!!

    SSLContext ctx;
    try {
    ctx = SSLContext.getInstance("SSLv3");
    // 创建密钥管理器
    KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
    // 创建证书管理器
    TrustManagerFactory tmf = TrustManagerFactory
    .getInstance("SunX509");
    // 用来存储密钥
    KeyStore ks = KeyStore.getInstance("JKS");
    // 用来存储证书
    KeyStore tks = KeyStore.getInstance("JKS");
    // 加载客户端密钥
    ks.load(new FileInputStream(Constants.CLIENT_KEYSTORE_PATH),
    Constants.CLIENT_KEY_STORE_PASSWORD.toCharArray());
    // 加载服务端证书,由服务端密钥生成
    tks.load(new FileInputStream(Constants.CLIENT_TRUSTSTORE_PATH),
    Constants.CLIENT_TRUST_KEY_STORE_PASSWORD.toCharArray());
    // 使用密钥初始化密钥管理器
    kmf.init(ks, Constants.CLIENT_KEY_STORE_PASSWORD.toCharArray());
    // 使用证书初始化证书管理器
    tmf.init(tks);
    ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

    String prxyhost = "192.168.5.25";
    int prxyport = 1080;
    SocketAddress addr = new InetSocketAddress(prxyhost, Integer
    .valueOf(prxyport));
    Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
    Socket socket = new Socket(proxy);
    sslSocket = ctx.getSocketFactory().createSocket(socket,Constants.DEFAULT_HOST,9500,false);

    EXCEPTION ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    java.net.SocketException: Underlying socket is not connected
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source)
    at com.aastocks.ssl.Client.init(Client.java:171)
    at com.aastocks.ssl.Client.createSSLConn(Client.java:54)
    at com.aastocks.ssl.Client.main(Client.java:47)
    2010-05-13 09:51:21 [INFO] main [com.aastocks.ssl.Client] - Create sslSocket error
  • 6. Re: [newbie]: SSLSocket for https via Proxy
    EJP Guru
    Currently Being Moderated
    Socket socket = new Socket(proxy);
    socket.connect();
    java.net.SocketException: Underlying socket is not connected