Sample code for 30+ languages & platforms
Tcl

Validate JWS with Multiple Signatures using the General JSON Serialization Format

See more JSON Web Signatures (JWS) Examples

Validates 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             |                    |
   +--------------+-------------------------------+--------------------+

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

# First, prepare the public keys that will be needed for each signature.

# ---------------------------------------------------
# Use the following RSA key loaded from JWK format.
set sbRsaJwk [new_CkStringBuilder]

CkStringBuilder_Append $sbRsaJwk "{\"kty\":\"RSA\","
CkStringBuilder_Append $sbRsaJwk "\"n\":\"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx"
CkStringBuilder_Append $sbRsaJwk "HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs"
CkStringBuilder_Append $sbRsaJwk "D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH"
CkStringBuilder_Append $sbRsaJwk "SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV"
CkStringBuilder_Append $sbRsaJwk "MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8"
CkStringBuilder_Append $sbRsaJwk "NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ\","
CkStringBuilder_Append $sbRsaJwk "\"e\":\"AQAB\""
CkStringBuilder_Append $sbRsaJwk "}"

set rsaKey [new_CkPublicKey]

set success [CkPublicKey_LoadFromString $rsaKey [CkStringBuilder_getAsString $sbRsaJwk]]
if {$success == 0} then {
    puts [CkPublicKey_lastErrorText $rsaKey]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPublicKey $rsaKey
    exit
}

# ---------------------------------------------------
# Use the following ECC public key loaded from JWK format.
set sbEccJwk [new_CkStringBuilder]

CkStringBuilder_Append $sbEccJwk "{\"kty\":\"EC\","
CkStringBuilder_Append $sbEccJwk "\"crv\":\"P-256\","
CkStringBuilder_Append $sbEccJwk "\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\","
CkStringBuilder_Append $sbEccJwk "\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\""
CkStringBuilder_Append $sbEccJwk "}"

set eccKey [new_CkPublicKey]

set success [CkPublicKey_LoadFromString $eccKey [CkStringBuilder_getAsString $sbEccJwk]]
if {$success == 0} then {
    puts [CkPublicKey_lastErrorText $eccKey]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPublicKey $rsaKey
    delete_CkStringBuilder $sbEccJwk
    delete_CkPublicKey $eccKey
    exit
}

# ---------------------------------------------------
# The HMAC key (in base64url format)
set hmacKey "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"
#     }
#   ]
# }

set sbJws [new_CkStringBuilder]

CkStringBuilder_Append $sbJws "{ "
CkStringBuilder_Append $sbJws "  \"payload\": \"SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu\","
CkStringBuilder_Append $sbJws "  \"signatures\": ["
CkStringBuilder_Append $sbJws "    { "
CkStringBuilder_Append $sbJws "      \"protected\": \"eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0\","
CkStringBuilder_Append $sbJws "      \"signature\": \"IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw\""
CkStringBuilder_Append $sbJws "    },"
CkStringBuilder_Append $sbJws "    { "
CkStringBuilder_Append $sbJws "      \"protected\": \"eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ\","
CkStringBuilder_Append $sbJws "      \"signature\": \"1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw\""
CkStringBuilder_Append $sbJws "    },"
CkStringBuilder_Append $sbJws "    { "
CkStringBuilder_Append $sbJws "      \"protected\": \"eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0\","
CkStringBuilder_Append $sbJws "      \"signature\": \"YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw\""
CkStringBuilder_Append $sbJws "    }"
CkStringBuilder_Append $sbJws "  ]"
CkStringBuilder_Append $sbJws "}"

set jws [new_CkJws]

set success [CkJws_LoadJwsSb $jws $sbJws]
if {$success == 0} then {
    puts [CkJws_lastErrorText $jws]
    delete_CkStringBuilder $sbRsaJwk
    delete_CkPublicKey $rsaKey
    delete_CkStringBuilder $sbEccJwk
    delete_CkPublicKey $eccKey
    delete_CkStringBuilder $sbJws
    delete_CkJws $jws
    exit
}

# The payload is easily accessible:
puts "Payload: [CkJws_getPayload $jws utf-8]"

set sbKeyId [new_CkStringBuilder]

set bCaseSensitive 0

set protHeader [new_CkJsonObject]

set numSignatures [CkJws_get_NumSignatures $jws]
set i 0
while {$i < $numSignatures} {
    set success [CkJws_GetProtectedH $jws $i $protHeader]
    if {$success == 0} then {
        puts [CkJws_lastErrorText $jws]
        delete_CkStringBuilder $sbRsaJwk
        delete_CkPublicKey $rsaKey
        delete_CkStringBuilder $sbEccJwk
        delete_CkPublicKey $eccKey
        delete_CkStringBuilder $sbJws
        delete_CkJws $jws
        delete_CkStringBuilder $sbKeyId
        delete_CkJsonObject $protHeader
        exit
    }

    puts "--------------------------"
    puts "$i: "

    # Get the protected header.
    CkJsonObject_put_EmitCompact $protHeader 0
    puts [CkJsonObject_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 GetUnprotectedH instead of GetProtectedH(i).

    CkStringBuilder_Clear $sbKeyId
    CkStringBuilder_Append $sbKeyId [CkJsonObject_stringOf $protHeader "kid"]

    # Set the key based on key ID.
    if {[CkStringBuilder_ContentsEqual $sbKeyId "myRsaKey" $bCaseSensitive] == 1} then {
        CkJws_SetPublicKey $jws $i $rsaKey
    }

    if {[CkStringBuilder_ContentsEqual $sbKeyId "myEcKey" $bCaseSensitive] == 1} then {
        CkJws_SetPublicKey $jws $i $eccKey
    }

    if {[CkStringBuilder_ContentsEqual $sbKeyId "myMacKey" $bCaseSensitive] == 1} then {
        CkJws_SetMacKey $jws $i $hmacKey "base64url"
    }

    # Validate this signature.
    set v [CkJws_Validate $jws $i]
    if {$v < 0} then {
        # Perhaps Chilkat was not unlocked or the trial expired..
        puts "Validate failed for some other reason."
        puts [CkJws_lastErrorText $jws]
    }     else {
        if {$v == 0} then {
            puts "Invalid signature.  The key was incorrect, the JWS was invalid, or both."
        }         else {
            puts "Signature validated."
        }

    }

    set i [expr $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.

delete_CkStringBuilder $sbRsaJwk
delete_CkPublicKey $rsaKey
delete_CkStringBuilder $sbEccJwk
delete_CkPublicKey $eccKey
delete_CkStringBuilder $sbJws
delete_CkJws $jws
delete_CkStringBuilder $sbKeyId
delete_CkJsonObject $protHeader