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) Create JWS with Multiple Signatures using the General JSON Serialization FormatCreates a JSON Web Signature (JWS) containing 3 signatures and output using the general JSON serialization format. Chilkat supports all of the following JWS algorithms: +--------------+-------------------------------+--------------------+ | "alg" Param | Digital Signature or MAC | Implementation | | Value | Algorithm | Requirements | +--------------+-------------------------------+--------------------+ | HS256 | HMAC using SHA-256 | Required | | HS384 | HMAC using SHA-384 | Optional | | HS512 | HMAC using SHA-512 | Optional | | RS256 | RSASSA-PKCS1-v1_5 using | Recommended | | | SHA-256 | | | RS384 | RSASSA-PKCS1-v1_5 using | Optional | | | SHA-384 | | | RS512 | RSASSA-PKCS1-v1_5 using | Optional | | | SHA-512 | | | ES256 | ECDSA using P-256 and SHA-256 | Recommended+ | | ES384 | ECDSA using P-384 and SHA-384 | Optional | | ES512 | ECDSA using P-521 and SHA-512 | Optional | | PS256 | RSASSA-PSS using SHA-256 and | Optional | | | MGF1 with SHA-256 | | | PS384 | RSASSA-PSS using SHA-384 and | Optional | | | MGF1 with SHA-384 | | | PS512 | RSASSA-PSS using SHA-512 and | Optional | | | MGF1 with SHA-512 | | +--------------+-------------------------------+--------------------+ Note: This example requires Chilkat v9.5.0.66 or greater.
#include <C_CkStringBuilderW.h> #include <C_CkPrivateKeyW.h> #include <C_CkJsonObjectW.h> #include <C_CkJwsW.h> void ChilkatSample(void) { HCkStringBuilderW sbRsaJwk; HCkPrivateKeyW rsaKey; BOOL success; HCkStringBuilderW sbEccJwk; HCkPrivateKeyW eccKey; const wchar_t *hmacKey; HCkJsonObjectW jwsProtHdr0; HCkJsonObjectW jwsUnprotHdr0; HCkJsonObjectW jwsProtHdr1; HCkJsonObjectW jwsUnprotHdr1; HCkJsonObjectW jwsProtHdr2; HCkJsonObjectW jwsUnprotHdr2; HCkJwsW jws; BOOL bIncludeBom; const wchar_t *payloadStr; const wchar_t *jwsStr; HCkJsonObjectW json; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Note: This example requires Chilkat v9.5.0.66 or greater. // The JWS to be created will contain three signatures. // The 1st will use an ECC key, the 2nd an RSA key, and the 3rd an HMAC key. // Prepare each key.. // --------------------------------------------------- // Use the following RSA key loaded from JWK format. sbRsaJwk = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbRsaJwk,L"{\"kty\":\"RSA\","); CkStringBuilderW_Append(sbRsaJwk,L"\"n\":\"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx"); CkStringBuilderW_Append(sbRsaJwk,L"HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs"); CkStringBuilderW_Append(sbRsaJwk,L"D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH"); CkStringBuilderW_Append(sbRsaJwk,L"SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV"); CkStringBuilderW_Append(sbRsaJwk,L"MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8"); CkStringBuilderW_Append(sbRsaJwk,L"NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ\","); CkStringBuilderW_Append(sbRsaJwk,L"\"e\":\"AQAB\","); CkStringBuilderW_Append(sbRsaJwk,L"\"d\":\"Eq5xpGnNCivDflJsRQBXHx1hdR1k6Ulwe2JZD50LpXyWPEAeP88vLNO97I"); CkStringBuilderW_Append(sbRsaJwk,L"jlA7_GQ5sLKMgvfTeXZx9SE-7YwVol2NXOoAJe46sui395IW_GO-pWJ1O0"); CkStringBuilderW_Append(sbRsaJwk,L"BkTGoVEn2bKVRUCgu-GjBVaYLU6f3l9kJfFNS3E0QbVdxzubSu3Mkqzjkn"); CkStringBuilderW_Append(sbRsaJwk,L"439X0M_V51gfpRLI9JYanrC4D4qAdGcopV_0ZHHzQlBjudU2QvXt4ehNYT"); CkStringBuilderW_Append(sbRsaJwk,L"CBr6XCLQUShb1juUO1ZdiYoFaFQT5Tw8bGUl_x_jTj3ccPDVZFD9pIuhLh"); CkStringBuilderW_Append(sbRsaJwk,L"BOneufuBiB4cS98l2SR_RQyGWSeWjnczT0QU91p1DhOVRuOopznQ\","); CkStringBuilderW_Append(sbRsaJwk,L"\"p\":\"4BzEEOtIpmVdVEZNCqS7baC4crd0pqnRH_5IB3jw3bcxGn6QLvnEtfdUdi"); CkStringBuilderW_Append(sbRsaJwk,L"YrqBdss1l58BQ3KhooKeQTa9AB0Hw_Py5PJdTJNPY8cQn7ouZ2KKDcmnPG"); CkStringBuilderW_Append(sbRsaJwk,L"BY5t7yLc1QlQ5xHdwW1VhvKn-nXqhJTBgIPgtldC-KDV5z-y2XDwGUc\","); CkStringBuilderW_Append(sbRsaJwk,L"\"q\":\"uQPEfgmVtjL0Uyyx88GZFF1fOunH3-7cepKmtH4pxhtCoHqpWmT8YAmZxa"); CkStringBuilderW_Append(sbRsaJwk,L"ewHgHAjLYsp1ZSe7zFYHj7C6ul7TjeLQeZD_YwD66t62wDmpe_HlB-TnBA"); CkStringBuilderW_Append(sbRsaJwk,L"-njbglfIsRLtXlnDzQkv5dTltRJ11BKBBypeeF6689rjcJIDEz9RWdc\","); CkStringBuilderW_Append(sbRsaJwk,L"\"dp\":\"BwKfV3Akq5_MFZDFZCnW-wzl-CCo83WoZvnLQwCTeDv8uzluRSnm71I3Q"); CkStringBuilderW_Append(sbRsaJwk,L"CLdhrqE2e9YkxvuxdBfpT_PI7Yz-FOKnu1R6HsJeDCjn12Sk3vmAktV2zb"); CkStringBuilderW_Append(sbRsaJwk,L"34MCdy7cpdTh_YVr7tss2u6vneTwrA86rZtu5Mbr1C1XsmvkxHQAdYo0\","); CkStringBuilderW_Append(sbRsaJwk,L"\"dq\":\"h_96-mK1R_7glhsum81dZxjTnYynPbZpHziZjeeHcXYsXaaMwkOlODsWa"); CkStringBuilderW_Append(sbRsaJwk,L"7I9xXDoRwbKgB719rrmI2oKr6N3Do9U0ajaHF-NKJnwgjMd2w9cjz3_-ky"); CkStringBuilderW_Append(sbRsaJwk,L"NlxAr2v4IKhGNpmM5iIgOS1VZnOZ68m6_pbLBSp3nssTdlqvd0tIiTHU\","); CkStringBuilderW_Append(sbRsaJwk,L"\"qi\":\"IYd7DHOhrWvxkwPQsRM2tOgrjbcrfvtQJipd-DlcxyVuuM9sQLdgjVk2o"); CkStringBuilderW_Append(sbRsaJwk,L"y26F0EmpScGLq2MowX7fhd_QJQ3ydy5cY7YIBi87w93IKLEdfnbJtoOPLU"); CkStringBuilderW_Append(sbRsaJwk,L"W0ITrJReOgo1cq9SbsxYawBgfp_gh6A5603k2-ZQwVK0JKSHuLFkuQ3U\""); CkStringBuilderW_Append(sbRsaJwk,L"}"); rsaKey = CkPrivateKeyW_Create(); // Note: This example loads the RSA key from JWK format. Any format can be loaded // into the private key object. (See the online reference documentation..) success = CkPrivateKeyW_LoadJwk(rsaKey,CkStringBuilderW_getAsString(sbRsaJwk)); if (success != TRUE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(rsaKey)); CkStringBuilderW_Dispose(sbRsaJwk); CkPrivateKeyW_Dispose(rsaKey); return; } // --------------------------------------------------- // Use the following ECC key loaded from JWK format. sbEccJwk = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbEccJwk,L"{\"kty\":\"EC\","); CkStringBuilderW_Append(sbEccJwk,L"\"crv\":\"P-256\","); CkStringBuilderW_Append(sbEccJwk,L"\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\","); CkStringBuilderW_Append(sbEccJwk,L"\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\","); CkStringBuilderW_Append(sbEccJwk,L"\"d\":\"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI\""); CkStringBuilderW_Append(sbEccJwk,L"}"); eccKey = CkPrivateKeyW_Create(); // Note: This example loads the ECDSA key from JWK format. Any format can be loaded // into the private key object. (See the online reference documentation..) success = CkPrivateKeyW_LoadJwk(eccKey,CkStringBuilderW_getAsString(sbEccJwk)); if (success != TRUE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(eccKey)); CkStringBuilderW_Dispose(sbRsaJwk); CkPrivateKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPrivateKeyW_Dispose(eccKey); return; } // --------------------------------------------------- // The HMAC key (in base64url format) hmacKey = L"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"; // --------------------------------------------------- // Prepare the headers.. // RSASSA-PKCS1-v1_5 SHA-256 jwsProtHdr0 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsProtHdr0,L"alg",L"RS256"); jwsUnprotHdr0 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsUnprotHdr0,L"kid",L"myRsaKey"); // ECDSA using P-256 and SHA-256 jwsProtHdr1 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsProtHdr1,L"alg",L"ES256"); jwsUnprotHdr1 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsUnprotHdr1,L"kid",L"myEcKey"); // HMAC using SHA-256 jwsProtHdr2 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsProtHdr2,L"alg",L"HS256"); jwsUnprotHdr2 = CkJsonObjectW_Create(); CkJsonObjectW_AppendString(jwsUnprotHdr2,L"kid",L"myMacKey"); // --------------------------------------------------- // First set the JWS headers, keys, and payload, then we'll create the JWS... jws = CkJwsW_Create(); CkJwsW_SetProtectedHeader(jws,0,jwsProtHdr0); CkJwsW_SetUnprotectedHeader(jws,0,jwsUnprotHdr0); CkJwsW_SetProtectedHeader(jws,1,jwsProtHdr1); CkJwsW_SetUnprotectedHeader(jws,1,jwsUnprotHdr1); CkJwsW_SetProtectedHeader(jws,2,jwsProtHdr2); CkJwsW_SetUnprotectedHeader(jws,2,jwsUnprotHdr2); CkJwsW_SetPrivateKey(jws,0,rsaKey); CkJwsW_SetPrivateKey(jws,1,eccKey); CkJwsW_SetMacKey(jws,2,hmacKey,L"base64url"); bIncludeBom = FALSE; payloadStr = L"In our village, folks say God crumbles up the old moon into stars."; CkJwsW_SetPayload(jws,payloadStr,L"utf-8",bIncludeBom); // --------------------------------------------------- // Create the JWS. // Givent that multiple signatures will exist, only the general JSON serialization // format is possible, and that is what will be produced. jwsStr = CkJwsW_createJws(jws); if (CkJwsW_getLastMethodSuccess(jws) != TRUE) { wprintf(L"%s\n",CkJwsW_lastErrorText(jws)); CkStringBuilderW_Dispose(sbRsaJwk); CkPrivateKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPrivateKeyW_Dispose(eccKey); CkJsonObjectW_Dispose(jwsProtHdr0); CkJsonObjectW_Dispose(jwsUnprotHdr0); CkJsonObjectW_Dispose(jwsProtHdr1); CkJsonObjectW_Dispose(jwsUnprotHdr1); CkJsonObjectW_Dispose(jwsProtHdr2); CkJsonObjectW_Dispose(jwsUnprotHdr2); CkJwsW_Dispose(jws); return; } // The jwsStr is contains JSON in the smallest possible size, which is a single line. // To get in human-readable format, load into a Chilkat JSON object and emit.. json = CkJsonObjectW_Create(); CkJsonObjectW_Load(json,jwsStr); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(json)); // Sample output: // { // "payload": "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu", // "signatures": [ // { // "protected": "eyJhbGciOiJSUzI1NiJ9", // "header": { // "kid": "myRsaKey" // }, // "signature": "B04c24gSnpVm1Z-_bemfyNMCpZm6Knj1yB-yzaIOvijsWfDgoF_mSJccTIbzapNgwJudnobr5iDOfZWiRR9iqCyDJLe5M1S40vFF7MFEI3JecYRgrRc6n1lTkYLMRyVq48BwbQlmKgPqmK9drun3agklsr0FmgNx65pfmcnlYdXsgwxf8WbgppefrlrMImp-98-dNtBcUL8ce1aOjbcyVFjGMCzpm3JerQqIzWQvEwBstnMEQle73KHcyx_nsTmlzY70CaydbRTsciOATL7WfiMwuX1q9Y2NIpTg3CbOTWKdwjh7iyfiAKQxNBaF2mApnqj9hjpf8GwR-CfxAzJtPg" // }, // { // "protected": "eyJhbGciOiJFUzI1NiJ9", // "header": { // "kid": "myEcKey" // }, // "signature": "2cbugKq0ERaQMh01n2B-86EZFYleeMf8bsccaQMxzOxAg14PxfjR3IImvodTJYqkmfBJYW203etz2-7ZtJUOGw" // }, // { // "protected": "eyJhbGciOiJIUzI1NiJ9", // "header": { // "kid": "myMacKey" // }, // "signature": "e7R9gjx0RsUNa3c7qd8k9mQGEhtcG8vsN1W7jbLb2MA" // } // ] // } CkStringBuilderW_Dispose(sbRsaJwk); CkPrivateKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPrivateKeyW_Dispose(eccKey); CkJsonObjectW_Dispose(jwsProtHdr0); CkJsonObjectW_Dispose(jwsUnprotHdr0); CkJsonObjectW_Dispose(jwsProtHdr1); CkJsonObjectW_Dispose(jwsUnprotHdr1); CkJsonObjectW_Dispose(jwsProtHdr2); CkJsonObjectW_Dispose(jwsUnprotHdr2); CkJwsW_Dispose(jws); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.