ca

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 22 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 ca is the controller's certificate authority: the root of trust of a fabric the controller owns (its RCAC, optionally an ICAC), which issues the controller's own NOC and the NOC of every node it commissions. It is the port of matter.js packages/protocol/src/certificate/CertificateAuthority.ts at 0ceb202d (ADR 0020, stage 2), with the Matter TLV certificate encoder the commissioner of bridge/bridgetest uses (ca.go there) grown into production code, and the compressed fabric id derivation of packages/types/src/datatype/GlobalFabricId.ts.

A Store keeps the root (and ICAC) key material and the certificate counter across restarts; MemoryStore keeps them in memory. Unlike matter.js, which writes its storage only when it creates the root, the CA saves after every certificate it issues, so serial numbers do not repeat after a restart.

Config.RootSigner and Config.ICACSigner take the keys out of the store: any P-256 crypto.Signer — the passphrase-encrypted file of package keyfile, or a host's PKCS#11 or KMS signer (keyfile's package documentation says how to wire one). The record then holds the certificates and a KeyRef, no private key; without a signer the store keeps the key as matter.js does. commissioner.FabricConfig.OperationalSigner is the same seam for the controller's operational key. matter.js has no such seam (packages/protocol/src/fabric/Fabric.ts:75 at db021da0 keeps #keyPair).

Index

Constants

View Source
const (
	RootCertID         uint64 = 0
	FirstCertificateID uint64 = 1
)

Certificate ids. Mirrors matter.js packages/protocol/src/certificate/CertificateAuthority.ts:52 (#rootCertId = 0) and :56 (#nextCertificateId = 1) at db021da0.

View Source
const (
	NotBeforeYears = -1
	NotAfterYears  = 10
)

Validity of every certificate the CA issues, in years around the issuing instant — CertificateAuthority.ts:233-234, :263-264, :304-305 (jsToMatterDate(now, -1), jsToMatterDate(now, 10)).

View Source
const DefaultSettingKey = "controller.ca"

DefaultSettingKey is the key a SettingStore with no Key uses.

View Source
const ExternalKeyRef = "external"

ExternalKeyRef is the KeyRef recorded for a signer that is no KeyReferencer.

Variables

View Source
var ErrConfig = errors.New("ca: stored credentials do not match the configuration")

ErrConfig is a stored CA that does not fit the configuration: its ICAC presence differs from Config.IntermediateCA, or it has neither an ICAC nor a root private key to sign with — matter.js ImplementationError (CertificateAuthority.ts:367-373, :382-387). It is also a stored record New does not load and does not replace: one that is incomplete (the root's key identifier or certificate missing, an ICAC with some but not all of its parts), whose certificate counter would repeat a serial, or whose stored root or ICAC key is not its certificate's, and one whose JSON, keys or certificates do not parse (review II, R2-P6-store-1851–1853, Fable read F4).

View Source
var ErrInvalidCATs = errors.New("ca: invalid CASE Authenticated Tag list")

ErrInvalidCATs is a CASE Authenticated Tag list GenerateNOC refuses to sign: more than three tags, two tags with the same identifier (the upper 16 bits), or a tag with version 0 (the lower 16 bits). matter.js applies the same rules when it turns the subject into ASN.1 (kinds/Noc.ts:116 → CaseAuthenticatedTag.validateNocTagList, datatype/CaseAuthenticatedTag.ts:96-105; version 0 refused at :32-33), so a NOC the device would refuse at AddNOC (InvalidNOC) is never issued.

Functions

func CompressedFabricID

func CompressedFabricID(rootPub []byte, fabricID uint64) ([8]byte, error)

CompressedFabricID is the compressed fabric id of fabricID under the root public key rootPub (65-byte uncompressed): HKDF-SHA256 with the key without its 0x04 prefix as input, the fabric id big-endian as salt, "CompressedFabric" as info, 8 bytes. Mirrors matter.js packages/types/src/datatype/GlobalFabricId.ts:11, :36-47 (GlobalFabricId.compute; DataWriter is big-endian by default, general/src/util/DataWriter.ts:12-19) at db021da0.

func KeyIdentifier

func KeyIdentifier(pub []byte) []byte

KeyIdentifier is the SHA-1 key identifier of a public key — matter.js CertificateAuthority.ts:170, :254, :314 (computeHash(publicKey, "SHA-1")).

func KeyRefOf added in v0.7.0

func KeyRefOf(s crypto.Signer) string

KeyRefOf is the KeyRef the record keeps for s.

func P256PublicKey added in v0.7.0

func P256PublicKey(s crypto.Signer) ([]byte, error)

P256PublicKey is the 65-byte uncompressed public key of s, an error when s is not a P-256 ECDSA key.

Types

type CA

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

CA is a fabric's certificate authority — matter.js CertificateAuthority (CertificateAuthority.ts:50-437) at b25ed960. It is safe for concurrent use.

func New

func New(cfg Config) (*CA, error)

New loads the CA cfg.Store holds, or creates one. Mirrors matter.js CertificateAuthority.ts:149-184 (the construction): a valid stored root (:336-346) is loaded (:357-389) and must agree with cfg.IntermediateCA (:367-373); with nothing stored, a P-256 root key, its SHA-1 key identifier (:169-170), the RCAC (:225-249), the ICAC when configured (:251-285), and the result is saved (:181-183). A stored record that is not valid is ErrConfig and stays as it is — never replaced, as matter.js replaces it; the module's own rule (review II, R2-P6-store-1851, owner decision 191).

func (*CA) GenerateNOC

func (c *CA) GenerateNOC(pub []byte, fabricID, nodeID uint64, cats []uint32) ([]byte, error)

GenerateNOC issues a NOC for the subject key pub (65-byte uncompressed P-256), on fabricID for nodeID with the CASE Authenticated Tags cats. Mirrors matter.js CertificateAuthority.ts:287-320 (generateNoc) at db021da0: the next certificate id; issuer and signer the ICAC when there is one, else the root (#getSigningParameters :411-429); subject {fabricId, nodeId, caseAuthenticatedTags} in that order (the DN list keeps the caller's order: kinds/definitions/operational.ts:111 makes it a TlvDistinguishedName, whose DistinguishedNameSchema.encodeTlvInternal and rdnsOfProperties keep it, kinds/DistinguishedName.ts:613-630, :460-482); validity −1 / +10 years; not a CA; keyUsage digitalSignature; extendedKeyUsage [clientAuth, serverAuth]; SHA-1 of pub as the subject key identifier and the signer's as the authority key identifier.

Unlike matter.js, which writes the counter only when it creates the root (:181-183), the CA saves after each issue so serial numbers do not repeat after a restart; a save error fails the issue.

func (*CA) ICACCert

func (c *CA) ICACCert() []byte

ICACCert is the ICAC as Matter TLV, nil without one — matter.js icacCert (:198-203).

func (*CA) RootCert

func (c *CA) RootCert() []byte

RootCert is the RCAC as Matter TLV — matter.js rootCert (:194-196).

func (*CA) RootPublicKey

func (c *CA) RootPublicKey() []byte

RootPublicKey is the 65-byte uncompressed root public key.

func (*CA) Verifier

func (c *CA) Verifier() *mattercert.Verifier

Verifier is the verifier of NOC chains over the CA's root, with the CA's clock.

type Config

type Config struct {
	// Store keeps the CA; nil keeps nothing (a fresh root every New, as
	// matter.js without a storage context).
	Store Store
	// Clock is the issuing instant of every certificate and the
	// verifier's time; nil is [clock.System].
	Clock clock.Clock
	// IntermediateCA issues an ICAC that signs the NOCs (3-tier PKI). A
	// stored CA must match it.
	IntermediateCA bool
	// DetectIntermediateCA lets a stored CA decide IntermediateCA: a
	// stored ICAC is loaded and required, a stored CA without one stays
	// 2-tier. A new CA follows IntermediateCA. It is matter.js's
	// generateIntermediateCert left undefined (CertificateAuthority.ts:157-159).
	DetectIntermediateCA bool
	// Rand is the randomness of keys and signatures; nil is crypto/rand.
	// Since Go 1.26 crypto/ecdsa ignores a caller's reader for key
	// generation and signing (a secure source is always used; see
	// ecdsa.GenerateKey), so a seeded reader no longer makes the
	// certificates reproducible — a test that needs that uses
	// testing/cryptotest.SetGlobalRandom.
	Rand io.Reader
	// RootSigner, when set, is the root key: a P-256 crypto.Signer whose
	// private key the store never sees (a key file, an HSM, a KMS). A new
	// CA issues its RCAC for the signer's public key; a stored CA must
	// have been made under it. The stored record then holds the
	// certificates and RootKeyRef, no root private key; a record that
	// still holds one is rewritten without it. matter.js has no such seam
	// (its CertificateAuthority keeps the key pair in its storage).
	RootSigner crypto.Signer
	// ICACSigner is RootSigner for the ICAC key, with IntermediateCA.
	ICACSigner crypto.Signer
}

Config configures New.

type FabricRecord

type FabricRecord struct {
	FabricID     uint64 `json:"fabricId"`
	RootNodeID   uint64 `json:"rootNodeId"`
	RootVendorID uint16 `json:"rootVendorId"`
	// IPK is the 16-byte epoch identity protection key.
	IPK         []byte `json:"identityProtectionKey"`
	FabricIndex uint8  `json:"fabricIndex"`
	Label       string `json:"label"`
	// KeyPair is the controller's operational key; NOC its operational
	// certificate. Both nil makes the loader mint a new NOC from the CA.
	KeyPair *KeyPair `json:"keyPair,omitempty"`
	NOC     []byte   `json:"operationalCert,omitempty"`
	// KeyRef names the operational key when it is a signer
	// (commissioner.FabricConfig.OperationalSigner); KeyPair then holds
	// the public key only. Not in matter.js's Fabric.Config.
	KeyRef string `json:"keyRef,omitempty"`
	// RootPublicKey is the RCAC public key the record was made under.
	RootPublicKey []byte `json:"rootPublicKey,omitempty"`
}

FabricRecord is what a controller keeps of the one fabric it owns, beside the CA that roots it: enough to rebuild the same operational identity on the next start, so every node it commissioned still trusts it.

It is matter.js Fabric.Config (packages/protocol/src/fabric/Fabric.ts:714-733 at db021da0), which FabricManager persists as a list under "fabrics" with "nextFabricIndex" beside it (packages/protocol/src/fabric/FabricManager.ts:68-73), narrowed to one fabric per store. Kept: fabricIndex, fabricId, rootNodeId (which is the controller's nodeId on its own fabric), rootVendorId, identityProtectionKey, label, keyPair and operationalCert, and the derived rootPublicKey as a consistency check against the CA. Dropped: rootCert and intermediateCACert (the CA's Stored holds them), vidVerificationStatement and vvsc (no VID verification on the controller side), globalId and operationalIdentityProtectionKey (derived again on load), and nextFabricIndex (no fabric list).

type FabricStore

type FabricStore interface {
	// LoadFabric returns what is stored; ok is false when nothing is.
	LoadFabric() (r FabricRecord, ok bool, err error)
	// SaveFabric replaces what is stored.
	SaveFabric(r FabricRecord) error
}

FabricStore keeps a FabricRecord.

type KeyPair

type KeyPair struct {
	PublicKey  []byte
	PrivateKey []byte
}

KeyPair is a P-256 key pair as the store keeps it: the 65-byte uncompressed public key and the 32-byte private scalar — matter.js BinaryKeyPair.

func (KeyPair) MarshalJSON

func (k KeyPair) MarshalJSON() ([]byte, error)

MarshalJSON gives KeyPair matter.js BinaryKeyPair's field names; a key pair whose private key a signer keeps has no privateKey.

func (*KeyPair) UnmarshalJSON

func (k *KeyPair) UnmarshalJSON(b []byte) error

UnmarshalJSON is the inverse of KeyPair.MarshalJSON.

type KeyReferencer added in v0.7.0

type KeyReferencer interface {
	KeyRef() string
}

KeyReferencer is a signer that names its key for the stored record — a path, a PKCS#11 URI, a KMS key name. A Config.RootSigner or Config.ICACSigner that is none is recorded as ExternalKeyRef.

type MemoryStore

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

MemoryStore is a Store and a FabricStore in memory. The zero value is empty and ready to use; it is safe for concurrent use.

func (*MemoryStore) Load

func (m *MemoryStore) Load() (Stored, bool, error)

Load implements Store.

func (*MemoryStore) LoadFabric

func (m *MemoryStore) LoadFabric() (FabricRecord, bool, error)

LoadFabric implements FabricStore.

func (*MemoryStore) Save

func (m *MemoryStore) Save(s Stored) error

Save implements Store.

func (*MemoryStore) SaveFabric

func (m *MemoryStore) SaveFabric(r FabricRecord) error

SaveFabric implements FabricStore.

func (*MemoryStore) Saves

func (m *MemoryStore) Saves() int

Saves is the number of Save calls so far.

type SettingStore

type SettingStore struct {
	Settings Settings
	// Key is the settings key of the CA; "" is "controller.ca".
	Key string
}

SettingStore is a Store and a FabricStore under keys of a Settings store: the CA as JSON under Key, the fabric record under Key+".fabric". The JSON field names are matter.js's (CertificateAuthority.Configuration, CertificateAuthority.ts:448-474; Fabric.Config, Fabric.ts:666-685), byte strings in base64.

func (SettingStore) FabricKey

func (s SettingStore) FabricKey() string

FabricKey is the settings key the fabric record is kept under.

func (SettingStore) Load

func (s SettingStore) Load() (Stored, bool, error)

Load implements Store.

func (SettingStore) LoadFabric

func (s SettingStore) LoadFabric() (FabricRecord, bool, error)

LoadFabric implements FabricStore.

func (SettingStore) Save

func (s SettingStore) Save(st Stored) error

Save implements Store.

func (SettingStore) SaveFabric

func (s SettingStore) SaveFabric(r FabricRecord) error

SaveFabric implements FabricStore.

type Settings

type Settings interface {
	GetSetting(ctx context.Context, key string) (string, bool, error)
	SetSetting(ctx context.Context, key, value string) error
}

Settings is a string key-value store — store.Settings' GetSetting and SetSetting, the table the node keeps its own settings in.

type Store

type Store interface {
	// Load returns what is stored; ok is false when nothing is.
	Load() (s Stored, ok bool, err error)
	// Save replaces what is stored.
	Save(s Stored) error
}

Store keeps a CA's key material and certificate counter.

type Stored

type Stored struct {
	// RootCertID is the RCAC id (rootCertId).
	RootCertID uint64
	// RootKeyIdentifier is the SHA-1 of the root public key.
	RootKeyIdentifier []byte
	// RootCertBytes is the RCAC as Matter TLV.
	RootCertBytes []byte
	// NextCertificateID is the id of the next certificate to issue.
	NextCertificateID uint64
	// RootKeyPair is the root key; optional with an ICAC (a 3-tier PKI
	// whose root key stays offline, :455-474).
	RootKeyPair *KeyPair
	// ICAC is the intermediate CA, nil for a 2-tier PKI.
	ICAC *StoredICAC
	// RootKeyRef names the root key when it is a [Config.RootSigner]: the
	// record then has no RootKeyPair, the key stays where the signer
	// keeps it. "" for a key in RootKeyPair.
	RootKeyRef string
}

Stored is what a Store keeps of a CA — matter.js CertificateAuthority.Configuration (CertificateAuthority.ts:446-484), written by #buildStorageData (:391-409).

type StoredICAC

type StoredICAC struct {
	CertID        uint64
	KeyPair       KeyPair
	KeyIdentifier []byte
	CertBytes     []byte
	// KeyRef names the ICAC key when it is a [Config.ICACSigner]; KeyPair
	// then holds the public key only.
	KeyRef string
}

StoredICAC is the ICAC part of Stored (icacCertId, icacKeyPair, icacKeyIdentifier, icacCertBytes).

Directories

Path Synopsis
Package keyfile keeps a P-256 key in a passphrase-encrypted file and signs with it as a crypto.Signer — the alternative to the SQLite record for [ca.Config.RootSigner], [ca.Config.ICACSigner] and commissioner.FabricConfig.OperationalSigner.
Package keyfile keeps a P-256 key in a passphrase-encrypted file and signs with it as a crypto.Signer — the alternative to the SQLite record for [ca.Config.RootSigner], [ca.Config.ICACSigner] and commissioner.FabricConfig.OperationalSigner.

Jump to

Keyboard shortcuts

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