簡體   English   中英

如何驗證SMIME多部分/已簽名應用程序/ x-pkcs7簽名郵件的簽名

[英]Howto Verify Signature of a SMIME multipart/signed application/x-pkcs7-signature Mail

我正在使用一個更大的應用程序,該應用程序通過POP3,IMAP或通過從.msg文件導入(從Outlook導出或從Outlook拖入)來接收電子郵件。

最近,我收到了一封帶有附件“ smime.p7m”的電子郵件。 經過進一步檢查,結果證明是帶有

內容類型:多部分/已簽名; protocol =“ application / x-pkcs7-signature”;

除其他部分外,它包含一個部分

內容類型:application / x-pkcs7-signature; name =“ smime.p7s” Content-Transfer-Encoding:base64 Content-Disposition:附件; filename =“ smime.p7s”

我試圖使用OpenPop作為MIME消息解析器和SignedCms來檢查此簽名來驗證此簽名。 我的嘗試如下所示:

var datapart = OpenPop.MessagePart[...];
var part3 = OpenPop.MessagePart[3]; // the signature

var ci = new ContentInfo(datapart);            
var sCMS = new SignedCms(ci, detached: true);
sCMS.Decode(part3.Body);
sCMS.CheckHash();

sCMS.CheckSignature(verifySignatureOnly:true);

但無論怎樣我用datapart我總是

System.Security.Cryptography.CryptographicException哈希值不正確。

如何驗證簽名?

有更好的方法嗎?

最簡單的方法是使用MimeKit (它不僅是開源的,而且對於商業用途是免費的)。

由於您只關心驗證簽名,因此可以只使用MimeKit.Cryptography.TemporarySecureMimeContext而不是設置自己的簽名(如README和其他有關此操作的文檔中所述)。

通常,當您收到通過S / MIME簽名的消息時,幾乎始終是根級MIME部分,即multipart/signed部分,這使此操作有些容易(驗證簽名的第一步是找到multipart/signed部分)。

var signed = message.Body as MultipartSigned;
if (signed != null) {
    using (var ctx = new TemporaryMimeContext ()) {
        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.
            }
        }
    }
}

您可以在www.mimekit.net/docs上找到MimeKit的API文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM