Skip to content

Commit

Permalink
Export a method to decode Envelope payload using flexible base64 decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-lowman-dd committed Apr 8, 2022
1 parent 7438362 commit 77f2aec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 10 additions & 0 deletions dsse/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ type Envelope struct {
Signatures []Signature `json:"signatures"`
}

/*
DecodePayload returns the serialized body, decoded
from the envelope's payload field. A flexible
decoder is used, first trying standard base64, then
URL-encoded base64.
*/
func (e *Envelope) DecodePayload() ([]byte, error) {
return b64Decode(e.Payload)
}

/*
Signature represents a generic in-toto signature that contains the identifier
of the key which was used to create the signature.
Expand Down
22 changes: 17 additions & 5 deletions dsse/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestEcdsaSign(t *testing.T) {
assert.Equal(t, acceptedKeys[0].KeyID, keyID, "unexpected keyid")
}

func TestB64Decode(t *testing.T) {
func TestDecodePayload(t *testing.T) {
var want = make([]byte, 256)
for i := range want {
want[i] = byte(i)
Expand All @@ -342,23 +342,35 @@ func TestB64Decode(t *testing.T) {
var b64StdErr = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w"

t.Run("Standard encoding", func(t *testing.T) {
got, err := b64Decode(b64Std)
env := &Envelope{
Payload: b64Std,
}
got, err := env.DecodePayload()
assert.Nil(t, err, "unexpected error")
assert.Equal(t, want, got, "wrong data")
})
t.Run("URL encoding", func(t *testing.T) {
got, err := b64Decode(b64Url)
env := &Envelope{
Payload: b64Url,
}
got, err := env.DecodePayload()
assert.Nil(t, err, "unexpected error")
assert.Equal(t, want, got, "wrong data")
})

t.Run("Standard encoding - error", func(t *testing.T) {
got, err := b64Decode(b64StdErr)
env := &Envelope{
Payload: b64StdErr,
}
got, err := env.DecodePayload()
assert.NotNil(t, err, "expected error")
assert.Nil(t, got, "wrong data")
})
t.Run("URL encoding - error", func(t *testing.T) {
got, err := b64Decode(b64UrlErr)
env := &Envelope{
Payload: b64UrlErr,
}
got, err := env.DecodePayload()
assert.NotNil(t, err, "expected error")
assert.Nil(t, got, "wrong data")
})
Expand Down
2 changes: 1 addition & 1 deletion dsse/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (ev *EnvelopeVerifier) Verify(e *Envelope) ([]AcceptedKey, error) {
}

// Decode payload (i.e serialized body)
body, err := b64Decode(e.Payload)
body, err := e.DecodePayload()
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 77f2aec

Please sign in to comment.