[英]How to sign message body with RSASSA-PSS in mimekit?
我在 .NET Core 2.2 中使用 Mimekit。 我正在尝试使用 RSASSA-PSS 对消息进行签名,但找不到为 RSASignaturePadding 设置签名算法的方法。 更改 DigestAlgorithm 会导致使用错误的填充。
如何修复此代码片段以使用 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;
}
这目前在 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 包)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.