DataFlex
DataFlex
Create JWS Using Private Key on a Smart Card
See more JSON Web Signatures (JWS) Examples
Creates and validates a JSON Web Signature (JWS) using the private key associated with a certificate on a smart card.Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Variant vCert
Handle hoCert
ProtHdr Handle hoJwsProtHdr
Handle hoJws
Integer iSignatureIndex
Boolean iBIncludeBom
String sPayloadStr
String sJwsCompact
2 Handle hoJws2
Variant vPubKey
Handle hoPubKey
Integer v
Variant vJoseHeader
Handle hoJoseHeader
String sTemp1
Boolean bTemp1
Move False To iSuccess
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load the certificate from a smart card.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
// Set the smarcard PIN prior to loading
Set ComSmartCardPin Of hoCert To "123456"
// Detect the connected smartcard or USB security token and load the default certificate.
Get ComLoadFromSmartcard Of hoCert "" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
// Note: Chilkat provides many different ways to load a certificate from a smartcard or USB token,
// such as selecting a certificate if the card contains multiple certificates with private keys,
// or working with lower-level PKCS11 or ScMinidriver API's (both of which Chilkat provides).
// Create the JWS Protected Header
Get Create (RefClass(cComChilkatJsonObject)) To hoJwsProtHdr
If (Not(IsComObjectCreated(hoJwsProtHdr))) Begin
Send CreateComObject of hoJwsProtHdr
End
Get ComIsEcdsa Of hoCert To bTemp1
If (bTemp1 = True) Begin
Get ComAppendString Of hoJwsProtHdr "alg" "ES256" To iSuccess
End
Else Begin
Get ComAppendString Of hoJwsProtHdr "alg" "RS256" To iSuccess
End
Get Create (RefClass(cComChilkatJws)) To hoJws
If (Not(IsComObjectCreated(hoJws))) Begin
Send CreateComObject of hoJws
End
// Set the protected header:
Move 0 To iSignatureIndex
Get pvComObject of hoJwsProtHdr to vJwsProtHdr
Get ComSetProtectedHeader Of hoJws iSignatureIndex vJwsProtHdr To iSuccess
// Provide the private key via the certificate.
// This requires Chilkat v11.5.0 or greater.
Get pvComObject of hoCert to vCert
Get ComSetSigningCert Of hoJws iSignatureIndex vCert To iSuccess
// Set the payload.
Move False To iBIncludeBom
Move "In our village, folks say God crumbles up the old moon into stars." To sPayloadStr
Get ComSetPayload Of hoJws sPayloadStr "utf-8" iBIncludeBom To iSuccess
// Create the JWS
// By default, the compact serialization is used.
Get ComCreateJws Of hoJws To sJwsCompact
Get ComLastMethodSuccess Of hoJws To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoJws To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "JWS: " sJwsCompact
// sample output:
// JWS: eyJhbGciOiJQUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.TRWhwRo5dMv9-8OzrInfJTwmUGYgjLfHk8lqF072ND-FmLWEBnUTOpY8oJXp8FdWw2SalbdOeNlrtlJjwk4XK8Ql2iJ_2qMCtxsvLPhKBOqFoAF4aBvTOEDVJDxf0DaBSiydEEtfTVV2iwBcjWabu5J2XieR5y7QZQtuHsn7T3qKBvCcCejN3Y2oqAT3qMHvu1fTms1r_91wBn_K7Wjd9UkZ1n02qQcUHJznR_OF2BgN7_KWIDAF9ZS9keoju2NPpPelO4yxa2XUPnehY3G7dHKoCxUEQR4d2Xc5voqDASTVCDqQS4PVOZdvT3Ein6-SanAlCwbWBbkvT8g6-5PImQ
// Now load the JWS, validate, and recover the original text.
Get Create (RefClass(cComChilkatJws)) To hoJws2
If (Not(IsComObjectCreated(hoJws2))) Begin
Send CreateComObject of hoJws2
End
// Load the JWS.
Get ComLoadJws Of hoJws2 sJwsCompact To iSuccess
Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey
If (Not(IsComObjectCreated(hoPubKey))) Begin
Send CreateComObject of hoPubKey
End
Get pvComObject of hoPubKey to vPubKey
Get ComGetPublicKey Of hoCert vPubKey To iSuccess
// Set the public key used for validation.
Move 0 To iSignatureIndex
Get pvComObject of hoPubKey to vPubKey
Get ComSetPublicKey Of hoJws2 iSignatureIndex vPubKey To iSuccess
// Validate the 1st (and only) signature at index 0..
Get ComValidate Of hoJws2 iSignatureIndex To v
If (v < 0) Begin
// Perhaps Chilkat was not unlocked or the trial expired..
Showln "Method call failed for some other reason."
Get ComLastErrorText Of hoJws2 To sTemp1
Showln sTemp1
Procedure_Return
End
If (v = 0) Begin
Showln "Invalid signature. The key was incorrect, the JWS was invalid, or both."
Procedure_Return
End
// If we get here, the signature was validated..
Showln "Signature validated."
// Recover the original content:
Get ComGetPayload Of hoJws2 "utf-8" To sTemp1
Showln sTemp1
// Examine the protected header:
Get Create (RefClass(cComChilkatJsonObject)) To hoJoseHeader
If (Not(IsComObjectCreated(hoJoseHeader))) Begin
Send CreateComObject of hoJoseHeader
End
Get pvComObject of hoJoseHeader to vJoseHeader
Get ComGetProtectedH Of hoJws2 iSignatureIndex vJoseHeader To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoJws2 To sTemp1
Showln sTemp1
Procedure_Return
End
Set ComEmitCompact Of hoJoseHeader To False
Showln "Protected (JOSE) header:"
Get ComEmit Of hoJoseHeader To sTemp1
Showln sTemp1
// Output:
// Signature validated.
// In our village, folks say God crumbles up the old moon into stars.
// Protected (JOSE) header:
// {
// "alg": "RS256"
// }
End_Procedure