-
Notifications
You must be signed in to change notification settings - Fork 22
[INJIVER-1369] - Add verification and validation for cwt formet VC with new library #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jaswanthkumartw
wants to merge
11
commits into
inji:develop
Choose a base branch
from
tw-mosip:injiver-1369-new
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9ad877b
[INJIVER-1369] - add verification and validation for cwt formet
jaswanthkumartw dbc85e2
[INJIVER-1369] - add test and clear the issues regarding the build
jaswanthkumartw ba7a8a2
[INJIVER-1369] - update the cwt verifer with new library
jaswanthkumartw 5392095
[INJIVER-1369] - update the verifier method and test with cwt
jaswanthkumartw 724202a
[INJIVER-1369] - clear the URI issue with http
jaswanthkumartw 1de3d02
[INJIVER-1369] - clear coderabbit comments
jaswanthkumartw c967142
[INJIVER-1369] - clear coderabbit comments
jaswanthkumartw a6891f9
[INJIVER-1369] Add duplicate-claims logic and HTTPS VC public key ext…
jaswanthkumartw 6bd957b
[INJIVER-1369] - clear coderabbit comments
jaswanthkumartw 4a8e8b0
[INJIVER-1369] - clear coderabbit comments
jaswanthkumartw c6574d7
[INJIVER-1369] - clear coderabbit comments
jaswanthkumartw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...kotlin/vcverifier/src/main/java/io/mosip/vercred/vcverifier/constants/CredentialFormat.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...main/java/io/mosip/vercred/vcverifier/credentialverifier/types/CwtVerifiableCredential.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package io.mosip.vercred.vcverifier.credentialverifier.types | ||
|
|
||
| import io.mosip.vercred.vcverifier.credentialverifier.VerifiableCredential | ||
| import io.mosip.vercred.vcverifier.credentialverifier.validator.CwtValidator | ||
| import io.mosip.vercred.vcverifier.credentialverifier.verifier.CwtVerifier | ||
| import io.mosip.vercred.vcverifier.data.CredentialStatusResult | ||
| import io.mosip.vercred.vcverifier.data.ValidationStatus | ||
|
|
||
| class CwtVerifiableCredential: VerifiableCredential { | ||
| override fun validate(credential: String): ValidationStatus { | ||
| return CwtValidator().validate(credential) | ||
| } | ||
|
|
||
| override fun verify(credential: String): Boolean { | ||
| return CwtVerifier().verify(credential); | ||
| } | ||
|
|
||
| override fun checkStatus(credential: String, statusPurposes: List<String>?): Map<String, CredentialStatusResult> { | ||
| return emptyMap(); | ||
| } | ||
| } |
210 changes: 210 additions & 0 deletions
210
...er/src/main/java/io/mosip/vercred/vcverifier/credentialverifier/validator/CwtValidator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| package io.mosip.vercred.vcverifier.credentialverifier.validator | ||
|
|
||
| import com.upokecenter.cbor.CBOREncodeOptions | ||
| import com.upokecenter.cbor.CBORObject | ||
| import com.upokecenter.cbor.CBORType | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_CURRENT_DATE_BEFORE_PROCESSING_DATE | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_GENERIC | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_INVALID | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_MISSING | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CODE_VC_EXPIRED | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_CURRENT_DATE_BEFORE_PROCESSING_DATE | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_INVALID_FIELD | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_EMPTY_VC_CWT | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_VC_EXPIRED | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.EXCEPTION_DURING_VALIDATION | ||
| import io.mosip.vercred.vcverifier.constants.CredentialValidatorConstants.ERROR_MESSAGE_INVALID_HEX_VC_CWT | ||
| import io.mosip.vercred.vcverifier.data.ValidationStatus | ||
|
|
||
| import io.mosip.vercred.vcverifier.exception.ValidationException | ||
| import io.mosip.vercred.vcverifier.utils.Util.hexToBytes | ||
| import io.mosip.vercred.vcverifier.utils.Util.validateNumericDate | ||
|
|
||
| class CwtValidator { | ||
|
|
||
| fun validate(credential: String): ValidationStatus { | ||
| try { | ||
| if (credential.isEmpty()) { | ||
| throw ValidationException( | ||
| ERROR_MESSAGE_EMPTY_VC_CWT, | ||
| ERROR_CODE_INVALID + "EMPTY" | ||
| ) | ||
| } | ||
|
|
||
| if (!isValidHex(credential)) { | ||
| throw ValidationException( | ||
| ERROR_MESSAGE_INVALID_HEX_VC_CWT, | ||
| ERROR_CODE_INVALID + "HEX" | ||
| ) | ||
| } | ||
|
|
||
| val coseObj = decodeCose(credential) | ||
|
|
||
| validateCoseStructure(coseObj) | ||
|
|
||
| val protectedHeader = decodeProtectedHeader(coseObj) | ||
| validateProtectedHeader(protectedHeader) | ||
|
|
||
| val claims = decodeCwtClaims(coseObj) | ||
| validateCwtStructure(claims) | ||
| validateNumericDates(claims) | ||
| return ValidationStatus("", "") | ||
| } catch (e: ValidationException) { | ||
| return ValidationStatus(e.errorMessage, e.errorCode) | ||
| } catch (e: Exception) { | ||
| return ValidationStatus( | ||
| "${EXCEPTION_DURING_VALIDATION}${e.message}", | ||
| ERROR_CODE_GENERIC | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private fun validateCoseStructure(coseObj: CBORObject) { | ||
|
|
||
| if (coseObj.type != CBORType.Array) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "COSE_Sign1 must be a CBOR array", | ||
| ERROR_CODE_INVALID + "COSE_STRUCTURE" | ||
| ) | ||
| } | ||
|
|
||
| if (coseObj.size() != 4) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "COSE_Sign1 must have exactly 4 elements", | ||
| ERROR_CODE_INVALID + "COSE_STRUCTURE" | ||
| ) | ||
| } | ||
|
|
||
| // Index 0: Protected header | ||
| if (coseObj[0].type != CBORType.ByteString) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Protected header must be a CBOR byte string (bstr)", | ||
| ERROR_CODE_INVALID + "PROTECTED_HEADER" | ||
| ) | ||
| } | ||
|
|
||
| // Index 1: Unprotected header | ||
| if (coseObj[1].type != CBORType.Map) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Unprotected header must be a CBOR map", | ||
| ERROR_CODE_INVALID + "UNPROTECTED_HEADER" | ||
| ) | ||
| } | ||
|
|
||
| // Index 2: Payload (The CWT Claims) | ||
| if (coseObj[2].type != CBORType.ByteString) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Payload must be a CBOR byte string (bstr)", | ||
| ERROR_CODE_INVALID + "PAYLOAD" | ||
| ) | ||
| } | ||
|
|
||
| // Index 3: Signature | ||
| if (coseObj[3].type != CBORType.ByteString) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Signature must be a CBOR byte string (bstr)", | ||
| ERROR_CODE_INVALID + "SIGNATURE" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private fun validateProtectedHeader(protectedHeader: CBORObject) { | ||
|
|
||
| if (protectedHeader.type != CBORType.Map) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Protected header must decode to a CBOR map", | ||
| ERROR_CODE_INVALID + "PROTECTED_HEADER" | ||
| ) | ||
| } | ||
|
|
||
| val ALG = CBORObject.FromObject(1) | ||
|
|
||
| if (!protectedHeader.ContainsKey(ALG)) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "Missing alg in protected header", | ||
| ERROR_CODE_MISSING + "ALG" | ||
| ) | ||
| } | ||
|
|
||
| if (!protectedHeader[ALG].isNumber) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "alg must be an integer", | ||
| ERROR_CODE_INVALID + "ALG" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private fun validateCwtStructure(claims: CBORObject) { | ||
|
|
||
| if (claims.type != CBORType.Map) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "CWT payload must be a CBOR map", | ||
| ERROR_CODE_INVALID + "CWT_STRUCTURE" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private fun validateNumericDates(claims: CBORObject) { | ||
|
|
||
| val EXP = CBORObject.FromObject(4) | ||
| val NBF = CBORObject.FromObject(5) | ||
| val IAT = CBORObject.FromObject(6) | ||
|
|
||
| val now = System.currentTimeMillis() / 1000 | ||
|
|
||
| val exp = validateNumericDate(claims, EXP, "exp") | ||
| val nbf = validateNumericDate(claims, NBF, "nbf") | ||
| val iat = validateNumericDate(claims, IAT, "iat") | ||
|
|
||
| // Expired Check | ||
| if (exp != null && exp <= now) { | ||
| throw ValidationException( | ||
| ERROR_MESSAGE_VC_EXPIRED + " (exp=$exp, now=$now)", | ||
| ERROR_CODE_VC_EXPIRED | ||
| ) | ||
| } | ||
|
|
||
| // Not Before Check | ||
| if (nbf != null && nbf > now) { | ||
| throw ValidationException( | ||
| ERROR_CURRENT_DATE_BEFORE_PROCESSING_DATE + " (nbf=$nbf, now=$now)", | ||
| ERROR_CODE_CURRENT_DATE_BEFORE_PROCESSING_DATE | ||
| ) | ||
| } | ||
|
|
||
| // Issued At Check | ||
| if (iat != null && iat > now) { | ||
| throw ValidationException( | ||
| ERROR_INVALID_FIELD + "CWT issued in the future (iat=$iat, now=$now)", | ||
| ERROR_CODE_INVALID + "IAT" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| private fun decodeCose(credential: String): CBORObject { | ||
| val bytes = hexToBytes(credential) | ||
| return CBORObject.DecodeFromBytes(bytes) | ||
| } | ||
|
|
||
| private fun decodeProtectedHeader(coseObj: CBORObject): CBORObject { | ||
| val bytes = coseObj[0].GetByteString() | ||
| return CBORObject.DecodeFromBytes(bytes) | ||
| } | ||
|
|
||
| private fun decodeCwtClaims(coseObj: CBORObject): CBORObject { | ||
| val payloadBytes = coseObj[2].GetByteString() | ||
| return CBORObject.DecodeFromBytes(payloadBytes, CBOREncodeOptions("allowduplicatekeys=false")) | ||
| } | ||
|
|
||
| private fun isValidHex(credential: String): Boolean { | ||
| return credential.length % 2 == 0 && | ||
| credential.matches(Regex("^[0-9a-fA-F]+$")) | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.