3

我正在尝试连接到使用 TLS 和客户端证书身份验证的服务器。下面是一个代码片段:

async Task TestClientCertAuth()
{
    int iWinInetError = 0;
    Uri theUri = new Uri("http://xxx-xxx");
    try
    {
        using (HttpBaseProtocolFilter baseProtocolFilter = new HttpBaseProtocolFilter())
        {
            // Task<Certificate> GetClientCertificate() displays a UI with all available 
            // certificates with and returns the user selecter certificate.  An 
            // oversimplified implementation is included for completeness.
            baseProtocolFilter.ClientCertificate = await GetClientCertificate();
            baseProtocolFilter.AllowAutoRedirect = false;
            baseProtocolFilter.AllowUI = false;
            using (HttpClient httpClient = new HttpClient(baseProtocolFilter))
            using (HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Get, theUri))
            using (HttpResponseMessage httpResponse = await httpClient.SendRequestAsync(httpRequest))
            {
                httpResponse.EnsureSuccessStatusCode();

                // Further HTTP calls using httpClient based on app logic.
            }
        }
    }
    catch (Exception ex)
    {
        iWinInetError = ex.HResult & 0xFFFF;
        LogMessage(ex.ToString() + " Error code: " + iWinInetError);
        throw;
    }
}

// Task<Certificate> GetClientCertificate() displays a UI with all available 
// certificates with and returns the user selecter certificate.  An 
// oversimplified implementation is included for completeness.
private async Task<Certificate> GetClientCertificate()
{
    IReadOnlyList<Certificate> certList = await CertificateStores.FindAllAsync();
    Certificate clientCert = null;
    // Always choose first enumerated certificate.  Works so long as there is only one cert
    // installed and it's the right one.
    if ((null != certList) && (certList.Count > 0))
    {
        clientCert = certList.First();
    }
    return clientCert;
}

SendRequestAsync 调用引发 HRESULT 0x80072F7D 异常 - 我相信这意味着 ERROR_INTERNET_SECURITY_CHANNEL_ERROR。服务器证书信任没有问题。客户端证书安装在应用程序本地商店中,我准备使用 CertificateStores.FindAllAsync 检索它。查看 SSL 跟踪,我可以看到没有发送客户端证书。

如果 HttpBaseProtocolFilter.AllowUI 设置为 true,则不会出现上述问题。在这种情况下,SendRequestAsync 调用会导致显示一个 UI,请求同意使用客户端证书。在此对话框中选择“允许”后,我可以看到客户端证书和证书验证消息正在跟踪中发送,并且连接已成功建立。

问题:应用程序代码已经处理了用户的证书选择。我想知道是否有任何方法可以指定同意以编程方式使用客户端证书。因为启用 AllowUI 会导致其他副作用 - 例如,如果服务器重新运行带有 WWW-Authenticate: Basic 标头的 401 HTTP 代码,则基本协议过滤器会弹出它自己的 UI 以接受用户凭据,而不会给调用者机会处理它。想避免上述两种 UI,因为我已经选择了客户端证书并从具有自己 UI 的用户那里获得了凭据。谢谢

4

2 回答 2

3

Microsoft 博客条目提供了有关在 AllowUI 为 false 时为什么会发生此错误的信息,并提供了一种解决方法。在任何情况下都不能绕过证书同意 UI,因此这是欧盟必须经历的事情。Windows Phone 上的行为似乎也有所不同。尝试了这个解决方案,它似乎可以在桌面和表面上工作。一般的想法是通过尝试访问私钥来“准备”证书以供当前应用会话中的较低级别 API 使用。在这种情况下,我们只是尝试签署一些虚拟数据。一旦欧盟授予对证书的访问权限,TLS 会话建立就会成功。不过需要检查它在 Windows Phone 上的行为。

    private async Task<bool> RequestCertificateAccess(Certificate cert)
    {
        bool signOK = false;

        try
        {
            IBuffer data = CryptographicBuffer.ConvertStringToBinary("ABCDEFGHIJKLMNOPQRSTUVWXYZ012345656789", 
                BinaryStringEncoding.Utf8);

            CryptographicKey key = await PersistedKeyProvider.OpenKeyPairFromCertificateAsync(cert,
                HashAlgorithmNames.Sha1, CryptographicPadding.RsaPkcs1V15);

            IBuffer sign = await CryptographicEngine.SignAsync(key, data);

            signOK = CryptographicEngine.VerifySignature(key, data, sign);
        }
        catch (Exception ex)
        {
            LogMessage(ex.ToString(), "Certificate access denied or sign/verify failure.");
            signOK = false;
        }
        return signOK;
    }

在基本协议过滤器上设置客户端证书之前,可以调用 RequestClientCertificateAccess。

@Tomas Karban,感谢您的回复。我没有使用 sharedUserCertificates,所以如果我理解正确,我可以枚举的任何证书都必须在应用程序的证书存储中。如果您还没有看到我分享的链接,可能会对您的案例有所帮助。

于 2016-05-12T05:13:44.150 回答
0

我认为您没有将证书存储在应用程序自己的证书存储中。如果您设置HttpBaseProtocolFilter.AllowUI = true并确认对话框,应用程序将获得使用来自用户存储的私钥的权限。如果没有 UI 确认,应用程序只能使用其自己的证书存储中的私钥。

Windows 10 Mobile 上的情况更糟——据我所知,您无法设置HttpBaseProtocolFilter.AllowUI为 true (请参阅我的问题Cannot set HttpBaseProtocolFilter.AllowUI to true on Windows 10 Mobile)。这留下了使用应用程序自己的证书存储的唯一选择。

于 2016-05-09T12:20:18.123 回答