Sample code for 30+ languages & platforms
Tcl

Load PKCS12 / PFX and Access Contents

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set pfx [new_CkPfx]

# Load the PKCS12 from a file
set success [CkPfx_LoadPfxFile $pfx "/someDir/my.p12" "pfxFilePassword"]
if {$success == 0} then {
    puts [CkPfx_lastErrorText $pfx]
    delete_CkPfx $pfx
    exit
}

set numPrivateKeys [CkPfx_get_NumPrivateKeys $pfx]

set privKey [new_CkPrivateKey]

puts "Private Keys:"

set i 0
while {$i < $numPrivateKeys} {
    CkPfx_PrivateKeyAt $pfx $i $privKey

    # Do something with the private key ...

    set i [expr $i + 1]
}

set cert [new_CkCert]

set numCerts [CkPfx_get_NumCerts $pfx]

puts "Certs:"
set i 0
while {$i < $numCerts} {
    CkPfx_CertAt $pfx $i $cert
    puts [CkCert_subjectDN $cert]

    # If the certificate has a private key (one of the private keys within the PFX)
    # then it can also be obtained via the certificate object:
    if {[CkCert_HasPrivateKey $cert] == 1} then {

        puts "Has private key!"

        set success [CkCert_GetPrivateKey $cert $privKey]
        # ...

    }

    set i [expr $i + 1]
}

delete_CkPfx $pfx
delete_CkPrivateKey $privKey
delete_CkCert $cert