CWebView::GetSecurityInfo Method  
 
BOOL GetSecurityInfo(
  WEBVIEW_SECURITY_INFO *lpSecurityInfo  
);

The GetSecurityInfo method returns certificate and host security details for the last navigation.

Parameters

lpSecurityInfo
A pointer to a WEBVIEW_SECURITY_INFO structure which will contain information about the specified download when the method returns. The dwSize member of this structure must be initialized to the size of the structure prior to being passed to this method.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The GetSecurityInfo method retrieves details about the security state of the current page and populates a WEBVIEW_SECURITY_INFO structure. This includes the negotiated TLS protocol, cipher suite, certificate subject and issuer, and the validity period of the certificate. The method also reports an overall security level (dwLevel) and condition-specific flags (dwFlags) that identify issues such as expired certificates, mixed content, or weak cryptographic algorithms.

The information returned reflects the most recent navigation state. During navigation, these structure values may be provisional. For example, a page may initially appear secure but later be downgraded to a warning state if mixed content is detected. Applications should consider the values as advisory until a WEBVIEW_EVENT_NAVIGATION_COMPLETED notification confirms the final state.

The security level (dwLevel) is intended for user interface decisions, such as whether to display a lock icon. The flags (dwFlags) provide more granular context and may include multiple indicators at once. For instance, a site may be considered secure overall but still include a WEBVIEW_SECURITY_FLAG_CERT_EXPIRED flag if the certificate is past its validity period. Because the browser can revise its assessment as resources are loaded, applications that display security indicators should refresh their state after navigation completes. Do not assume that an initially secure status will remain unchanged for the life of the page. Conditions such as mixed content or blocked subresources may alter the security level after the main document is retrieved.

For plain HTTP or local content, the szProtocol and szCipher members will be empty, dwFlags will typically include WEBVIEW_SECURITY_FLAG_UNENCRYPTED, and the security level will be WEBVIEW_SECURITY_LEVEL_INSECURE or WEBVIEW_SECURITY_LEVEL_NEUTRAL. For HTTPS with a valid certificate, the protocol and cipher suite are reported, the subject and issuer strings identify the certificate, and the security level will be WEBVIEW_SECURITY_LEVEL_SECURE.

The subject and issuer members are provided for display or logging purposes only; certificate parsing and validation are handled internally by the browser engine. Likewise, the validity dates (ftValidFrom and ftValidTo) are exposed for simple checks but do not require applications to perform their own certificate validation.

Before calling this method, the dwSize member of the structure must be initialized to the size of the structure. This ensures compatibility with future versions of the library if additional fields are introduced. If the size does not match, the function will fail or only initialize the subset of fields it recognizes.

Both ANSI (A) and Unicode (W) variants are available. When compiling with UNICODE defined, string members are UTF-16 encoded; otherwise, they are UTF-8 encoded. The GetSecurityInfo macro resolves to the appropriate version based on the project's character set.

If you are only interested in determining the current security level of the connection you can use the GetSecurityLevel function.

Example

// Map security level to text
static LPCTSTR SecLevelToText(DWORD level)
{
    switch (level)
    {
    case WEBVIEW_SECURITY_LEVEL_SECURE: return _T("SECURE");
    case WEBVIEW_SECURITY_LEVEL_WARNING: return _T("WARNING");
    case WEBVIEW_SECURITY_LEVEL_INSECURE: return _T("INSECURE");
    case WEBVIEW_SECURITY_LEVEL_NEUTRAL: return _T("NEUTRAL");
    default: return _T("UNKNOWN");
    }
}

// Print a FILETIME value as YYYY-MM-DD HH:MM:SS
static void PrintFileTime(LPCTSTR label, const FILETIME* pft)
{
    if (!pft || (pft->dwLowDateTime == 0 && pft->dwHighDateTime == 0))
    {
        _tprintf(_T("%s: (not available)\n"), label);
        return;
    }

    FILETIME lft = {0};
    SYSTEMTIME st = {0};

    if (FileTimeToLocalFileTime(pft, &lft) && FileTimeToSystemTime(&lft, &st))
    {
        _tprintf(_T("%s: %04u-%02u-%02u %02u:%02u:%02u\n"),
                 label,
                 (unsigned)st.wYear,
                 (unsigned)st.wMonth,
                 (unsigned)st.wDay,
                 (unsigned)st.wHour,
                 (unsigned)st.wMinute,
                 (unsigned)st.wSecond);
    }
    else
    {
        _tprintf(_T("%s: (conversion failed)\n"), label);
    }
}

// Build and print a readable flag list
static void PrintSecurityFlags(DWORD dwFlags)
{
    if (dwFlags == WEBVIEW_SECURITY_FLAG_NONE)
    {
        _tprintf(_T("Flags: NONE (0x%08lX)\n"), dwFlags);
        return;
    }

    _tprintf(_T("Flags: 0x%08lX"), dwFlags);

    if (dwFlags & WEBVIEW_SECURITY_FLAG_UNENCRYPTED) _tprintf(_T(" | UNENCRYPTED"));
    if (dwFlags & WEBVIEW_SECURITY_FLAG_INSECURE) _tprintf(_T(" | INSECURE"));
    if (dwFlags & WEBVIEW_SECURITY_FLAG_CERT_WEAK) _tprintf(_T(" | CERT_WEAK"));
    if (dwFlags & WEBVIEW_SECURITY_FLAG_CERT_ERROR) _tprintf(_T(" | CERT_ERROR"));
    if (dwFlags & WEBVIEW_SECURITY_FLAG_CERT_EXPIRED) _tprintf(_T(" | CERT_EXPIRED"));
    if (dwFlags & WEBVIEW_SECURITY_FLAG_UNSAFE) _tprintf(_T(" | UNSAFE"));

    _tprintf(_T("\n"));
}

// Query and display the current security info
static void ShowSecurityInfo(CWebView *pWebView)
{
    WEBVIEW_SECURITY_INFO secInfo = { 0 };
    secInfo.dwSize = sizeof(secInfo);

    if (!pWebView->GetSecurityInfo(&secInfo))
    {
        _tprintf(_T("GetSecurityInfo failed (Error 0x%lx)\n"), pWebView->GetLastError());
        return;
    }

    _tprintf(_T("Security Level: %s\n"), SecLevelToText(secInfo.dwLevel));
    PrintSecurityFlags(secInfo.dwFlags);

    if (secInfo.szProtocol[0])
        _tprintf(_T("Protocol: %s\n"), secInfo.szProtocol);
    else
        _tprintf(_T("Protocol: (none)\n"));

    if (secInfo.szCipher[0])
        _tprintf(_T("Cipher Suite: %s\n"), secInfo.szCipher);
    else
        _tprintf(_T("Cipher Suite: (none)\n"));

    if (secInfo.szSubject[0])
        _tprintf(_T("Certificate Subject: %s\n"), secInfo.szSubject);
    else
        _tprintf(_T("Certificate Subject: (not available)\n"));

    if (secInfo.szIssuer[0])
        _tprintf(_T("Certificate Issuer: %s\n"), secInfo.szIssuer);
    else
        _tprintf(_T("Certificate Issuer: (not available)\n"));

    PrintFileTime(_T("Valid From"), &secInfo.ftValidFrom);
    PrintFileTime(_T("Valid To "), &secInfo.ftValidTo);
}          

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also