Sample code for 30+ languages & platforms
DataFlex

MIME S/MIME Signing Algorithm Properties

See more MIME Examples

Demonstrates the properties that control S/MIME signing: SigningAlg (the RSA signature scheme, PKCS1-V1_5 or RSASSA-PSS), SigningHashAlg (the message-digest algorithm), Micalg and Protocol (the micalg and protocol parameters of a multipart/signed Content-Type), and UseXPkcs7 (whether historical x-pkcs7 media-type names are used). The properties are configured before creating a detached signature.

Background. These settings determine how the CMS/PKCS #7 signature is produced and how a multipart/signed wrapper advertises it. The defaults (PKCS1-V1_5, sha256) suit most modern uses; the properties allow interoperability with specific requirements.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoMime
    String sPfxPassword
    Variant vCert
    Handle hoCert
    String sTemp1
    String sTemp2

    Move False To iSuccess

    Get Create (RefClass(cComChilkatMime)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get ComSetBodyFromPlainText Of hoMime "This message will be signed." To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  SigningAlg is the RSA signature scheme.  The default is PKCS1-V1_5; set RSASSA-PSS (or pss) for
    //  RSASSA-PSS.
    Set ComSigningAlg Of hoMime To "PKCS1-V1_5"

    //  SigningHashAlg is the message-digest algorithm.  The default is sha256.
    Set ComSigningHashAlg Of hoMime To "sha256"

    //  Micalg and Protocol are the micalg and protocol parameters written into a multipart/signed
    //  Content-Type header field.  They are normally set automatically, but can be controlled here.
    Set ComMicalg Of hoMime To "sha-256"
    Set ComProtocol Of hoMime To "application/pkcs7-signature"

    //  UseXPkcs7 controls whether newly created S/MIME media types use the historical x-pkcs7 names.
    Set ComUseXPkcs7 Of hoMime To False

    //  Load a signing certificate (with private key access) from a PFX.  The PFX password should come
    //  from a secure source rather than being hard-coded.
    Move "myPfxPassword" To sPfxPassword
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComLoadPfxFile Of hoCert "qa_data/signer.pfx" sPfxPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Create the detached signature using the algorithms configured above.
    Get pvComObject of hoCert to vCert
    Get ComAddDetachedSignature Of hoMime vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoMime To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSigningAlg Of hoMime To sTemp1
    Get ComSigningHashAlg Of hoMime To sTemp2
    Showln "Signed with SigningAlg=" sTemp1 " SigningHashAlg=" sTemp2


End_Procedure