【发布时间】:2015-06-09 10:39:51
【问题描述】:
我的服务器使用 TLSv1.2 并且需要客户端证书才能连接。我可以使用 CURL 向服务器发送请求(此请求工作正常):
curl --data "SAMPLETEXT" https://myserver.com/webservice --insecure --key privkey.pem --cert certificate.cert
(是的,服务器有自签名证书并且需要--insecure 标志;不,我无法解决这个问题)。
现在,我想创建客户端以从 Scala 代码发送请求。 MyClient 是包含所需密码和路径的对象。为此,我创建了SSLContext:
private val keyStore = {
//Setting up BouncyCastle provider for message signing
Security.addProvider(new BouncyCastleProvider())
//Loading keystore from specified file
val clientStore = KeyStore.getInstance("JKS")
val inputStream = new FileInputStream(MyClient.keystore)
clientStore.load(inputStream, MyClient.keystorePassword.toCharArray)
inputStream.close()
clientStore
}
//Retrieving certificate and key
private val cert = keyStore.getCertificate(MyClient.keyAlias).asInstanceOf[X509Certificate]
private val key = keyStore.getKey(MyClient.keyAlias, MyClient.keystorePassword.toCharArray).asInstanceOf[PrivateKey]
//Creating SSL context
private val sslContext = {
val context = SSLContext.getInstance("TLS")
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
val kmf: KeyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm)
kmf.init(keyStore, MyClient.keystorePassword.toCharArray)
tmf.init(keyStore)
context.init(kmf.getKeyManagers, tmf.getTrustManagers, null)
context
}
然后用它来构建客户端:
private val httpClient =
richHttpBuilder(HttpEndpoint(baseUri))
.hostConnectionLimit(1)
.tlsWithoutValidation()
.tls(sslContext, Some(MyClient.host))
.build()
但我仍然收到错误:
future 返回了一个异常类型: com.twitter.finagle.ChannelWriteException,带有消息: com.twitter.finagle.SslHandshakeException:一般 SSLEngine 问题 在远程地址:
我做错了什么?
【问题讨论】: