Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(VB.NET UWP/WinRT) 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.
' 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. Dim sbRsaJwk As New Chilkat.StringBuilder sbRsaJwk.Append("{""kty"":""RSA"",") sbRsaJwk.Append("""n"":""ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx") sbRsaJwk.Append("HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs") sbRsaJwk.Append("D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH") sbRsaJwk.Append("SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV") sbRsaJwk.Append("MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8") sbRsaJwk.Append("NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ"",") sbRsaJwk.Append("""e"":""AQAB""") sbRsaJwk.Append("}") Dim rsaKey As New Chilkat.PublicKey Dim success As Boolean = rsaKey.LoadFromString(sbRsaJwk.GetAsString()) If (success <> True) Then Debug.WriteLine(rsaKey.LastErrorText) Exit Sub End If ' --------------------------------------------------- ' Use the following ECC public key loaded from JWK format. Dim sbEccJwk As New Chilkat.StringBuilder sbEccJwk.Append("{""kty"":""EC"",") sbEccJwk.Append("""crv"":""P-256"",") sbEccJwk.Append("""x"":""f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU"",") sbEccJwk.Append("""y"":""x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0""") sbEccJwk.Append("}") Dim eccKey As New Chilkat.PublicKey success = eccKey.LoadFromString(sbEccJwk.GetAsString()) If (success <> True) Then Debug.WriteLine(eccKey.LastErrorText) Exit Sub End If ' --------------------------------------------------- ' The HMAC key (in base64url format) Dim hmacKey As String = "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" ' } ' ] ' } Dim sbJws As New Chilkat.StringBuilder sbJws.Append("{ ") sbJws.Append(" ""payload"": ""SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu"",") sbJws.Append(" ""signatures"": [") sbJws.Append(" { ") sbJws.Append(" ""protected"": ""eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0"",") sbJws.Append(" ""signature"": ""IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw""") sbJws.Append(" },") sbJws.Append(" { ") sbJws.Append(" ""protected"": ""eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ"",") sbJws.Append(" ""signature"": ""1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw""") sbJws.Append(" },") sbJws.Append(" { ") sbJws.Append(" ""protected"": ""eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0"",") sbJws.Append(" ""signature"": ""YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw""") sbJws.Append(" }") sbJws.Append(" ]") sbJws.Append("}") Dim jws As New Chilkat.Jws success = jws.LoadJwsSb(sbJws) If (success <> True) Then Debug.WriteLine(jws.LastErrorText) Exit Sub End If ' The payload is easily accessible: Debug.WriteLine("Payload: " & jws.GetPayload("utf-8")) Dim protHeader As Chilkat.JsonObject Dim sbKeyId As New Chilkat.StringBuilder Dim bCaseSensitive As Boolean = False Dim numSignatures As Integer = jws.NumSignatures Dim i As Integer = 0 While i < numSignatures protHeader = jws.GetProtectedHeader(i) Debug.WriteLine("--------------------------") Debug.WriteLine(i & ": ") ' Get the protected header. protHeader.EmitCompact = False Debug.WriteLine(protHeader.Emit()) ' 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). sbKeyId.Clear() sbKeyId.Append(protHeader.StringOf("kid")) ' Set the key based on key ID. If (sbKeyId.ContentsEqual("myRsaKey",bCaseSensitive) = True) Then jws.SetPublicKey(i,rsaKey) End If If (sbKeyId.ContentsEqual("myEcKey",bCaseSensitive) = True) Then jws.SetPublicKey(i,eccKey) End If If (sbKeyId.ContentsEqual("myMacKey",bCaseSensitive) = True) Then jws.SetMacKey(i,hmacKey,"base64url") End If ' Validate this signature. Dim v As Integer = jws.Validate(i) If (v < 0) Then ' Perhaps Chilkat was not unlocked or the trial expired.. Debug.WriteLine("Validate failed for some other reason.") Debug.WriteLine(jws.LastErrorText) Else If (v = 0) Then Debug.WriteLine("Invalid signature. The key was incorrect, the JWS was invalid, or both.") Else Debug.WriteLine("Signature validated.") End If End If i = i + 1 End While ' 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. |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.