Sample code for 30+ languages & platforms
PowerBuilder

Verify an S/MIME Signed Message

See more MIME Examples

Demonstrates the Chilkat Mime.Verify method, which verifies a CMS/PKCS #7 signed MIME entity and removes the signature wrapper. It takes no arguments and supports both detached (multipart/signed) and opaque (signed-data) messages.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Verification confirms two things: that the content was signed by the holder of the signer's certificate, and that it has not been altered since. On success Chilkat strips the signature layer so the object holds the original content, ready to read — effectively "open and check" in one step. A false return means the signature is invalid or the content was tampered with, which is exactly the case your code must not treat as trusted.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Mime
string ls_Body

li_Success = 0

loo_Mime = create oleobject
li_rc = loo_Mime.ConnectToNewObject("Chilkat.Mime")
if li_rc < 0 then
    destroy loo_Mime
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Mime.LoadMimeFile("qa_data/signed.eml")
if li_Success = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

//  Verify the signature and remove the signature wrapper, restoring the underlying content.  Both
//  detached (multipart/signed) and opaque (CMS signed-data) messages are supported.
li_Success = loo_Mime.Verify()
if li_Success <> 1 then
    Write-Debug "Signature verification failed."
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

Write-Debug "Signature verified. Number of signers: " + string(loo_Mime.NumSignerCerts)

//  The object now holds the original content with the signature removed.
ls_Body = loo_Mime.GetBodyDecoded()
if loo_Mime.LastMethodSuccess = 0 then
    Write-Debug loo_Mime.LastErrorText
    destroy loo_Mime
    return
end if

Write-Debug ls_Body


destroy loo_Mime