Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
object SslClient {
val jks_file_name = "jks_2015.store"
// Crypto initializer
val keyManagerFactory = KeyManagerFactory.getInstance("SunX509")
val trustManagerFactory = TrustManagerFactory.getInstance("SunX509")
val keyStore = KeyStore.getInstance("JKS")
val trustKeyStore = KeyStore.getInstance("JKS")
val sslContext = SSLContext.getInstance("TLS")
keyStore.load(new FileInputStream(jks_file_name), "changeit".toCharArray())
trustKeyStore.load(new FileInputStream(jks_file_name), "changeit".toCharArray())
keyManagerFactory.init(keyStore, "changeit".toCharArray())
trustManagerFactory.init(trustKeyStore)
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null)
val socketFactory = sslContext.getSocketFactory()
val cftHost = "194.85.10.1"
val timeout = Play.current.configuration.getString("sslClientTimeout").get.toInt //20 * 1000
val logger = Logger // ("SslClient")
def sendRequest(requestString: String, direction: String = "QuickPay"): String = {
val port = 17108
val socket = socketFactory.createSocket().asInstanceOf[SSLSocket]
try {
socket.connect(new InetSocketAddress(cftHost, port), timeout)
val session = socket.getSession()
if (session != null && session.getPeerCertificateChain() != null && session.getPeerCertificateChain().length > 0) {
val serial = session.getPeerCertificateChain()(0).getSerialNumber()
val subject = session.getPeerCertificateChain()(0).getSubjectDN()
val issuer = session.getPeerCertificateChain()(0).getIssuerDN()
logger.debug(s"Server certificate serial = $serial subject = $subject issuer = $issuer")
val outputStream = socket.getOutputStream()
outputStream.write(requestString.getBytes())
outputStream.flush()
scala.io.Source.fromInputStream(socket.getInputStream()).mkString
} else {
Error.faultXml(" ssl socket session to 194.85.10.1")
}
} catch {
case e: SocketTimeoutException => {
Error.faultXml("timeout (" + timeout + "ms)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment