Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
fix: Presentation Without Holder Binding should end with ~ (#3468)
Browse files Browse the repository at this point in the history
Fix: Presentation Without Holder Binding should end with ~

Also, rename SDJWT to CombinedPresentation and JWTSerialized to SDJWT to follow spec terms.

Closes #3467

Signed-off-by: Sandra Vrtikapa <[email protected]>

Signed-off-by: Sandra Vrtikapa <[email protected]>
  • Loading branch information
sandrask authored Jan 10, 2023
1 parent e6c87c5 commit c8f690f
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 112 deletions.
70 changes: 61 additions & 9 deletions pkg/doc/sdjwt/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,43 @@ type Payload struct {
SDAlg string `json:"_sd_alg,omitempty"`
}

// SDJWT holds SD-JWT info.
type SDJWT struct {
JWTSerialized string
// CombinedFormatForIssuance holds SD-JWT and disclosures.
type CombinedFormatForIssuance struct {
SDJWT string
Disclosures []string
}

// Serialize will assemble combined format for issuance.
func (cf *CombinedFormatForIssuance) Serialize() string {
presentation := cf.SDJWT
for _, disclosure := range cf.Disclosures {
presentation += DisclosureSeparator + disclosure
}

return presentation
}

// CombinedFormatForPresentation holds SD-JWT, disclosures and optional holder binding info.
type CombinedFormatForPresentation struct {
SDJWT string
Disclosures []string
HolderBinding string
}

// Serialize will assemble combined format for presentation.
func (cf *CombinedFormatForPresentation) Serialize() string {
presentation := cf.SDJWT
for _, disclosure := range cf.Disclosures {
presentation += DisclosureSeparator + disclosure
}

if len(cf.Disclosures) > 0 || cf.HolderBinding != "" {
presentation += DisclosureSeparator
}

presentation += cf.HolderBinding

return presentation
}

// DisclosureClaim defines claim.
Expand Down Expand Up @@ -107,18 +140,37 @@ func getDisclosureClaim(disclosure string) (*DisclosureClaim, error) {
return claim, nil
}

// ParseSDJWT parses SD-JWT serialized token into SDJWT parts.
func ParseSDJWT(sdJWTSerialized string) *SDJWT {
parts := strings.Split(sdJWTSerialized, DisclosureSeparator)
// ParseCombinedFormatForIssuance parses combined format for issuance into CombinedFormatForIssuance parts.
func ParseCombinedFormatForIssuance(combinedFormatForIssuance string) *CombinedFormatForIssuance {
parts := strings.Split(combinedFormatForIssuance, DisclosureSeparator)

var disclosures []string
if len(parts) > 1 {
disclosures = parts[1:]
}

jwtSerialized := parts[0]
sdJWT := parts[0]

return &CombinedFormatForIssuance{SDJWT: sdJWT, Disclosures: disclosures}
}

// ParseCombinedFormatForPresentation parses combined format for presentation into CombinedFormatForPresentation parts.
func ParseCombinedFormatForPresentation(combinedFormatForPresentation string) *CombinedFormatForPresentation {
parts := strings.Split(combinedFormatForPresentation, DisclosureSeparator)

var disclosures []string
if len(parts) > 2 {
disclosures = parts[1 : len(parts)-1]
}

var holderBinding string
if len(parts) > 1 {
holderBinding = parts[len(parts)-1]
}

sdJWT := parts[0]

return &SDJWT{JWTSerialized: jwtSerialized, Disclosures: disclosures}
return &CombinedFormatForPresentation{SDJWT: sdJWT, Disclosures: disclosures, HolderBinding: holderBinding}
}

// GetHash calculates hash of data using hash function identified by hash.
Expand Down Expand Up @@ -207,7 +259,7 @@ func getSDAlg(claims map[string]interface{}) (string, error) {
return str, nil
}

// GetDisclosureDigests returns digests from from claims map.
// GetDisclosureDigests returns digests from claims map.
func GetDisclosureDigests(claims map[string]interface{}) (map[string]bool, error) {
disclosuresObj, ok := claims[SDKey]
if !ok {
Expand Down
102 changes: 85 additions & 17 deletions pkg/doc/sdjwt/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,79 @@ func TestGetHash(t *testing.T) {
})
}

func TestParseSDJWT(t *testing.T) {
t.Run("success", func(t *testing.T) {
sdJWT := ParseSDJWT(sdJWT)
require.Equal(t, 1, len(sdJWT.Disclosures))
func TestParseCombinedFormatForIssuance(t *testing.T) {
t.Run("success - SD-JWT only", func(t *testing.T) {
cfi := ParseCombinedFormatForIssuance(testCombinedFormatForIssuance)
require.Equal(t, testSDJWT, cfi.SDJWT)
require.Equal(t, 1, len(cfi.Disclosures))

require.Equal(t, testCombinedFormatForIssuance, cfi.Serialize())
})
t.Run("success", func(t *testing.T) {
sdJWT := ParseSDJWT(specSDJWT)
require.Equal(t, 7, len(sdJWT.Disclosures))
t.Run("success - spec example", func(t *testing.T) {
cfi := ParseCombinedFormatForIssuance(specCombinedFormatForIssuance)
require.Equal(t, 7, len(cfi.Disclosures))

require.Equal(t, specCombinedFormatForIssuance, cfi.Serialize())
})
t.Run("success - AFG generated", func(t *testing.T) {
cfi := ParseCombinedFormatForIssuance(testSDJWT)
require.Equal(t, testSDJWT, cfi.SDJWT)
require.Equal(t, 0, len(cfi.Disclosures))

require.Equal(t, testSDJWT, cfi.Serialize())
})
}

func TestParseCombinedFormatForPresentation(t *testing.T) {
const testHolderBinding = "holder.binding.jwt"

testCombinedFormatForPresentation := testCombinedFormatForIssuance + DisclosureSeparator

t.Run("success - AFG example", func(t *testing.T) {
cfp := ParseCombinedFormatForPresentation(testCombinedFormatForPresentation)
require.Equal(t, testSDJWT, cfp.SDJWT)
require.Equal(t, 1, len(cfp.Disclosures))
require.Empty(t, cfp.HolderBinding)

require.Equal(t, testCombinedFormatForPresentation, cfp.Serialize())
})

t.Run("success - spec example", func(t *testing.T) {
cfp := ParseCombinedFormatForPresentation(specCombinedFormatForIssuance + DisclosureSeparator)
require.Equal(t, 7, len(cfp.Disclosures))
require.Empty(t, cfp.HolderBinding)

require.Equal(t, specCombinedFormatForIssuance+DisclosureSeparator, cfp.Serialize())
})

t.Run("success - AFG test with holder binding", func(t *testing.T) {
testCFI := testCombinedFormatForPresentation + testHolderBinding
cfp := ParseCombinedFormatForPresentation(testCFI)
require.Equal(t, testSDJWT, cfp.SDJWT)
require.Equal(t, 1, len(cfp.Disclosures))
require.Equal(t, testHolderBinding, cfp.HolderBinding)

require.Equal(t, testCFI, cfp.Serialize())
})

t.Run("success - SD-JWT only", func(t *testing.T) {
cfp := ParseCombinedFormatForPresentation(testSDJWT)
require.Equal(t, testSDJWT, cfp.SDJWT)
require.Equal(t, 0, len(cfp.Disclosures))
require.Empty(t, cfp.HolderBinding)

require.Equal(t, testSDJWT, cfp.Serialize())
})

t.Run("success - SD-JWT + holder binding", func(t *testing.T) {
testCFI := testSDJWT + DisclosureSeparator + testHolderBinding

cfp := ParseCombinedFormatForPresentation(testCFI)
require.Equal(t, testSDJWT, cfp.SDJWT)
require.Equal(t, 0, len(cfp.Disclosures))
require.Equal(t, testHolderBinding, cfp.HolderBinding)

require.Equal(t, testCFI, cfp.Serialize())
})
}

Expand All @@ -62,10 +127,10 @@ func TestVerifyDisclosuresInSDJWT(t *testing.T) {
signer := afjwt.NewEd25519Signer(privKey)

t.Run("success", func(t *testing.T) {
sdJWT := ParseSDJWT(sdJWT)
sdJWT := ParseCombinedFormatForIssuance(testCombinedFormatForIssuance)
require.Equal(t, 1, len(sdJWT.Disclosures))

signedJWT, err := afjwt.Parse(sdJWT.JWTSerialized, afjwt.WithSignatureVerifier(&NoopSignatureVerifier{}))
signedJWT, err := afjwt.Parse(sdJWT.SDJWT, afjwt.WithSignatureVerifier(&NoopSignatureVerifier{}))
require.NoError(t, err)

err = VerifyDisclosuresInSDJWT(sdJWT.Disclosures, signedJWT)
Expand Down Expand Up @@ -98,10 +163,10 @@ func TestVerifyDisclosuresInSDJWT(t *testing.T) {
})

t.Run("error - disclosure not present in SD-JWT", func(t *testing.T) {
sdJWT := ParseSDJWT(sdJWT)
sdJWT := ParseCombinedFormatForIssuance(testCombinedFormatForIssuance)
require.Equal(t, 1, len(sdJWT.Disclosures))

signedJWT, err := afjwt.Parse(sdJWT.JWTSerialized, afjwt.WithSignatureVerifier(&NoopSignatureVerifier{}))
signedJWT, err := afjwt.Parse(sdJWT.SDJWT, afjwt.WithSignatureVerifier(&NoopSignatureVerifier{}))
require.NoError(t, err)

err = VerifyDisclosuresInSDJWT(append(sdJWT.Disclosures, additionalDisclosure), signedJWT)
Expand Down Expand Up @@ -195,7 +260,7 @@ func TestGetDisclosureClaims(t *testing.T) {
r := require.New(t)

t.Run("success", func(t *testing.T) {
sdJWT := ParseSDJWT(sdJWT)
sdJWT := ParseCombinedFormatForIssuance(testCombinedFormatForIssuance)
require.Equal(t, 1, len(sdJWT.Disclosures))

disclosureClaims, err := GetDisclosureClaims(sdJWT.Disclosures)
Expand All @@ -207,7 +272,7 @@ func TestGetDisclosureClaims(t *testing.T) {
})

t.Run("error - invalid disclosure format (not encoded)", func(t *testing.T) {
sdJWT := ParseSDJWT("jws~xyz")
sdJWT := ParseCombinedFormatForIssuance("jws~xyz")
require.Equal(t, 1, len(sdJWT.Disclosures))

disclosureClaims, err := GetDisclosureClaims(sdJWT.Disclosures)
Expand All @@ -221,7 +286,7 @@ func TestGetDisclosureClaims(t *testing.T) {
disclosureJSON, err := json.Marshal(disclosureArr)
require.NoError(t, err)

sdJWT := ParseSDJWT(fmt.Sprintf("jws~%s", base64.RawURLEncoding.EncodeToString(disclosureJSON)))
sdJWT := ParseCombinedFormatForIssuance(fmt.Sprintf("jws~%s", base64.RawURLEncoding.EncodeToString(disclosureJSON)))
require.Equal(t, 1, len(sdJWT.Disclosures))

disclosureClaims, err := GetDisclosureClaims(sdJWT.Disclosures)
Expand All @@ -235,7 +300,7 @@ func TestGetDisclosureClaims(t *testing.T) {
disclosureJSON, err := json.Marshal(disclosureArr)
require.NoError(t, err)

sdJWT := ParseSDJWT(fmt.Sprintf("jws~%s", base64.RawURLEncoding.EncodeToString(disclosureJSON)))
sdJWT := ParseCombinedFormatForIssuance(fmt.Sprintf("jws~%s", base64.RawURLEncoding.EncodeToString(disclosureJSON)))
require.Equal(t, 1, len(sdJWT.Disclosures))

disclosureClaims, err := GetDisclosureClaims(sdJWT.Disclosures)
Expand All @@ -255,7 +320,10 @@ func (sv *NoopSignatureVerifier) Verify(joseHeaders jose.Headers, payload, signi
const additionalDisclosure = `WyJfMjZiYzRMVC1hYzZxMktJNmNCVzVlcyIsICJmYW1pbHlfbmFtZSIsICJNw7ZiaXVzIl0`

// nolint: lll
const sdJWT = `eyJhbGci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~WyJFUktNMENOZUZKa2FENW1UWFZfWDh3IiwgImJpcnRoZGF0ZSIsICIxOTQwLTAxLTAxIl0`
28 changes: 14 additions & 14 deletions pkg/doc/sdjwt/holder/holder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func WithSignatureVerifier(signatureVerifier jose.SignatureVerifier) ParseOpt {
}

// Parse parses issuer SD-JWT and returns claims that can be selected.
func Parse(sdJWTSerialized string, opts ...ParseOpt) ([]*Claim, error) {
func Parse(combinedFormatForIssuance string, opts ...ParseOpt) ([]*Claim, error) {
pOpts := &parseOpts{
sigVerifier: &NoopSignatureVerifier{},
}
Expand All @@ -60,19 +60,19 @@ func Parse(sdJWTSerialized string, opts ...ParseOpt) ([]*Claim, error) {
afgjwt.WithSignatureVerifier(pOpts.sigVerifier),
afgjwt.WithJWTDetachedPayload(pOpts.detachedPayload))

sdJWT := common.ParseSDJWT(sdJWTSerialized)
cfi := common.ParseCombinedFormatForIssuance(combinedFormatForIssuance)

signedJWT, err := afgjwt.Parse(sdJWT.JWTSerialized, jwtOpts...)
signedJWT, err := afgjwt.Parse(cfi.SDJWT, jwtOpts...)
if err != nil {
return nil, err
}

err = common.VerifyDisclosuresInSDJWT(sdJWT.Disclosures, signedJWT)
err = common.VerifyDisclosuresInSDJWT(cfi.Disclosures, signedJWT)
if err != nil {
return nil, err
}

return getClaims(sdJWT.Disclosures)
return getClaims(cfi.Disclosures)
}

func getClaims(disclosures []string) ([]*Claim, error) {
Expand All @@ -94,14 +94,14 @@ func getClaims(disclosures []string) ([]*Claim, error) {
}

// DiscloseClaims discloses claims with specified claim names.
func DiscloseClaims(sdJWTSerialized string, claimNames []string) (string, error) {
sdJWT := common.ParseSDJWT(sdJWTSerialized)
func DiscloseClaims(combinedFormatForIssuance string, claimNames []string) (string, error) {
cfi := common.ParseCombinedFormatForIssuance(combinedFormatForIssuance)

if len(sdJWT.Disclosures) == 0 {
if len(cfi.Disclosures) == 0 {
return "", fmt.Errorf("no disclosures found in SD-JWT")
}

disclosures, err := common.GetDisclosureClaims(sdJWT.Disclosures)
disclosures, err := common.GetDisclosureClaims(cfi.Disclosures)
if err != nil {
return "", err
}
Expand All @@ -110,16 +110,16 @@ func DiscloseClaims(sdJWTSerialized string, claimNames []string) (string, error)

for _, claimName := range claimNames {
if index := getDisclosureByClaimName(claimName, disclosures); index != notFound {
selectedDisclosures = append(selectedDisclosures, sdJWT.Disclosures[index])
selectedDisclosures = append(selectedDisclosures, cfi.Disclosures[index])
}
}

combinedFormatForPresentation := sdJWT.JWTSerialized
for _, disclosure := range selectedDisclosures {
combinedFormatForPresentation += common.DisclosureSeparator + disclosure
cf := common.CombinedFormatForPresentation{
SDJWT: cfi.SDJWT,
Disclosures: selectedDisclosures,
}

return combinedFormatForPresentation, nil
return cf.Serialize(), nil
}

func getDisclosureByClaimName(name string, disclosures []*common.DisclosureClaim) int {
Expand Down
Loading

0 comments on commit c8f690f

Please sign in to comment.