8000 Remove unnecessary cbor marshal methods by qmuntal · Pull Request #155 · veraison/go-cose · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove unnecessary cbor marshal methods #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 5, 2023
35 changes: 0 additions & 35 deletions algorithm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cose

import (
"crypto"
"fmt"
"strconv"
)

Expand Down Expand Up @@ -75,35 +74,6 @@ func (a Algorithm) String() string {
}
}

// MarshalCBOR marshals the Algorithm as a CBOR int.
func (a Algorithm) MarshalCBOR() ([]byte, error) {
return encMode.Marshal(int64(a))
}

// UnmarshalCBOR populates the Algorithm from the provided CBOR value (must be
// int or tstr).
func (a *Algorithm) UnmarshalCBOR(data []byte) error {
var raw intOrStr

if err := raw.UnmarshalCBOR(data); err != nil {
return fmt.Errorf("invalid algorithm value: %w", err)
}

if raw.IsString() {
v := algorithmFromString(raw.String())
if v == AlgorithmInvalid {
return fmt.Errorf("unknown algorithm value %q", raw.String())
}

*a = v
} else {
v := raw.Int()
*a = Algorithm(v)
}

return nil
}

// hashFunc returns the hash associated with the algorithm supported by this
// library.
func (a Algorithm) hashFunc() crypto.Hash {
Expand Down Expand Up @@ -135,8 +105,3 @@ func computeHash(h crypto.Hash, data []byte) ([]byte, error) {
}
return hh.Sum(nil), nil
}

// NOTE: there are currently no registered string values for an algorithm.
func algorithmFromString(v string) Algorithm {
return AlgorithmInvalid
}
17 changes: 0 additions & 17 deletions algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ func TestAlgorithm_String(t *testing.T) {
}
}

func TestAlgorithm_CBOR(t *testing.T) {
tvs2 := []struct {
Data []byte
ExpectedError string
}{
{[]byte{0x63, 0x66, 0x6f, 0x6f}, "unknown algorithm value \"foo\""},
{[]byte{0x40}, "invalid algorithm value: must be int or string, found []uint8"},
}

for _, tv := range tvs2 {
var a Algorithm

err := a.UnmarshalCBOR(tv.Data)
assertEqualError(t, err, tv.ExpectedError)
}
}

func TestAlgorithm_computeHash(t *testing.T) {
// run tests
data := []byte("hello world")
Expand Down
3 changes: 3 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ func Test_intOrStr_CBOR(t *testing.T) {
}

func requireNoError(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Errorf("Unexpected error: %q", err)
t.Fail()
}
}

func assertEqualError(t *testing.T, err error, expected string) {
t.Helper()
if err == nil || err.Error() != expected {
t.Errorf("Unexpected error: want %q, got %q", expected, err)
}
}

func assertEqual(t *testing.T, expected, actual interface{}) {
t.Helper()
if !objectsAreEqualValues(expected, actual) {
t.Errorf("Unexpected value: want %v, got %v", expected, actual)
}
Expand Down
9 changes: 1 addition & 8 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ func (h ProtectedHeader) Algorithm() (Algorithm, error) {
case int64:
return Algorithm(alg), nil
case string:
v := algorithmFromString(alg)

var err error
if v == AlgorithmInvalid {
err = fmt.Errorf("unknown algorithm value %q", alg)
}

return v, err
return AlgorithmInvalid, fmt.Errorf("unknown algorithm value %q", alg)
default:
return AlgorithmInvalid, ErrInvalidAlgorithm
}
Expand Down
126 changes: 8 additions & 118 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ func (ko KeyOp) String() string {
}
}

// IsSupported returnns true if the specified value is represents one of the
// key_ops defined in
// https://www.rfc-editor.org/rfc/rfc9052.html#name-cose-key-common-parameters
func (ko KeyOp) IsSupported() bool {
return ko >= 1 && ko <= 10
}

// MarshalCBOR marshals the KeyOp as a CBOR int.
func (ko KeyOp) MarshalCBOR() ([]byte, error) {
return encMode.Marshal(int64(ko))
Expand All @@ -141,10 +134,6 @@ func (ko *KeyOp) UnmarshalCBOR(data []byte) error {
} else {
v := raw.Int()
*ko = KeyOp(v)

if !ko.IsSupported() {
return fmt.Errorf("unknown key_ops value %d", v)
}
}

return nil
Expand Down Expand Up @@ -182,51 +171,6 @@ func (kt KeyType) String() string {
}
}

// MarshalCBOR marshals the KeyType as a CBOR int.
func (kt KeyType) MarshalCBOR() ([]byte, error) {
return encMode.Marshal(int(kt))
}

// UnmarshalCBOR populates the KeyType from the provided CBOR value (must be
// int or tstr).
func (kt *KeyType) UnmarshalCBOR(data []byte) error {
var raw intOrStr

if err := raw.UnmarshalCBOR(data); err != nil {
return fmt.Errorf("invalid key type value: %w", err)
}

if raw.IsString() {
v, err := keyTypeFromString(raw.String())

if err != nil {
return err
}

*kt = v
} else {
v := raw.Int()

if v == 0 {
// 0 is reserved, and so can never be valid
return fmt.Errorf("invalid key type value 0")
}

if v > 4 || v < 0 || v == 3 {
return fmt.Errorf("unknown key type value %d", v)
}

*kt = KeyType(v)
}

return nil
}

// NOTE: there are currently no registered string key type values.
func keyTypeFromString(v string) (KeyType, error) {
return KeyTypeInvalid, fmt.Errorf("unknown key type value %q", v)
}

const (

// Invalid/unrecognised curve
Expand Down Expand Up @@ -282,46 +226,6 @@ func (c Curve) String() string {
}
}

// MarshalCBOR marshals the KeyType as a CBOR int.
func (c Curve) MarshalCBOR() ([]byte, error) {
return encMode.Marshal(int(c))
}

// UnmarshalCBOR populates the KeyType from the provided CBOR value (must be
// int or tstr).
func (c *Curve) UnmarshalCBOR(data []byte) error {
var raw intOrStr

if err := raw.UnmarshalCBOR(data); err != nil {
return fmt.Errorf("invalid curve value: %w", err)
}

if raw.IsString() {
v, err := curveFromString(raw.String())

if err != nil {
return err
}

*c = v
} else {
v := raw.Int()

if v < 1 || v > 7 {
return fmt.Errorf("unknown curve value %d", v)
}

*c = Curve(v)
}

return nil
}

// NOTE: there are currently no registered string values for curves.
func curveFromString(v string) (Curve, error) {
return CurveInvalid, fmt.Errorf("unknown curve value %q", v)
}

// Key represents a COSE_Key structure, as defined by RFC8152.
// Note: currently, this does NOT support RFC8230 (RSA algorithms).
type Key struct {
Expand All @@ -333,15 +237,15 @@ type Key struct {
KeyType KeyType `cbor:"1,keyasint"`
// KeyID is the identification value matched to the kid in the message.
KeyID []byte `cbor:"2,keyasint,omitempty"`
// Algorithm is used to restrict the algorithm that is used with the
// key. If it is set, the application MUST verify that it matches the
// algorithm for which the Key is being used.
Algorithm Algorithm `cbor:"3,keyasint,omitempty"`
// KeyOps can be set to restrict the set of operations that the Key is used for.
KeyOps []KeyOp `cbor:"4,keyasint,omitempty"`
// BaseIV is the Base IV to be xor-ed with Partial IVs.
BaseIV []byte `cbor:"5,keyasint,omitempty"`

// Algorithm is used to restrict the algorithm that is used with the
// key. If it is set, the application MUST verify that it matches the
// algorithm for which the Key is being used.
Algorithm Algorithm `cbor:"-"`
// Curve is EC identifier -- taken form "COSE Elliptic Curves" IANA registry.
// Populated from keyStruct.RawKeyParam when key type is EC2 or OKP.
Curve Curve `cbor:"-"`
Expand Down Expand Up @@ -505,11 +409,6 @@ type keyalias Key
type marshaledKey struct {
keyalias

// RawAlgorithm contains the raw Algorithm value, this is necessary
// because cbor library ignores omitempty on types that implement the
// cbor.Marshaler interface.
RawAlgorithm cbor.RawMessage `cbor:"3,keyasint,omitempty"`

// RawKeyParam contains the raw CBOR encoded data for the label -1.
// Depending on the KeyType this is used to populate either Curve or K
// below.
Expand All @@ -535,13 +434,6 @@ func (k *Key) MarshalCBOR() ([]byte, error) {
default:
return nil, fmt.Errorf("invalid key type: %q", k.KeyType.String())
}

if k.Algorithm != AlgorithmInvalid {
if tmp.RawAlgorithm, err = encMode.Marshal(k.Algorithm); err != nil {
return nil, err
}
}

return encMode.Marshal(tmp)
}

Expand All @@ -552,14 +444,12 @@ func (k *Key) UnmarshalCBOR(data []byte) error {
if err := decMode.Unmarshal(data, &tmp); err != nil {
return err
}
*k = Key(tmp.keyalias)

if tmp.RawAlgorithm != nil {
if err := decMode.Unmarshal(tmp.RawAlgorithm, &k.Algorithm); err != nil {
return err
}
if tmp.KeyType == KeyTypeInvalid {
return errors.New("invalid key type value 0")
}

*k = Key(tmp.keyalias)

switch k.KeyType {
case KeyTypeEC2:
if tmp.RawKeyParam == nil {
Expand Down
58 changes: 1 addition & 57 deletions key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,8 @@ func Test_KeyOp(t *testing.T) {

var ko KeyOp

data := []byte{0x20}
data := []byte{0x63, 0x66, 0x6f, 0x6f}
err := ko.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown key_ops value -1")

data = []byte{0x18, 0xff}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown key_ops value 255")

data = []byte{0x63, 0x66, 0x6f, 0x6f}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, `unknown key_ops value "foo"`)

data = []byte{0x40}
Expand All @@ -109,54 +101,6 @@ func Test_KeyOp(t *testing.T) {
}
}

func Test_KeyType(t *testing.T) {
var ko KeyType

data := []byte{0x20}
err := ko.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown key type value -1")

data = []byte{0x00}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, "invalid key type value 0")

data = []byte{0x03}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown key type value 3")

data = []byte{0x63, 0x66, 0x6f, 0x6f}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, `unknown key type value "foo"`)

data = []byte{0x40}
err = ko.UnmarshalCBOR(data)
assertEqualError(t, err, "invalid key type value: must be int or string, found []uint8")
}

func Test_Curve(t *testing.T) {
var c Curve

data := []byte{0x20}
err := c.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown curve value -1")

data = []byte{0x00}
err = c.UnmarshalCBOR(data)
assertEqualError(t, err, "unknown curve value 0")

data = []byte{0x63, 0x66, 0x6f, 0x6f}
err = c.UnmarshalCBOR(data)
assertEqualError(t, err, `unknown curve value "foo"`)

data = []byte{0x40}
err = c.UnmarshalCBOR(data)
assertEqualError(t, err, "invalid curve value: must be int or string, found []uint8")

if "unknown curve value 42" != Curve(42).String() {
t.Errorf("Unexpected string value %q", Curve(42).String())
}
}

func Test_Key_UnmarshalCBOR(t *testing.T) {
tvs := []struct {
Name string
Expand Down
0