Chilkat Online Tools

IssueCertificate PureBasic Example

AWS Private CA

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

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

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

    success.i

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

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(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::setCkRegion(authAws, "us-west-2")
    CkAuthAws::setCkServiceName(authAws, "acm-pca")
    ; SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRest::ckSetAuthAws(rest,authAws)

    ; URL: https://acm-pca.us-west-2.amazonaws.com/
    ; Use the same region as specified above.
    success = CkRest::ckConnect(rest,"acm-pca.us-west-2.amazonaws.com",443,1,1)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; 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.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].CertPolicyId","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].PolicyQualifierId","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.CertificatePolicies[0].PolicyQualifiers[0].Qualifier.CpsUri","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageObjectIdentifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.ExtendedKeyUsage[0].ExtendedKeyUsageType","string")
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.CRLSign",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DataEncipherment",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DecipherOnly",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.DigitalSignature",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.EncipherOnly",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyAgreement",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyCertSign",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.KeyEncipherment",123)
    CkJsonObject::ckUpdateInt(json,"ApiPassthrough.Extensions.KeyUsage.NonRepudiation",123)
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.CommonName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Country","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.DistinguishedNameQualifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GenerationQualifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.GivenName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Initials","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Locality","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Organization","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.OrganizationalUnit","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Pseudonym","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.SerialNumber","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.State","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Surname","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DirectoryName.Title","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].DnsName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.NameAssigner","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].EdiPartyName.PartyName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].IpAddress","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.TypeId","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].OtherName.Value","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].RegisteredId","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].Rfc822Name","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Extensions.SubjectAlternativeNames[0].UniformResourceIdentifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.CommonName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Country","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.DistinguishedNameQualifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.GenerationQualifier","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.GivenName","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Initials","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Locality","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Organization","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.OrganizationalUnit","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Pseudonym","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.SerialNumber","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.State","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Surname","string")
    CkJsonObject::ckUpdateString(json,"ApiPassthrough.Subject.Title","string")
    CkJsonObject::ckUpdateString(json,"CertificateAuthorityArn","string")
    CkJsonObject::ckUpdateInt(json,"Csr",123)
    CkJsonObject::ckUpdateString(json,"IdempotencyToken","string")
    CkJsonObject::ckUpdateString(json,"SigningAlgorithm","string")
    CkJsonObject::ckUpdateString(json,"TemplateArn","string")
    CkJsonObject::ckUpdateString(json,"Validity.Type","string")
    CkJsonObject::ckUpdateInt(json,"Validity.Value",123)
    CkJsonObject::ckUpdateString(json,"ValidityNotBefore.Type","string")
    CkJsonObject::ckUpdateInt(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::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.1")
    CkRest::ckAddHeader(rest,"X-Amz-Target","ACMPrivateCA.IssueCertificate")

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

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    Debug "response status code = " + Str(respStatusCode)
    If respStatusCode <> 200
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckLoadSb(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

    CertificateArn.s = CkJsonObject::ckStringOf(jResp,"CertificateArn")

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

    ; {
    ;   "CertificateArn": "string"
    ; }


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure