来自... tls的TLS握手错误:客户端未提供证书

时间:2018-10-17 04:55:12

标签: c# authentication ssl smartcard pkcs#11

我有一个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对象,为了通过握手阶段,您应该与智能卡进行通信,但是,我放弃了!

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

1 个答案:

答案 0 :(得分:0)

对于大多数智能卡,应该有微型驱动程序或独立的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;
}