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 Smime

Example

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

NameDescription
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.cs

Decrypts 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

entity MimeEntity
An encrypted entity (see Smime.GetFormat).
certificates X509Certificate2Collection
Certificates with their private key; the certificates of the store of the current user are used too.
options MimeReadOptions?
The options to read the decrypted entity, or null for 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.cs

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.

public static SmimeFormat GetFormat(MimeEntity entity)

Parameters

entity MimeEntity
The entity, such as the body of a message.

Returns

SmimeFormat The structure, or SmimeFormat.None.

Verify(MimeEntity, bool, X509Certificate2Collection?, MimeReadOptions?)

Smime.cs

Checks 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

entity MimeEntity
A clear-signed or opaque-signed entity (see Smime.GetFormat).
verifySignatureOnly bool
false to also check the certificates of the signers: their chain of trust, their validity and their usage; true to check only that the content was signed by the keys of the certificates.
extraStore X509Certificate2Collection?
Certificates used to find the certificates of the signers and to build their chains, or null.
options MimeReadOptions?
The options to read the content of an opaque signature, or null for 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.