【问题标题】:.NET Core Custom Server Certificate CA.NET Core 自定义服务器证书 CA
【发布时间】:2018-11-19 01:38:40
【问题描述】:

我想从集群外的 .NET Core 应用程序调用 Kubernetes API。

我有一个带有 HttpClientHandler 的 HttpClient,我将这个回调设置为忽略无效(不受信任)的证书并且它可以工作:

handler.ServerCertificateCustomValidationCallback +=
    (message, certificate, chain, errors) => true;

但在我的 kubectl 的 kubeconfig 中,我有这个:

...
clusters:
- cluster:
    certificate-authority-data: SOME_AUTHORITY_DATA
    server: https://myserver.io:443
...

如何在我的应用程序中使用该 certificate-authority-data 验证服务器证书?

【问题讨论】:

    标签: c# .net validation kubernetes x509certificate


    【解决方案1】:
    private static byte[] s_issuingCABytes = { ... };
    
    handler.ServerCertificateCustomValidationCallback +=
        (message, certificate, chain, errors) =>
        {
            const SslPolicyErrors Mask =
    #if CA_IS_TRUSTED
                ~SslPolicyErrors.None;
    #else
                ~SslPolicyErrors.RemoteCertificateChainErrors;
    #endif
    
            // If a cert is not present, or it didn't match the host.
            // (And if the CA should have been root trusted anyways, also checks that)
            if ((errors & Mask) != SslPolicyErrors.None)
            {
                return false;
            }
    
            foreach (X509ChainElement element in chain.ChainElements)
            {
                if (element.Certificate.RawData.SequenceEqual(s_issuingCABytes))
                {
                    // The expected certificate was found, huzzah!
                    return true;
                }
            }
    
            // The expected cert was not in the chain.
            return false;
        };
    

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多