Tcl
Tcl
JWE Using General JWE JSON Serialization
See more JSON Web Encryption (JWE) Examples
This example duplicates the example A.4 in RFC 7516 for JSON Web Encryption (JWE).This example demonstrates the capability for encrypting the same plaintext to multiple recipients. Two recipients are present in this example.
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# 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.
set plaintext "Live long and prosper."
set jwe [new_CkJwe]
# First build the JWE Protected Header: {"enc":"A128CBC-HS256"}
set jweProtHdr [new_CkJsonObject]
CkJsonObject_AppendString $jweProtHdr "enc" "A128CBC-HS256"
CkJwe_SetProtectedHeader $jwe $jweProtHdr
# The first recipient uses the RSAES-PKCS1-v1_5 algorithm to encrypt
# the CEK. The second uses AES Key Wrap to encrypt the CEK. Key ID
# values are supplied for both keys. The two JWE Per-Recipient
# Unprotected Header values used to represent these algorithms and key
# IDs are:
#
# {"alg":"RSA1_5","kid":"2011-04-29"}
#
# and
#
# {"alg":"A128KW","kid":"7"}
set jweRecipientHdr1 [new_CkJsonObject]
CkJsonObject_AppendString $jweRecipientHdr1 "alg" "RSA1_5"
CkJsonObject_AppendString $jweRecipientHdr1 "kid" "2011-04-29"
set recipientIndex 0
CkJwe_SetRecipientHeader $jwe $recipientIndex $jweRecipientHdr1
set jweRecipientHdr2 [new_CkJsonObject]
CkJsonObject_AppendString $jweRecipientHdr2 "alg" "A128KW"
CkJsonObject_AppendString $jweRecipientHdr2 "kid" "7"
set recipientIndex 1
CkJwe_SetRecipientHeader $jwe $recipientIndex $jweRecipientHdr2
# Set the Shared Unprotected Header: {"jku":"https://server.example.com/keys.jwks"}
set jweUnprotHdr [new_CkJsonObject]
CkJsonObject_AppendString $jweUnprotHdr "jku" "https://server.example.com/keys.jwks"
CkJwe_SetUnprotectedHeader $jwe $jweUnprotHdr
# Note: The intent of specifying a "kid" (an acronym for "Key ID") is that
# the software would somehow download the keys.jwks from https://server.example.com/keys.jwks,
# and would select the key (whatever format it may be, such as RSA, or an AES key wrap key, etc.),
# and then automatically use it.
# This example keeps the "kid" and "jku" in their respective headers, but does not actually fetch
# the keys from some URL. We'll just provide the keys directly..
# Recipient 0 uses this RSA key:
set sbJwk [new_CkStringBuilder]
CkStringBuilder_Append $sbJwk "{\"kty\":\"RSA\","
CkStringBuilder_Append $sbJwk "\"n\":\"sXchDaQebHnPiGvyDOAT4saGEUetSyo9MKLOoWFsueri23bOdgWp4Dy1Wl"
CkStringBuilder_Append $sbJwk "UzewbgBHod5pcM9H95GQRV3JDXboIRROSBigeC5yjU1hGzHHyXss8UDpre"
CkStringBuilder_Append $sbJwk "cbAYxknTcQkhslANGRUZmdTOQ5qTRsLAt6BTYuyvVRdhS8exSZEy_c4gs_"
CkStringBuilder_Append $sbJwk "7svlJJQ4H9_NxsiIoLwAEk7-Q3UXERGYw_75IDrGA84-lA_-Ct4eTlXHBI"
CkStringBuilder_Append $sbJwk "Y2EaV7t7LjJaynVJCpkv4LKjTTAumiGUIuQhrNhZLuF_RJLqHpM2kgWFLU"
CkStringBuilder_Append $sbJwk "7-VTdL1VbC2tejvcI2BlMkEpk1BzBZI0KQB0GaDWFLN-aEAw3vRw\","
CkStringBuilder_Append $sbJwk "\"e\":\"AQAB\","
CkStringBuilder_Append $sbJwk "\"d\":\"VFCWOqXr8nvZNyaaJLXdnNPXZKRaWCjkU5Q2egQQpTBMwhprMzWzpR8Sxq"
CkStringBuilder_Append $sbJwk "1OPThh_J6MUD8Z35wky9b8eEO0pwNS8xlh1lOFRRBoNqDIKVOku0aZb-ry"
CkStringBuilder_Append $sbJwk "nq8cxjDTLZQ6Fz7jSjR1Klop-YKaUHc9GsEofQqYruPhzSA-QgajZGPbE_"
CkStringBuilder_Append $sbJwk "0ZaVDJHfyd7UUBUKunFMScbflYAAOYJqVIVwaYR5zWEEceUjNnTNo_CVSj"
CkStringBuilder_Append $sbJwk "-VvXLO5VZfCUAVLgW4dpf1SrtZjSt34YLsRarSb127reG_DUwg9Ch-Kyvj"
CkStringBuilder_Append $sbJwk "T1SkHgUWRVGcyly7uvVGRSDwsXypdrNinPA4jlhoNdizK2zF2CWQ\","
CkStringBuilder_Append $sbJwk "\"p\":\"9gY2w6I6S6L0juEKsbeDAwpd9WMfgqFoeA9vEyEUuk4kLwBKcoe1x4HG68"
CkStringBuilder_Append $sbJwk "ik918hdDSE9vDQSccA3xXHOAFOPJ8R9EeIAbTi1VwBYnbTp87X-xcPWlEP"
CkStringBuilder_Append $sbJwk "krdoUKW60tgs1aNd_Nnc9LEVVPMS390zbFxt8TN_biaBgelNgbC95sM\","
CkStringBuilder_Append $sbJwk "\"q\":\"uKlCKvKv_ZJMVcdIs5vVSU_6cPtYI1ljWytExV_skstvRSNi9r66jdd9-y"
CkStringBuilder_Append $sbJwk "BhVfuG4shsp2j7rGnIio901RBeHo6TPKWVVykPu1iYhQXw1jIABfw-MVsN"
CkStringBuilder_Append $sbJwk "-3bQ76WLdt2SDxsHs7q7zPyUyHXmps7ycZ5c72wGkUwNOjYelmkiNS0\","
CkStringBuilder_Append $sbJwk "\"dp\":\"w0kZbV63cVRvVX6yk3C8cMxo2qCM4Y8nsq1lmMSYhG4EcL6FWbX5h9yuv"
CkStringBuilder_Append $sbJwk "ngs4iLEFk6eALoUS4vIWEwcL4txw9LsWH_zKI-hwoReoP77cOdSL4AVcra"
CkStringBuilder_Append $sbJwk "Hawlkpyd2TWjE5evgbhWtOxnZee3cXJBkAi64Ik6jZxbvk-RR3pEhnCs\","
CkStringBuilder_Append $sbJwk "\"dq\":\"o_8V14SezckO6CNLKs_btPdFiO9_kC1DsuUTd2LAfIIVeMZ7jn1Gus_Ff"
CkStringBuilder_Append $sbJwk "7B7IVx3p5KuBGOVF8L-qifLb6nQnLysgHDh132NDioZkhH7mI7hPG-PYE_"
CkStringBuilder_Append $sbJwk "odApKdnqECHWw0J-F0JWnUd6D2B_1TvF9mXA2Qx-iGYn8OVV1Bsmp6qU\","
CkStringBuilder_Append $sbJwk "\"qi\":\"eNho5yRBEBxhGBtQRww9QirZsB66TrfFReG_CcteI1aCneT0ELGhYlRlC"
CkStringBuilder_Append $sbJwk "tUkTRclIfuEPmNsNDPbLoLqqCVznFbvdB7x-Tl-m0l_eFTj2KiqwGqE9PZ"
CkStringBuilder_Append $sbJwk "B9nNTwMVvH3VRRSLWACvPnSiwP8N5Usy-WRXS-V7TbpxIhvepTfE0NNo\""
CkStringBuilder_Append $sbJwk "}"
# Load this JWK into a Chilkat private key object.
set rsaPrivKey [new_CkPrivateKey]
set success [CkPrivateKey_LoadJwk $rsaPrivKey [CkStringBuilder_getAsString $sbJwk]]
if {$success == 0} then {
puts [CkPrivateKey_lastErrorText $rsaPrivKey]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
exit
}
# Get the public key for encryption. (The RSA private key is used for decryption.)
set rsaPubKey [new_CkPublicKey]
CkPrivateKey_ToPublicKey $rsaPrivKey $rsaPubKey
set recipientIndex 0
CkJwe_SetPublicKey $jwe $recipientIndex $rsaPubKey
# Recipient 1 uses AES Key Wrap
set aesWrappingKey "GawgguFyGrWKav7AX4VKUg"
set recipientIndex 1
CkJwe_SetWrappingKey $jwe $recipientIndex $aesWrappingKey "base64url"
# OK.. everything has been specified.
# Now encrypt. Chilkat will use the general JSON serialization because it is only
# possible serializationto use given there are multiple recipients.
set strJwe [CkJwe_encrypt $jwe $plaintext "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe] == 0} then {
puts [CkJwe_lastErrorText $jwe]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
exit
}
# The strJwe is in the most compact form possible (a single line).
# Let's load it into a JSON object and examine in a non-compact pretty-printed format:
set jsonTemp [new_CkJsonObject]
CkJsonObject_Load $jsonTemp $strJwe
CkJsonObject_put_EmitCompact $jsonTemp 0
puts [CkJsonObject_emit $jsonTemp]
# The JWE looks like this:
# (Note: Because of random values used in the encryption process, your encrypted results will be different.)
# {
# "protected": "eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0",
# "unprotected": {
# "jku": "https://server.example.com/keys.jwks"
# },
# "recipients": [
# {
# "header": {
# "alg": "RSA1_5",
# "kid": "2011-04-29"
# },
# "encrypted_key": "WlUggejR-vStJVNKgjG2mQrVv74aga1FFutT8E-n-yJEOTsOVnGjhj1NW_Snd9DqHAhFrc7NEvKCFplKWGnusBZxxjm1JpdUa0MIpkGmncJHQQdfm21vcbEUbDmfqVY79SnEis3tih1D3qmyp_Bxti4byDAHJIOJv_cj0Sx8oHZzgGtOLjtHsydyo1MtBIqI0w86i_uhUuraihZ3ngj67YZ6uqpR6lulkPIVohfF3oJ0D3Ay_XOCHWlHYkTg6VZsa0FDrPtfB2pG3TxTBPwI4IMj5uF_D3zrg51dCYKU1Gah71ujLaXRE5q9XF7oOknxiWQuWc7Ox8JP03lSx-DiVA"
# },
# {
# "header": {
# "alg": "A128KW",
# "kid": "7"
# },
# "encrypted_key": "8diBP2aUUB_Jl5WxCuMJLN6HsppE3rhSjcecee0fBcwB31zbPAVejQ"
# }
# ],
# "iv": "JqahFKx5Z8SFT_LnkfOq0Q",
# "ciphertext": "YcSdjlszsaY1ADcs4Vw85H_WoAqnkDIJaJsTkmCj05s",
# "tag": "G3KF2CZ-DMhm0cqUbhJKMA"
# }
# To decrypt, we don't need both recipient keys. We can decrypt with one key or the other.
# The FindRecipient method can be used to find a particular recipient index.
# First, load the JWE..
set jwe2 [new_CkJwe]
set success [CkJwe_LoadJwe $jwe2 $strJwe]
if {$success == 0} then {
puts [CkJwe_lastErrorText $jwe2]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
exit
}
# Let's say we have the AES key wrap key, and we know the "kid" equals "7".
set caseSensitive 0
set recipientIndex [CkJwe_FindRecipient $jwe2 "kid" "7" $caseSensitive]
if {$recipientIndex < 0} then {
puts "Unable to find recipient with kid=7"
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
exit
}
# Set the AES wrap key for the recipient index.
CkJwe_SetWrappingKey $jwe2 $recipientIndex $aesWrappingKey "base64url"
# Decrypt
set originalPlaintext [CkJwe_decrypt $jwe2 $recipientIndex "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe2] == 0} then {
puts [CkJwe_lastErrorText $jwe2]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
exit
}
puts "original text decrypted with AES key wrap key: "
puts "$originalPlaintext"
# Now, let's do the same with the RSA private key:
set recipientIndex [CkJwe_FindRecipient $jwe2 "kid" "2011-04-29" $caseSensitive]
if {$recipientIndex < 0} then {
puts "Unable to find recipient with kid=2011-04-29"
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
exit
}
# Set the RSA private key for the recipient index.
CkJwe_SetPrivateKey $jwe2 $recipientIndex $rsaPrivKey
# Decrypt
set originalPlaintext [CkJwe_decrypt $jwe2 $recipientIndex "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe2] == 0} then {
puts [CkJwe_lastErrorText $jwe2]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
exit
}
puts "original text decrypted with RSA private key: "
puts "$originalPlaintext"
# ---------------------------------------------------------------------------------
# It should also be possible to decrypt the JWE as shown in RFC 7516, Appendix A.4.7
# because it was produced using the same keys.
set sbJwe [new_CkStringBuilder]
CkStringBuilder_Append $sbJwe "{"
CkStringBuilder_Append $sbJwe "\"protected\":"
CkStringBuilder_Append $sbJwe "\"eyJlbmMiOiJBMTI4Q0JDLUhTMjU2In0\","
CkStringBuilder_Append $sbJwe "\"unprotected\":"
CkStringBuilder_Append $sbJwe "{\"jku\":\"https://server.example.com/keys.jwks\"},"
CkStringBuilder_Append $sbJwe "\"recipients\":["
CkStringBuilder_Append $sbJwe "{\"header\":"
CkStringBuilder_Append $sbJwe "{\"alg\":\"RSA1_5\",\"kid\":\"2011-04-29\"},"
CkStringBuilder_Append $sbJwe "\"encrypted_key\":"
CkStringBuilder_Append $sbJwe "\"UGhIOguC7IuEvf_NPVaXsGMoLOmwvc1GyqlIKOK1nN94nHPoltGRhWhw7Zx0-"
CkStringBuilder_Append $sbJwe "kFm1NJn8LE9XShH59_i8J0PH5ZZyNfGy2xGdULU7sHNF6Gp2vPLgNZ__deLKx"
CkStringBuilder_Append $sbJwe "GHZ7PcHALUzoOegEI-8E66jX2E4zyJKx-YxzZIItRzC5hlRirb6Y5Cl_p-ko3"
CkStringBuilder_Append $sbJwe "YvkkysZIFNPccxRU7qve1WYPxqbb2Yw8kZqa2rMWI5ng8OtvzlV7elprCbuPh"
CkStringBuilder_Append $sbJwe "cCdZ6XDP0_F8rkXds2vE4X-ncOIM8hAYHHi29NX0mcKiRaD0-D-ljQTP-cFPg"
CkStringBuilder_Append $sbJwe "wCp6X-nZZd9OHBv-B3oWh2TbqmScqXMR4gp_A\"},"
CkStringBuilder_Append $sbJwe "{\"header\":"
CkStringBuilder_Append $sbJwe "{\"alg\":\"A128KW\",\"kid\":\"7\"},"
CkStringBuilder_Append $sbJwe "\"encrypted_key\":"
CkStringBuilder_Append $sbJwe "\"6KB707dM9YTIgHtLvtgWQ8mKwboJW3of9locizkDTHzBC2IlrT1oOQ\"}],"
CkStringBuilder_Append $sbJwe "\"iv\":"
CkStringBuilder_Append $sbJwe "\"AxY8DCtDaGlsbGljb3RoZQ\","
CkStringBuilder_Append $sbJwe "\"ciphertext\":"
CkStringBuilder_Append $sbJwe "\"KDlTtXchhZTGufMYmOYGS4HffxPSUrfmqCHXaI9wOGY\","
CkStringBuilder_Append $sbJwe "\"tag\":"
CkStringBuilder_Append $sbJwe "\"Mz-VPPyU4RlcuYv1IwIvzw\""
CkStringBuilder_Append $sbJwe "}"
set success [CkJwe_LoadJweSb $jwe2 $sbJwe]
if {$success == 0} then {
puts [CkJwe_lastErrorText $jwe2]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
delete_CkStringBuilder $sbJwe
exit
}
# We can decrypt with either key. Let's use the AES key wrap key...
set recipientIndex [CkJwe_FindRecipient $jwe2 "kid" "7" $caseSensitive]
if {$recipientIndex < 0} then {
puts "Unable to find recipient with kid=7"
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
delete_CkStringBuilder $sbJwe
exit
}
# Set the AES wrap key for the recipient index.
CkJwe_SetWrappingKey $jwe2 $recipientIndex $aesWrappingKey "base64url"
# Decrypt
set originalPlaintext [CkJwe_decrypt $jwe2 $recipientIndex "utf-8"]
if {[CkJwe_get_LastMethodSuccess $jwe2] == 0} then {
puts [CkJwe_lastErrorText $jwe2]
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
delete_CkStringBuilder $sbJwe
exit
}
puts "original text decrypted from published JWE, with AES key wrap key: "
puts "$originalPlaintext"
# The output:
# original text decrypted with AES key wrap key:
# Live long and prosper.
# original text decrypted with RSA private key:
# Live long and prosper.
# original text decrypted from published JWE, with AES key wrap key:
# Live long and prosper.
delete_CkJwe $jwe
delete_CkJsonObject $jweProtHdr
delete_CkJsonObject $jweRecipientHdr1
delete_CkJsonObject $jweRecipientHdr2
delete_CkJsonObject $jweUnprotHdr
delete_CkStringBuilder $sbJwk
delete_CkPrivateKey $rsaPrivKey
delete_CkPublicKey $rsaPubKey
delete_CkJsonObject $jsonTemp
delete_CkJwe $jwe2
delete_CkStringBuilder $sbJwe