Skip to content

Commit

Permalink
Check aesgcmpb.AesGcmKey version when parsing
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 670447171
Change-Id: I213a14ec5587eb61675fefee1cf1f5fd80f332f0
  • Loading branch information
morambro authored and copybara-github committed Sep 3, 2024
1 parent 5ad2c14 commit c6b40f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion aead/aesgcm/protoserialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ import (
tinkpb "github.com/tink-crypto/tink-go/v2/proto/tink_go_proto"
)

const (
// protoVersion is the accepted [gcmpb.AesGcmKey] proto version.
//
// Currently, only version 0 is supported; other versions are rejected.
protoVersion = 0
)

type serializer struct{}

func protoOutputPrefixTypeFromVariant(variant Variant) (tinkpb.OutputPrefixType, error) {
Expand Down Expand Up @@ -56,7 +63,7 @@ func (s *serializer) SerializeKey(key key.Key) (*tinkpb.Keyset_Key, error) {
keyBytes := actualKey.KeyBytes()
protoKey := &gcmpb.AesGcmKey{
KeyValue: keyBytes.Data(insecuresecretdataaccess.Token{}),
Version: 0,
Version: protoVersion,
}
serializedKey, err := proto.Marshal(protoKey)
if err != nil {
Expand Down Expand Up @@ -109,6 +116,9 @@ func (s *parser) ParseKey(keysetKey *tinkpb.Keyset_Key) (key.Key, error) {
if err := proto.Unmarshal(keyData.GetValue(), protoKey); err != nil {
return nil, err
}
if protoKey.GetVersion() != protoVersion {
return nil, fmt.Errorf("key has unsupported version: %v", protoKey.GetVersion())
}
variant, err := variantFromProto(keysetKey.GetOutputPrefixType())
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions aead/aesgcm/protoserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func TestParseKeyFails(t *testing.T) {
if err != nil {
t.Fatalf("proto.Marshal(keyWithInvalidSize) err = %v, want nil", err)
}
keyWithInvalidVersion := aesgcmpb.AesGcmKey{
Version: 1,
KeyValue: []byte("1234567890123456"),
}
serializedKeyWithInvalidVersion, err := proto.Marshal(&keyWithInvalidVersion)
if err != nil {
t.Fatalf("proto.Marshal(keyWithInvalidVersion) err = %v, want nil", err)
}
for _, tc := range []struct {
name string
keysetKey *tinkpb.Keyset_Key
Expand Down Expand Up @@ -100,6 +108,19 @@ func TestParseKeyFails(t *testing.T) {
KeyId: 12345,
},
},
{
name: "invalid AES GCM key version",
keysetKey: &tinkpb.Keyset_Key{
KeyData: &tinkpb.KeyData{
TypeUrl: typeURL,
Value: serializedKeyWithInvalidVersion,
KeyMaterialType: tinkpb.KeyData_SYMMETRIC,
},
Status: tinkpb.KeyStatusType_ENABLED,
OutputPrefixType: tinkpb.OutputPrefixType_TINK,
KeyId: 12345,
},
},
{
name: "invalid key material type",
keysetKey: &tinkpb.Keyset_Key{
Expand Down

0 comments on commit c6b40f3

Please sign in to comment.