-1

I am trying to do a quick HTTP GET over SSL Socket in Java without using external libraries or the HttpURLConnection class and noticed that the results are varying depending on the type of HTTP Server in use.

Here's the main Java code:

    private static SSLContext context = null;
    private static KeyStore keystore = null;
    private static SSLSocketFactory sslClientSockFactory = null;
    private static KeyManagerFactory kmFactory = null;
    private static KeyManager[] keyMan = null;
    private static TrustManager[] trustMan = null;
    private static int portNum = 443;
    private static String address = "www.google.com";
    protected static final String sslContext = "TLSv1.2";
    private static OutputStream sockOS = null;
    private static InputStream sockIS = null;

    public static void main(String[] args) {
        try {
            // Load HTTPS keystore from nothing
            keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(null, null);
            kmFactory = KeyManagerFactory.getInstance("PKIX");
            kmFactory.init(keystore, null);
            keyMan = kmFactory.getKeyManagers();
            trustMan = new TrustManager[] { new TrustedTrustManager() };
            context = SSLContext.getInstance(sslContext);
            context.init(keyMan, trustMan, null);
            sslClientSockFactory = context.getSocketFactory();

            // Open HTTPS socket
            System.out.println("Attempting to open client ssl sock: " + address + ":" + portNum);
            if (sslClientSockFactory == null) {
                System.out.println("[ERR] Client socket factory is NULL !!!");
            }
            SSLSocket socket = (SSLSocket) sslClientSockFactory.createSocket(address, portNum);

            boolean isConnected = socket.isConnected();
            System.out.println("Is Connected: " + isConnected);
            System.out.println("Is Closed: " + socket.isClosed());
            System.out.println("Is InputShutdown: " + socket.isInputShutdown());
            System.out.println("Is OutputShutdown: " + socket.isOutputShutdown());

            if (isConnected) {    
                String reqStr = "GET / HTTP/1.1\r\n"
                        + "Host: www.google.com\r\n"
                        + "User-Agent: curl/7.79.1\r\n"
                        + "Accept: */*\r\n\r\n";
                
                sockOS = socket.getOutputStream();
                sockIS = socket.getInputStream();
                
                sockOS.write(reqStr.getBytes());
                sockOS.flush();
                    
                byte[] response = sockIS.readAllBytes();
                System.out.println("Response Len: " + response.length);
                socket.close();
            } else {
                System.out.println("[ERR] Failed to connect to destination server ...");
            }
        } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException
                | UnrecoverableKeyException | KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

For the TrustedTrustManager code:

public class TrustedTrustManager implements X509TrustManager {

    @Override
    public X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
            String authType) {
    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
            String authType) {
    }   
}

Somehow, I could access a couple of lighttpd hosted webpages but this does not extend to Google's and many other types of servers.

An example of a successful querying of lighttpd's own homepage:

Attempting to open client ssl sock: www.lighttpd.net:443
Is Connected: true
Is Closed: false
Is InputShutdown: false
Is OutputShutdown: false
Request String: 
GET / HTTP/1.1
Host: www.lighttpd.net
User-Agent: curl/7.79.1
Accept: */*


Response Len: 5670
Response: 
HTTP/1.1 200 OK
ETag: "3058255306"
Last-Modified: Wed, 19 Jan 2022 18:07:58 GMT
Accept-Ranges: bytes
Content-Type: text/html
Vary: Accept-Encoding
Strict-Transport-Security: max-age=31536000
Content-Length: 5387
Date: Tue, 17 May 2022 15:24:50 GMT
Server: lighttpd/2.0.0

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Home - Lighttpd - fly light</title>

    <link type="application/atom+xml" href="http://www.lighttpd.net/feed/atom.xml" rel="alternate" title="Atom feed" />

    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/app.css" rel="stylesheet">
</head>
<body>

    <div class="container-fluid"><div class="row-fluid">

        <div class="span2">
            <div style="width: 170px; margin: 0 auto;">
                <a href="/" id="logo" alt="lighttpd"></a>
                <div class="well well-small">
                    <ul class="nav nav-list">
                        <li class="nav-header">
                            Search
                            <a class="pull-right feedicon" href="/feed/atom.xml">
                                <i class="feedicon-small"></i>
                            </a>
                        </li>
                        <li>
                            .....More info not shown here .....    


            </div>

        </div>

    </div></div>

    <script src="/js/jquery-1.8.3.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
</body>
</html>

Any idea of what my code is lacking which causes it to work on lighttpd hosted servers but not on other types of servers would be greatly appreciated.

thotheolh
  • 7,040
  • 7
  • 33
  • 49

1 Answers1

0

Figured out the answer. It's a simple fix.

Thanks for the -1 .

Attempting to open client ssl sock: www.google.com:443
Is Connected: true
Is Closed: false
Is InputShutdown: false
Is OutputShutdown: false
HTTP/1.1 200 OK
Date: Tue, 17 May 2022 16:22:17 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2022-05-17-16; expires=Thu, 16-Jun-2022 16:22:17 GMT; path=/; domain=.google.com; Secure
Set-Cookie: AEC=AakniGPcJmAKB3nIVsBIA3Xg-vq9cxGQ2HOxdRvp15U76U7IfOSWP-lCRg; expires=Sun, 13-Nov-2022 16:22:17 GMT; path=/; domain=.google.com; Secure; HttpOnly; SameSite=lax
qzuW9eN5utjjrbwbmGqzvjyQ1GHADHuwEkkL5tX8RKae7FNAmKXm-4QoGF9nS6wmwhGZeMq8gnVEn27pRI4-P7oAxAyg; expires=Wed, 16-Nov-2022 16:22:17 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

3a48

.....More info not shown here .....

\x22stok\x22:\x22fJdxZy6Sfc7-Pi0HckoyI_RHf40\x22,\x22uhde\x22:false}}';google.pmc=JSON.parse(pmc);})();</script>        </body></html>
0
thotheolh
  • 7,040
  • 7
  • 33
  • 49