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
(Unicode C) Convert PFX to JWK SetDemonstrates how to convert a PFX (.pfx or .p12) to a JWK Set (a JSON Web Key Set). This example requires Chilkat v9.5.0.66 or greater.
#include <C_CkPfxW.h> #include <C_CkJavaKeyStoreW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkPfxW pfx; BOOL success; const wchar_t *alias; const wchar_t *password; HCkJavaKeyStoreW jks; HCkStringBuilderW sbJwkSet; HCkJsonObjectW jwkSet; // IMPORTANT: This example requires Chilkat v9.5.0.66 or greater. // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. pfx = CkPfxW_Create(); success = CkPfxW_LoadPfxFile(pfx,L"qa_data/pfx/myEccCert.p12",L"MY_ECC_PFX_PASSWORD"); if (success != TRUE) { wprintf(L"%s\n",CkPfxW_lastErrorText(pfx)); CkPfxW_Dispose(pfx); return; } // To get the contents of the PFX in JWK Set form, we must first convert // to a Java KeyStore object: // The alias will become the key id ("kid") the the JWK Set. // The password is an input argument that becomes the password for the JavaKeyStore. alias = L"my_ecc_key"; password = L"secret123"; jks = CkPfxW_ToJavaKeyStore(pfx,alias,password); if (CkPfxW_getLastMethodSuccess(pfx) != TRUE) { wprintf(L"%s\n",CkPfxW_lastErrorText(pfx)); CkPfxW_Dispose(pfx); return; } sbJwkSet = CkStringBuilderW_Create(); // The ToJwkSet method writes to sbJwkSet. success = CkJavaKeyStoreW_ToJwkSet(jks,password,sbJwkSet); if (success != TRUE) { wprintf(L"%s\n",CkJavaKeyStoreW_lastErrorText(jks)); CkJavaKeyStoreW_Dispose(jks); CkPfxW_Dispose(pfx); CkStringBuilderW_Dispose(sbJwkSet); return; } CkJavaKeyStoreW_Dispose(jks); jwkSet = CkJsonObjectW_Create(); CkJsonObjectW_LoadSb(jwkSet,sbJwkSet); CkJsonObjectW_putEmitCompact(jwkSet,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(jwkSet)); // The output is: // (Line breaks in the "x5c" base64 data were added for readability) // { // "keys": [ // { // "kty": "EC", // "crv": "P-384", // "x": "uB62JGMJKXnp1PNsOOIYKGhrzfLev3O-UuthL6UzEqNNDTd8dEYMUQP_DTS3qk98", // "y": "gyQEFxdmZUsPF0fFokkZZ1cV6z7QD3MsPvSjrBzB0GUB3r8CLnDK_F4pF2Q995hr", // "d": "ugTy2ZpuQqv1uQyLSgz1SPRvngzrd5vLyzU2ICaJd25zZRIxxlNR-uFo9UWC6llt", // "kid": "my_ecc_key", // "x5c": [ // "MIICzjCCAlOgAwIBAgIETULS8zAKBggqhkjOPQQDAzBtMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNRW50 // cnVzdCwgSW5jLjEfMB0GA1UECxMWRm9yIFRlc3QgUHVycG9zZXMgT25seTElMCMGA1UEAxMcRW50cnVzdCBFQ0MgR // GVtb25zdHJhdGlvbiBDQTAeFw0xNjAzMTgxMjM5MzFaFw0xNjA1MTcxMzA5MzFaMFgxHzAdBgNVBAsTFkZvciBUZX // N0IFB1cnBvc2VzIE9ubHkxHTAbBgNVBAsTFFBlcnNvbmEgTm90IFZlcmlmaWVkMRYwFAYDVQQDEw1DaGlsa2F0IEF // kbWluMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEuB62JGMJKXnp1PNsOOIYKGhrzfLev3O+UuthL6UzEqNNDTd8dEYM // UQP/DTS3qk98gyQEFxdmZUsPF0fFokkZZ1cV6z7QD3MsPvSjrBzB0GUB3r8CLnDK/F4pF2Q995hro4HYMIHVMA4GA // 1UdDwEB/wQEAwIDiDAdBgNVHSUEFjAUBggrBgEFBQcDAgYIKwYBBQU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ifpw5pMy6jAdBgNVHQ4EFgQUJAVL4WSCGvgJzPt4eSH // 6cOaTMuowDAYDVR0TBAUwAwEB/zAKBggqhkjOPQQDAwNpADBmAjEAyAhA70OIb6lbfN6kOTQloHyCLmihNK+PT5wO // uaMR//KSQP2c/H18YhDYnlwMxF9fAjEA0URaJOXMF0qwdvS2rm7N5PWMLc/4BbeOZyZ94XJiG5u96iTgp6N9JI0MM // mCQE87N" // ] // } // ] // } // CkPfxW_Dispose(pfx); CkStringBuilderW_Dispose(sbJwkSet); CkJsonObjectW_Dispose(jwkSet); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.