我有以下代码:
SSLContext sslContext = SSLContext.getInstance("TLS", BouncyCastleProvider.PROVIDER_NAME);
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
SSLEngine sslEngine = sslContext.createSSLEngine();
String[] suites = { "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" };
sslEngine.setEnabledCipherSuites(suites);
谢谢。
编辑: 我发现我应该使用需要SecureRandom
对象的BouncyCastleJsseProvider
,如下所示:
sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
使用新提供程序后,我的代码库中得到了以下堆栈跟踪,据我了解,它应该像以前一样工作:
Aug 17, 2017 8:47:32 PM org.bouncycastle.jsse.provider.ProvTlsServer notifyAlertRaised
INFO: Server raised fatal(2) handshake_failure(40) alert: Failed to read record
org.bouncycastle.tls.TlsFatalAlert: handshake_failure(40)
at org.bouncycastle.tls.AbstractTlsServer.getSelectedCipherSuite(Unknown Source)
at org.bouncycastle.jsse.provider.ProvTlsServer.getSelectedCipherSuite(Unknown Source)
at org.bouncycastle.tls.TlsServerProtocol.sendServerHelloMessage(Unknown Source)
at org.bouncycastle.tls.TlsServerProtocol.handleHandshakeMessage(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.processHandshakeQueue(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.processRecord(Unknown Source)
at org.bouncycastle.tls.RecordStream.readRecord(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.safeReadRecord(Unknown Source)
at org.bouncycastle.tls.TlsProtocol.offerInput(Unknown Source)
at org.bouncycastle.jsse.provider.ProvSSLEngine.unwrap(Unknown Source)
为了让它工作,我做了以下工作。
-
添加提供程序:
BouncyCastleProvider
和BouncyCastleJsseProvider
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleJsseProvider());
-
使用
BouncyCastleJsseProvider
创建SSLContext
SSLContext sslContext = SSLContext.getInstance("TLS", BouncyCastleJsseProvider.PROVIDER_NAME);
-
使用带有
BouncyCastleProvider
的SecureRandom
实例初始化sslContext
sslContext.init( keyManagers, trustManagers, SecureRandom.getInstance("DEFAULT", BouncyCastleProvider.PROVIDER_NAME));
-
您可能需要使用
BouncyCastleJsseProvider
和PKIX
算法创建KeyManagerFactory
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("PKIX", BouncyCastleJsseProvider.PROVIDER_NAME);
-
现在,我们能够启用密码套件
SSLEngine sslEngine = sslContext.createSSLEngine(); String[] suites = { "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8" }; sslEngine.setEnabledCipherSuites(suites);