PureBasic
PureBasic
Add a PFX Source (BinData) for S/MIME Decryption
See more IMAP Examples
Demonstrates the Chilkat Imap.AddPfxSourceBd method, which adds PFX data held in a BinData as a source of certificates and private keys for S/MIME processing. The first argument is the BinData and the second is the PFX password. Call it once for each additional source.
Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.
Background: When downloading encrypted (S/MIME) email, Chilkat needs the recipient's private key to decrypt it. Adding one or more PFX sources gives Chilkat a pool of keys to search; the matching key is used automatically as messages are fetched. The
BinData form is handy when the PFX comes from memory — a database blob or a downloaded buffer — rather than a file on disk. On Windows and macOS the system certificate stores are also searched automatically.Chilkat PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkImap.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the Imap.AddPfxSourceBd method, which adds PFX data (from a BinData) as a
; source of certificates and private keys for S/MIME decryption. The 1st argument is the
; BinData containing the PFX, and the 2nd is the PFX password.
imap.i = CkImap::ckCreate()
If imap.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkImap::setCkSsl(imap, 1)
CkImap::setCkPort(imap, 993)
success = CkImap::ckConnect(imap,"imap.example.com")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
success = CkImap::ckLogin(imap,"user@example.com","myPassword")
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
ProcedureReturn
EndIf
; Load the PFX data into a BinData.
bdPfx.i = CkBinData::ckCreate()
If bdPfx.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkBinData::ckLoadFile(bdPfx,"qa_data/smime.pfx")
If success = 0
Debug CkBinData::ckLastErrorText(bdPfx)
CkImap::ckDispose(imap)
CkBinData::ckDispose(bdPfx)
ProcedureReturn
EndIf
; The PFX password is not hard-coded in source. Insert code here to obtain it from an
; interactive prompt, environment variable, or a secrets vault.
pfxPassword.s
; Add the PFX as a source for S/MIME certificates and private keys. Call once per source.
success = CkImap::ckAddPfxSourceBd(imap,bdPfx,pfxPassword)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkBinData::ckDispose(bdPfx)
ProcedureReturn
EndIf
; Messages downloaded after this are automatically decrypted when a matching key is found.
success = CkImap::ckDisconnect(imap)
If success = 0
Debug CkImap::ckLastErrorText(imap)
CkImap::ckDispose(imap)
CkBinData::ckDispose(bdPfx)
ProcedureReturn
EndIf
CkImap::ckDispose(imap)
CkBinData::ckDispose(bdPfx)
ProcedureReturn
EndProcedure