Sample code for 30+ languages & platforms
PowerBuilder

Create JWK Set Containing Certificates

See more Certificates Examples

Demonstrates how to create a JWK Set containing N certificates.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Cert1
oleobject loo_Cert2
oleobject loo_Crypt
oleobject loo_Json
string ls_HexThumbprint
string ls_Base64Thumbprint
oleobject loo_PubKey
oleobject loo_PubKeyJwk

li_Success = 0

// This example creates the following JWK Set from two certificates:

// {
//   "keys": [
//     {
//       "kty": "RSA",
//       "use": "sig",
//       "kid": "BB8CeFVqyaGrGNuehJIiL4dfjzw",
//       "x5t": "BB8CeFVqyaGrGNuehJIiL4dfjzw",
//       "n": "nYf1jpn7cFdQ...9Iw",
//       "e": "AQAB",
//       "x5c": [
//         "MIIDBTCCAe2...Z+NTZo"
//       ]
//     },
//     {
//       "kty": "RSA",
//       "use": "sig",
//       "kid": "M6pX7RHoraLsprfJeRCjSxuURhc",
//       "x5t": "M6pX7RHoraLsprfJeRCjSxuURhc",
//       "n": "xHScZMPo8F...EO4QQ",
//       "e": "AQAB",
//       "x5c": [
//         "MIIC8TCCAdmgA...Vt5432GA=="
//       ]
//     }
//   ]
// }

// First get two certificates from files.
loo_Cert1 = create oleobject
li_rc = loo_Cert1.ConnectToNewObject("Chilkat.Cert")
if li_rc < 0 then
    destroy loo_Cert1
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Cert1.LoadFromFile("qa_data/certs/brasil_cert.pem")
if li_Success = 0 then
    Write-Debug loo_Cert1.LastErrorText
    destroy loo_Cert1
    return
end if

loo_Cert2 = create oleobject
li_rc = loo_Cert2.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert2.LoadFromFile("qa_data/certs/testCert.cer")
if li_Success = 0 then
    Write-Debug loo_Cert2.LastErrorText
    destroy loo_Cert1
    destroy loo_Cert2
    return
end if

// We'll need this crypt object re-encode the SHA1 thumbprint from hex to base64.
loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

// Let's begin with the 1st cert:
loo_Json.I = 0
loo_Json.UpdateString("keys[i].kty","RSA")
loo_Json.UpdateString("keys[i].use","sig")

ls_HexThumbprint = loo_Cert1.Sha1Thumbprint
ls_Base64Thumbprint = loo_Crypt.ReEncode(ls_HexThumbprint,"hex","base64")
loo_Json.UpdateString("keys[i].kid",ls_Base64Thumbprint)
loo_Json.UpdateString("keys[i].x5t",ls_Base64Thumbprint)

// (We're assuming these are RSA certificates)
// To get the modulus (n) and exponent (e), we need to get the cert's public key and then get its JWK.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

loo_Cert1.GetPublicKey(loo_PubKey)

loo_PubKeyJwk = create oleobject
li_rc = loo_PubKeyJwk.ConnectToNewObject("Chilkat.JsonObject")

loo_PubKeyJwk.Load(loo_PubKey.GetJwk())
loo_Json.UpdateString("keys[i].n",loo_PubKeyJwk.StringOf("n"))
loo_Json.UpdateString("keys[i].e",loo_PubKeyJwk.StringOf("e"))

// Now add the entire X.509 certificate 
loo_Json.UpdateString("keys[i].x5c[0]",loo_Cert1.GetEncoded())

// Now do the same for cert2..
loo_Json.I = 1

loo_Json.UpdateString("keys[i].kty","RSA")
loo_Json.UpdateString("keys[i].use","sig")

ls_HexThumbprint = loo_Cert2.Sha1Thumbprint
ls_Base64Thumbprint = loo_Crypt.ReEncode(ls_HexThumbprint,"hex","base64")
loo_Json.UpdateString("keys[i].kid",ls_Base64Thumbprint)
loo_Json.UpdateString("keys[i].x5t",ls_Base64Thumbprint)
loo_Cert2.GetPublicKey(loo_PubKey)

loo_PubKeyJwk.Load(loo_PubKey.GetJwk())
loo_Json.UpdateString("keys[i].n",loo_PubKeyJwk.StringOf("n"))
loo_Json.UpdateString("keys[i].e",loo_PubKeyJwk.StringOf("e"))

// Now add the entire X.509 certificate 
loo_Json.UpdateString("keys[i].x5c[0]",loo_Cert2.GetEncoded())

// Emit the JSON..
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()


destroy loo_Cert1
destroy loo_Cert2
destroy loo_Crypt
destroy loo_Json
destroy loo_PubKey
destroy loo_PubKeyJwk