networking - EOFException: java.io.EOFException is thrown in Java client-server application -
i've programmed following client-server pair set simplified version of ipsec-connection (cryptography-related).
the problem is, on second call readobject(), i.e.:
// receive finished message server finishedmessage = (biginteger) inputstream.readobject();
i java.io.eofexception.
it should said, on runs eofexception
thrown, on runs runs flawlessly ?
i've been debugging hours now, can't find error.
if can see error, please let me know - appreciate !
error message:
[client]: connected... [client]: common key = 33569 java.io.eofexception @ java.io.objectinputstream$blockdatainputstream.peekbyte(unknown source) @ java.io.objectinputstream.readobject0(unknown source) @ java.io.objectinputstream.readobject(unknown source) @ ipsecclient.setupsslconnection(ipsecclient.java:68) @ ipsecclient.main(ipsecclient.java:116)
client:
import java.math.biginteger; import java.net.*; import java.util.arraylist; import java.util.random; import java.io.*; public class ipsecclient { private socket socket; private objectinputstream inputstream; private objectoutputstream outputstream; private ipsec gen; private arraylist<biginteger[]> messages; private biginteger[] message; private final int port, numbits; private string address; private random rand; private int fixednumber; private biginteger fixedprime, fixedbase, partialkeyclient, partialkeyserver, commonkey, publickeyserver, modulusserver; public ipsecclient() { rand = new random(); numbits = 256; fixednumber = rand.nextint(1000); fixedprime = new biginteger("51803"); fixedbase = new biginteger("3"); gen = new ipsec(); gen.keygen(numbits); messages = new arraylist<biginteger[]>(); port = 5000; address = "localhost"; } public void setupsslconnection() { try { socket = new socket(address, port); outputstream = new objectoutputstream(socket.getoutputstream()); inputstream = new objectinputstream(socket.getinputstream()); system.out.println("[client]: connected..."); // send partial key , certificate (public key) server partialkeyclient = fixedbase.pow(fixednumber).mod(fixedprime); message = new biginteger[] {partialkeyclient, gen.publickey(), gen.modulus()}; messages.add(message); outputstream.writeobject(message); outputstream.flush(); // receive partial key , certificate server message = (biginteger[]) inputstream.readobject(); messages.add(message); partialkeyserver = message[0]; publickeyserver = message[1]; modulusserver = message[2]; // generate common key commonkey = partialkeyserver.pow(fixednumber).mod(fixedprime); system.out.println("[client]: common key = " + commonkey.intvalue()); // send finished message biginteger accumulatedmessages = accumulatemessages(messages).mod(gen.publickey()); biginteger finishedmessage = gen.generatersasignature(accumulatedmessages); outputstream.writeobject(finishedmessage); outputstream.flush(); // receive finished message server finishedmessage = (biginteger) inputstream.readobject(); // verify finished message boolean result = gen.verifyrsasignature(accumulatemessages(messages).mod(publickeyserver), finishedmessage, publickeyserver, modulusserver); system.out.println("[client]: verification of finished message " + (result ? "succeeded" : "failed")); if (!result) { system.out.println("[client]: ssl-connection not estasblished..."); closeconnection(-1); } system.out.println("[client]: ssl-connection estasblished..."); closeconnection(0); } catch (socketexception se) { se.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } } private void closeconnection(int exitcode) { try { socket.close(); outputstream.close(); inputstream.close(); system.exit(exitcode); } catch (ioexception e) { e.printstacktrace(); } } private biginteger accumulatemessages(arraylist<biginteger[]> messages) { biginteger accumulator = new biginteger("0"); (biginteger[] message : messages) { (biginteger part : message) { accumulator = accumulator.add(part); } } return accumulator; } public static void main(string[] args) { ipsecclient client = new ipsecclient(); client.setupsslconnection(); } }
server:
import java.io.*; import java.math.biginteger; import java.net.*; import java.util.arraylist; import java.util.random; public class ipsecserver { private serversocket serversocket; private socket socket; private objectinputstream inputstream; private objectoutputstream outputstream; private ipsec gen; private arraylist<biginteger[]> messages; private biginteger[] message; private final int port; private random rand; private int fixednumber; private biginteger fixedprime, fixedbase, partialkeyclient, partialkeyserver, commonkey, publickeyclient, modulusclient; public ipsecserver() { rand = new random(); fixednumber = rand.nextint(1000); fixedprime = new biginteger("51803"); fixedbase = new biginteger("3"); gen = new ipsec(); gen.keygen(2048); messages = new arraylist<biginteger[]>(); port = 5000; } public void setupsslconnection() { try { serversocket = new serversocket(port); system.out.println("[server]: listening..."); socket = serversocket.accept(); inputstream = new objectinputstream(socket.getinputstream()); outputstream = new objectoutputstream(socket.getoutputstream()); system.out.println("[server]: connected... " + "port/ip: " + socket.getport() + socket.getinetaddress()); // receive partial key , certificate client message = (biginteger[]) inputstream.readobject(); messages.add(message); partialkeyclient = message[0]; publickeyclient = message[1]; modulusclient = message[2]; // send partial key , certificate client partialkeyserver = fixedbase.pow(fixednumber).mod(fixedprime); message = new biginteger[] {partialkeyserver, gen.publickey(), gen.modulus()}; messages.add(message); outputstream.writeobject(message); outputstream.flush(); // generate common key commonkey = partialkeyclient.pow(fixednumber).mod(fixedprime); system.out.println("[server]: common key = " + commonkey.intvalue()); // receive finished message client biginteger finishedmessage = (biginteger) inputstream.readobject(); messages.add(new biginteger[] {finishedmessage}); // verify finished message boolean result = gen.verifyrsasignature(accumulatemessages(messages).mod(publickeyclient), finishedmessage, publickeyclient, modulusclient); system.out.println("[server]: verification of finished message " + (result ? "succeeded" : "failed")); if (!result) { system.out.println("[server]: ssl-connection not estasblished..."); closeconnection(-1); } // send finished message client biginteger accumulatedmessages = accumulatemessages(messages).mod(gen.publickey()); finishedmessage = gen.generatersasignature(accumulatedmessages); outputstream.writeobject(finishedmessage); outputstream.flush(); system.out.println("[server]: ssl-connection estasblished..."); closeconnection(0); } catch (socketexception se) { system.exit(0); } catch (ioexception e) { e.printstacktrace(); } catch (classnotfoundexception e) { e.printstacktrace(); } } private void closeconnection(int exitcode) { try { socket.close(); outputstream.close(); inputstream.close(); serversocket.close(); system.exit(exitcode); } catch (ioexception e) { e.printstacktrace(); } } private biginteger accumulatemessages(arraylist<biginteger[]> messages) { biginteger accumulator = new biginteger("0"); (biginteger[] message : messages) { (biginteger part : message) { accumulator = accumulator.add(part); } } return accumulator; } public static void main(string[] args) { ipsecserver server = new ipsecserver(); server.setupsslconnection(); } }
ipsec:
import java.math.biginteger; import java.util.random; import java.security.*; public class ipsec { private static final biginteger 1 = new biginteger("1"); // private key (n,d) private biginteger privatekey; // public key (n,e) private biginteger publickey = new biginteger("3"); // modulus n private biginteger modulus; public ipsec() { } // public key public biginteger publickey() { return publickey; } public biginteger modulus() { return modulus; } // key generation public void keygen(int keylength) { biginteger p = biginteger.probableprime((int)math.ceil(keylength / 2), new random()); biginteger q = biginteger.probableprime((int)math.ceil(keylength / 2), new random()); while (!(p.subtract(one)).gcd(publickey).equals(one)) p = p.nextprobableprime(); while (!(q.subtract(one)).gcd(publickey).equals(one)) q = q.nextprobableprime(); biginteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); privatekey = publickey.modinverse(phi); } // encrypt public biginteger encrypt(biginteger message) { return message.modpow(publickey, modulus); } public static biginteger encrypt(biginteger message, biginteger publickey, biginteger modulus) { return message.modpow(publickey, modulus); } // decrypt public biginteger decrypt(biginteger message) { return message.modpow(privatekey, modulus); } // signature generation // generate rsa-signatures message public biginteger generatersasignature(biginteger message) { messagedigest digest; try { digest = messagedigest.getinstance("sha-256"); return decrypt(new biginteger(1, digest.digest(message.tobytearray())).mod(modulus())); } catch (nosuchalgorithmexception e) { e.printstacktrace(); system.exit(-1); } return message; } // verify rsa-signatures message public boolean verifyrsasignature(biginteger message, biginteger signature) { try { messagedigest digest = messagedigest.getinstance("sha-256"); return (new biginteger(1, digest.digest(message.tobytearray())).mod(modulus())).equals(encrypt(signature)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); system.exit(-1); } return false; } public boolean verifyrsasignature(biginteger message, biginteger signature, biginteger publickey, biginteger modulus) { try { messagedigest digest = messagedigest.getinstance("sha-256"); return (new biginteger(1, digest.digest(message.tobytearray())).mod(modulus())).equals(encrypt(signature, publickey, modulus)); } catch (nosuchalgorithmexception e) { e.printstacktrace(); system.exit(-1); } return false; } public static void main(string[] args) { testing(); } // misc public void printkeys() { string s = ""; s += "public = " + publickey + "\n"; s += "private = " + privatekey + "\n"; s += "modulus = " + modulus; system.out.println(s); } public static void testing() { ipsec gen = new ipsec(); gen.keygen(128); biginteger message = new biginteger("329"); system.out.println("verify: " + gen.verifyrsasignature(message, gen.generatersasignature(message))); } }
your server barfing @ signature-verifying stage here:
if (!result) { system.out.println("[server]: ssl-connection not established..."); closeconnection(-1); }
and closing socket without sending finished message. check output log. in such case maybe should send error object first. or else treat eofexception
handshake failure.
nb:
- for safety best contruct
objectoutputstream
beforeobjectinputstream
@ both ends. - you should close
objectoutputstream,
not socket, or input stream. way can sure gets flushed. closing of 3 closes other two. - don't call things 'ssl' when aren't.
Comments
Post a Comment