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.