certs

package
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 14 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 certs is a tiny demo PKI for the separated client/server holt examples. It generates a CA plus two leaf certificates — one named "hub", one named "peer" — each valid for BOTH server and client authentication, and writes them as PEM files that two separate processes can load.

A single leaf that works in either TLS role keeps the examples simple: the transport-tls hub uses the "hub" cert as its server cert while the peer uses "peer" as its client cert; the encrypted example (roles inverted, peer is the TLS server) uses the same two files the other way round. Both sides trust the shared CA.

This is example scaffolding, not a real PKI: keys live in a temp dir, unencrypted. Never reuse it for anything real.

Index

Constants

View Source
const (
	Hub  = "hub"
	Peer = "peer"
)

Names of the two identities issued by EnsureDir.

Variables

This section is empty.

Functions

func DefaultDir

func DefaultDir() string

DefaultDir is the shared location both example binaries default to, so the server can generate the certs and the client can read them without any flags.

func EnsureDir

func EnsureDir(dir string) error

EnsureDir generates the CA + hub + peer certificates into dir if they are not already present. Idempotent: an existing ca.pem short- circuits. Start the server (which calls this) before the client.

func Issue

func Issue(dir, name string) (tls.Certificate, error)

Issue mints a fresh leaf certificate for name, signed by the demo CA, and returns it in memory (nothing written to disk). It lets a client present a certificate carrying its OWN identity — the CN the hub then reads as the authenticated peer id. Requires EnsureDir to have created ca.pem / ca-key.pem.

func Load

func Load(dir, name string) (tls.Certificate, error)

Load returns the tls.Certificate for the named identity (Hub or Peer).

func LoadCA

func LoadCA(dir string) (*x509.CertPool, error)

LoadCA returns a pool containing the demo CA — the trust anchor for verifying the other side's certificate.

Types

type Bundle

type Bundle struct {
	CA   []byte `json:"ca"`
	Cert []byte `json:"cert"`
	Key  []byte `json:"key"`
}

Bundle is a client's credential set as PEM: the CA to trust plus the client's own certificate and key. Encode it to a copy-pasteable token; DecodeBundle reverses it.

func DecodeBundle

func DecodeBundle(token string) (Bundle, error)

DecodeBundle parses a token produced by Bundle.Encode.

func (Bundle) ClientTLS

func (b Bundle) ClientTLS(serverName string) (*tls.Config, error)

ClientTLS builds a mutual-TLS client config from the bundle: present our cert, verify the server's cert (with the expected serverName) against the bundled CA.

func (Bundle) Encode

func (b Bundle) Encode() string

Encode returns a single-line base64 token carrying the bundle.

type PKI

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

PKI is an in-memory certificate authority for one demo run.

func NewPKI

func NewPKI() (*PKI, error)

NewPKI generates a fresh in-memory CA.

func (*PKI) ClientBundle

func (p *PKI) ClientBundle(name string) (Bundle, error)

ClientBundle issues a leaf for name and packages it with the CA into a Bundle the client can load without touching disk.

func (*PKI) Pool

func (p *PKI) Pool() *x509.CertPool

Pool returns a cert pool trusting this CA — used to verify the other side's certificate.

func (*PKI) ServerCert

func (p *PKI) ServerCert(name string) (tls.Certificate, error)

ServerCert issues a leaf certificate for name, for the hub to present on its TLS listener.

Jump to

Keyboard shortcuts

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