Sample code for 30+ languages & platforms
Tcl

Get the Certificate Used for Decryption

See more MIME Examples

Loads a CMS/PKCS #7 encrypted S/MIME message and decrypts it back to the original MIME content.

After a successful decryption, LastDecryptCert loads the certificate(s) actually used into a Cert object, indexed from zero up to NumDecryptCerts minus one.

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. Following decryption or security unwrapping, Chilkat records which certificate(s) were used. LastDecryptCert retrieves them so the application can report or verify the identity involved.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set mime [new_CkMime]

set success [CkMime_LoadMimeFile $mime "qa_data/encrypted.eml"]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkMime $mime
    exit
}

#  Configure a decryption source and decrypt.
set pfxPassword "myPfxPassword"
set success [CkMime_AddPfxSourceFile $mime "qa_data/recipient.pfx" $pfxPassword]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkMime $mime
    exit
}

set success [CkMime_Decrypt $mime]
if {$success == 0} then {
    puts [CkMime_lastErrorText $mime]
    delete_CkMime $mime
    exit
}

#  After a successful decryption, load the certificate(s) that were used.  The 1st argument is the
#  zero-based index and the 2nd is a Cert that receives the certificate.
set n [CkMime_get_NumDecryptCerts $mime]
set cert [new_CkCert]

for {set i 0} {$i <= [expr $n - 1]} {incr i} {
    set success [CkMime_LastDecryptCert $mime $i $cert]
    if {$success == 0} then {
        puts [CkMime_lastErrorText $mime]
        delete_CkMime $mime
        delete_CkCert $cert
        exit
    }

    puts "Decrypted with: [CkCert_subjectCN $cert]"
}

delete_CkMime $mime
delete_CkCert $cert