【问题标题】:How to sign message body with RSASSA-PSS in mimekit?如何在 mimekit 中使用 RSASSA-PSS 签署消息正文?
【发布时间】:2019-09-04 22:45:38
【问题描述】:

我正在使用带有 .NET Core 2.2 的 Mimekit。我正在尝试使用 RSASSA-PSS 对消息进行签名,但找不到为 RSASignaturePadding 设置签名算法的方法。更改 DigestAlgorithm 会导致使用错误的填充。

如何修复此代码 sn-p 以使用 RSASSA-PSS 而不是默认填充?

    public MimeMessage SignMessage(MimeMessage message, MailboxAddress address)
    {
        CryptographyContext.Register(typeof(WindowsSecureMimeContext));

        using (var ctx = new WindowsSecureMimeContext(StoreLocation.LocalMachine))
        {
            X509Certificate2 cert = null;
            string thumbprint = "<myThumbprint>";
            var machineStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            machineStore.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection machineCerts = machineStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
            if (machineCerts.Count == 1)
            {
                cert = machineCerts[0];
            }

            if (cert != null)
            {

                CmsSigner signer = new CmsSigner(cert)
                {
                    DigestAlgorithm = DigestAlgorithm.Sha256
                };


                message.Body = MultipartSigned.Create(ctx, signer, message.Body);
            }
        }

        return message;
    }

【问题讨论】:

    标签: c# mimekit


    【解决方案1】:

    目前在 MimeKit 中无法做到这一点,而且似乎 SignedCms 类没有任何方法可以指定 PSS 填充模式,除非它是 X509Certificate2 的私钥上的属性?

    更新:

    我做了更多的研究,并想出了如何使用 BouncyCastle 来支持这一点,所以我刚刚添加了一个 CmsSigner.RsaSignaturePaddingScheme 属性,它允许您指定 RsaSignaturePaddingScheme.Pss

    在使用基于 BouncyCastle 的 SecureMimeContext 时有效,但是,您可以这样做:

    public MimeMessage SignMessage(MimeMessage message, MailboxAddress address)
    {
        using (var ctx = new TemporarySecureMimeContext ())
        {
            X509Certificate2 cert = null;
            string thumbprint = "<myThumbprint>";
            var machineStore = new X509Store(StoreName.My, StoreLocation.LocalMachine);
            machineStore.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection machineCerts = machineStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
            if (machineCerts.Count == 1)
            {
                cert = machineCerts[0];
            }
    
            if (cert != null)
            {
                CmsSigner signer = new CmsSigner(cert)
                {
                    RsaSignaturePaddingScheme = RsaSignaturePaddingScheme.Pss,
                    DigestAlgorithm = DigestAlgorithm.Sha256
                };
    
    
                message.Body = MultipartSigned.Create(ctx, signer, message.Body);
            }
        }
    
        return message;
    }
    

    今天开始使用这个功能(我还没有公开发布),你可以去https://github.com/jstedfast/MimeKit获取最新的MyGet构建(这是一个CI生成的nuget包) .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-07
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多