首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适用于anon CipherSuite的BouncyCastle .NET自定义TlsClient可在安卓上运行,但不能在Windows上运行

适用于anon CipherSuite的BouncyCastle .NET自定义TlsClient可在安卓上运行,但不能在Windows上运行
EN

Stack Overflow用户
提问于 2018-02-27 03:01:39
回答 1查看 2.3K关注 0票数 3

我已经访问了一个java应用程序,它使用CipherSuite TLS_ECDH_anon_WITH_AES_256_CBC_SHA进行通信(不可能使用另一个)。

前段时间,我不得不用C#编写了一个Xamarin应用程序,它使用Bouncy Castle PCL成功地连接到了它。我不得不使用Bouncy Castle,因为安卓(>=6.0)默认情况下不允许使用前面提到的密码套件。

现在,我必须在Window上编写一个.NET应用程序,它将连接到完全相同的Java应用程序,所以我只复制了用于建立连接的代码,并从NuGet安装了Bouncy Castle。TcpClient能够建立连接,但是从下面的示例代码中调用protocol.Connect(new CustomTlsClient());时,我得到了异常"System.IO.IOException: Internal TLS error,this but be a attack“。

覆盖"CustomTlsClient“的NotifyAlertRaised告诉我,Bouncy Castle无法读取记录(AlertLevel: 2,AlertDescription: 80,Message:"Failed to read record",Exception:"Org.BouncyCastle.Crypto.Tls.TlsFatalAlert: internal_error(80)")。

缩短的示例代码:

代码语言:javascript
复制
...
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
...

CustomTlsClient:

代码语言:javascript
复制
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) { }
}

由于这段代码在Xamarin应用程序中与Bouncy Castle的PCL版本一起工作,我真的不知道我在这里做错了什么……有什么建议吗?如有任何帮助,我们不胜感激!提前谢谢你。

EN

回答 1

Stack Overflow用户

发布于 2018-02-27 08:19:36

这个问题似乎只出现在专用的.NET NuGet包中;卸载它并安装Portable.BouncyCastle解决了这个问题,而不需要更改一行代码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48995332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档