Refactor signer code into command and package

This commit is contained in:
Jan Dittberner 2021-01-04 14:15:12 +01:00
parent 3a2578ae55
commit 38566f35ef
13 changed files with 476 additions and 276 deletions

View file

@ -3,12 +3,14 @@ package protocol
import (
"bytes"
"fmt"
"git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared"
log "github.com/sirupsen/logrus"
"io"
"sync"
"time"
log "github.com/sirupsen/logrus"
"git.cacert.org/cacert-gosigner/datastructures"
"git.cacert.org/cacert-gosigner/shared"
)
type SignerProtocolHandler interface {
@ -52,7 +54,7 @@ func (rc *SignerProtocolRequestChannel) IsClosed() bool {
type protocolHandler struct {
requestChannel *SignerProtocolRequestChannel
responseChannel *chan *datastructures.SignerResponse
serialConnection *io.ReadWriteCloser
serialConnection io.ReadWriteCloser
config *signerProtocolConfig
}
@ -76,7 +78,12 @@ func (ph *protocolHandler) Close() error {
return nil
}
func NewProtocolHandler(requests *SignerProtocolRequestChannel, response *chan *datastructures.SignerResponse, serialConnection *io.ReadWriteCloser, config *signerProtocolConfig) SignerProtocolHandler {
func NewProtocolHandler(
requests *SignerProtocolRequestChannel,
response *chan *datastructures.SignerResponse,
serialConnection io.ReadWriteCloser,
config *signerProtocolConfig,
) SignerProtocolHandler {
return &protocolHandler{
requestChannel: requests,
responseChannel: response,
@ -123,13 +130,13 @@ func (ph *protocolHandler) HandleSignerProtocol() error {
func (ph *protocolHandler) sendHandshake() (err error) {
var bytesWritten, bytesRead int
if bytesWritten, err = (*ph.serialConnection).Write([]byte{shared.HandshakeByte}); err != nil {
if bytesWritten, err = ph.serialConnection.Write([]byte{shared.HandshakeByte}); err != nil {
return
} else {
log.Tracef("wrote %d bytes of handshake info", bytesWritten)
}
data := make([]byte, 1)
if bytesRead, err = (*ph.serialConnection).Read(data); err != nil {
if bytesRead, err = ph.serialConnection.Read(data); err != nil {
return
}
log.Tracef("%d bytes read", bytesRead)
@ -142,13 +149,13 @@ func (ph *protocolHandler) sendHandshake() (err error) {
func (ph *protocolHandler) sendRequest(requestBytes *[]byte) error {
for {
if length, err := (*ph.serialConnection).Write(*requestBytes); err != nil {
if length, err := ph.serialConnection.Write(*requestBytes); err != nil {
return err
} else {
log.Tracef("wrote %d request bytes", length)
}
if length, err := (*ph.serialConnection).Write([]byte{
if length, err := ph.serialConnection.Write([]byte{
datastructures.CalculateXorCheckSum([][]byte{*requestBytes}),
}); err != nil {
return err
@ -156,7 +163,7 @@ func (ph *protocolHandler) sendRequest(requestBytes *[]byte) error {
log.Tracef("wrote %d checksum bytes", length)
}
if length, err := (*ph.serialConnection).Write([]byte(shared.MagicTrailer)); err != nil {
if length, err := ph.serialConnection.Write([]byte(shared.MagicTrailer)); err != nil {
return err
} else {
log.Tracef("wrote %d trailer bytes", length)
@ -184,7 +191,7 @@ func (ph *protocolHandler) waitForResponseHandshake() (err error) {
log.Warnf("received invalid handshake byte 0x%x", data[0])
return UnExpectedHandshakeByte{data[0]}
}
if err = shared.SendByte(ph.serialConnection, shared.AckByte); err != nil {
if err = shared.SendBytes(ph.serialConnection, []byte{shared.AckByte}); err != nil {
return
}
@ -229,7 +236,7 @@ func (ph *protocolHandler) readResponse() (*[]byte, *[]byte, byte, error) {
if calculatedChecksum != checkSum {
return nil, nil, 0, fmt.Errorf("calculated checksum mismatch 0x%x vs 0x%x", calculatedChecksum, checkSum)
}
if err := shared.SendByte(ph.serialConnection, shared.AckByte); err != nil {
if err := shared.SendBytes(ph.serialConnection, []byte{shared.AckByte}); err != nil {
return nil, nil, 0, err
}