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

@ -2,27 +2,6 @@ package datastructures
import "encoding/binary"
type Action uint8
const (
ActionNul = Action(0)
ActionSign = Action(1)
ActionRevoke = Action(2)
)
func (a Action) String() string {
switch a {
case ActionNul:
return "NUL"
case ActionSign:
return "SIGN"
case ActionRevoke:
return "REVOKE"
default:
return "unknown"
}
}
func encode24BitLength(data []byte) []byte {
lengthBytes := make([]byte, 4)
binary.BigEndian.PutUint32(lengthBytes, uint32(len(data)))

View file

@ -3,14 +3,14 @@ package datastructures
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"time"
"git.cacert.org/cacert-gosigner/shared"
)
type SignerRequest struct {
Version uint8
Action Action
Action shared.Action
System uint8
Root uint8
Configuration uint8
@ -22,9 +22,7 @@ type SignerRequest struct {
Content3 string
}
const protocolVersion = 1
func SignerRequestFromData(lengthBytes []byte, blockData []byte, checkSum byte) (*SignerRequest, error) {
func SignerRequestFromData(blockData []byte) (*SignerRequest, error) {
headerLength := Decode24BitLength(blockData[0:3])
headerBytes := blockData[3 : 3+headerLength]
@ -40,13 +38,9 @@ func SignerRequestFromData(lengthBytes []byte, blockData []byte, checkSum byte)
content3Length := Decode24BitLength(contentBytes[content3Offset : content3Offset+3])
content3 := string(contentBytes[3+content3Offset : 3+content3Offset+content3Length])
calculated := CalculateXorCheckSum([][]byte{lengthBytes, blockData})
if checkSum != calculated {
return nil, errors.New(fmt.Sprintf("invalid checksum expected 0x%x got 0x%x", calculated, checkSum))
}
return &SignerRequest{
Version: headerBytes[0],
Action: Action(headerBytes[1]),
Action: shared.Action(headerBytes[1]),
System: headerBytes[2],
Root: headerBytes[3],
Configuration: headerBytes[4],
@ -79,8 +73,8 @@ func (r SignerRequest) Serialize() []byte {
func NewNulRequest() *SignerRequest {
return &SignerRequest{
Version: protocolVersion,
Action: ActionNul,
Version: shared.ProtocolVersion,
Action: shared.ActionNul,
Content1: time.Now().UTC().Format("010203042006.05"),
}
}

View file

@ -4,11 +4,13 @@ import (
"bytes"
"errors"
"fmt"
"git.cacert.org/cacert-gosigner/shared"
)
type SignerResponse struct {
Version uint8
Action Action
Action shared.Action
Reserved1 uint8
Reserved2 uint8
Content1 string
@ -45,7 +47,7 @@ func SignerResponseFromData(lengthBytes []byte, blockData []byte, checkSum byte)
return &SignerResponse{
Version: headerBytes[0],
Action: Action(headerBytes[1]),
Action: shared.Action(headerBytes[1]),
Reserved1: headerBytes[2],
Reserved2: headerBytes[3],
Content1: content[0],