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) Validate JWS with Multiple Signatures using the General JSON Serialization FormatValidates and recovers data and headers from a JSON Web Signature (JWS) containing 3 signatures. Note: 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_CkPublicKeyW.h> #include <C_CkJwsW.h> #include <C_CkJsonObjectW.h> void ChilkatSample(void) { HCkStringBuilderW sbRsaJwk; HCkPublicKeyW rsaKey; BOOL success; HCkStringBuilderW sbEccJwk; HCkPublicKeyW eccKey; const wchar_t *hmacKey; HCkStringBuilderW sbJws; HCkJwsW jws; HCkJsonObjectW protHeader; HCkStringBuilderW sbKeyId; BOOL bCaseSensitive; int numSignatures; int i; int v; // 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. // First, prepare the public keys that will be needed for each signature. // --------------------------------------------------- // 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"}"); rsaKey = CkPublicKeyW_Create(); success = CkPublicKeyW_LoadFromString(rsaKey,CkStringBuilderW_getAsString(sbRsaJwk)); if (success != TRUE) { wprintf(L"%s\n",CkPublicKeyW_lastErrorText(rsaKey)); CkStringBuilderW_Dispose(sbRsaJwk); CkPublicKeyW_Dispose(rsaKey); return; } // --------------------------------------------------- // Use the following ECC public 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"}"); eccKey = CkPublicKeyW_Create(); success = CkPublicKeyW_LoadFromString(eccKey,CkStringBuilderW_getAsString(sbEccJwk)); if (success != TRUE) { wprintf(L"%s\n",CkPublicKeyW_lastErrorText(eccKey)); CkStringBuilderW_Dispose(sbRsaJwk); CkPublicKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPublicKeyW_Dispose(eccKey); return; } // --------------------------------------------------- // The HMAC key (in base64url format) hmacKey = L"AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"; // The code below will verify each of the signatures in this JWS: // { // "payload": "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu", // "signatures": [ // { // "protected": "eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0", // "signature": "IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw" // }, // { // "protected": "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ", // "signature": "1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw" // }, // { // "protected": "eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0", // "signature": "YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw" // } // ] // } sbJws = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbJws,L"{ "); CkStringBuilderW_Append(sbJws,L" \"payload\": \"SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu\","); CkStringBuilderW_Append(sbJws,L" \"signatures\": ["); CkStringBuilderW_Append(sbJws,L" { "); CkStringBuilderW_Append(sbJws,L" \"protected\": \"eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0\","); CkStringBuilderW_Append(sbJws,L" \"signature\": \"IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw\""); CkStringBuilderW_Append(sbJws,L" },"); CkStringBuilderW_Append(sbJws,L" { "); CkStringBuilderW_Append(sbJws,L" \"protected\": \"eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ\","); CkStringBuilderW_Append(sbJws,L" \"signature\": \"1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw\""); CkStringBuilderW_Append(sbJws,L" },"); CkStringBuilderW_Append(sbJws,L" { "); CkStringBuilderW_Append(sbJws,L" \"protected\": \"eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0\","); CkStringBuilderW_Append(sbJws,L" \"signature\": \"YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw\""); CkStringBuilderW_Append(sbJws,L" }"); CkStringBuilderW_Append(sbJws,L" ]"); CkStringBuilderW_Append(sbJws,L"}"); jws = CkJwsW_Create(); success = CkJwsW_LoadJwsSb(jws,sbJws); if (success != TRUE) { wprintf(L"%s\n",CkJwsW_lastErrorText(jws)); CkStringBuilderW_Dispose(sbRsaJwk); CkPublicKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPublicKeyW_Dispose(eccKey); CkStringBuilderW_Dispose(sbJws); CkJwsW_Dispose(jws); return; } // The payload is easily accessible: wprintf(L"Payload: %s\n",CkJwsW_getPayload(jws,L"utf-8")); sbKeyId = CkStringBuilderW_Create(); bCaseSensitive = FALSE; numSignatures = CkJwsW_getNumSignatures(jws); i = 0; while (i < numSignatures) { protHeader = CkJwsW_GetProtectedHeader(jws,i); wprintf(L"--------------------------\n"); wprintf(L"%d: \n",i); // Get the protected header. CkJsonObjectW_putEmitCompact(protHeader,FALSE); wprintf(L"%s\n",CkJsonObjectW_emit(protHeader)); // Get the key ID ("kid") member. // Note: In this example, the "kid" values are contained in the protected headers. // They could've just as easily been located in unprotected headers. In that case, // we would've called GetUnprotectedHeader(i) instead of GetProtectedHeader(i). CkStringBuilderW_Clear(sbKeyId); CkStringBuilderW_Append(sbKeyId,CkJsonObjectW_stringOf(protHeader,L"kid")); // Set the key based on key ID. if (CkStringBuilderW_ContentsEqual(sbKeyId,L"myRsaKey",bCaseSensitive) == TRUE) { CkJwsW_SetPublicKey(jws,i,rsaKey); } if (CkStringBuilderW_ContentsEqual(sbKeyId,L"myEcKey",bCaseSensitive) == TRUE) { CkJwsW_SetPublicKey(jws,i,eccKey); } if (CkStringBuilderW_ContentsEqual(sbKeyId,L"myMacKey",bCaseSensitive) == TRUE) { CkJwsW_SetMacKey(jws,i,hmacKey,L"base64url"); } // Validate this signature. v = CkJwsW_Validate(jws,i); if (v < 0) { // Perhaps Chilkat was not unlocked or the trial expired.. wprintf(L"Validate failed for some other reason.\n"); wprintf(L"%s\n",CkJwsW_lastErrorText(jws)); } else { if (v == 0) { wprintf(L"Invalid signature. The key was incorrect, the JWS was invalid, or both.\n"); } else { wprintf(L"Signature validated.\n"); } } CkJsonObjectW_Dispose(protHeader); i = i + 1; } // The output of this program is: // Payload: In our village, folks say God crumbles up the old moon into stars. // -------------------------- // 0: // { // "alg": "RS256", // "kid": "myRsaKey" // } // // Signature validated. // -------------------------- // 1: // { // "alg": "ES256", // "kid": "myEcKey" // } // // Signature validated. // -------------------------- // 2: // { // "alg": "HS256", // "kid": "myMacKey" // } // // Signature validated. CkStringBuilderW_Dispose(sbRsaJwk); CkPublicKeyW_Dispose(rsaKey); CkStringBuilderW_Dispose(sbEccJwk); CkPublicKeyW_Dispose(eccKey); CkStringBuilderW_Dispose(sbJws); CkJwsW_Dispose(jws); CkStringBuilderW_Dispose(sbKeyId); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.