我可以访问一个使用 CipherSuite 的 java 应用程序TLS_ECDH_anon_WITH_AES_256_CBC_SHA
用于通信(不可能使用另一个)。
前段时间,我不得不用 C# 编写一个 Xamarin 应用程序,它使用 Bouncy Castle PCL 成功连接到它。我不得不使用 Bouncy Castle,因为 Android(>=6.0)默认不允许使用前面提到的密码套件。
现在,我必须在 Windows 上编写一个 .NET 应用程序,它将连接到完全相同的 Java 应用程序,因此我只需复制用于建立连接的代码并从 NuGet 安装 Bouncy Castle。 TcpClient 能够建立连接,但调用protocol.Connect(new CustomTlsClient());
下面的示例代码给出了异常“System.IO.IOException:内部 TLS 错误,这可能是一次攻击”。
压倒一切NotifyAlertRaised
对于“CustomTlsClient”告诉我,Bouncy Castle 无法读取记录(AlertLevel:2,AlertDescription:80,消息:“无法读取记录”,异常:“Org.BouncyCastle.Crypto.Tls.TlsFatalAlert:internal_error(80) )”)。
缩短的示例代码:
...
TcpClient client = tryConnect(ip, port); // simply returns a TcpClient on success
NetworkStream targetStream = client.GetStream();
targetStream.ReadTimeout = Config.Network.TcpStreamReadTimeout;
targetStream.WriteTimeout = Config.Network.TcpStreamWriteTimeout;
TlsClientProtocol protocol = new TlsClientProtocol(targetStream, new Org.BouncyCastle.Security.SecureRandom());
protocol.Connect(new CustomTlsClient()); // <---- Here's the problem
...
自定义Tls客户端:
private class CustomTlsClient : DefaultTlsClient
{
public override TlsAuthentication GetAuthentication() => new CustomTlsAuthentication();
public override int[] GetCipherSuites() => new[] { CipherSuite.TLS_ECDH_anon_WITH_AES_256_CBC_SHA };
public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
{
// This gave me further information on the error
base.NotifyAlertRaised(alertLevel, alertDescription, message, cause);
Console.WriteLine("AlertLevel: " + alertLevel);
Console.WriteLine("AlertDescription: " + alertDescription);
Console.WriteLine("Message: " + message);
Console.WriteLine("Exception: " + cause);
}
}
private class CustomTlsAuthentication : TlsAuthentication
{
public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest) => null;
public void NotifyServerCertificate(Certificate serverCertificate) { }
}
由于此代码在具有 PCL 版本 Bouncy Castle 的 Xamarin 应用程序中运行,我真的不知道我在这里做错了什么......有什么建议吗?任何帮助表示赞赏!先感谢您。