H
Hal Vaughan
The code for this is long and I can post the entire thing or post parts as
needed. I have a hunch the issue is probably not so much one of code, but
some "obvious" point I've missed that newbies working with SSL don't get.
I have a port forwarding program. It has a server port and a client port.
I'm using netcat to test it. When I run the program, I start with a
listening copy of netcat acting as a server, run my program and the client
socket in my program connects to the listening netcat. Then I run another
netcat that connects with the ServerSocket in my program. I type something
in the 2nd instance of netcat, my program prints it out (it tracks what it
is forwarding), then the listening instance of netcat also echos it. It
works the same in reverse. I can, without stopping or staring any of the
programs, also type in from the first instance of netcat (the listening
one), watch my program print it out, and watch it echoed on the other
instance of netcat.
So when I use a regular ServerSocket or Socket, it works fine. I'm not
using non-blocking IO, I'm just reading and writing from the InputStream
and OutputStream on each socket. I have noticed I need to use a regular
OutputStream on both sockets for writing so data is not delayed by a
BufferedOutputStream, but I am using a BufferedInputStream wrapped around
the InputStreams.
Once it worked, I added another forwarding program instance to it, like so:
(The numbers in parenthesis are port numbers.)
netcat (in/out: 6000) -->
forwarder (in: 6000, out: 6005) -->
forwarder (in: 6005, out: 6010) -->
netcat (in/out: 6010)
I added to this the ability to make either the server or client socket an
SSLServerSocket or SSLSocket, as needed. I followed a book (Java Tutorial
1.4) for directions on creating an SSLContext and using the right keystores
and key authorities for this. I have set, on the server
SSLServerSocket.setNeedClientAuth(true). At this point, since I easily
have keys for both, I'm requiring two way authentication. Now it looks
like this:
netcat (in/out: 6000) -->
forwarder (in: 6000, SSLout: 6005) -->
forwarder (SSLin: 6005, out: 6010) -->
netcat (in/out: 6010)
Basically, I've made the first forwarder use SSL on a client socket that
connects to an SSLServerSocket on the 2nd forwarder.
This is where it stops working. I type in a word in netcat, it is echoed by
the first forwarder, but immediately after that, the next line in the
program writes to the OutputStream of the client SSLSocket. After that
write (OutputStream.write(byte[]) statement I added a flush() to it and a
print statement for debugging. It turns out my forwarder gets the input,
echoes it with a print statement, but does NOT finish writing to the
OutputStream.
It is the same on the other end: when the 2nd forwarder receives, on its
client SSLSocket, data, it echoes it, but freezes on writing it to the
SSLServerServerSocket's OutputStream.
So what is going on? Why is it there's no problem sending data back and
forth, but once I change them into SSLSockets, write operations to the
OutputStreams lock up. I don't get any error messages, just a freeze up.
If anyone has any ideas what is causing this, I'd appreciate any help.
I've found it very difficult to get help on this entire topic, so I suspect
most people don't spend a lot of time programming sockets, so any help,
links, or info is definitely appreciated.
Thank you!
Hal
needed. I have a hunch the issue is probably not so much one of code, but
some "obvious" point I've missed that newbies working with SSL don't get.
I have a port forwarding program. It has a server port and a client port.
I'm using netcat to test it. When I run the program, I start with a
listening copy of netcat acting as a server, run my program and the client
socket in my program connects to the listening netcat. Then I run another
netcat that connects with the ServerSocket in my program. I type something
in the 2nd instance of netcat, my program prints it out (it tracks what it
is forwarding), then the listening instance of netcat also echos it. It
works the same in reverse. I can, without stopping or staring any of the
programs, also type in from the first instance of netcat (the listening
one), watch my program print it out, and watch it echoed on the other
instance of netcat.
So when I use a regular ServerSocket or Socket, it works fine. I'm not
using non-blocking IO, I'm just reading and writing from the InputStream
and OutputStream on each socket. I have noticed I need to use a regular
OutputStream on both sockets for writing so data is not delayed by a
BufferedOutputStream, but I am using a BufferedInputStream wrapped around
the InputStreams.
Once it worked, I added another forwarding program instance to it, like so:
(The numbers in parenthesis are port numbers.)
netcat (in/out: 6000) -->
forwarder (in: 6000, out: 6005) -->
forwarder (in: 6005, out: 6010) -->
netcat (in/out: 6010)
I added to this the ability to make either the server or client socket an
SSLServerSocket or SSLSocket, as needed. I followed a book (Java Tutorial
1.4) for directions on creating an SSLContext and using the right keystores
and key authorities for this. I have set, on the server
SSLServerSocket.setNeedClientAuth(true). At this point, since I easily
have keys for both, I'm requiring two way authentication. Now it looks
like this:
netcat (in/out: 6000) -->
forwarder (in: 6000, SSLout: 6005) -->
forwarder (SSLin: 6005, out: 6010) -->
netcat (in/out: 6010)
Basically, I've made the first forwarder use SSL on a client socket that
connects to an SSLServerSocket on the 2nd forwarder.
This is where it stops working. I type in a word in netcat, it is echoed by
the first forwarder, but immediately after that, the next line in the
program writes to the OutputStream of the client SSLSocket. After that
write (OutputStream.write(byte[]) statement I added a flush() to it and a
print statement for debugging. It turns out my forwarder gets the input,
echoes it with a print statement, but does NOT finish writing to the
OutputStream.
It is the same on the other end: when the 2nd forwarder receives, on its
client SSLSocket, data, it echoes it, but freezes on writing it to the
SSLServerServerSocket's OutputStream.
So what is going on? Why is it there's no problem sending data back and
forth, but once I change them into SSLSockets, write operations to the
OutputStreams lock up. I don't get any error messages, just a freeze up.
If anyone has any ideas what is causing this, I'd appreciate any help.
I've found it very difficult to get help on this entire topic, so I suspect
most people don't spend a lot of time programming sockets, so any help,
links, or info is definitely appreciated.
Thank you!
Hal