Smime Class
- Not supported on browser
Reads the signed and encrypted MIME entities of S/MIME ([RFC 8551]): checks the signatures of clear-signed (multipart/signed) and opaque-signed entities, and decrypts the encrypted ones with the certificate of a recipient. To read a signed or encrypted message, pass its MimeMessage.Body.
[UnsupportedOSPlatform("browser")]
public static class SmimeExample
var message = MimeMessage.Load(bytes);
Assert.Equal(SmimeFormat.Encrypted, Smime.GetFormat(message.Body));
// Decrypt with the certificate of the recipient, which has its private key
var decrypted = Smime.Decrypt(message.Body, [certificate]);
Assert.Equal(SmimeFormat.ClearSigned, Smime.GetFormat(decrypted));
// Check the signature: verifySignatureOnly does not check the chain of the certificate
var signed = Smime.Verify(decrypted, verifySignatureOnly: true);
Assert.True(signed.IsValid);
Assert.Equal("alice@example.com", signed.Signatures.Single().Certificate?.GetNameInfo(X509NameType.EmailName, forIssuer: false));
Assert.Equal("Hello", ((MimePart)signed.Content).GetText().TrimEnd());Methods
| Name | Description |
|---|---|
| Decrypt(MimeEntity, X509Certificate2Collection, MimeReadOptions?) | Decrypts an encrypted entity with the certificate of one of its recipients, and returns the entity it contains, which can be signed too. |
| GetFormat(MimeEntity) | Returns the S/MIME structure of an entity, from its content type, and from the content of its signature or encryption when the content type does not say it. |
| Verify(MimeEntity, bool, X509Certificate2Collection?, MimeReadOptions?) | Checks the signatures of a signed entity, and returns its content. |
Decrypt(MimeEntity, X509Certificate2Collection, MimeReadOptions?)
Smime.csDecrypts an encrypted entity with the certificate of one of its recipients, and returns the entity it contains, which can be signed too.
public static MimeEntity Decrypt(MimeEntity entity, X509Certificate2Collection certificates, MimeReadOptions? options = null)Parameters
entityMimeEntity- An encrypted entity (see
Smime.GetFormat). certificatesX509Certificate2Collection- Certificates with their private key; the certificates of the store of the current user are used too.
optionsMimeReadOptions?- The options to read the decrypted entity, or
nullfor the default options.
Returns
MimeEntity The decrypted entity.
Exceptions
InvalidOperationException- The entity is not encrypted.
InvalidEmailDataException- The encrypted content is malformed.
UnsupportedEmailFeatureException- The content uses authenticated encryption (
authEnveloped-data), which the library does not support. CryptographicException- No certificate can decrypt the content: none of them is the certificate of a recipient with its private key.
GetFormat(MimeEntity)
Smime.csReturns the S/MIME structure of an entity, from its content type, and from the content of its signature or encryption when the content type does not say it.
public static SmimeFormat GetFormat(MimeEntity entity)Parameters
entityMimeEntity- The entity, such as the body of a message.
Returns
SmimeFormat The structure, or SmimeFormat.None.
Verify(MimeEntity, bool, X509Certificate2Collection?, MimeReadOptions?)
Smime.csChecks the signatures of a signed entity, and returns its content.
public static SmimeSignedContent Verify(MimeEntity entity, bool verifySignatureOnly = false, X509Certificate2Collection? extraStore = null, MimeReadOptions? options = null)Parameters
entityMimeEntity- A clear-signed or opaque-signed entity (see
Smime.GetFormat). verifySignatureOnlyboolfalseto also check the certificates of the signers: their chain of trust, their validity and their usage;trueto check only that the content was signed by the keys of the certificates.extraStoreX509Certificate2Collection?- Certificates used to find the certificates of the signers and to build their chains, or
null. optionsMimeReadOptions?- The options to read the content of an opaque signature, or
nullfor the default options.
Returns
SmimeSignedContent The content and the result of the verification of each signature.
Exceptions
InvalidOperationException- The entity is not signed.
InvalidEmailDataException- The signature is malformed.