SQL Server
SQL Server
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 SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @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.
DECLARE @sbRsaJwk int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRsaJwk OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '{"kty":"RSA",'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, 'NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '"e":"AQAB"'
EXEC sp_OAMethod @sbRsaJwk, 'Append', @success OUT, '}'
DECLARE @rsaKey int
EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @rsaKey OUT
EXEC sp_OAMethod @sbRsaJwk, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @rsaKey, 'LoadFromString', @success OUT, @sTmp0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsaKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbRsaJwk
EXEC @hr = sp_OADestroy @rsaKey
RETURN
END
-- ---------------------------------------------------
-- Use the following ECC public key loaded from JWK format.
DECLARE @sbEccJwk int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbEccJwk OUT
EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '{"kty":"EC",'
EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"crv":"P-256",'
EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",'
EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '"y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0"'
EXEC sp_OAMethod @sbEccJwk, 'Append', @success OUT, '}'
DECLARE @eccKey int
EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @eccKey OUT
EXEC sp_OAMethod @sbEccJwk, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @eccKey, 'LoadFromString', @success OUT, @sTmp0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @eccKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbRsaJwk
EXEC @hr = sp_OADestroy @rsaKey
EXEC @hr = sp_OADestroy @sbEccJwk
EXEC @hr = sp_OADestroy @eccKey
RETURN
END
-- ---------------------------------------------------
-- The HMAC key (in base64url format)
DECLARE @hmacKey nvarchar(4000)
SELECT @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"
-- }
-- ]
-- }
DECLARE @sbJws int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbJws OUT
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, '{ '
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "payload": "SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu",'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "signatures": ['
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' { '
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "protected": "eyJhbGciOiJSUzI1NiIsImtpZCI6Im15UnNhS2V5In0",'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "signature": "IPMQ02niTQDwLzsRZSCaEm9VEyAX_AVe3HWjniNt9kW-a8d6ZVbd2k6jGae8s1yIh0cgxDnXQ6-p6_sBI0cnMO0xpuJANhh2vFtNJl5lisad94-H3mB3lSfafRqxeYp5D8bh39BPv7y3PrUNVMQdKEJp_D5oJ0ROPTIYx3EG8eJQOx1HO0KqhcUo401XR6KSsIyFm5joBLNKTVzxZUTT1RRZZtwTdeZkbGevugIOX_9gHAtARpV6WaFA4Vvjnq8X9wPgqjWNCQRupadhTPz0JAsa-wy5vXQjsFlXAn43mDPpMfna5Ab3F5pS4yDwkbX6nRn7XBxH1SnnNJRFholQZw"'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' },'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' { '
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "protected": "eyJhbGciOiJFUzI1NiIsImtpZCI6Im15RWNLZXkifQ",'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "signature": "1OQtaT3pgZmkDxvlfghvxL_8kX16WIen6u1MadEq1pA4qytA0--_EwZDNk00GDPWFpoJtKznibMZzLOg_UhHIw"'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' },'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' { '
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "protected": "eyJhbGciOiJIUzI1NiIsImtpZCI6Im15TWFjS2V5In0",'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' "signature": "YY8yVjmJJfy7YJOn3uUydG8WCY2PEuCvOLil5Ks5lnw"'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' }'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, ' ]'
EXEC sp_OAMethod @sbJws, 'Append', @success OUT, '}'
DECLARE @jws int
EXEC @hr = sp_OACreate 'Chilkat.Jws', @jws OUT
EXEC sp_OAMethod @jws, 'LoadJwsSb', @success OUT, @sbJws
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbRsaJwk
EXEC @hr = sp_OADestroy @rsaKey
EXEC @hr = sp_OADestroy @sbEccJwk
EXEC @hr = sp_OADestroy @eccKey
EXEC @hr = sp_OADestroy @sbJws
EXEC @hr = sp_OADestroy @jws
RETURN
END
-- The payload is easily accessible:
EXEC sp_OAMethod @jws, 'GetPayload', @sTmp0 OUT, 'utf-8'
PRINT 'Payload: ' + @sTmp0
DECLARE @sbKeyId int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbKeyId OUT
DECLARE @bCaseSensitive int
SELECT @bCaseSensitive = 0
DECLARE @protHeader int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @protHeader OUT
DECLARE @numSignatures int
EXEC sp_OAGetProperty @jws, 'NumSignatures', @numSignatures OUT
DECLARE @i int
SELECT @i = 0
WHILE @i < @numSignatures
BEGIN
EXEC sp_OAMethod @jws, 'GetProtectedH', @success OUT, @i, @protHeader
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @sbRsaJwk
EXEC @hr = sp_OADestroy @rsaKey
EXEC @hr = sp_OADestroy @sbEccJwk
EXEC @hr = sp_OADestroy @eccKey
EXEC @hr = sp_OADestroy @sbJws
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @sbKeyId
EXEC @hr = sp_OADestroy @protHeader
RETURN
END
PRINT '--------------------------'
PRINT @i + ': '
-- Get the protected header.
EXEC sp_OASetProperty @protHeader, 'EmitCompact', 0
EXEC sp_OAMethod @protHeader, 'Emit', @sTmp0 OUT
PRINT @sTmp0
-- 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).
EXEC sp_OAMethod @sbKeyId, 'Clear', NULL
EXEC sp_OAMethod @protHeader, 'StringOf', @sTmp0 OUT, 'kid'
EXEC sp_OAMethod @sbKeyId, 'Append', @success OUT, @sTmp0
-- Set the key based on key ID.
EXEC sp_OAMethod @sbKeyId, 'ContentsEqual', @iTmp0 OUT, 'myRsaKey', @bCaseSensitive
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @jws, 'SetPublicKey', @success OUT, @i, @rsaKey
END
EXEC sp_OAMethod @sbKeyId, 'ContentsEqual', @iTmp0 OUT, 'myEcKey', @bCaseSensitive
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @jws, 'SetPublicKey', @success OUT, @i, @eccKey
END
EXEC sp_OAMethod @sbKeyId, 'ContentsEqual', @iTmp0 OUT, 'myMacKey', @bCaseSensitive
IF @iTmp0 = 1
BEGIN
EXEC sp_OAMethod @jws, 'SetMacKey', @success OUT, @i, @hmacKey, 'base64url'
END
-- Validate this signature.
DECLARE @v int
EXEC sp_OAMethod @jws, 'Validate', @v OUT, @i
IF @v < 0
BEGIN
-- Perhaps Chilkat was not unlocked or the trial expired..
PRINT 'Validate failed for some other reason.'
EXEC sp_OAGetProperty @jws, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
END
ELSE
BEGIN
IF @v = 0
BEGIN
PRINT 'Invalid signature. The key was incorrect, the JWS was invalid, or both.'
END
ELSE
BEGIN
PRINT 'Signature validated.'
END
END
SELECT @i = @i + 1
END
-- 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.
EXEC @hr = sp_OADestroy @sbRsaJwk
EXEC @hr = sp_OADestroy @rsaKey
EXEC @hr = sp_OADestroy @sbEccJwk
EXEC @hr = sp_OADestroy @eccKey
EXEC @hr = sp_OADestroy @sbJws
EXEC @hr = sp_OADestroy @jws
EXEC @hr = sp_OADestroy @sbKeyId
EXEC @hr = sp_OADestroy @protHeader
END
GO