Skip to content

Commit

Permalink
Return a copy of the internal proto representation in keysetMaterial
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 651378740
Change-Id: I85ea0dc7a8e7ac825998f56aceb6675898c30850
  • Loading branch information
morambro authored and copybara-github committed Jul 11, 2024
1 parent b9c6ece commit 84d8dda
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 17 additions & 0 deletions keyset/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ func TestNewHandle(t *testing.T) {
}
}

func TestKeysetMaterialMakesACopy(t *testing.T) {
wantProtoKeyset := testutil.NewKeyset(1, []*tinkpb.Keyset_Key{
testutil.NewKey(testutil.NewKeyData("some type url", []byte{0}, tinkpb.KeyData_SYMMETRIC), tinkpb.KeyStatusType_ENABLED, 1, tinkpb.OutputPrefixType_TINK),
})
handle, err := testkeyset.NewHandle(wantProtoKeyset)
if err != nil {
t.Errorf("testkeyset.NewHandle(wantProtoKeyset) = %v, want nil", err)
}
gotProtoKeyset := testkeyset.KeysetMaterial(handle)
if wantProtoKeyset == gotProtoKeyset {
t.Errorf("testkeyset.KeysetMaterial(handle) = %v, want a copy of %v", gotProtoKeyset, wantProtoKeyset)
}
if !proto.Equal(gotProtoKeyset, wantProtoKeyset) {
t.Errorf("testkeyset.NewHandle(wantProtoKeyset) = %v, want %v", gotProtoKeyset, wantProtoKeyset)
}
}

func TestNewHandleWithInvalidTypeURLFails(t *testing.T) {
// template with unknown TypeURL
invalidTemplate := mac.HMACSHA256Tag128KeyTemplate()
Expand Down
5 changes: 3 additions & 2 deletions keyset/keyset.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package keyset

import (
"google.golang.org/protobuf/proto"
"github.com/tink-crypto/tink-go/v2/internal"
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
)
Expand All @@ -30,9 +31,9 @@ func keysetHandle(ks *tinkpb.Keyset, opts ...Option) (*Handle, error) {

// keysetMaterial is used by package insecurecleartextkeyset and package
// testkeyset (via package internal) to read the key material in a
// keyset.Handle.
// keyset.Handle. Returns a clone of the keyset.
func keysetMaterial(h *Handle) *tinkpb.Keyset {
return h.ks
return proto.Clone(h.ks).(*tinkpb.Keyset)
}

func init() {
Expand Down

0 comments on commit 84d8dda

Please sign in to comment.