Skip to content

Commit

Permalink
pgcryptocipherccl: add pgcode for pkcs unpad errors
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
andyyang890 committed Sep 18, 2023
1 parent a20aee0 commit 0e38f38
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions pkg/ccl/pgcryptoccl/pgcryptocipherccl/padding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"bytes"
"math"

"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/errors"
)

Expand All @@ -36,16 +38,18 @@ func pkcsPad(data []byte, blockSize int) ([]byte, error) {
// pkcsUnpad removes the padding added by pkcsPad.
func pkcsUnpad(data []byte) ([]byte, error) {
if len(data) == 0 {
return nil, errors.New("PKCS-padded data is empty")
return nil, pgerror.New(pgcode.InvalidParameterValue, "PKCS-padded data is empty")
}

paddingLen := data[len(data)-1]
if paddingLen == 0 || int(paddingLen) > len(data) {
return nil, errors.Newf("invalid final byte found in PKCS-padded data: %d", paddingLen)
return nil, pgerror.Newf(pgcode.InvalidParameterValue,
"invalid final byte found in PKCS-padded data: %d", paddingLen)
}
for i := 1; i < int(paddingLen); i++ {
if b := data[len(data)-i-1]; b != paddingLen {
return nil, errors.Newf("invalid byte found in PKCS-padded data: expected %d, but found %d", paddingLen, b)
return nil, pgerror.Newf(pgcode.InvalidParameterValue,
"invalid byte found in PKCS-padded data: expected %d, but found %d", paddingLen, b)
}
}

Expand Down

0 comments on commit 0e38f38

Please sign in to comment.