Skip to content

Commit aedc9b3

Browse files
committed
feat: require a didRotate or didDoc attachment so we can validate signatures
Signed-off-by: KolbyRKunz <[email protected]>
1 parent 79bd5ce commit aedc9b3

File tree

1 file changed

+52
-47
lines changed

1 file changed

+52
-47
lines changed

packages/core/src/modules/connections/DidExchangeProtocol.ts

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -504,10 +504,13 @@ export class DidExchangeProtocol {
504504
message: DidExchangeRequestMessage | DidExchangeResponseMessage,
505505
invitationKeysBase58: string[] = []
506506
) {
507-
// The only supported case where we expect to receive a did-document attachment is did:peer algo 1
508-
return isDid(message.did, 'peer') && getNumAlgoFromPeerDid(message.did) === PeerDidNumAlgo.GenesisDoc
509-
? this.extractAttachedDidDocument(agentContext, message, invitationKeysBase58)
510-
: this.extractResolvableDidDocument(agentContext, message, invitationKeysBase58)
507+
// Not all agents use didRotate yet, some may still send a didDoc attach with various did types
508+
// we should check if the didDoc attach is there and if not require that the didRotate be present
509+
if (message.didDoc) {
510+
return this.extractAttachedDidDocument(agentContext, message, invitationKeysBase58)
511+
} else {
512+
return this.extractResolvableDidDocument(agentContext, message, invitationKeysBase58)
513+
}
511514
}
512515

513516
/**
@@ -522,57 +525,59 @@ export class DidExchangeProtocol {
522525
// Validate did-rotate attachment in case of DID Exchange response
523526
if (message instanceof DidExchangeResponseMessage) {
524527
const didRotateAttachment = message.didRotate
528+
if (!didRotateAttachment) {
529+
throw new DidExchangeProblemReportError(
530+
'Either a DID Rotate attachment or a didDoc attachment must be provided to make a secure connection',
531+
{ problemCode: DidExchangeProblemReportReason.ResponseNotAccepted }
532+
)
533+
}
525534

526-
if (didRotateAttachment) {
527-
const jws = didRotateAttachment.data.jws
535+
const jws = didRotateAttachment.data.jws
528536

529-
if (!jws) {
530-
throw new DidExchangeProblemReportError('DID Rotate signature is missing.', {
531-
problemCode: DidExchangeProblemReportReason.ResponseNotAccepted,
532-
})
533-
}
537+
if (!jws) {
538+
throw new DidExchangeProblemReportError('DID Rotate signature is missing.', {
539+
problemCode: DidExchangeProblemReportReason.ResponseNotAccepted,
540+
})
541+
}
534542

535-
if (!didRotateAttachment.data.base64) {
536-
throw new CredoError('DID Rotate attachment is missing base64 property for signed did.')
537-
}
543+
if (!didRotateAttachment.data.base64) {
544+
throw new CredoError('DID Rotate attachment is missing base64 property for signed did.')
545+
}
538546

539-
// JWS payload must be base64url encoded
540-
const base64UrlPayload = base64ToBase64URL(didRotateAttachment.data.base64)
541-
const signedDid = TypedArrayEncoder.fromBase64(base64UrlPayload).toString()
547+
// JWS payload must be base64url encoded
548+
const base64UrlPayload = base64ToBase64URL(didRotateAttachment.data.base64)
549+
const signedDid = TypedArrayEncoder.fromBase64(base64UrlPayload).toString()
542550

543-
if (signedDid !== message.did) {
544-
throw new CredoError(
545-
`DID Rotate attachment's did ${message.did} does not correspond to message did ${message.did}`
546-
)
547-
}
551+
if (signedDid !== message.did) {
552+
throw new CredoError(
553+
`DID Rotate attachment's did ${message.did} does not correspond to message did ${message.did}`
554+
)
555+
}
548556

549-
const { isValid, signerKeys } = await this.jwsService.verifyJws(agentContext, {
550-
jws: {
551-
...jws,
552-
payload: base64UrlPayload,
553-
},
554-
jwkResolver: ({ jws: { header } }) => {
555-
if (typeof header.kid !== 'string' || !isDid(header.kid, 'key')) {
556-
throw new CredoError('JWS header kid must be a did:key DID.')
557-
}
557+
const { isValid, signerKeys } = await this.jwsService.verifyJws(agentContext, {
558+
jws: {
559+
...jws,
560+
payload: base64UrlPayload,
561+
},
562+
jwkResolver: ({ jws: { header } }) => {
563+
if (typeof header.kid !== 'string' || !isDid(header.kid, 'key')) {
564+
throw new CredoError('JWS header kid must be a did:key DID.')
565+
}
558566

559-
const didKey = DidKey.fromDid(header.kid)
560-
return getJwkFromKey(didKey.key)
561-
},
562-
})
567+
const didKey = DidKey.fromDid(header.kid)
568+
return getJwkFromKey(didKey.key)
569+
},
570+
})
563571

564-
if (!isValid || !signerKeys.every((key) => invitationKeysBase58?.includes(key.publicKeyBase58))) {
565-
throw new DidExchangeProblemReportError(
566-
`DID Rotate signature is invalid. isValid: ${isValid} signerKeys: ${JSON.stringify(
567-
signerKeys
568-
)} invitationKeys:${JSON.stringify(invitationKeysBase58)}`,
569-
{
570-
problemCode: DidExchangeProblemReportReason.ResponseNotAccepted,
571-
}
572-
)
573-
}
574-
} else {
575-
this.logger.warn(`Document does not contain didRotate`)
572+
if (!isValid || !signerKeys.every((key) => invitationKeysBase58?.includes(key.publicKeyBase58))) {
573+
throw new DidExchangeProblemReportError(
574+
`DID Rotate signature is invalid. isValid: ${isValid} signerKeys: ${JSON.stringify(
575+
signerKeys
576+
)} invitationKeys:${JSON.stringify(invitationKeysBase58)}`,
577+
{
578+
problemCode: DidExchangeProblemReportReason.ResponseNotAccepted,
579+
}
580+
)
576581
}
577582
}
578583

0 commit comments

Comments
 (0)