cacert-gosigner/signer/main.go

206 lines
4.9 KiB
Go
Raw Normal View History

2018-10-31 11:17:51 +01:00
package main
import (
"errors"
"flag"
"fmt"
"io"
2018-10-31 11:17:51 +01:00
"time"
log "github.com/sirupsen/logrus"
"go.bug.st/serial"
"git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared"
2018-10-31 11:17:51 +01:00
)
var (
address string
baudrate int
)
func main() {
flag.StringVar(&address, "a", "/dev/ttyUSB0", "address")
flag.IntVar(&baudrate, "b", 115200, "baud rate")
flag.Parse()
2021-01-04 08:15:49 +01:00
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
serialMode := &serial.Mode{
2018-10-31 11:17:51 +01:00
BaudRate: baudrate,
DataBits: 8,
StopBits: serial.OneStopBit,
Parity: serial.NoParity,
2018-10-31 11:17:51 +01:00
}
log.Infof("connecting to %s using %+v", address, serialMode)
port, err := serial.Open(address, serialMode)
2018-10-31 11:17:51 +01:00
if err != nil {
2021-01-04 08:15:49 +01:00
log.Fatalf("could not open serial port: %v", err)
2018-10-31 11:17:51 +01:00
}
2021-01-04 08:15:49 +01:00
log.Info("connected")
2018-10-31 11:17:51 +01:00
count := 0
defer func() {
err := port.Close()
if err != nil {
2021-01-04 08:15:49 +01:00
log.Fatalf("could not close port: %v", err)
2018-10-31 11:17:51 +01:00
}
2021-01-04 08:15:49 +01:00
log.Info("closed")
2018-10-31 11:17:51 +01:00
}()
readLoop:
for {
// timeout if no command has been received within 15 seconds
timeout := time.After(15 * time.Second)
commandChan := make(chan datastructures.SignerRequest, 1)
errChan := make(chan error, 1)
readWriteCloser := (io.ReadWriteCloser)(port)
go Receive(&readWriteCloser, &commandChan, &errChan)
2018-10-31 11:17:51 +01:00
select {
case command := <-commandChan:
response, err := Process(command)
if err != nil {
2021-01-04 08:15:49 +01:00
log.Errorf("error processing command: %v", err)
break readLoop
2018-10-31 11:17:51 +01:00
}
2021-01-04 08:15:49 +01:00
_ = SendResponse(&readWriteCloser, response)
2018-10-31 11:17:51 +01:00
case <-timeout:
2021-01-04 08:15:49 +01:00
log.Error("timeout in main loop")
2018-10-31 11:17:51 +01:00
break readLoop
case err := <-errChan:
2021-01-04 08:15:49 +01:00
log.Errorf("error from io goroutine %v", err)
2018-10-31 11:17:51 +01:00
}
count++
2021-01-04 08:15:49 +01:00
log.Infof("%d requests processed. Waiting for next request ...", count)
2018-10-31 11:17:51 +01:00
}
}
// Send a response to the client
func SendResponse(port *io.ReadWriteCloser, response *datastructures.SignerResponse) error {
2018-10-31 11:17:51 +01:00
if _, err := (*port).Write([]byte{0x02}); err != nil {
return err
}
2021-01-04 08:15:49 +01:00
if ack, err := shared.ReceiveBytes(port, 1, 5*time.Second); err != nil {
2018-10-31 11:17:51 +01:00
return err
} else if ack[0] != 0x10 {
return errors.New(fmt.Sprintf("invalid ack byte 0x%02x", ack[0]))
}
tryAgain := true
for tryAgain {
2018-10-31 11:17:51 +01:00
data := response.Serialize()
if _, err := (*port).Write(data); err != nil {
return err
}
checksum := datastructures.CalculateXorCheckSum([][]byte{data})
if _, err := (*port).Write([]byte{checksum}); err != nil {
return err
}
if _, err := (*port).Write([]byte(shared.MagicTrailer)); err != nil {
return err
}
2021-01-04 08:15:49 +01:00
if ack, err := shared.ReceiveBytes(port, 1, 5*time.Second); err != nil {
2018-10-31 11:17:51 +01:00
return err
} else if ack[0] == 0x10 {
tryAgain = false
} else if ack[0] != 0x11 {
return errors.New(fmt.Sprintf("invalid ack byte 0x%02x", ack[0]))
}
}
return nil
}
// Process the signer request
func Process(command datastructures.SignerRequest) (response *datastructures.SignerResponse, err error) {
2021-01-04 08:15:49 +01:00
log.Infof("analyze %+v", command)
2018-10-31 11:17:51 +01:00
switch command.Action {
case datastructures.ActionNul:
response, err = handleNulAction(command)
return
default:
2021-01-04 08:15:49 +01:00
return nil, errors.New(fmt.Sprintf(
"unsupported Action 0x%02x %s",
int(command.Action),
command.Action,
))
2018-10-31 11:17:51 +01:00
}
}
func handleNulAction(command datastructures.SignerRequest) (*datastructures.SignerResponse, error) {
// todo: generate script to set system time from command time data in command.content1
return &datastructures.SignerResponse{
Version: command.Version, Action: command.Action,
Content1: "", Content2: "", Content3: ""}, nil
}
// Receive a request and generate a request data structure
func Receive(port *io.ReadWriteCloser, commandChan *chan datastructures.SignerRequest, errorChan *chan error) {
2021-01-04 08:15:49 +01:00
header, err := shared.ReceiveBytes(port, 1, 20*time.Second)
2018-10-31 11:17:51 +01:00
if err != nil {
*errorChan <- err
return
}
if header[0] != shared.HandshakeByte {
*errorChan <- fmt.Errorf("unexpected byte 0x%x expected 0x%x", header[0], shared.HandshakeByte)
2018-10-31 11:17:51 +01:00
}
if _, err := (*port).Write([]byte{shared.AckByte}); err != nil {
2018-10-31 11:17:51 +01:00
*errorChan <- errors.New("could not write ACK")
return
}
2021-01-04 08:15:49 +01:00
lengthBytes, err := shared.ReceiveBytes(port, 3, 2*time.Second)
2018-10-31 11:17:51 +01:00
if err != nil {
*errorChan <- err
return
}
blockLength := datastructures.Decode24BitLength(lengthBytes)
2021-01-04 08:15:49 +01:00
blockData, err := shared.ReceiveBytes(port, blockLength, 5*time.Second)
2018-10-31 11:17:51 +01:00
if err != nil {
*errorChan <- err
return
}
2021-01-04 08:15:49 +01:00
checkSum, err := shared.ReceiveBytes(port, 1, 2*time.Second)
2018-10-31 11:17:51 +01:00
if err != nil {
*errorChan <- err
return
}
command, err := datastructures.SignerRequestFromData(lengthBytes, blockData, checkSum[0])
if err != nil {
*errorChan <- err
2021-01-04 08:15:49 +01:00
return
2018-10-31 11:17:51 +01:00
}
2021-01-04 08:15:49 +01:00
trailer, err := shared.ReceiveBytes(port, len(shared.MagicTrailer), 2*time.Second)
2018-10-31 11:17:51 +01:00
if err != nil {
*errorChan <- err
return
}
if string(trailer) != shared.MagicTrailer {
*errorChan <- errors.New("expected trailer bytes not found")
return
}
if _, err := (*port).Write([]byte{0x10}); err != nil {
*errorChan <- err
return
}
*commandChan <- *command
}