Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.8.0+

Decrypt an S/MIME email and verify its signature

See more Email Object Examples

Register the recipient’s certificate and private key (from a PEM file) before opening signed-encrypted.eml. This workflow expects both encryption and a signature, so it checks both layers before reading the message. Cryptographic signature validity is one input to a signer-trust decision.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set email [new_CkEmail]

#  The recipient's certificate and private key are in a PEM file.  The 2nd argument of LoadPemFile
#  is the password of the private key.
set pem [new_CkPem]

set success [CkPem_LoadPemFile $pem "recipient.pem" "RECIPIENT_PASSWORD"]
if {$success != 1} then {
    puts [CkPem_lastErrorText $pem]
    delete_CkEmail $email
    delete_CkPem $pem
    exit
}

set success [CkEmail_AddPemSource $email $pem]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    delete_CkPem $pem
    exit
}

set success [CkEmail_LoadEml $email "signed-encrypted.eml"]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    delete_CkPem $pem
    exit
}

if {expr [[CkEmail_get_ReceivedEncrypted $email] != 1]  ||  [[CkEmail_get_Decrypted $email] != 1]} then {
    puts "Expected a successfully decrypted S/MIME envelope."
    delete_CkEmail $email
    delete_CkPem $pem
    exit
}

if {expr [[CkEmail_get_ReceivedSigned $email] != 1]  ||  [[CkEmail_get_SignaturesValid $email] != 1]} then {
    puts "Expected a valid signature inside the envelope."
    delete_CkEmail $email
    delete_CkPem $pem
    exit
}

#  This message has one signer. Examine its certificate after verification.
set signer [new_CkCert]

set success [CkEmail_LastSignerCert $email 0 $signer]
if {$success != 1} then {
    puts [CkEmail_lastErrorText $email]
    delete_CkEmail $email
    delete_CkPem $pem
    delete_CkCert $signer
    exit
}

puts "Signer certificate: [CkCert_subjectDN $signer]"
puts "Recovered subject: [CkEmail_subject $email]"
puts "Recovered attachments: [CkEmail_get_NumAttachments $email]"

delete_CkEmail $email
delete_CkPem $pem
delete_CkCert $signer