如何使用WinHTTP对自签名证书执行SSL

txu3uszq  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(144)

我似乎有这个问题,并在精神上有一个通用的问题,可以被其他人引用,我正在寻找一个很好的例子使用SSL。
更具体地说,我从WinHttpSendRequest得到错误0x00002F8F,它是ERROR_INTERNET_DECODING_FAILED(这表明我是一个cert错误)。我已经在这台机器上导入了证书,并且能够在IE中拉出页面而没有证书错误。
The code I am using is here.
TLDR:如何使用WinHTTP和自签名证书?

zdwk9cvp

zdwk9cvp1#

对于WinHTTP,为了接受/允许SSL验证失败,您必须首先发出请求并允许其失败,然后禁用安全检查并在请求句柄上重试操作。沿着如下的东西:

// Certain circumstances dictate that we may need to loop on WinHttpSendRequest
// hence the do/while
do
{
    retry = false;
    result = NO_ERROR;

    // no retry on success, possible retry on failure
    if(WinHttpSendRequest(
        mHRequest,
        WINHTTP_NO_ADDITIONAL_HEADERS,
        0,
        optionalData,
        optionalLength,
        totalLength,
        NULL
        ) == FALSE)
    {
        result = GetLastError();

        // (1) If you want to allow SSL certificate errors and continue
        // with the connection, you must allow and initial failure and then
        // reset the security flags. From: "HOWTO: Handle Invalid Certificate
        // Authority Error with WinInet"
        // http://support.microsoft.com/default.aspx?scid=kb;EN-US;182888
        if(result == ERROR_WINHTTP_SECURE_FAILURE)
        {
            DWORD dwFlags =
                SECURITY_FLAG_IGNORE_UNKNOWN_CA |
                SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE |
                SECURITY_FLAG_IGNORE_CERT_CN_INVALID |
                SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;

            if(WinHttpSetOption(
                mHRequest,
                WINHTTP_OPTION_SECURITY_FLAGS,
                &dwFlags,
                sizeof(dwFlags)))
            {
                retry = true;
            }
        }
        // (2) Negotiate authorization handshakes may return this error
        // and require multiple attempts
        // http://msdn.microsoft.com/en-us/library/windows/desktop/aa383144%28v=vs.85%29.aspx
        else if(result == ERROR_WINHTTP_RESEND_REQUEST)
        {
            retry = true;
        }
    }
} while(retry);

字符串

j91ykkif

j91ykkif2#

如果您碰巧使用MFC Package 类:

CHttpFile* pFile = m_pConnection->OpenRequest(nRequestVerb, strRequest, NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT | dwFlags);
...
DWORD dwSecurity = 0;
if (pFile->QueryOption(INTERNET_OPTION_SECURITY_FLAGS, dwSecurity))
{
    dwSecurity |= SECURITY_IGNORE_ERROR_MASK;
    pFile->SetOption(INTERNET_OPTION_SECURITY_FLAGS, dwSecurity);
}
...
pFile->SendRequest(strRequestHeaders); // throws exception without IGNORE_ERROR_MASK

字符串
请注意,可以在SendRequest之前在CHttpFile上设置安全标志。您不必等到错误/异常发生。

相关问题