Merge pull request #1295 from akka/wip-3161-use-truststore-on-server-too-2.2-√

#3161 - adding trust managers for server connection too
This commit is contained in:
Viktor Klang (√) 2013-04-03 10:21:25 -07:00
commit 718e2d115c

View file

@ -14,6 +14,7 @@ import java.io.{ IOException, FileNotFoundException, FileInputStream }
import java.security._ import java.security._
import javax.net.ssl.{ KeyManagerFactory, TrustManager, TrustManagerFactory, SSLContext } import javax.net.ssl.{ KeyManagerFactory, TrustManager, TrustManagerFactory, SSLContext }
import org.jboss.netty.handler.ssl.SslHandler import org.jboss.netty.handler.ssl.SslHandler
import scala.util.Try
/** /**
* INTERNAL API * INTERNAL API
@ -92,7 +93,7 @@ private[akka] object NettySSLSupport {
trustManagerFactory.init({ trustManagerFactory.init({
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType) val trustStore = KeyStore.getInstance(KeyStore.getDefaultType)
val fin = new FileInputStream(trustStorePath) val fin = new FileInputStream(trustStorePath)
try trustStore.load(fin, trustStorePassword.toCharArray) finally fin.close() try trustStore.load(fin, trustStorePassword.toCharArray) finally Try(fin.close())
trustStore trustStore
}) })
trustManagerFactory.getTrustManagers trustManagerFactory.getTrustManagers
@ -140,10 +141,23 @@ private[akka] object NettySSLSupport {
factory.init({ factory.init({
val keyStore = KeyStore.getInstance(KeyStore.getDefaultType) val keyStore = KeyStore.getInstance(KeyStore.getDefaultType)
val fin = new FileInputStream(keyStorePath) val fin = new FileInputStream(keyStorePath)
try keyStore.load(fin, keyStorePassword.toCharArray) finally fin.close() try keyStore.load(fin, keyStorePassword.toCharArray) finally Try(fin.close())
keyStore keyStore
}, keyStorePassword.toCharArray) }, keyStorePassword.toCharArray)
Option(SSLContext.getInstance(protocol)) map { ctx ctx.init(factory.getKeyManagers, null, rng); ctx }
val trustManagers: Option[Array[TrustManager]] = settings.SSLTrustStore map {
path
val pwd = settings.SSLTrustStorePassword.map(_.toCharArray).orNull
val trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm)
trustManagerFactory.init({
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType)
val fin = new FileInputStream(path)
try trustStore.load(fin, pwd) finally Try(fin.close())
trustStore
})
trustManagerFactory.getTrustManagers
}
Option(SSLContext.getInstance(protocol)) map { ctx ctx.init(factory.getKeyManagers, trustManagers.orNull, rng); ctx }
} catch { } catch {
case e: FileNotFoundException throw new RemoteTransportException("Server SSL connection could not be established because key store could not be loaded", e) case e: FileNotFoundException throw new RemoteTransportException("Server SSL connection could not be established because key store could not be loaded", e)
case e: IOException throw new RemoteTransportException("Server SSL connection could not be established because: " + e.getMessage, e) case e: IOException throw new RemoteTransportException("Server SSL connection could not be established because: " + e.getMessage, e)