C
C
Set the Optional JWS Unprotected Header
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetUnprotectedHeader, which sets the optional unprotected header for a signature.
Background. Unlike the protected header, the unprotected header is not covered by the signature. It is carried in the JSON serialization forms of a JWS, so producing a JWS with an unprotected header yields a JSON serialization rather than compact form.
Chilkat C Downloads
#include <C_CkJws.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
HCkJws jws;
HCkJsonObject header;
HCkJsonObject unprotected;
BOOL bIncludeBom;
const char *base64Key;
const char *jwsJson;
success = FALSE;
jws = CkJws_Create();
// Protected header specifying HMAC-SHA256.
header = CkJsonObject_Create();
CkJsonObject_UpdateString(header,"alg","HS256");
success = CkJws_SetProtectedHeader(jws,0,header);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
return;
}
// Set the optional unprotected header for signature 0. Unlike the protected header, it is not
// covered by the signature. It is carried in the JSON serialization forms of a JWS.
unprotected = CkJsonObject_Create();
CkJsonObject_UpdateString(unprotected,"kid","signer-key-1");
success = CkJws_SetUnprotectedHeader(jws,0,unprotected);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);
return;
}
bIncludeBom = FALSE;
success = CkJws_SetPayload(jws,"This is the content to sign.","utf-8",bIncludeBom);
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);
return;
}
base64Key = "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU=";
success = CkJws_SetMacKey(jws,0,base64Key,"base64");
if (success == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);
return;
}
// Because an unprotected header is present, the JWS is produced in a JSON serialization form.
jwsJson = CkJws_createJws(jws);
if (CkJws_getLastMethodSuccess(jws) == FALSE) {
printf("%s\n",CkJws_lastErrorText(jws));
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);
return;
}
printf("%s\n",jwsJson);
CkJws_Dispose(jws);
CkJsonObject_Dispose(header);
CkJsonObject_Dispose(unprotected);
}