Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkMime.pb"

Procedure ChilkatExample()

    success.i = 0

    mime.i = CkMime::ckCreate()
    If mime.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkMime::ckSetBodyFromPlainText(mime,"This message will be signed.")
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        ProcedureReturn
    EndIf

    ;  SigningAlg is the RSA signature scheme.  The default is PKCS1-V1_5; set RSASSA-PSS (or pss) for
    ;  RSASSA-PSS.
    CkMime::setCkSigningAlg(mime, "PKCS1-V1_5")

    ;  SigningHashAlg is the message-digest algorithm.  The default is sha256.
    CkMime::setCkSigningHashAlg(mime, "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.
    CkMime::setCkMicalg(mime, "sha-256")
    CkMime::setCkProtocol(mime, "application/pkcs7-signature")

    ;  UseXPkcs7 controls whether newly created S/MIME media types use the historical x-pkcs7 names.
    CkMime::setCkUseXPkcs7(mime, 0)

    ;  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.
    pfxPassword.s = "myPfxPassword"
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"qa_data/signer.pfx",pfxPassword)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ;  Create the detached signature using the algorithms configured above.
    success = CkMime::ckAddDetachedSignature(mime,cert)
    If success = 0
        Debug CkMime::ckLastErrorText(mime)
        CkMime::ckDispose(mime)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    Debug "Signed with SigningAlg=" + CkMime::ckSigningAlg(mime) + " SigningHashAlg=" + CkMime::ckSigningHashAlg(mime)


    CkMime::ckDispose(mime)
    CkCert::ckDispose(cert)


    ProcedureReturn
EndProcedure