简体   繁体   English

如何在 C# 中创建与 BouncyCastle 的 TLS 连接?

[英]How to create a TLS connection with BouncyCastle in C#?

I'm trying to create a TLS connection with client authentication using BouncyCastle in C#.我正在尝试使用 C# 中的 BouncyCastle 创建具有客户端身份验证的 TLS 连接。 However I'm unsure how to properly set the context and I'm receiving an exception "Cannot be null for TLS 1.2 "signatureAndHashAlgorithm" ".但是,我不确定如何正确设置上下文,并且我收到一个异常“TLS 1.2“signatureAndHashAlgorithm”不能为空“。 My understanding is that this comes from the DefaultTlsCipherFactory used in the TlsClient not being set right.我的理解是,这来自未正确设置 TlsClient 中使用的 DefaultTlsCipherFactory。 Do I also need to extend it like I have the other Tls classes or is there something else I'm missing?我是否还需要像我拥有其他 Tls 类一样扩展它,或者还有其他我遗漏的东西吗?

var client = new TcpClient(ip.Address.ToString(), port);
var sr = new SecureRandom();
var protocol = new TlsClientProtocol(client.GetStream(), sr);
var tlsClient = new MyTlsClient(CertChainStructure, PrivateKey);
protocol.Connect(tlsClient);

Below are the MyTlsClient and MyTlsAuthentication classes.下面是 MyTlsClient 和 MyTlsAuthentication 类。

class MyTlsClient : DefaultTlsClient
{
    private X509CertificateStructure[] CertChain;

    private AsymmetricKeyParameter PrivateKey;

    public MyTlsClient(X509CertificateStructure[] certChain, AsymmetricKeyParameter privateKey)
    {
        CertChain = certChain;
        PrivateKey = privateKey;
    }

    public override TlsAuthentication GetAuthentication()
    {
        return new MyTlsAuthentication(CertChain, PrivateKey, this.mContext);
    }
}

class MyTlsAuthentication : TlsAuthentication
{
    private Certificate CertChain;
    private AsymmetricKeyParameter PrivateKey;
    private TlsContext Context;

    public MyTlsAuthentication(X509CertificateStructure[] certChain, AsymmetricKeyParameter privateKey, TlsContext context)
    {
        CertChain = new Certificate(certChain);
        Context = context;
        PrivateKey = privateKey;
    }

    public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest)
    {
        var creds = new DefaultTlsSignerCredentials(Context, CertChain, PrivateKey);
        return creds;
    }

    public void NotifyServerCertificate(Certificate serverCertificate) { }
}

UPDATE更新

Turns out the issue was that i wasn't supplying a signature and hash algorithm with the credentials.原来问题是我没有提供带有凭据的签名和哈希算法。 Adding this solved the issue and I'm able to connect with client authentication.添加这个解决了这个问题,我能够连接客户端身份验证。

public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest)
        {
            byte[] certificateTypes = certificateRequest.CertificateTypes;
            if (certificateTypes == null || !Arrays.Contains(certificateTypes, ClientCertificateType.rsa_sign))
                return null;

            SignatureAndHashAlgorithm signatureAndHashAlgorithm = null;
            if (certificateRequest.SupportedSignatureAlgorithms != null)
            {
                foreach (SignatureAndHashAlgorithm alg in certificateRequest.SupportedSignatureAlgorithms)
                {
                    if (alg.Signature == SignatureAlgorithm.rsa)
                    {
                        signatureAndHashAlgorithm = alg;
                        break;
                    }
                }

                if (signatureAndHashAlgorithm == null)
                    return null;
            }

            var creds = new DefaultTlsSignerCredentials(mContext, CertChain, PrivateKey, signatureAndHashAlgorithm);
            return creds;
        }

在问题更新中解决,只需要将 SignatureAndHashAlgorithm 添加到我的签名者信用中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM