Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Tcl Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Tcl) Backup Windows Current User / Personal Certificates to a .zip

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 Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

set certStore [new_CkCertStore]

set readOnly 1
set success [CkCertStore_OpenCurrentUserStore $certStore $readOnly]
if {!$success} then {
    puts [CkCertStore_lastErrorText $certStore]
    delete_CkCertStore $certStore
    exit
}

set pfxPassword "secret"

set allSuccess 1
set numSuccess 0

# entry is a CkZipEntry

set zip [new_CkZip]

CkZip_NewZip $zip "qa_output/personalCerts.zip"

set certData [new_CkBinData]

set sbFilename [new_CkStringBuilder]

# Iterate over the certificates in the Current User store.
set numCerts [CkCertStore_get_NumCertificates $certStore]
set i 0
while {$i < $numCerts} {
    # cert is a CkCert
    set cert [CkCertStore_GetCertificate $certStore $i]
    puts "DN = [CkCert_subjectDN $cert]"

    CkStringBuilder_SetString $sbFilename "cert"
    CkStringBuilder_AppendInt $sbFilename [expr $i + 1]

    set bHasPrivateKey [CkCert_HasPrivateKey $cert]
    if {expr [$bHasPrivateKey == 1]  &&  [[CkCert_get_PrivateKeyExportable $cert] == 1]} then {
        # Export to a .pfx
        set success [CkCert_ExportToPfxBd $cert $pfxPassword 1 $certData]
        if {$success == 1} then {
            CkStringBuilder_Append $sbFilename ".pfx"
            set entry [CkZip_AppendBd $zip [CkStringBuilder_getAsString $sbFilename] $certData]
            delete_CkZipEntry $entry

        }

    }     else {
        # Export to a .cer
        set success [CkCert_ExportCertDerBd $cert $certData]
        if {$success == 1} then {
            CkStringBuilder_Append $sbFilename ".cer"
            set entry [CkZip_AppendBd $zip [CkStringBuilder_getAsString $sbFilename] $certData]
            delete_CkZipEntry $entry

        }

    }

    if {$success != 1} then {
        set allSuccess 0
    }     else {
        set numSuccess [expr $numSuccess + 1]
    }

    set i [expr $i + 1]
}

if {$numSuccess > 0} then {
    set success [CkZip_WriteZipAndClose $zip]
    if {$success != 1} then {
        puts [CkZip_lastErrorText $zip]
        set allSuccess 0
    }

}

puts "All success = $allSuccess"

delete_CkCertStore $certStore
delete_CkZip $zip
delete_CkBinData $certData
delete_CkStringBuilder $sbFilename

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.