Sample code for 30+ languages & platforms
Tcl

Set Cert and Private Key for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: Sometimes the certificate (the public part) and the private key are stored separately — a .cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
#  separately supplied private key for decrypting S/MIME messages.  The 1st argument is the
#  Cert and the 2nd is the PrivateKey.

set imap [new_CkImap]

CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993

set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set success [CkImap_Login $imap "user@example.com" "myPassword"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

#  Load the certificate and its private key from separate files.
set cert [new_CkCert]

set success [CkCert_LoadFromFile $cert "qa_data/smime.cer"]
if {$success == 0} then {
    puts [CkCert_lastErrorText $cert]
    delete_CkImap $imap
    delete_CkCert $cert
    exit
}

set privKey [new_CkPrivateKey]

set success [CkPrivateKey_LoadPemFile $privKey "qa_data/smime_privkey.pem"]
if {$success == 0} then {
    puts [CkPrivateKey_lastErrorText $privKey]
    delete_CkImap $imap
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}

#  Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
set success [CkImap_SetDecryptCert2 $imap $cert $privKey]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}

set success [CkImap_Disconnect $imap]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkCert $cert
    delete_CkPrivateKey $privKey
    exit
}


delete_CkImap $imap
delete_CkCert $cert
delete_CkPrivateKey $privKey