Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

PureBasic Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(PureBasic) Validate JWS with Multiple Signatures using the General JSON Serialization Format

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

Note: This example requires Chilkat v9.5.0.66 or greater.

Chilkat PureBasic Module Download

Chilkat PureBasic Module

IncludeFile "CkJws.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPublicKey.pb"

Procedure ChilkatExample()

    ; 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.i = CkStringBuilder::ckCreate()
    If sbRsaJwk.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbRsaJwk,"{" + Chr(34) + "kty" + Chr(34) + ":" + Chr(34) + "RSA" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbRsaJwk,Chr(34) + "n" + Chr(34) + ":" + Chr(34) + "ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx")
    CkStringBuilder::ckAppend(sbRsaJwk,"HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs")
    CkStringBuilder::ckAppend(sbRsaJwk,"D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH")
    CkStringBuilder::ckAppend(sbRsaJwk,"SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV")
    CkStringBuilder::ckAppend(sbRsaJwk,"MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8")
    CkStringBuilder::ckAppend(sbRsaJwk,"NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbRsaJwk,Chr(34) + "e" + Chr(34) + ":" + Chr(34) + "AQAB" + Chr(34))
    CkStringBuilder::ckAppend(sbRsaJwk,"}")

    rsaKey.i = CkPublicKey::ckCreate()
    If rsaKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i = CkPublicKey::ckLoadFromString(rsaKey,CkStringBuilder::ckGetAsString(sbRsaJwk))
    If success <> 1
        Debug CkPublicKey::ckLastErrorText(rsaKey)
        CkStringBuilder::ckDispose(sbRsaJwk)
        CkPublicKey::ckDispose(rsaKey)
        ProcedureReturn
    EndIf

    ; ---------------------------------------------------
    ; Use the following ECC public key loaded from JWK format.
    sbEccJwk.i = CkStringBuilder::ckCreate()
    If sbEccJwk.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbEccJwk,"{" + Chr(34) + "kty" + Chr(34) + ":" + Chr(34) + "EC" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "crv" + Chr(34) + ":" + Chr(34) + "P-256" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "x" + Chr(34) + ":" + Chr(34) + "f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbEccJwk,Chr(34) + "y" + Chr(34) + ":" + Chr(34) + "x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0" + Chr(34))
    CkStringBuilder::ckAppend(sbEccJwk,"}")

    eccKey.i = CkPublicKey::ckCreate()
    If eccKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPublicKey::ckLoadFromString(eccKey,CkStringBuilder::ckGetAsString(sbEccJwk))
    If success <> 1
        Debug CkPublicKey::ckLastErrorText(eccKey)
        CkStringBuilder::ckDispose(sbRsaJwk)
        CkPublicKey::ckDispose(rsaKey)
        CkStringBuilder::ckDispose(sbEccJwk)
        CkPublicKey::ckDispose(eccKey)
        ProcedureReturn
    EndIf

    ; ---------------------------------------------------
    ; The HMAC key (in base64url format)
    hmacKey.s = "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.i = CkStringBuilder::ckCreate()
    If sbJws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbJws,"{ ")
    CkStringBuilder::ckAppend(sbJws,"  " + Chr(34) + "payload" + Chr(34) + ": " + Chr(34) + "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbJws,"  " + Chr(34) + "signatures" + Chr(34) + ": [")
    CkStringBuilder::ckAppend(sbJws,"    { ")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "protected" + Chr(34) + ": " + Chr(34) + "eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "signature" + Chr(34) + ": " + Chr(34) + "IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw" + Chr(34))
    CkStringBuilder::ckAppend(sbJws,"    },")
    CkStringBuilder::ckAppend(sbJws,"    { ")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "protected" + Chr(34) + ": " + Chr(34) + "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "signature" + Chr(34) + ": " + Chr(34) + "1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw" + Chr(34))
    CkStringBuilder::ckAppend(sbJws,"    },")
    CkStringBuilder::ckAppend(sbJws,"    { ")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "protected" + Chr(34) + ": " + Chr(34) + "eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0" + Chr(34) + ",")
    CkStringBuilder::ckAppend(sbJws,"      " + Chr(34) + "signature" + Chr(34) + ": " + Chr(34) + "YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw" + Chr(34))
    CkStringBuilder::ckAppend(sbJws,"    }")
    CkStringBuilder::ckAppend(sbJws,"  ]")
    CkStringBuilder::ckAppend(sbJws,"}")

    jws.i = CkJws::ckCreate()
    If jws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkJws::ckLoadJwsSb(jws,sbJws)
    If success <> 1
        Debug CkJws::ckLastErrorText(jws)
        CkStringBuilder::ckDispose(sbRsaJwk)
        CkPublicKey::ckDispose(rsaKey)
        CkStringBuilder::ckDispose(sbEccJwk)
        CkPublicKey::ckDispose(eccKey)
        CkStringBuilder::ckDispose(sbJws)
        CkJws::ckDispose(jws)
        ProcedureReturn
    EndIf

    ; The payload is easily accessible:
    Debug "Payload: " + CkJws::ckGetPayload(jws,"utf-8")

    protHeader.i
    sbKeyId.i = CkStringBuilder::ckCreate()
    If sbKeyId.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bCaseSensitive.i = 0

    numSignatures.i = CkJws::ckNumSignatures(jws)
    i.i = 0
    While i < numSignatures
        protHeader = CkJws::ckGetProtectedHeader(jws,i)

        Debug "--------------------------"
        Debug Str(i) + ": "

        ; Get the protected header.
        CkJsonObject::setCkEmitCompact(protHeader, 0)
        Debug CkJsonObject::ckEmit(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).

        CkStringBuilder::ckClear(sbKeyId)
        CkStringBuilder::ckAppend(sbKeyId,CkJsonObject::ckStringOf(protHeader,"kid"))

        ; Set the key based on key ID.
        If CkStringBuilder::ckContentsEqual(sbKeyId,"myRsaKey",bCaseSensitive) = 1
            CkJws::ckSetPublicKey(jws,i,rsaKey)
        EndIf

        If CkStringBuilder::ckContentsEqual(sbKeyId,"myEcKey",bCaseSensitive) = 1
            CkJws::ckSetPublicKey(jws,i,eccKey)
        EndIf

        If CkStringBuilder::ckContentsEqual(sbKeyId,"myMacKey",bCaseSensitive) = 1
            CkJws::ckSetMacKey(jws,i,hmacKey,"base64url")
        EndIf

        ; Validate this signature.
        v.i = CkJws::ckValidate(jws,i)
        If v < 0
            ; Perhaps Chilkat was not unlocked or the trial expired..
            Debug "Validate failed for some other reason."
            Debug CkJws::ckLastErrorText(jws)
        Else
            If v = 0
                Debug "Invalid signature.  The key was incorrect, the JWS was invalid, or both."
            Else
                Debug "Signature validated."
            EndIf

        EndIf

        CkJsonObject::ckDispose(protHeader)

        i = i + 1
    Wend

    ; 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.


    CkStringBuilder::ckDispose(sbRsaJwk)
    CkPublicKey::ckDispose(rsaKey)
    CkStringBuilder::ckDispose(sbEccJwk)
    CkPublicKey::ckDispose(eccKey)
    CkStringBuilder::ckDispose(sbJws)
    CkJws::ckDispose(jws)
    CkStringBuilder::ckDispose(sbKeyId)


    ProcedureReturn
EndProcedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.