This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Updating to OID4VP_1_0_20 #78
Draft
sksadjad
wants to merge
4
commits into
develop
Choose a base branch
from
feature/CWALL-175_update_to_oid4vp_1_0_20
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.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.vscode/* | ||
.idea/* | ||
.tsimp/* | ||
*.iml | ||
.nyc_output | ||
build | ||
|
This file contains 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 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 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { JWTVerifyOptions } from 'did-jwt'; | ||
import { decodeJWT } from 'did-jwt'; | ||
import { JWTDecoded } from 'did-jwt/lib/JWT'; | ||
import forge from 'node-forge'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cannot work. This library is used in browser, RN and Node. The lib is node only |
||
|
||
import { PresentationDefinitionWithLocation } from '../authorization-response'; | ||
import { PresentationExchange } from '../authorization-response/PresentationExchange'; | ||
|
@@ -186,11 +189,15 @@ | |
throw new Error(`${SIOPErrors.INVALID_REQUEST}, redirect_uri or response_uri is needed`); | ||
} | ||
|
||
if (mergedPayload.client_id_scheme === 'verifier_attestation') { | ||
verifiedJwt = await AuthorizationRequest.verifyAttestationJWT(jwt, mergedPayload.client_id); | ||
} else if (mergedPayload.client_id_scheme === 'x509_san_dns') { | ||
await this.checkX509SanDNSScheme(jwt, mergedPayload.client_id); | ||
} else if (mergedPayload.client_id_scheme === 'x509_san_uri') { | ||
throw new Error(SIOPErrors.VERIFICATION_X509_SAN_URI_SCHEME_NOT_IMPLEMENTED_ERROR); | ||
} | ||
await checkWellknownDIDFromRequest(mergedPayload, opts); | ||
|
||
// TODO: we need to verify somewhere that if response_mode is direct_post, that the response_uri may be present, | ||
// BUT not both redirect_uri and response_uri. What is the best place to do this? | ||
|
||
const presentationDefinitions = await PresentationExchange.findValidPresentationDefinitions(mergedPayload, await this.getSupportedVersion()); | ||
return { | ||
...verifiedJwt, | ||
|
@@ -248,6 +255,97 @@ | |
}; | ||
} | ||
|
||
/** | ||
* Verifies a JWT according to the 'verifier_attestation' client_id_scheme, where the JWT must be | ||
* signed with a private key corresponding to the public key specified within the JWT itself. This method | ||
* ensures that the JWT's 'sub' claim matches the provided clientId, and it extracts and validates the | ||
* public key from the JWT's 'cnf' (confirmation) claim, which must contain a JWK. | ||
* | ||
* An example of such request would be: | ||
* GET /authorize? | ||
* response_type=vp_token | ||
* &client_id=https%3A%2F%2Fverifier.example.org | ||
* &client_id_scheme=verifier_attestation | ||
* &redirect_uri=https%3A%2F%2Fclient.example.org%2Fcb | ||
* &presentation_definition=... | ||
* &nonce=n-0S6_WzA2Mj | ||
* &jwt=eyJ...abc | ||
* | ||
* @param jwt The JSON Web Token string to be verified. It is expected that this JWT is formatted correctly | ||
* and includes a 'cnf' claim with a JWK representing the public key used for signing the JWT. | ||
* @param clientId The client identifier expected to match the 'sub' claim in the JWT. This is used to | ||
* validate that the JWT is intended for the correct recipient/client. | ||
*/ | ||
private static async verifyAttestationJWT(jwt: string, clientId: string): Promise<VerifiedJWT> { | ||
if (!jwt) { | ||
throw new Error(SIOPErrors.NO_JWT); | ||
} | ||
const payload = decodeJWT(jwt); | ||
AuthorizationRequest.checkPayloadClaims(payload, ['iss', 'sub', 'exp', 'cnf']); | ||
const sub = payload['sub']; | ||
const cnf = payload['cnf']; | ||
|
||
if (sub !== clientId || !cnf || typeof cnf !== 'object' || !cnf['jwk'] || typeof cnf['jwk'] !== 'object') { | ||
throw new Error(SIOPErrors.VERIFICATION_VERIFIER_ATTESTATION_SCHEME_ERROR); | ||
} | ||
|
||
return { | ||
jwt, | ||
payload: payload.payload, | ||
issuer: payload['iss'], | ||
jwk: cnf['jwk'], | ||
}; | ||
} | ||
|
||
/** | ||
* verifying JWTs against X.509 certificates focusing on DNS SAN compliance, which is crucial for environments where certificate-based security is pivotal. | ||
* | ||
* An example of such request would be: | ||
* GET /authorize? | ||
* response_type=vp_token | ||
* &client_id=client.example.org | ||
* &client_id_scheme=x509_san_dns | ||
* &redirect_uri=https%3A%2F%2Fclient.example.org%2Fcb | ||
* &presentation_definition=... | ||
* &nonce=n-0S6_WzA2Mj | ||
* | ||
* @param jwt The encoded JWT from which the certificate needs to be extracted. | ||
* @param clientId The DNS name to match against the certificate's SANs. | ||
*/ | ||
private async checkX509SanDNSScheme(jwt: string, clientId: string): Promise<void> { | ||
const jwtDecoded: JWTDecoded = decodeJWT(jwt); | ||
const x5c = jwtDecoded.header['x5c']; | ||
|
||
if (x5c == null || !Array.isArray(x5c) || x5c.length === 0) { | ||
throw new Error(SIOPErrors.VERIFICATION_X509_SAN_DNS_SCHEME_ERROR); | ||
} | ||
|
||
const certificate = x5c[0]; | ||
if (!certificate) { | ||
throw new Error(SIOPErrors.VERIFICATION_X509_SAN_DNS_SCHEME_NO_CERTIFICATE_ERROR); | ||
} | ||
|
||
const der = forge.util.decode64(certificate); | ||
const asn1 = forge.asn1.fromDer(der); | ||
const cert = forge.pki.certificateFromAsn1(asn1); | ||
|
||
const subjectAltNames = cert.getExtension('subjectAltName'); | ||
if (!subjectAltNames || !Array.isArray(subjectAltNames['altNames'])) { | ||
throw new Error(SIOPErrors.VERIFICATION_X509_SAN_DNS_ALT_NAMES_ERROR); | ||
} | ||
if (!subjectAltNames || !subjectAltNames['altNames'].some((name: any) => name.value === clientId)) { | ||
Check warning on line 336 in src/authorization-request/AuthorizationRequest.ts GitHub Actions / build
|
||
throw new Error(SIOPErrors.VERIFICATION_X509_SAN_DNS_SCHEME_DNS_NAME_MATCH); | ||
} | ||
} | ||
|
||
private static checkPayloadClaims(payload: JWTDecoded, requiredClaims: string[]): void { | ||
requiredClaims.forEach((claim) => { | ||
if (payload[claim] === undefined) { | ||
throw new Error(`Payload is missing ${claim}`); | ||
} | ||
}); | ||
} | ||
|
||
public async containsResponseType(singleType: ResponseType | string): Promise<boolean> { | ||
const responseType: string = await this.getMergedProperty('response_type'); | ||
return responseType?.includes(singleType) === true; | ||
|
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { AuthorizationRequestPayloadVD11Schema, AuthorizationRequestPayloadVID1Schema } from '../schemas'; | ||
import { AuthorizationRequestPayloadVD12OID4VPD18Schema } from '../schemas/validation/schemaValidation'; | ||
import { AuthorizationRequestPayloadVD13OID4VPD20Schema } from '../schemas/validation/schemaValidation'; | ||
import { AuthorizationRequestPayload, ResponseMode, SupportedVersion } from '../types'; | ||
import errors from '../types/Errors'; | ||
|
||
|
@@ -34,15 +34,15 @@ export const authorizationRequestVersionDiscovery = (authorizationRequest: Autho | |
const versions = []; | ||
const authorizationRequestCopy: AuthorizationRequestPayload = JSON.parse(JSON.stringify(authorizationRequest)); | ||
// todo: We could use v11 validation for v12 for now, as we do not differentiate in the schema at this point\ | ||
const vd12Validation = AuthorizationRequestPayloadVD12OID4VPD18Schema(authorizationRequestCopy); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't remove older versions |
||
if (vd12Validation) { | ||
const vd13Validation = AuthorizationRequestPayloadVD13OID4VPD20Schema(authorizationRequestCopy); | ||
if (vd13Validation) { | ||
if ( | ||
!authorizationRequestCopy.registration_uri && | ||
!authorizationRequestCopy.registration && | ||
!(authorizationRequestCopy.claims && 'vp_token' in authorizationRequestCopy.claims) && | ||
authorizationRequestCopy.response_mode !== ResponseMode.POST // Post has been replaced by direct post | ||
) { | ||
versions.push(SupportedVersion.SIOPv2_D12_OID4VP_D18); | ||
versions.push(SupportedVersion.SIOPv2_D13_OID4VP_D20); | ||
} | ||
} | ||
const vd11Validation = AuthorizationRequestPayloadVD11Schema(authorizationRequestCopy); | ||
|
This file contains 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 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 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 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 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 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we removing v18?