-
Notifications
You must be signed in to change notification settings - Fork 16
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
split signature verification from verifier #2683
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package verifier | ||
|
||
import ( | ||
crypt "crypto" | ||
"errors" | ||
"fmt" | ||
"github.com/nuts-foundation/nuts-node/vcr/credential" | ||
"github.com/nuts-foundation/nuts-node/vcr/types" | ||
"strings" | ||
"time" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
"github.com/nuts-foundation/go-did/vc" | ||
"github.com/nuts-foundation/nuts-node/crypto" | ||
"github.com/nuts-foundation/nuts-node/jsonld" | ||
"github.com/nuts-foundation/nuts-node/vcr/issuer" | ||
"github.com/nuts-foundation/nuts-node/vcr/signature" | ||
"github.com/nuts-foundation/nuts-node/vcr/signature/proof" | ||
"github.com/nuts-foundation/nuts-node/vdr/resolver" | ||
) | ||
|
||
type signatureVerifier struct { | ||
keyResolver resolver.KeyResolver | ||
jsonldManager jsonld.JSONLD | ||
} | ||
|
||
// VerifySignature checks if the signature on a VP is valid at a given time | ||
func (sv *signatureVerifier) VerifySignature(credentialToVerify vc.VerifiableCredential, validateAt *time.Time) error { | ||
switch credentialToVerify.Format() { | ||
case issuer.JSONLDCredentialFormat: | ||
return sv.jsonldProof(credentialToVerify, credentialToVerify.Issuer.String(), validateAt) | ||
case issuer.JWTCredentialFormat: | ||
return sv.jwtSignature(credentialToVerify.Raw(), credentialToVerify.Issuer.String(), validateAt) | ||
default: | ||
return errors.New("unsupported credential proof format") | ||
} | ||
} | ||
|
||
// VerifyVPSignature checks if the signature on a VP is valid at a given time | ||
func (sv *signatureVerifier) VerifyVPSignature(presentation vc.VerifiablePresentation, validateAt *time.Time) error { | ||
signerDID, err := credential.PresentationSigner(presentation) | ||
if err != nil { | ||
return toVerificationError(err) | ||
} | ||
|
||
switch presentation.Format() { | ||
case issuer.JSONLDPresentationFormat: | ||
return sv.jsonldProof(presentation, signerDID.String(), validateAt) | ||
case issuer.JWTPresentationFormat: | ||
return sv.jwtSignature(presentation.Raw(), signerDID.String(), validateAt) | ||
default: | ||
return errors.New("unsupported presentation proof format") | ||
} | ||
} | ||
|
||
// jsonldProof implements the Proof Verification Algorithm: https://w3c-ccg.github.io/data-integrity-spec/#proof-verification-algorithm | ||
func (sv *signatureVerifier) jsonldProof(documentToVerify any, issuer string, at *time.Time) error { | ||
signedDocument, err := proof.NewSignedDocument(documentToVerify) | ||
if err != nil { | ||
return newVerificationError("invalid LD-JSON document: %w", err) | ||
} | ||
|
||
ldProof := proof.LDProof{} | ||
if err = signedDocument.UnmarshalProofValue(&ldProof); err != nil { | ||
return newVerificationError("unsupported proof type: %w", err) | ||
} | ||
|
||
// for a VP this will not fail | ||
verificationMethod := ldProof.VerificationMethod.String() | ||
verificationMethodIssuer := strings.Split(verificationMethod, "#")[0] | ||
if verificationMethodIssuer == "" || verificationMethodIssuer != issuer { | ||
return errVerificationMethodNotOfIssuer | ||
} | ||
|
||
// verify signing time | ||
validAt := time.Now() | ||
if at != nil { | ||
validAt = *at | ||
} | ||
if !ldProof.ValidAt(validAt, maxSkew) { | ||
return toVerificationError(types.ErrPresentationNotValidAtTime) | ||
} | ||
|
||
// find key | ||
signingKey, err := sv.keyResolver.ResolveKeyByID(ldProof.VerificationMethod.String(), at, resolver.NutsSigningKeyType) | ||
if err != nil { | ||
return fmt.Errorf("unable to resolve valid signing key: %w", err) | ||
} | ||
|
||
// verify signature | ||
err = ldProof.Verify(signedDocument.DocumentWithoutProof(), signature.JSONWebSignature2020{ContextLoader: sv.jsonldManager.DocumentLoader()}, signingKey) | ||
if err != nil { | ||
return newVerificationError("invalid signature: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (sv *signatureVerifier) jwtSignature(jwtDocumentToVerify string, issuer string, at *time.Time) error { | ||
var keyID string | ||
_, err := crypto.ParseJWT(jwtDocumentToVerify, func(kid string) (crypt.PublicKey, error) { | ||
keyID = kid | ||
return sv.resolveSigningKey(kid, issuer, at) | ||
}, jwt.WithClock(jwt.ClockFunc(func() time.Time { | ||
if at == nil { | ||
return time.Now() | ||
} | ||
return *at | ||
}))) | ||
if err != nil { | ||
return fmt.Errorf("unable to validate JWT signature: %w", err) | ||
} | ||
if keyID != "" && strings.Split(keyID, "#")[0] != issuer { | ||
return errVerificationMethodNotOfIssuer | ||
} | ||
return nil | ||
} | ||
|
||
func (sv *signatureVerifier) resolveSigningKey(kid string, issuer string, at *time.Time) (crypt.PublicKey, error) { | ||
// Compatibility: VC data model v1 puts key discovery out of scope and does not require the `kid` header. | ||
// When `kid` isn't present use the JWT issuer as `kid`, then it is at least compatible with DID methods that contain a single verification method (did:jwk). | ||
if kid == "" { | ||
kid = issuer | ||
} | ||
if strings.HasPrefix(kid, "did:jwk:") && !strings.Contains(kid, "#") { | ||
kid += "#0" | ||
} | ||
return sv.keyResolver.ResolveKeyByID(kid, at, resolver.NutsSigningKeyType) | ||
} |
Oops, something went wrong.
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.
I realized clock skew is missing here..
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.
deferred to #2684