【问题标题】:How do I disable validation of the x509 certificate used to validate a message signature?如何禁用对用于验证消息签名的 x509 证书的验证?
【发布时间】:2018-09-19 00:23:14
【问题描述】:

我有一个调用 API 的客户端,该 API 对其响应消息进行签名。签名验证设置需要如下所示的特殊绑定:

public class SignatureBinding : Binding
{
    public override BindingElementCollection CreateBindingElements()
    {
        var signingElement = new AsymmetricSecurityBindingElement
        {
            AllowInsecureTransport = false,
            RecipientTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.Never),
            InitiatorTokenParameters = new X509SecurityTokenParameters(X509KeyIdentifierClauseType.IssuerSerial, SecurityTokenInclusionMode.AlwaysToRecipient),
            DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256,
            SecurityHeaderLayout = SecurityHeaderLayout.Strict,
            MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt,
            MessageSecurityVersion = MessageSecurityVersion.WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10,
            AllowSerializedSigningTokenOnReply = true
        };
        signingElement.SetKeyDerivation(false);

        return new BindingElementCollection
        {
            signingElement,
            new HttpsTransportBindingElement()
        };
    }
}

在 ClientCredentials 行为中:

public class CredentialsBehavior : ClientCredentials 
{
    public CredentialsBehavior()
    {
        base.ServiceCertificate.DefaultCertificate = store.FindBySerialNumber(signatureCertSN);
    }

    //Code omitted
}

我已经确认上述代码在普通计算机上运行时可以正常工作。消息发送完毕,服务器制作响应并对其进行签名,然后返回,客户端验证签名,一切正常。

但是,从目标服务器运行时出现故障,由于防火墙,无法访问CRL services。当我通过通道发送消息时,ServiceModel 调用返回错误。该错误与包含用于验证签名的公钥的证书有关。错误是:

X.509 证书 CN=somecert.somedomain.com, OU=CE_Operations, O="MyCompany, Inc.", L=City, S=State, C=US 链构建失败。使用的证书具有无法验证的信任链。更换证书或更改 certificateValidationMode。由于吊销服务器离线,吊销功能无法检查吊销。

服务器存在于无法访问 CRL 的域中,因此我在 this answer 的帮助下禁用了检查:

ServicePointManager.ServerCertificateValidationCallback += ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
ServicePointManager.CheckCertificateRevocationList = false;

但是,错误仍然存​​在。我猜ServerCertificateValidationCallback只会触发服务器证书,而这个证书是不同的。

如何在不检查 CRL 或执行其他验证程序的情况下说服服务模型允许使用此证书?

【问题讨论】:

    标签: c# .net ssl x509certificate2


    【解决方案1】:

    将 certificateValidationMode 设置为 None 以忽略证书验证 X509CertificateValidationMode

    这是一种行为,因此如果您想以编程方式执行此操作,您应该将其作为新行为绑定到您的服务:

    ServiceHost host = new ServiceHost(typeof(Service));
    ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "http://...");
    
    var endpointClientbehavior = new ClientCredentials();
    endpointClientbehavior.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
    
    endpoint.Behaviors.Add(endpointClientbehavior);
    

    【讨论】:

    • 我不清楚的是如何获取公开certifcateValidationMode 属性的对象的实例。我看到的答案都使用config,但是我的程序没有任何WCF配置(一切都是通过代码完成的)。
    猜你喜欢
    • 1970-01-01
    • 2015-03-03
    • 2016-02-26
    • 2011-04-06
    • 1970-01-01
    • 2012-09-28
    • 2015-10-09
    • 2011-02-13
    • 2014-01-06
    相关资源
    最近更新 更多