Sample code for 30+ languages & platforms
Java

Binary Certificate Extension Data

See more Certificates Examples

Demonstrates how to binary certificate extension data by OID.

The GetExtensionBd method is added in Chilkat v9.5.0.96.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        Cert cert = new Cert();

        try {
            cert.loadFromFile("qa_data/certs/testCert.cer");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        BinData bd = new BinData();

        // The particular certificate in this test contains the following extensions

        String oid = "1.2.250.1.71.1.2.5";
        try {
            cert.getExtensionBd(oid, bd);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println(oid + ": " + bd.getEncoded("hex_lower"));
        }

        oid = "1.2.250.1.71.1.2.2";
        try {
            cert.getExtensionBd(oid, bd);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println(oid + ": " + bd.getEncoded("hex_lower"));
        }

        oid = "1.2.250.1.71.1.2.3";
        try {
            cert.getExtensionBd(oid, bd);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println(oid + ": " + bd.getEncoded("hex_lower"));
        }

        oid = "1.2.250.1.71.1.2.7";
        try {
            cert.getExtensionBd(oid, bd);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println(oid + ": " + bd.getEncoded("hex_lower"));
        }

        oid = "1.2.250.1.71.4.2.5";
        try {
            cert.getExtensionBd(oid, bd);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (success) {
            System.out.println(oid + ": " + bd.getEncoded("hex_lower"));
        }

        // Sample output:
        // 1.2.250.1.71.1.2.5: 040180
        // 1.2.250.1.71.1.2.2: 020100
        // 1.2.250.1.71.1.2.3: 1315383032353030303030312f32393030303539313432
        // 1.2.250.1.71.1.2.7: 02010a
        // 1.2.250.1.71.4.2.5: 30060c04534d3236

        // The above binary values are actually ASN.1
        // You can get the ASN.1 decoed by calling GetExtensionAsXml to get it in XML format,
        // and then you extract the values from the XML.

        oid = "1.2.250.1.71.1.2.5";
        String strXml;
        try {
            strXml = cert.getExtensionAsXml(oid);
        } catch (ChilkatException e) {
            strXml = null;
        }
        if (strXml != null) {
            System.out.println(oid + ": " + strXml);
        }

        oid = "1.2.250.1.71.1.2.2";
        try {
            strXml = cert.getExtensionAsXml(oid);
        } catch (ChilkatException e) {
            strXml = null;
        }
        if (strXml != null) {
            System.out.println(oid + ": " + strXml);
        }

        oid = "1.2.250.1.71.1.2.3";
        try {
            strXml = cert.getExtensionAsXml(oid);
        } catch (ChilkatException e) {
            strXml = null;
        }
        if (strXml != null) {
            System.out.println(oid + ": " + strXml);
        }

        oid = "1.2.250.1.71.1.2.7";
        try {
            strXml = cert.getExtensionAsXml(oid);
        } catch (ChilkatException e) {
            strXml = null;
        }
        if (strXml != null) {
            System.out.println(oid + ": " + strXml);
        }

        oid = "1.2.250.1.71.4.2.5";
        try {
            strXml = cert.getExtensionAsXml(oid);
        } catch (ChilkatException e) {
            strXml = null;
        }
        if (strXml != null) {
            System.out.println(oid + ": " + strXml);
        }

        // Sample output:
        // 1.2.250.1.71.1.2.5: <octets>gA==</octets>
        // 1.2.250.1.71.1.2.2: <int>00</int>
        // 1.2.250.1.71.1.2.3: <printable>8025000001/2900059142</printable>
        // 1.2.250.1.71.1.2.7: <int>0A</int>
        // 1.2.250.1.71.4.2.5: <sequence><utf8>SM26</utf8></sequence>

        // "gA==" is the base64 encoded byte values
        // "0A" is hex for decimal 10
    }
}