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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// client simulator
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-04 08:16:41 +00:00
|
|
|
"context"
|
2022-11-29 09:29:09 +00:00
|
|
|
"crypto/rand"
|
2022-12-02 17:31:59 +00:00
|
|
|
"crypto/x509"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-29 09:29:09 +00:00
|
|
|
"io"
|
2022-08-03 12:38:36 +00:00
|
|
|
"os"
|
2022-12-02 17:31:59 +00:00
|
|
|
"sort"
|
2022-08-03 12:38:36 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
2022-11-20 09:07:02 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-11-20 08:13:11 +00:00
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/protocol"
|
|
|
|
|
2022-11-20 08:13:11 +00:00
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type TestCommandGenerator struct {
|
2022-11-29 13:05:10 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
commands chan *protocol.Command
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *TestCommandGenerator) GenerateCommands(ctx context.Context) error {
|
2022-11-29 09:29:09 +00:00
|
|
|
// write some leading garbage to test signer robustness
|
|
|
|
_, _ = io.CopyN(os.Stdout, rand.Reader, 50) //nolint:gomnd
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
g.commands <- &protocol.Command{
|
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdHealth),
|
|
|
|
Command: &messages.HealthCommand{},
|
2022-11-29 09:29:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
healthInterval = 5 * time.Second
|
|
|
|
crlInterval = 15 * time.Minute
|
|
|
|
startPause = 3 * time.Second
|
|
|
|
)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.logger.Info("start generating commands")
|
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
time.Sleep(startPause)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
g.commands <- &protocol.Command{
|
2022-12-01 10:34:07 +00:00
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdFetchCRL),
|
2022-11-20 17:59:37 +00:00
|
|
|
Command: &messages.FetchCRLCommand{IssuerID: "sub-ecc_person_2022"},
|
|
|
|
}
|
|
|
|
|
2022-11-29 09:29:09 +00:00
|
|
|
healthTimer := time.NewTimer(healthInterval)
|
|
|
|
crlTimer := time.NewTimer(crlInterval)
|
2022-08-04 08:16:41 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2022-11-29 09:29:09 +00:00
|
|
|
_ = healthTimer.Stop()
|
2022-08-04 08:16:41 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
g.logger.Info("stopped health check loop")
|
|
|
|
|
|
|
|
_ = crlTimer.Stop()
|
|
|
|
|
|
|
|
g.logger.Info("stopped CRL fetch loop")
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-08-04 08:16:41 +00:00
|
|
|
return nil
|
2022-11-29 09:29:09 +00:00
|
|
|
case <-healthTimer.C:
|
2022-11-21 07:26:50 +00:00
|
|
|
g.commands <- &protocol.Command{
|
2022-12-01 10:34:07 +00:00
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdHealth),
|
2022-11-20 17:59:37 +00:00
|
|
|
Command: &messages.HealthCommand{},
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 09:29:09 +00:00
|
|
|
healthTimer.Reset(healthInterval)
|
|
|
|
case <-crlTimer.C:
|
|
|
|
g.commands <- &protocol.Command{
|
2022-12-01 10:34:07 +00:00
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdFetchCRL),
|
2022-11-29 09:29:09 +00:00
|
|
|
Command: &messages.FetchCRLCommand{IssuerID: "sub-ecc_person_2022"},
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 08:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
type clientSimulator struct {
|
2022-11-29 13:05:10 +00:00
|
|
|
clientHandler protocol.ClientHandler
|
2022-11-21 07:26:50 +00:00
|
|
|
framesIn chan []byte
|
2022-11-29 08:57:23 +00:00
|
|
|
framesOut chan []byte
|
|
|
|
framer protocol.Framer
|
2022-11-21 07:26:50 +00:00
|
|
|
commandGenerator *TestCommandGenerator
|
2022-11-29 13:05:10 +00:00
|
|
|
logger *logrus.Logger
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
const (
|
|
|
|
responseAnnounceTimeout = 30 * time.Second
|
|
|
|
responseDataTimeout = 2 * time.Second
|
|
|
|
)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
func (c *clientSimulator) Run(ctx context.Context) error {
|
|
|
|
framerErrors := make(chan error)
|
|
|
|
protocolErrors := make(chan error)
|
|
|
|
generatorErrors := make(chan error)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
go func() {
|
2022-12-01 20:36:10 +00:00
|
|
|
err := c.framer.ReadFrames(ctx, os.Stdin, c.framesIn)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
framerErrors <- err
|
|
|
|
}()
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
go func() {
|
2022-12-01 20:36:10 +00:00
|
|
|
err := c.framer.WriteFrames(ctx, os.Stdout, c.framesOut)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
framerErrors <- err
|
|
|
|
}()
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
go func() {
|
|
|
|
clientProtocol := protocol.NewClient(c.clientHandler, c.commandGenerator.commands, c.framesIn, c.framesOut, c.logger)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
err := clientProtocol.Handle(ctx)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
protocolErrors <- err
|
|
|
|
}()
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
go func() {
|
|
|
|
err := c.commandGenerator.GenerateCommands(ctx)
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
generatorErrors <- err
|
|
|
|
}()
|
2022-11-29 09:29:09 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case err := <-framerErrors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from framer: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-29 09:29:09 +00:00
|
|
|
return nil
|
2022-11-29 13:05:10 +00:00
|
|
|
case err := <-generatorErrors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from command generator: %w", err)
|
|
|
|
}
|
2022-11-29 09:29:09 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
return nil
|
|
|
|
case err := <-protocolErrors:
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error from protocol handler: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
return nil
|
2022-11-29 09:29:09 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
type ClientHandler struct {
|
2022-12-02 17:31:59 +00:00
|
|
|
logger *logrus.Logger
|
|
|
|
commands chan *protocol.Command
|
|
|
|
caList []string
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func (c *ClientHandler) Send(ctx context.Context, command *protocol.Command, out chan<- []byte) error {
|
2022-11-29 13:05:10 +00:00
|
|
|
var (
|
|
|
|
frame []byte
|
|
|
|
err error
|
|
|
|
)
|
2022-11-29 09:29:09 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
frame, err = msgpack.Marshal(command.Announce)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal command annoucement: %w", err)
|
|
|
|
}
|
2022-11-29 09:29:09 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
c.logger.WithField("announcement", command.Announce).Info("write command announcement")
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
c.logger.Trace("writing command announcement")
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case out <- frame:
|
|
|
|
break
|
|
|
|
}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
frame, err = msgpack.Marshal(command.Command)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not marshal command data: %w", err)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
c.logger.WithField("command", command.Command).Info("write command data")
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-12-01 20:36:10 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case out <- frame:
|
|
|
|
break
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-11-29 08:57:23 +00:00
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func (c *ClientHandler) ResponseAnnounce(ctx context.Context, in <-chan []byte) (*protocol.Response, error) {
|
2022-11-29 13:05:10 +00:00
|
|
|
response := &protocol.Response{}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
var announce messages.ResponseAnnounce
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
select {
|
2022-12-01 20:36:10 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, nil
|
2022-11-29 13:05:10 +00:00
|
|
|
case frame := <-in:
|
|
|
|
if err := msgpack.Unmarshal(frame, &announce); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not unmarshal response announcement: %w", err)
|
|
|
|
}
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
response.Announce = &announce
|
2022-11-21 07:26:50 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
c.logger.WithField("announcement", response.Announce).Debug("received response announcement")
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
return response, nil
|
|
|
|
case <-time.After(responseAnnounceTimeout):
|
|
|
|
return nil, protocol.ErrResponseAnnounceTimeoutExpired
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-12-04 12:47:51 +00:00
|
|
|
func (c *ClientHandler) ResponseData(ctx context.Context, in <-chan []byte, response *protocol.Response) error {
|
2022-11-29 13:05:10 +00:00
|
|
|
select {
|
2022-12-01 20:36:10 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
2022-11-29 13:05:10 +00:00
|
|
|
case frame := <-in:
|
|
|
|
switch response.Announce.Code {
|
|
|
|
case messages.RespHealth:
|
|
|
|
var resp messages.HealthResponse
|
|
|
|
if err := msgpack.Unmarshal(frame, &resp); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal health response data: %w", err)
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
c.updateCAs(ctx, c.commands, resp.Info)
|
|
|
|
|
|
|
|
response.Response = &resp
|
|
|
|
case messages.RespCAInfo:
|
|
|
|
var resp messages.CAInfoResponse
|
|
|
|
if err := msgpack.Unmarshal(frame, &resp); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal CA info response data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certificate, err := x509.ParseCertificate(resp.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse certificate data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.Infof(
|
|
|
|
"certificate for %s: subject=%s, issuer=%s, serial=0x%x, valid from %s to %s",
|
|
|
|
resp.Name,
|
|
|
|
certificate.Subject,
|
|
|
|
certificate.Issuer,
|
|
|
|
certificate.SerialNumber,
|
|
|
|
certificate.NotBefore,
|
|
|
|
certificate.NotAfter)
|
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
response.Response = &resp
|
|
|
|
case messages.RespFetchCRL:
|
|
|
|
var resp messages.FetchCRLResponse
|
|
|
|
if err := msgpack.Unmarshal(frame, &resp); err != nil {
|
|
|
|
return fmt.Errorf("could not unmarshal fetch CRL response data: %w", err)
|
|
|
|
}
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-11-29 13:05:10 +00:00
|
|
|
response.Response = &resp
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unhandled response code %s", response.Announce.Code)
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
2022-11-29 13:05:10 +00:00
|
|
|
case <-time.After(responseDataTimeout):
|
|
|
|
return protocol.ErrResponseDataTimeoutExpired
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
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-12-01 20:36:10 +00:00
|
|
|
func (c *ClientHandler) HandleResponse(_ context.Context, response *protocol.Response) error {
|
2022-11-29 13:05:10 +00:00
|
|
|
c.logger.WithField("response", response.Announce).Info("handled response")
|
|
|
|
c.logger.WithField("response", response).Debug("full response")
|
2022-08-03 12:38:36 +00:00
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
func (c *ClientHandler) updateCAs(ctx context.Context, out chan *protocol.Command, info []*messages.HealthInfo) {
|
|
|
|
caList := make([]string, 0)
|
|
|
|
|
|
|
|
for _, i := range info {
|
|
|
|
if i.Source == "HSM" {
|
|
|
|
c.logger.Debugf("info from HSM: %s", i)
|
|
|
|
|
|
|
|
for caName := range i.MoreInfo {
|
|
|
|
caList = append(caList, caName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(caList)
|
|
|
|
|
|
|
|
if len(caList) != len(c.caList) {
|
|
|
|
c.caList = caList
|
|
|
|
|
|
|
|
for _, ca := range c.caList {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case out <- &protocol.Command{
|
|
|
|
Announce: messages.BuildCommandAnnounce(messages.CmdCAInfo),
|
|
|
|
Command: &messages.CAInfoCommand{Name: ca},
|
|
|
|
}:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientHandler(logger *logrus.Logger, commands chan *protocol.Command) *ClientHandler {
|
|
|
|
return &ClientHandler{logger: logger, commands: commands}
|
2022-11-29 13:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
func main() {
|
|
|
|
logger := logrus.New()
|
|
|
|
logger.SetOutput(os.Stderr)
|
2022-11-28 16:10:46 +00:00
|
|
|
logger.SetLevel(logrus.DebugLevel)
|
2022-11-21 07:26:50 +00:00
|
|
|
|
|
|
|
messages.RegisterGeneratedResolver()
|
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
cobsFramer, err := protocol.NewCOBSFramer(logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not create COBS framer")
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:31:59 +00:00
|
|
|
commandBufferSize := 50
|
|
|
|
|
|
|
|
commandCh := make(chan *protocol.Command, commandBufferSize)
|
|
|
|
|
2022-11-21 07:26:50 +00:00
|
|
|
sim := &clientSimulator{
|
|
|
|
commandGenerator: &TestCommandGenerator{
|
|
|
|
logger: logger,
|
2022-12-02 17:31:59 +00:00
|
|
|
commands: commandCh,
|
2022-11-21 07:26:50 +00:00
|
|
|
},
|
2022-11-29 13:05:10 +00:00
|
|
|
logger: logger,
|
|
|
|
framesIn: make(chan []byte),
|
|
|
|
framesOut: make(chan []byte),
|
2022-12-02 08:09:52 +00:00
|
|
|
framer: cobsFramer,
|
2022-12-02 17:31:59 +00:00
|
|
|
clientHandler: newClientHandler(logger, commandCh),
|
2022-11-21 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 08:09:52 +00:00
|
|
|
err = sim.Run(context.Background())
|
2022-11-21 07:26:50 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("simulator returned an error")
|
|
|
|
}
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|