Chilkat Online Tools

IssueCertificate C Example

AWS Private CA

#include <C_CkRest.h>
#include <C_CkAuthAws.h>
#include <C_CkJsonObject.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkRest rest;
    BOOL success;
    HCkAuthAws authAws;
    HCkJsonObject json;
    HCkStringBuilder sbRequestBody;
    HCkStringBuilder sbResponseBody;
    int respStatusCode;
    HCkJsonObject jResp;
    const char *CertificateArn;

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

    rest = CkRest_Create();

    authAws = CkAuthAws_Create();
    CkAuthAws_putAccessKey(authAws,"AWS_ACCESS_KEY");
    CkAuthAws_putSecretKey(authAws,"AWS_SECRET_KEY");

    // Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
    CkAuthAws_putRegion(authAws,"us-west-2");
    CkAuthAws_putServiceName(authAws,"acm-pca");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRest_SetAuthAws(rest,authAws);

    // URL: https://acm-pca.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = CkRest_Connect(rest,"acm-pca.us-west-2.amazonaws.com",443,TRUE,TRUE);
    if (success != TRUE) {
        printf("ConnectFailReason: %d\n",CkRest_getConnectFailReason(rest));
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        return;
    }

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].CertPolicyId","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].PolicyQualifierId","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].Qualifier.CpsUri","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageObjectIdentifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageType","string");
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.CRLSign",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DataEncipherment",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DecipherOnly",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DigitalSignature",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.EncipherOnly",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyAgreement",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyCertSign",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyEncipherment",123);
    CkJsonObject_UpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.NonRepudiation",123);
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.CommonName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Country","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.DistinguishedNameQualifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GenerationQualifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GivenName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Initials","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Locality","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Organization","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.OrganizationalUnit","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Pseudonym","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.SerialNumber","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.State","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Surname","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Title","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DnsName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.NameAssigner","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.PartyName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].IpAddress","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.TypeId","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.Value","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].RegisteredId","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].Rfc822Name","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].UniformResourceIdentifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.CommonName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Country","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.DistinguishedNameQualifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.GenerationQualifier","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.GivenName","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Initials","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Locality","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Organization","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.OrganizationalUnit","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Pseudonym","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.SerialNumber","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.State","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Surname","string");
    CkJsonObject_UpdateString(json,"ApiPassthrough.Subject.Title","string");
    CkJsonObject_UpdateString(json,"CertificateAuthorityArn","string");
    CkJsonObject_UpdateInt(json,"Csr",123);
    CkJsonObject_UpdateString(json,"IdempotencyToken","string");
    CkJsonObject_UpdateString(json,"SigningAlgorithm","string");
    CkJsonObject_UpdateString(json,"TemplateArn","string");
    CkJsonObject_UpdateString(json,"Validity.Type","string");
    CkJsonObject_UpdateInt(json,"Validity.Value",123);
    CkJsonObject_UpdateString(json,"ValidityNotBefore.Type","string");
    CkJsonObject_UpdateInt(json,"ValidityNotBefore.Value",123);

    // The JSON request body created by the above code:

    // {
    //   "ApiPassthrough": {
    //     "Extensions": {
    //       "CertificatePolicies": [
    //         {
    //           "CertPolicyId": "string",
    //           "PolicyQualifiers": [
    //             {
    //               "PolicyQualifierId": "string",
    //               "Qualifier": {
    //                 "CpsUri": "string"
    //               }
    //             }
    //           ]
    //         }
    //       ],
    //       "ExtendedKeyUsage": [
    //         {
    //           "ExtendedKeyUsageObjectIdentifier": "string",
    //           "ExtendedKeyUsageType": "string"
    //         }
    //       ],
    //       "KeyUsage": {
    //         "CRLSign": boolean,
    //         "DataEncipherment": boolean,
    //         "DecipherOnly": boolean,
    //         "DigitalSignature": boolean,
    //         "EncipherOnly": boolean,
    //         "KeyAgreement": boolean,
    //         "KeyCertSign": boolean,
    //         "KeyEncipherment": boolean,
    //         "NonRepudiation": boolean
    //       },
    //       "SubjectAlternativeNames": [
    //         {
    //           "DirectoryName": {
    //             "CommonName": "string",
    //             "Country": "string",
    //             "DistinguishedNameQualifier": "string",
    //             "GenerationQualifier": "string",
    //             "GivenName": "string",
    //             "Initials": "string",
    //             "Locality": "string",
    //             "Organization": "string",
    //             "OrganizationalUnit": "string",
    //             "Pseudonym": "string",
    //             "SerialNumber": "string",
    //             "State": "string",
    //             "Surname": "string",
    //             "Title": "string"
    //           },
    //           "DnsName": "string",
    //           "EdiPartyName": {
    //             "NameAssigner": "string",
    //             "PartyName": "string"
    //           },
    //           "IpAddress": "string",
    //           "OtherName": {
    //             "TypeId": "string",
    //             "Value": "string"
    //           },
    //           "RegisteredId": "string",
    //           "Rfc822Name": "string",
    //           "UniformResourceIdentifier": "string"
    //         }
    //       ]
    //     },
    //     "Subject": {
    //       "CommonName": "string",
    //       "Country": "string",
    //       "DistinguishedNameQualifier": "string",
    //       "GenerationQualifier": "string",
    //       "GivenName": "string",
    //       "Initials": "string",
    //       "Locality": "string",
    //       "Organization": "string",
    //       "OrganizationalUnit": "string",
    //       "Pseudonym": "string",
    //       "SerialNumber": "string",
    //       "State": "string",
    //       "Surname": "string",
    //       "Title": "string"
    //     }
    //   },
    //   "CertificateAuthorityArn": "string",
    //   "Csr": blob,
    //   "IdempotencyToken": "string",
    //   "SigningAlgorithm": "string",
    //   "TemplateArn": "string",
    //   "Validity": {
    //     "Type": "string",
    //     "Value": number
    //   },
    //   "ValidityNotBefore": {
    //     "Type": "string",
    //     "Value": number
    //   }
    // }

    CkRest_AddHeader(rest,"Content-Type","application/x-amz-json-1.1");
    CkRest_AddHeader(rest,"X-Amz-Target","ACMPrivateCA.IssueCertificate");

    sbRequestBody = CkStringBuilder_Create();
    CkJsonObject_EmitSb(json,sbRequestBody);
    sbResponseBody = CkStringBuilder_Create();
    success = CkRest_FullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody);
    if (success != TRUE) {
        printf("%s\n",CkRest_lastErrorText(rest));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRest_getResponseStatusCode(rest);
    printf("response status code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        printf("Response Header:\n");
        printf("%s\n",CkRest_responseHeader(rest));
        printf("Response Body:\n");
        printf("%s\n",CkStringBuilder_getAsString(sbResponseBody));
        CkRest_Dispose(rest);
        CkAuthAws_Dispose(authAws);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbRequestBody);
        CkStringBuilder_Dispose(sbResponseBody);
        return;
    }

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);

    // The following code parses the JSON response.
    // A sample JSON response is shown below the sample code.

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    CertificateArn = CkJsonObject_stringOf(jResp,"CertificateArn");

    // A sample JSON response body parsed by the above code:

    // {
    //   "CertificateArn": "string"
    // }


    CkRest_Dispose(rest);
    CkAuthAws_Dispose(authAws);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbRequestBody);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }