Skip to content

Commit

Permalink
treewide: implement serializer interface for socket API
Browse files Browse the repository at this point in the history
For the socket API, the cmcd and testtool used CBOR
serialization for the payloads (regardless of the
serialization of metadata and attestation reports).

This commit introduces the socketApiSerializer configuration
for the testtool and adds automatic serialization detection
for socket API requests in the cmcd. This makes it possible
to interact with the cmcd socket API without requiring
CBOR support.

Signed-off-by: Simon Ott <[email protected]>
  • Loading branch information
smo4201 committed Sep 26, 2024
1 parent 75260c9 commit 4d42b87
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 117 deletions.
29 changes: 2 additions & 27 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"fmt"
"net"

"github.com/fxamacker/cbor/v2"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -203,7 +202,7 @@ func SignerOptsToHash(opts crypto.SignerOpts) (HashFunction, error) {
//
// Len uint32 -> Length of the payload to be sent
// Type uint32 -> Type of the payload
// payload []byte -> CBOR-encoded payload
// payload []byte -> encoded payload
func Receive(conn net.Conn) ([]byte, uint32, error) {

// If unix domain sockets are used, set the write buffer size
Expand Down Expand Up @@ -260,24 +259,14 @@ func Receive(conn net.Conn) ([]byte, uint32, error) {

log.Tracef("Received payload length %v", payloadLen)

if msgType == TypeError {
resp := new(SocketError)
err = cbor.Unmarshal(payload.Bytes(), resp)
if err != nil {
return nil, 0, fmt.Errorf("failed to unmarshal error response")
} else {
return nil, 0, fmt.Errorf("server responded with error: %v", resp.Msg)
}
}

return payload.Bytes(), msgType, nil
}

// Send sends data to a socket with the following format
//
// Len uint32 -> Length of the payload to be sent
// Type uint32 -> Type of the payload
// payload []byte -> CBOR-encoded payload
// payload []byte -> encoded payload
func Send(conn net.Conn, payload []byte, t uint32) error {

if len(payload) > MaxMsgLen {
Expand Down Expand Up @@ -320,17 +309,3 @@ func Send(conn net.Conn, payload []byte, t uint32) error {

return nil
}

func SendError(conn net.Conn, format string, args ...interface{}) error {
msg := fmt.Sprintf(format, args...)
log.Warn(msg)
resp := &SocketError{
Msg: msg,
}
payload, err := cbor.Marshal(resp)
if err != nil {
return fmt.Errorf("failed to marshal error response: %v", err)
}

return Send(conn, payload, TypeError)
}
Loading

0 comments on commit 4d42b87

Please sign in to comment.