Sample code for 30+ languages & platforms
PureBasic

AWS KMS Import PFX Key

See more AWS KMS Examples

Imports a certificate's private key from a .pfx file to new key created in AWS KMS.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Note: This example is using a relative file path.  You can also specify a full file path, such as "C:/someDir/myCertAndKey.pfx"
    ; or a file path the makes sense on non-Windows operating systems..
    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckLoadPfxFile(cert,"qa_data/pfx/myCertAndKey.pfx","pfx_password")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Get the cert's private key.  This is what will be uploaded to AWS KMS.
    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkCert::ckGetPrivateKey(cert,privKey)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

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

    CkJsonObject::ckUpdateString(json,"service","aws-kms")
    CkJsonObject::ckUpdateString(json,"auth.access_key","AWS_ACCESS_KEY")
    CkJsonObject::ckUpdateString(json,"auth.secret_key","AWS_SECRET_KEY")
    CkJsonObject::ckUpdateString(json,"auth.region","us-west-2")
    CkJsonObject::ckUpdateString(json,"description","Test of uploading existing private key to AWS KMS")

    ; Let's add some information about the certificate this key belongs to.
    ; This is for informational purposes only, so that we can examine the tags
    ; in the AWS KMS console and know the corresponding certificate.
    CkJsonObject::ckUpdateString(json,"tags[0].key","CertSerial")
    CkJsonObject::ckUpdateString(json,"tags[0].value",CkCert::ckSerialNumber(cert))
    CkJsonObject::ckUpdateString(json,"tags[1].key","CertIssuer")
    CkJsonObject::ckUpdateString(json,"tags[1].value",CkCert::ckIssuerCN(cert))
    CkJsonObject::ckUpdateString(json,"tags[2].key","CertSubject")
    CkJsonObject::ckUpdateString(json,"tags[2].value",CkCert::ckSubjectCN(cert))

    CkJsonObject::ckUpdateString(json,"keyUsage","SIGN_VERIFY")

    ; The UploadToCloud method was added in Chilkat v9.5.0.96
    jsonOut.i = CkJsonObject::ckCreate()
    If jsonOut.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckUploadToCloud(privKey,json,jsonOut)
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        CkJsonObject::ckDispose(json)
        CkJsonObject::ckDispose(jsonOut)
        ProcedureReturn
    EndIf

    ; When successful, the jsonOut contains information about the private key in AWS KMS.
    ; Most importantly, the KeyId.
    CkJsonObject::setCkEmitCompact(jsonOut, 0)
    Debug CkJsonObject::ckEmit(jsonOut)

    ; Sample JSON result:

    ; {
    ;   "AWSAccountId": "954491834127",
    ;   "Arn": "arn:aws:kms:us-west-2:954491834127:key/187012e8-008f-4fc7-b100-5efe6146dff2",
    ;   "KeySpec": "RSA_4096",
    ;   "Description": "Test of uploading existing private key to AWS KMS",
    ;   "KeyId": "187012e8-008f-4fc7-b100-5efe6146dff2",
    ;   "KeyUsage": "SIGN_VERIFY"
    ; }


    CkCert::ckDispose(cert)
    CkPrivateKey::ckDispose(privKey)
    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jsonOut)


    ProcedureReturn
EndProcedure