Skip to content

How do I access server certificate validation errors during LDAP connections? #55017

Description

@AnthonyMastrean

How am I supposed to implement server certificate checks in the LdapSessionOptions.VerifyServerCertificate callback without access to a populated SslPolicyErrors?

I noticed that SslStream, via SecureChannel, performs certificate validation that returns a complete set of policy errors. The routine it calls appears to be internal/private and based on complicated interop that I doubt I'm supposed to duplicate.

I know that X509Certificate2 can provide a pass/fail answer. But, that runs only the X509Chain status checks, which does not include, for example, "RemoteCertificateNameMismatch".

private bool VerifyServerCertificate(LdapConnection connection, X509Certificate certificate)
{
    var certificate2 = new X509Certificate2(certificate);
    return certificate2.Verify();
}

I could build an X509Chain and enumerate status, but that still misses the (few) other validations from SslPolicyErrors. And now I'm responsible for managing two extra disposable resources (although I'm not sure I can dispose them because each contains the original certificate that was passed in and I don't dare dispose that early).

private bool VerifyServerCertificate(LdapConnection connection, X509Certificate certificate)
{
    var certificate2 = new X509Certificate2(certificate);
    var chain = new X509Chain();

    if (chain.Build(certificate2))
    {
        return true;
    }

    foreach (var element in chain.ChainElements)
    {
        foreach (var status in elements.ChainElementStatus)
        {
            // ... log something
        }
    }

    return false;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Net.SecurityquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions