Reputation: 33
I want to JUST check if a MIME message is encrypted or signed using Mimekit.
I don't want to decrypt it or verify its signature, I just want to know if it is encrypted or signed.
I expected to find this as functions: IsSigned() & IsEncrypted() in the MimeMessage Class but these functions don't exist!
Thank you.
Upvotes: 3
Views: 1211
Reputation: 38538
Those methods don't exist because a MimeMessage
itself cannot be signed or encrypted, only the Body
(or a subset of the body) of the message can be signed or encrypted.
A very simple solution is to do this:
var pkcs7 = message.Body as ApplicationPkcs7Mime;
bool signed = (message.Body is MultipartSigned) ||
(pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.SignedData);
bool encrypted = (message.Body is MultipartEncrypted) ||
(pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.EnvelopedData);
Note: MultipartEncrypted
is only used with PGP (not S/MIME), so if you only care about S/MIME, you do not need to check for MultipartEncrypted
.
Likewise, ApplicationPkcs7Mime
is only used for S/MIME, so if you only care about PGP, you do not need to check for ApplicationPkcs7Mime
.
Both PGP and S/MIME (can) use MultipartSigned
, though, so you'll have to check that in either case.
Since every client I've ever used or received mail from only ever signs and/or encrypts the top-level Body
part of the message, the above check is likely all you'll need. However, it is possible for MIME-compliant clients to sign and/or encrypt a subpart of the Body
, so you'd have to traverse the MIME tree and check each node if you want to be 100% compliant.
Upvotes: 4