Sample code for 30+ languages & platforms
PureBasic

Read a PFX Safe-Bag Attribute

See more PFX/P12 Examples

Demonstrates Pfx.GetSafeBagAttr, which reads a recognized safe-bag attribute from a private key or certificate inside the PFX.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. A safe-bag attribute is metadata attached to an item inside a PKCS#12/PFX container; it does not change the certificate or private-key material. Recognized attributes include friendlyName, keyContainerName, keyName, and localKeyId.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPfx.pb"

Procedure ChilkatExample()

    success.i = 0

    pfx.i = CkPfx::ckCreate()
    If pfx.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  The file path is relative to the application's current working directory.  An absolute path
    ;  may also be used.  Supply the path appropriate to your own environment.
    ;  The PFX password should come from a secure source rather than being hard-coded.
    password.s = "myPfxPassword"
    success = CkPfx::ckLoadPfxFile(pfx,"qa_data/identity.pfx",password)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        ProcedureReturn
    EndIf

    ;  Read a recognized safe-bag attribute from an item inside the PFX.  The 1st argument is 1 for a
    ;  private key or 0 for a certificate, and the 2nd is the zero-based index within that
    ;  collection.
    bForPrivateKey.i = 1
    friendlyName.s = CkPfx::ckGetSafeBagAttr(pfx,bForPrivateKey,0,"friendlyName")
    If CkPfx::ckLastMethodSuccess(pfx) = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        ProcedureReturn
    EndIf

    Debug "friendlyName: " + friendlyName


    CkPfx::ckDispose(pfx)


    ProcedureReturn
EndProcedure