首页 > 解决方案 > 来自 ... tls 的 TLS 握手错误:客户端未提供证书

问题描述

我有一个 C# RestFull 客户端试图连接到 Go 服务器。一旦我到达 TLS 握手阶段,它就会失败,因为客户端没有提供证书

在执行请求之前,我已验证客户端证书与 RestClient 对象相关联。

// Initializing the client
RestClient client = new RestClient(Config.SERVER_DOMAIN);

// Adding client certificate (exported from a smart card with no private key attributes)
client.ClientCertificates = Global.cpf.getCertCollection();

// defining our request then executing it
var request = new RestRequest("users", Method.GET);
var response = await client.ExecuteTaskAsync(request);

仅当从存在私有组件的 .PFX 文件中读取证书时,它才有效。但是当我切换到智能卡证书(它没有私钥属性,因为智能卡不希望你拥有它们)时,服务器不会从客户端收到任何证书。

我知道 TLS 需要一个用于握手阶段的私钥,但客户端 obj 没有看到任何与给定证书关联的私钥,因此无法将该证书识别为 TLS 建立的有效证书。

我知道私钥不能从智能卡中导出,并且我知道必须有一种方法告诉 RestClient 对象为了通过握手阶段,你应该与智能卡进行通信,但是,我放弃了!

有人可以指出我正确的方向吗?

标签: c#authenticationsslsmartcardpkcs#11

解决方案


对于大多数智能卡,应该有可用的微型驱动程序或独立 CSP(加密服务提供程序)。这些组件充当将卡与 Windows 加密子系统集成的驱动程序。它们通常由设备供应商提供,一旦正确设置,您应该能够参考 Windows 证书存储中的正确私钥获取证书。

尝试使用以下方法:

private static X509Certificate2 GetCertificate()
{
    X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
    store.Open(OpenFlags.ReadOnly);

    try
    {
        // Note: X509Certificate2UI requires reference to System.Security
        X509Certificate2Collection certs = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection);
        if (certs != null && certs.Count > 0)
            return certs[0];
    }
    finally
    {
        store.Close();
    }

    return null;
}

推荐阅读