2018-10-31 11:17:51 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2020-04-17 19:38:54 +02:00
|
|
|
"fmt"
|
2020-04-17 19:39:01 +02:00
|
|
|
"time"
|
|
|
|
|
2020-04-17 19:39:06 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.bug.st/serial"
|
|
|
|
|
2018-10-31 11:17:51 +01:00
|
|
|
"git.cacert.org/cacert-gosigner/datastructures"
|
|
|
|
"git.cacert.org/cacert-gosigner/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-04-17 19:38:54 +02:00
|
|
|
var configFile string
|
|
|
|
|
|
|
|
flag.StringVar(&configFile, "c", "client.yaml", "client configuration file in YAML format")
|
2018-10-31 11:17:51 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
2020-04-17 19:38:54 +02:00
|
|
|
var clientConfig *ClientConfig
|
2020-04-17 19:39:06 +02:00
|
|
|
var serialConfig *serial.Mode
|
2020-04-17 19:38:54 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if clientConfig, err = readConfig(configFile); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2020-04-17 19:39:06 +02:00
|
|
|
serialConfig = fillSerialMode(clientConfig)
|
2020-04-17 19:38:54 +02:00
|
|
|
if clientConfig.Debug {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:06 +02:00
|
|
|
log.Infof("connecting to %s using %+v", clientConfig.SerialAddress, serialConfig)
|
|
|
|
port, err := serial.Open(clientConfig.SerialAddress, serialConfig)
|
2018-10-31 11:17:51 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("serial port connected")
|
2018-10-31 11:17:51 +01:00
|
|
|
defer func() {
|
|
|
|
err := port.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("serial port closed")
|
2018-10-31 11:17:51 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
errorChannel := make(chan error, 1)
|
2020-04-17 19:39:01 +02:00
|
|
|
responseChannel := make(chan datastructures.SignerResponse, 1)
|
|
|
|
crlCheck := 0
|
|
|
|
|
|
|
|
log.Debug("starting main loop")
|
|
|
|
|
|
|
|
for {
|
|
|
|
requestChannel := make(chan datastructures.SignerRequest, 1)
|
|
|
|
|
|
|
|
go HandleRequests(&port, &responseChannel, &errorChannel, &requestChannel)
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("handling GPG database ...")
|
|
|
|
// HandleGPG(&requestChannel)
|
|
|
|
log.Debug("issuing certificates ...")
|
|
|
|
// HandleCertificates(&requestChannel)
|
|
|
|
log.Debug("revoking certificates ...")
|
|
|
|
// RevokeCertificates(&requestChannel)
|
|
|
|
|
|
|
|
crlCheck++
|
|
|
|
if crlCheck%100 == 0 {
|
|
|
|
log.Debug("refresh CRLs ...")
|
|
|
|
// RefreshCRLs(&requestChannel)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
log.Debug("send NUL request to keep connection open")
|
|
|
|
requestChannel <- *datastructures.NewNulRequest()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case response := <-responseChannel:
|
|
|
|
if err := Process(response); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
case err := <-errorChannel:
|
|
|
|
log.Error(err)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
|
|
|
|
log.Debug("sleep for 2.7 seconds")
|
|
|
|
time.Sleep(2700 * time.Millisecond)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
func Process(response datastructures.SignerResponse) (err error) {
|
|
|
|
log.Infof("process response of type %s", response.Action)
|
|
|
|
log.Tracef("process response %v", response)
|
2018-10-31 11:17:51 +01:00
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
switch response.Action {
|
|
|
|
case datastructures.ActionNul:
|
|
|
|
log.Trace("received response for NUL request")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported action in response 0x%x", response.Action)
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
func HandleRequests(port *serial.Port, responseChan *chan datastructures.SignerResponse, errorChan *chan error, requestChan *chan datastructures.SignerRequest) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case request := <-*requestChan:
|
|
|
|
SendRequest(port, responseChan, errorChan, &request)
|
|
|
|
}
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
}
|
2020-04-17 19:38:54 +02:00
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
func SendRequest(port *serial.Port, responseChan *chan datastructures.SignerResponse, errorChan *chan error, request *datastructures.SignerRequest) {
|
|
|
|
log.Tracef("send request %v to serial port %v", *request, *port)
|
|
|
|
if err := sendHandShake(*port); err != nil {
|
|
|
|
*errorChan <- err
|
2020-04-17 19:38:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
requestBytes := request.Serialize()
|
|
|
|
if length, err := (*port).Write(requestBytes); err != nil {
|
2020-04-17 19:38:54 +02:00
|
|
|
*errorChan <- err
|
|
|
|
return
|
2020-04-17 19:39:01 +02:00
|
|
|
} else {
|
|
|
|
log.Tracef("wrote %d request bytes", length)
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
|
|
|
|
if length, err := (*port).Write([]byte{datastructures.CalculateXorCheckSum([][]byte{requestBytes})}); err != nil {
|
2020-04-17 19:38:54 +02:00
|
|
|
*errorChan <- err
|
|
|
|
return
|
2020-04-17 19:39:01 +02:00
|
|
|
} else {
|
|
|
|
log.Tracef("wrote %d checksum bytes", length)
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
if length, err := (*port).Write([]byte(shared.MagicTrailer)); err != nil {
|
2020-04-17 19:38:54 +02:00
|
|
|
*errorChan <- err
|
|
|
|
return
|
2020-04-17 19:39:01 +02:00
|
|
|
} else {
|
|
|
|
log.Tracef("wrote %d trailer bytes", length)
|
2020-04-17 19:38:54 +02:00
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
|
|
|
|
header, err := shared.ReceiveBytes(port, 1, 20)
|
2020-04-17 19:38:54 +02:00
|
|
|
if err != nil {
|
|
|
|
*errorChan <- err
|
|
|
|
return
|
|
|
|
}
|
2020-04-17 19:39:01 +02:00
|
|
|
if header[0] != shared.AckByte {
|
|
|
|
*errorChan <- fmt.Errorf("unexpected byte 0x%x expected 0x%x", header, shared.AckByte)
|
2020-04-17 19:38:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-17 19:39:01 +02:00
|
|
|
receiveResponse(port, responseChan, errorChan)
|
2018-10-31 11:17:51 +01:00
|
|
|
}
|