Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Create JWK Set Containing CertificatesDemonstrates how to create a JWK Set containing N certificates.
integer li_rc oleobject loo_Cert1 integer li_Success oleobject loo_Cert2 oleobject loo_Crypt oleobject loo_Json string ls_HexThumbprint string ls_Base64Thumbprint oleobject loo_PubKey oleobject loo_PubKeyJwk // 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 // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 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 <> 1 then Write-Debug loo_Cert1.LastErrorText destroy loo_Cert1 return end if loo_Cert2 = create oleobject // Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 li_rc = loo_Cert2.ConnectToNewObject("Chilkat.Cert") li_Success = loo_Cert2.LoadFromFile("qa_data/certs/testCert.cer") if li_Success <> 1 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 // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 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 = loo_Cert1.ExportPublicKey() loo_PubKeyJwk = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_PubKeyJwk.ConnectToNewObject("Chilkat.JsonObject") loo_PubKeyJwk.Load(loo_PubKey.GetJwk()) destroy loo_PubKey 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_PubKey = loo_Cert2.ExportPublicKey() loo_PubKeyJwk.Load(loo_PubKey.GetJwk()) destroy loo_PubKey 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_PubKeyJwk |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.