Created
August 21, 2015 23:51
-
-
Save Houdini/55bc5a10aaf602b5d64c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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