【问题标题】:Bouncy Castle TLS API usageBouncy Castle TLS API 使用
【发布时间】:2013-05-24 04:52:38
【问题描述】:

我想使用 bouncy castle TLS 库使用套接字在服务器和客户端之间进行通信。 我浏览了很多文档(这对我来说还不够),但我不知道该怎么做,

我正在使用 BouncyCastle v1.7.48(runtime version=v2.0.50727) 二进制文件, 我找到了这些信息,

我必须使用 Org.BouncyCastle.Crypto.Tls 命名空间和 TlsProtocolHandler 类。

要实现TLS通信,

  1. 我应该在服务器端使用什么 API?
  2. 我应该在客户端使用什么 API?

        System.IO.Stream inputStream, outputStream;
        TlsProtocolHandler tls = new TlsProtocolHandler(inputStream, outputStream);
    
  3. inputStreamoutputStream的参数是什么?

public virtual void Connect(TlsClient tlsClient);

其中,TlsClient 是一个接口,它包含许多接口内部

4.如何使用上述 API?我必须向所有人声明新类并在其中实现方法?

请帮我建造这个充气城堡。

编辑 1: 我创建了一个类,它继承自一个名为DefaultTlsClient 的抽象类。 然后我可以创建我的类的一个实例并将其传递给接口参考。 所以我可以像这样发送参数。 tls.Connect(tlsClient);

除了上面提到的以外,我没有初始化任何参数。 (在 2055 上进行这些操作之前连接套接字) 但我不确定握手是否完成。我的程序将进入阅读状态。

【问题讨论】:

  • 我找不到任何可以使用上述 API 的测试用例。
  • 然后您可以使用我们的 SecureBlackbox - 它附带文档、支持和示例。

标签: c# ssl bouncycastle


【解决方案1】:

充气城堡中没有服务器端 TLS API。您可以在主页上阅读它们仅支持客户端。

对于客户端,您已经找到了正确的类。 TlsProtocolHandler 可以完成这项工作,但如果没有自定义类,它将无法工作。这是示例代码:

    // Need class with TlsClient in inheritance chain
    class MyTlsClient : DefaultTlsClient
    {
        public override TlsAuthentication GetAuthentication()
        {
            return new MyTlsAuthentication();
        }
    }

    // Need class to handle certificate auth
    class MyTlsAuthentication : TlsAuthentication
    {
        public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest)
        {
            // return client certificate
            return null;
        }

        public void NotifyServerCertificate(Certificate serverCertificate)
        {
            // validate server certificate
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient();

            client.Connect(IPAddress.Loopback, 6000);

            // input/output streams are deprecated, just pass client stream
            TlsProtocolHandler handler = new TlsProtocolHandler(client.GetStream());

            handler.Connect(new MyTlsClient());

            // handshake completed
            // use handler.Stream.Write/Read for sending app data

            Console.ReadLine();
        }
    }

我已经用我的 tcp 服务器对此进行了测试,并收到了客户端的问候。

请记住,它是 1.0 版中的 TLS,所以如果您需要其他版本或服务器 api,那么我建议使用其他库(.NET 框架支持 TLS)。

【讨论】:

  • 请验证:1.我想要 TLS v1.2,我不能使用充气城堡 2. there is no server side api available for TLS means, Client side I can use Bouncy castle. But server side, I have to implement TLS on TCP by myslelf or using some libraries.(may be .net framework or some other)
  • 您可以在源代码 TlsProtocolHandler.cs“TLS 1.0 中所有高级协议的实现”中看到它们在标头 0x0301 = 1.0 版中发送。我会在客户端和服务器中使用 .NET SslStream
  • 在最新的 Bouncy Castle TlsProtocolHandler 已弃用,因此请使用 TlsServerProtocolTlsClientProtocol 与流和 new SecureRandom() 作为参数。
猜你喜欢
  • 2023-02-09
  • 2012-03-19
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多