Sample code for 30+ languages & platforms
Unicode C

Get Public Key from CSR

See more CSR Examples

Demonstrates how to get the public key from a CSR.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPemW.h>
#include <C_CkAsnW.h>
#include <C_CkXmlW.h>
#include <C_CkBinDataW.h>
#include <C_CkPublicKeyW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPemW pem;
    const wchar_t *noPassword;
    const wchar_t *strBase64;
    HCkAsnW asn;
    HCkXmlW xml;
    const wchar_t *strModulusHex;
    HCkBinDataW bd;
    const wchar_t *modulus64;
    HCkXmlW xmlPubKey;
    HCkPublicKeyW pubkey;
    BOOL preferPkcs1;

    success = FALSE;

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    pem = CkPemW_Create();

    // No password is required.  Pass an empty password string..
    noPassword = L"";
    success = CkPemW_LoadPemFile(pem,L"qa_data/csr/csr2.pem",noPassword);
    if (success != TRUE) {
        wprintf(L"%s\n",CkPemW_lastErrorText(pem));
        CkPemW_Dispose(pem);
        return;
    }

    strBase64 = CkPemW_getEncodedItem(pem,L"csr",L"",L"base64",0);

    asn = CkAsnW_Create();
    success = CkAsnW_LoadEncoded(asn,strBase64,L"base64");
    if (success != TRUE) {
        wprintf(L"%s\n",CkAsnW_lastErrorText(asn));
        CkPemW_Dispose(pem);
        CkAsnW_Dispose(asn);
        return;
    }

    // Convert the ASN.1 to XML.
    xml = CkXmlW_Create();
    success = CkXmlW_LoadXml(xml,CkAsnW_asnToXml(asn));
    wprintf(L"%s\n",CkXmlW_getXml(xml));
    wprintf(L"----\n");

    strModulusHex = CkXmlW_getChildContent(xml,L"bits");
    wprintf(L"strModulusHex = %s\n",strModulusHex);
    wprintf(L"----\n");

    // We need the modulus as base64.
    bd = CkBinDataW_Create();
    CkBinDataW_AppendEncoded(bd,strModulusHex,L"hex");
    modulus64 = CkBinDataW_getEncoded(bd,L"base64");
    wprintf(L"modulus64 = %s\n",modulus64);
    wprintf(L"----\n");

    // Build the XML for the public key.
    xmlPubKey = CkXmlW_Create();
    CkXmlW_putTag(xmlPubKey,L"RSAPublicKey");
    CkXmlW_UpdateChildContent(xmlPubKey,L"Modulus",modulus64);
    // The RSA exponent will always be decimal 65537 (base64 = AQAB)
    CkXmlW_UpdateChildContent(xmlPubKey,L"Exponent",L"AQAB");

    wprintf(L"RSA public key as XML:\n");
    wprintf(L"%s\n",CkXmlW_getXml(xmlPubKey));
    wprintf(L"----\n");

    // Load the XML into a Chilkat public key object.
    pubkey = CkPublicKeyW_Create();
    success = CkPublicKeyW_LoadFromString(pubkey,CkXmlW_getXml(xmlPubKey));
    if (success != TRUE) {
        wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubkey));
        CkPemW_Dispose(pem);
        CkAsnW_Dispose(asn);
        CkXmlW_Dispose(xml);
        CkBinDataW_Dispose(bd);
        CkXmlW_Dispose(xmlPubKey);
        CkPublicKeyW_Dispose(pubkey);
        return;
    }

    // Show the public key as PEM.
    preferPkcs1 = TRUE;
    wprintf(L"%s\n",CkPublicKeyW_getPem(pubkey,preferPkcs1));


    CkPemW_Dispose(pem);
    CkAsnW_Dispose(asn);
    CkXmlW_Dispose(xml);
    CkBinDataW_Dispose(bd);
    CkXmlW_Dispose(xmlPubKey);
    CkPublicKeyW_Dispose(pubkey);

    }