如何验证证书?

5
我正在开始一个项目,需要使用FTPS连接到第三方服务器。他们将提供我们他们的证书,但我还没有收到,所以我将创建自己的开发环境来开始。我使用Alex FTPS客户端作为起点(http://ftps.codeplex.com/)... 但也可以考虑使用WinSCP作为另一种客户端。
我的问题如下 - 如何在.Net中验证证书?
已完成的步骤:
1. 安装和配置FileZilla服务器 2. 使用FileZilla创建自签名证书
3. 在Alex FTPS客户端中,他们有一个回调函数,具有以下签名:
private static bool ValidateTestServerCertificate(
    object sender, 
    X509Certificate certificate, 
    X509Chain chain, 
    SslPolicyErrors sslPolicyErrors)
{
    // what goes here?
}

我的(各种问题)是:

  1. 方法中应该放什么内容?

  2. 我对证书只有一些基本的了解。FileZilla自动生成的证书是否为X509证书?我必须承认,我不知道不同类型的证书格式。

  3. 我需要编写什么类型的代码?当我在WCF或HTTP中使用SSL时,大部分工作已经由系统处理了。FTPS是否也是这种情况?例如,我会使用Windows中的MMC插件控制台导入或安装证书。

  4. 如果我不在Alex FTPS中实现回调方法,我会收到以下消息:“根据验证过程,远程证书无效”

感谢您提供任何提示和指导。

1个回答

8

哎呦,没人回答......那我来回答自己的问题,并且这可能对其他人有帮助。

以下是我的步骤,不确定是否每个步骤都需要,但这就是我最终所做的。

1)安装FileZilla Server

  • 使用它创建自己的自签名证书
  • 菜单: 设置 | SSL/TSL设置 | 生成新证书
  • 输入相应的值
  • 确保我拥有正确的通用名称=服务器地址。
  • 这将生成扩展名为.crt的带有私钥的证书。

2)由于我在Windows上,发现我无法将此证书安装在证书存储中,因此额外一步是我需要先将其转换

3)启动Windows MMC Snap-in控制台

  • 将证书安装到计算机帐户的可信根认证颁发机构存储中

4)在我的代码中(在FTPS库中,例如Alex FTPS),我的连接看起来像是这样的:

var credential = new NetworkCredential(username, password);
string message = _client.Connect(hostname, port, credential, 
    ESSLSupportMode.Implicit,
    null, // new RemoteCertificateValidationCallback(ValidateTestServerCertificate), 
    null, 0, 0, 0, null); 

.net/Windows基础设施已经为我处理了所有验证工作。
如果您需要自定义验证,或者不想在Windows存储中安装证书,您可以使用此示例代码: http://msdn.microsoft.com/en-us/library/office/dd633677%28v=exchg.80%29.aspx
private static bool ValidateTestServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
      // If the certificate is a valid, signed certificate, return true.
      if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
      {
        return true;
      }

      // If there are errors in the certificate chain, look at each error to determine the cause.
      if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
      {
        if (chain != null && chain.ChainStatus != null)
        {
          foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
          {
            if ((certificate.Subject == certificate.Issuer) &&
               (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
            {
              // Self-signed certificates with an untrusted root are valid. 
              continue;
            }
            else
            {
              if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
              {
                // If there are any other errors in the certificate chain, the certificate is invalid,
             // so the method returns false.
                return false;
              }
            }
          }
        }

        // When processing reaches this line, the only errors in the certificate chain are 
    // untrusted root errors for self-signed certificates. These certificates are valid
    // for default Exchange server installations, so return true.
        return true;
      }
      else
      {
     // In all other cases, return false.
        return false;
      }
    }

希望这能对人们有所帮助。

但是_client是TcpClient,对吧?如果是这样的话,connect就没有这些重载。字符串消息=_client.Connect(hostname, port, credential, ESSLSupportMode.Implicit, null, null, 0, 0, 0, null); - nitesh.kodle123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接