2022-08-03 12:38:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-11-29 08:57:23 +00:00
|
|
|
// Package serial provides a handler for the serial connection of the signer machine.
|
2022-11-28 16:39:48 +00:00
|
|
|
package serial
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-21 07:26:50 +00:00
|
|
|
"context"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-21 07:26:50 +00:00
|
|
|
"sync"
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-08-03 12:38:36 +00:00
|
|
|
"github.com/tarm/serial"
|
|
|
|
|
2022-11-28 16:39:48 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/internal/config"
|
2022-08-03 12:38:36 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type protocolState int8
|
|
|
|
|
|
|
|
const (
|
2022-11-21 07:26:50 +00:00
|
|
|
cmdAnnounce protocolState = iota
|
|
|
|
cmdData
|
|
|
|
respAnnounce
|
|
|
|
respData
|
2022-11-20 17:59:37 +00:00
|
|
|
)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
var validTransitions = map[protocolState]protocolState{
|
|
|
|
cmdAnnounce: cmdData,
|
|
|
|
cmdData: respAnnounce,
|
|
|
|
respAnnounce: respData,
|
|
|
|
respData: cmdAnnounce,
|
|
|
|
}
|
|
|
|
|
|
|
|
var protocolStateNames = map[protocolState]string{
|
|
|
|
cmdAnnounce: "CMD ANNOUNCE",
|
|
|
|
cmdData: "CMD DATA",
|
|
|
|
respAnnounce: "RESP ANNOUNCE",
|
|
|
|
respData: "RESP DATA",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p protocolState) String() string {
|
|
|
|
if name, ok := protocolStateNames[p]; ok {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("unknown %d", p)
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:38:36 +00:00
|
|
|
type Handler struct {
|
|
|
|
protocolHandler protocol.Handler
|
2022-11-20 17:59:37 +00:00
|
|
|
protocolState protocolState
|
2022-11-29 08:57:23 +00:00
|
|
|
framer protocol.Framer
|
2022-08-03 12:38:36 +00:00
|
|
|
config *serial.Config
|
|
|
|
port *serial.Port
|
2022-11-21 07:26:50 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
lock sync.Mutex
|
|
|
|
framesIn chan []byte
|
2022-11-29 08:57:23 +00:00
|
|
|
framesOut chan []byte
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) setupConnection() error {
|
|
|
|
s, err := serial.OpenPort(h.config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open serial port: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.port = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) Close() error {
|
|
|
|
err := h.port.Close()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not close serial port: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) Run(ctx context.Context) error {
|
|
|
|
h.protocolState = cmdAnnounce
|
|
|
|
errors := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
2022-11-29 08:57:23 +00:00
|
|
|
err := h.framer.ReadFrames(h.port, h.framesIn)
|
|
|
|
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := h.framer.WriteFrames(h.port, h.framesOut)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
errors <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case err := <-errors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from handler loop: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
default:
|
|
|
|
if err := h.handleProtocolState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) nextState() error {
|
|
|
|
next, ok := validTransitions[h.protocolState]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("illegal protocol state %s", h.protocolState)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
h.protocolState = next
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) handleProtocolState() error {
|
|
|
|
h.logger.Tracef("handling protocol state %s", h.protocolState)
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
h.lock.Lock()
|
|
|
|
defer h.lock.Unlock()
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
switch h.protocolState {
|
|
|
|
case cmdAnnounce:
|
|
|
|
if err := h.handleCmdAnnounce(); err != nil {
|
|
|
|
return err
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
case cmdData:
|
|
|
|
if err := h.handleCmdData(); err != nil {
|
2022-11-20 17:59:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
case respAnnounce:
|
|
|
|
if err := h.handleRespAnnounce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case respData:
|
|
|
|
if err := h.handleRespData(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown protocol state %s", h.protocolState)
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) handleCmdAnnounce() error {
|
|
|
|
h.logger.Trace("waiting for command announce")
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
frame := <-h.framesIn
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := h.protocolHandler.HandleCommandAnnounce(frame); err != nil {
|
|
|
|
return fmt.Errorf("command announce handling failed: %w", err)
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := h.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) handleCmdData() error {
|
|
|
|
h.logger.Trace("waiting for command data")
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
frame := <-h.framesIn
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if frame == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := h.protocolHandler.HandleCommand(frame); err != nil {
|
|
|
|
return fmt.Errorf("command handler failed: %w", err)
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
if err := h.nextState(); err != nil {
|
|
|
|
return err
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) handleRespAnnounce() error {
|
|
|
|
frame, err := h.protocolHandler.ResponseAnnounce()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get response announcement: %w", err)
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 08:57:23 +00:00
|
|
|
h.framesOut <- frame
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
if err := h.nextState(); err != nil {
|
|
|
|
return err
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func (h *Handler) handleRespData() error {
|
|
|
|
frame, err := h.protocolHandler.ResponseData()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get response data: %w", err)
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 08:57:23 +00:00
|
|
|
h.framesOut <- frame
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
if err := h.nextState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func New(cfg *config.Serial, logger *logrus.Logger, protocolHandler protocol.Handler) (*Handler, error) {
|
|
|
|
h := &Handler{
|
|
|
|
protocolHandler: protocolHandler,
|
|
|
|
logger: logger,
|
2022-11-28 16:10:46 +00:00
|
|
|
framesIn: make(chan []byte),
|
2022-11-29 08:57:23 +00:00
|
|
|
framesOut: make(chan []byte),
|
|
|
|
framer: protocol.NewCOBSFramer(logger),
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
h.config = &serial.Config{Name: cfg.Device, Baud: cfg.Baud, ReadTimeout: cfg.Timeout}
|
|
|
|
|
|
|
|
err := h.setupConnection()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|