Sample code for 30+ languages & platforms
DataFlex

Get Certificate User Principal Name (UPN)

See more Certificates Examples

Demonstrates how to get a certificate's UPN from the SAN (Subject Alternative Name). Most certificates do not have a UPN. This example only applies to those certificates that have a User Principal Name.

Note: This example requires Chilkat v9.5.0.90 or greater due to fixes made in getting the UPN from the SAN.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCert
    Handle hoXml
    String sUpn
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    Get ComLoadFromFile Of hoCert "qa_data/certs/sample.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get ComSubjectAlternativeName Of hoCert To sTemp1
    Get ComLoadXml Of hoXml sTemp1 To iSuccess
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

    // A certificate with a User Principal Name in the SAN might have this:

    // <SubjectAltName>
    //     <rfc822Name>joe@example.com</rfc822Name>
    //     <name type="oid" oid="1.3.6.1.4.1.311.20.2.3">joe@example.com</name>
    // </SubjectAltName>

    // The OID 1.3.6.1.4.1.311.20.2.3 is for the User Principal Name.
    Get ComChilkatPath Of hoXml "/A/name,oid,1.3.6.1.4.1.311.20.2.3|*" To sUpn
    Get ComLastMethodSuccess Of hoXml To bTemp1
    If (bTemp1 = False) Begin
        Showln "No user principle name."
    End
    Else Begin
        Showln "User Principle Name = " sUpn
    End



End_Procedure