Connecting to https with Socket - java

My app is based on Sockets, but regular Sockets, not SSLSockets. Do I have to change all of them to SSL sockets to be able connect to https server?

You must change how the socket is created, by using the SSLSocketFactory, but you don't have to change all your datatypes from Socket to SSLSocket.

Yes i think you have to change for SSL, you can try that :
System.setProperty("javax.net.ssl.trustStore", "clienttrust");
SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket s = ssf.createSocket("127.0.0.1", 8888);
OutputStream outs = s.getOutputStream();
PrintStream out = new PrintStream(outs);
InputStream ins = s.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
out.println("Hi,How are u!");
out.println("");
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
out.close();
it's from : http://www.java2s.com/Tutorial/Java/0490__Security/HttpsSocketClient.htm
Hope it helps !

Related

Server chat java - SSLException

I am stuck in a serious place..., I am trying to create a secure server. So I had to modify my code to be able to send in a secure way, the problem is the following.
It displays me the error: javax.net.ssl.SSLException: Connection has closed: javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
I already made a certificate via keytool but I don't have the impression that it works
I have looked everywhere but I can't find a solution to my problem...
Here are my codes:
Server :
System.setProperty("javax.net.ssl.keyStore", "test.store");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
ServerSocketFactory serverSocketFactory =
ServerSocketFactory.getDefault();
ServerSocket serverSocket =
serverSocketFactory.createServerSocket(4444);
System.out.println("Started...");
while(true){
new ServerThread(serverSocket.accept()).start();
}
Client
System.setProperty("javax.net.ssl.trustStore", "test.store");
SocketFactory socketFactory = SSLSocketFactory.getDefault();
Socket socket = socketFactory.createSocket("localhost", 4444);
BufferedReader bf1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter username");
writer.println(bf.readLine());
String message = null;
while(true){
System.out.println("enter message send to server");
message = bf.readLine();
if(message.equals("quit")){
socket.close();
break;
}
writer.println(message);
System.out.println("message from server : " + bf1.readLine());
}
I don't thing it is important to give you the serverThread cause it is not usefull.
Thank and have nice day :)
You server is not listening in SSL as you are calling the wrong factory. You would need SSLServerSocketFactory instead. Try
ServerSocketFactory serverSocketFactory = SSLServerSocketFactory.getDefault();

How to open ssl socket in already opened SSL Session java

I have HttpsURLConnection opened.
How can i "connect" my ssl socket to this connection, means not to do another ssl handshake and don't change my ciphers.
URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();
SSLSocketFactory ssf = connection.getSSLSocketFactory();
SSLSocket socket = (SSLSocket) ssf.createSocket("example.com", 8080);
// and here i want to send message with socket which will be in the same ssl session as https connection.
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(s.getOutputStream()));
out.write("Hello");
out.flush();
out.close();
Is it really to do such thing in Java?
It should happen that way by default, as long as the session remains valid, and provided you haven't messed around with SSLContexts.
AFAIK, you cannot attach a new socket to the existing SSL session. If you want to send/read data on the original SSL session, use the original socket connection created by the URLConnection object. You can use the URLConnection.getInputStream() and URLConnection.getOutputStream() methods to get the input/output streams for it:
URL url = new URL ("https://example.com:8080");
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.connect();
...
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
out.write("Hello");
out.flush();
out.close();

HTTP/1.0 403 Forbidden java ssl socket

I am new to java and need to open a TLS connection and send data but I am getting 403 forbidden error, am I missing something in the code?
javax.net.SocketFactory basicSocketFactory = javax.net.SocketFactory.getDefault();
java.net.Socket s = basicSocketFactory.createSocket(host,port);
javax.net.ssl.SSLSocketFactory tlsSocketFactory = (SSLSocketFactory) javax.net.ssl.SSLSocketFactory.getDefault();
s = tlsSocketFactory.createSocket(s, host, port, true);
if (s.isConnected()){
System.out.println("connected"); <-passes here
}
java.io.PrintWriter pw = new java.io.PrintWriter(s.getOutputStream());
pw.println("POST /direct.aspx HTTP/1.1\n\r\n");
pw.println("Host: "some host-sorry can't reveal"\n\r\n");
pw.println("Connection: Close\n\r\n");
pw.println("Content-type: application/x-www-form-urlencoded\n\r\n");
pw.println("Content-Length: " + Integer.toString(input.length())+"\n\r\n");
pw.println("Charset: UTF-8\n\r\n");
pw.println(input);
pw.flush();
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(s.getInputStream()));
String t;
while((t = br.readLine()) != null) System.out.println(t);
br.close();

Android : Simple HTTPS request and get response

I'm creating a android proxy server and i use this for HTTP request :
Socket socket = new Socket(InetAddress.getByName("perdu.com"), 80);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print("GET / HTTP/1.1\r\nHost: perdu.com\r\nConnection: close\r\n\r\n");
out.flush();
I doesn't work for HTTPS request, so my question is : How to replace this for SSL working and get server response ?
Thank you
PS: i'm french, sorry for my english :)
I found solution
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) factory.createSocket(domain, 443);
socket.startHandshake();
BufferedReader in = new BufferedReader(new InpuStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());

Socket Server will not receive properly

I am trying to make a socket server, I am connecting through putty to this server. Whenever I type "hi" it says "no" rather than "hi" which I want it to do. I found this on A java website. If you could tell me what I am doing wrong that would be great. Thanks!
int port = 12345;
ServerSocket sock = new ServerSocket(port);
System.out.println("Server now active on port: " + port);
Socket link = sock.accept();
System.out.println("Interface accepted request, IP: " + link.getInetAddress());
BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
PrintWriter output = new PrintWriter(link.getOutputStream(), true);
output.println("ISEEYOU");
String inputLine;
Thread.sleep(1500);
while((inputLine = input.readLine()) != null) {
if(inputLine.equals("hi")) {
output.println("hi");
}else{
output.println("no");
}
}
Your Java program is correct.
I've tried your code, just added System.out.printf("[%s]", inputLine); as first line in the while loop to ensure, what I get from putty.
I guess your problem is the protocol putty uses to connect. It worked with RAW for me. See below the session setting I've used:
EDIT:
According to your comment I added some code for a simple client, that reads the line from console, sends it to the server and prints the echo back to console.
public void Client() throws IOException {
// Client that closes the communication when the user types "quit"
Socket socket = new Socket("localhost", 8080);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
String line;
while(!(line = user.readLine()).equals("quit")) {
ps.println(line); // Write to server
System.out.println(reader.readLine()); // Receive echo
}
socket.shutdownOutput(); // Send EOF to server
socket.close();
}
The corresponding server would look like this:
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintStream ps = new PrintStream(socket.getOutputStream());
// Just read a line and echo it till EOF
String line;
while((line = reader.readLine()) != null) ps.println(line);
}
You might need to change the port I used here, if 8080 is already binded on your machine. Also you might want to have the server running on another computer then the client. In this case you need to change "localhost".

Categories