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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//go:generate go run github.com/shamaton/msgpackgen
|
|
|
|
|
|
|
|
// Package messages contains structure definitions for protocol messages
|
|
|
|
package messages
|
|
|
|
|
|
|
|
import (
|
2022-11-20 17:59:37 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-20 09:07:02 +00:00
|
|
|
"strings"
|
2022-08-03 12:38:36 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type CommandCode int8
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
CmdHealth CommandCode = iota
|
2022-11-20 17:59:37 +00:00
|
|
|
CmdFetchCRL
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
var commandNames = map[CommandCode]string{
|
|
|
|
CmdHealth: "HEALTH",
|
|
|
|
CmdFetchCRL: "FETCH URL",
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:38:36 +00:00
|
|
|
func (c CommandCode) String() string {
|
2022-11-20 17:59:37 +00:00
|
|
|
if name, ok := commandNames[c]; ok {
|
|
|
|
return name
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
return fmt.Sprintf("unknown %d", c)
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type ResponseCode int8
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
const (
|
2022-11-20 17:59:37 +00:00
|
|
|
RespError ResponseCode = -1
|
|
|
|
RespHealth ResponseCode = iota
|
|
|
|
RespFetchCRL
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
var responseNames = map[ResponseCode]string{
|
|
|
|
RespError: "ERROR",
|
|
|
|
RespHealth: "HEALTH",
|
|
|
|
RespFetchCRL: "FETCH CRL",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ResponseCode) String() string {
|
|
|
|
if name, ok := responseNames[c]; ok {
|
|
|
|
return name
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("unknown %d", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandAnnounce struct {
|
|
|
|
Code CommandCode `msgpack:"code"`
|
|
|
|
Created time.Time `msgpack:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandAnnounce) String() string {
|
|
|
|
return fmt.Sprintf("CommandAnnounce[code=%s, created=%s]", r.Code, r.Created)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResponseAnnounce struct {
|
|
|
|
Code ResponseCode `msgpack:"code"`
|
|
|
|
Created time.Time `msgpack:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ResponseAnnounce) String() string {
|
|
|
|
return fmt.Sprintf("ResponseAnnounce[code=%s, created=%s]", r.Code, r.Created)
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type FetchCRLCommand struct {
|
|
|
|
IssuerID string `msgpack:"issuer_id"`
|
|
|
|
LastKnownHash struct {
|
|
|
|
Algorithm string `msgpack:"algorithm"`
|
|
|
|
Value string `msgpack:"value"`
|
|
|
|
} `msgpack:"last_known_hash"` // optional last known hash in format
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type HealthCommand struct {
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 13:45:27 +00:00
|
|
|
type HealthInfo struct {
|
|
|
|
Source string
|
|
|
|
Healthy bool
|
|
|
|
MoreInfo map[string]string
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:07:02 +00:00
|
|
|
func (i *HealthInfo) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(builder, "source: %s, healthy: %v, [\n", i.Source, i.Healthy)
|
|
|
|
|
|
|
|
for k, v := range i.MoreInfo {
|
|
|
|
_, _ = fmt.Fprintf(builder, " %s: '%s'\n", k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = builder.WriteRune(']')
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
2022-08-03 12:38:36 +00:00
|
|
|
type HealthResponse struct {
|
|
|
|
Version string `msgpack:"version"`
|
2022-11-20 17:59:37 +00:00
|
|
|
Healthy bool `msgpack:"healthy"`
|
|
|
|
Info []*HealthInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HealthResponse) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(builder, "signer version=%s, healthy=%v, health data:\n", h.Version, h.Healthy)
|
|
|
|
|
|
|
|
for _, info := range h.Info {
|
|
|
|
_, _ = fmt.Fprintf(builder, " - %s", info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type FetchCRLResponse struct {
|
|
|
|
IssuerID string `msgpack:"issuer_id"`
|
|
|
|
IsDelta bool `msgpack:"is_delta"`
|
|
|
|
CRLData []byte `msgpack:"crl_data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *FetchCRLResponse) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(builder, "issuer id=%s, delta CRL data=%v", r.IssuerID, r.IsDelta)
|
|
|
|
|
|
|
|
if r.IsDelta {
|
|
|
|
_, _ = fmt.Fprint(builder, ", delta CRL data not shown")
|
|
|
|
} else {
|
|
|
|
revlist, err := x509.ParseRevocationList(r.CRLData)
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintf(builder, ", could not parse CRL: %s", err.Error())
|
|
|
|
} else {
|
|
|
|
_, _ = fmt.Fprintf(builder, ", CRL info: issuer=%s, number=%s, next update=%s, revoked certificates=%d",
|
|
|
|
revlist.Issuer, revlist.Number, revlist.NextUpdate, len(revlist.RevokedCertificates))
|
|
|
|
_, _ = builder.WriteString(", CRL data:\n")
|
|
|
|
_ = pem.Encode(builder, &pem.Block{
|
|
|
|
Type: "CERTIFICATE REVOCATION LIST",
|
|
|
|
Bytes: r.CRLData,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ErrorResponse struct {
|
|
|
|
Message string `msgpack:"message"`
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
func BuildCommandAnnounce(code CommandCode) *CommandAnnounce {
|
|
|
|
return &CommandAnnounce{Code: code, Created: time.Now().UTC()}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildResponseAnnounce(code ResponseCode) *ResponseAnnounce {
|
|
|
|
return &ResponseAnnounce{Code: code, Created: time.Now().UTC()}
|
|
|
|
}
|