/// <summary> /// Create the ContentType MIME header for a Signed MIME entity /// </summary> /// <param name="digestAlgorithm">Digest algorithm being used, such as SHA1</param> /// <returns>ContentType header</returns> public static ContentType CreateContentType(DigestAlgorithm digestAlgorithm) { ContentType contentType = new ContentType(SMIMEStandard.MultiPartTypeSigned); contentType.Parameters.Add(SMIMEStandard.ProtocolParameterKey, SMIMEStandard.SignatureProtocol); contentType.Parameters.Add(SMIMEStandard.MICAlgorithmKey, SMIMEStandard.ToString(digestAlgorithm)); return(contentType); }
//----------------------------------------------------- // // Decryption // //----------------------------------------------------- // // Cryptography is performed only on the Mime portions of the message, not the RFC822 headers // /// <summary> /// Given a MimeEntity: /// - Checks that the MimeEntity is encrypted /// - Converts the entity body to bytes... /// The returned bytes can then be decrypted using the appropriate private key /// </summary> /// <param name="encryptedEntity">Entity containinng encrypted data</param> /// <returns>encrypted bytes</returns> public byte[] GetEncryptedBytes(MimeEntity encryptedEntity) { if (encryptedEntity == null) { throw new EncryptionException(EncryptionError.NullEntity); } if (!SMIMEStandard.IsEncrypted(encryptedEntity)) { throw new EncryptionException(EncryptionError.NotEncrypted); } return(Convert.FromBase64String(encryptedEntity.Body.Text)); }
/// <summary> /// Tranforms an enveloped signature to the corresponding <see cref="SignedCms"/> /// </summary> /// <param name="envelopeEntity">The entity containing the enveloped signature</param> /// <returns>the corresponding <see cref="SignedCms"/></returns> public SignedCms DeserializeEnvelopedSignature(MimeEntity envelopeEntity) { if (envelopeEntity == null) { throw new SignatureException(SignatureError.NullEntity); } if (!SMIMEStandard.IsSignedEnvelope(envelopeEntity)) { throw new SignatureException(SignatureError.NotSignatureEnvelope); } byte[] envelopeBytes = Convert.FromBase64String(envelopeEntity.Body.Text); return(DeserializeEnvelopedSignature(envelopeBytes)); }
/// <summary> /// Transforms the <paramref name="algorithm"/> to a string suitable for the <c>micalg</c> parameter value /// </summary> /// <param name="algorithm">The digest algorithm to transform</param> /// <returns>A string suitable for the value of the <c>micalg</c> parameter</returns> public static string AsString(this DigestAlgorithm algorithm) { return(SMIMEStandard.ToString(algorithm)); }
/// <summary> /// Tests content type to determine if it indicates a detached signature /// </summary> /// <param name="contentType">The content-type to examine</param> /// <returns><c>true</c> if this is a detached signature, <c>false</c> if not</returns> public static bool IsDetachedSignature(this ContentType contentType) { return(SMIMEStandard.IsContentDetachedSignature(contentType)); }
/// <summary> /// Tests content type to determine if it indicates a multipart message with detached signature /// </summary> /// <param name="contentType">The content-type to examine</param> /// <returns><c>true</c> if this is multipart signature content, <c>false</c> if not</returns> public static bool IsMultipartSignature(this ContentType contentType) { return(SMIMEStandard.IsContentMultipartSignature(contentType)); }
/// <summary> /// Tests content type to determine if it indicates enveloped (non-detached) signature data /// </summary> /// <param name="contentType">The content-type to examine</param> /// <returns><c>true</c> if this is eveloped signature content, <c>false</c> if not</returns> public static bool IsEnvelopedSignature(this ContentType contentType) { return(SMIMEStandard.IsContentEnvelopedSignature(contentType)); }
/// <summary> /// Tests content type to determine if it indicates encrypted data /// </summary> /// <param name="contentType">The content-type to examine</param> /// <returns><c>true</c> if this is encrypted content, <c>false</c> if not</returns> public static bool IsEncrypted(this ContentType contentType) { return(SMIMEStandard.IsContentEncrypted(contentType)); }
//--------------------------------------- // // ContentType // //--------------------------------------- /// <summary> /// Tests if the content type indicates enveloped (encrypted or enveloped signed) data. /// </summary> /// <param name="contentType">The content type to test</param> /// <returns><c>true</c> if this is enveloped content, <c>false</c> if not</returns> public static bool IsCms(this ContentType contentType) { return(SMIMEStandard.IsContentCms(contentType)); }