1

Hey i'm building a chat application, I was previously using regular sockets for communications but want to add encryption so I've switched over to SSL sockets. However since switching to SSL sockets as soon as I go to send a message I receive a SSLHandshakeException, can anyone give me any clues as to why this is happening? Below is the ServerConnection class:

import javax.net.ssl.SSLServerSocket
import javax.net.ssl.SSLServerSocketFactory
import java.net.Socket
import java.net.ServerSocket
import java.io._
import scala.io._


class ServerConnection(port: Int) {

  def connect(): Socket = { 
    val factory = SSLServerSocketFactory.getDefault()
    val serverSocket = factory.createServerSocket(port)
    serverSocket.accept() 
  }  

  def getMsg(client: Socket): String = new BufferedSource(client.getInputStream()).getLines().next()

  def sendMsg(client: Socket, msg: String): Unit = {
    val out = new PrintStream(client.getOutputStream())
    out.println(msg)
    out.flush()
  }

  def close(client: Socket): Unit = client.close() 

Below is the ClientConnection class:

import javax.net.ssl.SSLSocket
import javax.net.ssl.SSLSocketFactory
import javax.net.SocketFactory
import java.net.Socket
import java.net.InetAddress
import java.net.InetSocketAddress
import java.io._
import scala.io._
import java.util.Calendar

class ClientConnection(host: InetAddress, port: Int) {

 def connect(): Socket = { 
   val sslfactory = SSLSocketFactory.getDefault()
   val sslsocket = sslfactory.createSocket(host, port)
   sslsocket
  }

 def getMsg(server: Socket): String = new BufferedSource(server.getInputStream()).getLines().next()

 def sendMsg(server: Socket, user: User, msg: String): Unit = {
   val out = new PrintStream(server.getOutputStream())
   out.println(this.getCurrDate() + " " + user.getUsername() + ": " + msg)
   out.flush()
 }  

 def getCurrDate(): String = "(" + Calendar.getInstance().getTime().toString().substring(11, 19) + ")"

 def close(server: Socket): Unit = server.close()
}

EDIT: Below is the full stack trace

javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHands
hakeException: Received fatal alert: handshake_failure
        at sun.security.ssl.SSLSocketImpl.checkEOF(Unknown Source)
        at sun.security.ssl.AppInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at scala.io.BufferedSource$BufferedLineIterator.next(BufferedSource.scal
a:78)
        at scala.io.BufferedSource$BufferedLineIterator.next(BufferedSource.scal
a:66)
        at ClientConnection.getMsg(ClientConnection.scala:20)
        at Client$.main(Client.scala:20)
        at Client.main(Client.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(Sca
laClassLoader.scala:70)
        at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaCla
ssLoader.scala:31)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext
(ScalaClassLoader.scala:101)
        at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoad
er.scala:70)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(Scala
ClassLoader.scala:101)
        at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
        at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)

  at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala
:65)
        at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)

   at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_
failure
        at sun.security.ssl.Alerts.getSSLException(Unknown Source)
        at sun.security.ssl.Alerts.getSSLException(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.recvAlert(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source
)
        at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
        at sun.security.ssl.AppOutputStream.write(Unknown Source)
        at java.io.PrintStream.write(Unknown Source)
        at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
        at sun.nio.cs.StreamEncoder.implFlushBuffer(Unknown Source)
        at sun.nio.cs.StreamEncoder.flushBuffer(Unknown Source)
        at java.io.OutputStreamWriter.flushBuffer(Unknown Source)
        at java.io.PrintStream.write(Unknown Source)
        at java.io.PrintStream.print(Unknown Source)
        at java.io.PrintStream.println(Unknown Source)
        at ClientConnection.sendMsg(ClientConnection.scala:24)
        at Client$.main(Client.scala:19)
        ... 19 more
user2069328
  • 71
  • 1
  • 7
  • Without any more data of the specific exception, I'd make an educated guess and send you here: http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate – Shloim Aug 29 '16 at 13:35
  • If that's not the case, Please give a full stack trace of the exception. – Shloim Aug 29 '16 at 13:36
  • @Shloim: it very unlikely (or even impossible) that a handshake error is the result of a failed validation of the server certificate. Thus the link will probably not help. – Steffen Ullrich Aug 29 '16 at 13:41
  • Please enable SSL debugging on both client and server side and add the output to your question. – Steffen Ullrich Aug 29 '16 at 13:42
  • I have added the full stack trace, Steffen how do i enable SSL debugging? btw I am not using an IDE just gvim, scalac and scala – user2069328 Aug 29 '16 at 13:46
  • @user2069328 - Check this question -> http://stackoverflow.com/questions/6659360/how-to-solve-javax-net-ssl-sslhandshakeexception-error. I hope this would help you! – Am_I_Helpful Aug 29 '16 at 17:35
  • Am_I_Helpful thanks, i generated the cert using the keytool application and then including the keystore and password using this command scala -Djavax.net.ssl.keyStore=mySrvKeystore -Djavax.net.ssl.keyStorePassword=123456 EchoServer however I still receive the SSLHandshakeException – user2069328 Aug 29 '16 at 18:40

0 Answers0