-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yogesh Deshpande <[email protected]>
- Loading branch information
1 parent
5671628
commit d0b1d5b
Showing
5 changed files
with
228 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package tdx | ||
|
||
import cbor "github.com/fxamacker/cbor/v2" | ||
|
||
var ( | ||
em, emError = initCBOREncMode() | ||
dm, dmError = initCBORDecMode() | ||
) | ||
|
||
func initCBOREncMode() (en cbor.EncMode, err error) { | ||
encOpt := cbor.EncOptions{ | ||
IndefLength: cbor.IndefLengthForbidden, | ||
TimeTag: cbor.EncTagRequired, | ||
} | ||
return encOpt.EncMode() | ||
} | ||
|
||
func initCBORDecMode() (dm cbor.DecMode, err error) { | ||
decOpt := cbor.DecOptions{ | ||
IndefLength: cbor.IndefLengthForbidden, | ||
} | ||
return decOpt.DecMode() | ||
} | ||
|
||
func init() { | ||
if emError != nil { | ||
panic(emError) | ||
} | ||
if dmError != nil { | ||
panic(dmError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
package tdx | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/fxamacker/cbor/v2" | ||
"github.com/veraison/corim/encoding" | ||
) | ||
|
||
// TeeIsvProdID stores an ISV Product Identifier. The supported formats are uint and variable-length bytes. | ||
type TeeIsvProdID struct { | ||
Value interface{} | ||
} | ||
|
||
func NewISVProdID(val interface{}) *TeeIsvProdID { | ||
switch t := val.(type) { | ||
case uint, uint64: | ||
return &TeeIsvProdID{Value: t} | ||
case []byte: | ||
return &TeeIsvProdID{Value: t} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (o *TeeIsvProdID) SetISVProdID(val interface{}) error { | ||
switch t := val.(type) { | ||
case uint, uint64: | ||
o.Value = val | ||
case []byte: | ||
o.Value = val | ||
case int: | ||
if t < 0 { | ||
return fmt.Errorf("unsupported negative ISVProdID: %d", t) | ||
} | ||
o.Value = val | ||
default: | ||
return fmt.Errorf("unsupported ISVProdID type: %T", t) | ||
} | ||
return nil | ||
} | ||
|
||
func (o TeeIsvProdID) Valid() error { | ||
if o.Value == nil { | ||
return fmt.Errorf("empty IsvProdID") | ||
} | ||
switch t := o.Value.(type) { | ||
case uint, uint64: | ||
return nil | ||
case []byte: | ||
if len(t) == 0 { | ||
return fmt.Errorf("empty IsvProdID") | ||
} | ||
case int: | ||
if t < 0 { | ||
return fmt.Errorf("unsupported negative ISVProdID: %d", t) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (o TeeIsvProdID) GetUintIsvProdID() (uint, error) { | ||
switch t := o.Value.(type) { | ||
case uint64: | ||
return uint(t), nil | ||
case uint: | ||
return t, nil | ||
default: | ||
return 0, fmt.Errorf("ISVProdID type is: %T", t) | ||
} | ||
} | ||
|
||
func (o TeeIsvProdID) GetBytesIsvProdID() ([]byte, error) { | ||
switch t := o.Value.(type) { | ||
case []byte: | ||
if len(t) == 0 { | ||
return nil, fmt.Errorf("ISVProdID type is of zero length") | ||
} | ||
return t, nil | ||
default: | ||
return nil, fmt.Errorf("ISVProdID type is: %T", t) | ||
} | ||
} | ||
func (o TeeIsvProdID) IsBytesIsvProdID() bool { | ||
switch t := o.Value.(type) { | ||
case []byte: | ||
return true | ||
default: | ||
fmt.Printf("ISVProdID type is: %T\n", t) | ||
return false | ||
} | ||
} | ||
|
||
func (o TeeIsvProdID) IsUintIsvProdID() bool { | ||
switch t := o.Value.(type) { | ||
case uint64, uint: | ||
return true | ||
default: | ||
fmt.Printf("ISVProdID type is: %T\n", t) | ||
return false | ||
} | ||
} | ||
|
||
func (o TeeIsvProdID) MarshalJSON() ([]byte, error) { | ||
|
||
if o.Valid() != nil { | ||
return nil, fmt.Errorf("invalid ISVProdID") | ||
} | ||
var ( | ||
v encoding.TypeAndValue | ||
b []byte | ||
err error | ||
) | ||
switch t := o.Value.(type) { | ||
case uint, uint64, int: | ||
b, err = json.Marshal(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v = encoding.TypeAndValue{Type: "uint", Value: b} | ||
case []byte: | ||
b, err = json.Marshal(t) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v = encoding.TypeAndValue{Type: "bytes", Value: b} | ||
default: | ||
return nil, fmt.Errorf("unknown type %T for ISVProdID", t) | ||
} | ||
return json.Marshal(v) | ||
} | ||
|
||
func (o *TeeIsvProdID) UnmarshalJSON(data []byte) error { | ||
var v encoding.TypeAndValue | ||
|
||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
|
||
switch v.Type { | ||
case "uint": | ||
var x uint | ||
if err := json.Unmarshal(v.Value, &x); err != nil { | ||
return fmt.Errorf( | ||
"cannot unmarshal TeeIsvProdID of type uint: %w", err) | ||
} | ||
o.Value = x | ||
case "bytes": | ||
var x []byte | ||
if err := json.Unmarshal(v.Value, &x); err != nil { | ||
return fmt.Errorf( | ||
"cannot unmarshal TeeIsvProdID of type bytes: %w", err) | ||
} | ||
o.Value = x | ||
} | ||
return nil | ||
} | ||
func (o TeeIsvProdID) MarshalCBOR() ([]byte, error) { | ||
return cbor.Marshal(o.Value) | ||
} | ||
|
||
func (o *TeeIsvProdID) UnmarshalCBOR(data []byte) error { | ||
return cbor.Unmarshal(data, &o.Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters