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"
|
2022-11-30 17:42:40 +00:00
|
|
|
"encoding/json"
|
2022-11-20 17:59:37 +00:00
|
|
|
"encoding/pem"
|
2022-08-03 12:38:36 +00:00
|
|
|
"fmt"
|
2022-11-28 16:10:46 +00:00
|
|
|
"math/big"
|
2022-12-01 10:34:07 +00:00
|
|
|
"sort"
|
2022-11-20 09:07:02 +00:00
|
|
|
"strings"
|
2022-08-03 12:38:36 +00:00
|
|
|
"time"
|
2022-11-28 16:10:46 +00:00
|
|
|
|
|
|
|
// required for msgpackgen
|
|
|
|
_ "github.com/dave/jennifer"
|
|
|
|
"github.com/google/uuid"
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type CommandCode int8
|
2022-08-03 12:38:36 +00:00
|
|
|
|
|
|
|
const (
|
2022-11-29 13:05:10 +00:00
|
|
|
CmdUndef CommandCode = iota
|
|
|
|
CmdHealth
|
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{
|
2022-11-29 13:05:10 +00:00
|
|
|
CmdUndef: "UNDEFINED",
|
2022-11-20 17:59:37 +00:00
|
|
|
CmdHealth: "HEALTH",
|
2022-11-29 08:57:23 +00:00
|
|
|
CmdFetchCRL: "FETCH CRL",
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
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-29 13:05:10 +00:00
|
|
|
RespError ResponseCode = -1
|
|
|
|
RespUndef ResponseCode = iota
|
|
|
|
RespHealth
|
2022-11-20 17:59:37 +00:00
|
|
|
RespFetchCRL
|
2022-08-03 12:38:36 +00:00
|
|
|
)
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
var responseNames = map[ResponseCode]string{
|
|
|
|
RespError: "ERROR",
|
2022-11-29 13:05:10 +00:00
|
|
|
RespUndef: "UNDEFINED",
|
2022-11-20 17:59:37 +00:00
|
|
|
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"`
|
2022-11-28 16:10:46 +00:00
|
|
|
ID string `msgpack:"id"`
|
2022-11-20 17:59:37 +00:00
|
|
|
Created time.Time `msgpack:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandAnnounce) String() string {
|
2022-11-28 16:10:46 +00:00
|
|
|
return fmt.Sprintf("code=%s, id=%s, created=%s", r.Code, r.ID, r.Created.Format(time.RFC3339))
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
func BuildCommandAnnounce(code CommandCode) *CommandAnnounce {
|
|
|
|
commandID := uuid.NewString()
|
|
|
|
|
|
|
|
return &CommandAnnounce{Code: code, ID: commandID, Created: time.Now().UTC()}
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type ResponseAnnounce struct {
|
|
|
|
Code ResponseCode `msgpack:"code"`
|
|
|
|
Created time.Time `msgpack:"created"`
|
2022-11-28 16:10:46 +00:00
|
|
|
ID string `msgpack:"id"`
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ResponseAnnounce) String() string {
|
2022-11-28 16:10:46 +00:00
|
|
|
return fmt.Sprintf("code=%s, id=%s, created=%s", r.Code, r.ID, r.Created.Format(time.RFC3339))
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
func BuildResponseAnnounce(code ResponseCode, commandID string) *ResponseAnnounce {
|
|
|
|
return &ResponseAnnounce{Code: code, ID: commandID, Created: time.Now().UTC()}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HealthCommand struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HealthCommand) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
type FetchCRLCommand struct {
|
2022-11-30 17:47:18 +00:00
|
|
|
IssuerID string `msgpack:"issuer_id"`
|
|
|
|
LastKnownID []byte `msgpack:"last_known_id"`
|
2022-11-28 16:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FetchCRLCommand) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(builder, "issuerId='%s'", f.IssuerID)
|
|
|
|
|
|
|
|
if f.LastKnownID != nil {
|
2022-12-01 10:34:07 +00:00
|
|
|
_, _ = fmt.Fprintf(builder, ", lastKnownId=0x%x", new(big.Int).SetBytes(f.LastKnownID))
|
2022-11-28 16:10:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
2022-08-03 12:38:36 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
type ProfileUsage uint8
|
2022-11-20 09:07:02 +00:00
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
const (
|
|
|
|
UsageInvalid ProfileUsage = iota
|
|
|
|
|
|
|
|
UsageOCSP
|
|
|
|
|
|
|
|
UsageClient
|
|
|
|
UsageCode
|
|
|
|
UsagePerson
|
|
|
|
UsageServer
|
|
|
|
UsageServerClient
|
|
|
|
|
|
|
|
UsageOrgClient
|
|
|
|
UsageOrgCode
|
|
|
|
UsageOrgEmail
|
|
|
|
UsageOrgPerson
|
|
|
|
UsageOrgServer
|
|
|
|
UsageOrgServerClient
|
|
|
|
)
|
2022-11-28 16:10:46 +00:00
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
var validProfileUsages = map[string]ProfileUsage{
|
|
|
|
"ocsp": UsageOCSP,
|
|
|
|
|
|
|
|
"client": UsageClient,
|
|
|
|
"code": UsageCode,
|
|
|
|
"person": UsagePerson,
|
|
|
|
"server": UsageServer,
|
|
|
|
"server_client": UsageServerClient,
|
|
|
|
|
|
|
|
"org_client": UsageOrgClient,
|
|
|
|
"org_code": UsageOrgCode,
|
|
|
|
"org_email": UsageOrgEmail,
|
|
|
|
"org_person": UsageOrgPerson,
|
|
|
|
"org_server": UsageOrgServer,
|
|
|
|
"org_server_client": UsageOrgServerClient,
|
2022-08-03 13:45:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
var profileUsageDetails = map[ProfileUsage]struct {
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
}{
|
|
|
|
UsageInvalid: {"invalid", "Invalid certificate profile, not to be used"},
|
|
|
|
|
|
|
|
UsageOCSP: {"ocsp", "OCSP responder signing certificate"},
|
|
|
|
|
|
|
|
UsageClient: {"client", "machine TLS client certificate"},
|
|
|
|
UsageCode: {"code", "individual code signing certificate"},
|
|
|
|
UsagePerson: {"person", "person identity certificate"},
|
|
|
|
UsageServer: {"server", "TLS server certificate"},
|
|
|
|
UsageServerClient: {"server_client", "combined TLS server and client certificate"},
|
|
|
|
|
|
|
|
UsageOrgClient: {"org_client", "organization machine TLS client certificate"},
|
|
|
|
UsageOrgCode: {"org_code", "organization code signing certificate"},
|
|
|
|
UsageOrgEmail: {"org_email", "organization email certificate"},
|
|
|
|
UsageOrgPerson: {"org_person", "organizational person identity certificate"},
|
|
|
|
UsageOrgServer: {"org_server", "organization TLS server certificate"},
|
|
|
|
UsageOrgServerClient: {
|
|
|
|
"org_server_client",
|
|
|
|
"combined organization TLS server and client certificate",
|
|
|
|
},
|
|
|
|
}
|
2022-11-20 09:07:02 +00:00
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
func (p ProfileUsage) String() string {
|
|
|
|
name, ok := profileUsageDetails[p]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Sprintf("unknown profile usage %d", p)
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
return name.Name
|
2022-11-20 09:07:02 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
func (p ProfileUsage) Description() string {
|
|
|
|
name, ok := profileUsageDetails[p]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Sprintf("unknown profile usage %d", p)
|
|
|
|
}
|
2022-11-30 17:42:40 +00:00
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
return name.Description
|
2022-11-30 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func ParseUsage(u string) (ProfileUsage, error) {
|
|
|
|
usage, ok := validProfileUsages[u]
|
|
|
|
if !ok {
|
2022-12-01 10:34:07 +00:00
|
|
|
return UsageInvalid, fmt.Errorf("unsupported profile usage: %s", u)
|
2022-11-30 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return usage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CAProfile struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
UseFor ProfileUsage `json:"use-for"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p CAProfile) String() string {
|
|
|
|
return fmt.Sprintf("profile['%s': '%s']", p.Name, p.UseFor)
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
type CertificateStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
CertStatusOk CertificateStatus = "ok"
|
|
|
|
CertStatusFailed CertificateStatus = "failed"
|
|
|
|
)
|
|
|
|
|
2022-11-30 17:42:40 +00:00
|
|
|
type CertificateInfo struct {
|
2022-12-01 10:34:07 +00:00
|
|
|
Status CertificateStatus `json:"status"`
|
|
|
|
Signing bool `json:"signing"`
|
|
|
|
Profiles []CAProfile `json:"profiles,omitempty"`
|
|
|
|
ValidUntil time.Time `json:"valid-until"`
|
2022-11-30 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i CertificateInfo) String() string {
|
|
|
|
marshal, _ := json.Marshal(i)
|
|
|
|
|
|
|
|
return string(marshal)
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
var CertificateInfoFailed = CertificateInfo{
|
|
|
|
Status: CertStatusFailed,
|
|
|
|
Signing: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseCertificateInfo(info string) (*CertificateInfo, error) {
|
2022-11-30 17:42:40 +00:00
|
|
|
certInfo := CertificateInfo{}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(info), &certInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse certificate information: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &certInfo, nil
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
type HealthInfo struct {
|
|
|
|
Source string
|
|
|
|
Healthy bool
|
|
|
|
MoreInfo map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *HealthInfo) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(builder, "source: %s, healthy: %v", i.Source, i.Healthy)
|
|
|
|
|
|
|
|
if len(i.MoreInfo) > 0 {
|
|
|
|
keys := make([]string, 0, len(i.MoreInfo))
|
|
|
|
parts := make([]string, len(i.MoreInfo))
|
|
|
|
|
|
|
|
for k := range i.MoreInfo {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for j, k := range keys {
|
|
|
|
parts[j] = fmt.Sprintf("'%s': '%s'", k, i.MoreInfo[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.WriteRune('[')
|
|
|
|
builder.WriteString(strings.Join(parts, ", "))
|
|
|
|
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{}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
_, _ = fmt.Fprintf(builder, "signer version=%s, healthy=%v, health data=[", h.Version, h.Healthy)
|
|
|
|
|
|
|
|
infos := make([]string, len(h.Info))
|
2022-11-20 17:59:37 +00:00
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
for i, info := range h.Info {
|
|
|
|
infos[i] = fmt.Sprintf("{%s}", info)
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 10:34:07 +00:00
|
|
|
builder.WriteString(strings.Join(infos, ", "))
|
|
|
|
|
|
|
|
builder.WriteRune(']')
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type FetchCRLResponse struct {
|
2022-11-28 16:10:46 +00:00
|
|
|
IssuerID string `msgpack:"issuer_id"`
|
|
|
|
IsDelta bool `msgpack:"is_delta"`
|
2022-11-30 17:47:18 +00:00
|
|
|
UnChanged bool `msgpack:"unchanged"`
|
2022-11-28 16:10:46 +00:00
|
|
|
CRLData []byte `msgpack:"crl_data"`
|
2022-11-30 17:47:18 +00:00
|
|
|
CRLNumber []byte `msgpack:"crl_number"`
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *FetchCRLResponse) String() string {
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
_, _ = fmt.Fprintf(
|
|
|
|
builder,
|
2022-12-01 10:34:07 +00:00
|
|
|
"issuer id=%s, delta=%t, unchanged=%t, CRL number=0x%x",
|
2022-11-30 17:47:18 +00:00
|
|
|
r.IssuerID,
|
|
|
|
r.IsDelta,
|
|
|
|
r.UnChanged,
|
2022-12-01 10:34:07 +00:00
|
|
|
new(big.Int).SetBytes(r.CRLNumber),
|
2022-11-30 17:47:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if r.UnChanged {
|
|
|
|
return builder.String()
|
|
|
|
}
|
2022-11-20 17:59:37 +00:00
|
|
|
|
|
|
|
if r.IsDelta {
|
2022-12-01 10:34:07 +00:00
|
|
|
_, _ = fmt.Fprintf(builder, ", delta CRL data of %d bytes not shown", len(r.CRLData))
|
2022-11-30 17:47:18 +00:00
|
|
|
|
|
|
|
return builder.String()
|
2022-11-20 17:59:37 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:47:18 +00:00
|
|
|
revocationList, err := x509.ParseRevocationList(r.CRLData)
|
|
|
|
if err != nil {
|
|
|
|
_, _ = fmt.Fprintf(builder, ", could not parse CRL: %s", err.Error())
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(
|
|
|
|
builder,
|
2022-12-01 10:34:07 +00:00
|
|
|
", CRL info: issuer=%s, number=0x%x, next update=%s, revoked certificates=%d",
|
2022-11-30 17:47:18 +00:00
|
|
|
revocationList.Issuer,
|
2022-12-01 10:34:07 +00:00
|
|
|
revocationList.Number,
|
2022-11-30 17:47:18 +00:00
|
|
|
revocationList.NextUpdate,
|
|
|
|
len(revocationList.RevokedCertificates),
|
|
|
|
)
|
|
|
|
_, _ = builder.WriteString(", CRL data:\n")
|
|
|
|
_ = pem.Encode(builder, &pem.Block{
|
|
|
|
Type: "CERTIFICATE REVOCATION LIST",
|
|
|
|
Bytes: r.CRLData,
|
|
|
|
})
|
|
|
|
|
2022-11-20 17:59:37 +00:00
|
|
|
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
|
|
|
|
2022-11-28 16:10:46 +00:00
|
|
|
func (e *ErrorResponse) String() string {
|
|
|
|
return fmt.Sprintf("message=%s", e.Message)
|
|
|
|
}
|