DataFlex
DataFlex
Backup Windows Current User / Personal Certificates to a .zip
See more Certificates Examples
Demonstrates how to backup the certificates in the Windows registry-based Current User certificate store (in the "Personal" Logical Store as seen in certmgr.msc), to a zip archive. Certificates having an exportable private key are exported to .pfx files. Certificates with no private key, or with a non-exportable private key, are exported to .cer files.Obviously, this example only runs on Windows computers.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vCert
Store Handle hoCertStore
Boolean iReadOnly
String sPfxPassword
Boolean iAllSuccess
Integer iNumSuccess
Handle hoZip
Variant vCert
Data Handle hoCertData
Handle hoSbFilename
Variant vCert
Handle hoCert
Integer iNumCerts
Integer i
Boolean iBHasPrivateKey
String sTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatCertStore)) To hoCertStore
If (Not(IsComObjectCreated(hoCertStore))) Begin
Send CreateComObject of hoCertStore
End
Move True To iReadOnly
Get ComOpenCurrentUserStore Of hoCertStore iReadOnly To iSuccess
If (Not iSuccess) Begin
Get ComLastErrorText Of hoCertStore To sTemp1
Showln sTemp1
Procedure_Return
End
Move "secret" To sPfxPassword
Move True To iAllSuccess
Move 0 To iNumSuccess
Get Create (RefClass(cComChilkatZip)) To hoZip
If (Not(IsComObjectCreated(hoZip))) Begin
Send CreateComObject of hoZip
End
Get ComNewZip Of hoZip "qa_output/personalCerts.zip" To iSuccess
Get Create (RefClass(cComChilkatBinData)) To hoCertData
If (Not(IsComObjectCreated(hoCertData))) Begin
Send CreateComObject of hoCertData
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbFilename
If (Not(IsComObjectCreated(hoSbFilename))) Begin
Send CreateComObject of hoSbFilename
End
// Iterate over the certificates in the Current User store.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComNumCertificates Of hoCertStore To iNumCerts
Move 0 To i
While (i < iNumCerts)
Get pvComObject of hoCert to vCert
Get ComGetCert Of hoCertStore i vCert To iSuccess
Get ComSubjectDN Of hoCert To sTemp1
Showln "DN = " sTemp1
Get ComSetString Of hoSbFilename "cert" To iSuccess
Get ComAppendInt Of hoSbFilename (i + 1) To iSuccess
Get ComHasPrivateKey Of hoCert To iBHasPrivateKey
Get ComPrivateKeyExportable Of hoCert To bTemp1
If ((iBHasPrivateKey = True) And (bTemp1 = True)) Begin
// Export to a .pfx
Get pvComObject of hoCertData to vCertData
Get ComExportToPfxBd Of hoCert sPfxPassword True vCertData To iSuccess
If (iSuccess = True) Begin
Get ComAppend Of hoSbFilename ".pfx" To iSuccess
Get ComGetAsString Of hoSbFilename To sTemp1
Get pvComObject of hoCertData to vCertData
Get ComAddBd Of hoZip sTemp1 vCertData To iSuccess
End
End
Else Begin
// Export to a .cer
Get pvComObject of hoCertData to vCertData
Get ComExportCertDerBd Of hoCert vCertData To iSuccess
If (iSuccess = True) Begin
Get ComAppend Of hoSbFilename ".cer" To iSuccess
Get ComGetAsString Of hoSbFilename To sTemp1
Get pvComObject of hoCertData to vCertData
Get ComAddBd Of hoZip sTemp1 vCertData To iSuccess
End
End
If (iSuccess <> True) Begin
Move False To iAllSuccess
End
Else Begin
Move (iNumSuccess + 1) To iNumSuccess
End
Move (i + 1) To i
Loop
If (iNumSuccess > 0) Begin
Get ComWriteZipAndClose Of hoZip To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoZip To sTemp1
Showln sTemp1
Move False To iAllSuccess
End
End
Showln "All success = " iAllSuccess
End_Procedure