pki

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pki implements the cluster's own certificate authority (ADR 0006, FA-PKI-01..04): an ECDSA P-256 CA created at bootstrap, node certificates issued from CSRs, peer verification against the CA bundle and the revocation list, and one-time join tokens that pin the CA fingerprint.

Node identities are carried in a URI SAN "uc://<cluster-id>/<role>/<node-id>[?tenant=<tenant-id>]"; host names are not used, because nodes move and advertise addresses change (FA-CLU-11).

Index

Constants

View Source
const (
	CAValidity   = 10 * 365 * 24 * time.Hour
	LeafValidity = 90 * 24 * time.Hour
	// Backdate tolerates clock skew between nodes (PF-11).
	Backdate = 5 * time.Minute
)

Validity periods (ADR 0006, FA-PKI-03).

Variables

View Source
var (
	ErrNoPeerCertificate = errors.New("peer presented no certificate")
	ErrRevoked           = errors.New("peer certificate is revoked")
	ErrWrongCluster      = errors.New("peer belongs to another cluster")
	ErrWrongPeer         = errors.New("peer is not the expected node")
)

Errors of peer verification.

View Source
var ErrInvalidToken = errors.New("invalid join token")

ErrInvalidToken means the token is malformed. Callers report every token failure the same way, so a client cannot tell why it was rejected.

Functions

func ClientConfig

func ClientConfig(h *Holder, v *Verifier, expectNodeID string, check PeerCheck, nextProtos ...string) *tls.Config

ClientConfig returns the TLS configuration for connecting to another node. The server is verified against the CA bundle instead of a host name; with expectNodeID set, only that node is accepted.

func Fingerprint

func Fingerprint(cert *x509.Certificate) string

Fingerprint identifies a CA by the SHA-256 of its public key info, as embedded in join tokens (FA-PKI-02). It survives re-signing the CA certificate with the same key.

func HashSecret

func HashSecret(secret string) string

HashSecret is what the cluster stores. The secret has 256 bits of entropy, so a plain domain-separated SHA-256 is sufficient (no password hashing).

func JoinClientConfig

func JoinClientConfig(fingerprint string, ca *atomic.Pointer[x509.Certificate]) *tls.Config

JoinClientConfig is used by a joining node that has no certificate yet. It trusts only a voter certificate issued by a CA whose fingerprint matches the join token (FA-PKI-02); the server must send the CA certificate after its leaf. It returns the pinned CA through ca once the handshake succeeded.

func Leaf

func Leaf(leaf *x509.Certificate, key any, ca *x509.Certificate) *tls.Certificate

Leaf builds the TLS certificate of a node: its leaf followed by the CA, so joining nodes can pin the CA (JoinClientConfig).

func NewCSR

func NewCSR(key *ecdsa.PrivateKey) ([]byte, error)

NewCSR creates a certificate signing request for key. The private key never leaves the node.

func NewKey

func NewKey() (*ecdsa.PrivateKey, error)

NewKey creates a node or CA key (ECDSA P-256).

func OnlyVoters

func OnlyVoters(id Identity) error

OnlyVoters accepts voter certificates only (Raft, replication between voters).

func RenewAt

func RenewAt(cert *x509.Certificate) time.Time

RenewAt returns when a certificate should be renewed: after two thirds of its lifetime (ADR 0006).

func SecretMatches

func SecretMatches(secret, storedHash string) bool

SecretMatches compares a presented secret with a stored hash in constant time.

func SerialString

func SerialString(cert *x509.Certificate) string

SerialString is the canonical form of a serial number in the revocation list.

func ServerConfig

func ServerConfig(h *Holder, v *Verifier, requireClient bool, check PeerCheck, nextProtos ...string) *tls.Config

ServerConfig returns the TLS configuration of the cluster port. With requireClient false a client may connect without a certificate (only the Join RPC allows that); a presented certificate is always verified, and callers must check ConnectionState().PeerCertificates per RPC.

Session tickets are disabled and all checks run in VerifyConnection, which also runs for resumed sessions, so a revoked certificate cannot reconnect through resumption.

Types

type CA

type CA struct {
	Cert *x509.Certificate
	Key  *ecdsa.PrivateKey
}

CA is the cluster certificate authority.

func NewCA

func NewCA(clusterID string, now time.Time) (*CA, error)

NewCA creates the CA of a new cluster.

func ParseCA

func ParseCA(certDER, keyDER []byte) (*CA, error)

ParseCA loads a CA from its certificate and PKCS#8 key (both DER).

func (*CA) ClusterID

func (ca *CA) ClusterID() string

ClusterID returns the cluster the CA belongs to.

func (*CA) Issue

func (ca *CA) Issue(csrDER []byte, id Identity, now time.Time) (*x509.Certificate, error)

Issue signs a node certificate for the public key of csrDER. The identity comes from the caller (join token or existing certificate), never from the CSR, whose subject and SANs are ignored.

func (*CA) MarshalKey

func (ca *CA) MarshalKey() ([]byte, error)

MarshalKey returns the CA key as PKCS#8 DER, for encryption (ADR 0011).

type Holder

type Holder struct {
	// contains filtered or unexported fields
}

Holder serves the node's current certificate to TLS callbacks; replacing it takes effect at the next handshake (FA-PKI-03).

func (*Holder) Get

func (h *Holder) Get() *tls.Certificate

Get returns the current certificate, or nil.

func (*Holder) Set

func (h *Holder) Set(c *tls.Certificate)

Set replaces the certificate.

type Identity

type Identity struct {
	ClusterID string
	NodeID    string
	Role      Role
	// TenantID is set for private probes of a tenant (FA-PRB-05).
	TenantID string
}

Identity is who a node certificate belongs to.

func IdentityOf

func IdentityOf(cert *x509.Certificate) (Identity, error)

IdentityOf reads the identity from a node certificate. It does not verify the certificate; use Verifier for that.

func (Identity) URI

func (id Identity) URI() *url.URL

URI returns the identity as URI SAN.

type JoinToken

type JoinToken struct {
	ID          string
	Secret      string
	Fingerprint string
}

JoinToken is a parsed join token.

func NewJoinToken

func NewJoinToken(caFingerprint string) (JoinToken, error)

NewJoinToken creates a token for the CA with the given fingerprint.

func ParseJoinToken

func ParseJoinToken(s string) (JoinToken, error)

ParseJoinToken checks the format strictly.

func (JoinToken) String

func (t JoinToken) String() string

String formats the token for the operator.

type PeerCheck

type PeerCheck func(Identity) error

PeerCheck may restrict which identities are accepted (e.g. only voters).

type Role

type Role string

Role is the cluster role a certificate grants.

const (
	RoleVoter Role = "voter"
	RoleProbe Role = "probe"
)

Roles (FA-PKI-02).

func (Role) Valid

func (r Role) Valid() bool

Valid reports whether r is a known role.

type Verifier

type Verifier struct {
	ClusterID string
	// ClusterIDFunc, if set, replaces ClusterID; nodes learn the cluster ID
	// only after bootstrap or join.
	ClusterIDFunc func() string
	// Roots returns the trusted CA certificates (several during CA rotation).
	Roots func() []*x509.Certificate
	// Revoked reports whether a serial (SerialString) is revoked.
	Revoked func(serial string) bool
	// RevokedNode returns when a node was revoked (removed from the cluster);
	// its certificates issued before that time are rejected, a later re-join
	// gets a valid one.
	RevokedNode func(nodeID string) (time.Time, bool)
	// Now is the clock; nil uses time.Now.
	Now func() time.Time
}

Verifier checks peer certificates against the trusted CA bundle and the revocation list (FA-PKI-01, FA-PKI-04). Its functions are read on every handshake, so updates (CA rotation, new revocations) apply immediately.

func (*Verifier) Verify

func (v *Verifier) Verify(chain []*x509.Certificate, usage x509.ExtKeyUsage) (Identity, error)

Verify checks a peer chain (leaf first) for the given usage and returns the peer's identity. Only certificates issued directly by a trusted CA are accepted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL