2023-07-29 10:04:44 +00:00
|
|
|
// package bootprotocol contains the elements necessary to use the custom network boot protocol
|
|
|
|
package bootprotocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-09-16 11:51:01 +00:00
|
|
|
"strings"
|
2023-07-29 10:04:44 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Action int8
|
|
|
|
|
|
|
|
const (
|
|
|
|
ActionRequest Action = iota
|
|
|
|
ActionAccept
|
|
|
|
ActionDeny
|
2024-03-24 12:21:56 +00:00
|
|
|
ActionDiscover
|
2023-07-29 10:04:44 +00:00
|
|
|
ActionUnknown
|
|
|
|
)
|
|
|
|
|
|
|
|
var spaceByte = []byte(" ")
|
2023-09-16 11:51:01 +00:00
|
|
|
var commaByte = []byte(";")
|
2023-07-29 10:04:44 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
keyID = "id"
|
|
|
|
keyEfiApp = "efi_app"
|
|
|
|
keyReason = "reason"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrInvalidFormat = errors.New("invalid format for message")
|
|
|
|
var ErrUnknownAction = errors.New("unknown action for message")
|
|
|
|
var ErrInvalidParam = errors.New("invalid parameter for message")
|
|
|
|
var ErrMissingParam = errors.New("missing parameter for message")
|
|
|
|
|
|
|
|
func (a Action) String() string {
|
|
|
|
switch a {
|
|
|
|
case ActionAccept:
|
|
|
|
return "BOOT_ACCEPT"
|
|
|
|
case ActionDeny:
|
|
|
|
return "BOOT_DENY"
|
|
|
|
case ActionRequest:
|
|
|
|
return "BOOT_REQUEST"
|
2024-03-24 12:21:56 +00:00
|
|
|
case ActionDiscover:
|
|
|
|
return "BOOT_DISCOVER"
|
2023-07-29 10:04:44 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newActionFromBytes(raw []byte) Action {
|
|
|
|
switch string(raw) {
|
|
|
|
case "BOOT_ACCEPT":
|
|
|
|
return ActionAccept
|
|
|
|
case "BOOT_DENY":
|
|
|
|
return ActionDeny
|
|
|
|
case "BOOT_REQUEST":
|
|
|
|
return ActionRequest
|
2024-03-24 12:21:56 +00:00
|
|
|
case "BOOT_DISCOVER":
|
|
|
|
return ActionDiscover
|
2023-07-29 10:04:44 +00:00
|
|
|
default:
|
|
|
|
return ActionUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message interface {
|
|
|
|
encoding.BinaryUnmarshaler
|
|
|
|
encoding.BinaryMarshaler
|
|
|
|
Action() Action
|
|
|
|
ID() uuid.UUID
|
2023-07-29 19:23:36 +00:00
|
|
|
String() string
|
2023-07-29 10:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type requestMessage struct {
|
|
|
|
id uuid.UUID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *requestMessage) UnmarshalBinary(data []byte) error {
|
|
|
|
params := bytes.Split(data, commaByte)
|
|
|
|
for _, p := range params {
|
|
|
|
k, v, err := splitKeyValue(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse parameter %q: %w", string(p), err)
|
|
|
|
}
|
|
|
|
if bytes.Equal(k, []byte(keyID)) {
|
|
|
|
parsedId, err := uuid.ParseBytes(v)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidParam
|
|
|
|
}
|
|
|
|
rm.id = parsedId
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ErrMissingParam
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *requestMessage) MarshalBinary() (data []byte, err error) {
|
|
|
|
action := []byte(rm.Action().String())
|
|
|
|
params := []byte(fmt.Sprintf("%s=%s", keyID, rm.id.String()))
|
|
|
|
return bytes.Join([][]byte{action, params}, spaceByte), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *requestMessage) Action() Action {
|
|
|
|
return ActionRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *requestMessage) ID() uuid.UUID {
|
|
|
|
return rm.id
|
|
|
|
}
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
func (rm *requestMessage) String() string {
|
|
|
|
return fmt.Sprintf("%s from %s", ActionRequest.String(), rm.ID().String())
|
|
|
|
}
|
|
|
|
|
2023-07-29 10:04:44 +00:00
|
|
|
type acceptMessage struct {
|
|
|
|
id uuid.UUID
|
|
|
|
efiApp string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *acceptMessage) UnmarshalBinary(data []byte) error {
|
|
|
|
params := bytes.Split(data, commaByte)
|
|
|
|
for _, p := range params {
|
|
|
|
k, v, err := splitKeyValue(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse parameter %q: %w", string(p), err)
|
|
|
|
}
|
|
|
|
switch string(k) {
|
|
|
|
case keyID:
|
|
|
|
parsedId, err := uuid.ParseBytes(v)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidParam
|
|
|
|
}
|
|
|
|
am.id = parsedId
|
|
|
|
case keyEfiApp:
|
|
|
|
am.efiApp = string(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if am.id == uuid.Nil || am.efiApp == "" {
|
|
|
|
return ErrMissingParam
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *acceptMessage) MarshalBinary() (data []byte, err error) {
|
|
|
|
action := []byte(am.Action().String())
|
2023-09-16 11:51:01 +00:00
|
|
|
// efiApp := strings.ReplaceAll(am.efiApp, `\`, `\\`)
|
|
|
|
// efiApp = strings.ReplaceAll(efiApp, "File(", "")
|
|
|
|
efiApp := strings.ReplaceAll(am.efiApp, "File(", "")
|
|
|
|
efiApp, _ = strings.CutSuffix(efiApp, ")")
|
2023-07-29 10:04:44 +00:00
|
|
|
params := [][]byte{
|
|
|
|
[]byte(fmt.Sprintf("%s=%s", keyID, am.id.String())),
|
2023-09-16 11:51:01 +00:00
|
|
|
[]byte(fmt.Sprintf("%s=%s", keyEfiApp, efiApp)),
|
2023-07-29 10:04:44 +00:00
|
|
|
}
|
|
|
|
param_bytes := bytes.Join(params, commaByte)
|
|
|
|
return bytes.Join([][]byte{action, param_bytes}, spaceByte), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *acceptMessage) Action() Action {
|
|
|
|
return ActionAccept
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *acceptMessage) ID() uuid.UUID {
|
|
|
|
return am.id
|
|
|
|
}
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
func (am *acceptMessage) String() string {
|
|
|
|
return fmt.Sprintf("%s from %s, app %s", ActionAccept.String(), am.ID().String(), am.efiApp)
|
|
|
|
}
|
|
|
|
|
2023-07-29 10:04:44 +00:00
|
|
|
type denyMessage struct {
|
|
|
|
id uuid.UUID
|
|
|
|
reason string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *denyMessage) UnmarshalBinary(data []byte) error {
|
|
|
|
params := bytes.Split(data, commaByte)
|
|
|
|
for _, p := range params {
|
|
|
|
k, v, err := splitKeyValue(p)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse parameter %q: %w", string(p), err)
|
|
|
|
}
|
|
|
|
switch string(k) {
|
|
|
|
case keyID:
|
|
|
|
parsedId, err := uuid.ParseBytes(v)
|
|
|
|
if err != nil {
|
|
|
|
return ErrInvalidParam
|
|
|
|
}
|
|
|
|
dm.id = parsedId
|
|
|
|
case keyReason:
|
|
|
|
dm.reason = string(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if dm.id == uuid.Nil || dm.reason == "" {
|
|
|
|
return ErrMissingParam
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *denyMessage) MarshalBinary() (data []byte, err error) {
|
|
|
|
action := []byte(dm.Action().String())
|
|
|
|
params := [][]byte{
|
|
|
|
[]byte(fmt.Sprintf("%s=%s", keyID, dm.id.String())),
|
|
|
|
[]byte(fmt.Sprintf("%s=%s", keyReason, dm.reason)),
|
|
|
|
}
|
|
|
|
param_bytes := bytes.Join(params, commaByte)
|
|
|
|
return bytes.Join([][]byte{action, param_bytes}, spaceByte), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *denyMessage) Action() Action {
|
|
|
|
return ActionDeny
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *denyMessage) ID() uuid.UUID {
|
|
|
|
return dm.id
|
|
|
|
}
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
func (dm *denyMessage) String() string {
|
|
|
|
return fmt.Sprintf("%s from %s, reason %q", ActionDeny.String(), dm.ID().String(), dm.reason)
|
|
|
|
}
|
|
|
|
|
2024-03-24 12:21:56 +00:00
|
|
|
type discoverMessage struct{}
|
|
|
|
|
|
|
|
func (dm *discoverMessage) UnmarshalBinary(data []byte) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *discoverMessage) MarshalBinary() (data []byte, err error) {
|
|
|
|
return []byte(dm.Action().String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *discoverMessage) Action() Action {
|
|
|
|
return ActionDiscover
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *discoverMessage) ID() uuid.UUID {
|
|
|
|
return uuid.Nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dm *discoverMessage) String() string {
|
|
|
|
return ActionDiscover.String()
|
|
|
|
}
|
|
|
|
|
2023-07-29 10:04:44 +00:00
|
|
|
func MessageFromBytes(dat []byte) (Message, error) {
|
2024-03-24 12:21:56 +00:00
|
|
|
rawAction, content, _ := bytes.Cut(dat, spaceByte)
|
2023-07-29 10:04:44 +00:00
|
|
|
|
|
|
|
var message Message
|
|
|
|
action := newActionFromBytes(rawAction)
|
|
|
|
switch action {
|
|
|
|
case ActionRequest:
|
|
|
|
message = &requestMessage{}
|
|
|
|
case ActionAccept:
|
|
|
|
message = &acceptMessage{}
|
|
|
|
case ActionDeny:
|
|
|
|
message = &denyMessage{}
|
2024-03-24 12:21:56 +00:00
|
|
|
case ActionDiscover:
|
|
|
|
message = &discoverMessage{}
|
2023-07-29 10:04:44 +00:00
|
|
|
default:
|
|
|
|
return nil, ErrUnknownAction
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := message.UnmarshalBinary(content); err != nil {
|
2024-03-24 12:21:56 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse %s message: %w", message.Action().String(), err)
|
2023-07-29 10:04:44 +00:00
|
|
|
}
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Accept(id uuid.UUID, efiApp string) Message {
|
|
|
|
return &acceptMessage{
|
|
|
|
id: id,
|
|
|
|
efiApp: efiApp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Deny(id uuid.UUID, reason string) Message {
|
|
|
|
return &denyMessage{
|
|
|
|
id: id,
|
|
|
|
reason: reason,
|
|
|
|
}
|
|
|
|
}
|