【问题标题】:Digitalsignature verification failed using Mimekit使用 Mimekit 验证数字签名失败
【发布时间】:2019-12-02 11:54:43
【问题描述】:

我们正在尝试使用 MimeKit 来验证数字签名的电子邮件 (.p7m) 签名。当我调用signature.Verify(); 时,它会抛出错误消息:

{"验证数字签名失败:非空集 必需\r\n参数名称:值"}。

但同一封邮件已被 Limilabs.Mail 成功验证。

我正在使用下面的代码来验证签名。

if (message.Body is MultipartSigned)
{
    var signed = (MultipartSigned)message.Body;
    foreach (var signature in signed.Verify())
    {
        try
        {
            bool valid = signature.Verify();

            // If valid is true, then it signifies that the signed content
            // has not been modified since this particular signer signed the
            // content.
            // However, if it is false, then it indicates that the signed
            // content has been modified.
        }
        catch (DigitalSignatureVerifyException)
        {
            // There was an error verifying the signature.
        }
    }
}

任何人都可以帮助我解决为什么我收到错误吗?

【问题讨论】:

  • 安全性有很多选择。您可能对 32 位和 64 位加密模式有疑问。请参阅:en.wikipedia.org/wiki/Transport_Layer_Security
  • 除了Message 属性之外,还有更多的异常。异常的类型是什么? StackTrace 是什么?什么是 InnerException 类型/消息/堆栈跟踪?
  • 您将哪种类型的 SecureMimeContext 注册为默认值? WindowsSecureMimeContext?还是 DefaultSecureMimeContext?还是别的什么?
  • 我得到了 DigitalSignatureVerifyException,内部异常是 System.ArgumentException。内部异常消息是“需要非空集\r\n参数名称:值”
  • 堆栈跟踪——在 Org.BouncyCastle.Pkix.PkixParameters.SetTrustAnchors(ISet tas) 在 Org.BouncyCastle.Pkix.PkixParameters..ctor(ISet trustAnchors) 在 Org.BouncyCastle.Pkix.PkixBuilderParameters。 .ctor(ISet trustAnchors, IX509Selector targetConstraints) 在 MimeKit.Cryptography.BouncyCastleSecureMimeContext.BuildCertPath(HashSet 锚点, IX509Store 证书, IX509Store crls, X509Certificate 证书, DateTime signingTime) 在 MimeKit.Cryptography.BouncyCastleSecureMimeContext.d__29.MoveNext()跨度>

标签: c# cryptography smime mimekit mime-mail


【解决方案1】:

这里的问题是,默认情况下,当开发人员没有明确提供用于 MultipartSigned.Verify() 方法调用的上下文并且也没有注册替代方案时,MimeKit 默认使用 S/MIME 的 DefaultSecureMimeContext 后端使用 CryptographyContext.Register() 的 S/MIME 上下文。

由于DefaultSecureMimeContext 从一个空的 S/MIME 证书数据库开始,它没有受信任的锚点(也称为根证书颁发机构证书),因此在为验证签名时的 S/MIME 签名者。

您可以通过导入一些根证书颁发机构证书(最好包括为所述签名者构建证书链所需的证书)或使用WindowsSecureMimeContext 来解决此问题:

if (message.Body is MultipartSigned)
{
    var signed = (MultipartSigned)message.Body;

    using (var ctx = new WindowsSecureMimeContext ()) {
        foreach (var signature in signed.Verify(ctx))
        {
            try
            {
                bool valid = signature.Verify();

                // If valid is true, then it signifies that the signed content
                // has not been modified since this particular signer signed the
                // content.
                // However, if it is false, then it indicates that the signed
                // content has been modified.
            }
            catch (DigitalSignatureVerifyException)
            {
                // There was an error verifying the signature.
            }
        }
    }
}

【讨论】:

  • 嗨 jstedfast,感谢您的回答。添加 WindowsSecureMimeContext 后它工作正常。
  • 很高兴它对你有用。您介意点击“接受”我的回答吗?它将帮助其他用户无需阅读 cmets 即可了解该解决方案是否有效。谢谢!
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 2017-05-13
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-17
  • 1970-01-01
  • 2017-11-28
相关资源
最近更新 更多