PureBasic
PureBasic
Validate JWS Using RSASSA-PKCS1-v1_5 SHA-256
See more JSON Web Signatures (JWS) Examples
Validates a JSON Web Signature (JWS) that uses RSASSA-PKCS1-v1_5 SHA-256.Chilkat PureBasic Downloads
IncludeFile "CkJws.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPublicKey.pb"
Procedure ChilkatExample()
success.i = 0
; This requires the Chilkat API to have been previously unlocked.
; See Global Unlock Sample for sample code.
; This example takes a JSON signature in compact serialization format,
; and uses an RSA public key to validate and recover the protected header and payload.
; We only need a public key for signature validation. This is the RSA public key
; that is used:
; {"kty":"RSA",
; "n":"ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx
; HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs
; D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH
; SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV
; MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8
; NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ",
; "e":"AQAB"
; }
sbPubKey.i = CkStringBuilder::ckCreate()
If sbPubKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbPubKey,"{" + Chr(34) + "kty" + Chr(34) + ":" + Chr(34) + "RSA" + Chr(34) + ",")
CkStringBuilder::ckAppend(sbPubKey,Chr(34) + "n" + Chr(34) + ":" + Chr(34) + "ofgWCuLjybRlzo0tZWJjNiuSfb4p4fAkd_wWJcyQoTbji9k0l8W26mPddx")
CkStringBuilder::ckAppend(sbPubKey,"HmfHQp-Vaw-4qPCJrcS2mJPMEzP1Pt0Bm4d4QlL-yRT-SFd2lZS-pCgNMs")
CkStringBuilder::ckAppend(sbPubKey,"D1W_YpRPEwOWvG6b32690r2jZ47soMZo9wGzjb_7OMg0LOL-bSf63kpaSH")
CkStringBuilder::ckAppend(sbPubKey,"SXndS5z5rexMdbBYUsLA9e-KXBdQOS-UTo7WTBEMa2R2CapHg665xsmtdV")
CkStringBuilder::ckAppend(sbPubKey,"MTBQY4uDZlxvb3qCo5ZwKh9kG4LT6_I5IhlJH7aGhyxXFvUK-DWNmoudF8")
CkStringBuilder::ckAppend(sbPubKey,"NAco9_h9iaGNj8q2ethFkMLs91kzk2PAcDTW9gb54h4FRWyuXpoQ" + Chr(34) + ",")
CkStringBuilder::ckAppend(sbPubKey,Chr(34) + "e" + Chr(34) + ":" + Chr(34) + "AQAB" + Chr(34))
CkStringBuilder::ckAppend(sbPubKey,"}")
pubKey.i = CkPublicKey::ckCreate()
If pubKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPublicKey::ckLoadFromString(pubKey,CkStringBuilder::ckGetAsString(sbPubKey))
If success = 0
Debug CkPublicKey::ckLastErrorText(pubKey)
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
ProcedureReturn
EndIf
jws.i = CkJws::ckCreate()
If jws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Set the RSA public key:
signatureIndex.i = 0
CkJws::ckSetPublicKey(jws,signatureIndex,pubKey)
; Load the JWS.
sbJws.i = CkStringBuilder::ckCreate()
If sbJws.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkStringBuilder::ckAppend(sbJws,"eyJhbGciOiJSUzI1NiJ9")
CkStringBuilder::ckAppend(sbJws,".")
CkStringBuilder::ckAppend(sbJws,"eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt")
CkStringBuilder::ckAppend(sbJws,"cGxlLmNvbS9pc19yb290Ijp0cnVlfQ")
CkStringBuilder::ckAppend(sbJws,".")
CkStringBuilder::ckAppend(sbJws,"cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7")
CkStringBuilder::ckAppend(sbJws,"AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4")
CkStringBuilder::ckAppend(sbJws,"BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K")
CkStringBuilder::ckAppend(sbJws,"0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqv")
CkStringBuilder::ckAppend(sbJws,"hJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrB")
CkStringBuilder::ckAppend(sbJws,"p0igcN_IoypGlUPQGe77Rw")
success = CkJws::ckLoadJwsSb(jws,sbJws)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
CkJws::ckDispose(jws)
CkStringBuilder::ckDispose(sbJws)
ProcedureReturn
EndIf
; Validate the 1st (and only) signature at index 0..
v.i = CkJws::ckValidate(jws,signatureIndex)
If v < 0
; Perhaps Chilkat was not unlocked or the trial expired..
Debug "Method call failed for some other reason."
Debug CkJws::ckLastErrorText(jws)
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
CkJws::ckDispose(jws)
CkStringBuilder::ckDispose(sbJws)
ProcedureReturn
EndIf
If v = 0
Debug "Invalid signature. The RSA key was incorrect, the JWS was invalid, or both."
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
CkJws::ckDispose(jws)
CkStringBuilder::ckDispose(sbJws)
ProcedureReturn
EndIf
; If we get here, the signature was validated..
Debug "Signature validated."
Debug "--"
; Recover the original content:
Debug "Recovered content:"
Debug CkJws::ckGetPayload(jws,"utf-8")
Debug "--"
; Examine the protected header:
joseHeader.i = CkJsonObject::ckCreate()
If joseHeader.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkJws::ckGetProtectedH(jws,signatureIndex,joseHeader)
If success = 0
Debug CkJws::ckLastErrorText(jws)
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
CkJws::ckDispose(jws)
CkStringBuilder::ckDispose(sbJws)
CkJsonObject::ckDispose(joseHeader)
ProcedureReturn
EndIf
CkJsonObject::setCkEmitCompact(joseHeader, 0)
Debug "Protected (JOSE) header:"
Debug CkJsonObject::ckEmit(joseHeader)
; Output:
; Signature validated.
; --
; Recovered content:
; {"iss":"joe",
; "exp":1300819380,
; "http://example.com/is_root":true}
; --
; Protected (JOSE) header:
; {
; "alg": "RS256"
; }
CkStringBuilder::ckDispose(sbPubKey)
CkPublicKey::ckDispose(pubKey)
CkJws::ckDispose(jws)
CkStringBuilder::ckDispose(sbJws)
CkJsonObject::ckDispose(joseHeader)
ProcedureReturn
EndProcedure