mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 09:16:52 +01:00
Compare commits
49
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eff67f1159 | ||
|
|
a3dc54b012 | ||
|
|
e8d6cd9b58 | ||
|
|
d190ac7446 | ||
|
|
05410034e3 | ||
|
|
cbcf441d7c | ||
|
|
4b295e086e | ||
|
|
35b3d92bdd | ||
|
|
7c2dcd7ba1 | ||
|
|
4d42378537 | ||
|
|
cb9ab9c516 | ||
|
|
6c6bf5e486 | ||
|
|
d102107f2e | ||
|
|
b92806d199 | ||
|
|
437d47ffc5 | ||
|
|
bd82089a3f | ||
|
|
1d0281cba5 | ||
|
|
dff5c51dd6 | ||
|
|
656164b259 | ||
|
|
84e28c62ea | ||
|
|
6885bac85d | ||
|
|
5c5d6e3314 | ||
|
|
0aa15ef247 | ||
|
|
2ecf2f738e | ||
|
|
1bf0bf382c | ||
|
|
19578b3b6b | ||
|
|
5edcaefa33 | ||
|
|
bbade4dc9a | ||
|
|
cbf8146bda | ||
|
|
3b850c6ecf | ||
|
|
a508a4e22c | ||
|
|
e7b2abb0b2 | ||
|
|
5cc53c22db | ||
|
|
70e7d3f2ea | ||
|
|
4ce04760ae | ||
|
|
1e6a95df15 | ||
|
|
4d37a3140f | ||
|
|
b26c1e9c3e | ||
|
|
5c5254dc18 | ||
|
|
ab89556c5c | ||
|
|
1a8fb8d67e | ||
|
|
73ddb56c34 | ||
|
|
13658b74a0 | ||
|
|
22cdb0739a | ||
|
|
45e449a591 | ||
|
|
a36ce25af4 | ||
|
|
2e5adfddc6 | ||
|
|
31f3d6435e | ||
|
|
8f9a631f6e |
No files matched your search
@@ -1,2 +0,0 @@
|
|||||||
# Goldens are compared byte for byte, so they must come out of git the way they went in.
|
|
||||||
*.golden -text
|
|
||||||
@@ -42,7 +42,7 @@ jobs:
|
|||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v9
|
uses: golangci/golangci-lint-action@v9
|
||||||
with:
|
with:
|
||||||
version: v2.14
|
version: v2.12
|
||||||
|
|
||||||
test:
|
test:
|
||||||
name: Test ${{ matrix.name }}
|
name: Test ${{ matrix.name }}
|
||||||
|
|||||||
+17
-5
@@ -7,15 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
### Removed
|
### Added
|
||||||
|
|
||||||
- The SSH debug server's `start-cpu-profile`, `stop-cpu-profile`, `save-heap-profile`, `save-mutex-profile`, and
|
- Version 3 certificates and post-quantum hybrid handshakes (`ix_hybrid_mlkem1024`). A version 3 certificate is a
|
||||||
`mutex-profile-fraction` commands, and `sshd.sandbox_dir`, which is now ignored if set.
|
version 2 certificate extended with a post-quantum key and a deterministic post-quantum CA signature, and a node
|
||||||
|
holding one still presents the version 2 certificate it extends to peers that need it. Its post-quantum suite names
|
||||||
|
the algorithms, as its curve does the classical ones; the one suite today is `MLKEM1024_MLDSA87`, an ML-KEM-1024
|
||||||
|
key for hosts and ML-DSA-87 for CAs and their signatures, chosen with `nebula-cert -pq-suite`. `nebula-cert
|
||||||
|
pq-extend` upgrades existing CAs and hosts in place, and extending a certificate twice gives the same certificate.
|
||||||
|
See `docs/pq-handshake.md`.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- A version 3 certificate has two fingerprints: its own, and that of the version 2 certificate it extends. Both
|
||||||
|
identify it in logs and to `pki.blocklist`. **To block a version 3 certificate, block its v2 fingerprint**: the
|
||||||
|
pool checks it for the v3 certificate and for its v2 form alike, so that one entry covers both forms. Blocking only
|
||||||
|
the v3 fingerprint leaves the v2 form usable in classical handshakes. `nebula-cert print` leads with both
|
||||||
|
fingerprints and says which one to block; in JSON they are `fingerprint` and `v2Fingerprint`.
|
||||||
|
- Firewall `ca_sha` rules match a version 3 peer by either fingerprint of its CA.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Signing a v2 certificate with an empty name, a name longer than 253 bytes, or an empty group now fails.
|
|
||||||
These certificates were signed successfully but could not be loaded.
|
|
||||||
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
||||||
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
||||||
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
||||||
|
|||||||
+55
-1
@@ -15,6 +15,11 @@ import (
|
|||||||
type CAPool struct {
|
type CAPool struct {
|
||||||
CAs map[string]*CachedCertificate
|
CAs map[string]*CachedCertificate
|
||||||
certBlocklist map[string]struct{}
|
certBlocklist map[string]struct{}
|
||||||
|
|
||||||
|
// v3CAs indexes the version 3 CAs in CAs by the fingerprint of their v2 form, which is what the certificates
|
||||||
|
// they issue name as issuer. A version 3 CA only verifies version 3 certificates: version 2 certificates find
|
||||||
|
// their issuer in CAs, where only the v2 form of the CA, if listed itself, answers to that fingerprint.
|
||||||
|
v3CAs map[string]*CachedCertificate
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCAPool creates an empty CAPool
|
// NewCAPool creates an empty CAPool
|
||||||
@@ -22,6 +27,7 @@ func NewCAPool() *CAPool {
|
|||||||
ca := CAPool{
|
ca := CAPool{
|
||||||
CAs: make(map[string]*CachedCertificate),
|
CAs: make(map[string]*CachedCertificate),
|
||||||
certBlocklist: make(map[string]struct{}),
|
certBlocklist: make(map[string]struct{}),
|
||||||
|
v3CAs: make(map[string]*CachedCertificate),
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ca
|
return &ca
|
||||||
@@ -122,6 +128,17 @@ func (ncp *CAPool) AddCA(c Certificate) error {
|
|||||||
cc.InvertedGroups[g] = struct{}{}
|
cc.InvertedGroups[g] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v2 := V2Form(c); v2 != nil {
|
||||||
|
v2Sum, err := v2.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not calculate fingerprint for provided CA; error: %w; %s", err, c.Name())
|
||||||
|
}
|
||||||
|
if other, ok := ncp.v3CAs[v2Sum]; ok && other.Fingerprint != sum {
|
||||||
|
return fmt.Errorf("%s: two version 3 CAs extend the same version 2 CA %s", c.Name(), v2Sum)
|
||||||
|
}
|
||||||
|
ncp.v3CAs[v2Sum] = cc
|
||||||
|
}
|
||||||
|
|
||||||
ncp.CAs[sum] = cc
|
ncp.CAs[sum] = cc
|
||||||
|
|
||||||
if c.Expired(time.Now()) {
|
if c.Expired(time.Now()) {
|
||||||
@@ -180,11 +197,34 @@ func (ncp *CAPool) VerifyCertificate(now time.Time, c Certificate) (*CachedCerti
|
|||||||
return nil, ErrBlockListed
|
return nil, ErrBlockListed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Blocking the v2 form of a version 3 certificate blocks the certificate too.
|
||||||
|
var v2Fps []string
|
||||||
|
if v2 := V2Form(c); v2 != nil {
|
||||||
|
v2Fp, err := v2.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not calculate v2 fingerprint to verify: %w", err)
|
||||||
|
}
|
||||||
|
v2Fp2, err := CalculateAlternateFingerprint(v2)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not calculate alternate v2 fingerprint to verify: %w", err)
|
||||||
|
}
|
||||||
|
v2Fps = append(v2Fps, v2Fp)
|
||||||
|
if v2Fp2 != "" {
|
||||||
|
v2Fps = append(v2Fps, v2Fp2)
|
||||||
|
}
|
||||||
|
for _, f := range v2Fps {
|
||||||
|
if ncp.IsBlocklisted(f) {
|
||||||
|
return nil, ErrBlockListed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cc := CachedCertificate{
|
cc := CachedCertificate{
|
||||||
Certificate: c,
|
Certificate: c,
|
||||||
InvertedGroups: make(map[string]struct{}),
|
InvertedGroups: make(map[string]struct{}),
|
||||||
Fingerprint: fp,
|
Fingerprint: fp,
|
||||||
fingerprint2: fp2,
|
fingerprint2: fp2,
|
||||||
|
v2Fingerprints: v2Fps,
|
||||||
signerFingerprint: signer.Fingerprint,
|
signerFingerprint: signer.Fingerprint,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +242,11 @@ func (ncp *CAPool) VerifyCachedCertificate(now time.Time, c *CachedCertificate)
|
|||||||
if c.fingerprint2 != "" && ncp.IsBlocklisted(c.fingerprint2) {
|
if c.fingerprint2 != "" && ncp.IsBlocklisted(c.fingerprint2) {
|
||||||
return ErrBlockListed
|
return ErrBlockListed
|
||||||
}
|
}
|
||||||
|
for _, f := range c.v2Fingerprints {
|
||||||
|
if ncp.IsBlocklisted(f) {
|
||||||
|
return ErrBlockListed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err := ncp.verify(c.Certificate, now, c.Fingerprint, c.signerFingerprint)
|
_, err := ncp.verify(c.Certificate, now, c.Fingerprint, c.signerFingerprint)
|
||||||
return err
|
return err
|
||||||
@@ -221,6 +266,10 @@ func (ncp *CAPool) verify(c Certificate, now time.Time, certFp string, signerFp
|
|||||||
return nil, ErrCurveMismatch
|
return nil, ErrCurveMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if PQSuiteOf(signer.Certificate) != PQSuiteOf(c) {
|
||||||
|
return nil, ErrPQSuiteMismatch
|
||||||
|
}
|
||||||
|
|
||||||
if signer.Certificate.Expired(now) {
|
if signer.Certificate.Expired(now) {
|
||||||
return nil, ErrRootExpired
|
return nil, ErrRootExpired
|
||||||
}
|
}
|
||||||
@@ -257,7 +306,12 @@ func (ncp *CAPool) GetCAForCert(c Certificate) (*CachedCertificate, error) {
|
|||||||
return nil, fmt.Errorf("no issuer in certificate")
|
return nil, fmt.Errorf("no issuer in certificate")
|
||||||
}
|
}
|
||||||
|
|
||||||
signer, ok := ncp.CAs[issuer]
|
cas := ncp.CAs
|
||||||
|
if c.Version() == Version3 {
|
||||||
|
cas = ncp.v3CAs
|
||||||
|
}
|
||||||
|
|
||||||
|
signer, ok := cas[issuer]
|
||||||
if ok {
|
if ok {
|
||||||
return signer, nil
|
return signer, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,12 +14,14 @@ const (
|
|||||||
VersionPre1 Version = 0
|
VersionPre1 Version = 0
|
||||||
Version1 Version = 1
|
Version1 Version = 1
|
||||||
Version2 Version = 2
|
Version2 Version = 2
|
||||||
|
Version3 Version = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
type Certificate interface {
|
type Certificate interface {
|
||||||
// Version defines the underlying certificate structure and wire protocol version
|
// Version defines the underlying certificate structure and wire protocol version
|
||||||
// Version1 certificates are ipv4 only and uses protobuf serialization
|
// Version1 certificates are ipv4 only and uses protobuf serialization
|
||||||
// Version2 certificates are ipv4 or ipv6 and uses asn.1 serialization
|
// Version2 certificates are ipv4 or ipv6 and uses asn.1 serialization
|
||||||
|
// Version3 certificates extend a Version2 certificate with post-quantum keys and signatures
|
||||||
Version() Version
|
Version() Version
|
||||||
|
|
||||||
// Name is the human-readable name that identifies this certificate.
|
// Name is the human-readable name that identifies this certificate.
|
||||||
@@ -115,6 +117,9 @@ type CachedCertificate struct {
|
|||||||
|
|
||||||
// A place to store a 2nd fingerprint if the certificate could have one, such as with P256
|
// A place to store a 2nd fingerprint if the certificate could have one, such as with P256
|
||||||
fingerprint2 string
|
fingerprint2 string
|
||||||
|
|
||||||
|
// The fingerprints of a version 3 certificate's v2 form, which the blocklist also applies to
|
||||||
|
v2Fingerprints []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *CachedCertificate) String() string {
|
func (cc *CachedCertificate) String() string {
|
||||||
@@ -143,6 +148,8 @@ func Recombine(v Version, rawCertBytes, publicKey []byte, curve Curve) (Certific
|
|||||||
c, err = unmarshalCertificateV1(rawCertBytes, publicKey)
|
c, err = unmarshalCertificateV1(rawCertBytes, publicKey)
|
||||||
case Version2:
|
case Version2:
|
||||||
c, err = unmarshalCertificateV2(rawCertBytes, publicKey, curve)
|
c, err = unmarshalCertificateV2(rawCertBytes, publicKey, curve)
|
||||||
|
case Version3:
|
||||||
|
c, err = unmarshalCertificateV3(rawCertBytes, publicKey, curve)
|
||||||
default:
|
default:
|
||||||
return nil, ErrUnknownVersion
|
return nil, ErrUnknownVersion
|
||||||
}
|
}
|
||||||
@@ -165,6 +172,12 @@ func CalculateAlternateFingerprint(c Certificate) (string, error) {
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The post-quantum signature of a version 3 certificate covers its classical signature, so it has no second form.
|
||||||
|
// The pool checks the fingerprints of its v2 form separately.
|
||||||
|
if c.Version() == Version3 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|
||||||
nc := c.Copy()
|
nc := c.Copy()
|
||||||
b, err := p256.Swap(nc.Signature())
|
b, err := p256.Swap(nc.Signature())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+1
-10
@@ -389,16 +389,7 @@ func (c *certificateV2) fromTBSCertificate(t *TBSCertificate) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *certificateV2) validate() error {
|
func (c *certificateV2) validate() error {
|
||||||
// Mirror the name and group rules in unmarshalDetails so signing fails instead of producing an unparseable certificate
|
// Empty names are allowed
|
||||||
if c.details.name == "" || len(c.details.name) > MaxNameLength {
|
|
||||||
return NewErrInvalidCertificateProperties("name must be between 1 and %d bytes", MaxNameLength)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, group := range c.details.groups {
|
|
||||||
if group == "" {
|
|
||||||
return NewErrInvalidCertificateProperties("groups may not be empty")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(c.publicKey) == 0 {
|
if len(c.publicKey) == 0 {
|
||||||
return ErrInvalidPublicKey
|
return ErrInvalidPublicKey
|
||||||
|
|||||||
+516
@@ -0,0 +1,516 @@
|
|||||||
|
package cert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/mldsa"
|
||||||
|
"crypto/mlkem"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
"golang.org/x/crypto/cryptobyte/asn1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A version 3 certificate is a version 2 certificate extended with post-quantum keys. Its first fields are exactly
|
||||||
|
// the fields of the v2 certificate it extends, so a v3 holder can always marshal that v2 form, byte for byte, and use
|
||||||
|
// it with peers that only speak v2. The v2 form keeps its own classical CA signature and fingerprint.
|
||||||
|
//
|
||||||
|
// The certificate's PQSuite says which post-quantum algorithms it uses, the way its curve does for the classical
|
||||||
|
// ones. Hosts carry a public key for the suite's KEM and CAs one for its signature algorithm. The issuing CA, which
|
||||||
|
// has the same suite, signs every field with that signature algorithm, including the suite and the classical
|
||||||
|
// signature, so neither can be swapped out. Signatures are deterministic, so a certificate extended twice is the same
|
||||||
|
// certificate.
|
||||||
|
//
|
||||||
|
// Wherever a v3 certificate deals in keys they are composite: the classical key followed by the post-quantum one.
|
||||||
|
// PublicKey returns classical || post-quantum, and VerifyPrivateKey and signing take the private keys the same way.
|
||||||
|
|
||||||
|
const (
|
||||||
|
TagCertPQSuite = 4 | classContextSpecific
|
||||||
|
TagCertPQPublicKey = 5 | classContextSpecific
|
||||||
|
TagCertPQSignature = 6 | classContextSpecific
|
||||||
|
)
|
||||||
|
|
||||||
|
// Sizes of the keys and signatures of PQSuite_MLKEM1024_MLDSA87.
|
||||||
|
const (
|
||||||
|
// MLKEM1024PublicKeySize is the size of an ML-KEM-1024 encapsulation key.
|
||||||
|
MLKEM1024PublicKeySize = mlkem.EncapsulationKeySize1024
|
||||||
|
// MLKEM1024PrivateKeySize is the size of an ML-KEM-1024 decapsulation key seed.
|
||||||
|
MLKEM1024PrivateKeySize = mlkem.SeedSize
|
||||||
|
// MLDSA87PublicKeySize is the size of an ML-DSA-87 public key.
|
||||||
|
MLDSA87PublicKeySize = mldsa.MLDSA87PublicKeySize
|
||||||
|
// MLDSA87PrivateKeySize is the size of an ML-DSA-87 private key seed.
|
||||||
|
MLDSA87PrivateKeySize = mldsa.PrivateKeySize
|
||||||
|
// MLDSA87SignatureSize is the size of an ML-DSA-87 signature.
|
||||||
|
MLDSA87SignatureSize = mldsa.MLDSA87SignatureSize
|
||||||
|
|
||||||
|
// pqSignatureContext is the ML-DSA context string for certificate signatures.
|
||||||
|
pqSignatureContext = "nebula certificate v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
type certificateV3 struct {
|
||||||
|
// v2 is the version 2 certificate this one extends. Its public key is the classical key.
|
||||||
|
v2 certificateV2
|
||||||
|
|
||||||
|
// pqSuite names the post-quantum algorithms, and so the kind of pqPublicKey and pqSignature.
|
||||||
|
pqSuite PQSuite
|
||||||
|
|
||||||
|
// pqPublicKey is the suite's KEM public key of a host, or its signature algorithm's public key of a CA.
|
||||||
|
pqPublicKey []byte
|
||||||
|
|
||||||
|
// pqSignature is the issuing CA's post-quantum signature over every other field.
|
||||||
|
pqSignature []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// V2Form returns the version 2 certificate a version 3 certificate extends, or nil for any other version. It is
|
||||||
|
// what a v3 holder presents to peers that do not speak v3.
|
||||||
|
func V2Form(c Certificate) Certificate {
|
||||||
|
if c3, ok := c.(*certificateV3); ok {
|
||||||
|
return &c3.v2
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Version() Version {
|
||||||
|
return Version3
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Curve() Curve {
|
||||||
|
return c.v2.curve
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Groups() []string {
|
||||||
|
return c.v2.details.groups
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) IsCA() bool {
|
||||||
|
return c.v2.details.isCA
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Issuer() string {
|
||||||
|
return c.v2.details.issuer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Name() string {
|
||||||
|
return c.v2.details.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Networks() []netip.Prefix {
|
||||||
|
return c.v2.details.networks
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) NotAfter() time.Time {
|
||||||
|
return c.v2.details.notAfter
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) NotBefore() time.Time {
|
||||||
|
return c.v2.details.notBefore
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKey returns the composite public key, classical || post-quantum.
|
||||||
|
func (c *certificateV3) PublicKey() []byte {
|
||||||
|
k := make([]byte, 0, len(c.v2.publicKey)+len(c.pqPublicKey))
|
||||||
|
k = append(k, c.v2.publicKey...)
|
||||||
|
return append(k, c.pqPublicKey...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) MarshalPublicKeyPEM() []byte {
|
||||||
|
return marshalCertPublicKeyToPEM(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Signature returns the classical signature. The post-quantum signature covers it.
|
||||||
|
func (c *certificateV3) Signature() []byte {
|
||||||
|
return c.v2.signature
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) UnsafeNetworks() []netip.Prefix {
|
||||||
|
return c.v2.details.unsafeNetworks
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fingerprint is the sha256 of the whole certificate. It differs from the fingerprint of the v2 form.
|
||||||
|
func (c *certificateV3) Fingerprint() (string, error) {
|
||||||
|
b, err := c.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
sum := sha256.Sum256(b)
|
||||||
|
return hex.EncodeToString(sum[:]), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckSignature checks both signatures against the composite public key of the signing CA. Like the classical
|
||||||
|
// signature with the curve, the post-quantum signature is checked with the algorithm of this certificate's suite,
|
||||||
|
// which the signing CA has to share.
|
||||||
|
func (c *certificateV3) CheckSignature(key []byte) bool {
|
||||||
|
p := pqSuites[c.pqSuite]
|
||||||
|
if p == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
classical, pq, err := c.pqSuite.SplitPublicKey(true, key)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if !c.v2.CheckSignature(classical) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
b, err := c.marshalForPQSigning()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return p.dsa.verify(pq, b, c.pqSignature)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Expired(t time.Time) bool {
|
||||||
|
return c.v2.Expired(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyPrivateKey checks a composite private key, classical || post-quantum seed, against the composite public key.
|
||||||
|
func (c *certificateV3) VerifyPrivateKey(curve Curve, key []byte) error {
|
||||||
|
classical, seed, err := c.pqSuite.SplitPrivateKey(c.IsCA(), key)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := c.v2.VerifyPrivateKey(curve, classical); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pub, err := c.pqSuite.publicKey(c.IsCA(), seed)
|
||||||
|
if err != nil {
|
||||||
|
return ErrInvalidPrivateKey
|
||||||
|
}
|
||||||
|
if !bytes.Equal(pub, c.pqPublicKey) {
|
||||||
|
return ErrPublicPrivateKeyMismatch
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) String() string {
|
||||||
|
mb, err := c.marshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("<error marshalling certificate: %v>", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := json.MarshalIndent(mb, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("<error marshalling certificate: %v>", err)
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalForHandshakes leaves out both public keys, the handshake carries them as the composite static key. It keeps
|
||||||
|
// the suite, which says how to split that key.
|
||||||
|
func (c *certificateV3) MarshalForHandshakes() ([]byte, error) {
|
||||||
|
return c.marshal(false, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Marshal() ([]byte, error) {
|
||||||
|
return c.marshal(true, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshalForPQSigning returns the bytes the post-quantum signature covers: every field but that signature, with the
|
||||||
|
// curve always present. The suite is always present anyway.
|
||||||
|
func (c *certificateV3) marshalForPQSigning() ([]byte, error) {
|
||||||
|
return c.marshal(true, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) marshal(withPublicKeys, forSigning bool) ([]byte, error) {
|
||||||
|
if c.v2.rawDetails == nil {
|
||||||
|
return nil, ErrEmptyRawDetails
|
||||||
|
}
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(c.v2.rawDetails)
|
||||||
|
|
||||||
|
if withPublicKeys {
|
||||||
|
if forSigning || c.v2.curve != Curve_CURVE25519 {
|
||||||
|
b.AddASN1(TagCertCurve, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes([]byte{byte(c.v2.curve)})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
b.AddASN1(TagCertPublicKey, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(c.v2.publicKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
b.AddASN1(TagCertSignature, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(c.v2.signature)
|
||||||
|
})
|
||||||
|
|
||||||
|
b.AddASN1(TagCertPQSuite, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddUint8(uint8(c.pqSuite))
|
||||||
|
})
|
||||||
|
|
||||||
|
if withPublicKeys {
|
||||||
|
b.AddASN1(TagCertPQPublicKey, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(c.pqPublicKey)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !forSigning {
|
||||||
|
b.AddASN1(TagCertPQSignature, func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(c.pqSignature)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return b.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) MarshalPEM() ([]byte, error) {
|
||||||
|
b, err := c.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: CertificateV3Banner, Bytes: b}), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) MarshalJSON() ([]byte, error) {
|
||||||
|
b, err := c.marshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return json.Marshal(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) marshalJSON() (m, error) {
|
||||||
|
mb, err := c.v2.marshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fp, err := c.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
mb["v2Fingerprint"] = mb["fingerprint"]
|
||||||
|
mb["fingerprint"] = fp
|
||||||
|
mb["version"] = Version3
|
||||||
|
mb["pqSuite"] = c.pqSuite.String()
|
||||||
|
mb["pqPublicKey"] = fmt.Sprintf("%x", c.pqPublicKey)
|
||||||
|
mb["pqSignature"] = fmt.Sprintf("%x", c.pqSignature)
|
||||||
|
return mb, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) Copy() Certificate {
|
||||||
|
nc := &certificateV3{
|
||||||
|
v2: *c.v2.Copy().(*certificateV2),
|
||||||
|
pqSuite: c.pqSuite,
|
||||||
|
pqPublicKey: make([]byte, len(c.pqPublicKey)),
|
||||||
|
pqSignature: make([]byte, len(c.pqSignature)),
|
||||||
|
}
|
||||||
|
copy(nc.pqPublicKey, c.pqPublicKey)
|
||||||
|
copy(nc.pqSignature, c.pqSignature)
|
||||||
|
return nc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *certificateV3) validate() error {
|
||||||
|
if err := c.v2.validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.pqSuite.Valid() {
|
||||||
|
return fmt.Errorf("%w: %v", ErrUnknownPQSuite, c.pqSuite)
|
||||||
|
}
|
||||||
|
if !c.pqSuite.checkPublicKey(c.IsCA(), c.pqPublicKey) {
|
||||||
|
return ErrInvalidPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.pqSignature) != c.pqSuite.signatureSize() {
|
||||||
|
return ErrBadFormat
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalCertificateV3 parses a v3 certificate. publicKey is the composite public key when the certificate came
|
||||||
|
// from a handshake, which then must not carry either public key itself.
|
||||||
|
func unmarshalCertificateV3(b []byte, publicKey []byte, curve Curve) (*certificateV3, error) {
|
||||||
|
l := len(b)
|
||||||
|
if l == 0 || l > MaxCertificateSize {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
input := cryptobyte.String(b)
|
||||||
|
var body cryptobyte.String
|
||||||
|
if !input.ReadASN1(&body, asn1.SEQUENCE) || body.Empty() || !input.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawDetails cryptobyte.String
|
||||||
|
if !body.ReadASN1Element(&rawDetails, TagCertDetails) || rawDetails.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawCurve byte
|
||||||
|
if !readOptionalASN1Byte(&body, &rawCurve, TagCertCurve, byte(curve)) {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
curve = Curve(rawCurve)
|
||||||
|
|
||||||
|
fromHandshake := len(publicKey) > 0
|
||||||
|
var classical, pq cryptobyte.String
|
||||||
|
if fromHandshake {
|
||||||
|
if body.PeekASN1Tag(TagCertPublicKey) {
|
||||||
|
return nil, ErrCertPubkeyPresent
|
||||||
|
}
|
||||||
|
} else if !body.ReadASN1(&classical, TagCertPublicKey) || classical.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
var signature cryptobyte.String
|
||||||
|
if !body.ReadASN1(&signature, TagCertSignature) || signature.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawSuite cryptobyte.String
|
||||||
|
if !body.ReadASN1(&rawSuite, TagCertPQSuite) || len(rawSuite) != 1 {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
suite := PQSuite(rawSuite[0])
|
||||||
|
if !suite.Valid() {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, suite)
|
||||||
|
}
|
||||||
|
|
||||||
|
if fromHandshake {
|
||||||
|
if body.PeekASN1Tag(TagCertPQPublicKey) {
|
||||||
|
return nil, ErrCertPubkeyPresent
|
||||||
|
}
|
||||||
|
} else if !body.ReadASN1(&pq, TagCertPQPublicKey) || pq.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
var pqSignature cryptobyte.String
|
||||||
|
if !body.ReadASN1(&pqSignature, TagCertPQSignature) || pqSignature.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
if !body.Empty() {
|
||||||
|
return nil, ErrBadFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
details, err := unmarshalDetails(rawDetails)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if fromHandshake {
|
||||||
|
k, p, err := suite.SplitPublicKey(details.isCA, publicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
classical = bytes.Clone(k)
|
||||||
|
pq = bytes.Clone(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
c := &certificateV3{
|
||||||
|
v2: certificateV2{
|
||||||
|
details: details,
|
||||||
|
rawDetails: rawDetails,
|
||||||
|
curve: curve,
|
||||||
|
publicKey: classical,
|
||||||
|
signature: signature,
|
||||||
|
},
|
||||||
|
pqSuite: suite,
|
||||||
|
pqPublicKey: pq,
|
||||||
|
pqSignature: pqSignature,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.validate(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExtendToV3 builds the version 3 certificate in suite that extends the version 2 certificate c with pqPublicKey, the
|
||||||
|
// composite public key's post-quantum half. pqSign produces the suite's post-quantum signature, see SignPQ.
|
||||||
|
//
|
||||||
|
// A CA certificate signs its own extension: signer must be nil and pqSign must use the private key for pqPublicKey.
|
||||||
|
// A host certificate is extended by its issuing CA: signer is the v3 form of that CA, which must be in the same suite.
|
||||||
|
// In both cases c's classical signature, and so its fingerprint, are left unchanged, so certificates in use can be
|
||||||
|
// extended in place.
|
||||||
|
func ExtendToV3(c Certificate, suite PQSuite, pqPublicKey []byte, signer Certificate, pqSign SignerLambda) (Certificate, error) {
|
||||||
|
c2, ok := c.(*certificateV2)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("only version 2 certificates can be extended to version 3, got version %d", c.Version())
|
||||||
|
}
|
||||||
|
if !suite.Valid() {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, suite)
|
||||||
|
}
|
||||||
|
|
||||||
|
var signingKey []byte
|
||||||
|
if c2.IsCA() {
|
||||||
|
if signer != nil {
|
||||||
|
return nil, fmt.Errorf("a CA certificate must extend itself, signer must be nil")
|
||||||
|
}
|
||||||
|
if !c2.CheckSignature(c2.publicKey) {
|
||||||
|
return nil, ErrNotSelfSigned
|
||||||
|
}
|
||||||
|
signingKey = append(bytes.Clone(c2.publicKey), pqPublicKey...)
|
||||||
|
} else {
|
||||||
|
if signer == nil || signer.Version() != Version3 || !signer.IsCA() {
|
||||||
|
return nil, fmt.Errorf("a host certificate must be extended by a version 3 CA")
|
||||||
|
}
|
||||||
|
if PQSuiteOf(signer) != suite {
|
||||||
|
return nil, ErrPQSuiteMismatch
|
||||||
|
}
|
||||||
|
signerV2 := V2Form(signer)
|
||||||
|
issuer, err := signerV2.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if c2.Issuer() != issuer {
|
||||||
|
return nil, fmt.Errorf("certificate was not issued by the signing CA")
|
||||||
|
}
|
||||||
|
if !c2.CheckSignature(signerV2.PublicKey()) {
|
||||||
|
return nil, ErrSignatureMismatch
|
||||||
|
}
|
||||||
|
signingKey = signer.PublicKey()
|
||||||
|
}
|
||||||
|
|
||||||
|
c3 := &certificateV3{
|
||||||
|
v2: *c2.Copy().(*certificateV2),
|
||||||
|
pqSuite: suite,
|
||||||
|
pqPublicKey: bytes.Clone(pqPublicKey),
|
||||||
|
}
|
||||||
|
if len(c3.pqPublicKey) != suite.PublicKeySize(c3.IsCA()) {
|
||||||
|
return nil, ErrInvalidPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := c3.marshalForPQSigning()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sig, err := pqSign(b)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c3.pqSignature = sig
|
||||||
|
|
||||||
|
if err := c3.validate(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Catch a pqSign that used the wrong key before the certificate goes anywhere.
|
||||||
|
if !c3.CheckSignature(signingKey) {
|
||||||
|
return nil, ErrSignatureMismatch
|
||||||
|
}
|
||||||
|
|
||||||
|
return c3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SignPQ returns a SignerLambda that signs certificates with the private key seed of suite's signature algorithm.
|
||||||
|
// Signatures are deterministic, so signing the same certificate twice gives the same bytes and the same fingerprint,
|
||||||
|
// and extending a certificate is idempotent.
|
||||||
|
func SignPQ(suite PQSuite, seed []byte) (SignerLambda, error) {
|
||||||
|
p := pqSuites[suite]
|
||||||
|
if p == nil {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, suite)
|
||||||
|
}
|
||||||
|
if len(seed) != p.dsa.privateKeySize {
|
||||||
|
return nil, ErrInvalidPrivateKey
|
||||||
|
}
|
||||||
|
return p.dsa.signer(seed)
|
||||||
|
}
|
||||||
@@ -0,0 +1,493 @@
|
|||||||
|
package cert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/mldsa"
|
||||||
|
"crypto/mlkem"
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert/p256"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
"golang.org/x/crypto/cryptobyte/asn1"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testCurves = []Curve{Curve_CURVE25519, Curve_P256}
|
||||||
|
|
||||||
|
func newTestV3(t *testing.T, curve Curve) (ca Certificate, caKey []byte, host Certificate, hostKey []byte) {
|
||||||
|
t.Helper()
|
||||||
|
ca, _, caKey, _ = NewTestCaCert(Version3, curve, time.Time{}, time.Time{}, nil, nil, []string{"g"})
|
||||||
|
host, _, hostKeyPEM, _ := NewTestCert(Version3, curve, ca, caKey, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.1/16")}, nil, []string{"g"})
|
||||||
|
hostKey, _, _, err := UnmarshalPrivateKeyFromPEM(hostKeyPEM)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return ca, caKey, host, hostKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCertificateV3_Sign(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, caKey, host, hostKey := newTestV3(t, curve)
|
||||||
|
assert.Equal(t, Version3, ca.Version())
|
||||||
|
assert.Equal(t, Version3, host.Version())
|
||||||
|
|
||||||
|
assert.True(t, ca.CheckSignature(ca.PublicKey()))
|
||||||
|
assert.True(t, host.CheckSignature(ca.PublicKey()))
|
||||||
|
require.NoError(t, ca.VerifyPrivateKey(curve, caKey))
|
||||||
|
require.NoError(t, host.VerifyPrivateKey(curve, hostKey))
|
||||||
|
|
||||||
|
// The classical half of the key alone is not the key.
|
||||||
|
require.Error(t, host.VerifyPrivateKey(curve, hostKey[:len(hostKey)-MLKEM1024PrivateKeySize]))
|
||||||
|
|
||||||
|
// The issuer is the v2 form of the CA, which old peers know it by.
|
||||||
|
caV2Fp, err := V2Form(ca).Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, caV2Fp, host.Issuer())
|
||||||
|
|
||||||
|
// A v2 CA can not sign a v3 certificate, nor a v3 CA a v2 one.
|
||||||
|
_, err = (&TBSCertificate{Version: Version3, Curve: curve, PQSuite: DefaultPQSuite, Name: "x", Networks: host.Networks(), NotBefore: host.NotBefore(), NotAfter: host.NotAfter(), PublicKey: host.PublicKey()}).Sign(V2Form(ca), curve, caKey)
|
||||||
|
require.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The v2 form must be exactly the certificate the CA's v2 form would have signed, so peers that only speak v2
|
||||||
|
// accept it. Ed25519 signatures are deterministic, so the bytes can be compared outright.
|
||||||
|
func TestCertificateV3_V2FormIsExact(t *testing.T) {
|
||||||
|
ca, caKey, host, _ := newTestV3(t, Curve_CURVE25519)
|
||||||
|
classicalCAKey := caKey[:len(caKey)-MLDSA87PrivateKeySize]
|
||||||
|
classicalPub := V2Form(host).PublicKey()
|
||||||
|
|
||||||
|
tbs := &TBSCertificate{
|
||||||
|
Version: Version2,
|
||||||
|
Curve: Curve_CURVE25519,
|
||||||
|
Name: host.Name(),
|
||||||
|
Networks: host.Networks(),
|
||||||
|
Groups: host.Groups(),
|
||||||
|
NotBefore: host.NotBefore(),
|
||||||
|
NotAfter: host.NotAfter(),
|
||||||
|
PublicKey: classicalPub,
|
||||||
|
}
|
||||||
|
v2, err := tbs.Sign(V2Form(ca), Curve_CURVE25519, classicalCAKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
want, err := v2.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
got, err := V2Form(host).Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, want, got)
|
||||||
|
|
||||||
|
wantHS, err := v2.MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
gotHS, err := V2Form(host).MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, wantHS, gotHS)
|
||||||
|
|
||||||
|
assert.Nil(t, V2Form(v2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCertificateV3_MarshalRoundTrip(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, _, host, _ := newTestV3(t, curve)
|
||||||
|
|
||||||
|
for _, c := range []Certificate{ca, host} {
|
||||||
|
b, err := c.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
c2, err := unmarshalCertificateV3(b, nil, Curve_CURVE25519)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assertCertsEqual(t, c, c2)
|
||||||
|
|
||||||
|
p, err := c.MarshalPEM()
|
||||||
|
require.NoError(t, err)
|
||||||
|
c3, rest, err := UnmarshalCertificateFromPEM(p)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, rest)
|
||||||
|
assertCertsEqual(t, c, c3)
|
||||||
|
|
||||||
|
// Handshakes carry the composite public key in the noise static, not in the certificate.
|
||||||
|
hs, err := c.MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Less(t, len(hs), len(b)-len(c.PublicKey()))
|
||||||
|
c4, err := Recombine(Version3, hs, c.PublicKey(), curve)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assertCertsEqual(t, c, c4)
|
||||||
|
|
||||||
|
_, err = Recombine(Version3, b, c.PublicKey(), curve)
|
||||||
|
require.ErrorIs(t, err, ErrCertPubkeyPresent)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertCertsEqual(t *testing.T, want, got Certificate) {
|
||||||
|
t.Helper()
|
||||||
|
wb, err := want.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
gb, err := got.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, wb, gb)
|
||||||
|
wf, err := want.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
gf, err := got.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, wf, gf)
|
||||||
|
assert.Equal(t, want.PublicKey(), got.PublicKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
// rebuildV3 re-encodes a v3 certificate's envelope after edit has had a go at its fields.
|
||||||
|
func rebuildV3(t *testing.T, c Certificate, edit func(fields map[asn1.Tag][]byte, order *[]asn1.Tag)) []byte {
|
||||||
|
t.Helper()
|
||||||
|
b, err := c.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
in := cryptobyte.String(b)
|
||||||
|
var body cryptobyte.String
|
||||||
|
require.True(t, in.ReadASN1(&body, asn1.SEQUENCE))
|
||||||
|
|
||||||
|
fields := map[asn1.Tag][]byte{}
|
||||||
|
var order []asn1.Tag
|
||||||
|
for !body.Empty() {
|
||||||
|
var el cryptobyte.String
|
||||||
|
var tag asn1.Tag
|
||||||
|
require.True(t, body.ReadAnyASN1Element(&el, &tag))
|
||||||
|
fields[tag] = el
|
||||||
|
order = append(order, tag)
|
||||||
|
}
|
||||||
|
edit(fields, &order)
|
||||||
|
|
||||||
|
var out cryptobyte.Builder
|
||||||
|
out.AddASN1(asn1.SEQUENCE, func(b *cryptobyte.Builder) {
|
||||||
|
for _, tag := range order {
|
||||||
|
b.AddBytes(fields[tag])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return out.BytesOrPanic()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCertificateV3_StrictParse(t *testing.T) {
|
||||||
|
_, _, host, _ := newTestV3(t, Curve_CURVE25519)
|
||||||
|
good, err := host.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = unmarshalCertificateV3(append(good, 0), nil, Curve_CURVE25519)
|
||||||
|
require.ErrorIs(t, err, ErrBadFormat, "trailing bytes after the envelope")
|
||||||
|
|
||||||
|
trailing := rebuildV3(t, host, func(fields map[asn1.Tag][]byte, order *[]asn1.Tag) {
|
||||||
|
fields[7|classContextSpecific] = []byte{7 | classContextSpecific, 1, 0}
|
||||||
|
*order = append(*order, 7|classContextSpecific)
|
||||||
|
})
|
||||||
|
_, err = unmarshalCertificateV3(trailing, nil, Curve_CURVE25519)
|
||||||
|
require.ErrorIs(t, err, ErrBadFormat, "unknown field after the pq signature")
|
||||||
|
|
||||||
|
for _, tag := range []asn1.Tag{TagCertPQSuite, TagCertPQPublicKey, TagCertPQSignature} {
|
||||||
|
missing := rebuildV3(t, host, func(fields map[asn1.Tag][]byte, order *[]asn1.Tag) {
|
||||||
|
*order = removeTag(*order, tag)
|
||||||
|
})
|
||||||
|
_, err = unmarshalCertificateV3(missing, nil, Curve_CURVE25519)
|
||||||
|
require.Error(t, err, "missing %x", tag)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A v2 certificate is not a v3 one.
|
||||||
|
v2, err := V2Form(host).Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = unmarshalCertificateV3(v2, nil, Curve_CURVE25519)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
// A wrong sized post-quantum key fails validation.
|
||||||
|
short := rebuildV3(t, host, func(fields map[asn1.Tag][]byte, order *[]asn1.Tag) {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddASN1(TagCertPQPublicKey, func(b *cryptobyte.Builder) { b.AddBytes(make([]byte, MLKEM1024PublicKeySize-1)) })
|
||||||
|
fields[TagCertPQPublicKey] = b.BytesOrPanic()
|
||||||
|
})
|
||||||
|
_, err = unmarshalCertificateV3(short, nil, Curve_CURVE25519)
|
||||||
|
require.ErrorIs(t, err, ErrInvalidPublicKey)
|
||||||
|
|
||||||
|
// The suite is one known value, PQSuiteUnknown included in what is not.
|
||||||
|
for _, suite := range [][]byte{{byte(PQSuiteUnknown)}, {200}, {}, {1, 1}} {
|
||||||
|
bad := rebuildV3(t, host, func(fields map[asn1.Tag][]byte, order *[]asn1.Tag) {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddASN1(TagCertPQSuite, func(b *cryptobyte.Builder) { b.AddBytes(suite) })
|
||||||
|
fields[TagCertPQSuite] = b.BytesOrPanic()
|
||||||
|
})
|
||||||
|
_, err = unmarshalCertificateV3(bad, nil, Curve_CURVE25519)
|
||||||
|
require.Error(t, err, "suite %x", suite)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeTag(order []asn1.Tag, tag asn1.Tag) []asn1.Tag {
|
||||||
|
var out []asn1.Tag
|
||||||
|
for _, t := range order {
|
||||||
|
if t != tag {
|
||||||
|
out = append(out, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCertificateV3_Tamper(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, _, host, _ := newTestV3(t, curve)
|
||||||
|
|
||||||
|
// Swapping the post-quantum key breaks only the post-quantum signature, which must still catch it.
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
require.NoError(t, err)
|
||||||
|
swapped := host.Copy().(*certificateV3)
|
||||||
|
swapped.pqPublicKey = dk.EncapsulationKey().Bytes()
|
||||||
|
assert.True(t, V2Form(swapped).CheckSignature(V2Form(ca).PublicKey()))
|
||||||
|
assert.False(t, swapped.CheckSignature(ca.PublicKey()))
|
||||||
|
|
||||||
|
// The post-quantum signature covers the classical one.
|
||||||
|
resigned := host.Copy().(*certificateV3)
|
||||||
|
if curve == Curve_P256 {
|
||||||
|
sig, err := p256.Swap(resigned.v2.signature)
|
||||||
|
require.NoError(t, err)
|
||||||
|
resigned.v2.signature = sig
|
||||||
|
assert.True(t, V2Form(resigned).CheckSignature(V2Form(ca).PublicKey()))
|
||||||
|
} else {
|
||||||
|
resigned.v2.signature[0] ^= 1
|
||||||
|
}
|
||||||
|
assert.False(t, resigned.CheckSignature(ca.PublicKey()))
|
||||||
|
|
||||||
|
// Another CA's post-quantum key does not verify.
|
||||||
|
other, _, _, _ := NewTestCaCert(Version3, curve, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
k := append(V2Form(ca).PublicKey(), other.PublicKey()[len(V2Form(other).PublicKey()):]...)
|
||||||
|
assert.False(t, host.CheckSignature(k))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCAPool_V3(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, _, host, _ := newTestV3(t, curve)
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
// A v3 CA verifies v3 certificates, and only those.
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
cc, err := pool.VerifyCertificate(now, host)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, pool.VerifyCachedCertificate(now, cc))
|
||||||
|
_, err = pool.VerifyCertificate(now, V2Form(host))
|
||||||
|
require.ErrorIs(t, err, ErrCaNotFound)
|
||||||
|
|
||||||
|
// Listing the v2 form of the CA as well accepts the v2 form of the certificate.
|
||||||
|
require.NoError(t, pool.AddCA(V2Form(ca)))
|
||||||
|
_, err = pool.VerifyCertificate(now, V2Form(host))
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = pool.VerifyCertificate(now, host)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// The v2 form of the CA alone does not verify a v3 certificate.
|
||||||
|
v2Pool := NewCAPool()
|
||||||
|
require.NoError(t, v2Pool.AddCA(V2Form(ca)))
|
||||||
|
_, err = v2Pool.VerifyCertificate(now, host)
|
||||||
|
require.ErrorIs(t, err, ErrCaNotFound)
|
||||||
|
|
||||||
|
// A second v3 extension of the same v2 CA is ambiguous.
|
||||||
|
sk, err := mldsa.GenerateKey(mldsa.MLDSA87())
|
||||||
|
require.NoError(t, err)
|
||||||
|
signPQ, err := SignPQ(DefaultPQSuite, sk.Bytes())
|
||||||
|
require.NoError(t, err)
|
||||||
|
again, err := ExtendToV3(V2Form(ca), DefaultPQSuite, sk.PublicKey().Bytes(), nil, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.ErrorContains(t, pool.AddCA(again), "two version 3 CAs extend the same version 2 CA")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCAPool_V3Blocklist(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, _, host, _ := newTestV3(t, curve)
|
||||||
|
now := time.Now()
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
require.NoError(t, pool.AddCA(V2Form(ca)))
|
||||||
|
cc, err := pool.VerifyCertificate(now, host)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
fp, err := host.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
v2Fp, err := V2Form(host).Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotEqual(t, fp, v2Fp)
|
||||||
|
|
||||||
|
// Blocking the v3 fingerprint leaves the v2 form alone, which is why operators list both.
|
||||||
|
pool.BlocklistFingerprint(fp)
|
||||||
|
_, err = pool.VerifyCertificate(now, host)
|
||||||
|
require.ErrorIs(t, err, ErrBlockListed)
|
||||||
|
require.ErrorIs(t, pool.VerifyCachedCertificate(now, cc), ErrBlockListed)
|
||||||
|
_, err = pool.VerifyCertificate(now, V2Form(host))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Blocking the v2 fingerprint blocks both.
|
||||||
|
pool.ResetCertBlocklist()
|
||||||
|
pool.BlocklistFingerprint(v2Fp)
|
||||||
|
_, err = pool.VerifyCertificate(now, host)
|
||||||
|
require.ErrorIs(t, err, ErrBlockListed)
|
||||||
|
require.ErrorIs(t, pool.VerifyCachedCertificate(now, cc), ErrBlockListed)
|
||||||
|
_, err = pool.VerifyCertificate(now, V2Form(host))
|
||||||
|
require.ErrorIs(t, err, ErrBlockListed)
|
||||||
|
|
||||||
|
if curve == Curve_P256 {
|
||||||
|
// So does the other form of a P256 v2 signature.
|
||||||
|
alt, err := CalculateAlternateFingerprint(V2Form(host))
|
||||||
|
require.NoError(t, err)
|
||||||
|
pool.ResetCertBlocklist()
|
||||||
|
pool.BlocklistFingerprint(alt)
|
||||||
|
_, err = pool.VerifyCertificate(now, host)
|
||||||
|
require.ErrorIs(t, err, ErrBlockListed)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Certificates already in use extend in place: their v2 forms, and fingerprints, do not change.
|
||||||
|
func TestExtendToV3(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca2, _, caKey2, _ := NewTestCaCert(Version2, curve, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
host2, _, _, _ := NewTestCert(Version2, curve, ca2, caKey2, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.1/16")}, nil, nil)
|
||||||
|
|
||||||
|
sk, err := mldsa.GenerateKey(mldsa.MLDSA87())
|
||||||
|
require.NoError(t, err)
|
||||||
|
signPQ, err := SignPQ(DefaultPQSuite, sk.Bytes())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ca3, err := ExtendToV3(ca2, DefaultPQSuite, sk.PublicKey().Bytes(), nil, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assertCertsEqual(t, ca2, V2Form(ca3))
|
||||||
|
require.NoError(t, ca3.VerifyPrivateKey(curve, append(caKey2, sk.Bytes()...)))
|
||||||
|
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
require.NoError(t, err)
|
||||||
|
host3, err := ExtendToV3(host2, DefaultPQSuite, dk.EncapsulationKey().Bytes(), ca3, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assertCertsEqual(t, host2, V2Form(host3))
|
||||||
|
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca3))
|
||||||
|
_, err = pool.VerifyCertificate(time.Now(), host3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// A host must be extended by its own issuer, and with that issuer's key.
|
||||||
|
other2, _, _, _ := NewTestCaCert(Version2, curve, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
other3, err := ExtendToV3(other2, DefaultPQSuite, sk.PublicKey().Bytes(), nil, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = ExtendToV3(host2, DefaultPQSuite, dk.EncapsulationKey().Bytes(), other3, signPQ)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
wrong, err := mldsa.GenerateKey(mldsa.MLDSA87())
|
||||||
|
require.NoError(t, err)
|
||||||
|
wrongSign, err := SignPQ(DefaultPQSuite, wrong.Bytes())
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = ExtendToV3(host2, DefaultPQSuite, dk.EncapsulationKey().Bytes(), ca3, wrongSign)
|
||||||
|
require.ErrorIs(t, err, ErrSignatureMismatch)
|
||||||
|
|
||||||
|
// A CA extends itself, and needs its own post-quantum key to.
|
||||||
|
_, err = ExtendToV3(ca2, DefaultPQSuite, sk.PublicKey().Bytes(), nil, wrongSign)
|
||||||
|
require.ErrorIs(t, err, ErrSignatureMismatch)
|
||||||
|
_, err = ExtendToV3(ca2, DefaultPQSuite, sk.PublicKey().Bytes(), ca3, signPQ)
|
||||||
|
require.Error(t, err)
|
||||||
|
|
||||||
|
// Only v2 certificates extend.
|
||||||
|
_, err = ExtendToV3(host3, DefaultPQSuite, dk.EncapsulationKey().Bytes(), ca3, signPQ)
|
||||||
|
require.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ML-DSA-87 signing is deterministic, so extending a certificate twice gives the same bytes and the same fingerprint:
|
||||||
|
// pq-extend is idempotent, and a CA extended again is not a second v3 CA to the pool.
|
||||||
|
func TestExtendToV3_Deterministic(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca2, _, caKey2, _ := NewTestCaCert(Version2, curve, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
host2, _, _, _ := NewTestCert(Version2, curve, ca2, caKey2, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.1/16")}, nil, nil)
|
||||||
|
sk, err := mldsa.GenerateKey(mldsa.MLDSA87())
|
||||||
|
require.NoError(t, err)
|
||||||
|
signPQ, err := SignPQ(DefaultPQSuite, sk.Bytes())
|
||||||
|
require.NoError(t, err)
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
extend := func(c Certificate, pq []byte, signer Certificate) ([]byte, string, Certificate) {
|
||||||
|
c3, err := ExtendToV3(c, DefaultPQSuite, pq, signer, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
b, err := c3.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
fp, err := c3.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
return b, fp, c3
|
||||||
|
}
|
||||||
|
|
||||||
|
caBytesA, caFpA, caA := extend(ca2, sk.PublicKey().Bytes(), nil)
|
||||||
|
caBytesB, caFpB, caB := extend(ca2, sk.PublicKey().Bytes(), nil)
|
||||||
|
assert.Equal(t, caBytesA, caBytesB)
|
||||||
|
assert.Equal(t, caFpA, caFpB)
|
||||||
|
|
||||||
|
hostBytesA, hostFpA, _ := extend(host2, dk.EncapsulationKey().Bytes(), caA)
|
||||||
|
hostBytesB, hostFpB, _ := extend(host2, dk.EncapsulationKey().Bytes(), caB)
|
||||||
|
assert.Equal(t, hostBytesA, hostBytesB)
|
||||||
|
assert.Equal(t, hostFpA, hostFpB)
|
||||||
|
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(caA))
|
||||||
|
require.NoError(t, pool.AddCA(caB))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCertificateV3_KeyPEM(t *testing.T) {
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, caKey, host, hostKey := newTestV3(t, curve)
|
||||||
|
|
||||||
|
pub, rest, gotCurve, err := UnmarshalPublicKeyFromPEM(host.MarshalPublicKeyPEM())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Empty(t, rest)
|
||||||
|
assert.Equal(t, curve, gotCurve)
|
||||||
|
assert.Equal(t, host.PublicKey(), pub)
|
||||||
|
|
||||||
|
pub, _, gotCurve, err = UnmarshalSigningPublicKeyFromPEM(ca.MarshalPublicKeyPEM())
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, curve, gotCurve)
|
||||||
|
assert.Equal(t, ca.PublicKey(), pub)
|
||||||
|
|
||||||
|
key, _, gotCurve, err := UnmarshalPrivateKeyFromPEM(MarshalPQPrivateKeyToPEM(curve, DefaultPQSuite, hostKey))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, curve, gotCurve)
|
||||||
|
assert.Equal(t, hostKey, key)
|
||||||
|
|
||||||
|
key, _, gotCurve, err = UnmarshalSigningPrivateKeyFromPEM(MarshalPQSigningPrivateKeyToPEM(curve, DefaultPQSuite, caKey))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, curve, gotCurve)
|
||||||
|
assert.Equal(t, caKey, key)
|
||||||
|
|
||||||
|
kdf := NewArgon2Parameters(64*1024, 4, 1)
|
||||||
|
enc, err := EncryptAndMarshalPQSigningPrivateKey(curve, DefaultPQSuite, caKey, []byte("pw"), kdf)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, _, _, err = UnmarshalSigningPrivateKeyFromPEM(enc)
|
||||||
|
require.ErrorIs(t, err, ErrPrivateKeyEncrypted)
|
||||||
|
gotCurve, key, _, err = DecryptAndUnmarshalSigningPrivateKey([]byte("pw"), enc)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, curve, gotCurve)
|
||||||
|
assert.Equal(t, caKey, key)
|
||||||
|
|
||||||
|
// The banners name both halves.
|
||||||
|
assert.Contains(t, string(MarshalPQPrivateKeyToPEM(curve, DefaultPQSuite, hostKey)), " MLKEM1024 PRIVATE KEY-----")
|
||||||
|
assert.Contains(t, string(MarshalPQSigningPrivateKeyToPEM(curve, DefaultPQSuite, caKey)), " MLDSA87 PRIVATE KEY-----")
|
||||||
|
assert.Contains(t, string(enc), " MLDSA87 ENCRYPTED PRIVATE KEY-----")
|
||||||
|
|
||||||
|
// A key of the wrong size, or for an unknown suite, has no banner.
|
||||||
|
assert.Nil(t, MarshalPQPrivateKeyToPEM(curve, DefaultPQSuite, hostKey[1:]))
|
||||||
|
assert.Nil(t, MarshalPQPrivateKeyToPEM(curve, PQSuiteUnknown, hostKey))
|
||||||
|
assert.Nil(t, MarshalPQSigningPrivateKeyToPEM(curve, DefaultPQSuite, caKey[1:]))
|
||||||
|
_, err = EncryptAndMarshalPQSigningPrivateKey(curve, DefaultPQSuite, caKey[1:], []byte("pw"), kdf)
|
||||||
|
require.Error(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
+43
-11
@@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/crypto/argon2"
|
"golang.org/x/crypto/argon2"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
@@ -158,6 +159,33 @@ func splitNonceCiphertext(blob []byte, nonceSize int) ([]byte, []byte, error) {
|
|||||||
|
|
||||||
// EncryptAndMarshalSigningPrivateKey is a simple helper to encrypt and PEM encode a private key
|
// EncryptAndMarshalSigningPrivateKey is a simple helper to encrypt and PEM encode a private key
|
||||||
func EncryptAndMarshalSigningPrivateKey(curve Curve, b []byte, passphrase []byte, kdfParams *Argon2Parameters) ([]byte, error) {
|
func EncryptAndMarshalSigningPrivateKey(curve Curve, b []byte, passphrase []byte, kdfParams *Argon2Parameters) ([]byte, error) {
|
||||||
|
switch curve {
|
||||||
|
case Curve_CURVE25519:
|
||||||
|
return encryptAndMarshalSigningPrivateKey(EncryptedEd25519PrivateKeyBanner, b, passphrase, kdfParams)
|
||||||
|
case Curve_P256:
|
||||||
|
return encryptAndMarshalSigningPrivateKey(EncryptedECDSAP256PrivateKeyBanner, b, passphrase, kdfParams)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid curve: %v", curve)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncryptAndMarshalPQSigningPrivateKey encrypts and PEM encodes a composite CA private key of suite, the classical
|
||||||
|
// signing key followed by the seed of the suite's signature algorithm.
|
||||||
|
func EncryptAndMarshalPQSigningPrivateKey(curve Curve, suite PQSuite, b []byte, passphrase []byte, kdfParams *Argon2Parameters) ([]byte, error) {
|
||||||
|
p := pqSuites[suite]
|
||||||
|
if p == nil {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, suite)
|
||||||
|
}
|
||||||
|
if curve != Curve_CURVE25519 && curve != Curve_P256 {
|
||||||
|
return nil, fmt.Errorf("invalid curve: %v", curve)
|
||||||
|
}
|
||||||
|
if len(b) != pqKeySize(pqSigningPrivateKey, curve, p) {
|
||||||
|
return nil, ErrInvalidPrivateKey
|
||||||
|
}
|
||||||
|
return encryptAndMarshalSigningPrivateKey(pqBannerName(pqEncryptedSigningPrivateKey, curve, p), b, passphrase, kdfParams)
|
||||||
|
}
|
||||||
|
|
||||||
|
func encryptAndMarshalSigningPrivateKey(banner string, b []byte, passphrase []byte, kdfParams *Argon2Parameters) ([]byte, error) {
|
||||||
ciphertext, err := aes256Encrypt(passphrase, kdfParams, b)
|
ciphertext, err := aes256Encrypt(passphrase, kdfParams, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -180,14 +208,7 @@ func EncryptAndMarshalSigningPrivateKey(curve Curve, b []byte, passphrase []byte
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch curve {
|
return pem.EncodeToMemory(&pem.Block{Type: banner, Bytes: b}), nil
|
||||||
case Curve_CURVE25519:
|
|
||||||
return pem.EncodeToMemory(&pem.Block{Type: EncryptedEd25519PrivateKeyBanner, Bytes: b}), nil
|
|
||||||
case Curve_P256:
|
|
||||||
return pem.EncodeToMemory(&pem.Block{Type: EncryptedECDSAP256PrivateKeyBanner, Bytes: b}), nil
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("invalid curve: %v", curve)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalNebulaEncryptedData will unmarshal a protobuf byte representation of a nebula cert into its
|
// UnmarshalNebulaEncryptedData will unmarshal a protobuf byte representation of a nebula cert into its
|
||||||
@@ -260,10 +281,14 @@ func DecryptAndUnmarshalSigningPrivateKey(passphrase, b []byte) (Curve, []byte,
|
|||||||
return curve, nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
|
return curve, nil, r, fmt.Errorf("input did not contain a valid PEM encoded block")
|
||||||
}
|
}
|
||||||
|
|
||||||
switch k.Type {
|
pq, pqKey := pqKeyBanners[k.Type]
|
||||||
case EncryptedEd25519PrivateKeyBanner:
|
pqKey = pqKey && pq.kind == pqEncryptedSigningPrivateKey
|
||||||
|
switch {
|
||||||
|
case pqKey:
|
||||||
|
curve = pq.curve
|
||||||
|
case k.Type == EncryptedEd25519PrivateKeyBanner:
|
||||||
curve = Curve_CURVE25519
|
curve = Curve_CURVE25519
|
||||||
case EncryptedECDSAP256PrivateKeyBanner:
|
case k.Type == EncryptedECDSAP256PrivateKeyBanner:
|
||||||
curve = Curve_P256
|
curve = Curve_P256
|
||||||
default:
|
default:
|
||||||
return curve, nil, r, fmt.Errorf("bytes did not contain a proper nebula encrypted Ed25519/ECDSA private key banner")
|
return curve, nil, r, fmt.Errorf("bytes did not contain a proper nebula encrypted Ed25519/ECDSA private key banner")
|
||||||
@@ -285,6 +310,13 @@ func DecryptAndUnmarshalSigningPrivateKey(passphrase, b []byte) (Curve, []byte,
|
|||||||
return curve, nil, r, fmt.Errorf("unsupported encryption algorithm: %s", ned.EncryptionMetadata.EncryptionAlgorithm)
|
return curve, nil, r, fmt.Errorf("unsupported encryption algorithm: %s", ned.EncryptionMetadata.EncryptionAlgorithm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if pqKey {
|
||||||
|
if len(bytes) != pq.size {
|
||||||
|
return curve, nil, r, fmt.Errorf("key was not %d bytes, is invalid %s", pq.size, strings.ToLower(strings.TrimPrefix(k.Type, "NEBULA ")))
|
||||||
|
}
|
||||||
|
return curve, bytes, r, nil
|
||||||
|
}
|
||||||
|
|
||||||
switch curve {
|
switch curve {
|
||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
if len(bytes) != ed25519.PrivateKeySize {
|
if len(bytes) != ed25519.PrivateKeySize {
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ var (
|
|||||||
ErrUnknownVersion = errors.New("certificate version unrecognized")
|
ErrUnknownVersion = errors.New("certificate version unrecognized")
|
||||||
ErrCertPubkeyPresent = errors.New("certificate has unexpected pubkey present")
|
ErrCertPubkeyPresent = errors.New("certificate has unexpected pubkey present")
|
||||||
ErrCurveMismatch = errors.New("certificate curve does not match CA")
|
ErrCurveMismatch = errors.New("certificate curve does not match CA")
|
||||||
|
ErrPQSuiteMismatch = errors.New("certificate post-quantum suite does not match CA")
|
||||||
|
ErrUnknownPQSuite = errors.New("unknown post-quantum suite")
|
||||||
|
|
||||||
ErrInvalidPEMBlock = errors.New("input did not contain a valid PEM encoded block")
|
ErrInvalidPEMBlock = errors.New("input did not contain a valid PEM encoded block")
|
||||||
ErrInvalidPEMCertificateBanner = errors.New("bytes did not contain a proper certificate banner")
|
ErrInvalidPEMCertificateBanner = errors.New("bytes did not contain a proper certificate banner")
|
||||||
|
|||||||
@@ -39,6 +39,10 @@ func NewTestCaCert(version Version, curve Curve, before, after time.Time, networ
|
|||||||
// There is no default to allow the underlying lib to respond with an error
|
// There is no default to allow the underlying lib to respond with an error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version == Version3 {
|
||||||
|
pub, priv = appendPQSigningKey(pub, priv)
|
||||||
|
}
|
||||||
|
|
||||||
if before.IsZero() {
|
if before.IsZero() {
|
||||||
before = testCertNow.Add(time.Second * -60)
|
before = testCertNow.Add(time.Second * -60)
|
||||||
}
|
}
|
||||||
@@ -58,6 +62,9 @@ func NewTestCaCert(version Version, curve Curve, before, after time.Time, networ
|
|||||||
Groups: groups,
|
Groups: groups,
|
||||||
IsCA: true,
|
IsCA: true,
|
||||||
}
|
}
|
||||||
|
if version == Version3 {
|
||||||
|
t.PQSuite = DefaultPQSuite
|
||||||
|
}
|
||||||
|
|
||||||
c, err := t.Sign(nil, curve, priv)
|
c, err := t.Sign(nil, curve, priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -97,6 +104,10 @@ func NewTestCert(v Version, curve Curve, ca Certificate, key []byte, name string
|
|||||||
panic("unknown curve")
|
panic("unknown curve")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v == Version3 {
|
||||||
|
pub, priv = appendPQKey(ca, pub, priv)
|
||||||
|
}
|
||||||
|
|
||||||
nc := &TBSCertificate{
|
nc := &TBSCertificate{
|
||||||
Version: v,
|
Version: v,
|
||||||
Curve: curve,
|
Curve: curve,
|
||||||
@@ -108,6 +119,7 @@ func NewTestCert(v Version, curve Curve, ca Certificate, key []byte, name string
|
|||||||
NotAfter: time.Unix(after.Unix(), 0),
|
NotAfter: time.Unix(after.Unix(), 0),
|
||||||
PublicKey: pub,
|
PublicKey: pub,
|
||||||
IsCA: false,
|
IsCA: false,
|
||||||
|
PQSuite: PQSuiteOf(ca),
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := nc.Sign(ca, ca.Curve(), key)
|
c, err := nc.Sign(ca, ca.Curve(), key)
|
||||||
@@ -120,9 +132,30 @@ func NewTestCert(v Version, curve Curve, ca Certificate, key []byte, name string
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v == Version3 {
|
||||||
|
return c, pub, MarshalPQPrivateKeyToPEM(curve, PQSuiteOf(ca), priv), pem
|
||||||
|
}
|
||||||
return c, pub, MarshalPrivateKeyToPEM(curve, priv), pem
|
return c, pub, MarshalPrivateKeyToPEM(curve, priv), pem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appendPQSigningKey makes a classical CA keypair into a version 3 composite one with a fresh key of the default suite.
|
||||||
|
func appendPQSigningKey(pub, priv []byte) ([]byte, []byte) {
|
||||||
|
pqPub, pqPriv, err := DefaultPQSuite.GenerateKey(true)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendPQKey makes a classical host keypair into a version 3 composite one with a fresh key of the CA's suite.
|
||||||
|
func appendPQKey(ca Certificate, pub, priv []byte) ([]byte, []byte) {
|
||||||
|
pqPub, pqPriv, err := PQSuiteOf(ca).GenerateKey(false)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...)
|
||||||
|
}
|
||||||
|
|
||||||
func X25519Keypair() ([]byte, []byte) {
|
func X25519Keypair() ([]byte, []byte) {
|
||||||
privkey := make([]byte, 32)
|
privkey := make([]byte, 32)
|
||||||
if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
|
if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
|
||||||
|
|||||||
+165
@@ -5,6 +5,8 @@ import (
|
|||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
)
|
)
|
||||||
@@ -64,6 +66,7 @@ func SplitPEM(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
|||||||
const ( //cert banners
|
const ( //cert banners
|
||||||
CertificateBanner = "NEBULA CERTIFICATE"
|
CertificateBanner = "NEBULA CERTIFICATE"
|
||||||
CertificateV2Banner = "NEBULA CERTIFICATE V2"
|
CertificateV2Banner = "NEBULA CERTIFICATE V2"
|
||||||
|
CertificateV3Banner = "NEBULA CERTIFICATE V3"
|
||||||
)
|
)
|
||||||
|
|
||||||
const ( //key-agreement-key banners
|
const ( //key-agreement-key banners
|
||||||
@@ -73,6 +76,109 @@ const ( //key-agreement-key banners
|
|||||||
P256PublicKeyBanner = "NEBULA P256 PUBLIC KEY"
|
P256PublicKeyBanner = "NEBULA P256 PUBLIC KEY"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Version 3 keys are composite: the classical key followed by the post-quantum key of the certificate's PQSuite, a
|
||||||
|
// KEM seed or public key for hosts and a signature algorithm's seed or public key for CAs. Their banners name both
|
||||||
|
// halves, "NEBULA X25519 MLKEM1024 PRIVATE KEY" or "NEBULA ED25519 MLDSA87 PUBLIC KEY" for example, because the
|
||||||
|
// post-quantum algorithm can not be told from the key's size: ML-KEM and ML-DSA seeds are the same size at every
|
||||||
|
// strength. The banners name algorithms, not suites, so suites sharing an algorithm share its banners too.
|
||||||
|
|
||||||
|
// Classical key sizes, the composite keys of version 3 add the post-quantum key to these.
|
||||||
|
const (
|
||||||
|
x25519KeySize = 32
|
||||||
|
p256PublicKeySize = 65 // uncompressed
|
||||||
|
p256PrivateKeySize = 32
|
||||||
|
ed25519PublicKeySize = 32
|
||||||
|
ed25519PrivateKeySize = 64
|
||||||
|
ecdsaP256PublicKeySize = 65 // uncompressed
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClassicalPrivateKey returns the classical half of a host private key: all of a classical key, or the classical half
|
||||||
|
// of a composite one, whatever its suite.
|
||||||
|
func ClassicalPrivateKey(key []byte) []byte {
|
||||||
|
// X25519 and P256 private keys are the same size.
|
||||||
|
if len(key) > x25519KeySize {
|
||||||
|
return key[:x25519KeySize]
|
||||||
|
}
|
||||||
|
return key
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqKeyKind is what a composite key banner holds.
|
||||||
|
type pqKeyKind int
|
||||||
|
|
||||||
|
const (
|
||||||
|
pqPrivateKey pqKeyKind = iota
|
||||||
|
pqPublicKey
|
||||||
|
pqSigningPrivateKey
|
||||||
|
pqSigningPublicKey
|
||||||
|
pqEncryptedSigningPrivateKey
|
||||||
|
)
|
||||||
|
|
||||||
|
// pqKeyBanner is what a composite key banner says about the key under it.
|
||||||
|
type pqKeyBanner struct {
|
||||||
|
kind pqKeyKind
|
||||||
|
curve Curve
|
||||||
|
size int // of the whole composite key; for an encrypted key, once decrypted
|
||||||
|
}
|
||||||
|
|
||||||
|
var pqKeyKinds = []pqKeyKind{pqPrivateKey, pqPublicKey, pqSigningPrivateKey, pqSigningPublicKey, pqEncryptedSigningPrivateKey}
|
||||||
|
|
||||||
|
// pqKeyBanners maps every composite key banner of every suite to what it holds, see registerPQSuite.
|
||||||
|
var pqKeyBanners = map[string]pqKeyBanner{}
|
||||||
|
|
||||||
|
// pqBannerName returns the banner for a composite key.
|
||||||
|
func pqBannerName(kind pqKeyKind, curve Curve, suite *pqSuite) string {
|
||||||
|
classical := map[Curve]string{Curve_CURVE25519: "X25519", Curve_P256: "P256"}[curve]
|
||||||
|
alg := suite.kem.name
|
||||||
|
if kind != pqPrivateKey && kind != pqPublicKey {
|
||||||
|
classical = map[Curve]string{Curve_CURVE25519: "ED25519", Curve_P256: "ECDSA P256"}[curve]
|
||||||
|
alg = suite.dsa.name
|
||||||
|
}
|
||||||
|
return "NEBULA " + classical + " " + alg + " " + map[pqKeyKind]string{
|
||||||
|
pqPrivateKey: "PRIVATE KEY",
|
||||||
|
pqPublicKey: "PUBLIC KEY",
|
||||||
|
pqSigningPrivateKey: "PRIVATE KEY",
|
||||||
|
pqSigningPublicKey: "PUBLIC KEY",
|
||||||
|
pqEncryptedSigningPrivateKey: "ENCRYPTED PRIVATE KEY",
|
||||||
|
}[kind]
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqKeySize returns the size of a composite key.
|
||||||
|
func pqKeySize(kind pqKeyKind, curve Curve, suite *pqSuite) int {
|
||||||
|
p256 := curve == Curve_P256
|
||||||
|
switch kind {
|
||||||
|
case pqPrivateKey:
|
||||||
|
return x25519KeySize + suite.kem.privateKeySize // a P256 private key is the same size
|
||||||
|
case pqPublicKey:
|
||||||
|
return map[bool]int{false: x25519KeySize, true: p256PublicKeySize}[p256] + suite.kem.publicKeySize
|
||||||
|
case pqSigningPublicKey:
|
||||||
|
return map[bool]int{false: ed25519PublicKeySize, true: ecdsaP256PublicKeySize}[p256] + suite.dsa.publicKeySize
|
||||||
|
default:
|
||||||
|
return map[bool]int{false: ed25519PrivateKeySize, true: p256PrivateKeySize}[p256] + suite.dsa.privateKeySize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshalPQKeyToPEM PEM encodes a composite key of suite, nil for an unknown curve or suite or a key of the wrong size.
|
||||||
|
func marshalPQKeyToPEM(kind pqKeyKind, curve Curve, suite PQSuite, b []byte) []byte {
|
||||||
|
p := pqSuites[suite]
|
||||||
|
if p == nil || (curve != Curve_CURVE25519 && curve != Curve_P256) || len(b) != pqKeySize(kind, curve, p) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return pem.EncodeToMemory(&pem.Block{Type: pqBannerName(kind, curve, p), Bytes: b})
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalPQKeyBlock checks a PEM block that may hold a composite key of one of kinds. ok is false when the banner
|
||||||
|
// is not a composite key banner of those kinds at all.
|
||||||
|
func unmarshalPQKeyBlock(k *pem.Block, kinds ...pqKeyKind) (curve Curve, ok bool, err error) {
|
||||||
|
b, found := pqKeyBanners[k.Type]
|
||||||
|
if !found || !slices.Contains(kinds, b.kind) {
|
||||||
|
return 0, false, nil
|
||||||
|
}
|
||||||
|
if b.kind != pqEncryptedSigningPrivateKey && len(k.Bytes) != b.size {
|
||||||
|
return b.curve, true, fmt.Errorf("key was not %d bytes, is invalid %s", b.size, strings.ToLower(strings.TrimPrefix(k.Type, "NEBULA ")))
|
||||||
|
}
|
||||||
|
return b.curve, true, nil
|
||||||
|
}
|
||||||
|
|
||||||
/* including "ECDSA" in the P256 banners is a clue that these keys should be used only for signing */
|
/* including "ECDSA" in the P256 banners is a clue that these keys should be used only for signing */
|
||||||
const ( //signing key banners
|
const ( //signing key banners
|
||||||
EncryptedECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 ENCRYPTED PRIVATE KEY"
|
EncryptedECDSAP256PrivateKeyBanner = "NEBULA ECDSA P256 ENCRYPTED PRIVATE KEY"
|
||||||
@@ -109,12 +215,20 @@ func unmarshalCertificateBlock(block *pem.Block) (Certificate, error) {
|
|||||||
return unmarshalCertificateV1(block.Bytes, nil)
|
return unmarshalCertificateV1(block.Bytes, nil)
|
||||||
case CertificateV2Banner:
|
case CertificateV2Banner:
|
||||||
return unmarshalCertificateV2(block.Bytes, nil, Curve_CURVE25519)
|
return unmarshalCertificateV2(block.Bytes, nil, Curve_CURVE25519)
|
||||||
|
case CertificateV3Banner:
|
||||||
|
return unmarshalCertificateV3(block.Bytes, nil, Curve_CURVE25519)
|
||||||
default:
|
default:
|
||||||
return nil, ErrInvalidPEMCertificateBanner
|
return nil, ErrInvalidPEMCertificateBanner
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalCertPublicKeyToPEM(c Certificate) []byte {
|
func marshalCertPublicKeyToPEM(c Certificate) []byte {
|
||||||
|
if suite := PQSuiteOf(c); suite != PQSuiteUnknown {
|
||||||
|
if c.IsCA() {
|
||||||
|
return MarshalPQSigningPublicKeyToPEM(c.Curve(), suite, c.PublicKey())
|
||||||
|
}
|
||||||
|
return MarshalPQPublicKeyToPEM(c.Curve(), suite, c.PublicKey())
|
||||||
|
}
|
||||||
if c.IsCA() {
|
if c.IsCA() {
|
||||||
return MarshalSigningPublicKeyToPEM(c.Curve(), c.PublicKey())
|
return MarshalSigningPublicKeyToPEM(c.Curve(), c.PublicKey())
|
||||||
} else {
|
} else {
|
||||||
@@ -135,6 +249,12 @@ func MarshalPublicKeyToPEM(curve Curve, b []byte) []byte {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalPQPublicKeyToPEM returns a PEM representation of a composite host public key of suite, the classical ECDH
|
||||||
|
// key followed by the suite's KEM public key, or nil if b is not one.
|
||||||
|
func MarshalPQPublicKeyToPEM(curve Curve, suite PQSuite, b []byte) []byte {
|
||||||
|
return marshalPQKeyToPEM(pqPublicKey, curve, suite, b)
|
||||||
|
}
|
||||||
|
|
||||||
// MarshalSigningPublicKeyToPEM returns a PEM representation of a public key used for signing.
|
// MarshalSigningPublicKeyToPEM returns a PEM representation of a public key used for signing.
|
||||||
// if your public key came from a certificate, prefer Certificate.PublicKeyPEM() if possible, to avoid mistakes!
|
// if your public key came from a certificate, prefer Certificate.PublicKeyPEM() if possible, to avoid mistakes!
|
||||||
func MarshalSigningPublicKeyToPEM(curve Curve, b []byte) []byte {
|
func MarshalSigningPublicKeyToPEM(curve Curve, b []byte) []byte {
|
||||||
@@ -148,6 +268,12 @@ func MarshalSigningPublicKeyToPEM(curve Curve, b []byte) []byte {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalPQSigningPublicKeyToPEM returns a PEM representation of a composite CA public key of suite, the classical
|
||||||
|
// signing key followed by the public key of the suite's signature algorithm, or nil if b is not one.
|
||||||
|
func MarshalPQSigningPublicKeyToPEM(curve Curve, suite PQSuite, b []byte) []byte {
|
||||||
|
return marshalPQKeyToPEM(pqSigningPublicKey, curve, suite, b)
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalPublicKeyFromPEM will try to unmarshal the first pem block in a byte array, returning any non
|
// UnmarshalPublicKeyFromPEM will try to unmarshal the first pem block in a byte array, returning any non
|
||||||
// consumed data or an error on failure. Only key-agreement (ECDH) public key banners are accepted.
|
// consumed data or an error on failure. Only key-agreement (ECDH) public key banners are accepted.
|
||||||
// Use UnmarshalSigningPublicKeyFromPEM for Ed25519/ECDSA banners.
|
// Use UnmarshalSigningPublicKeyFromPEM for Ed25519/ECDSA banners.
|
||||||
@@ -156,6 +282,12 @@ func UnmarshalPublicKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
|||||||
if k == nil {
|
if k == nil {
|
||||||
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
||||||
}
|
}
|
||||||
|
if curve, ok, err := unmarshalPQKeyBlock(k, pqPublicKey); ok {
|
||||||
|
if err != nil {
|
||||||
|
return nil, r, 0, err
|
||||||
|
}
|
||||||
|
return k.Bytes, r, curve, nil
|
||||||
|
}
|
||||||
var expectedLen int
|
var expectedLen int
|
||||||
var curve Curve
|
var curve Curve
|
||||||
switch k.Type {
|
switch k.Type {
|
||||||
@@ -183,6 +315,12 @@ func UnmarshalSigningPublicKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
|||||||
if k == nil {
|
if k == nil {
|
||||||
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
||||||
}
|
}
|
||||||
|
if curve, ok, err := unmarshalPQKeyBlock(k, pqSigningPublicKey); ok {
|
||||||
|
if err != nil {
|
||||||
|
return nil, r, 0, err
|
||||||
|
}
|
||||||
|
return k.Bytes, r, curve, nil
|
||||||
|
}
|
||||||
var expectedLen int
|
var expectedLen int
|
||||||
var curve Curve
|
var curve Curve
|
||||||
switch k.Type {
|
switch k.Type {
|
||||||
@@ -213,6 +351,18 @@ func MarshalPrivateKeyToPEM(curve Curve, b []byte) []byte {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MarshalPQPrivateKeyToPEM returns a PEM representation of a composite host private key of suite, the classical ECDH
|
||||||
|
// key followed by the seed of the suite's KEM, or nil if b is not one.
|
||||||
|
func MarshalPQPrivateKeyToPEM(curve Curve, suite PQSuite, b []byte) []byte {
|
||||||
|
return marshalPQKeyToPEM(pqPrivateKey, curve, suite, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarshalPQSigningPrivateKeyToPEM returns a PEM representation of a composite CA private key of suite, the classical
|
||||||
|
// signing key followed by the seed of the suite's signature algorithm, or nil if b is not one.
|
||||||
|
func MarshalPQSigningPrivateKeyToPEM(curve Curve, suite PQSuite, b []byte) []byte {
|
||||||
|
return marshalPQKeyToPEM(pqSigningPrivateKey, curve, suite, b)
|
||||||
|
}
|
||||||
|
|
||||||
func MarshalSigningPrivateKeyToPEM(curve Curve, b []byte) []byte {
|
func MarshalSigningPrivateKeyToPEM(curve Curve, b []byte) []byte {
|
||||||
switch curve {
|
switch curve {
|
||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
@@ -231,6 +381,12 @@ func UnmarshalPrivateKeyFromPEM(b []byte) ([]byte, []byte, Curve, error) {
|
|||||||
if k == nil {
|
if k == nil {
|
||||||
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
||||||
}
|
}
|
||||||
|
if curve, ok, err := unmarshalPQKeyBlock(k, pqPrivateKey); ok {
|
||||||
|
if err != nil {
|
||||||
|
return nil, r, 0, err
|
||||||
|
}
|
||||||
|
return k.Bytes, r, curve, nil
|
||||||
|
}
|
||||||
var expectedLen int
|
var expectedLen int
|
||||||
var curve Curve
|
var curve Curve
|
||||||
switch k.Type {
|
switch k.Type {
|
||||||
@@ -254,6 +410,15 @@ func UnmarshalSigningPrivateKeyFromPEM(b []byte) ([]byte, []byte, Curve, error)
|
|||||||
if k == nil {
|
if k == nil {
|
||||||
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
return nil, r, 0, fmt.Errorf("input did not contain a valid PEM encoded block")
|
||||||
}
|
}
|
||||||
|
if curve, ok, err := unmarshalPQKeyBlock(k, pqSigningPrivateKey, pqEncryptedSigningPrivateKey); ok {
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
return nil, r, 0, err
|
||||||
|
case pqKeyBanners[k.Type].kind == pqEncryptedSigningPrivateKey:
|
||||||
|
return nil, nil, curve, ErrPrivateKeyEncrypted
|
||||||
|
}
|
||||||
|
return k.Bytes, r, curve, nil
|
||||||
|
}
|
||||||
var curve Curve
|
var curve Curve
|
||||||
switch k.Type {
|
switch k.Type {
|
||||||
case EncryptedEd25519PrivateKeyBanner:
|
case EncryptedEd25519PrivateKeyBanner:
|
||||||
|
|||||||
@@ -0,0 +1,282 @@
|
|||||||
|
package cert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/mldsa"
|
||||||
|
"crypto/mlkem"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// PQSuite names the post-quantum algorithms of a version 3 certificate, the way Curve names its classical ones: the
|
||||||
|
// key encapsulation mechanism of a host's key, and the signature algorithm of a CA's key and of the certificates it
|
||||||
|
// signs. A CA and every certificate it signs share a suite. Every version 3 certificate names its suite.
|
||||||
|
type PQSuite uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PQSuiteUnknown is never valid in a certificate.
|
||||||
|
PQSuiteUnknown PQSuite = 0
|
||||||
|
|
||||||
|
// PQSuite_MLKEM1024_MLDSA87 gives hosts ML-KEM-1024 keys and CAs ML-DSA-87 keys and signatures.
|
||||||
|
PQSuite_MLKEM1024_MLDSA87 PQSuite = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultPQSuite is the suite tools use when none is asked for.
|
||||||
|
const DefaultPQSuite = PQSuite_MLKEM1024_MLDSA87
|
||||||
|
|
||||||
|
// pqKEM is the key encapsulation mechanism of a suite, what host keys use. Private keys are seeds.
|
||||||
|
type pqKEM struct {
|
||||||
|
name string // as it appears in key banners
|
||||||
|
publicKeySize int
|
||||||
|
privateKeySize int
|
||||||
|
generate func() (pub, priv []byte, err error)
|
||||||
|
publicKey func(priv []byte) ([]byte, error)
|
||||||
|
checkPublicKey func(pub []byte) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqDSA is the signature algorithm of a suite, what CA keys and certificate signatures use. Private keys are seeds.
|
||||||
|
type pqDSA struct {
|
||||||
|
name string // as it appears in key banners
|
||||||
|
publicKeySize int
|
||||||
|
privateKeySize int
|
||||||
|
signatureSize int
|
||||||
|
generate func() (pub, priv []byte, err error)
|
||||||
|
publicKey func(priv []byte) ([]byte, error)
|
||||||
|
checkPublicKey func(pub []byte) bool
|
||||||
|
signer func(priv []byte) (SignerLambda, error) // must sign deterministically
|
||||||
|
verify func(pub, msg, sig []byte) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type pqSuite struct {
|
||||||
|
kem pqKEM
|
||||||
|
dsa pqDSA
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqSuites holds every known suite, see registerPQSuite.
|
||||||
|
var pqSuites = map[PQSuite]*pqSuite{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
registerPQSuite(PQSuite_MLKEM1024_MLDSA87, &pqSuite{kem: mlkem1024, dsa: mldsa87})
|
||||||
|
}
|
||||||
|
|
||||||
|
// registerPQSuite makes a suite known, along with the banners of its composite keys.
|
||||||
|
func registerPQSuite(id PQSuite, s *pqSuite) {
|
||||||
|
pqSuites[id] = s
|
||||||
|
for _, curve := range []Curve{Curve_CURVE25519, Curve_P256} {
|
||||||
|
for _, kind := range pqKeyKinds {
|
||||||
|
pqKeyBanners[pqBannerName(kind, curve, s)] = pqKeyBanner{kind: kind, curve: curve, size: pqKeySize(kind, curve, s)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var mlkem1024 = pqKEM{
|
||||||
|
name: "MLKEM1024",
|
||||||
|
publicKeySize: mlkem.EncapsulationKeySize1024,
|
||||||
|
privateKeySize: mlkem.SeedSize,
|
||||||
|
generate: func() ([]byte, []byte, error) {
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return dk.EncapsulationKey().Bytes(), dk.Bytes(), nil
|
||||||
|
},
|
||||||
|
publicKey: func(priv []byte) ([]byte, error) {
|
||||||
|
dk, err := mlkem.NewDecapsulationKey1024(priv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dk.EncapsulationKey().Bytes(), nil
|
||||||
|
},
|
||||||
|
checkPublicKey: func(pub []byte) bool {
|
||||||
|
return len(pub) == mlkem.EncapsulationKeySize1024 && mlkemModulusOK(pub[:len(pub)-32])
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// mlkemModulusOK is the modulus check of FIPS 203 section 7.2, the one crypto/mlkem runs on an encapsulation key: t,
|
||||||
|
// the key less its trailing 32 byte seed, packs 12 bit coefficients that must all be below q. crypto/mlkem would also
|
||||||
|
// expand the seed into the ~10KB matrix of an encapsulation key, which a certificate check has no use for.
|
||||||
|
func mlkemModulusOK(t []byte) bool {
|
||||||
|
const q = 3329
|
||||||
|
for i := 0; i+3 <= len(t); i += 3 {
|
||||||
|
d1 := uint16(t[i]) | uint16(t[i+1]&0x0f)<<8
|
||||||
|
d2 := uint16(t[i+1]>>4) | uint16(t[i+2])<<4
|
||||||
|
if d1 >= q || d2 >= q {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var mldsa87 = pqDSA{
|
||||||
|
name: "MLDSA87",
|
||||||
|
publicKeySize: mldsa.MLDSA87PublicKeySize,
|
||||||
|
privateKeySize: mldsa.PrivateKeySize,
|
||||||
|
signatureSize: mldsa.MLDSA87SignatureSize,
|
||||||
|
generate: func() ([]byte, []byte, error) {
|
||||||
|
sk, err := mldsa.GenerateKey(mldsa.MLDSA87())
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return sk.PublicKey().Bytes(), sk.Bytes(), nil
|
||||||
|
},
|
||||||
|
publicKey: func(priv []byte) ([]byte, error) {
|
||||||
|
sk, err := mldsa.NewPrivateKey(mldsa.MLDSA87(), priv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return sk.PublicKey().Bytes(), nil
|
||||||
|
},
|
||||||
|
checkPublicKey: func(pub []byte) bool {
|
||||||
|
_, err := mldsa.NewPublicKey(mldsa.MLDSA87(), pub)
|
||||||
|
return err == nil
|
||||||
|
},
|
||||||
|
// Deterministic signing (FIPS 204 section 3.4): the hedged variant guards signers an attacker can fault
|
||||||
|
// physically, which a CA key in a file is not, and the Ed25519 signature beside it is deterministic too.
|
||||||
|
signer: func(priv []byte) (SignerLambda, error) {
|
||||||
|
sk, err := mldsa.NewPrivateKey(mldsa.MLDSA87(), priv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return func(b []byte) ([]byte, error) {
|
||||||
|
return sk.SignDeterministic(b, &mldsa.Options{Context: pqSignatureContext})
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
verify: func(pub, msg, sig []byte) bool {
|
||||||
|
pk, err := mldsa.NewPublicKey(mldsa.MLDSA87(), pub)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return mldsa.Verify(pk, msg, sig, &mldsa.Options{Context: pqSignatureContext}) == nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// PQSuites returns every known suite.
|
||||||
|
func PQSuites() []PQSuite {
|
||||||
|
s := make([]PQSuite, 0, len(pqSuites))
|
||||||
|
for k := range pqSuites {
|
||||||
|
s = append(s, k)
|
||||||
|
}
|
||||||
|
slices.Sort(s)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParsePQSuite returns the suite named by s, as String names it, ignoring case.
|
||||||
|
func ParsePQSuite(s string) (PQSuite, error) {
|
||||||
|
for _, k := range PQSuites() {
|
||||||
|
if strings.EqualFold(s, k.String()) {
|
||||||
|
return k, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return PQSuiteUnknown, fmt.Errorf("%w: %q", ErrUnknownPQSuite, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PQSuiteOf returns the suite of a version 3 certificate, or PQSuiteUnknown for any other version.
|
||||||
|
func PQSuiteOf(c Certificate) PQSuite {
|
||||||
|
if c3, ok := c.(*certificateV3); ok {
|
||||||
|
return c3.pqSuite
|
||||||
|
}
|
||||||
|
return PQSuiteUnknown
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s PQSuite) String() string {
|
||||||
|
if p := pqSuites[s]; p != nil {
|
||||||
|
return p.kem.name + "_" + p.dsa.name
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("PQSuite(%d)", uint8(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Valid reports whether s is a known suite.
|
||||||
|
func (s PQSuite) Valid() bool {
|
||||||
|
return pqSuites[s] != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicKeySize returns the size of the post-quantum half of a composite public key, a CA's or a host's.
|
||||||
|
func (s PQSuite) PublicKeySize(isCA bool) int {
|
||||||
|
p := pqSuites[s]
|
||||||
|
switch {
|
||||||
|
case p == nil:
|
||||||
|
return 0
|
||||||
|
case isCA:
|
||||||
|
return p.dsa.publicKeySize
|
||||||
|
default:
|
||||||
|
return p.kem.publicKeySize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateKeySize returns the size of the post-quantum half of a composite private key, a CA's or a host's.
|
||||||
|
func (s PQSuite) PrivateKeySize(isCA bool) int {
|
||||||
|
p := pqSuites[s]
|
||||||
|
switch {
|
||||||
|
case p == nil:
|
||||||
|
return 0
|
||||||
|
case isCA:
|
||||||
|
return p.dsa.privateKeySize
|
||||||
|
default:
|
||||||
|
return p.kem.privateKeySize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateKey makes a post-quantum keypair, a CA's signing key or a host's KEM key, to append to a classical one.
|
||||||
|
func (s PQSuite) GenerateKey(isCA bool) (pub, priv []byte, err error) {
|
||||||
|
p := pqSuites[s]
|
||||||
|
switch {
|
||||||
|
case p == nil:
|
||||||
|
return nil, nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, s)
|
||||||
|
case isCA:
|
||||||
|
return p.dsa.generate()
|
||||||
|
default:
|
||||||
|
return p.kem.generate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// publicKey derives the post-quantum public key from the post-quantum half of a private key.
|
||||||
|
func (s PQSuite) publicKey(isCA bool, priv []byte) ([]byte, error) {
|
||||||
|
p := pqSuites[s]
|
||||||
|
switch {
|
||||||
|
case p == nil:
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, s)
|
||||||
|
case isCA:
|
||||||
|
return p.dsa.publicKey(priv)
|
||||||
|
default:
|
||||||
|
return p.kem.publicKey(priv)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkPublicKey reports whether pub is a well formed post-quantum public key, a CA's or a host's.
|
||||||
|
func (s PQSuite) checkPublicKey(isCA bool, pub []byte) bool {
|
||||||
|
p := pqSuites[s]
|
||||||
|
switch {
|
||||||
|
case p == nil, len(pub) != s.PublicKeySize(isCA):
|
||||||
|
return false
|
||||||
|
case isCA:
|
||||||
|
return p.dsa.checkPublicKey(pub)
|
||||||
|
default:
|
||||||
|
return p.kem.checkPublicKey(pub)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// signatureSize returns the size of the post-quantum signature of a certificate in this suite.
|
||||||
|
func (s PQSuite) signatureSize() int {
|
||||||
|
if p := pqSuites[s]; p != nil {
|
||||||
|
return p.dsa.signatureSize
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// SplitPublicKey splits a composite public key into its classical and post-quantum halves.
|
||||||
|
func (s PQSuite) SplitPublicKey(isCA bool, key []byte) (classical, pq []byte, err error) {
|
||||||
|
n := s.PublicKeySize(isCA)
|
||||||
|
if n == 0 || len(key) <= n {
|
||||||
|
return nil, nil, ErrInvalidPublicKey
|
||||||
|
}
|
||||||
|
return key[:len(key)-n], key[len(key)-n:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SplitPrivateKey splits a composite private key into its classical and post-quantum halves.
|
||||||
|
func (s PQSuite) SplitPrivateKey(isCA bool, key []byte) (classical, pq []byte, err error) {
|
||||||
|
n := s.PrivateKeySize(isCA)
|
||||||
|
if n == 0 || len(key) <= n {
|
||||||
|
return nil, nil, ErrInvalidPrivateKey
|
||||||
|
}
|
||||||
|
return key[:len(key)-n], key[len(key)-n:], nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,211 @@
|
|||||||
|
package cert
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/mlkem"
|
||||||
|
"crypto/rand"
|
||||||
|
"net/netip"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// testPQSuite stands in for a second suite. It reuses ML-KEM-1024 and ML-DSA-87 under other names, so it is
|
||||||
|
// interchangeable with the default suite in everything but its identity, which is what has to keep them apart.
|
||||||
|
const testPQSuite PQSuite = 200
|
||||||
|
|
||||||
|
func registerTestPQSuite(t *testing.T) {
|
||||||
|
t.Helper()
|
||||||
|
kem, dsa := mlkem1024, mldsa87
|
||||||
|
kem.name, dsa.name = "TESTKEM", "TESTDSA"
|
||||||
|
registerPQSuite(testPQSuite, &pqSuite{kem: kem, dsa: dsa})
|
||||||
|
t.Cleanup(func() {
|
||||||
|
delete(pqSuites, testPQSuite)
|
||||||
|
for k := range pqKeyBanners {
|
||||||
|
if strings.Contains(k, "TESTKEM") || strings.Contains(k, "TESTDSA") {
|
||||||
|
delete(pqKeyBanners, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// newTestV3InSuite issues a v3 CA and host certificate in suite.
|
||||||
|
func newTestV3InSuite(t *testing.T, curve Curve, suite PQSuite) (ca Certificate, caKey []byte, host Certificate, hostKey []byte) {
|
||||||
|
t.Helper()
|
||||||
|
ca2, _, caKey2, _ := NewTestCaCert(Version2, curve, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
pqPub, pqPriv, err := suite.GenerateKey(true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
signPQ, err := SignPQ(suite, pqPriv)
|
||||||
|
require.NoError(t, err)
|
||||||
|
ca, err = ExtendToV3(ca2, suite, pqPub, nil, signPQ)
|
||||||
|
require.NoError(t, err)
|
||||||
|
caKey = append(caKey2, pqPriv...)
|
||||||
|
|
||||||
|
var pub, priv []byte
|
||||||
|
if curve == Curve_P256 {
|
||||||
|
pub, priv = P256Keypair()
|
||||||
|
} else {
|
||||||
|
pub, priv = X25519Keypair()
|
||||||
|
}
|
||||||
|
hostPQPub, hostPQPriv, err := suite.GenerateKey(false)
|
||||||
|
require.NoError(t, err)
|
||||||
|
tbs := &TBSCertificate{
|
||||||
|
Version: Version3,
|
||||||
|
Curve: curve,
|
||||||
|
PQSuite: suite,
|
||||||
|
Name: "host",
|
||||||
|
Networks: []netip.Prefix{netip.MustParsePrefix("10.1.0.1/16")},
|
||||||
|
NotBefore: ca.NotBefore(),
|
||||||
|
NotAfter: ca.NotAfter(),
|
||||||
|
PublicKey: append(pub, hostPQPub...),
|
||||||
|
}
|
||||||
|
host, err = tbs.Sign(ca, curve, caKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return ca, caKey, host, append(priv, hostPQPriv...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQSuite_Names(t *testing.T) {
|
||||||
|
assert.Equal(t, "MLKEM1024_MLDSA87", PQSuite_MLKEM1024_MLDSA87.String())
|
||||||
|
assert.Equal(t, []PQSuite{PQSuite_MLKEM1024_MLDSA87}, PQSuites())
|
||||||
|
assert.False(t, PQSuiteUnknown.Valid())
|
||||||
|
|
||||||
|
s, err := ParsePQSuite("mlkem1024_mldsa87")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, PQSuite_MLKEM1024_MLDSA87, s)
|
||||||
|
_, err = ParsePQSuite("mlkem768_mldsa65")
|
||||||
|
require.ErrorIs(t, err, ErrUnknownPQSuite)
|
||||||
|
|
||||||
|
_, _, host, _ := newTestV3(t, Curve_CURVE25519)
|
||||||
|
assert.Equal(t, DefaultPQSuite, PQSuiteOf(host))
|
||||||
|
assert.Equal(t, PQSuiteUnknown, PQSuiteOf(V2Form(host)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A second suite works end to end, and is never confused with the first, even with keys of the same sizes.
|
||||||
|
func TestPQSuite_Second(t *testing.T) {
|
||||||
|
registerTestPQSuite(t)
|
||||||
|
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, caKey, host, hostKey := newTestV3InSuite(t, curve, testPQSuite)
|
||||||
|
assert.Equal(t, testPQSuite, PQSuiteOf(ca))
|
||||||
|
assert.Equal(t, testPQSuite, PQSuiteOf(host))
|
||||||
|
require.NoError(t, ca.VerifyPrivateKey(curve, caKey))
|
||||||
|
require.NoError(t, host.VerifyPrivateKey(curve, hostKey))
|
||||||
|
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
_, err := pool.VerifyCertificate(time.Now(), host)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// The suite travels in the certificate, handshakes included.
|
||||||
|
hs, err := host.MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
c, err := Recombine(Version3, hs, host.PublicKey(), curve)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, testPQSuite, PQSuiteOf(c))
|
||||||
|
assertCertsEqual(t, host, c)
|
||||||
|
|
||||||
|
// Keys get their algorithms' banners.
|
||||||
|
b := MarshalPQPrivateKeyToPEM(curve, testPQSuite, hostKey)
|
||||||
|
assert.Contains(t, string(b), " TESTKEM PRIVATE KEY-----")
|
||||||
|
key, _, _, err := UnmarshalPrivateKeyFromPEM(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, hostKey, key)
|
||||||
|
assert.Contains(t, string(host.MarshalPublicKeyPEM()), " TESTKEM PUBLIC KEY-----")
|
||||||
|
assert.Contains(t, string(ca.MarshalPublicKeyPEM()), " TESTDSA PUBLIC KEY-----")
|
||||||
|
|
||||||
|
// The suite is signed: relabelling a certificate breaks it, though the keys fit either suite.
|
||||||
|
_, _, other, _ := newTestV3(t, curve)
|
||||||
|
relabelled := other.Copy().(*certificateV3)
|
||||||
|
relabelled.pqSuite = testPQSuite
|
||||||
|
assert.False(t, relabelled.CheckSignature(ca.PublicKey()))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A CA and the certificates it signs share a suite, like they share a curve.
|
||||||
|
func TestPQSuite_Mismatch(t *testing.T) {
|
||||||
|
registerTestPQSuite(t)
|
||||||
|
|
||||||
|
for _, curve := range testCurves {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
ca, caKey, host, _ := newTestV3(t, curve)
|
||||||
|
|
||||||
|
// Signing refuses a certificate in another suite than its CA.
|
||||||
|
tbs := &TBSCertificate{
|
||||||
|
Version: Version3, Curve: curve, PQSuite: testPQSuite, Name: "x",
|
||||||
|
Networks: host.Networks(), NotBefore: host.NotBefore(), NotAfter: host.NotAfter(), PublicKey: host.PublicKey(),
|
||||||
|
}
|
||||||
|
_, err := tbs.Sign(ca, curve, caKey)
|
||||||
|
require.ErrorIs(t, err, ErrPQSuiteMismatch)
|
||||||
|
|
||||||
|
// Extending refuses it too.
|
||||||
|
v2CA := V2Form(ca)
|
||||||
|
v2Host := V2Form(host)
|
||||||
|
_, pqHost, err := DefaultPQSuite.SplitPublicKey(false, host.PublicKey())
|
||||||
|
require.NoError(t, err)
|
||||||
|
signPQ, err := SignPQ(DefaultPQSuite, caKey[len(caKey)-DefaultPQSuite.PrivateKeySize(true):])
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = ExtendToV3(v2Host, testPQSuite, pqHost, ca, signPQ)
|
||||||
|
require.ErrorIs(t, err, ErrPQSuiteMismatch)
|
||||||
|
|
||||||
|
// The pool rejects a certificate whose CA, found by issuer, is in another suite: here the same v2 CA
|
||||||
|
// extended once per suite, with only the default suite's extension trusted.
|
||||||
|
pqPub, pqPriv, err := testPQSuite.GenerateKey(true)
|
||||||
|
require.NoError(t, err)
|
||||||
|
signOther, err := SignPQ(testPQSuite, pqPriv)
|
||||||
|
require.NoError(t, err)
|
||||||
|
otherCA, err := ExtendToV3(v2CA, testPQSuite, pqPub, nil, signOther)
|
||||||
|
require.NoError(t, err)
|
||||||
|
otherHost, err := ExtendToV3(v2Host, testPQSuite, pqHost, otherCA, signOther)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
pool := NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
_, err = pool.VerifyCertificate(time.Now(), otherHost)
|
||||||
|
require.ErrorIs(t, err, ErrPQSuiteMismatch)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMLKEMModulusOK(t *testing.T) {
|
||||||
|
stdlib := func(pub []byte) bool {
|
||||||
|
_, err := mlkem.NewEncapsulationKey1024(pub)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
setCoeff := func(pub []byte, i int, v uint16) {
|
||||||
|
b := pub[i/2*3:]
|
||||||
|
if i%2 == 0 {
|
||||||
|
b[0] = byte(v)
|
||||||
|
b[1] = b[1]&0xf0 | byte(v>>8)
|
||||||
|
} else {
|
||||||
|
b[1] = b[1]&0x0f | byte(v<<4)
|
||||||
|
b[2] = byte(v >> 4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for range 8 {
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
require.NoError(t, err)
|
||||||
|
pub := dk.EncapsulationKey().Bytes()
|
||||||
|
require.True(t, mlkem1024.checkPublicKey(pub))
|
||||||
|
for _, i := range []int{0, 1, 2, 3, 511, 512, 1022, 1023} {
|
||||||
|
for _, v := range []uint16{0, 3328, 3329, 4095} {
|
||||||
|
p := bytes.Clone(pub)
|
||||||
|
setCoeff(p, i, v)
|
||||||
|
assert.Equal(t, stdlib(p), mlkem1024.checkPublicKey(p), "coefficient %d = %d", i, v)
|
||||||
|
assert.Equal(t, v < 3329, mlkem1024.checkPublicKey(p), "coefficient %d = %d", i, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub := make([]byte, mlkem.EncapsulationKeySize1024)
|
||||||
|
for range 64 {
|
||||||
|
_, _ = rand.Read(pub)
|
||||||
|
assert.Equal(t, stdlib(pub), mlkem1024.checkPublicKey(pub))
|
||||||
|
}
|
||||||
|
assert.False(t, mlkem1024.checkPublicKey(pub[:len(pub)-1]))
|
||||||
|
}
|
||||||
+75
-8
@@ -26,6 +26,7 @@ type TBSCertificate struct {
|
|||||||
NotAfter time.Time
|
NotAfter time.Time
|
||||||
PublicKey []byte
|
PublicKey []byte
|
||||||
Curve Curve
|
Curve Curve
|
||||||
|
PQSuite PQSuite // version 3 only, the suite of the certificate and of its signing CA
|
||||||
issuer string
|
issuer string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,31 +48,95 @@ type SignerLambda func(certBytes []byte) ([]byte, error)
|
|||||||
// details do not violate constraints of the signing certificate.
|
// details do not violate constraints of the signing certificate.
|
||||||
// If the TBSCertificate is a CA then signer must be nil.
|
// If the TBSCertificate is a CA then signer must be nil.
|
||||||
func (t *TBSCertificate) Sign(signer Certificate, curve Curve, key []byte) (Certificate, error) {
|
func (t *TBSCertificate) Sign(signer Certificate, curve Curve, key []byte) (Certificate, error) {
|
||||||
switch t.Curve {
|
if t.Version == Version3 {
|
||||||
|
// The signing key is a composite CA key, classical || the seed of the suite's signature algorithm.
|
||||||
|
classicalKey, pqKey, err := t.PQSuite.SplitPrivateKey(true, key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
classical, err := classicalSigner(t.Curve, classicalKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pq, err := SignPQ(t.PQSuite, pqKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.SignHybridWith(signer, curve, classical, pq)
|
||||||
|
}
|
||||||
|
|
||||||
|
sp, err := classicalSigner(t.Curve, key)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return t.SignWith(signer, curve, sp)
|
||||||
|
}
|
||||||
|
|
||||||
|
// classicalSigner returns a SignerLambda for a classical signing private key.
|
||||||
|
func classicalSigner(curve Curve, key []byte) (SignerLambda, error) {
|
||||||
|
switch curve {
|
||||||
case Curve_CURVE25519:
|
case Curve_CURVE25519:
|
||||||
|
if len(key) != ed25519.PrivateKeySize {
|
||||||
|
return nil, ErrInvalidPrivateKey
|
||||||
|
}
|
||||||
pk := ed25519.PrivateKey(key)
|
pk := ed25519.PrivateKey(key)
|
||||||
sp := func(certBytes []byte) ([]byte, error) {
|
return func(certBytes []byte) ([]byte, error) {
|
||||||
sig := ed25519.Sign(pk, certBytes)
|
sig := ed25519.Sign(pk, certBytes)
|
||||||
return sig, nil
|
return sig, nil
|
||||||
}
|
}, nil
|
||||||
return t.SignWith(signer, curve, sp)
|
|
||||||
case Curve_P256:
|
case Curve_P256:
|
||||||
pk, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), key)
|
pk, err := ecdsa.ParseRawPrivateKey(elliptic.P256(), key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
sp := func(certBytes []byte) ([]byte, error) {
|
return func(certBytes []byte) ([]byte, error) {
|
||||||
// We need to hash first for ECDSA
|
// We need to hash first for ECDSA
|
||||||
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
// - https://pkg.go.dev/crypto/ecdsa#SignASN1
|
||||||
hashed := sha256.Sum256(certBytes)
|
hashed := sha256.Sum256(certBytes)
|
||||||
return ecdsa.SignASN1(rand.Reader, pk, hashed[:])
|
return ecdsa.SignASN1(rand.Reader, pk, hashed[:])
|
||||||
}
|
}, nil
|
||||||
return t.SignWith(signer, curve, sp)
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid curve: %s", t.Curve)
|
return nil, fmt.Errorf("invalid curve: %s", curve)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SignHybridWith signs a version 3 certificate, using sp for the classical signature and pqSp for the post-quantum
|
||||||
|
// signature of t.PQSuite. t.PublicKey is the composite public key. signer must be a version 3 CA in the same suite,
|
||||||
|
// or nil for a self signed CA. The v2 form is signed exactly as SignWith would sign it, and so is the v2 form of
|
||||||
|
// signer.
|
||||||
|
func (t *TBSCertificate) SignHybridWith(signer Certificate, curve Curve, sp, pqSp SignerLambda) (Certificate, error) {
|
||||||
|
if t.Version != Version3 {
|
||||||
|
return nil, fmt.Errorf("SignHybridWith only signs version 3 certificates")
|
||||||
|
}
|
||||||
|
if !t.PQSuite.Valid() {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrUnknownPQSuite, t.PQSuite)
|
||||||
|
}
|
||||||
|
classical, pq, err := t.PQSuite.SplitPublicKey(t.IsCA, t.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var signerV2 Certificate
|
||||||
|
if signer != nil {
|
||||||
|
if signer.Version() != Version3 {
|
||||||
|
return nil, fmt.Errorf("a version 3 certificate must be signed by a version 3 CA")
|
||||||
|
}
|
||||||
|
if PQSuiteOf(signer) != t.PQSuite {
|
||||||
|
return nil, ErrPQSuiteMismatch
|
||||||
|
}
|
||||||
|
signerV2 = V2Form(signer)
|
||||||
|
}
|
||||||
|
|
||||||
|
t2 := *t
|
||||||
|
t2.Version = Version2
|
||||||
|
t2.PublicKey = classical
|
||||||
|
c2, err := t2.SignWith(signerV2, curve, sp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ExtendToV3(c2, t.PQSuite, pq, signer, pqSp)
|
||||||
|
}
|
||||||
|
|
||||||
// SignWith does the same thing as sign, but uses the function in `sp` to calculate the signature.
|
// SignWith does the same thing as sign, but uses the function in `sp` to calculate the signature.
|
||||||
// You should only use SignWith if you do not have direct access to your private key.
|
// You should only use SignWith if you do not have direct access to your private key.
|
||||||
func (t *TBSCertificate) SignWith(signer Certificate, curve Curve, sp SignerLambda) (Certificate, error) {
|
func (t *TBSCertificate) SignWith(signer Certificate, curve Curve, sp SignerLambda) (Certificate, error) {
|
||||||
@@ -114,6 +179,8 @@ func (t *TBSCertificate) SignWith(signer Certificate, curve Curve, sp SignerLamb
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
case Version3:
|
||||||
|
return nil, fmt.Errorf("version 3 certificates carry two signatures, use SignHybridWith")
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown cert version %d", t.Version)
|
return nil, fmt.Errorf("unknown cert version %d", t.Version)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -136,47 +135,3 @@ func TestCertificate_SignP256_AlwaysNormalized(t *testing.T) {
|
|||||||
assert.True(t, normie)
|
assert.True(t, normie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCertificateV2_SignRejectsUnparseable(t *testing.T) {
|
|
||||||
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
||||||
require.NoError(t, err)
|
|
||||||
var invalid *ErrInvalidCertificateProperties
|
|
||||||
|
|
||||||
newTBS := func() TBSCertificate {
|
|
||||||
return TBSCertificate{
|
|
||||||
Version: Version2,
|
|
||||||
Name: "testing",
|
|
||||||
Networks: []netip.Prefix{mustParsePrefixUnmapped("10.1.1.1/24")},
|
|
||||||
Groups: []string{"test-group1"},
|
|
||||||
NotBefore: time.Now().Add(-time.Minute),
|
|
||||||
NotAfter: time.Now().Add(time.Minute),
|
|
||||||
PublicKey: []byte("1234567890abcedfghij1234567890ab"),
|
|
||||||
Curve: Curve_CURVE25519,
|
|
||||||
IsCA: true,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tbs := newTBS()
|
|
||||||
tbs.Name = strings.Repeat("a", MaxNameLength)
|
|
||||||
c, err := tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.NoError(t, err)
|
|
||||||
b, err := c.Marshal()
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = unmarshalCertificateV2(b, nil, Curve_CURVE25519)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Name = strings.Repeat("a", MaxNameLength+1)
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Name = ""
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
|
|
||||||
tbs = newTBS()
|
|
||||||
tbs.Groups = []string{"test-group1", ""}
|
|
||||||
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
|
||||||
require.ErrorAs(t, err, &invalid)
|
|
||||||
}
|
|
||||||
@@ -40,6 +40,10 @@ func NewTestCaCert(version cert.Version, curve cert.Curve, before, after time.Ti
|
|||||||
// There is no default to allow the underlying lib to respond with an error
|
// There is no default to allow the underlying lib to respond with an error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version == cert.Version3 {
|
||||||
|
pub, priv = appendPQSigningKey(pub, priv)
|
||||||
|
}
|
||||||
|
|
||||||
if before.IsZero() {
|
if before.IsZero() {
|
||||||
before = testCertNow.Add(time.Second * -60)
|
before = testCertNow.Add(time.Second * -60)
|
||||||
}
|
}
|
||||||
@@ -59,6 +63,9 @@ func NewTestCaCert(version cert.Version, curve cert.Curve, before, after time.Ti
|
|||||||
Groups: groups,
|
Groups: groups,
|
||||||
IsCA: true,
|
IsCA: true,
|
||||||
}
|
}
|
||||||
|
if version == cert.Version3 {
|
||||||
|
t.PQSuite = cert.DefaultPQSuite
|
||||||
|
}
|
||||||
|
|
||||||
c, err := t.Sign(nil, curve, priv)
|
c, err := t.Sign(nil, curve, priv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -94,6 +101,10 @@ func NewTestCert(v cert.Version, curve cert.Curve, ca cert.Certificate, key []by
|
|||||||
panic("unknown curve")
|
panic("unknown curve")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v == cert.Version3 {
|
||||||
|
pub, priv = appendPQKey(ca, pub, priv)
|
||||||
|
}
|
||||||
|
|
||||||
nc := &cert.TBSCertificate{
|
nc := &cert.TBSCertificate{
|
||||||
Version: v,
|
Version: v,
|
||||||
Curve: curve,
|
Curve: curve,
|
||||||
@@ -105,6 +116,7 @@ func NewTestCert(v cert.Version, curve cert.Curve, ca cert.Certificate, key []by
|
|||||||
NotAfter: time.Unix(after.Unix(), 0),
|
NotAfter: time.Unix(after.Unix(), 0),
|
||||||
PublicKey: pub,
|
PublicKey: pub,
|
||||||
IsCA: false,
|
IsCA: false,
|
||||||
|
PQSuite: cert.PQSuiteOf(ca),
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := nc.Sign(ca, ca.Curve(), key)
|
c, err := nc.Sign(ca, ca.Curve(), key)
|
||||||
@@ -117,6 +129,9 @@ func NewTestCert(v cert.Version, curve cert.Curve, ca cert.Certificate, key []by
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v == cert.Version3 {
|
||||||
|
return c, pub, cert.MarshalPQPrivateKeyToPEM(curve, cert.PQSuiteOf(ca), priv), pem
|
||||||
|
}
|
||||||
return c, pub, cert.MarshalPrivateKeyToPEM(curve, priv), pem
|
return c, pub, cert.MarshalPrivateKeyToPEM(curve, priv), pem
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,6 +147,7 @@ func NewTestCertDifferentVersion(c cert.Certificate, v cert.Version, ca cert.Cer
|
|||||||
NotAfter: time.Unix(c.NotAfter().Unix(), 0),
|
NotAfter: time.Unix(c.NotAfter().Unix(), 0),
|
||||||
PublicKey: c.PublicKey(),
|
PublicKey: c.PublicKey(),
|
||||||
IsCA: false,
|
IsCA: false,
|
||||||
|
PQSuite: cert.PQSuiteOf(ca),
|
||||||
}
|
}
|
||||||
|
|
||||||
c, err := nc.Sign(ca, ca.Curve(), key)
|
c, err := nc.Sign(ca, ca.Curve(), key)
|
||||||
@@ -147,6 +163,24 @@ func NewTestCertDifferentVersion(c cert.Certificate, v cert.Version, ca cert.Cer
|
|||||||
return c, pem
|
return c, pem
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// appendPQSigningKey makes a classical CA keypair into a version 3 composite one with a fresh key of the default suite.
|
||||||
|
func appendPQSigningKey(pub, priv []byte) ([]byte, []byte) {
|
||||||
|
pqPub, pqPriv, err := cert.DefaultPQSuite.GenerateKey(true)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendPQKey makes a classical host keypair into a version 3 composite one with a fresh key of the CA's suite.
|
||||||
|
func appendPQKey(ca cert.Certificate, pub, priv []byte) ([]byte, []byte) {
|
||||||
|
pqPub, pqPriv, err := cert.PQSuiteOf(ca).GenerateKey(false)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...)
|
||||||
|
}
|
||||||
|
|
||||||
func X25519Keypair() ([]byte, []byte) {
|
func X25519Keypair() ([]byte, []byte) {
|
||||||
privkey := make([]byte, 32)
|
privkey := make([]byte, 32)
|
||||||
if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
|
if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
|
||||||
|
|||||||
+32
-8
@@ -37,6 +37,7 @@ type caFlags struct {
|
|||||||
argonParallelism *uint
|
argonParallelism *uint
|
||||||
encryption *bool
|
encryption *bool
|
||||||
version *uint
|
version *uint
|
||||||
|
pqSuite *string
|
||||||
|
|
||||||
curve *string
|
curve *string
|
||||||
p11url *string
|
p11url *string
|
||||||
@@ -71,7 +72,8 @@ func newCaFlags() *caFlags {
|
|||||||
cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
|
cf := caFlags{set: flag.NewFlagSet("ca", flag.ContinueOnError)}
|
||||||
cf.set.Usage = func() {}
|
cf.set.Usage = func() {}
|
||||||
cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
|
cf.name = cf.set.String("name", "", "Required: name of the certificate authority")
|
||||||
cf.version = cf.set.Uint("version", uint(cert.Version2), "Optional: version of the certificate format to use")
|
cf.version = cf.set.Uint("version", uint(cert.Version2), "Optional: version of the certificate format to use. Version 3 adds a post-quantum key, for post-quantum certificates")
|
||||||
|
cf.pqSuite = pqSuiteFlag(cf.set, "Optional: post-quantum suite of a version 3 CA, which the certificates it signs share")
|
||||||
cf.duration = cf.set.Duration("duration", time.Duration(time.Hour*8760), "Optional: amount of time the certificate should be valid for. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
|
cf.duration = cf.set.Duration("duration", time.Duration(time.Hour*8760), "Optional: amount of time the certificate should be valid for. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
|
||||||
cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
|
cf.outKeyPath = cf.set.String("out-key", "ca.key", "Optional: path to write the private key to")
|
||||||
cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
|
cf.outCertPath = cf.set.String("out-crt", "ca.crt", "Optional: path to write the certificate to")
|
||||||
@@ -160,8 +162,15 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
version := cert.Version(*cf.version)
|
version := cert.Version(*cf.version)
|
||||||
if version != cert.Version1 && version != cert.Version2 {
|
if version != cert.Version1 && version != cert.Version2 && version != cert.Version3 {
|
||||||
return newHelpErrorf("-version must be either %v or %v", cert.Version1, cert.Version2)
|
return newHelpErrorf("-version must be %v, %v or %v", cert.Version1, cert.Version2, cert.Version3)
|
||||||
|
}
|
||||||
|
if version == cert.Version3 && isP11 {
|
||||||
|
return newHelpErrorf("-version 3 can not be used with -pkcs11, the post-quantum key has to be in out-key")
|
||||||
|
}
|
||||||
|
suite, err := parsePQSuiteFlag(cf.set, *cf.pqSuite, version == cert.Version3, "is only for -version 3")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var networks []netip.Prefix
|
var networks []netip.Prefix
|
||||||
@@ -295,6 +304,13 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid curve: %s", *cf.curve)
|
return fmt.Errorf("invalid curve: %s", *cf.curve)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if version == cert.Version3 {
|
||||||
|
pub, rawPriv, err = appendPQSigningKey(suite, pub, rawPriv)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &cert.TBSCertificate{
|
t := &cert.TBSCertificate{
|
||||||
@@ -309,6 +325,9 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
|||||||
IsCA: true,
|
IsCA: true,
|
||||||
Curve: curve,
|
Curve: curve,
|
||||||
}
|
}
|
||||||
|
if version == cert.Version3 {
|
||||||
|
t.PQSuite = suite
|
||||||
|
}
|
||||||
|
|
||||||
if !isP11 && !isStdio(*cf.outKeyPath) {
|
if !isP11 && !isStdio(*cf.outKeyPath) {
|
||||||
if _, err := os.Stat(*cf.outKeyPath); err == nil {
|
if _, err := os.Stat(*cf.outKeyPath); err == nil {
|
||||||
@@ -336,14 +355,19 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
|||||||
return fmt.Errorf("error while signing: %s", err)
|
return fmt.Errorf("error while signing: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *cf.encryption {
|
switch {
|
||||||
|
case *cf.encryption && version == cert.Version3:
|
||||||
|
b, err = cert.EncryptAndMarshalPQSigningPrivateKey(curve, suite, rawPriv, passphrase, kdfParams)
|
||||||
|
case *cf.encryption:
|
||||||
b, err = cert.EncryptAndMarshalSigningPrivateKey(curve, rawPriv, passphrase, kdfParams)
|
b, err = cert.EncryptAndMarshalSigningPrivateKey(curve, rawPriv, passphrase, kdfParams)
|
||||||
if err != nil {
|
case version == cert.Version3:
|
||||||
return fmt.Errorf("error while encrypting out-key: %s", err)
|
b = cert.MarshalPQSigningPrivateKeyToPEM(curve, suite, rawPriv)
|
||||||
}
|
default:
|
||||||
} else {
|
|
||||||
b = cert.MarshalSigningPrivateKeyToPEM(curve, rawPriv)
|
b = cert.MarshalSigningPrivateKeyToPEM(curve, rawPriv)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while encrypting out-key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
err = writeOutput(*cf.outKeyPath, b, 0600, out)
|
err = writeOutput(*cf.outKeyPath, b, 0600, out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -68,12 +68,14 @@ func Test_caHelp(t *testing.T) {
|
|||||||
" -out-qr string\n"+
|
" -out-qr string\n"+
|
||||||
" \tOptional: output a qr code image (png) of the certificate\n"+
|
" \tOptional: output a qr code image (png) of the certificate\n"+
|
||||||
optionalPkcs11String(" -pkcs11 string\n \tOptional: PKCS#11 URI to an existing private key\n")+
|
optionalPkcs11String(" -pkcs11 string\n \tOptional: PKCS#11 URI to an existing private key\n")+
|
||||||
|
" -pq-suite string\n"+
|
||||||
|
" \tOptional: post-quantum suite of a version 3 CA, which the certificates it signs share (MLKEM1024_MLDSA87) (default \"MLKEM1024_MLDSA87\")\n"+
|
||||||
" -subnets string\n"+
|
" -subnets string\n"+
|
||||||
" \tDeprecated, see -unsafe-networks\n"+
|
" \tDeprecated, see -unsafe-networks\n"+
|
||||||
" -unsafe-networks string\n"+
|
" -unsafe-networks string\n"+
|
||||||
" \tOptional: comma separated list of ip address and network in CIDR notation. This will limit which ip addresses and networks subordinate certs can use in unsafe networks\n"+
|
" \tOptional: comma separated list of ip address and network in CIDR notation. This will limit which ip addresses and networks subordinate certs can use in unsafe networks\n"+
|
||||||
" -version uint\n"+
|
" -version uint\n"+
|
||||||
" \tOptional: version of the certificate format to use (default 2)\n",
|
" \tOptional: version of the certificate format to use. Version 3 adds a post-quantum key, for post-quantum certificates (default 2)\n",
|
||||||
ob.String(),
|
ob.String(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ type keygenFlags struct {
|
|||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
outKeyPath *string
|
outKeyPath *string
|
||||||
outPubPath *string
|
outPubPath *string
|
||||||
|
inKeyPath *string
|
||||||
curve *string
|
curve *string
|
||||||
|
version *uint
|
||||||
|
pqSuite *string
|
||||||
p11url *string
|
p11url *string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +30,9 @@ func newKeygenFlags() *keygenFlags {
|
|||||||
cf.outPubPath = cf.set.String("out-pub", "", "Required: path to write the public key to")
|
cf.outPubPath = cf.set.String("out-pub", "", "Required: path to write the public key to")
|
||||||
cf.outKeyPath = cf.set.String("out-key", "", "Required: path to write the private key to")
|
cf.outKeyPath = cf.set.String("out-key", "", "Required: path to write the private key to")
|
||||||
cf.curve = cf.set.String("curve", defaultCurve(), "ECDH Curve (25519, P256)")
|
cf.curve = cf.set.String("curve", defaultCurve(), "ECDH Curve (25519, P256)")
|
||||||
|
cf.version = cf.set.Uint("version", 0, "Optional: 3 adds a post-quantum key, for version 3 certificates")
|
||||||
|
cf.inKeyPath = cf.set.String("in-key", "", "Optional: path to an existing private key to add a post-quantum key to, keeping the existing key. Implies -version 3")
|
||||||
|
cf.pqSuite = pqSuiteFlag(cf.set, "Optional: post-quantum suite of a version 3 key, which must be the suite of the CA that signs it")
|
||||||
cf.p11url = p11Flag(cf.set)
|
cf.p11url = p11Flag(cf.set)
|
||||||
return &cf
|
return &cf
|
||||||
}
|
}
|
||||||
@@ -51,9 +57,45 @@ func keygen(args []string, out io.Writer, errOut io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
version := cert.Version(*cf.version)
|
||||||
|
if *cf.inKeyPath != "" {
|
||||||
|
version = cert.Version3
|
||||||
|
}
|
||||||
|
if version != 0 && version != cert.Version3 {
|
||||||
|
return newHelpErrorf("-version must be %v, or left out for keys that work with any other version", cert.Version3)
|
||||||
|
}
|
||||||
|
if version == cert.Version3 && isP11 {
|
||||||
|
return newHelpErrorf("-version 3 can not be used with -pkcs11, the post-quantum key has to be in out-key")
|
||||||
|
}
|
||||||
|
suite, err := parsePQSuiteFlag(cf.set, *cf.pqSuite, version == cert.Version3, "is only for -version 3")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var claims ioClaims
|
||||||
|
if err := reserveInputs(&claims, "in-key", *cf.inKeyPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var pub, rawPriv []byte
|
var pub, rawPriv []byte
|
||||||
var curve cert.Curve
|
var curve cert.Curve
|
||||||
if isP11 {
|
if *cf.inKeyPath != "" {
|
||||||
|
raw, err := readInput("in-key", *cf.inKeyPath, &claims)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while reading in-key: %s", err)
|
||||||
|
}
|
||||||
|
rawPriv, _, curve, err = cert.UnmarshalPrivateKeyFromPEM(raw)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while parsing in-key: %s", err)
|
||||||
|
}
|
||||||
|
pub, err = publicKeyFor(curve, rawPriv)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while parsing in-key: %s", err)
|
||||||
|
}
|
||||||
|
if fips140.Enforced() && curve == cert.Curve_CURVE25519 {
|
||||||
|
return errors.New("use of Curve25519 is not allowed in FIPS 140-only mode")
|
||||||
|
}
|
||||||
|
} else if isP11 {
|
||||||
switch *cf.curve {
|
switch *cf.curve {
|
||||||
case "P256":
|
case "P256":
|
||||||
curve = cert.Curve_P256
|
curve = cert.Curve_P256
|
||||||
@@ -76,7 +118,13 @@ func keygen(args []string, out io.Writer, errOut io.Writer) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var claims ioClaims
|
if version == cert.Version3 {
|
||||||
|
pub, rawPriv, err = appendPQKey(suite, pub, rawPriv)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := reserveOutputs(&claims,
|
if err := reserveOutputs(&claims,
|
||||||
"out-key", *cf.outKeyPath,
|
"out-key", *cf.outKeyPath,
|
||||||
"out-pub", *cf.outPubPath,
|
"out-pub", *cf.outPubPath,
|
||||||
@@ -97,12 +145,20 @@ func keygen(args []string, out io.Writer, errOut io.Writer) error {
|
|||||||
return fmt.Errorf("error while getting public key: %w", err)
|
return fmt.Errorf("error while getting public key: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = writeOutput(*cf.outKeyPath, cert.MarshalPrivateKeyToPEM(curve, rawPriv), 0600, out)
|
b := cert.MarshalPrivateKeyToPEM(curve, rawPriv)
|
||||||
|
if version == cert.Version3 {
|
||||||
|
b = cert.MarshalPQPrivateKeyToPEM(curve, suite, rawPriv)
|
||||||
|
}
|
||||||
|
err = writeOutput(*cf.outKeyPath, b, 0600, out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error while writing out-key: %s", err)
|
return fmt.Errorf("error while writing out-key: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = writeOutput(*cf.outPubPath, cert.MarshalPublicKeyToPEM(curve, pub), 0600, out)
|
b := cert.MarshalPublicKeyToPEM(curve, pub)
|
||||||
|
if version == cert.Version3 {
|
||||||
|
b = cert.MarshalPQPublicKeyToPEM(curve, suite, pub)
|
||||||
|
}
|
||||||
|
err = writeOutput(*cf.outPubPath, b, 0600, out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error while writing out-pub: %s", err)
|
return fmt.Errorf("error while writing out-pub: %s", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,17 @@ func Test_keygenHelp(t *testing.T) {
|
|||||||
" Pass \"-\" to any path flag to read from stdin or write to stdout.\n"+
|
" Pass \"-\" to any path flag to read from stdin or write to stdout.\n"+
|
||||||
" -curve string\n"+
|
" -curve string\n"+
|
||||||
" \tECDH Curve (25519, P256) (default \"25519\")\n"+
|
" \tECDH Curve (25519, P256) (default \"25519\")\n"+
|
||||||
|
" -in-key string\n"+
|
||||||
|
" \tOptional: path to an existing private key to add a post-quantum key to, keeping the existing key. Implies -version 3\n"+
|
||||||
" -out-key string\n"+
|
" -out-key string\n"+
|
||||||
" \tRequired: path to write the private key to\n"+
|
" \tRequired: path to write the private key to\n"+
|
||||||
" -out-pub string\n"+
|
" -out-pub string\n"+
|
||||||
" \tRequired: path to write the public key to\n"+
|
" \tRequired: path to write the public key to\n"+
|
||||||
optionalPkcs11String(" -pkcs11 string\n \tOptional: PKCS#11 URI to an existing private key\n"),
|
optionalPkcs11String(" -pkcs11 string\n \tOptional: PKCS#11 URI to an existing private key\n")+
|
||||||
|
" -pq-suite string\n"+
|
||||||
|
" \tOptional: post-quantum suite of a version 3 key, which must be the suite of the CA that signs it (MLKEM1024_MLDSA87) (default \"MLKEM1024_MLDSA87\")\n"+
|
||||||
|
" -version uint\n"+
|
||||||
|
" \tOptional: 3 adds a post-quantum key, for version 3 certificates\n",
|
||||||
ob.String(),
|
ob.String(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ func main() {
|
|||||||
err = printCert(args[1:], os.Stdout, os.Stderr)
|
err = printCert(args[1:], os.Stdout, os.Stderr)
|
||||||
case "verify":
|
case "verify":
|
||||||
err = verify(args[1:], os.Stdout, os.Stderr)
|
err = verify(args[1:], os.Stdout, os.Stderr)
|
||||||
|
case "pq-extend":
|
||||||
|
err = pqExtend(args[1:], os.Stdout, os.Stderr, StdinPasswordReader{})
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("unknown mode: %s", args[0])
|
err = fmt.Errorf("unknown mode: %s", args[0])
|
||||||
}
|
}
|
||||||
@@ -122,6 +124,8 @@ func handleError(mode string, e error, out io.Writer) int {
|
|||||||
printHelp(out)
|
printHelp(out)
|
||||||
case "verify":
|
case "verify":
|
||||||
verifyHelp(out)
|
verifyHelp(out)
|
||||||
|
case "pq-extend":
|
||||||
|
pqExtendHelp(out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +149,7 @@ func help(err string, out io.Writer) {
|
|||||||
fmt.Fprintln(out, " "+signSummary())
|
fmt.Fprintln(out, " "+signSummary())
|
||||||
fmt.Fprintln(out, " "+printSummary())
|
fmt.Fprintln(out, " "+printSummary())
|
||||||
fmt.Fprintln(out, " "+verifySummary())
|
fmt.Fprintln(out, " "+verifySummary())
|
||||||
|
fmt.Fprintln(out, " "+pqExtendSummary())
|
||||||
fmt.Fprintln(out, "")
|
fmt.Fprintln(out, "")
|
||||||
fmt.Fprintf(out, " To see usage for a given mode, use %s <mode> -h\n", os.Args[0])
|
fmt.Fprintf(out, " To see usage for a given mode, use %s <mode> -h\n", os.Args[0])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ func Test_help(t *testing.T) {
|
|||||||
" " + signSummary() + "\n" +
|
" " + signSummary() + "\n" +
|
||||||
" " + printSummary() + "\n" +
|
" " + printSummary() + "\n" +
|
||||||
" " + verifySummary() + "\n" +
|
" " + verifySummary() + "\n" +
|
||||||
|
" " + pqExtendSummary() + "\n" +
|
||||||
"\n" +
|
"\n" +
|
||||||
" To see usage for a given mode, use " + os.Args[0] + " <mode> -h\n"
|
" To see usage for a given mode, use " + os.Args[0] + " <mode> -h\n"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,311 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/ecdh"
|
||||||
|
"encoding/pem"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// pqSuiteFlag adds -pq-suite to a flag set.
|
||||||
|
func pqSuiteFlag(set *flag.FlagSet, usage string) *string {
|
||||||
|
var names []string
|
||||||
|
for _, s := range cert.PQSuites() {
|
||||||
|
names = append(names, s.String())
|
||||||
|
}
|
||||||
|
return set.String("pq-suite", cert.DefaultPQSuite.String(), usage+" ("+strings.Join(names, ", ")+")")
|
||||||
|
}
|
||||||
|
|
||||||
|
// parsePQSuiteFlag returns the suite -pq-suite names. Setting it is an error unless allowed, when the suite has no use
|
||||||
|
// or comes from elsewhere.
|
||||||
|
func parsePQSuiteFlag(set *flag.FlagSet, value string, allowed bool, why string) (cert.PQSuite, error) {
|
||||||
|
given := false
|
||||||
|
set.Visit(func(f *flag.Flag) {
|
||||||
|
if f.Name == "pq-suite" {
|
||||||
|
given = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if given && !allowed {
|
||||||
|
return cert.PQSuiteUnknown, newHelpErrorf("-pq-suite %s", why)
|
||||||
|
}
|
||||||
|
s, err := cert.ParsePQSuite(value)
|
||||||
|
if err != nil {
|
||||||
|
return cert.PQSuiteUnknown, newHelpErrorf("-pq-suite: %s", err)
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendPQSigningKey makes a classical CA keypair into a version 3 composite one with a fresh key of suite.
|
||||||
|
func appendPQSigningKey(suite cert.PQSuite, pub, priv []byte) ([]byte, []byte, error) {
|
||||||
|
pqPub, pqPriv, err := suite.GenerateKey(true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("error while generating %v CA key: %s", suite, err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// appendPQKey makes a classical host keypair into a version 3 composite one with a fresh key of suite.
|
||||||
|
func appendPQKey(suite cert.PQSuite, pub, priv []byte) ([]byte, []byte, error) {
|
||||||
|
pqPub, pqPriv, err := suite.GenerateKey(false)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("error while generating %v key: %s", suite, err)
|
||||||
|
}
|
||||||
|
return append(pub, pqPub...), append(priv, pqPriv...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// publicKeyFor returns the public key of a classical host private key.
|
||||||
|
func publicKeyFor(curve cert.Curve, priv []byte) ([]byte, error) {
|
||||||
|
var c ecdh.Curve
|
||||||
|
switch curve {
|
||||||
|
case cert.Curve_CURVE25519:
|
||||||
|
c = ecdh.X25519()
|
||||||
|
case cert.Curve_P256:
|
||||||
|
c = ecdh.P256()
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("invalid curve: %s", curve)
|
||||||
|
}
|
||||||
|
k, err := c.NewPrivateKey(priv)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("not a classical %s private key, it may already have a post-quantum key", curve)
|
||||||
|
}
|
||||||
|
return k.PublicKey().Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type pqExtendFlags struct {
|
||||||
|
set *flag.FlagSet
|
||||||
|
caKeyPath *string
|
||||||
|
caCertPath *string
|
||||||
|
inCertPath *string
|
||||||
|
inPubPath *string
|
||||||
|
outCertPath *string
|
||||||
|
outKeyPath *string
|
||||||
|
pqSuite *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPQExtendFlags() *pqExtendFlags {
|
||||||
|
pf := pqExtendFlags{set: flag.NewFlagSet("pq-extend", flag.ContinueOnError)}
|
||||||
|
pf.set.Usage = func() {}
|
||||||
|
pf.inCertPath = pf.set.String("in-crt", "", "Required: path to the version 2 certificate to extend, a CA or a host")
|
||||||
|
pf.outCertPath = pf.set.String("out-crt", "", "Required: path to write the version 3 certificate to")
|
||||||
|
pf.caKeyPath = pf.set.String("ca-key", "ca.key", "Optional: path to the CA key. For a CA in-crt, its own version 2 key. For a host in-crt, the version 3 key of the CA that issued it")
|
||||||
|
pf.caCertPath = pf.set.String("ca-crt", "", "Required for a host in-crt: path to the version 3 form of the CA that issued it")
|
||||||
|
pf.inPubPath = pf.set.String("in-pub", "", "Required for a host in-crt: path to the host's public key from keygen -in-key")
|
||||||
|
pf.outKeyPath = pf.set.String("out-key", "", "Required for a CA in-crt: path to write the CA's version 3 key to, encrypted again if ca-key was")
|
||||||
|
pf.pqSuite = pqSuiteFlag(pf.set, "Optional, for a CA in-crt: post-quantum suite of the version 3 CA. A host takes its CA's suite")
|
||||||
|
return &pf
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqExtend turns a version 2 certificate into a version 3 one in place: the v2 form, and so its fingerprint and
|
||||||
|
// existing trust in it, stays exactly as it was.
|
||||||
|
func pqExtend(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error {
|
||||||
|
pf := newPQExtendFlags()
|
||||||
|
if err := pf.set.Parse(args); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mustFlagString("in-crt", pf.inCertPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mustFlagString("out-crt", pf.outCertPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := mustFlagString("ca-key", pf.caKeyPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var claims ioClaims
|
||||||
|
if err := reserveInputs(&claims,
|
||||||
|
"in-crt", *pf.inCertPath,
|
||||||
|
"ca-key", *pf.caKeyPath,
|
||||||
|
"ca-crt", *pf.caCertPath,
|
||||||
|
"in-pub", *pf.inPubPath,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := reserveOutputs(&claims,
|
||||||
|
"out-crt", *pf.outCertPath,
|
||||||
|
"out-key", *pf.outKeyPath,
|
||||||
|
); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rawCert, err := readInput("in-crt", *pf.inCertPath, &claims)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while reading in-crt: %s", err)
|
||||||
|
}
|
||||||
|
c, _, err := cert.UnmarshalCertificateFromPEM(rawCert)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while parsing in-crt: %s", err)
|
||||||
|
}
|
||||||
|
if c.Version() != cert.Version2 {
|
||||||
|
return fmt.Errorf("only version 2 certificates extend to version 3, in-crt is version %d", c.Version())
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, p := range []string{*pf.outCertPath, *pf.outKeyPath} {
|
||||||
|
if p != "" && !isStdio(p) {
|
||||||
|
if _, err := os.Stat(p); err == nil {
|
||||||
|
return fmt.Errorf("refusing to overwrite existing file: %s", p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rawCAKey, err := readInput("ca-key", *pf.caKeyPath, &claims)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while reading ca-key: %s", err)
|
||||||
|
}
|
||||||
|
curve, caKey, passphrase, err := readSigningKey(rawCAKey, errOut, pr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
suite, err := parsePQSuiteFlag(pf.set, *pf.pqSuite, c.IsCA(), "is for CA certificates, a host takes its CA's suite")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var c3 cert.Certificate
|
||||||
|
if c.IsCA() {
|
||||||
|
c3, err = extendCA(pf, c, suite, curve, caKey, rawCAKey, passphrase, out)
|
||||||
|
} else {
|
||||||
|
c3, err = extendHost(pf, c, curve, caKey, &claims)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := c3.MarshalPEM()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while marshalling certificate: %s", err)
|
||||||
|
}
|
||||||
|
if err := writeOutput(*pf.outCertPath, b, 0600, out); err != nil {
|
||||||
|
return fmt.Errorf("error while writing out-crt: %s", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// extendCA gives a v2 CA a post-quantum key of suite and writes out the composite key.
|
||||||
|
func extendCA(pf *pqExtendFlags, ca cert.Certificate, suite cert.PQSuite, curve cert.Curve, key, rawKey, passphrase []byte, out io.Writer) (cert.Certificate, error) {
|
||||||
|
if *pf.caCertPath != "" || *pf.inPubPath != "" {
|
||||||
|
return nil, newHelpErrorf("-ca-crt and -in-pub are for host certificates, a CA extends itself")
|
||||||
|
}
|
||||||
|
if err := mustFlagString("out-key", pf.outKeyPath); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := ca.VerifyPrivateKey(curve, key); err != nil {
|
||||||
|
return nil, fmt.Errorf("refusing to extend, the CA certificate does not match ca-key")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub, priv, err := appendPQSigningKey(suite, nil, append([]byte(nil), key...))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signPQ, err := cert.SignPQ(suite, priv[len(key):])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c3, err := cert.ExtendToV3(ca, suite, pub, nil, signPQ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while extending: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b []byte
|
||||||
|
if passphrase != nil {
|
||||||
|
// Encrypt it the way ca-key was, with a fresh salt.
|
||||||
|
block, _ := pem.Decode(rawKey)
|
||||||
|
if block == nil {
|
||||||
|
return nil, fmt.Errorf("error while parsing ca-key: input did not contain a valid PEM encoded block")
|
||||||
|
}
|
||||||
|
ned, err := cert.UnmarshalNebulaEncryptedData(block.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while parsing encrypted ca-key: %s", err)
|
||||||
|
}
|
||||||
|
a := ned.EncryptionMetadata.Argon2Parameters
|
||||||
|
kdf := cert.NewArgon2Parameters(a.Memory, a.Parallelism, a.Iterations)
|
||||||
|
b, err = cert.EncryptAndMarshalPQSigningPrivateKey(curve, suite, priv, passphrase, kdf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while encrypting out-key: %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
b = cert.MarshalPQSigningPrivateKeyToPEM(curve, suite, priv)
|
||||||
|
}
|
||||||
|
if err := writeOutput(*pf.outKeyPath, b, 0600, out); err != nil {
|
||||||
|
return nil, fmt.Errorf("error while writing out-key: %s", err)
|
||||||
|
}
|
||||||
|
return c3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// extendHost adds a host's post-quantum key to its certificate, signed by the v3 form of its CA, in the CA's suite.
|
||||||
|
func extendHost(pf *pqExtendFlags, c cert.Certificate, curve cert.Curve, caKey []byte, claims *ioClaims) (cert.Certificate, error) {
|
||||||
|
if *pf.outKeyPath != "" {
|
||||||
|
return nil, newHelpErrorf("-out-key is for CA certificates, a host's key comes from keygen -in-key")
|
||||||
|
}
|
||||||
|
if err := mustFlagString("ca-crt", pf.caCertPath); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := mustFlagString("in-pub", pf.inPubPath); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rawCA, err := readInput("ca-crt", *pf.caCertPath, claims)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while reading ca-crt: %s", err)
|
||||||
|
}
|
||||||
|
ca, _, err := cert.UnmarshalCertificateFromPEM(rawCA)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while parsing ca-crt: %s", err)
|
||||||
|
}
|
||||||
|
if ca.Version() != cert.Version3 {
|
||||||
|
return nil, fmt.Errorf("ca-crt must be a version 3 CA, extend the CA first")
|
||||||
|
}
|
||||||
|
if err := ca.VerifyPrivateKey(curve, caKey); err != nil {
|
||||||
|
return nil, fmt.Errorf("refusing to extend, ca-crt does not match ca-key")
|
||||||
|
}
|
||||||
|
|
||||||
|
rawPub, err := readInput("in-pub", *pf.inPubPath, claims)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while reading in-pub: %s", err)
|
||||||
|
}
|
||||||
|
pub, _, pubCurve, err := cert.UnmarshalPublicKeyFromPEM(rawPub)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while parsing in-pub: %s", err)
|
||||||
|
}
|
||||||
|
suite := cert.PQSuiteOf(ca)
|
||||||
|
classical, pq, err := suite.SplitPublicKey(false, pub)
|
||||||
|
if pubCurve != c.Curve() || err != nil {
|
||||||
|
return nil, fmt.Errorf("in-pub must be the host's key from keygen -in-key -pq-suite %v", suite)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(classical, c.PublicKey()) {
|
||||||
|
return nil, fmt.Errorf("in-pub does not extend the key in in-crt")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, pqCAKey, err := suite.SplitPrivateKey(true, caKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signPQ, err := cert.SignPQ(suite, pqCAKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c3, err := cert.ExtendToV3(c, suite, pq, ca, signPQ)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while extending: %s", err)
|
||||||
|
}
|
||||||
|
return c3, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func pqExtendSummary() string {
|
||||||
|
return "pq-extend <flags>: extend a version 2 CA or host certificate to version 3 in place, keeping its v2 form and fingerprint"
|
||||||
|
}
|
||||||
|
|
||||||
|
func pqExtendHelp(out io.Writer) {
|
||||||
|
pf := newPQExtendFlags()
|
||||||
|
out.Write([]byte("Usage of " + os.Args[0] + " " + pqExtendSummary() + "\n"))
|
||||||
|
out.Write([]byte(stdioHelpText))
|
||||||
|
pf.set.SetOutput(out)
|
||||||
|
pf.set.PrintDefaults()
|
||||||
|
}
|
||||||
@@ -0,0 +1,220 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readCert(t *testing.T, path string) cert.Certificate {
|
||||||
|
t.Helper()
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
c, _, err := cert.UnmarshalCertificateFromPEM(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func readSigningKeyFile(t *testing.T, path string) (cert.Curve, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
k, _, curve, err := cert.UnmarshalSigningPrivateKeyFromPEM(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return curve, k
|
||||||
|
}
|
||||||
|
|
||||||
|
func readKeyFile(t *testing.T, path string) (cert.Curve, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
require.NoError(t, err)
|
||||||
|
k, _, curve, err := cert.UnmarshalPrivateKeyFromPEM(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return curve, k
|
||||||
|
}
|
||||||
|
|
||||||
|
func verifiedBy(t *testing.T, c cert.Certificate, cas ...cert.Certificate) error {
|
||||||
|
t.Helper()
|
||||||
|
pool := cert.NewCAPool()
|
||||||
|
for _, ca := range cas {
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
}
|
||||||
|
_, err := pool.VerifyCertificate(time.Now(), c)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQ_CAAndSign(t *testing.T) {
|
||||||
|
for _, curve := range []string{"25519", "P256"} {
|
||||||
|
t.Run(curve, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
ob, eb := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
nopw := &StubPasswordReader{}
|
||||||
|
p := func(n string) string { return filepath.Join(dir, n) }
|
||||||
|
|
||||||
|
require.NoError(t, ca([]string{"-version", "3", "-curve", curve, "-name", "pq ca", "-out-crt", p("ca.crt"), "-out-key", p("ca.key")}, ob, eb, nopw))
|
||||||
|
caCrt := readCert(t, p("ca.crt"))
|
||||||
|
assert.Equal(t, cert.Version3, caCrt.Version())
|
||||||
|
caCurve, caKey := readSigningKeyFile(t, p("ca.key"))
|
||||||
|
require.NoError(t, caCrt.VerifyPrivateKey(caCurve, caKey))
|
||||||
|
|
||||||
|
// A v3 CA signs v3 by default.
|
||||||
|
require.NoError(t, signCert([]string{"-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "host", "-networks", "10.1.0.1/16", "-out-crt", p("host.crt"), "-out-key", p("host.key")}, ob, eb, nopw))
|
||||||
|
host := readCert(t, p("host.crt"))
|
||||||
|
assert.Equal(t, cert.Version3, host.Version())
|
||||||
|
hostCurve, hostKey := readKeyFile(t, p("host.key"))
|
||||||
|
require.NoError(t, host.VerifyPrivateKey(hostCurve, hostKey))
|
||||||
|
require.NoError(t, verifiedBy(t, host, caCrt))
|
||||||
|
|
||||||
|
// And v2 as its v2 form.
|
||||||
|
require.NoError(t, signCert([]string{"-version", "2", "-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "old", "-networks", "10.1.0.2/16", "-out-crt", p("old.crt"), "-out-key", p("old.key")}, ob, eb, nopw))
|
||||||
|
old := readCert(t, p("old.crt"))
|
||||||
|
assert.Equal(t, cert.Version2, old.Version())
|
||||||
|
require.NoError(t, verifiedBy(t, old, cert.V2Form(caCrt)))
|
||||||
|
|
||||||
|
// keygen -version 3 makes keys for v3 certificates, and only those.
|
||||||
|
require.NoError(t, keygen([]string{"-version", "3", "-curve", curve, "-out-key", p("k3.key"), "-out-pub", p("k3.pub")}, ob, eb))
|
||||||
|
require.NoError(t, signCert([]string{"-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "k3", "-networks", "10.1.0.3/16", "-in-pub", p("k3.pub"), "-out-crt", p("k3.crt")}, ob, eb, nopw))
|
||||||
|
_, k3 := readKeyFile(t, p("k3.key"))
|
||||||
|
require.NoError(t, readCert(t, p("k3.crt")).VerifyPrivateKey(hostCurve, k3))
|
||||||
|
require.ErrorContains(t, signCert([]string{"-version", "2", "-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "x", "-networks", "10.1.0.4/16", "-in-pub", p("k3.pub"), "-out-crt", p("x.crt")}, ob, eb, nopw), "only version 3 certificates carry")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQ_SignErrors(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
ob, eb := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
nopw := &StubPasswordReader{}
|
||||||
|
p := func(n string) string { return filepath.Join(dir, n) }
|
||||||
|
|
||||||
|
require.NoError(t, ca([]string{"-name", "v2 ca", "-out-crt", p("ca.crt"), "-out-key", p("ca.key")}, ob, eb, nopw))
|
||||||
|
require.ErrorContains(t, signCert([]string{"-version", "3", "-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "h", "-networks", "10.1.0.1/16", "-out-crt", p("h.crt"), "-out-key", p("h.key")}, ob, eb, nopw), "must be signed by a version 3 CA")
|
||||||
|
|
||||||
|
require.NoError(t, keygen([]string{"-out-key", p("k.key"), "-out-pub", p("k.pub")}, ob, eb))
|
||||||
|
require.NoError(t, ca([]string{"-version", "3", "-name", "v3 ca", "-out-crt", p("ca3.crt"), "-out-key", p("ca3.key")}, ob, eb, nopw))
|
||||||
|
require.ErrorContains(t, signCert([]string{"-ca-crt", p("ca3.crt"), "-ca-key", p("ca3.key"), "-name", "h", "-networks", "10.1.0.1/16", "-in-pub", p("k.pub"), "-out-crt", p("h3.crt")}, ob, eb, nopw), "need an in-pub from keygen -version 3")
|
||||||
|
|
||||||
|
// keygen -in-key refuses a key that already has a post-quantum key.
|
||||||
|
require.NoError(t, keygen([]string{"-version", "3", "-out-key", p("k3.key"), "-out-pub", p("k3.pub")}, ob, eb))
|
||||||
|
require.ErrorContains(t, keygen([]string{"-in-key", p("k3.key"), "-out-key", p("k33.key"), "-out-pub", p("k33.pub")}, ob, eb), "already have a post-quantum key")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQ_SuiteFlag(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
ob, eb := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
nopw := &StubPasswordReader{}
|
||||||
|
p := func(n string) string { return filepath.Join(dir, n) }
|
||||||
|
|
||||||
|
// The suite can be named, case aside, and the certificates a CA signs share its suite.
|
||||||
|
require.NoError(t, ca([]string{"-version", "3", "-pq-suite", "mlkem1024_mldsa87", "-name", "ca", "-out-crt", p("ca.crt"), "-out-key", p("ca.key")}, ob, eb, nopw))
|
||||||
|
assert.Equal(t, cert.PQSuite_MLKEM1024_MLDSA87, cert.PQSuiteOf(readCert(t, p("ca.crt"))))
|
||||||
|
require.NoError(t, keygen([]string{"-version", "3", "-pq-suite", "MLKEM1024_MLDSA87", "-out-key", p("k.key"), "-out-pub", p("k.pub")}, ob, eb))
|
||||||
|
require.NoError(t, signCert([]string{"-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "h", "-networks", "10.1.0.1/16", "-in-pub", p("k.pub"), "-out-crt", p("h.crt")}, ob, eb, nopw))
|
||||||
|
assert.Equal(t, cert.PQSuite_MLKEM1024_MLDSA87, cert.PQSuiteOf(readCert(t, p("h.crt"))))
|
||||||
|
|
||||||
|
// Printing shows it.
|
||||||
|
ob.Reset()
|
||||||
|
require.NoError(t, printCert([]string{"-json", "-path", p("h.crt")}, ob, eb))
|
||||||
|
assert.Contains(t, ob.String(), `"pqSuite":"MLKEM1024_MLDSA87"`)
|
||||||
|
|
||||||
|
// Unknown suites, and suites where there is no post-quantum key, are refused.
|
||||||
|
require.ErrorContains(t, ca([]string{"-version", "3", "-pq-suite", "mlkem768_mldsa65", "-name", "x", "-out-crt", p("x.crt"), "-out-key", p("x.key")}, ob, eb, nopw), "unknown post-quantum suite")
|
||||||
|
require.ErrorContains(t, ca([]string{"-pq-suite", "MLKEM1024_MLDSA87", "-name", "x", "-out-crt", p("x.crt"), "-out-key", p("x.key")}, ob, eb, nopw), "only for -version 3")
|
||||||
|
require.ErrorContains(t, keygen([]string{"-pq-suite", "MLKEM1024_MLDSA87", "-out-key", p("x.key"), "-out-pub", p("x.pub")}, ob, eb), "only for -version 3")
|
||||||
|
|
||||||
|
// A host takes its CA's suite when extended.
|
||||||
|
require.NoError(t, ca([]string{"-name", "ca2", "-out-crt", p("ca2.crt"), "-out-key", p("ca2.key")}, ob, eb, nopw))
|
||||||
|
require.NoError(t, signCert([]string{"-ca-crt", p("ca2.crt"), "-ca-key", p("ca2.key"), "-name", "h2", "-networks", "10.1.0.2/16", "-out-crt", p("h2.crt"), "-out-key", p("h2.key")}, ob, eb, nopw))
|
||||||
|
require.NoError(t, pqExtend([]string{"-in-crt", p("ca2.crt"), "-ca-key", p("ca2.key"), "-pq-suite", "MLKEM1024_MLDSA87", "-out-crt", p("ca23.crt"), "-out-key", p("ca23.key")}, ob, eb, nopw))
|
||||||
|
require.NoError(t, keygen([]string{"-in-key", p("h2.key"), "-out-key", p("h23.key"), "-out-pub", p("h23.pub")}, ob, eb))
|
||||||
|
require.ErrorContains(t, pqExtend([]string{"-in-crt", p("h2.crt"), "-in-pub", p("h23.pub"), "-ca-crt", p("ca23.crt"), "-ca-key", p("ca23.key"), "-pq-suite", "MLKEM1024_MLDSA87", "-out-crt", p("h23.crt")}, ob, eb, nopw), "a host takes its CA's suite")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQ_Extend upgrades a v2 CA and a host in place, the way a running network migrates.
|
||||||
|
func TestPQ_Extend(t *testing.T) {
|
||||||
|
for _, encrypt := range []bool{false, true} {
|
||||||
|
name := "plain"
|
||||||
|
if encrypt {
|
||||||
|
name = "encrypted"
|
||||||
|
}
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
ob, eb := &bytes.Buffer{}, &bytes.Buffer{}
|
||||||
|
pw := &StubPasswordReader{}
|
||||||
|
if encrypt {
|
||||||
|
t.Setenv("NEBULA_CA_PASSPHRASE", "hunter2")
|
||||||
|
}
|
||||||
|
p := func(n string) string { return filepath.Join(dir, n) }
|
||||||
|
|
||||||
|
caArgs := []string{"-name", "ca", "-out-crt", p("ca.crt"), "-out-key", p("ca.key"), "-argon-memory", "1024"}
|
||||||
|
if encrypt {
|
||||||
|
caArgs = append(caArgs, "-encrypt")
|
||||||
|
}
|
||||||
|
require.NoError(t, ca(caArgs, ob, eb, pw))
|
||||||
|
require.NoError(t, signCert([]string{"-ca-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-name", "host", "-networks", "10.1.0.1/16", "-out-crt", p("host.crt"), "-out-key", p("host.key")}, ob, eb, pw))
|
||||||
|
ca2 := readCert(t, p("ca.crt"))
|
||||||
|
host2 := readCert(t, p("host.crt"))
|
||||||
|
|
||||||
|
// The CA extends itself.
|
||||||
|
require.NoError(t, pqExtend([]string{"-in-crt", p("ca.crt"), "-ca-key", p("ca.key"), "-out-crt", p("ca3.crt"), "-out-key", p("ca3.key")}, ob, eb, pw))
|
||||||
|
ca3 := readCert(t, p("ca3.crt"))
|
||||||
|
assert.Equal(t, cert.Version3, ca3.Version())
|
||||||
|
assertSameCert(t, ca2, cert.V2Form(ca3))
|
||||||
|
raw, err := os.ReadFile(p("ca3.key"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
curve, ca3Key, _, err := readSigningKey(raw, eb, pw)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, ca3.VerifyPrivateKey(curve, ca3Key))
|
||||||
|
if encrypt {
|
||||||
|
_, _, _, err = cert.UnmarshalSigningPrivateKeyFromPEM(raw)
|
||||||
|
require.ErrorIs(t, err, cert.ErrPrivateKeyEncrypted, "an encrypted ca-key stays encrypted")
|
||||||
|
}
|
||||||
|
|
||||||
|
// The host gets a post-quantum key on top of the one it has, and the CA signs it.
|
||||||
|
require.NoError(t, keygen([]string{"-in-key", p("host.key"), "-out-key", p("host3.key"), "-out-pub", p("host3.pub")}, ob, eb))
|
||||||
|
require.NoError(t, pqExtend([]string{"-in-crt", p("host.crt"), "-in-pub", p("host3.pub"), "-ca-crt", p("ca3.crt"), "-ca-key", p("ca3.key"), "-out-crt", p("host3.crt")}, ob, eb, pw))
|
||||||
|
host3 := readCert(t, p("host3.crt"))
|
||||||
|
assert.Equal(t, cert.Version3, host3.Version())
|
||||||
|
assertSameCert(t, host2, cert.V2Form(host3))
|
||||||
|
hostCurve, host3Key := readKeyFile(t, p("host3.key"))
|
||||||
|
require.NoError(t, host3.VerifyPrivateKey(hostCurve, host3Key))
|
||||||
|
require.NoError(t, verifiedBy(t, host3, ca3))
|
||||||
|
require.NoError(t, verifiedBy(t, host2, ca2), "existing trust is untouched")
|
||||||
|
|
||||||
|
// print leads a version 3 certificate with both fingerprints and says which one blocks both forms.
|
||||||
|
ob.Reset()
|
||||||
|
require.NoError(t, printCert([]string{"-path", p("host3.crt")}, ob, eb))
|
||||||
|
fp3, err := host3.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
fp2, err := host2.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
printed := ob.String()
|
||||||
|
lead := "Fingerprint: " + fp3 + "\nv2 fingerprint: " + fp2 + " (block this one in pki.blocklist, it covers both forms)\n"
|
||||||
|
require.Greater(t, len(printed), len(lead))
|
||||||
|
assert.Equal(t, lead, printed[:len(lead)])
|
||||||
|
|
||||||
|
// Someone else's key does not extend this host.
|
||||||
|
require.NoError(t, keygen([]string{"-version", "3", "-out-key", p("other.key"), "-out-pub", p("other.pub")}, ob, eb))
|
||||||
|
require.ErrorContains(t, pqExtend([]string{"-in-crt", p("host.crt"), "-in-pub", p("other.pub"), "-ca-crt", p("ca3.crt"), "-ca-key", p("ca3.key"), "-out-crt", p("bad.crt")}, ob, eb, pw), "does not extend the key")
|
||||||
|
|
||||||
|
// Nor does the v2 CA key, or a v3 certificate.
|
||||||
|
require.ErrorContains(t, pqExtend([]string{"-in-crt", p("host.crt"), "-in-pub", p("host3.pub"), "-ca-crt", p("ca3.crt"), "-ca-key", p("ca.key"), "-out-crt", p("bad.crt")}, ob, eb, pw), "does not match ca-key")
|
||||||
|
require.ErrorContains(t, pqExtend([]string{"-in-crt", p("host3.crt"), "-in-pub", p("host3.pub"), "-ca-crt", p("ca3.crt"), "-ca-key", p("ca3.key"), "-out-crt", p("bad.crt")}, ob, eb, pw), "only version 2 certificates")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func assertSameCert(t *testing.T, want, got cert.Certificate) {
|
||||||
|
t.Helper()
|
||||||
|
wb, err := want.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
gb, err := got.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, wb, gb)
|
||||||
|
}
|
||||||
@@ -73,6 +73,19 @@ func printCert(args []string, out io.Writer, errOut io.Writer) error {
|
|||||||
if *pf.json {
|
if *pf.json {
|
||||||
jsonCerts = append(jsonCerts, c)
|
jsonCerts = append(jsonCerts, c)
|
||||||
} else {
|
} else {
|
||||||
|
// A version 3 certificate has two fingerprints, and only the v2 one blocks both of its forms.
|
||||||
|
// Lead with them so the right one is the obvious one to copy.
|
||||||
|
if v2 := cert.V2Form(c); v2 != nil {
|
||||||
|
fp, err := c.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while computing fingerprint: %s", err)
|
||||||
|
}
|
||||||
|
v2fp, err := v2.Fingerprint()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while computing v2 fingerprint: %s", err)
|
||||||
|
}
|
||||||
|
_, _ = fmt.Fprintf(out, "Fingerprint: %s\nv2 fingerprint: %s (block this one in pki.blocklist, it covers both forms)\n", fp, v2fp)
|
||||||
|
}
|
||||||
_, _ = out.Write([]byte(c.String()))
|
_, _ = out.Write([]byte(c.String()))
|
||||||
_, _ = out.Write([]byte("\n"))
|
_, _ = out.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
|||||||
+105
-35
@@ -44,7 +44,7 @@ type signFlags struct {
|
|||||||
func newSignFlags() *signFlags {
|
func newSignFlags() *signFlags {
|
||||||
sf := signFlags{set: flag.NewFlagSet("sign", flag.ContinueOnError)}
|
sf := signFlags{set: flag.NewFlagSet("sign", flag.ContinueOnError)}
|
||||||
sf.set.Usage = func() {}
|
sf.set.Usage = func() {}
|
||||||
sf.version = sf.set.Uint("version", 0, "Optional: version of the certificate format to use. The default is to match the version of the signing CA")
|
sf.version = sf.set.Uint("version", 0, "Optional: version of the certificate format to use. The default is to match the version of the signing CA. A version 3 CA also signs version 2 certificates, as its v2 form")
|
||||||
sf.caKeyPath = sf.set.String("ca-key", "ca.key", "Optional: path to the signing CA key")
|
sf.caKeyPath = sf.set.String("ca-key", "ca.key", "Optional: path to the signing CA key")
|
||||||
sf.caCertPath = sf.set.String("ca-crt", "ca.crt", "Optional: path to the signing CA cert")
|
sf.caCertPath = sf.set.String("ca-crt", "ca.crt", "Optional: path to the signing CA cert")
|
||||||
sf.name = sf.set.String("name", "", "Required: name of the cert, usually a hostname")
|
sf.name = sf.set.String("name", "", "Required: name of the cert, usually a hostname")
|
||||||
@@ -102,8 +102,8 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
}
|
}
|
||||||
|
|
||||||
version := cert.Version(*sf.version)
|
version := cert.Version(*sf.version)
|
||||||
if version != 0 && version != cert.Version1 && version != cert.Version2 {
|
if version != 0 && version != cert.Version1 && version != cert.Version2 && version != cert.Version3 {
|
||||||
return newHelpErrorf("-version must be either %v or %v", cert.Version1, cert.Version2)
|
return newHelpErrorf("-version must be %v, %v or %v", cert.Version1, cert.Version2, cert.Version3)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *sf.outKeyPath == "" {
|
if *sf.outKeyPath == "" {
|
||||||
@@ -139,37 +139,9 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
return fmt.Errorf("error while reading ca-key: %s", err)
|
return fmt.Errorf("error while reading ca-key: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// naively attempt to decode the private key as though it is not encrypted
|
curve, caKey, _, err = readSigningKey(rawCAKey, errOut, pr)
|
||||||
caKey, _, curve, err = cert.UnmarshalSigningPrivateKeyFromPEM(rawCAKey)
|
if err != nil {
|
||||||
if errors.Is(err, cert.ErrPrivateKeyEncrypted) {
|
return err
|
||||||
var passphrase []byte
|
|
||||||
passphrase = []byte(os.Getenv("NEBULA_CA_PASSPHRASE"))
|
|
||||||
if len(passphrase) == 0 {
|
|
||||||
// ask for a passphrase until we get one
|
|
||||||
for range 5 {
|
|
||||||
errOut.Write([]byte("Enter passphrase: "))
|
|
||||||
passphrase, err = pr.ReadPassword()
|
|
||||||
|
|
||||||
if errors.Is(err, ErrNoTerminal) {
|
|
||||||
return fmt.Errorf("ca-key is encrypted and must be decrypted interactively")
|
|
||||||
} else if err != nil {
|
|
||||||
return fmt.Errorf("error reading password: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(passphrase) > 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(passphrase) == 0 {
|
|
||||||
return fmt.Errorf("cannot open encrypted ca-key without passphrase")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
curve, caKey, _, err = cert.DecryptAndUnmarshalSigningPrivateKey(passphrase, rawCAKey)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error while parsing encrypted ca-key: %s", err)
|
|
||||||
}
|
|
||||||
} else if err != nil {
|
|
||||||
return fmt.Errorf("error while parsing ca-key: %s", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +169,23 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
version = caCert.Version()
|
version = caCert.Version()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A version 3 certificate is in the suite of its CA.
|
||||||
|
suite := cert.PQSuiteOf(caCert)
|
||||||
|
switch {
|
||||||
|
case version == cert.Version3 && caCert.Version() != cert.Version3:
|
||||||
|
return newHelpErrorf("version 3 certificates must be signed by a version 3 CA")
|
||||||
|
case version == cert.Version3 && isP11:
|
||||||
|
return newHelpErrorf("version 3 certificates can not be signed with -pkcs11, the post-quantum key has to be in ca-key")
|
||||||
|
case version != cert.Version3 && caCert.Version() == cert.Version3:
|
||||||
|
// A v3 CA signs anything older as its v2 form, with the classical half of its key.
|
||||||
|
caCert = cert.V2Form(caCert)
|
||||||
|
if caKey != nil {
|
||||||
|
if caKey, _, err = suite.SplitPrivateKey(true, caKey); err != nil {
|
||||||
|
return fmt.Errorf("error while parsing ca-key: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// if no duration is given, expire one second before the root expires
|
// if no duration is given, expire one second before the root expires
|
||||||
if *sf.duration <= 0 {
|
if *sf.duration <= 0 {
|
||||||
*sf.duration = time.Until(caCert.NotAfter()) - time.Second*1
|
*sf.duration = time.Until(caCert.NotAfter()) - time.Second*1
|
||||||
@@ -287,6 +276,16 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
if pubCurve != curve {
|
if pubCurve != curve {
|
||||||
return fmt.Errorf("curve of in-pub does not match ca")
|
return fmt.Errorf("curve of in-pub does not match ca")
|
||||||
}
|
}
|
||||||
|
// Composite keys are longer than any classical one, an uncompressed P256 key being the longest.
|
||||||
|
if isPQ := len(pub) > 65; isPQ != (version == cert.Version3) {
|
||||||
|
if isPQ {
|
||||||
|
return fmt.Errorf("in-pub has a post-quantum key, which only version 3 certificates carry")
|
||||||
|
}
|
||||||
|
return fmt.Errorf("version 3 certificates need an in-pub from keygen -version 3")
|
||||||
|
}
|
||||||
|
if _, _, err := suite.SplitPublicKey(false, pub); version == cert.Version3 && err != nil {
|
||||||
|
return fmt.Errorf("in-pub is not a key for the CA's post-quantum suite, %v", suite)
|
||||||
|
}
|
||||||
} else if isP11 {
|
} else if isP11 {
|
||||||
pub, err = p11Client.GetPubKey()
|
pub, err = p11Client.GetPubKey()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -294,6 +293,12 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pub, rawPriv = newKeypair(curve)
|
pub, rawPriv = newKeypair(curve)
|
||||||
|
if version == cert.Version3 {
|
||||||
|
pub, rawPriv, err = appendPQKey(suite, pub, rawPriv)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isStdio(*sf.outCertPath) {
|
if !isStdio(*sf.outCertPath) {
|
||||||
@@ -378,6 +383,28 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
}
|
}
|
||||||
|
|
||||||
crts = append(crts, nc)
|
crts = append(crts, nc)
|
||||||
|
|
||||||
|
case cert.Version3:
|
||||||
|
t := &cert.TBSCertificate{
|
||||||
|
Version: cert.Version3,
|
||||||
|
Name: *sf.name,
|
||||||
|
Networks: append(v4Networks, v6Networks...),
|
||||||
|
Groups: groups,
|
||||||
|
UnsafeNetworks: append(v4UnsafeNetworks, v6UnsafeNetworks...),
|
||||||
|
NotBefore: notBefore,
|
||||||
|
NotAfter: notAfter,
|
||||||
|
PublicKey: pub,
|
||||||
|
IsCA: false,
|
||||||
|
Curve: curve,
|
||||||
|
PQSuite: suite,
|
||||||
|
}
|
||||||
|
|
||||||
|
nc, err := t.Sign(caCert, curve, caKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error while signing: %w", err)
|
||||||
|
}
|
||||||
|
crts = append(crts, nc)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// this should be unreachable
|
// this should be unreachable
|
||||||
return fmt.Errorf("invalid version: %d", version)
|
return fmt.Errorf("invalid version: %d", version)
|
||||||
@@ -390,7 +417,11 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = writeOutput(*sf.outKeyPath, cert.MarshalPrivateKeyToPEM(curve, rawPriv), 0600, out)
|
b := cert.MarshalPrivateKeyToPEM(curve, rawPriv)
|
||||||
|
if version == cert.Version3 {
|
||||||
|
b = cert.MarshalPQPrivateKeyToPEM(curve, suite, rawPriv)
|
||||||
|
}
|
||||||
|
err = writeOutput(*sf.outKeyPath, b, 0600, out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error while writing out-key: %s", err)
|
return fmt.Errorf("error while writing out-key: %s", err)
|
||||||
}
|
}
|
||||||
@@ -470,3 +501,42 @@ func signHelp(out io.Writer) {
|
|||||||
sf.set.SetOutput(out)
|
sf.set.SetOutput(out)
|
||||||
sf.set.PrintDefaults()
|
sf.set.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// readSigningKey decodes a CA key, asking for its passphrase if it is encrypted.
|
||||||
|
// The passphrase is returned too, nil for a key that was not encrypted.
|
||||||
|
func readSigningKey(rawCAKey []byte, errOut io.Writer, pr PasswordReader) (cert.Curve, []byte, []byte, error) {
|
||||||
|
// naively attempt to decode the private key as though it is not encrypted
|
||||||
|
caKey, _, curve, err := cert.UnmarshalSigningPrivateKeyFromPEM(rawCAKey)
|
||||||
|
if errors.Is(err, cert.ErrPrivateKeyEncrypted) {
|
||||||
|
var passphrase []byte
|
||||||
|
passphrase = []byte(os.Getenv("NEBULA_CA_PASSPHRASE"))
|
||||||
|
if len(passphrase) == 0 {
|
||||||
|
// ask for a passphrase until we get one
|
||||||
|
for range 5 {
|
||||||
|
errOut.Write([]byte("Enter passphrase: "))
|
||||||
|
passphrase, err = pr.ReadPassword()
|
||||||
|
|
||||||
|
if errors.Is(err, ErrNoTerminal) {
|
||||||
|
return 0, nil, nil, fmt.Errorf("ca-key is encrypted and must be decrypted interactively")
|
||||||
|
} else if err != nil {
|
||||||
|
return 0, nil, nil, fmt.Errorf("error reading password: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(passphrase) > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(passphrase) == 0 {
|
||||||
|
return 0, nil, nil, fmt.Errorf("cannot open encrypted ca-key without passphrase")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
curve, caKey, _, err = cert.DecryptAndUnmarshalSigningPrivateKey(passphrase, rawCAKey)
|
||||||
|
if err != nil {
|
||||||
|
return 0, nil, nil, fmt.Errorf("error while parsing encrypted ca-key: %s", err)
|
||||||
|
}
|
||||||
|
return curve, caKey, passphrase, nil
|
||||||
|
} else if err != nil {
|
||||||
|
return 0, nil, nil, fmt.Errorf("error while parsing ca-key: %s", err)
|
||||||
|
}
|
||||||
|
return curve, caKey, nil, nil
|
||||||
|
}
|
||||||
@@ -55,7 +55,7 @@ func Test_signHelp(t *testing.T) {
|
|||||||
" -unsafe-networks string\n"+
|
" -unsafe-networks string\n"+
|
||||||
" \tOptional: comma separated list of ip address and network in CIDR notation. Unsafe networks this cert can route for\n"+
|
" \tOptional: comma separated list of ip address and network in CIDR notation. Unsafe networks this cert can route for\n"+
|
||||||
" -version uint\n"+
|
" -version uint\n"+
|
||||||
" \tOptional: version of the certificate format to use. The default is to match the version of the signing CA\n",
|
" \tOptional: version of the certificate format to use. The default is to match the version of the signing CA. A version 3 CA also signs version 2 certificates, as its v2 form\n",
|
||||||
ob.String(),
|
ob.String(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,10 +85,9 @@ func main() {
|
|||||||
fmt.Printf("failed to apply logging config: %s", err)
|
fmt.Printf("failed to apply logging config: %s", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
if err := logging.ApplyConfig(l, c); err != nil {
|
if err := logging.ApplyConfig(l, c); err != nil {
|
||||||
l.Error("Failed to reconfigure logger on reload", "error", err)
|
l.Error("Failed to reconfigure logger on reload", "error", err)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -34,10 +34,9 @@ func (p *program) Start(s service.Service) error {
|
|||||||
if err := logging.ApplyConfig(l, c); err != nil {
|
if err := logging.ApplyConfig(l, c); err != nil {
|
||||||
return fmt.Errorf("failed to apply logging config: %s", err)
|
return fmt.Errorf("failed to apply logging config: %s", err)
|
||||||
}
|
}
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
if err := logging.ApplyConfig(l, c); err != nil {
|
if err := logging.ApplyConfig(l, c); err != nil {
|
||||||
l.Error("Failed to reconfigure logger on reload", "error", err)
|
l.Error("Failed to reconfigure logger on reload", "error", err)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -71,10 +71,9 @@ func main() {
|
|||||||
fmt.Printf("failed to apply logging config: %s", err)
|
fmt.Printf("failed to apply logging config: %s", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
if err := logging.ApplyConfig(l, c); err != nil {
|
if err := logging.ApplyConfig(l, c); err != nil {
|
||||||
l.Error("Failed to reconfigure logger on reload", "error", err)
|
l.Error("Failed to reconfigure logger on reload", "error", err)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
-710
@@ -1,710 +0,0 @@
|
|||||||
package nebula
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"maps"
|
|
||||||
"net/netip"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/commands"
|
|
||||||
"github.com/slackhq/nebula/config"
|
|
||||||
"github.com/slackhq/nebula/logging"
|
|
||||||
)
|
|
||||||
|
|
||||||
// The commands nebula serves. attachCommands builds the one registry every surface
|
|
||||||
// reads. See the commands package for what a Command is.
|
|
||||||
|
|
||||||
type listHostMapFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
ByIndex bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// listHostMapFlagSet is the flag set list-hostmap and list-pending-hostmap share.
|
|
||||||
func listHostMapFlagSet() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := listHostMapFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
|
||||||
return fl, &s
|
|
||||||
}
|
|
||||||
|
|
||||||
type printCertFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
Raw bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type printTunnelFlags struct {
|
|
||||||
Pretty bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type changeRemoteFlags struct {
|
|
||||||
Address string
|
|
||||||
}
|
|
||||||
|
|
||||||
type closeTunnelFlags struct {
|
|
||||||
LocalOnly bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type createTunnelFlags struct {
|
|
||||||
Address string
|
|
||||||
}
|
|
||||||
|
|
||||||
type deviceInfoFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func attachCommands(l *slog.Logger, c *config.C, reg *commands.Registry, f *Interface) {
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.ListHostmap,
|
|
||||||
ShortDescription: "List all known previously connected hosts",
|
|
||||||
Flags: listHostMapFlagSet,
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return listHostMapCommand(f.hostMap, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.ListPendingHostmap,
|
|
||||||
ShortDescription: "List all handshaking hosts",
|
|
||||||
Flags: listHostMapFlagSet,
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return listHostMapCommand(f.handshakeManager, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.ListLighthouseAddrmap,
|
|
||||||
ShortDescription: "List all lighthouse map entries",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := listHostMapFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return listLighthouseAddrmapCommand(f.lightHouse, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.Reload,
|
|
||||||
ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return reloadCommand(l, c, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.LogLevel,
|
|
||||||
ShortDescription: "Gets or sets the current log level",
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return logLevelCommand(l, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.LogFormat,
|
|
||||||
ShortDescription: "Gets or sets the current log format",
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return logFormatCommand(l, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.Version,
|
|
||||||
ShortDescription: "Prints the currently running version of nebula",
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return versionCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.DeviceInfo,
|
|
||||||
ShortDescription: "Prints information about the network device.",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := deviceInfoFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return deviceInfoCommand(f, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.PrintCert,
|
|
||||||
ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := printCertFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return printCertCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.PrintTunnel,
|
|
||||||
ShortDescription: "Prints json details about a tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := printTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return printTunnelCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.PrintRelays,
|
|
||||||
ShortDescription: "Prints json details about all relay info",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := printTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return printRelaysCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.ChangeRemote,
|
|
||||||
ShortDescription: "Changes the remote address used in the tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := changeRemoteFlags{}
|
|
||||||
fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return changeRemoteCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.CloseTunnel,
|
|
||||||
ShortDescription: "Closes a tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := closeTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return closeTunnelCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.CreateTunnel,
|
|
||||||
ShortDescription: "Creates a tunnel for the provided vpn address",
|
|
||||||
Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := createTunnelFlags{}
|
|
||||||
fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return createTunnelCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
reg.Register(&commands.Command{
|
|
||||||
Name: commands.QueryLighthouse,
|
|
||||||
ShortDescription: "Query the lighthouses for the provided vpn address",
|
|
||||||
Help: "This command is asynchronous. Only currently known udp addresses will be printed.",
|
|
||||||
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return queryLighthouseCommand(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func listHostMapCommand(hl controlHostLister, a any, w commands.StringWriter) error {
|
|
||||||
fs, ok := a.(*listHostMapFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *listHostMapFlags but was %T", a)
|
|
||||||
}
|
|
||||||
|
|
||||||
var hm []ControlHostInfo
|
|
||||||
if fs.ByIndex {
|
|
||||||
hm = listHostMapIndexes(hl)
|
|
||||||
} else {
|
|
||||||
hm = listHostMapHosts(hl)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(hm, func(i, j int) bool {
|
|
||||||
return hm[i].VpnAddrs[0].Compare(hm[j].VpnAddrs[0]) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if fs.Json || fs.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if fs.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := js.Encode(hm); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for _, v := range hm {
|
|
||||||
err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddrs, v.RemoteAddrs))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func listLighthouseAddrmapCommand(lightHouse *LightHouse, a any, w commands.StringWriter) error {
|
|
||||||
fs, ok := a.(*listHostMapFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *listHostMapFlags but was %T", a)
|
|
||||||
}
|
|
||||||
|
|
||||||
type lighthouseInfo struct {
|
|
||||||
VpnAddr string `json:"vpnAddr"`
|
|
||||||
Addrs *CacheMap `json:"addrs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
lightHouse.RLock()
|
|
||||||
addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
|
|
||||||
x := 0
|
|
||||||
for k, v := range lightHouse.addrMap {
|
|
||||||
addrMap[x] = lighthouseInfo{
|
|
||||||
VpnAddr: k.String(),
|
|
||||||
Addrs: v.CopyCache(),
|
|
||||||
}
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
lightHouse.RUnlock()
|
|
||||||
|
|
||||||
sort.Slice(addrMap, func(i, j int) bool {
|
|
||||||
return strings.Compare(addrMap[i].VpnAddr, addrMap[j].VpnAddr) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if fs.Json || fs.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if fs.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := js.Encode(addrMap); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for _, v := range addrMap {
|
|
||||||
b, err := json.Marshal(v.Addrs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddr, string(b)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseVpnAddr parses the vpn address a command takes as its first operand, failing to w
|
|
||||||
// when there is none or it does not parse. noun is how the command's messages name it,
|
|
||||||
// "vpn address" or "vpn addr", which the output golden pins per command.
|
|
||||||
func parseVpnAddr(a []string, noun string, w commands.StringWriter) (netip.Addr, error) {
|
|
||||||
if len(a) == 0 {
|
|
||||||
return netip.Addr{}, commands.Fail(w, "No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return netip.Addr{}, commands.Fail(w, fmt.Sprintf("The provided %s could not be parsed: %s", noun, a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
return vpnAddr, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func versionCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
return w.WriteLine(ifce.version)
|
|
||||||
}
|
|
||||||
|
|
||||||
func queryLighthouseCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var cm *CacheMap
|
|
||||||
rl := ifce.lightHouse.Query(vpnAddr)
|
|
||||||
if rl != nil {
|
|
||||||
cm = rl.CopyCache()
|
|
||||||
}
|
|
||||||
return json.NewEncoder(w.GetWriter()).Encode(cm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func closeTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
flags, ok := fs.(*closeTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *closeTunnelFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !flags.LocalOnly {
|
|
||||||
ifce.sendCloseTunnel(hostInfo)
|
|
||||||
}
|
|
||||||
|
|
||||||
ifce.closeTunnel(hostInfo)
|
|
||||||
return w.WriteLine("Closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
func createTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
flags, ok := fs.(*createTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *createTunnelFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo != nil {
|
|
||||||
return commands.Fail(w, "Tunnel already exists")
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo = ifce.handshakeManager.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo != nil {
|
|
||||||
return commands.Fail(w, "Tunnel already handshaking")
|
|
||||||
}
|
|
||||||
|
|
||||||
var addr netip.AddrPort
|
|
||||||
if flags.Address != "" {
|
|
||||||
addr, err = netip.ParseAddrPort(flags.Address)
|
|
||||||
if err != nil {
|
|
||||||
return commands.Fail(w, "Address could not be parsed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the remote from the callback, which StartHandshake runs under the handshake manager lock
|
|
||||||
// before the hostinfo reaches the handshake loop or the static-host trigger, so the address is
|
|
||||||
// in the RemoteList before the first handshake is sent.
|
|
||||||
ifce.handshakeManager.StartHandshake(vpnAddr, func(hh *HandshakeHostInfo) {
|
|
||||||
if addr.IsValid() {
|
|
||||||
hh.hostinfo.SetRemote(addr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return w.WriteLine("Created")
|
|
||||||
}
|
|
||||||
|
|
||||||
func changeRemoteCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
flags, ok := fs.(*changeRemoteFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *changeRemoteFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if flags.Address == "" {
|
|
||||||
return commands.Fail(w, "No address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := netip.ParseAddrPort(flags.Address)
|
|
||||||
if err != nil {
|
|
||||||
return commands.Fail(w, "Address could not be parsed")
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo.SetRemote(addr)
|
|
||||||
return w.WriteLine("Changed")
|
|
||||||
}
|
|
||||||
|
|
||||||
func logLevelCommand(l *slog.Logger, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
ctrl, ok := l.Handler().(interface {
|
|
||||||
GetLevel() slog.Level
|
|
||||||
SetLevel(slog.Level)
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return commands.Fail(w, "Log level is not reconfigurable on this logger")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
|
||||||
}
|
|
||||||
|
|
||||||
level, err := logging.ParseLevel(strings.ToLower(a[0]))
|
|
||||||
if err != nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Unknown log level %s. Possible log levels: trace, debug, info, warn, error", a))
|
|
||||||
}
|
|
||||||
|
|
||||||
ctrl.SetLevel(level)
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
|
||||||
}
|
|
||||||
|
|
||||||
func logFormatCommand(l *slog.Logger, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
ctrl, ok := l.Handler().(interface {
|
|
||||||
GetFormat() string
|
|
||||||
SetFormat(string) error
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return commands.Fail(w, "Log format is not reconfigurable on this logger")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ctrl.SetFormat(strings.ToLower(a[0])); err != nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Unable to set log format: %s", err))
|
|
||||||
}
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func printCertCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
args, ok := fs.(*printCertFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *printCertFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
cert := ifce.pki.getCertState().GetDefaultCertificate()
|
|
||||||
if len(a) > 0 {
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn addr", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
cert = hostInfo.GetCert().Certificate
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Json || args.Pretty {
|
|
||||||
b, err := cert.MarshalJSON()
|
|
||||||
if err != nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Pretty {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
if err := json.Indent(buf, b, "", " "); err != nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
|
||||||
}
|
|
||||||
b = buf.Bytes()
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteBytes(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Raw {
|
|
||||||
b, err := cert.MarshalPEM()
|
|
||||||
if err != nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteBytes(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteLine(cert.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func printRelaysCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
args, ok := fs.(*printTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *printTunnelFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
ifce.hostMap.RLock()
|
|
||||||
relays := maps.Clone(ifce.hostMap.Relays)
|
|
||||||
ifce.hostMap.RUnlock()
|
|
||||||
|
|
||||||
type RelayFor struct {
|
|
||||||
Error string `json:",omitempty"`
|
|
||||||
Type string
|
|
||||||
State string
|
|
||||||
PeerAddr netip.Addr
|
|
||||||
LocalIndex uint32
|
|
||||||
RemoteIndex uint32
|
|
||||||
RelayedThrough []netip.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
type RelayOutput struct {
|
|
||||||
NebulaAddr netip.Addr
|
|
||||||
RelayForAddrs []RelayFor
|
|
||||||
}
|
|
||||||
|
|
||||||
type CmdOutput struct {
|
|
||||||
Relays []*RelayOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
co := CmdOutput{}
|
|
||||||
|
|
||||||
enc := json.NewEncoder(w.GetWriter())
|
|
||||||
|
|
||||||
if args.Pretty {
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range relays {
|
|
||||||
ro := RelayOutput{NebulaAddr: v.vpnAddrs[0]}
|
|
||||||
co.Relays = append(co.Relays, &ro)
|
|
||||||
relayHI := ifce.hostMap.QueryVpnAddr(v.vpnAddrs[0])
|
|
||||||
if relayHI == nil {
|
|
||||||
ro.RelayForAddrs = append(ro.RelayForAddrs, RelayFor{Error: "could not find hostinfo"})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, vpnAddr := range relayHI.relayState.CopyRelayForIps() {
|
|
||||||
rf := RelayFor{}
|
|
||||||
r, ok := relayHI.relayState.GetRelayForByAddr(vpnAddr)
|
|
||||||
if ok {
|
|
||||||
t := ""
|
|
||||||
switch r.Type {
|
|
||||||
case ForwardingType:
|
|
||||||
t = "forwarding"
|
|
||||||
case TerminalType:
|
|
||||||
t = "terminal"
|
|
||||||
default:
|
|
||||||
t = "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
s := ""
|
|
||||||
switch r.State {
|
|
||||||
case Requested:
|
|
||||||
s = "requested"
|
|
||||||
case Established:
|
|
||||||
s = "established"
|
|
||||||
default:
|
|
||||||
s = "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
rf.LocalIndex = r.LocalIndex
|
|
||||||
rf.RemoteIndex = r.RemoteIndex
|
|
||||||
rf.PeerAddr = r.PeerAddr
|
|
||||||
rf.Type = t
|
|
||||||
rf.State = s
|
|
||||||
if rf.LocalIndex != k {
|
|
||||||
rf.Error = fmt.Sprintf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
relayedHI := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if relayedHI != nil {
|
|
||||||
rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
|
|
||||||
}
|
|
||||||
|
|
||||||
ro.RelayForAddrs = append(ro.RelayForAddrs, rf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return enc.Encode(co)
|
|
||||||
}
|
|
||||||
|
|
||||||
func printTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
|
||||||
args, ok := fs.(*printTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected *printTunnelFlags but was %T", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := parseVpnAddr(a, "vpn addr", w)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
enc := json.NewEncoder(w.GetWriter())
|
|
||||||
if args.Pretty {
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func deviceInfoCommand(ifce *Interface, fs any, w commands.StringWriter) error {
|
|
||||||
flags, ok := fs.(*deviceInfoFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected flags to be deviceInfoFlags but was %+v", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
data := struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Cidr []netip.Prefix `json:"cidr"`
|
|
||||||
}{
|
|
||||||
Name: ifce.inside.Name(),
|
|
||||||
Cidr: make([]netip.Prefix, len(ifce.inside.Networks())),
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(data.Cidr, ifce.inside.Networks())
|
|
||||||
|
|
||||||
if flags.Json || flags.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if flags.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
return js.Encode(data)
|
|
||||||
} else {
|
|
||||||
return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// reloadCommand reloads the config and writes the result to the client. It reloads even if the client has gone away
|
|
||||||
func reloadCommand(l *slog.Logger, c *config.C, w commands.StringWriter) error {
|
|
||||||
writeErr := w.WriteLine("Reloading config")
|
|
||||||
res := c.ReloadConfig()
|
|
||||||
res.Log(l)
|
|
||||||
if err := w.WriteLine(res.String()); writeErr == nil {
|
|
||||||
writeErr = err
|
|
||||||
}
|
|
||||||
if writeErr != nil {
|
|
||||||
return writeErr
|
|
||||||
}
|
|
||||||
if res.Err != nil || res.Failed {
|
|
||||||
return &commands.Failure{Msg: res.String()}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CommandFlags is called before help or command execution to parse command line flags.
|
|
||||||
// It should return a flag.FlagSet instance and a pointer to the struct that will contain parsed flags
|
|
||||||
type CommandFlags func() (*flag.FlagSet, any)
|
|
||||||
|
|
||||||
// CommandCallback is called when your command should execute.
|
|
||||||
// fs will be a pointer to the struct provided by Command.Flags callback, if there was one. -h and -help are reserved
|
|
||||||
// and handled automatically for you.
|
|
||||||
// a will be any unconsumed arguments, if no Command.Flags was available this will be all the flags passed in.
|
|
||||||
// w is the writer to use when sending messages back to the client.
|
|
||||||
// An error returned by the callback is handed back to whoever dispatched the command, which reports a failure: exit
|
|
||||||
// status 1 over ssh. The error is not shown to the user. For a failure the user caused or can act on, write the
|
|
||||||
// message and return it as a *Failure, which Fail does in one step; the surface then reports the failure without
|
|
||||||
// logging what the user was already told. Any other error is logged by the surface as well.
|
|
||||||
type CommandCallback func(fs any, a []string, w StringWriter) error
|
|
||||||
|
|
||||||
type Command struct {
|
|
||||||
Name Name
|
|
||||||
ShortDescription string
|
|
||||||
Help string
|
|
||||||
|
|
||||||
Flags CommandFlags
|
|
||||||
Callback CommandCallback
|
|
||||||
}
|
|
||||||
|
|
||||||
// Failure is the error a command returns for a failure it has already explained to the user:
|
|
||||||
// the message it wrote is the whole story. A surface reports it as a failure and does not
|
|
||||||
// log it.
|
|
||||||
type Failure struct {
|
|
||||||
Msg string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Failure) Error() string {
|
|
||||||
return f.Msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fail writes msg to w and returns it as a *Failure, so a command reports a failure to the
|
|
||||||
// user and to its surface in one line. If the write itself fails, that error is returned
|
|
||||||
// instead: the user was not told anything.
|
|
||||||
func Fail(w StringWriter, msg string) error {
|
|
||||||
if err := w.WriteLine(msg); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return &Failure{Msg: msg}
|
|
||||||
}
|
|
||||||
|
|
||||||
func execCommand(c *Command, args []string, w StringWriter) error {
|
|
||||||
var (
|
|
||||||
fl *flag.FlagSet
|
|
||||||
fs any
|
|
||||||
)
|
|
||||||
|
|
||||||
if c.Flags != nil {
|
|
||||||
fl, fs = c.Flags()
|
|
||||||
if fl != nil {
|
|
||||||
// SetOutput() here in case fl.Parse dumps usage.
|
|
||||||
fl.SetOutput(w.GetWriter())
|
|
||||||
err := fl.Parse(args)
|
|
||||||
if err != nil {
|
|
||||||
// fl.Parse has dumped error information to the user via the w writer.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args = fl.Args()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Callback(fs, args, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkHelpArgs(args []string) bool {
|
|
||||||
for _, a := range args {
|
|
||||||
if a == "-h" || a == "-help" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
// Name identifies a command. Every command nebula serves is a constant here, so a
|
|
||||||
// surface, a client and a test spell it the same way and are told at compile time when
|
|
||||||
// they do not. It is a string rather than an integer so a test can register a throwaway
|
|
||||||
// command without adding it here, and so the wire carries the name as the string it has
|
|
||||||
// always been.
|
|
||||||
type Name string
|
|
||||||
|
|
||||||
const (
|
|
||||||
ChangeRemote Name = "change-remote"
|
|
||||||
CloseTunnel Name = "close-tunnel"
|
|
||||||
CreateTunnel Name = "create-tunnel"
|
|
||||||
DeviceInfo Name = "device-info"
|
|
||||||
Help Name = "help"
|
|
||||||
ListHostmap Name = "list-hostmap"
|
|
||||||
ListLighthouseAddrmap Name = "list-lighthouse-addrmap"
|
|
||||||
ListPendingHostmap Name = "list-pending-hostmap"
|
|
||||||
LogFormat Name = "log-format"
|
|
||||||
LogLevel Name = "log-level"
|
|
||||||
Logout Name = "logout"
|
|
||||||
PrintCert Name = "print-cert"
|
|
||||||
PrintRelays Name = "print-relays"
|
|
||||||
PrintTunnel Name = "print-tunnel"
|
|
||||||
QueryLighthouse Name = "query-lighthouse"
|
|
||||||
Reload Name = "reload"
|
|
||||||
Version Name = "version"
|
|
||||||
)
|
|
||||||
@@ -1,181 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"cmp"
|
|
||||||
"fmt"
|
|
||||||
"maps"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/anmitsu/go-shlex"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Registry holds a set of commands. New is the only way to make one.
|
|
||||||
//
|
|
||||||
// It is safe for concurrent use. Commands may be registered while a surface is serving
|
|
||||||
// from the registry: the ssh server clones it per session, and a clone may overlap a
|
|
||||||
// Register.
|
|
||||||
type Registry struct {
|
|
||||||
mu sync.RWMutex
|
|
||||||
commands map[Name]*Command
|
|
||||||
}
|
|
||||||
|
|
||||||
// New returns an empty Registry with only `help` available.
|
|
||||||
func New() *Registry {
|
|
||||||
r := &Registry{commands: map[Name]*Command{}}
|
|
||||||
r.registerHelp()
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Registry) registerHelp() {
|
|
||||||
r.Register(&Command{
|
|
||||||
Name: Help,
|
|
||||||
ShortDescription: "prints available commands or help <command> for specific usage info",
|
|
||||||
Callback: func(a any, args []string, w StringWriter) error {
|
|
||||||
return r.help(args, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register adds a command that can be run by a user. A command registered under a name
|
|
||||||
// already held replaces it; see Clone for what that means for Help.
|
|
||||||
func (r *Registry) Register(c *Command) {
|
|
||||||
r.mu.Lock()
|
|
||||||
defer r.mu.Unlock()
|
|
||||||
r.commands[c.Name] = c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get returns the command registered under name, or nil if there is no such command.
|
|
||||||
func (r *Registry) Get(name Name) *Command {
|
|
||||||
r.mu.RLock()
|
|
||||||
defer r.mu.RUnlock()
|
|
||||||
return r.commands[name]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Match returns the names of every command starting with prefix, sorted, as strings for
|
|
||||||
// a prompt to complete and print.
|
|
||||||
func (r *Registry) Match(prefix string) []string {
|
|
||||||
cmds := make([]string, 0)
|
|
||||||
r.mu.RLock()
|
|
||||||
for n := range r.commands {
|
|
||||||
if strings.HasPrefix(string(n), prefix) {
|
|
||||||
cmds = append(cmds, string(n))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r.mu.RUnlock()
|
|
||||||
slices.Sort(cmds)
|
|
||||||
return cmds
|
|
||||||
}
|
|
||||||
|
|
||||||
// All returns every registered command sorted by name, which is the order help lists them.
|
|
||||||
func (r *Registry) All() []*Command {
|
|
||||||
r.mu.RLock()
|
|
||||||
defer r.mu.RUnlock()
|
|
||||||
|
|
||||||
cmds := slices.Collect(maps.Values(r.commands))
|
|
||||||
slices.SortFunc(cmds, func(a, b *Command) int { return cmp.Compare(a.Name, b.Name) })
|
|
||||||
return cmds
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clone returns a copy holding the same commands, with help bound to the copy: a
|
|
||||||
// command registered on the clone alone is listed and described there, and one the
|
|
||||||
// clone withholds is not. That rebinding replaces whatever was registered under Help on
|
|
||||||
// the source, built-in or not.
|
|
||||||
func (r *Registry) Clone() *Registry {
|
|
||||||
r.mu.RLock()
|
|
||||||
c := &Registry{commands: maps.Clone(r.commands)}
|
|
||||||
r.mu.RUnlock()
|
|
||||||
c.registerHelp()
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dispatch parses a command line and runs it, reporting problems to w. The error is for
|
|
||||||
// the caller to log, not to show; see CommandCallback.
|
|
||||||
func (r *Registry) Dispatch(line string, w StringWriter) error {
|
|
||||||
// A line shlex cannot split, an unbalanced quote.
|
|
||||||
args, err := shlex.Split(line, true)
|
|
||||||
if err != nil {
|
|
||||||
return Fail(w, "could not parse the command line: "+err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.DispatchArgs(args, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DispatchArgs runs an already tokenized command line. The error is as for Dispatch.
|
|
||||||
func (r *Registry) DispatchArgs(args []string, w StringWriter) error {
|
|
||||||
if len(args) == 0 {
|
|
||||||
r.dumpCommands(w)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
c := r.Get(Name(args[0]))
|
|
||||||
if c == nil {
|
|
||||||
// Only the first word was looked up, so only it is named.
|
|
||||||
msg := fmt.Sprintf("did not understand: %s", args[0])
|
|
||||||
if err := w.WriteLine(msg); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
r.dumpCommands(w)
|
|
||||||
return &Failure{Msg: msg}
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkHelpArgs(args) {
|
|
||||||
// Through the registered help command, so -h and `help <name>` are one path.
|
|
||||||
return execCommand(r.Get(Help), []string{string(c.Name)}, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
return execCommand(c, args[1:], w)
|
|
||||||
}
|
|
||||||
|
|
||||||
// dumpCommands writes the list of available commands to w.
|
|
||||||
func (r *Registry) dumpCommands(w StringWriter) {
|
|
||||||
err := w.WriteLine("Available commands:")
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// In the order All gives, which is the display order.
|
|
||||||
cmds := make([]string, 0)
|
|
||||||
for _, l := range r.All() {
|
|
||||||
cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = w.Write(strings.Join(cmds, "\n") + "\n\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// help writes the command list, or usage for the single command named in a.
|
|
||||||
func (r *Registry) help(a []string, w StringWriter) error {
|
|
||||||
// Just typed help
|
|
||||||
if len(a) == 0 {
|
|
||||||
r.dumpCommands(w)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// We are printing a specific commands help text
|
|
||||||
cmd := r.Get(Name(a[0]))
|
|
||||||
if cmd == nil {
|
|
||||||
return Fail(w, "Command not available "+a[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Help != "" {
|
|
||||||
if err := w.WriteLine(fmt.Sprintf(" %s", cmd.Help)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flags != nil {
|
|
||||||
fs, _ := cmd.Flags()
|
|
||||||
if fs != nil {
|
|
||||||
fs.SetOutput(w.GetWriter())
|
|
||||||
fs.PrintDefaults()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,176 +0,0 @@
|
|||||||
package commands
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testRegistry() *Registry {
|
|
||||||
r := New()
|
|
||||||
r.Register(&Command{
|
|
||||||
Name: "served",
|
|
||||||
ShortDescription: "a command this surface serves",
|
|
||||||
Callback: func(fs any, a []string, w StringWriter) error {
|
|
||||||
return w.WriteLine("read " + strings.Join(a, ","))
|
|
||||||
},
|
|
||||||
})
|
|
||||||
r.Register(&Command{
|
|
||||||
Name: "withheld",
|
|
||||||
ShortDescription: "a command this surface withholds",
|
|
||||||
Callback: func(fs any, a []string, w StringWriter) error {
|
|
||||||
return w.WriteLine("wrote")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
return r
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestFlagUsageGoesToTheWriter pins that a parse failure's usage lands on the command's
|
|
||||||
// writer.
|
|
||||||
func TestFlagUsageGoesToTheWriter(t *testing.T) {
|
|
||||||
r := New()
|
|
||||||
r.Register(&Command{
|
|
||||||
Name: "emit",
|
|
||||||
ShortDescription: "emits",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
fl.Bool("json", false, "emit json")
|
|
||||||
return fl, nil
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w StringWriter) error { return nil },
|
|
||||||
})
|
|
||||||
|
|
||||||
merged := &bytes.Buffer{}
|
|
||||||
require.Error(t, r.DispatchArgs([]string{"emit", "-nope"}, NewStringWriter(merged)))
|
|
||||||
assert.Contains(t, merged.String(), "flag provided but not defined")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDispatchReportsUnparseableLine: a line shlex rejects, an unbalanced quote, is a
|
|
||||||
// failure the user is told about.
|
|
||||||
func TestDispatchReportsUnparseableLine(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
err := testRegistry().Dispatch(`served "unterminated`, NewStringWriter(out))
|
|
||||||
|
|
||||||
var f *Failure
|
|
||||||
require.ErrorAs(t, err, &f)
|
|
||||||
assert.Equal(t, f.Msg+"\n", out.String())
|
|
||||||
assert.Contains(t, out.String(), "could not parse the command line: ")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestFailTellsTheUserAndTheSurface: Fail writes the message and returns it as a Failure;
|
|
||||||
// when the write fails, the user heard nothing, so the write error is what comes back.
|
|
||||||
func TestFailTellsTheUserAndTheSurface(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
err := Fail(NewStringWriter(out), "no such tunnel")
|
|
||||||
var f *Failure
|
|
||||||
require.ErrorAs(t, err, &f)
|
|
||||||
assert.Equal(t, "no such tunnel", f.Msg)
|
|
||||||
assert.Equal(t, "no such tunnel\n", out.String())
|
|
||||||
|
|
||||||
err = Fail(NewStringWriter(failingWriter{}), "no such tunnel")
|
|
||||||
require.EqualError(t, err, "gone")
|
|
||||||
assert.NotErrorAs(t, err, &f)
|
|
||||||
}
|
|
||||||
|
|
||||||
type failingWriter struct{}
|
|
||||||
|
|
||||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("gone") }
|
|
||||||
|
|
||||||
// TestAllSortsByName: registration order does not matter.
|
|
||||||
func TestAllSortsByName(t *testing.T) {
|
|
||||||
r := New()
|
|
||||||
for _, n := range []Name{"zz", Version, "aa", ChangeRemote} {
|
|
||||||
r.Register(&Command{Name: n})
|
|
||||||
}
|
|
||||||
|
|
||||||
var got []Name
|
|
||||||
for _, c := range r.All() {
|
|
||||||
got = append(got, c.Name)
|
|
||||||
}
|
|
||||||
assert.Equal(t, []Name{"aa", ChangeRemote, Help, Version, "zz"}, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestHelpFlagAgreesWithHelpCommand pins that -h answers through the registered help
|
|
||||||
// command, and that help on a clone describes the clone: a command registered only there
|
|
||||||
// is known to both -h and `help <name>`, and one withheld from it is known to neither.
|
|
||||||
func TestHelpFlagAgreesWithHelpCommand(t *testing.T) {
|
|
||||||
r := testRegistry()
|
|
||||||
c := r.Clone()
|
|
||||||
c.Register(&Command{
|
|
||||||
Name: Logout,
|
|
||||||
ShortDescription: "ends the session",
|
|
||||||
Callback: func(fs any, a []string, w StringWriter) error { return nil },
|
|
||||||
})
|
|
||||||
|
|
||||||
flag := &bytes.Buffer{}
|
|
||||||
c.Dispatch("logout -h", NewStringWriter(flag))
|
|
||||||
cmd := &bytes.Buffer{}
|
|
||||||
c.Dispatch("help logout", NewStringWriter(cmd))
|
|
||||||
assert.Equal(t, cmd.String(), flag.String())
|
|
||||||
assert.Equal(t, "logout - ends the session\n", flag.String())
|
|
||||||
|
|
||||||
// The source never learns of it.
|
|
||||||
src := &bytes.Buffer{}
|
|
||||||
r.Dispatch("help logout", NewStringWriter(src))
|
|
||||||
assert.Equal(t, "Command not available logout\n", src.String())
|
|
||||||
|
|
||||||
// And a command both hold is described either way.
|
|
||||||
flag.Reset()
|
|
||||||
c.Dispatch("served -h", NewStringWriter(flag))
|
|
||||||
assert.Equal(t, "served - a command this surface serves\n", flag.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCloneHelpListsTheClone: the command list help prints is the clone's, so a clone
|
|
||||||
// that withholds a command does not name it, and the source still does.
|
|
||||||
func TestCloneHelpListsTheClone(t *testing.T) {
|
|
||||||
r := testRegistry()
|
|
||||||
c := New()
|
|
||||||
c.Register(r.Get("served"))
|
|
||||||
c = c.Clone()
|
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
c.Dispatch("help", NewStringWriter(out))
|
|
||||||
assert.Equal(t, "Available commands:\n"+
|
|
||||||
"help - prints available commands or help <command> for specific usage info\n"+
|
|
||||||
"served - a command this surface serves\n\n", out.String())
|
|
||||||
|
|
||||||
out.Reset()
|
|
||||||
c.Dispatch("help withheld", NewStringWriter(out))
|
|
||||||
assert.Equal(t, "Command not available withheld\n", out.String())
|
|
||||||
|
|
||||||
out.Reset()
|
|
||||||
r.Dispatch("help withheld", NewStringWriter(out))
|
|
||||||
assert.Equal(t, "withheld - a command this surface withholds\n", out.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestDispatchReturnsTheCommandsError: what the callback returns reaches the caller, so
|
|
||||||
// a surface can log it.
|
|
||||||
func TestDispatchReturnsTheCommandsError(t *testing.T) {
|
|
||||||
r := New()
|
|
||||||
r.Register(&Command{
|
|
||||||
Name: "fails",
|
|
||||||
Callback: func(fs any, a []string, w StringWriter) error {
|
|
||||||
return errors.New("boom")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
require.EqualError(t, r.Dispatch("fails", NewStringWriter(out)), "boom")
|
|
||||||
assert.Empty(t, out.String(), "a command's own error is not shown; the command speaks for itself")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestUnknownCommandNamesTheWord: only the first word is looked up, so only it is
|
|
||||||
// reported, followed by the command list, and the line is a failure to the surface.
|
|
||||||
func TestUnknownCommandNamesTheWord(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
err := testRegistry().Dispatch(`nope "with args"`, NewStringWriter(out))
|
|
||||||
var f *Failure
|
|
||||||
require.ErrorAs(t, err, &f)
|
|
||||||
assert.Equal(t, "did not understand: nope", f.Msg)
|
|
||||||
assert.True(t, strings.HasPrefix(out.String(), "did not understand: nope\nAvailable commands:\n"), out.String())
|
|
||||||
}
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
package nebula
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/commands"
|
|
||||||
"github.com/slackhq/nebula/logging"
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestLogFormatRefusalIsShown: a format the logger does not know is reported to the user
|
|
||||||
// as a failure, rather than returned bare, which over ssh was silence.
|
|
||||||
func TestLogFormatRefusalIsShown(t *testing.T) {
|
|
||||||
l := logging.NewLogger(io.Discard)
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
w := commands.NewStringWriter(out)
|
|
||||||
|
|
||||||
var f *commands.Failure
|
|
||||||
require.ErrorAs(t, logFormatCommand(l, nil, []string{"bogus"}, w), &f)
|
|
||||||
assert.Equal(t, "Unable to set log format: unknown log format `bogus`. possible formats: [text json]\n", out.String())
|
|
||||||
|
|
||||||
out.Reset()
|
|
||||||
require.NoError(t, logFormatCommand(l, nil, []string{"JSON"}, w))
|
|
||||||
assert.Equal(t, "Log format is: json\n", out.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestPrintRelaysReportsErrorsAsText: the Error field was an error value, which encodes
|
|
||||||
// as {} and lost the message. It is the message now, and absent when there is none.
|
|
||||||
func TestPrintRelaysReportsErrorsAsText(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
hm := newHostMap(l)
|
|
||||||
relay := netip.MustParseAddr("10.0.0.1")
|
|
||||||
peer := netip.MustParseAddr("10.0.0.2")
|
|
||||||
|
|
||||||
// A relay host that is in Relays but not in Hosts, so its hostinfo cannot be found.
|
|
||||||
hm.Relays[1] = &HostInfo{vpnAddrs: []netip.Addr{relay}}
|
|
||||||
ifce := &Interface{hostMap: hm}
|
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
|
||||||
assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Error":"could not find hostinfo","Type":"","State":"","PeerAddr":"","LocalIndex":0,"RemoteIndex":0,"RelayedThrough":null}]}]}`, out.String())
|
|
||||||
|
|
||||||
// The same relay host, found, relaying for a peer whose index matches: no Error key.
|
|
||||||
hi := hm.Relays[1]
|
|
||||||
r := &Relay{Type: ForwardingType, State: Established, PeerAddr: peer, LocalIndex: 1, RemoteIndex: 2}
|
|
||||||
hi.relayState = RelayState{
|
|
||||||
relayForByAddr: map[netip.Addr]*Relay{peer: r},
|
|
||||||
relayForByIdx: map[uint32]*Relay{r.LocalIndex: r},
|
|
||||||
}
|
|
||||||
hm.Hosts[relay] = hi
|
|
||||||
|
|
||||||
out.Reset()
|
|
||||||
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
|
||||||
assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Type":"forwarding","State":"established","PeerAddr":"10.0.0.2","LocalIndex":1,"RemoteIndex":2,"RelayedThrough":null}]}]}`, out.String())
|
|
||||||
|
|
||||||
// And an index mismatch names it.
|
|
||||||
hm.Relays[7] = hi
|
|
||||||
delete(hm.Relays, 1)
|
|
||||||
out.Reset()
|
|
||||||
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
|
||||||
assert.Contains(t, out.String(), `"Error":"hostmap LocalIndex '7' does not match RelayState LocalIndex"`)
|
|
||||||
}
|
|
||||||
+31
-40
@@ -26,12 +26,9 @@ type C struct {
|
|||||||
files []string
|
files []string
|
||||||
Settings map[string]any
|
Settings map[string]any
|
||||||
oldSettings map[string]any
|
oldSettings map[string]any
|
||||||
callbacks []func(*C, *Reload)
|
callbacks []func(*C)
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
reloadLock sync.Mutex
|
reloadLock sync.Mutex
|
||||||
|
|
||||||
// Set by the first reload that needs a restart and never cleared. Guarded by reloadLock
|
|
||||||
needsRestart bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewC(l *slog.Logger) *C {
|
func NewC(l *slog.Logger) *C {
|
||||||
@@ -75,9 +72,8 @@ func (c *C) LoadString(raw string) error {
|
|||||||
// RegisterReloadCallback stores a function to be called when a config reload is triggered. The functions registered
|
// RegisterReloadCallback stores a function to be called when a config reload is triggered. The functions registered
|
||||||
// here should decide if they need to make a change to the current process before making the change. HasChanged can be
|
// here should decide if they need to make a change to the current process before making the change. HasChanged can be
|
||||||
// used to help decide if a change is necessary.
|
// used to help decide if a change is necessary.
|
||||||
// These functions should return quickly or spawn their own go routine if they will take a while.
|
// These functions should return quickly or spawn their own go routine if they will take a while
|
||||||
// Use r to report anything that failed or needs a restart to apply.
|
func (c *C) RegisterReloadCallback(f func(*C)) {
|
||||||
func (c *C) RegisterReloadCallback(f func(c *C, r *Reload)) {
|
|
||||||
c.callbacks = append(c.callbacks, f)
|
c.callbacks = append(c.callbacks, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,55 +144,50 @@ func (c *C) CatchHUP(ctx context.Context) {
|
|||||||
return
|
return
|
||||||
case <-ch:
|
case <-ch:
|
||||||
c.l.Info("Caught HUP, reloading config")
|
c.l.Info("Caught HUP, reloading config")
|
||||||
c.ReloadConfig().Log(c.l)
|
c.ReloadConfig()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReloadConfig reloads the config from the path provided to Load and runs every registered callback. A config that
|
func (c *C) ReloadConfig() {
|
||||||
// fails to load is logged here, like any other failure, and the result carries the error for the summary.
|
|
||||||
func (c *C) ReloadConfig() ReloadResult {
|
|
||||||
c.reloadLock.Lock()
|
c.reloadLock.Lock()
|
||||||
defer c.reloadLock.Unlock()
|
defer c.reloadLock.Unlock()
|
||||||
|
|
||||||
c.oldSettings = make(map[string]any)
|
c.oldSettings = make(map[string]any)
|
||||||
maps.Copy(c.oldSettings, c.Settings)
|
maps.Copy(c.oldSettings, c.Settings)
|
||||||
|
|
||||||
if err := c.Load(c.path); err != nil {
|
err := c.Load(c.path)
|
||||||
c.l.Error("Error occurred while reloading config", "config_path", c.path, "error", err)
|
if err != nil {
|
||||||
return ReloadResult{Err: err, RestartRequired: c.needsRestart}
|
c.l.Error("Error occurred while reloading config",
|
||||||
|
"config_path", c.path,
|
||||||
|
"error", err,
|
||||||
|
)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.runCallbacks()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReloadConfigString is ReloadConfig for a config held in memory rather than on disk.
|
|
||||||
func (c *C) ReloadConfigString(raw string) ReloadResult {
|
|
||||||
c.reloadLock.Lock()
|
|
||||||
defer c.reloadLock.Unlock()
|
|
||||||
|
|
||||||
c.oldSettings = make(map[string]any)
|
|
||||||
maps.Copy(c.oldSettings, c.Settings)
|
|
||||||
|
|
||||||
if err := c.LoadString(raw); err != nil {
|
|
||||||
c.l.Error("Error occurred while reloading config", "error", err)
|
|
||||||
return ReloadResult{Err: err, RestartRequired: c.needsRestart}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.runCallbacks()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *C) runCallbacks() ReloadResult {
|
|
||||||
r := &Reload{c: c}
|
|
||||||
for _, v := range c.callbacks {
|
for _, v := range c.callbacks {
|
||||||
v(c, r)
|
v(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *C) ReloadConfigString(raw string) error {
|
||||||
|
c.reloadLock.Lock()
|
||||||
|
defer c.reloadLock.Unlock()
|
||||||
|
|
||||||
|
c.oldSettings = make(map[string]any)
|
||||||
|
maps.Copy(c.oldSettings, c.Settings)
|
||||||
|
|
||||||
|
err := c.LoadString(raw)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.restart {
|
for _, v := range c.callbacks {
|
||||||
c.needsRestart = true
|
v(c)
|
||||||
}
|
}
|
||||||
return ReloadResult{Failed: r.failed, RestartRequired: c.needsRestart, RestartKeys: r.keys}
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetString will get the string for k or return the default d if not found or invalid
|
// GetString will get the string for k or return the default d if not found or invalid
|
||||||
@@ -400,7 +391,7 @@ func (c *C) parse() error {
|
|||||||
var nm map[string]any
|
var nm map[string]any
|
||||||
err = yaml.Unmarshal(b, &nm)
|
err = yaml.Unmarshal(b, &nm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s: %w", path, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to use WithAppendSlice so that firewall rules in separate
|
// We need to use WithAppendSlice so that firewall rules in separate
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func TestConfig_Load(t *testing.T) {
|
|||||||
// invalid yaml
|
// invalid yaml
|
||||||
c := NewC(l)
|
c := NewC(l)
|
||||||
os.WriteFile(filepath.Join(dir, "01.yaml"), []byte(" invalid yaml"), 0644)
|
os.WriteFile(filepath.Join(dir, "01.yaml"), []byte(" invalid yaml"), 0644)
|
||||||
require.EqualError(t, c.Load(dir), filepath.Join(dir, "01.yaml")+": yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into map[string]interface {}")
|
require.EqualError(t, c.Load(dir), "yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into map[string]interface {}")
|
||||||
|
|
||||||
// simple multi config merge
|
// simple multi config merge
|
||||||
c = NewC(l)
|
c = NewC(l)
|
||||||
@@ -129,7 +129,7 @@ func TestConfig_ReloadConfig(t *testing.T) {
|
|||||||
|
|
||||||
os.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: ho"), 0644)
|
os.WriteFile(filepath.Join(dir, "01.yaml"), []byte("outer:\n inner: ho"), 0644)
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *C, _ *Reload) {
|
c.RegisterReloadCallback(func(c *C) {
|
||||||
done <- true
|
done <- true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"slices"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ReloadResult describes how a reload went. Problems are logged where they happen, Log writes a one line summary.
|
|
||||||
type ReloadResult struct {
|
|
||||||
// Err is set when the new config could not be read or parsed, no callbacks ran
|
|
||||||
Err error
|
|
||||||
// Failed is true when a callback could not apply part of the new config
|
|
||||||
Failed bool
|
|
||||||
// RestartRequired is true once any reload has needed a restart, and stays true until the process restarts
|
|
||||||
RestartRequired bool
|
|
||||||
// RestartKeys are the keys this reload changed that only apply on a restart
|
|
||||||
RestartKeys []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Summary returns a one line summary of the reload and the level to log it at.
|
|
||||||
func (r ReloadResult) Summary() (slog.Level, string) {
|
|
||||||
switch {
|
|
||||||
case r.Err != nil && r.RestartRequired:
|
|
||||||
return slog.LevelError, "Config reload failed, the new config did not load and a restart is still required"
|
|
||||||
case r.Err != nil:
|
|
||||||
return slog.LevelError, "Config reload failed, the new config did not load"
|
|
||||||
case r.Failed && r.RestartRequired:
|
|
||||||
return slog.LevelError, "Config reload completed with errors and requires a restart to fully apply, review the logs"
|
|
||||||
case r.Failed:
|
|
||||||
return slog.LevelError, "Config reload completed with errors, review the logs"
|
|
||||||
case r.RestartRequired:
|
|
||||||
return slog.LevelWarn, "Config reload was completed but requires a restart to fully apply"
|
|
||||||
default:
|
|
||||||
return slog.LevelInfo, "Config reload complete"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// String is the summary with Err and RestartKeys included, for output other than the log.
|
|
||||||
func (r ReloadResult) String() string {
|
|
||||||
_, msg := r.Summary()
|
|
||||||
if r.Err != nil {
|
|
||||||
msg = fmt.Sprintf("%s: %v", msg, r.Err)
|
|
||||||
}
|
|
||||||
if len(r.RestartKeys) > 0 {
|
|
||||||
msg = fmt.Sprintf("%s, keys %v", msg, r.RestartKeys)
|
|
||||||
}
|
|
||||||
return msg
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log writes Summary to l.
|
|
||||||
func (r ReloadResult) Log(l *slog.Logger) {
|
|
||||||
level, msg := r.Summary()
|
|
||||||
if len(r.RestartKeys) > 0 {
|
|
||||||
l.Log(context.Background(), level, msg, "keys", r.RestartKeys)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
l.Log(context.Background(), level, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reload is passed to each reload callback to report how applying the new config went.
|
|
||||||
type Reload struct {
|
|
||||||
c *C
|
|
||||||
failed bool
|
|
||||||
restart bool
|
|
||||||
// Changed keys that only apply on a restart, from every callback
|
|
||||||
keys []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Failed marks the reload as failed, the caller logs why. Safe to call on a nil Reload, which is what the initial load
|
|
||||||
// passes.
|
|
||||||
func (r *Reload) Failed() {
|
|
||||||
if r != nil {
|
|
||||||
r.failed = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RestartRequired marks the reload as needing a restart, the caller logs why. Safe to call on a nil Reload.
|
|
||||||
func (r *Reload) RestartRequired() {
|
|
||||||
if r != nil {
|
|
||||||
r.restart = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RestartRequiredIfChanged marks the reload as needing a restart if any of keys changed, and adds them to RestartKeys.
|
|
||||||
// Use it for keys that are only read at startup. Safe to call on a nil Reload.
|
|
||||||
func (r *Reload) RestartRequiredIfChanged(keys ...string) {
|
|
||||||
if r == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, k := range keys {
|
|
||||||
if r.c.HasChanged(k) && !slices.Contains(r.keys, k) {
|
|
||||||
r.keys = append(r.keys, k)
|
|
||||||
r.restart = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"log/slog"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestReloadConfigString(t *testing.T) {
|
|
||||||
c := NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("a: 1"))
|
|
||||||
fail := false
|
|
||||||
ran := 0
|
|
||||||
c.RegisterReloadCallback(func(c *C, r *Reload) {
|
|
||||||
ran++
|
|
||||||
if fail {
|
|
||||||
r.Failed()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
assert.Equal(t, ReloadResult{}, c.ReloadConfigString("a: 2"))
|
|
||||||
fail = true
|
|
||||||
assert.Equal(t, ReloadResult{Failed: true}, c.ReloadConfigString("a: 3"))
|
|
||||||
assert.Equal(t, 2, ran)
|
|
||||||
|
|
||||||
// A config that fails to load runs no callbacks and returns the error
|
|
||||||
res := c.ReloadConfigString("a: [")
|
|
||||||
require.Error(t, res.Err)
|
|
||||||
assert.False(t, res.Failed)
|
|
||||||
assert.Equal(t, 2, ran)
|
|
||||||
assert.Equal(t, 3, c.GetInt("a", 0))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReloadRestartRequiredLatches(t *testing.T) {
|
|
||||||
c := NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("a: 1\nb: 1"))
|
|
||||||
c.RegisterReloadCallback(func(c *C, r *Reload) { r.RestartRequiredIfChanged("a", "b") })
|
|
||||||
|
|
||||||
// Nothing that needs a restart changed
|
|
||||||
assert.Equal(t, ReloadResult{}, c.ReloadConfigString("a: 1\nb: 1\nc: 1"))
|
|
||||||
assert.Equal(t, ReloadResult{RestartRequired: true, RestartKeys: []string{"a"}}, c.ReloadConfigString("a: 2\nb: 1\nc: 1"))
|
|
||||||
|
|
||||||
// The restart sticks through a reload with no changes, reverting the key and a config that fails to load. Only the
|
|
||||||
// reload that changed the key lists it
|
|
||||||
assert.Equal(t, ReloadResult{RestartRequired: true}, c.ReloadConfigString("a: 2\nb: 1\nc: 1"))
|
|
||||||
assert.Equal(t, ReloadResult{RestartRequired: true, RestartKeys: []string{"a"}}, c.ReloadConfigString("a: 1\nb: 1\nc: 1"))
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("a: [")
|
|
||||||
require.Error(t, res.Err)
|
|
||||||
assert.True(t, res.RestartRequired)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A key reported by more than one callback is listed once
|
|
||||||
func TestReloadRestartKeysDeduped(t *testing.T) {
|
|
||||||
c := NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("a: 1\nb: 1"))
|
|
||||||
c.RegisterReloadCallback(func(c *C, r *Reload) { r.RestartRequiredIfChanged("a") })
|
|
||||||
c.RegisterReloadCallback(func(c *C, r *Reload) { r.RestartRequiredIfChanged("a", "b") })
|
|
||||||
|
|
||||||
assert.Equal(t, []string{"a", "b"}, c.ReloadConfigString("a: 2\nb: 2").RestartKeys)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A callback can require a restart without a key changing, a file the config points at may have
|
|
||||||
func TestReloadRestartRequiredWithoutAChange(t *testing.T) {
|
|
||||||
c := NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("a: 1"))
|
|
||||||
c.RegisterReloadCallback(func(c *C, r *Reload) { r.RestartRequired() })
|
|
||||||
|
|
||||||
assert.Equal(t, ReloadResult{RestartRequired: true}, c.ReloadConfigString("a: 1"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReloadResultSummary(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
res ReloadResult
|
|
||||||
level slog.Level
|
|
||||||
msg string
|
|
||||||
}{
|
|
||||||
{ReloadResult{}, slog.LevelInfo, "Config reload complete"},
|
|
||||||
{ReloadResult{RestartRequired: true}, slog.LevelWarn, "Config reload was completed but requires a restart to fully apply"},
|
|
||||||
{ReloadResult{Failed: true}, slog.LevelError, "Config reload completed with errors, review the logs"},
|
|
||||||
{ReloadResult{Failed: true, RestartRequired: true}, slog.LevelError, "Config reload completed with errors and requires a restart to fully apply, review the logs"},
|
|
||||||
{ReloadResult{Err: errors.New("bad yaml")}, slog.LevelError, "Config reload failed, the new config did not load"},
|
|
||||||
{ReloadResult{Err: errors.New("bad yaml"), RestartRequired: true}, slog.LevelError, "Config reload failed, the new config did not load and a restart is still required"},
|
|
||||||
}
|
|
||||||
for _, tc := range cases {
|
|
||||||
level, msg := tc.res.Summary()
|
|
||||||
assert.Equal(t, tc.level, level, tc.msg)
|
|
||||||
assert.Equal(t, tc.msg, msg)
|
|
||||||
}
|
|
||||||
|
|
||||||
res := ReloadResult{RestartRequired: true, RestartKeys: []string{"a", "b"}}
|
|
||||||
assert.Equal(t, "Config reload was completed but requires a restart to fully apply, keys [a b]", res.String())
|
|
||||||
|
|
||||||
// String includes the error
|
|
||||||
assert.Equal(t, "Config reload failed, the new config did not load: bad yaml", ReloadResult{Err: errors.New("bad yaml")}.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// The initial load passes a nil Reload
|
|
||||||
func TestReloadNilIsSafe(t *testing.T) {
|
|
||||||
var r *Reload
|
|
||||||
r.Failed()
|
|
||||||
r.RestartRequired()
|
|
||||||
r.RestartRequiredIfChanged("a")
|
|
||||||
}
|
|
||||||
@@ -57,8 +57,7 @@ func newConnectionManagerFromConfig(l *slog.Logger, c *config.C, hm *HostMap, p
|
|||||||
}
|
}
|
||||||
|
|
||||||
cm.reload(c, true)
|
cm.reload(c, true)
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
r.RestartRequiredIfChanged("timers.connection_alive_interval", "timers.pending_deletion_interval")
|
|
||||||
cm.reload(c, false)
|
cm.reload(c, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -273,7 +272,7 @@ func (cm *connectionManager) migrateRelayUsed(oldhostinfo, newhostinfo *HostInfo
|
|||||||
InitiatorRelayIndex: index,
|
InitiatorRelayIndex: index,
|
||||||
}
|
}
|
||||||
|
|
||||||
switch newhostinfo.GetCert().Certificate.Version() {
|
switch protocolVersion(newhostinfo.GetCert().Certificate.Version()) {
|
||||||
case cert.Version1:
|
case cert.Version1:
|
||||||
if !relayFrom.Is4() {
|
if !relayFrom.Is4() {
|
||||||
cm.l.Error("can not migrate v1 relay with a v6 network because the relay is not running a current nebula version")
|
cm.l.Error("can not migrate v1 relay with a v6 network because the relay is not running a current nebula version")
|
||||||
|
|||||||
@@ -92,6 +92,14 @@ func (cs *ConnectionState) NextMessageCounter() (uint64, bool) {
|
|||||||
return c, true
|
return c, true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// heardFromPeer reports whether anything from the peer has decrypted under these
|
||||||
|
// keys, beyond the handshake messages the replay window was seeded with.
|
||||||
|
func (cs *ConnectionState) heardFromPeer(handshakeMessages uint64) bool {
|
||||||
|
cs.decryptLock.Lock()
|
||||||
|
defer cs.decryptLock.Unlock()
|
||||||
|
return cs.window.current > handshakeMessages
|
||||||
|
}
|
||||||
|
|
||||||
func (cs *ConnectionState) Curve() cert.Curve {
|
func (cs *ConnectionState) Curve() cert.Curve {
|
||||||
return cs.myCert.Curve()
|
return cs.myCert.Curve()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/rcrowley/go-metrics"
|
"github.com/rcrowley/go-metrics"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
ct "github.com/slackhq/nebula/cert_test"
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/commands"
|
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/overlay"
|
"github.com/slackhq/nebula/overlay"
|
||||||
)
|
)
|
||||||
@@ -48,7 +47,6 @@ type Control struct {
|
|||||||
|
|
||||||
f *Interface
|
f *Interface
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
commands *commands.Registry
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
sshStart func()
|
sshStart func()
|
||||||
@@ -374,13 +372,6 @@ func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands returns the registry the daemon's surfaces serve from. It is the live
|
|
||||||
// registry, not a copy: a command registered on it is served to every ssh session that
|
|
||||||
// starts afterwards. Clone it to run commands without that reach.
|
|
||||||
func (c *Control) Commands() *commands.Registry {
|
|
||||||
return c.commands
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Control) Device() overlay.Device {
|
func (c *Control) Device() overlay.Device {
|
||||||
return c.f.inside
|
return c.f.inside
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -151,9 +151,9 @@ func (c *fakeConn) WriteTo(_ []byte, _ netip.AddrPort) error { return nil }
|
|||||||
func (c *fakeConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
|
func (c *fakeConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
|
||||||
return len(bufs), nil
|
return len(bufs), nil
|
||||||
}
|
}
|
||||||
func (c *fakeConn) ReloadConfig(_ *config.C, _ *config.Reload) {}
|
func (c *fakeConn) ReloadConfig(_ *config.C) {}
|
||||||
func (c *fakeConn) SupportsMultipleReaders() bool { return true }
|
func (c *fakeConn) SupportsMultipleReaders() bool { return true }
|
||||||
func (c *fakeConn) Close() error { c.closed = true; return nil }
|
func (c *fakeConn) Close() error { c.closed = true; return nil }
|
||||||
|
|
||||||
type multiqueueDevice struct {
|
type multiqueueDevice struct {
|
||||||
*fakeDevice
|
*fakeDevice
|
||||||
|
|||||||
+23
-51
@@ -38,8 +38,8 @@ type dnsServer struct {
|
|||||||
|
|
||||||
serverMu sync.Mutex
|
serverMu sync.Mutex
|
||||||
server *dns.Server
|
server *dns.Server
|
||||||
// started is closed once `server` is serving (or after serve returns
|
// started is closed once `server` has finished binding (or after
|
||||||
// without ever starting). Stop waits on it before
|
// ListenAndServe returns on a bind failure). Stop waits on it before
|
||||||
// calling Shutdown to avoid the miekg/dns "server not started" race
|
// calling Shutdown to avoid the miekg/dns "server not started" race
|
||||||
// where a Shutdown that arrives before bind completes is silently
|
// where a Shutdown that arrives before bind completes is silently
|
||||||
// ignored, leaving the listener running forever.
|
// ignored, leaving the listener running forever.
|
||||||
@@ -69,10 +69,9 @@ func newDnsServerFromConfig(ctx context.Context, l *slog.Logger, pki *PKI, hostM
|
|||||||
ds.mux = dns.NewServeMux()
|
ds.mux = dns.NewServeMux()
|
||||||
ds.mux.HandleFunc(".", ds.handleDnsRequest)
|
ds.mux.HandleFunc(".", ds.handleDnsRequest)
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
if err := ds.reload(c, false); err != nil {
|
if err := ds.reload(c, false); err != nil {
|
||||||
ds.l.Error("Failed to reload DNS responder from config", "error", err)
|
ds.l.Error("Failed to reload DNS responder from config", "error", err)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -84,7 +83,7 @@ func newDnsServerFromConfig(ctx context.Context, l *slog.Logger, pki *PKI, hostM
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reload applies the latest config and reconciles the running state with it:
|
// reload applies the latest config and reconciles the running state with it:
|
||||||
// - enabled toggled on -> bind, then serve in the background
|
// - enabled toggled on -> spawn a runner
|
||||||
// - enabled toggled off -> stop the runner
|
// - enabled toggled off -> stop the runner
|
||||||
// - listen address changed (while running) -> restart on the new address
|
// - listen address changed (while running) -> restart on the new address
|
||||||
// - everything else -> no-op
|
// - everything else -> no-op
|
||||||
@@ -121,20 +120,13 @@ func (d *dnsServer) reload(c *config.C, initial bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if running && !sameAddr {
|
|
||||||
// Stop clears the slot before shutting down, otherwise the listen below can find the dying server and refuse
|
|
||||||
d.Stop()
|
|
||||||
running = false
|
|
||||||
}
|
|
||||||
if !running {
|
if !running {
|
||||||
// Bind here rather than in the background, so a reload that can't listen says so
|
// Was disabled (or never started); bring it up now.
|
||||||
server, started, err := d.listen()
|
go d.Start()
|
||||||
if err != nil {
|
} else if !sameAddr {
|
||||||
return err
|
// Stop clears the slot before shutting down, otherwise the Start below can find the dying server and refuse
|
||||||
}
|
d.Stop()
|
||||||
if server != nil {
|
go d.Start()
|
||||||
go d.serve(server, started)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh the self entry every enabled reload so cert renewals that change our name or VPN addresses are picked up.
|
// Refresh the self entry every enabled reload so cert renewals that change our name or VPN addresses are picked up.
|
||||||
@@ -163,50 +155,30 @@ func (d *dnsServer) shutdownServer(srv *dns.Server, started chan struct{}, reaso
|
|||||||
// Must be invoked after the tun device is active so that lighthouse.dns.host
|
// Must be invoked after the tun device is active so that lighthouse.dns.host
|
||||||
// may bind to a nebula IP.
|
// may bind to a nebula IP.
|
||||||
func (d *dnsServer) Start() {
|
func (d *dnsServer) Start() {
|
||||||
server, started, err := d.listen()
|
|
||||||
if err != nil {
|
|
||||||
d.l.Warn("Failed to run the DNS responder", "error", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if server != nil {
|
|
||||||
d.serve(server, started)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// listen binds the responder and claims the server slot for serve. It returns a nil server, and no error, when DNS is
|
|
||||||
// disabled or a server is already running.
|
|
||||||
func (d *dnsServer) listen() (*dns.Server, chan struct{}, error) {
|
|
||||||
if !d.enabled.Load() {
|
if !d.enabled.Load() {
|
||||||
return nil, nil, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
started := make(chan struct{})
|
started := make(chan struct{})
|
||||||
d.serverMu.Lock()
|
d.serverMu.Lock()
|
||||||
defer d.serverMu.Unlock()
|
|
||||||
// Re-check enabled under the lock, a disable that raced our check above snapshots the slot under it too.
|
// Re-check enabled under the lock, a disable that raced our check above snapshots the slot under it too.
|
||||||
// A reload and Control.Start can both get here, the loser would orphan the live listener past Stop
|
// Two reloads in quick succession can both spawn a Start, the loser would orphan the live listener past Stop
|
||||||
if d.ctx.Err() != nil || d.server != nil || !d.enabled.Load() {
|
if d.ctx.Err() != nil || d.server != nil || !d.enabled.Load() {
|
||||||
return nil, nil, nil
|
d.serverMu.Unlock()
|
||||||
}
|
return
|
||||||
pc, err := net.ListenPacket("udp", d.addr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
addr := d.addr
|
||||||
server := &dns.Server{
|
server := &dns.Server{
|
||||||
Addr: d.addr,
|
Addr: addr,
|
||||||
PacketConn: pc,
|
Net: "udp",
|
||||||
Handler: d.mux,
|
Handler: d.mux,
|
||||||
NotifyStartedFunc: func() { close(started) },
|
NotifyStartedFunc: func() { close(started) },
|
||||||
}
|
}
|
||||||
d.server = server
|
d.server = server
|
||||||
d.started = started
|
d.started = started
|
||||||
d.l.Info("Starting DNS responder", "dnsListener", pc.LocalAddr())
|
d.serverMu.Unlock()
|
||||||
return server, started, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// serve answers queries on a server from listen until Stop or the context ends it.
|
// Per-invocation ctx watcher. Exits when Start does, so we don't leak a
|
||||||
func (d *dnsServer) serve(server *dns.Server, started chan struct{}) {
|
|
||||||
// Per-invocation ctx watcher. Exits when serve does, so we don't leak a
|
|
||||||
// watcher per reload-driven restart.
|
// watcher per reload-driven restart.
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
@@ -217,16 +189,16 @@ func (d *dnsServer) serve(server *dns.Server, started chan struct{}) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := server.ActivateAndServe()
|
d.l.Info("Starting DNS responder", "dnsListener", addr)
|
||||||
|
err := server.ListenAndServe()
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
// If serving failed before it started NotifyStartedFunc never fires, so close started here to release any Stop
|
// If the listener never bound (bind error) NotifyStartedFunc never fires,
|
||||||
// caller waiting on it. ActivateAndServe doesn't close the socket on that path either
|
// so close started here to release any Stop caller waiting on it.
|
||||||
select {
|
select {
|
||||||
case <-started:
|
case <-started:
|
||||||
default:
|
default:
|
||||||
close(started)
|
close(started)
|
||||||
_ = server.PacketConn.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release our slot, unless a reload already replaced us, so a dead listener can't block a future Start
|
// Release our slot, unless a reload already replaced us, so a dead listener can't block a future Start
|
||||||
|
|||||||
@@ -629,22 +629,3 @@ func TestDnsServer_Start_refusesWhenDisabledUnderLock(t *testing.T) {
|
|||||||
require.NoError(t, err, "an orphaned listener is still holding the port")
|
require.NoError(t, err, "an orphaned listener is still holding the port")
|
||||||
_ = pc.Close()
|
_ = pc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// A reload that can't bind says so, rather than finding out in the background
|
|
||||||
func TestDnsServer_reload_busyPortFails(t *testing.T) {
|
|
||||||
busy, err := net.ListenPacket("udp", "127.0.0.1:0")
|
|
||||||
require.NoError(t, err)
|
|
||||||
t.Cleanup(func() { _ = busy.Close() })
|
|
||||||
_, port, err := net.SplitHostPort(busy.LocalAddr().String())
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
ds, c := newTestDnsServer(t)
|
|
||||||
setDnsConfig(c, "127.0.0.1", port, false, true)
|
|
||||||
require.NoError(t, ds.reload(c, true))
|
|
||||||
|
|
||||||
setDnsConfig(c, "127.0.0.1", port, true, true)
|
|
||||||
require.Error(t, ds.reload(c, false))
|
|
||||||
ds.serverMu.Lock()
|
|
||||||
assert.Nil(t, ds.server, "a failed bind kept the slot")
|
|
||||||
ds.serverMu.Unlock()
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
# Post-quantum handshake transport compared with QUIC
|
||||||
|
|
||||||
|
Status: analysis, 2026-10-14. Compares the fragment transport in
|
||||||
|
[pq-handshake.md](pq-handshake.md) with how QUIC makes its handshake reliable. TLS is out of
|
||||||
|
scope; only the transport mechanics are compared.
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
QUIC's handshake reliability is closed-loop: the receiver acknowledges, and the sender
|
||||||
|
repairs exactly what is missing. Ours is open-loop: repair symbols are sent up front, and a
|
||||||
|
timer sends fresh symbols. At handshake sizes (K <= 15) a QUIC-style acknowledgement
|
||||||
|
shrinks to a 16-bit bitmap, and with an MDS code to a single count. So the real comparison
|
||||||
|
is forward error correction versus acknowledgements, not QUIC versus us.
|
||||||
|
|
||||||
|
FEC is the right core. One piece of feedback is worth adding, initiator to responder and for
|
||||||
|
msg2 only (see [Worth taking from QUIC](#worth-taking-from-quic)).
|
||||||
|
|
||||||
|
## What QUIC does
|
||||||
|
|
||||||
|
- **Messages as a stream.** Handshake bytes travel as `CRYPTO` frames (offset and length)
|
||||||
|
and are reassembled in order. Initial and Handshake packets each have their own packet
|
||||||
|
number space.
|
||||||
|
- **Immediate acknowledgements.** Receivers must acknowledge Initial and Handshake packets
|
||||||
|
without delay. ACK frames list ranges of received packet numbers.
|
||||||
|
- **Loss detection** (RFC 9002). A packet is lost once 3 later packets are acknowledged, or
|
||||||
|
after 9/8 of the round-trip time. Lost bytes are resent in new packets.
|
||||||
|
- **Probe timeout (PTO).** If a whole flight vanishes, a probe timer fires. Before any RTT
|
||||||
|
sample it starts at about 1 s (333 ms initial RTT plus 4x its variance) and doubles each
|
||||||
|
time. Both endpoints run it.
|
||||||
|
- **Amplification limit and anti-deadlock.** The server may send at most 3x what it
|
||||||
|
received until the client's address is validated. The client must keep probing so a
|
||||||
|
server stuck at that limit cannot deadlock.
|
||||||
|
- **Retry.** A stateless round trip to validate the address. `NEW_TOKEN` lets later
|
||||||
|
connections skip it.
|
||||||
|
- **Congestion control from the first packet.** The initial window is 10 packets of 1200
|
||||||
|
bytes.
|
||||||
|
|
||||||
|
## Side by side
|
||||||
|
|
||||||
|
| | QUIC | pq-handshake |
|
||||||
|
|---|---|---|
|
||||||
|
| Receiver feedback | ACK ranges, sent immediately | None; the next handshake message is the only acknowledgement |
|
||||||
|
| Partial loss | +1 to 2 RTT (ACK, detect, resend) | 0 RTT while at most m symbols are lost |
|
||||||
|
| Total loss | PTO: about 1 s, then doubling | Next PQ tick: 300 ms, then 1 s (alternating), or 100/300/600 ms (sticky) |
|
||||||
|
| Spurious retransmit | Wasted bytes | Still useful: fresh repair indexes add to what the receiver holds |
|
||||||
|
| Reassembly | Every byte offset, in order | Any K of up to 256 symbols |
|
||||||
|
| Who retransmits | Both sides on their own timers | Initiator on a timer; the responder resends msg2 only when a repeat msg1 arrives (rate limited) |
|
||||||
|
| State from the first packet | Server buffers and acknowledges partial flights | Capped pre-authentication table that never acknowledges |
|
||||||
|
| Address validation | Retry, 1 RTT | Cookie under load, 1 RTT |
|
||||||
|
|
||||||
|
From the [simulation results](pq-handshake.md#simulation-results): phase 2 at 5% random
|
||||||
|
loss with no FEC completes on the first attempt 39.8% of the time. Under
|
||||||
|
acknowledgement-based recovery roughly 60% of handshakes would pay at least one extra round
|
||||||
|
trip. With FEC, 0.4% wait for a retry. Spending bandwidth to save round trips is the stated
|
||||||
|
design preference, and open-loop recovery delivers it.
|
||||||
|
|
||||||
|
## Where the designs already agree
|
||||||
|
|
||||||
|
- **1200-byte floor and the 3x amplification budget**, both taken directly from QUIC.
|
||||||
|
- **Cookie and Retry.** Both cost one round trip and both can be forged by an on-path
|
||||||
|
attacker. QUIC's Retry integrity tag uses a fixed, published key.
|
||||||
|
- **On-path injection.** QUIC's Initial keys are derived from the client's connection ID,
|
||||||
|
which an observer can read, so QUIC accepts the same handshake-blocking injection
|
||||||
|
weakness documented for our symbols in
|
||||||
|
[Injected symbols](pq-handshake.md#injected-symbols).
|
||||||
|
- **Phase 2 msg3 confirmation.** Resending msg3 until any authenticated packet arrives,
|
||||||
|
with a `Test` from the responder on completion, is QUIC's handshake confirmation and
|
||||||
|
`HANDSHAKE_DONE`.
|
||||||
|
|
||||||
|
## Worth taking from QUIC
|
||||||
|
|
||||||
|
### 1. A msg2 progress probe from the initiator
|
||||||
|
|
||||||
|
This is the main gap. Today, recovering a partially lost msg2 means the initiator resends a
|
||||||
|
full msg1 attempt; the responder decodes it, matches it in the answered-msg1 cache, and
|
||||||
|
resends msg2. In phase 2 that is about 12 fragments (~13 KB) uploaded just to act as an
|
||||||
|
acknowledgement, and recovery waits for the 300 ms or 1 s tick.
|
||||||
|
|
||||||
|
- The initiator already knows the responder's index: msg2 fragments carry it as `FragID`
|
||||||
|
(`sendFragmentedResponse` passes `hostinfo.localIndexId`).
|
||||||
|
- A one-packet probe, "msg2: have n of K", sent to that index would recover in about one
|
||||||
|
round trip. The responder finds it the way phase 2 plans to find msg3.
|
||||||
|
- Timing comes for free: msg1 send to first msg2 fragment is an RTT sample. Probe at
|
||||||
|
roughly RTT/8 plus the repair delay after the first msg2 fragment if decoding has not
|
||||||
|
completed.
|
||||||
|
- Forging a probe needs the 32-bit responder index, so only an on-path attacker can, and
|
||||||
|
the existing 3x budget bounds the reply.
|
||||||
|
- If no msg2 fragment arrived at all, the initiator keeps today's msg1 resend. That is the
|
||||||
|
same tail-loss case QUIC's PTO covers.
|
||||||
|
|
||||||
|
The reverse direction, the responder reporting progress on msg1, should not be added. It
|
||||||
|
needs per-entry timers on the pre-authentication path, which is exactly the state QUIC
|
||||||
|
servers carry and the reason QUIC needs Retry.
|
||||||
|
|
||||||
|
### 2. Buffer early data in phase 2
|
||||||
|
|
||||||
|
QUIC stacks hold a few packets they cannot decrypt yet. The phase 2 plan drops data that
|
||||||
|
reaches the responder before msg3 decodes. Because repair fragments trail by
|
||||||
|
`handshakes.pq.repair_delay` (10 ms), losing one msg3 data fragment drops about 10 ms of
|
||||||
|
the initiator's first data while the repair is in flight. A small queue on the pending
|
||||||
|
responder entry, replayed once msg3 verifies, avoids this.
|
||||||
|
|
||||||
|
### 3. Burst size across remotes
|
||||||
|
|
||||||
|
QUIC handshakes on a single path. `handleOutbound` sends the same `now` and `later` sets to
|
||||||
|
every known remote and to relays (`StartRelays`).
|
||||||
|
|
||||||
|
- Phase 1 is fine.
|
||||||
|
- In phase 2, four remotes means about 48 packets (~54 KB) leaving one uplink at once,
|
||||||
|
every PQ tick, about 4x QUIC's initial window.
|
||||||
|
- The Gilbert-Elliott simulation treats each path independently, so it does not capture
|
||||||
|
this self-inflicted burst on a constrained uplink such as cellular.
|
||||||
|
- Options: send the full FEC set only to the most likely remote and a reduced set to the
|
||||||
|
others, or interleave packets across remotes.
|
||||||
|
|
||||||
|
## Not worth taking
|
||||||
|
|
||||||
|
- **Packet numbers, ACK ranges, an RTT estimator, loss thresholds, congestion control.**
|
||||||
|
QUIC pays for this machinery because it also serves a data transport. Nebula carries
|
||||||
|
packets and leaves reliability to the overlay's own transports. Building it only for
|
||||||
|
handshakes of at most 15 fragments buys nothing a symbol count cannot.
|
||||||
|
- **Offset-based reassembly.** It gives up the any-K-of-256 property and makes spurious
|
||||||
|
retransmits wasteful again.
|
||||||
|
- **An RTT-adaptive retry timer.** Low value. The fixed 100 ms linear schedule already fires
|
||||||
|
sooner than QUIC's 1 s first PTO, and a spurious resend costs bandwidth, not correctness.
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
Keep FEC as the core and add the msg2 progress probe. That gets QUIC's roughly one round
|
||||||
|
trip tail recovery without pre-authentication state, and cuts phase 2's retry cost on the
|
||||||
|
uplink by about 10x. Items 2 and 3 are phase 2 concerns; item 1 also helps phase 1.
|
||||||
@@ -0,0 +1,563 @@
|
|||||||
|
# Post-quantum handshakes
|
||||||
|
|
||||||
|
Status: draft. Decisions below were made on 2026-10-13 and 2026-10-14; open items are listed at
|
||||||
|
the end. Post-quantum handshakes ship hybrid only, on version 3 certificates (decided
|
||||||
|
2026-10-14); see [Implementation](#implementation-certificates-and-the-hybrid-handshake).
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
1. **Confidentiality against harvest-now-decrypt-later.** Traffic recorded today must stay
|
||||||
|
secret after a cryptographically relevant quantum computer exists. This needs a
|
||||||
|
post-quantum key exchange now.
|
||||||
|
2. **Post-quantum authentication.** Needs certificates whose CA signature and host keys are
|
||||||
|
post-quantum, so certificates have to be reissued or extended. Post-quantum key exchange
|
||||||
|
ships only together with it: there is no post-quantum handshake on version 2 certificates.
|
||||||
|
3. **Never rely on IP fragmentation.** ML-KEM and ML-DSA do not fit in one packet, so the
|
||||||
|
handshake must carry messages larger than the path MTU itself.
|
||||||
|
|
||||||
|
Non-goals: identity hiding beyond what IX offers today, and signatures at handshake time.
|
||||||
|
The only signature in the system stays the CA signature inside the certificate.
|
||||||
|
|
||||||
|
## Primitives and sizes
|
||||||
|
|
||||||
|
| Primitive | Size |
|
||||||
|
|---|---|
|
||||||
|
| ML-KEM-1024 encapsulation key | 1568 B |
|
||||||
|
| ML-KEM-1024 ciphertext | 1568 B |
|
||||||
|
| ML-DSA-87 public key / signature | 2592 B / 4627 B |
|
||||||
|
| Today's IX msg1 / msg2 (v2 cert, 151 B) | 252 B / 287 B |
|
||||||
|
|
||||||
|
`crypto/mlkem` is available in Go 1.26; `crypto/mldsa` arrives in Go 1.27, which `go.mod`
|
||||||
|
requires.
|
||||||
|
|
||||||
|
## An ephemeral-KEM handshake, built and dropped
|
||||||
|
|
||||||
|
The first design was `ix_psk2_mlkem1024`: classical IX on version 2 certificates with an ephemeral
|
||||||
|
ML-KEM-1024 exchange mixed in through the `psk2` slot, so recorded traffic was protected without
|
||||||
|
reissuing certificates. It was built, field tested and then dropped on 2026-10-14, before any
|
||||||
|
release. Post-quantum handshakes ship hybrid only, for three reasons:
|
||||||
|
|
||||||
|
- It left authentication classical, which a deployment wanting quantum safety needs anyway, and
|
||||||
|
a second post-quantum handshake would be twice the code to review and keep.
|
||||||
|
- It completes in two messages, so the responder could not tell which of two crossing attempts
|
||||||
|
the initiator kept (see [Crossing attempts](#three-messages-in-the-manager)); on real hosts 5 of
|
||||||
|
100 first pings failed at 50 ms round trip for that reason.
|
||||||
|
- Its `psk` token came last in msg2, so the responder's static key travelled under classical keys
|
||||||
|
only.
|
||||||
|
|
||||||
|
Subtype 2 and payload field 9 (its encapsulation key) stay reserved. The code is on the
|
||||||
|
`pq-handshake` branch for reference.
|
||||||
|
|
||||||
|
## Hybrid IX with certificate v3
|
||||||
|
|
||||||
|
Decided on 2026-10-14 and implemented on the `pq-identity` branch (see
|
||||||
|
[Implementation](#implementation-certificates-and-the-hybrid-handshake)).
|
||||||
|
|
||||||
|
### Certificate v3
|
||||||
|
|
||||||
|
A version 3 certificate is a version 2 certificate extended with post-quantum keys. It is its own
|
||||||
|
format, with its own PEM banner (`NEBULA CERTIFICATE V3`) and a strict parser, but its first
|
||||||
|
fields are exactly the fields of a v2 certificate:
|
||||||
|
|
||||||
|
```
|
||||||
|
CertificateV3 ::= SEQUENCE {
|
||||||
|
details [0] Details, -- v2 details, unchanged
|
||||||
|
curve [1] Curve DEFAULT curve25519,
|
||||||
|
publicKey [2] OCTET STRING, -- classical: X25519 / P256 for hosts, Ed25519 / ECDSA P256 for CAs
|
||||||
|
signature [3] OCTET STRING, -- classical CA signature over details || curve || publicKey, as in v2
|
||||||
|
pqSuite [4] OCTET STRING, -- one byte, the PQSuite; always present
|
||||||
|
pqPublicKey [5] OCTET STRING, -- the suite's KEM public key for hosts, its signature public key for CAs
|
||||||
|
pqSignature [6] OCTET STRING -- the suite's signature by the issuing CA, context "nebula certificate v3",
|
||||||
|
-- over the DER of fields 0 to 5, curve always present
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- **The suite.** `pqSuite` names the post-quantum algorithms the way `curve` names the classical
|
||||||
|
ones: the KEM of host keys, and the signature algorithm of CA keys and of the certificates a CA
|
||||||
|
signs. A CA and every certificate it signs share a suite, as they share a curve; the CA pool
|
||||||
|
rejects a mismatch, and the post-quantum signature covers the suite, so a certificate can not be
|
||||||
|
relabelled. The field is always present, in handshakes too, with no default, and 0 is never
|
||||||
|
valid. There is one suite today, `MLKEM1024_MLDSA87` (1): ML-KEM-1024 hosts, ML-DSA-87 CAs. A
|
||||||
|
new suite is an entry in the cert package's suite table plus, for a new KEM, a hybrid subtype; no
|
||||||
|
new certificate version. Changing a CA's suite means a new CA, or a new v3 extension of its v2
|
||||||
|
form that replaces the old one, since one v2 CA has one v3 extension.
|
||||||
|
|
||||||
|
- **The v2 form.** Fields 0 to 3 are a byte exact v2 certificate, signed by the v2 form of the CA.
|
||||||
|
A v3 holder presents it in classical handshakes, so peers that only speak v2 accept
|
||||||
|
it as they always have, and old parsers never see v3 bytes. A node therefore needs one
|
||||||
|
certificate, not a v2 and v3 pair.
|
||||||
|
- **Signatures.** Both must verify. The ML-DSA-87 signature covers the classical signature too, so
|
||||||
|
neither signature can be swapped out (which also makes P256 low/high-s alternates moot for v3).
|
||||||
|
If ML-DSA fell classically and someone swapped `pqPublicKey`, the hybrid handshake still needs
|
||||||
|
the classical static key, which only the classical signature covers. The ML-DSA-87 signature is
|
||||||
|
deterministic (FIPS 204 section 3.4), so extending the same v2 certificate with the same key twice
|
||||||
|
gives the same v3 certificate and fingerprint: `pq-extend` is idempotent. The hedged variant
|
||||||
|
guards signers an attacker can fault physically, which a CA key in a file is not, and the Ed25519
|
||||||
|
signature beside it is deterministic too.
|
||||||
|
- **Issuer and fingerprints.** The details are v2-shaped, so the issuer is the fingerprint of the
|
||||||
|
CA's v2 form. A v3 certificate's own fingerprint is the SHA-256 of its whole encoding and differs
|
||||||
|
from its v2 form's. Both identify it. To block a v3 certificate, block its v2 fingerprint: the
|
||||||
|
pool checks it for the v3 certificate and for its v2 form alike, so one entry covers both forms,
|
||||||
|
while blocking only the v3 fingerprint leaves the v2 form usable in classical handshakes.
|
||||||
|
`nebula-cert print` leads with both fingerprints and says which one to block. Firewall `ca_sha`
|
||||||
|
rules match a v3 peer by either fingerprint of its CA.
|
||||||
|
- **CA pool.** A v3 CA verifies only v3 certificates. To accept v2 certificates as well the pool
|
||||||
|
must also list the CA's v2 form. Dropping the v2 form is how a node requires post-quantum
|
||||||
|
authentication; there is no separate setting. Two different v3 extensions of one v2 CA are
|
||||||
|
rejected as ambiguous.
|
||||||
|
- **Keys are composite**, classical followed by post-quantum, wherever a v3 certificate deals in
|
||||||
|
them: `PublicKey()` returns `classical || pq`, and private keys are `classical || seed` (64 byte
|
||||||
|
ML-KEM seed for hosts, 32 byte ML-DSA seed for CAs), in one PEM block in `pki.key` with banners
|
||||||
|
such as `NEBULA X25519 MLKEM1024 PRIVATE KEY` and `NEBULA ED25519 MLDSA87 PRIVATE KEY`. The
|
||||||
|
banner names the post-quantum algorithm, not the suite, because a seed's size does not tell the
|
||||||
|
algorithm's strength.
|
||||||
|
- **Both curves.** Curve25519 and P256; the post-quantum half does not depend on the curve.
|
||||||
|
- **In-place upgrade.** `nebula-cert pq-extend` turns an existing v2 CA into a v3 CA whose v2 form
|
||||||
|
is the old CA certificate, byte for byte, and extends existing host certificates the same way.
|
||||||
|
Fingerprints, blocklists and trust in the v2 forms carry over unchanged. Hosts get their ML-KEM
|
||||||
|
key with `nebula-cert keygen -in-key`, which keeps the classical key.
|
||||||
|
|
||||||
|
| Certificate | Full | In a handshake | v2 form |
|
||||||
|
|---|---|---|---|
|
||||||
|
| CA, Curve25519 | 7365 B | n/a | 134 B |
|
||||||
|
| Host, Curve25519 | 6378 B | 4772 B | 171 B |
|
||||||
|
|
||||||
|
`crypto/mldsa` is new in Go 1.27, so `go.mod` moves to Go 1.27.
|
||||||
|
|
||||||
|
### Handshake
|
||||||
|
|
||||||
|
A new subtype, `ix_hybrid_mlkem1024` (3), runs `HandshakeHybridIX` from
|
||||||
|
[github.com/DefinedNet/noise](https://github.com/DefinedNet/noise) (its `PQ_RESEARCH.md` gives the
|
||||||
|
design rationale and security argument), with ML-KEM-1024 supplied by nebula:
|
||||||
|
|
||||||
|
```
|
||||||
|
-> e, s
|
||||||
|
<- ekem, skem, e, ee, se, s, es
|
||||||
|
-> skem
|
||||||
|
```
|
||||||
|
|
||||||
|
- `e` and `s` carry composite keys, X25519 (or P256) followed by ML-KEM-1024. DH tokens use the
|
||||||
|
classical halves, `ekem` and `skem` encapsulate to the ML-KEM halves, and every secret goes into
|
||||||
|
the one chaining key.
|
||||||
|
- **The responder's `e` is classical only.** It follows the responder's `ekem`, which already
|
||||||
|
completed the ephemeral KEM exchange, so it carries only the X25519 (or P256) key that `ee` and
|
||||||
|
`se` need. A second ephemeral KEM exchange would add nothing: one `ekem` is lost only if the
|
||||||
|
initiator's ephemeral private key or the responder's encapsulation randomness is, and a reverse
|
||||||
|
one would be lost in the same cases. PQNoise's pqIX has no responder `e` at all. The static ML-KEM key is the post-quantum half of the certificate's public
|
||||||
|
key, so it travels in `s` and the handshake form of the certificate leaves out both keys.
|
||||||
|
- **KEM tokens first.** msg2 starts with `ekem, skem`, so the responder's static key and
|
||||||
|
certificate are encrypted under keys that already include both KEM secrets: the responder's
|
||||||
|
identity is protected against harvest-now-decrypt-later, as TLS 1.3 with `X25519MLKEM768`
|
||||||
|
protects the server certificate. (OpenSSH offers no comparison: its hybrid key exchange sends the
|
||||||
|
host key in the clear.) The initiator's certificate travels in msg1 in the clear, as in IX today.
|
||||||
|
- **Authentication.** The responder authenticates the initiator when msg3 decrypts (it depends on
|
||||||
|
msg2's `skem` to the initiator's static key); the initiator authenticates the responder when the
|
||||||
|
first packet under the new keys decrypts (it depends on msg3's `skem`). Neither side needs a
|
||||||
|
signature. Knowing only a peer's classical private key gets an impersonator nowhere: as an
|
||||||
|
initiator it cannot read msg2, as a responder it cannot read msg3.
|
||||||
|
- **The two static ML-KEM keys see different ciphertexts.** msg2's `skem` ciphertext is encrypted
|
||||||
|
under a key derived from the `ekem` secret, which whoever sent msg2 chose, so anyone who can
|
||||||
|
address a pending initiator's index can have the initiator decapsulate a ciphertext of their
|
||||||
|
choosing with its static key. That is the IND-CCA setting ML-KEM is designed for, and
|
||||||
|
`crypto/mlkem` is constant time. The responder decrypts msg3's `skem` ciphertext under the keys of
|
||||||
|
a completed msg2 before decapsulating, so it only ever decapsulates ciphertexts the genuine
|
||||||
|
initiator sealed.
|
||||||
|
- v3 certificates go with this subtype and nothing else: a v3 certificate on any other subtype, or
|
||||||
|
a v2 one on this one, is a protocol error.
|
||||||
|
- **One subtype per KEM.** A hybrid subtype names its KEM, and a v3 holder initiates the subtype of
|
||||||
|
its suite's KEM. Peers need the same KEM, not the same suite: suites that differ only in their
|
||||||
|
signature algorithm share a subtype, and each certificate carries its own suite. A responder
|
||||||
|
whose suite uses another KEM drops msg1, so the pair behaves as if the responder did not speak
|
||||||
|
PQ: classical fallback, or no tunnel under `require`. A suite's KEM must have a subtype for the
|
||||||
|
pki to load a certificate in it.
|
||||||
|
|
||||||
|
| Message | Size (Curve25519) | Data fragments (k) |
|
||||||
|
|---|---|---|
|
||||||
|
| msg1 | 8009 B | 7 |
|
||||||
|
| msg2 | 9628 B | 9 |
|
||||||
|
| msg3 | 1616 B | 2 |
|
||||||
|
|
||||||
|
**Dependency.** DefinedNet/noise is a private repository today. `go.mod` points at a local clone
|
||||||
|
with a `replace` directive; the module has to be published (or the hybrid patterns upstreamed to
|
||||||
|
flynn/noise) before this can merge.
|
||||||
|
|
||||||
|
### Three messages in the manager
|
||||||
|
|
||||||
|
- **Responder.** After msg2 the responder keeps the handshake pending under its local index, the
|
||||||
|
multi-message responder the manager lacked before. A repeated msg1 maps to it through the
|
||||||
|
answered-msg1 cache and gets msg2 resent with fresh symbols. msg3's fragments name
|
||||||
|
the responder's index, so they reach the pending entry and never the pre-authentication table;
|
||||||
|
its reassembly is capped at 4 KiB. Until msg3 verifies, data for that index is dropped quietly:
|
||||||
|
a RecvError would tear down the initiator's new tunnel. On completing, the responder sends an
|
||||||
|
empty `Test`/`TestReply` under the new keys so the initiator hears back without traffic. A
|
||||||
|
pending responder expires after the handshake timeout.
|
||||||
|
- **Initiator.** Writing msg3 yields the transport keys, so the tunnel goes live at once, readable
|
||||||
|
only by the real responder; msg3 goes out before any packets cached during the handshake. Until
|
||||||
|
anything from the responder decrypts on the new tunnel, msg3 is resent with fresh symbols on a
|
||||||
|
linear backoff, and if nothing has decrypted by the handshake timeout the tunnel is closed.
|
||||||
|
Losing msg3 costs the data sent right behind it, which the overlay retransmits.
|
||||||
|
- **Lost confirmation.** A msg3 fragment for a v3 tunnel the responder already completed means the
|
||||||
|
initiator is still waiting, so the responder confirms again, at most once per retry interval and
|
||||||
|
only to the tunnel's own remote, never to the fragment's source.
|
||||||
|
- **Crossing attempts.** The initiator alternates, and its first classical attempt follows the
|
||||||
|
hybrid msg1 anywhere from right away to three retry intervals later (the retry timer lands two
|
||||||
|
slots ahead on the timer wheel, which moves on whole ticks), so at real round-trip times it
|
||||||
|
often goes out before msg2 is back. Answered, it would complete at once on the responder while
|
||||||
|
the hybrid handshake waits for msg3. So for four retry intervals after answering a peer's new
|
||||||
|
hybrid msg1, the responder leaves that peer's classical msg1s unanswered: the crossing one
|
||||||
|
arrives within the window whatever the round trip, because both take the same path. The
|
||||||
|
window is short so an initiator whose hybrid attempt fails on its own side (one that does not
|
||||||
|
yet trust our v3 CA, during a rollout) still gets a classical answer to a later attempt; a
|
||||||
|
block lasting as long as the hybrid state would refuse it until its handshake timed out, and
|
||||||
|
its next handshake would start with a hybrid msg1 again. A classical msg1 that still crosses,
|
||||||
|
past the window on a very slow path, is answered, but msg3 settles it: the initiator only
|
||||||
|
sends msg3 after committing to hybrid, so the responder takes the hybrid tunnel even though
|
||||||
|
the classical one is newer, rather than rejecting it as too old.
|
||||||
|
- **A failed attempt.** A post-quantum attempt can fail fatally where the classical one would not,
|
||||||
|
for example a v3 certificate from a CA the initiator trusts only in its v2 form. The failed
|
||||||
|
attempt is dropped and the handshake continues classical. With `require` the handshake ends.
|
||||||
|
|
||||||
|
### Rollout with v3
|
||||||
|
|
||||||
|
- `handshakes.pq` only does anything on a node with a v3 certificate. Without one, `initiate`,
|
||||||
|
`accept` and `sticky` log a warning and are ignored, and `require` is refused at startup and
|
||||||
|
on reload, since such a node could reach no one.
|
||||||
|
- A responder without a v3 certificate, or whose suite uses another KEM, drops hybrid msg1s
|
||||||
|
before reassembly, and the initiator falls back to classical.
|
||||||
|
- `handshakes.pq.require` means hybrid only, in both directions, so every peer needs a v3
|
||||||
|
certificate.
|
||||||
|
- Post-quantum authentication is required through the CA pool, by not listing the CA's v2 form.
|
||||||
|
A sticky or `require` initiator resists active downgrade of the key exchange; only the CA pool
|
||||||
|
resists downgrade of authentication, since a quantum attacker could forge a v2 form.
|
||||||
|
- `sticky` stays off by default for v3 holders too (decided 2026-10-14): a peer re-issued back to a
|
||||||
|
v2-only certificate would otherwise be unreachable until this process restarts. Deployments that
|
||||||
|
want downgrade resistance turn on `sticky` or `require`, or drop the CA's v2 form from the pool.
|
||||||
|
|
||||||
|
## Fragment transport
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
- No IP fragmentation: every datagram's UDP payload is at most 1232 bytes **after** relay
|
||||||
|
wrapping (16 B header + 16 B tag), the IPv6 minimum MTU budget also used by DNS Flag Day
|
||||||
|
2020 and Solana's Turbine.
|
||||||
|
- Every fragment of a handshake uses the same 5-tuple.
|
||||||
|
- Bounded pre-authentication state, bounded amplification, selective loss recovery,
|
||||||
|
tolerance of reordering and duplication, and a strictly validated, fuzzable parser.
|
||||||
|
|
||||||
|
### Wire format
|
||||||
|
|
||||||
|
Every packet of a fragmented handshake subtype is:
|
||||||
|
|
||||||
|
```
|
||||||
|
Nebula header (16) Version | Type=Handshake | Subtype | Reserved=0 | RemoteIndex | Counter
|
||||||
|
Fragment header (8) FragID u32 | MsgLen u16 | Index u8 | Flags u8
|
||||||
|
Cookie (16) only when Flags has 0x01
|
||||||
|
Symbol SymbolSize bytes
|
||||||
|
```
|
||||||
|
|
||||||
|
- `Counter` is the Noise message number (1, 2, 3). `RemoteIndex` is 0 for msg1, as today.
|
||||||
|
- `FragID` is the sender's local index (random, non-zero).
|
||||||
|
- `MsgLen` is the length of the whole message. Both ends derive the layout from it:
|
||||||
|
`K = ceil(MsgLen / 1160)`, `SymbolSize = ceil(MsgLen / K)`. Every symbol has the same
|
||||||
|
size. Room for a cookie is always reserved, so a packet is at most
|
||||||
|
16 + 8 + 16 + 1160 = 1200 bytes with or without one.
|
||||||
|
- `Index < K` is a data symbol (a slice of the message, the last one zero padded);
|
||||||
|
`Index >= K` is a repair symbol.
|
||||||
|
- `Flags` bit 0x01 means a cookie follows the fragment header. Other bits must be zero.
|
||||||
|
|
||||||
|
A cookie reply, sent by a responder under load, is a bare 32 byte packet:
|
||||||
|
|
||||||
|
```
|
||||||
|
Nebula header (16) Version | Type=Handshake | Subtype | Reserved=0 | RemoteIndex=FragID | Counter=0
|
||||||
|
Cookie (16)
|
||||||
|
```
|
||||||
|
|
||||||
|
It names the initiator's FragID, so the initiator finds its attempt the same way it finds
|
||||||
|
msg2.
|
||||||
|
|
||||||
|
### Forward error correction
|
||||||
|
|
||||||
|
Messages are protected with a systematic Reed-Solomon code over GF(2^8), implemented in
|
||||||
|
`handshake/fec`:
|
||||||
|
|
||||||
|
- The repair rows form a Cauchy matrix, `coefficient(row, col) = 1 / (row XOR col)` with
|
||||||
|
`row >= K > col`, so every square submatrix is invertible and **any K distinct symbols
|
||||||
|
reconstruct the message**. Naive identity-over-Vandermonde constructions are not MDS
|
||||||
|
(the PAR1 flaw and the 1997 Plank tutorial erratum).
|
||||||
|
- A repair row depends only on its index, so repair symbols are generated on demand and
|
||||||
|
their indexes never need to be agreed in advance. Up to 256 symbols exist per message.
|
||||||
|
- Known-answer vectors in `fec_test.go` pin the encoding and were cross-checked against an
|
||||||
|
independent implementation; changing them is a wire format change.
|
||||||
|
- Cost for a 9.8 KB message (k=9): about 13 µs for three repair symbols, about 19 µs to
|
||||||
|
decode with three data symbols missing.
|
||||||
|
|
||||||
|
**The sender chooses m**, the number of repair symbols per attempt. The receiver only needs
|
||||||
|
any K symbols, so this is policy and can change without a protocol change. The starting
|
||||||
|
rule is `m = max(2, ceil(K/3))`.
|
||||||
|
|
||||||
|
**Retries send fresh symbols.** Attempt 0 sends indexes `0 .. K+m-1`; each retry sends
|
||||||
|
the next `K+m` unused indexes. The receiver merges all distinct symbols across attempts,
|
||||||
|
so no received fragment is wasted. Indexes are a deterministic function of the message,
|
||||||
|
so retransmits stay idempotent: the responder caches msg2 itself and regenerates any
|
||||||
|
symbol.
|
||||||
|
|
||||||
|
**Repair symbols trail the data.** Data symbols go out back to back; repair symbols follow
|
||||||
|
after a short delay, so a queue tail-drop at the end of the data run does not also take the
|
||||||
|
repair. In simulation this lifts small messages substantially (k=2 at 10% loss, burst 8:
|
||||||
|
91.4% to 98.2% first-attempt success). Pacing every fragment was also evaluated; it gains
|
||||||
|
about 5 more points for k=7..9 on bursty paths, which is not worth the extra machinery,
|
||||||
|
and the retry with fresh symbols covers the remainder.
|
||||||
|
|
||||||
|
### Receiver rules
|
||||||
|
|
||||||
|
Implemented in `handshake/fragment` (`Sender`, `Assembly`, and the capped `Table`).
|
||||||
|
|
||||||
|
- Fragments are stored individually as they arrive and charged against a memory cap.
|
||||||
|
Nothing is allocated from a claimed size alone (FragmentSmack, game-engine "fragment
|
||||||
|
bombs").
|
||||||
|
- `MsgLen` must be identical for every fragment of a `FragID`; a mismatch drops the whole
|
||||||
|
entry. msg1 is capped at `MsgLen <= 16384` (K <= 15).
|
||||||
|
- The decoder keeps at most K distinct symbols, so an entry never exceeds about MsgLen bytes
|
||||||
|
however many repair symbols arrive.
|
||||||
|
- When K symbols are present the message is decoded and handed to the handshake `Machine`
|
||||||
|
as a synthetic `[header | message]`. The Machine never sees fragments.
|
||||||
|
- A completed message the responder answered is remembered without its symbols, outside the
|
||||||
|
memory and per-source caps, for the idle timeout: which symbol indexes it has seen, and its
|
||||||
|
hash. Fragments still trailing the attempt that completed it are counted and dropped. Once as
|
||||||
|
many new symbols as it has data symbols arrive, which only a retry offers, it is reported as a
|
||||||
|
repeat and the cached response is resent. Without this, the repair fragments trailing a msg1
|
||||||
|
whose data fragments already completed it open an entry that for K above the repair count can
|
||||||
|
never complete, and back to back handshakes from one source run into its quota (seen on real
|
||||||
|
hosts: the fifth hybrid handshake within 3 s fell back to classical). A message that decoded
|
||||||
|
but was not answered is forgotten, and the next attempt rebuilds it.
|
||||||
|
- If Noise rejects a decoded message, the entry is discarded and the next attempt
|
||||||
|
supplies fresh symbols. (Possible refinement: keep a few spare symbols so an injected
|
||||||
|
bad symbol can be identified by re-encoding instead of waiting for a retry.)
|
||||||
|
- Pre-authentication msg1 entries live in a table keyed by (source address or relay index,
|
||||||
|
FragID) with a total memory cap, per-source quotas (/32 for IPv4, /64 for IPv6), a
|
||||||
|
sliding TTL of about 2 s refreshed by progress, and random eviction under pressure.
|
||||||
|
msg2 and msg3 reassembly attaches to the existing pending handshake found by RemoteIndex.
|
||||||
|
The initiator keeps two msg2 slots keyed by the responder's FragID, each capped at 16 KiB.
|
||||||
|
When a stale lighthouse address for the peer belongs to another host now, both hosts answer
|
||||||
|
the same msg1, and their interleaved bursts would wipe each other out of a single slot that
|
||||||
|
emptied on every FragID change; with a slot each, whichever completes first is processed, and
|
||||||
|
a wrong host's answer takes the usual block-and-retry path. The responder's msg3 slot needs no
|
||||||
|
such care: it checks FragID against the initiator's index it learned from msg2.
|
||||||
|
|
||||||
|
### Retransmission and amplification
|
||||||
|
|
||||||
|
- Only timers cause transmission, with one reactive exception: the responder resends its
|
||||||
|
cached msg2 when a copy of msg1 completes again. An initiator sends msg1 to every address
|
||||||
|
it knows and through its relays, the responder reassembles each path separately, and each
|
||||||
|
path's first completion is answered on that path at once. A path that only works one way,
|
||||||
|
say a responder answering from the wrong interface, is then covered by the others in the
|
||||||
|
same attempt, and the initiator keeps the union of the msg2 symbols that reach it. After
|
||||||
|
that a path is answered again at most once per retry interval, and only once K symbols not
|
||||||
|
seen before on it arrive: duplicates do nothing, and there are 256 symbols per message, so
|
||||||
|
a resend costs the initiator K packets on that path and the answer is held to the 3x
|
||||||
|
budget below. That is TFTP's Sorcerer's Apprentice bound, per path rather than per
|
||||||
|
handshake.
|
||||||
|
A path is only an address and port, so a recently answered msg1 replayed from spoofed
|
||||||
|
sources draws one cached msg2 to each, at about 1.2-1.4x the bytes sent and never over 3x,
|
||||||
|
for as long as the answered msg1 is remembered (the handshake timeout). Accepted
|
||||||
|
(2026-10-15): classical IX already resends its stored msg2 to any source that replays msg1,
|
||||||
|
with no clock at all, and a per-handshake clock is what broke a path that only works one way.
|
||||||
|
- Duplicate msg1 detection compares the decoded message to the cached one **before**
|
||||||
|
running Noise or certificate verification.
|
||||||
|
- Before an address is validated, a responder sends at most 3x the bytes it received from
|
||||||
|
it (QUIC's rule). Normal handshakes sit near 1.1-1.2x.
|
||||||
|
- The initiator retransmits msg3 until any authenticated packet arrives from
|
||||||
|
the responder; the responder sends a small confirmation on completion and drops tunnel
|
||||||
|
traffic until msg3 verifies.
|
||||||
|
|
||||||
|
### Denial of service
|
||||||
|
|
||||||
|
The responder can no longer complete statelessly on msg1, since msg1 arrives in pieces.
|
||||||
|
Defenses are the capped table above plus a **cookie under load**, in the style of
|
||||||
|
WireGuard's mac2, QUIC Retry and the SCTP state cookie:
|
||||||
|
|
||||||
|
- Once the msg1 table is at least half full (by bytes or by entries), every msg1 fragment
|
||||||
|
must carry a valid cookie. One without is answered with a cookie reply and leaves no
|
||||||
|
state. Fragments that already belong to an entry need one too, so an attacker cannot
|
||||||
|
hold old entries open.
|
||||||
|
- A cookie is `HMAC-SHA256(secret, source address and port, relay channel, FragID)`
|
||||||
|
truncated to 16 bytes. The secret rotates every 2 minutes and the previous one is still
|
||||||
|
accepted, so a cookie is good for 2 to 4 minutes.
|
||||||
|
- Replies are never larger than the packet that prompted them (no amplification) and are
|
||||||
|
rate limited as a whole (a burst of 256, then 1000 per second), so spoofed floods cannot
|
||||||
|
turn the responder into a reflector.
|
||||||
|
- The initiator attaches the cookie to its PQ attempt, so every later packet carries it,
|
||||||
|
and immediately resends fresh symbols to the address the reply came from, at most once
|
||||||
|
per retry interval. Under load a PQ handshake costs one extra round trip, which in
|
||||||
|
practice still beats the classical attempt on the next retry tick.
|
||||||
|
- An attacker on the path can forge a cookie reply; the bogus cookie is simply rejected
|
||||||
|
and answered with a real one.
|
||||||
|
|
||||||
|
A CA-keyed pre-filter MAC was considered and rejected: networks commonly trust many CAs.
|
||||||
|
|
||||||
|
#### Pending hybrid responders
|
||||||
|
|
||||||
|
A hybrid responder that answered msg1 keeps a pending entry, keyed by its local index, until
|
||||||
|
msg3 arrives or the handshake timeout (5.5 s by default) passes. msg1 is cleartext and nothing
|
||||||
|
covers `e` or `Time`, so from one captured v3 msg1 an attacker can mint distinct valid msg1s
|
||||||
|
with a fresh `e` and a bumped `Time`. Each misses the answered-msg1 cache, costs the responder
|
||||||
|
the whole msg1 (two signature verifications, two encapsulations, four DH operations, the
|
||||||
|
certificate checks) and a pending entry, and the fragment table stays near empty because a
|
||||||
|
complete burst arrives together, so cookies are never demanded and spoofed sources work.
|
||||||
|
Pending entries are not bounded per peer or in total. Accepted as is (2026-10-14): the per-msg1
|
||||||
|
work is the same class as classical IX's pre-authentication cost, the expiry bounds the state in
|
||||||
|
time and the fragment table bounds bytes in flight. If it needs tightening, cap pending
|
||||||
|
responders per certificate fingerprint and in total, and count them toward the cookie
|
||||||
|
threshold.
|
||||||
|
|
||||||
|
#### Injected symbols
|
||||||
|
|
||||||
|
The decoder keeps the first K distinct symbols it sees, so one forged symbol in those K
|
||||||
|
spoils that attempt's decode; Noise rejects the result and the next attempt brings fresh
|
||||||
|
symbols. Forging one needs the attempt's FragID or index, which are random 32 bit values,
|
||||||
|
and for msg1 the initiator's exact address and port too, so in practice only an attacker
|
||||||
|
who sees the traffic can do it. An attacker who can also drop packets gains nothing new.
|
||||||
|
One who can only inject, and races a forged symbol into every attempt, can stop a PQ
|
||||||
|
handshake where it could not stop a classical one, since classical msg2s are tried one
|
||||||
|
packet at a time. Keeping spare symbols to find a consistent subset only raises the number
|
||||||
|
of forgeries needed per attempt, and authenticating symbols is impossible for msg1, which
|
||||||
|
travels before any key exists. So the decoder deliberately does neither. A non-sticky
|
||||||
|
initiator still gets a classical tunnel from its next attempt; a sticky one, by design,
|
||||||
|
does not downgrade.
|
||||||
|
|
||||||
|
## Rollout
|
||||||
|
|
||||||
|
- Separate settings for initiating and accepting PQ handshakes, both off by default:
|
||||||
|
`handshakes.pq.initiate`, `handshakes.pq.accept`. `handshakes.pq.repair_delay` (default
|
||||||
|
10ms) sets how long repair fragments trail the data fragments.
|
||||||
|
- `handshakes.pq.require` makes every handshake PQ: it implies `initiate` and `accept`,
|
||||||
|
never offers a classical attempt, and drops classical msg1s. It needs a v3 certificate.
|
||||||
|
- Old nodes silently drop unknown subtypes, so discovery is speculative: an initiator with
|
||||||
|
`initiate` enabled alternates PQ and classical attempts on successive timer retries,
|
||||||
|
keeping both pending, and the first to complete wins. A path nothing has been sent over
|
||||||
|
yet gets the PQ attempt first: an early resend triggered by a lighthouse reply is PQ, and
|
||||||
|
a relay that comes up pushes the PQ attempt through at once, counting as the PQ turn.
|
||||||
|
Without that, relays set up during the first attempt handed every relayed handshake to
|
||||||
|
the classical retry (seen on real hosts: 11 of 11). Once a msg2 fragment or cookie reply
|
||||||
|
arrives for the PQ attempt the peer evidently speaks PQ, and classical stops getting
|
||||||
|
turns.
|
||||||
|
- All `handshakes.pq` settings reload without a restart. Handshakes in flight carry on;
|
||||||
|
the new settings apply to the next attempt and the next packet.
|
||||||
|
- `handshakes.pq.sticky` (default off): once a peer has completed a PQ handshake, never fall
|
||||||
|
back to classical for it again during this process lifetime. This resists active
|
||||||
|
downgrade after first contact.
|
||||||
|
- No lighthouse capability advertisement.
|
||||||
|
|
||||||
|
## Simulation results
|
||||||
|
|
||||||
|
Gilbert-Elliott channel (bad state drops everything), losses expressed as average rate and
|
||||||
|
mean burst length in packets. First-attempt success for the whole handshake with
|
||||||
|
`m = max(2, ceil(K/3))` and repair delayed 16 packet slots, compared with no FEC:
|
||||||
|
|
||||||
|
| Handshake | Loss | Burst 1 | Burst 2 | Burst 4 | Burst 8 |
|
||||||
|
|---|---|---|---|---|---|
|
||||||
|
| two messages (2, 2) no FEC | 5% | 81.4% | 85.6% | 87.9% | 89.0% |
|
||||||
|
| two messages (2, 2) FEC | 5% | 99.9% | 99.4% | 99.3% | 98.6% |
|
||||||
|
| two messages (2, 2) FEC | 10% | 99.3% | 97.5% | 97.4% | 96.3% |
|
||||||
|
| hybrid (7, 9, 2) no FEC | 5% | 39.8% | 57.5% | 70.4% | 77.7% |
|
||||||
|
| hybrid (7, 9, 2) FEC | 5% | 99.6% | 94.2% | 90.4% | 88.8% |
|
||||||
|
| hybrid (7, 9, 2) FEC | 10% | 95.9% | 84.6% | 79.0% | 77.3% |
|
||||||
|
|
||||||
|
With fresh repair symbols, the hybrid handshake completes within two attempts at least 99.1% of the time
|
||||||
|
in every scenario above, and within three at least 99.99%.
|
||||||
|
|
||||||
|
## Prior art
|
||||||
|
|
||||||
|
- **WireGuard / Rosenpass**: fit every message in one packet; cookies and biscuits for
|
||||||
|
statelessness. Not possible at our sizes.
|
||||||
|
- **IKEv2**: RFC 7383 fragments only after keys exist, with each fragment authenticated;
|
||||||
|
RFC 9242/9370 move PQ key exchange into intermediate exchanges for that reason.
|
||||||
|
- **QUIC / DTLS 1.3**: offset-based reassembly with ACKs; QUIC's 3x amplification limit and
|
||||||
|
1200-byte floor.
|
||||||
|
- **Solana Turbine**: Reed-Solomon over 1232-byte UDP packets through untrusted relays;
|
||||||
|
moved to Merkle-authenticated shreds after unauthenticated repair symbols enabled
|
||||||
|
poisoning.
|
||||||
|
- **kcp-go, UDPspeeder, CM256**: RS FEC over UDP; parity must scale up for small groups.
|
||||||
|
- **gQUIC FEC (removed)**: FEC does not pay for bulk transfer under bursty loss; it does
|
||||||
|
for short, latency-critical transfers such as handshakes.
|
||||||
|
- **TFTP, NFS duplicate request cache, memcached UDP, IP reassembly bugs**: retransmit
|
||||||
|
storms, ID-wrap misassembly, reflection amplification, and allocation from claimed sizes.
|
||||||
|
|
||||||
|
## Implementation: fragment transport and manager
|
||||||
|
|
||||||
|
- `handshake/fec`, `handshake/fragment`: the code and the fragment transport.
|
||||||
|
- `handshake_manager_pq.go` and the manager:
|
||||||
|
- **Initiator.** Attempts alternate PQ and classical, starting with PQ (sticky peers get
|
||||||
|
only PQ). The PQ attempt has its own local index registered alongside the classical
|
||||||
|
one; whichever response completes first wins, and the other index is released
|
||||||
|
(`settleIndex`). A timed out or deleted pending handshake releases both.
|
||||||
|
- **Responder.** msg1 fragments go into the capped pre-authentication table. A decoded
|
||||||
|
msg1 is looked up by hash among recently answered ones before any Noise, KEM or
|
||||||
|
certificate work: the trailing repair fragments of an attempt rebuild a msg1 its data
|
||||||
|
fragments already completed, and a retrying initiator offers fresh symbols for the
|
||||||
|
same msg1. A repeat goes straight to the cached-response resend, which offers fresh
|
||||||
|
msg2 symbols on the path the repeat came by and is held to the 3x amplification
|
||||||
|
budget.
|
||||||
|
- **Relays** carry fragments unchanged; repair fragments trail via a timer.
|
||||||
|
- The pre-authentication table is capped at 4 MiB, 1024 entries, 4 per source (IPv4 /32,
|
||||||
|
IPv6 /64, or relay channel), 16 KiB messages and a 3 s idle timeout. Cookies are
|
||||||
|
demanded from half full.
|
||||||
|
- Tests: codec and fragment tests and fuzzers, manager unit tests
|
||||||
|
for the response budget, table tests for the per-path repeat clock, and e2e tests for a clean handshake, loss of
|
||||||
|
data fragments in both directions, fallback to a peer that does not accept PQ, a lost
|
||||||
|
msg2 recovered by fresh symbols, sticky mode under an attacker dropping PQ packets,
|
||||||
|
relays, a direct msg2 lost on every attempt and answered through a relay instead, index
|
||||||
|
cleanup on completion, fallback and timeout, and a responder flooded from
|
||||||
|
spoofed sources demanding and accepting a cookie. Unit tests cover cookie binding and
|
||||||
|
rotation, the never-amplify rule, the initiator's rate limited resend, and reload. ML-KEM also
|
||||||
|
passes under `GOEXPERIMENT=boringcrypto` and `GODEBUG=fips140=on`.
|
||||||
|
|
||||||
|
## Implementation: certificates and the hybrid handshake
|
||||||
|
|
||||||
|
- `cert`: `cert_v3.go` (format, `V2Form`, `ExtendToV3`, `SignPQ`), `pq_suite.go` (`PQSuite` and
|
||||||
|
the suite table), `SignHybridWith` and the composite `Sign` path, the CA pool's v3 index, suite
|
||||||
|
check and v2-form blocklisting, and the composite key PEM banners, generated per suite.
|
||||||
|
- `handshake`: the `ix_hybrid_mlkem1024` subtype on `noise.HandshakeHybridIX`, `KEMMLKEM1024`,
|
||||||
|
`HybridSubtype` mapping a suite to the subtype of its KEM,
|
||||||
|
composite credentials, cipher states ordered by role for three message patterns, and
|
||||||
|
`Machine.PeerCert` for a responder that has to act before completion.
|
||||||
|
- `pki.go` loads a v3 certificate with its composite key and derives the v2 form; messages after
|
||||||
|
the handshake treat v3 tunnels as v2 (`protocolVersion`).
|
||||||
|
- `handshake_manager_hybrid.go`: pending responders, confirmation and reconfirmation, the
|
||||||
|
initiator's msg3 resends and teardown, and the crossing window; `outside.go` keeps quiet about
|
||||||
|
pending responder indexes.
|
||||||
|
- `nebula-cert`: `ca`, `keygen` and `sign` take `-version 3`, `keygen -in-key` extends an existing
|
||||||
|
host key, `sign -version 2` with a v3 CA signs as its v2 form, and `pq-extend` upgrades v2
|
||||||
|
certificates in place, keeping an encrypted CA key encrypted. `ca`, `keygen` and `pq-extend` of a
|
||||||
|
CA take `-pq-suite`; `sign` and `pq-extend` of a host take the CA's suite.
|
||||||
|
- Tests: certificate format, strictness, tampering, pool and blocklist rules and in-place
|
||||||
|
extension on both curves; Machine-level hybrid handshakes including corrupted msg3 and
|
||||||
|
impersonation with only the classical key; e2e tests for a clean hybrid handshake, a lost
|
||||||
|
confirmation, a never confirmed handshake (no RecvErrors, teardown, expiry), a v2-only peer, an
|
||||||
|
initiator that trusts only the v2 form, `require` both ways, and relays; pki loading; firewall
|
||||||
|
`ca_sha`; and the `nebula-cert` flows.
|
||||||
|
|
||||||
|
## Open items
|
||||||
|
|
||||||
|
- Publish DefinedNet/noise and drop the `replace` in `go.mod`, once Nate says the fork is ready to
|
||||||
|
ship. The classical-only responder `e` is on the local clone's `drop-responder-ekem-key` branch
|
||||||
|
(b4a892b), not yet in DefinedNet/noise.
|
||||||
|
- DefinedNet/noise `ReadMessage` returns `ErrShortMessage` without rolling back the symmetric
|
||||||
|
state ([DefinedNet/noise#2](https://github.com/DefinedNet/noise/issues/2), inherited from
|
||||||
|
flynn/noise). A message that fails a length check after a key-mixing token leaves `ck` and `h`
|
||||||
|
advanced, so the genuine message can never be read: one spoofed short packet to a pending
|
||||||
|
initiator's index, which msg1 carries in the clear in every IX variant, kills that attempt until
|
||||||
|
it times out, and the Machine reports it as recoverable. Blocks publishing the module; the fix is
|
||||||
|
a deferred rollback that also restores `k`, `n` and `hasK`. Smaller pre-publish items are in
|
||||||
|
[DefinedNet/noise#3](https://github.com/DefinedNet/noise/issues/3).
|
||||||
|
Field tested on real hosts on 2026-10-14 (`pq-lab/2026-10-14-hybrid/`, untracked): LAN median 3.2 ms
|
||||||
|
against 1.4 ms classical, +2.0 ms at 50 ms round trip, no fallbacks with `require` under loss.
|
||||||
|
|
||||||
|
The cryptographic review of 2026-10-14 found no break in the composition or the certificate
|
||||||
|
chain. Its outcomes are folded in above: deterministic ML-DSA signing, the fingerprint guidance,
|
||||||
|
the `skem` oracle note, the psk2 note (moot now that handshake is dropped), the pending-responder
|
||||||
|
exposure and the `sticky` default.
|
||||||
|
|
||||||
|
Accepted as is: a tunnel that came up classical between two v3 holders (say, the hybrid attempt
|
||||||
|
was dropped) stays classical until it rehandshakes for another reason. `require`, `sticky` and a
|
||||||
|
CA pool without v2 forms cover deployments that need more. PKCS#11 keys do not support v3.
|
||||||
@@ -1,164 +0,0 @@
|
|||||||
//go:build e2e_testing
|
|
||||||
|
|
||||||
package e2e
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/cert"
|
|
||||||
"github.com/slackhq/nebula/cert_test"
|
|
||||||
"github.com/slackhq/nebula/commands"
|
|
||||||
"github.com/slackhq/nebula/sshd"
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// volatile lists everything in command output that legitimately differs between runs.
|
|
||||||
// Each run mints fresh certificates and indexes, so these are replaced with a marker
|
|
||||||
// before comparison. Keep the list short: anything added here is something the golden
|
|
||||||
// stops guarding.
|
|
||||||
var volatile = []struct {
|
|
||||||
re *regexp.Regexp
|
|
||||||
with string
|
|
||||||
}{
|
|
||||||
{regexp.MustCompile(`[0-9a-f]{40,}`), "<hex>"}, // fingerprints, keys, signatures
|
|
||||||
{regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})`), "<time>"}, // RFC 3339
|
|
||||||
{regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? [+-]\d{4} \w+`), "<time>"}, // time.Time.String()
|
|
||||||
{regexp.MustCompile(`(?s)(-----BEGIN [A-Z ]+-----\n).*?(\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // print-cert -raw
|
|
||||||
{regexp.MustCompile(`(-----BEGIN [A-Z ]+-----\\n).*?(\\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // the same, inside a JSON string
|
|
||||||
{regexp.MustCompile(`"(localIndex|remoteIndex|messageCounter)":\d+`), `"$1":<n>`},
|
|
||||||
{regexp.MustCompile(`(?m)^(\s*"?(?:LocalIndex|RemoteIndex|localIndex|remoteIndex|messageCounter)"?:?\s*)\d+`), "${1}<n>"}, // and the -pretty form
|
|
||||||
}
|
|
||||||
|
|
||||||
func normalise(s string) string {
|
|
||||||
for _, v := range volatile {
|
|
||||||
s = v.re.ReplaceAllString(s, v.with)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// outputCase is one command line to run through the ssh dispatch path.
|
|
||||||
type outputCase struct {
|
|
||||||
name string
|
|
||||||
args []string
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCommandOutput pins the bytes every command produces, in every format, through the
|
|
||||||
// ssh dispatch path. It is the safety net for moving rendering out of the commands: a
|
|
||||||
// change here is a change someone at an ssh prompt would see. Regenerate with:
|
|
||||||
//
|
|
||||||
// go test -tags e2e_testing ./e2e -run TestCommandOutput -update
|
|
||||||
func TestCommandOutput(t *testing.T) {
|
|
||||||
ca, _, caKey, _ := cert_test.NewTestCaCert(
|
|
||||||
cert.Version1, cert.Curve_CURVE25519,
|
|
||||||
time.Now(), time.Now().Add(10*time.Minute),
|
|
||||||
nil, nil, []string{},
|
|
||||||
)
|
|
||||||
|
|
||||||
control, _, _, _ := newSimpleServer(cert.Version1, ca, caKey, "output-golden", "10.222.0.1/24", nil)
|
|
||||||
control.Start()
|
|
||||||
t.Cleanup(func() { control.Stop() })
|
|
||||||
|
|
||||||
// As an ssh session does: a clone of the daemon's registry with logout added, which
|
|
||||||
// help on the clone lists and describes. There is no session to end here.
|
|
||||||
reg := control.Commands().Clone()
|
|
||||||
reg.Register(sshd.LogoutCommand(func() {}))
|
|
||||||
|
|
||||||
// run dispatches as the ssh exec path does and returns the output and the error, which
|
|
||||||
// over ssh is exit status 1.
|
|
||||||
run := func(args ...string) (string, error) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
err := reg.DispatchArgs(args, commands.NewStringWriter(buf))
|
|
||||||
return buf.String(), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seed a pending tunnel so the listing and per-address commands have something to show.
|
|
||||||
seed, err := run("create-tunnel", "10.222.0.9")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, "Created\n", seed, "seeding a tunnel should succeed before anything else is recorded")
|
|
||||||
|
|
||||||
cases := []outputCase{
|
|
||||||
{"version", nil},
|
|
||||||
{"device-info", nil},
|
|
||||||
{"list-hostmap", nil},
|
|
||||||
{"list-hostmap", []string{"-by-index"}},
|
|
||||||
{"list-pending-hostmap", nil},
|
|
||||||
{"list-pending-hostmap", []string{"-by-index"}},
|
|
||||||
{"list-lighthouse-addrmap", nil},
|
|
||||||
{"print-cert", nil},
|
|
||||||
{"print-cert", []string{"-raw"}},
|
|
||||||
{"print-cert", []string{"10.222.0.9"}},
|
|
||||||
{"print-cert", []string{"10.222.0.250"}},
|
|
||||||
{"print-cert", []string{"not-an-address"}},
|
|
||||||
{"print-tunnel", nil},
|
|
||||||
{"print-tunnel", []string{"10.222.0.9"}},
|
|
||||||
{"print-tunnel", []string{"10.222.0.250"}},
|
|
||||||
{"print-relays", nil},
|
|
||||||
{"query-lighthouse", nil},
|
|
||||||
{"query-lighthouse", []string{"10.222.0.9"}},
|
|
||||||
{"query-lighthouse", []string{"not-an-address"}},
|
|
||||||
// The test logger cannot change level or format, so these cases record the
|
|
||||||
// refusal each command prints on it, not a successful change.
|
|
||||||
{"log-level", nil},
|
|
||||||
{"log-level", []string{"debug"}},
|
|
||||||
{"log-level", []string{"bogus"}},
|
|
||||||
{"log-level", []string{"info"}},
|
|
||||||
{"log-format", nil},
|
|
||||||
{"log-format", []string{"json"}},
|
|
||||||
{"log-format", []string{"text"}},
|
|
||||||
{"change-remote", nil},
|
|
||||||
{"change-remote", []string{"10.222.0.9"}},
|
|
||||||
{"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.9"}},
|
|
||||||
{"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.250"}},
|
|
||||||
{"create-tunnel", nil},
|
|
||||||
{"create-tunnel", []string{"10.222.0.9"}},
|
|
||||||
// A fresh address, so this exercises the handshake start with a remote attached
|
|
||||||
// rather than the "already handshaking" path above.
|
|
||||||
{"create-tunnel", []string{"-address", "10.94.0.10:4242", "10.222.0.10"}},
|
|
||||||
{"create-tunnel", []string{"not-an-address"}},
|
|
||||||
{"close-tunnel", nil},
|
|
||||||
{"close-tunnel", []string{"10.222.0.250"}},
|
|
||||||
{"close-tunnel", []string{"not-an-address"}},
|
|
||||||
{"help", nil},
|
|
||||||
{"help", []string{"print-cert"}},
|
|
||||||
{"help", []string{"logout"}},
|
|
||||||
{"logout", []string{"-h"}},
|
|
||||||
{"not-a-command", []string{"10.222.0.9"}},
|
|
||||||
// reload is last so that whatever a reload does to the daemon cannot leak into an
|
|
||||||
// earlier case. In this harness it fails: the config was loaded from a string and
|
|
||||||
// has no path to re-read, which is what the recorded result says.
|
|
||||||
{"reload", nil},
|
|
||||||
}
|
|
||||||
|
|
||||||
got := &strings.Builder{}
|
|
||||||
for _, c := range cases {
|
|
||||||
for _, fmtFlags := range [][]string{nil, {"-json"}, {"-pretty"}} {
|
|
||||||
args := append([]string{c.name}, fmtFlags...)
|
|
||||||
args = append(args, c.args...)
|
|
||||||
|
|
||||||
fmt.Fprintf(got, "$ %s\n", strings.Join(args, " "))
|
|
||||||
out, err := run(args...)
|
|
||||||
out = normalise(out)
|
|
||||||
got.WriteString(out)
|
|
||||||
// Output that does not end in a newline is recorded as such, the way diff marks
|
|
||||||
// it, so the golden sees that byte too and the next prompt still starts a line.
|
|
||||||
if out != "" && !strings.HasSuffix(out, "\n") {
|
|
||||||
got.WriteString("\\ No newline at end of output\n")
|
|
||||||
}
|
|
||||||
// A failed command is recorded as the exit status an ssh caller sees, so the
|
|
||||||
// golden pins which outcomes count as failures.
|
|
||||||
if err != nil {
|
|
||||||
got.WriteString("exit status 1\n")
|
|
||||||
}
|
|
||||||
got.WriteString("\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
test.Golden(t, filepath.Join("testdata", "commands_output.golden"), got.String())
|
|
||||||
}
|
|
||||||
@@ -1513,7 +1513,7 @@ func TestLighthouseUpdateOnReload(t *testing.T) {
|
|||||||
// Reload the config. The lighthouse.hosts change triggers TriggerUpdate,
|
// Reload the config. The lighthouse.hosts change triggers TriggerUpdate,
|
||||||
// which wakes the update worker. It calls SendUpdate, initiating a
|
// which wakes the update worker. It calls SendUpdate, initiating a
|
||||||
// handshake to the new lighthouse and caching the HostUpdateNotification.
|
// handshake to the new lighthouse and caching the HostUpdateNotification.
|
||||||
require.NoError(t, myConfig.ReloadConfigString(string(newCfg)).Err)
|
require.NoError(t, myConfig.ReloadConfigString(string(newCfg)))
|
||||||
|
|
||||||
// Route until the lighthouse receives the HostUpdateNotification.
|
// Route until the lighthouse receives the HostUpdateNotification.
|
||||||
// This covers: handshake stage 1, stage 2, then the cached update.
|
// This covers: handshake stage 1, stage 2, then the cached update.
|
||||||
|
|||||||
@@ -0,0 +1,396 @@
|
|||||||
|
//go:build e2e_testing
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/e2e/router"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func pqRequire() m {
|
||||||
|
return m{"handshakes": m{"pq": m{"require": true, "repair_delay": "0s"}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pqConfig(initiate, accept, sticky bool) m {
|
||||||
|
return m{"handshakes": m{"pq": m{
|
||||||
|
"initiate": initiate,
|
||||||
|
"accept": accept,
|
||||||
|
"sticky": sticky,
|
||||||
|
"repair_delay": "0s",
|
||||||
|
}}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handshakeOf returns the handshake header p carries, looking inside relay
|
||||||
|
// wrapping, plus the fragment header when the handshake is fragmented.
|
||||||
|
func handshakeOf(p *udp.Packet) (*header.H, *fragment.Header) {
|
||||||
|
b := p.Data
|
||||||
|
h := &header.H{}
|
||||||
|
if h.Parse(b) != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if h.Type == header.Message && h.Subtype == header.MessageRelay {
|
||||||
|
// Relays authenticate but do not encrypt the inner packet.
|
||||||
|
b = b[header.Len:]
|
||||||
|
if h.Parse(b) != nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.Type != header.Handshake {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
if !header.IsFragmented(h.Subtype) {
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
// A relayed packet still has the relay's AEAD tag on the end.
|
||||||
|
for _, tag := range []int{0, 16} {
|
||||||
|
if len(b) < header.Len+tag {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if fh, _, err := fragment.Parse(b[header.Len : len(b)-tag]); err == nil {
|
||||||
|
return h, &fh
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handshakeTally counts handshake packets by style as the router moves them.
|
||||||
|
type handshakeTally struct {
|
||||||
|
sync.Mutex
|
||||||
|
pq, classical int
|
||||||
|
maxPQLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ht *handshakeTally) observe(p *udp.Packet) {
|
||||||
|
h, _ := handshakeOf(p)
|
||||||
|
if h == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ht.Lock()
|
||||||
|
defer ht.Unlock()
|
||||||
|
if h.Subtype == header.HandshakeIXHybridMLKEM1024 {
|
||||||
|
ht.pq++
|
||||||
|
ht.maxPQLen = max(ht.maxPQLen, len(p.Data))
|
||||||
|
} else {
|
||||||
|
ht.classical++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isTunnelData reports whether p is ordinary tunnel traffic headed for to,
|
||||||
|
// directly or as the last hop of a relay.
|
||||||
|
func isTunnelData(p *udp.Packet, receiver, to *nebula.Control) bool {
|
||||||
|
h := &header.H{}
|
||||||
|
if receiver != to || h.Parse(p.Data) != nil || h.Type != header.Message {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if h.Subtype == header.MessageRelay {
|
||||||
|
if h.Parse(p.Data[header.Len:]) != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return h.Type == header.Message && h.Subtype == header.MessageNone
|
||||||
|
}
|
||||||
|
|
||||||
|
// newPQPair builds two hosts with v3 certificates that also trust the CA's v2
|
||||||
|
// form, so a classical handshake can complete when the hybrid one does not.
|
||||||
|
func newPQPair(t *testing.T, mine, theirs m) (myControl, theirControl *nebula.Control, myVpn, theirVpn []netip.Prefix, myUdp, theirUdp netip.AddrPort, r *router.R) {
|
||||||
|
t.Helper()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
ca2, _ := ca.v2()
|
||||||
|
myControl, myVpn, myUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "me", "10.128.0.1/24", withCA(mine, ca.ca, ca2))
|
||||||
|
theirControl, theirVpn, theirUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "them", "10.128.0.2/24", withCA(theirs, ca.ca, ca2))
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
r = router.NewR(t, myControl, theirControl)
|
||||||
|
myControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQHandshakeLoss drops data fragments of both messages; the repair
|
||||||
|
// fragments must carry the handshake without a retry.
|
||||||
|
func TestPQHandshakeLoss(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, pqConfig(true, true, false), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
dropped := 0
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if h, fh := handshakeOf(p); fh != nil {
|
||||||
|
// Every data fragment of msg1, and the first of msg2.
|
||||||
|
if (h.MessageCounter == 1 && int(fh.Index) < 2) || (h.MessageCounter == 2 && fh.Index == 0) {
|
||||||
|
dropped++
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tally.observe(p)
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Equal(t, 3, dropped)
|
||||||
|
assert.Zero(t, tally.classical, "the first attempt should succeed from repair fragments")
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQFallback has a peer that does not accept PQ handshakes. The initiator's
|
||||||
|
// next attempt is classical and the tunnel comes up anyway.
|
||||||
|
func TestPQFallback(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, myUdp, theirUdp, r := newPQPair(t, pqConfig(true, true, false), nil)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Positive(t, tally.pq, "a PQ attempt comes first")
|
||||||
|
assert.Positive(t, tally.classical, "then a classical one")
|
||||||
|
|
||||||
|
assertHostInfoPair(t, myUdp, theirUdp, myVpn, theirVpn, myControl, theirControl)
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
assert.Empty(t, myControl.ListHostmapIndexes(true), "the losing PQ attempt's index must be released")
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQResendFreshSymbols loses the responder's whole first msg2. The
|
||||||
|
// initiator's next PQ attempt offers fresh msg1 symbols, the responder
|
||||||
|
// recognises the repeat and resends msg2 with symbols it has not sent before.
|
||||||
|
func TestPQResendFreshSymbols(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, pqConfig(true, true, false), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var firstMsg2, freshMsg2, freshMsg1 int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(10*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
h, fh := handshakeOf(p)
|
||||||
|
switch {
|
||||||
|
case h != nil && fh == nil:
|
||||||
|
// Keep the classical fallback out of the way.
|
||||||
|
return router.Drop
|
||||||
|
case fh != nil && h.MessageCounter == 2 && fh.Index < 12:
|
||||||
|
firstMsg2++
|
||||||
|
return router.Drop
|
||||||
|
case fh != nil && h.MessageCounter == 2:
|
||||||
|
freshMsg2++
|
||||||
|
case fh != nil && h.MessageCounter == 1 && fh.Index >= 10:
|
||||||
|
freshMsg1++
|
||||||
|
}
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Equal(t, 12, firstMsg2, "the first msg2 is 9 data and 3 repair fragments")
|
||||||
|
assert.Positive(t, freshMsg1, "the retry offers fresh msg1 symbols")
|
||||||
|
assert.Positive(t, freshMsg2, "the resend offers fresh msg2 symbols")
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQSticky checks a sticky initiator never offers a classical handshake to a
|
||||||
|
// peer it has already completed a PQ handshake with.
|
||||||
|
func TestPQSticky(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, pqConfig(true, true, true), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
theirControl.GetFromTun(true)
|
||||||
|
|
||||||
|
t.Log("Rehandshake while an attacker drops every PQ packet")
|
||||||
|
var tally handshakeTally
|
||||||
|
myControl.ReHandshake(theirVpn[0].Addr())
|
||||||
|
r.RouteForAllExitFuncOrTimeout(time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if h, _ := handshakeOf(p); h != nil && h.Subtype == header.HandshakeIXHybridMLKEM1024 {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
assert.Positive(t, tally.pq, "PQ attempts keep being made")
|
||||||
|
assert.Zero(t, tally.classical, "sticky must never fall back")
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQTimeoutReleasesIndexes lets a handshake with both a PQ and a classical
|
||||||
|
// attempt pending time out, and checks neither index is left behind.
|
||||||
|
func TestPQTimeoutReleasesIndexes(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
mine := pqConfig(true, true, false)
|
||||||
|
mine["handshakes"].(m)["retries"] = 2
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, mine, pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
r.RouteForAllExitFuncOrTimeout(1500*time.Millisecond, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
return router.Drop
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Positive(t, tally.pq)
|
||||||
|
assert.Positive(t, tally.classical)
|
||||||
|
assert.Nil(t, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), true), "the handshake should have timed out")
|
||||||
|
assert.Empty(t, myControl.ListHostmapIndexes(true))
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQCookieUnderLoad floods the responder with msg1 fragments from spoofed
|
||||||
|
// sources until it demands cookies, then checks a real initiator picks up the
|
||||||
|
// cookie and still completes a PQ handshake.
|
||||||
|
func TestPQCookieUnderLoad(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, theirUdp, r := newPQPair(t, pqConfig(true, true, false), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
// Half of the 1024 entry table is the cookie threshold. Stopping exactly there
|
||||||
|
// keeps them from answering the spoofed sources, which the router cannot reach.
|
||||||
|
t.Log("Flood them with the first fragment of msg1s nobody will finish")
|
||||||
|
for i := range 512 {
|
||||||
|
from := netip.AddrPortFrom(netip.AddrFrom4([4]byte{198, 18, byte(i >> 8), byte(i)}), 4242)
|
||||||
|
s, err := fragment.NewSender(make([]byte, 2000), header.HandshakeIXHybridMLKEM1024, 0, 1, uint32(i+1))
|
||||||
|
require.NoError(t, err)
|
||||||
|
pkts, _, err := s.Attempt(0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
theirControl.InjectUDPPacket(&udp.Packet{From: from, To: theirUdp, Data: pkts[0]})
|
||||||
|
}
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
var cookieReplies, msg2, withCookie int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if h, fh := handshakeOf(p); h != nil && h.Subtype == header.HandshakeIXHybridMLKEM1024 {
|
||||||
|
switch {
|
||||||
|
case h.MessageCounter == 0:
|
||||||
|
cookieReplies++
|
||||||
|
case h.MessageCounter == 2:
|
||||||
|
msg2++
|
||||||
|
case fh != nil && fh.HasCookie():
|
||||||
|
withCookie++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Positive(t, cookieReplies, "a loaded responder asks for a cookie")
|
||||||
|
assert.Positive(t, withCookie, "the initiator resends with it")
|
||||||
|
assert.Positive(t, msg2, "and the handshake completes post-quantum")
|
||||||
|
assert.Zero(t, tally.classical, "the cookie round trip beats the classical fallback")
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQRequire checks require mode never handshakes classically, in either
|
||||||
|
// direction.
|
||||||
|
func TestPQRequire(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
t.Run("initiator", func(t *testing.T) {
|
||||||
|
// They neither speak PQ nor require it; we never fall back.
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, pqRequire(), nil)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
r.RouteForAllExitFuncOrTimeout(time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
assert.Positive(t, tally.pq)
|
||||||
|
assert.Zero(t, tally.classical)
|
||||||
|
assert.Nil(t, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false), "no tunnel without PQ")
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("responder", func(t *testing.T) {
|
||||||
|
// We only speak classical; they refuse it.
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, nil, pqRequire())
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var answers int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
r.RouteForAllExitFuncOrTimeout(time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if h, _ := handshakeOf(p); h != nil && h.MessageCounter == 2 {
|
||||||
|
answers++
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
assert.Zero(t, answers, "a classical msg1 gets no answer")
|
||||||
|
assert.Nil(t, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false))
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("both", func(t *testing.T) {
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newPQPair(t, pqRequire(), pqRequire())
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally handshakeTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if isTunnelData(p, receiver, theirControl) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Zero(t, tally.classical)
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -0,0 +1,708 @@
|
|||||||
|
//go:build e2e_testing
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/cert_test"
|
||||||
|
"github.com/slackhq/nebula/e2e/router"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// hybridTally counts handshake packets by subtype and message.
|
||||||
|
type hybridTally struct {
|
||||||
|
sync.Mutex
|
||||||
|
hybrid, classical int
|
||||||
|
msg3 int
|
||||||
|
maxLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ht *hybridTally) observe(p *udp.Packet) {
|
||||||
|
h, _ := handshakeOf(p)
|
||||||
|
if h == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ht.Lock()
|
||||||
|
defer ht.Unlock()
|
||||||
|
switch h.Subtype {
|
||||||
|
case header.HandshakeIXHybridMLKEM1024:
|
||||||
|
ht.hybrid++
|
||||||
|
ht.maxLen = max(ht.maxLen, len(p.Data))
|
||||||
|
if h.MessageCounter == 3 {
|
||||||
|
ht.msg3++
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
ht.classical++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isType(p *udp.Packet, t header.MessageType) bool {
|
||||||
|
h := &header.H{}
|
||||||
|
return h.Parse(p.Data) == nil && h.Type == t
|
||||||
|
}
|
||||||
|
|
||||||
|
type hybridCA struct {
|
||||||
|
ca cert.Certificate
|
||||||
|
key []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridCA(t *testing.T) hybridCA {
|
||||||
|
t.Helper()
|
||||||
|
ca, _, key, _ := cert_test.NewTestCaCert(cert.Version3, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
return hybridCA{ca: ca, key: key}
|
||||||
|
}
|
||||||
|
|
||||||
|
// v2 returns the v2 form of the CA and its classical key, what a node that only has v2 certificates gets.
|
||||||
|
func (h hybridCA) v2() (cert.Certificate, []byte) {
|
||||||
|
return cert.V2Form(h.ca), h.key[:len(h.key)-cert.MLDSA87PrivateKeySize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// trust returns a pki.ca override trusting the given CAs.
|
||||||
|
func trust(cas ...cert.Certificate) string {
|
||||||
|
var s string
|
||||||
|
for _, c := range cas {
|
||||||
|
b, err := c.MarshalPEM()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
s += string(b)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func withCA(cfg m, cas ...cert.Certificate) m {
|
||||||
|
if cfg == nil {
|
||||||
|
cfg = m{}
|
||||||
|
}
|
||||||
|
cfg["pki"] = m{"ca": trust(cas...)}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridPair(t *testing.T, mine, theirs m) (myControl, theirControl *nebula.Control, myVpn, theirVpn []netip.Prefix, myUdp, theirUdp netip.AddrPort, r *router.R) {
|
||||||
|
t.Helper()
|
||||||
|
return newHybridPairWithCA(t, newHybridCA(t), mine, theirs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridPairWithCA(t *testing.T, ca hybridCA, mine, theirs m) (myControl, theirControl *nebula.Control, myVpn, theirVpn []netip.Prefix, myUdp, theirUdp netip.AddrPort, r *router.R) {
|
||||||
|
t.Helper()
|
||||||
|
myControl, myVpn, myUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "me", "10.128.0.1/24", mine)
|
||||||
|
theirControl, theirVpn, theirUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "them", "10.128.0.2/24", theirs)
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
r = router.NewR(t, myControl, theirControl)
|
||||||
|
myControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func routeUntilData(t *testing.T, r *router.R, to *nebula.Control, timeout time.Duration, fn func(p *udp.Packet, receiver *nebula.Control) router.ExitType) bool {
|
||||||
|
t.Helper()
|
||||||
|
return r.RouteForAllExitFuncOrTimeout(timeout, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if fn != nil {
|
||||||
|
if e := fn(p, receiver); e != router.KeepRouting {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isTunnelData(p, receiver, to) {
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHybridHandshake(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, myUdp, theirUdp, r := newHybridPair(t, pqConfig(true, true, false), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
var recvErrors int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if isType(p, header.RecvError) {
|
||||||
|
recvErrors++
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Zero(t, tally.classical, "a node with a v3 certificate speaks the hybrid handshake")
|
||||||
|
assert.GreaterOrEqual(t, tally.hybrid, 7+9+2, "all three messages travel as several fragments")
|
||||||
|
assert.Positive(t, tally.msg3)
|
||||||
|
assert.LessOrEqual(t, tally.maxLen, fragment.MaxPacketLen)
|
||||||
|
assert.Zero(t, recvErrors)
|
||||||
|
|
||||||
|
assertHostInfoPair(t, myUdp, theirUdp, myVpn, theirVpn, myControl, theirControl)
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
for _, c := range []*nebula.Control{myControl, theirControl} {
|
||||||
|
assert.Empty(t, c.ListHostmapIndexes(true), "no pending index may outlive the handshake")
|
||||||
|
}
|
||||||
|
assert.Equal(t, cert.Version3, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false).Cert.Version())
|
||||||
|
assert.Equal(t, cert.Version3, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false).Cert.Version())
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridConfirmationLost loses the responder's confirmation. The initiator
|
||||||
|
// keeps resending msg3, and the responder, already done, confirms again rather
|
||||||
|
// than leave the initiator to tear the tunnel down.
|
||||||
|
func TestHybridConfirmationLost(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
// Three retries make the handshake timeout 400ms, so the wait below outlasts it.
|
||||||
|
mine := pqConfig(true, true, false)
|
||||||
|
mine["handshakes"].(m)["retries"] = 3
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newHybridPair(t, mine, pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
var droppedConfirms, confirms int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
r.RouteForAllExitFuncOrTimeout(2*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if receiver == myControl && isType(p, header.Test) {
|
||||||
|
if droppedConfirms == 0 {
|
||||||
|
droppedConfirms++
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
confirms++
|
||||||
|
return router.RouteAndExit
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Equal(t, 1, droppedConfirms)
|
||||||
|
assert.Positive(t, confirms, "the responder confirms again")
|
||||||
|
assert.Greater(t, tally.msg3, 4, "msg3 was resent with fresh symbols")
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
|
||||||
|
// Long past the handshake timeout, the tunnel must still be there.
|
||||||
|
time.Sleep(700 * time.Millisecond)
|
||||||
|
r.FlushAll()
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridNeverConfirmed loses every msg3. The responder never completes, must
|
||||||
|
// not answer the initiator's data with RecvErrors meanwhile, and the initiator
|
||||||
|
// closes the tunnel at the handshake timeout.
|
||||||
|
func TestHybridNeverConfirmed(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
mine := pqConfig(true, true, false)
|
||||||
|
mine["handshakes"].(m)["retries"] = 3
|
||||||
|
theirs := pqConfig(false, true, false)
|
||||||
|
theirs["handshakes"].(m)["retries"] = 3
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newHybridPair(t, mine, theirs)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var recvErrors, dropped int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
r.RouteForAllExitFuncOrTimeout(1500*time.Millisecond, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if h, fh := handshakeOf(p); fh != nil && h.MessageCounter == 3 {
|
||||||
|
dropped++
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
if isType(p, header.RecvError) {
|
||||||
|
recvErrors++
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Greater(t, dropped, 4, "msg3 is resent")
|
||||||
|
assert.Zero(t, recvErrors, "data for a pending responder is dropped quietly")
|
||||||
|
assert.Nil(t, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false), "the responder never completes")
|
||||||
|
assert.Nil(t, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false), "the initiator gave up on the tunnel")
|
||||||
|
assert.Empty(t, theirControl.ListHostmapIndexes(true), "the pending responder expired")
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridToV2Peer has a v3 initiator meet a peer that holds only a v2
|
||||||
|
// certificate. Its post-quantum settings do nothing without a v3 certificate, so
|
||||||
|
// the tunnel comes up classical on the v2 form.
|
||||||
|
func TestHybridToV2Peer(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
ca2, key2 := ca.v2()
|
||||||
|
myControl, myVpn, myUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "me", "10.128.0.1/24", withCA(pqConfig(true, true, false), ca.ca, ca2))
|
||||||
|
theirControl, theirVpn, theirUdp, _ := newSimpleServer(cert.Version2, ca2, key2, "them", "10.128.0.2/24", pqConfig(true, true, false))
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
r := router.NewR(t, myControl, theirControl)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
myControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Positive(t, tally.hybrid, "the hybrid attempt comes first")
|
||||||
|
assert.Positive(t, tally.classical)
|
||||||
|
assertHostInfoPair(t, myUdp, theirUdp, myVpn, theirVpn, myControl, theirControl)
|
||||||
|
assert.Equal(t, cert.Version2, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false).Cert.Version())
|
||||||
|
assert.Empty(t, myControl.ListHostmapIndexes(true))
|
||||||
|
assert.Empty(t, theirControl.ListHostmapIndexes(true))
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridDistrustedV3 has an initiator that trusts only the v2 form of the CA.
|
||||||
|
// Its hybrid attempt fails verifying the responder's v3 certificate, which must
|
||||||
|
// leave the classical attempt going rather than end the handshake.
|
||||||
|
func TestHybridDistrustedV3(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
ca2, _ := ca.v2()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newHybridPairWithCA(t, ca,
|
||||||
|
withCA(pqConfig(true, true, false), ca2),
|
||||||
|
withCA(pqConfig(false, true, false), ca.ca, ca2),
|
||||||
|
)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Positive(t, tally.hybrid)
|
||||||
|
assert.Positive(t, tally.classical)
|
||||||
|
assert.Equal(t, cert.Version2, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false).Cert.Version())
|
||||||
|
assert.Empty(t, myControl.ListHostmapIndexes(true))
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridRequire checks require with a v3 certificate means hybrid only.
|
||||||
|
func TestHybridRequire(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
t.Run("both", func(t *testing.T) {
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newHybridPair(t, pqRequire(), pqRequire())
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.Zero(t, tally.classical)
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHybridRelays(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
useRelays := pqConfig(true, true, false)
|
||||||
|
useRelays["relay"] = m{"use_relays": true}
|
||||||
|
amRelay := pqConfig(true, true, false)
|
||||||
|
amRelay["relay"] = m{"am_relay": true}
|
||||||
|
myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "me ", "10.128.0.1/24", useRelays)
|
||||||
|
relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "relay ", "10.128.0.128/24", amRelay)
|
||||||
|
theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "them ", "10.128.0.2/24", useRelays)
|
||||||
|
|
||||||
|
myControl.InjectLightHouseAddr(relayVpnIpNet[0].Addr(), relayUdpAddr)
|
||||||
|
myControl.InjectRelays(theirVpnIpNet[0].Addr(), []netip.Addr{relayVpnIpNet[0].Addr()})
|
||||||
|
relayControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
|
||||||
|
|
||||||
|
r := router.NewR(t, myControl, relayControl, theirControl)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
myControl.Start()
|
||||||
|
relayControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
|
||||||
|
var relayedMsg3, relayedClassical int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpnIpNet[0].Addr(), 80, myVpnIpNet[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 10*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
outer := &header.H{}
|
||||||
|
if outer.Parse(p.Data) == nil && outer.Type == header.Message && outer.Subtype == header.MessageRelay {
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh != nil {
|
||||||
|
assert.LessOrEqual(t, len(p.Data), fragment.MaxPacketLen+32, "a relayed fragment fits the IPv6 minimum MTU")
|
||||||
|
if h.MessageCounter == 3 {
|
||||||
|
relayedMsg3++
|
||||||
|
}
|
||||||
|
} else if h != nil {
|
||||||
|
relayedClassical++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), 80, 80)
|
||||||
|
assert.Positive(t, relayedMsg3, "msg3 goes through the relay too")
|
||||||
|
assert.Zero(t, relayedClassical)
|
||||||
|
r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
relayControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridBackToBack rebuilds the tunnel more times than the responder's
|
||||||
|
// per-source quota allows messages in progress, faster than its table expires
|
||||||
|
// entries. The repair fragments trailing each completed msg1 must not count
|
||||||
|
// against that quota, or later handshakes fall back to classical.
|
||||||
|
func TestHybridBackToBack(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, theirUdp, r := newHybridPair(t, pqConfig(true, true, false), pqConfig(false, true, false))
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
for i := range 6 {
|
||||||
|
// Closing the last tunnel forgets the peer's address.
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, nil), "tunnel %d never came up", i)
|
||||||
|
theirControl.GetFromTun(true)
|
||||||
|
// Let the trailing repair fragments land before tearing down.
|
||||||
|
r.RouteForAllExitFuncOrTimeout(50*time.Millisecond, func(*udp.Packet, *nebula.Control) router.ExitType { return router.KeepRouting })
|
||||||
|
|
||||||
|
mine := myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false)
|
||||||
|
theirs := theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false)
|
||||||
|
require.NotNil(t, mine)
|
||||||
|
require.NotNil(t, theirs)
|
||||||
|
assert.Equal(t, cert.Version3, mine.Cert.Version(), "tunnel %d is hybrid", i)
|
||||||
|
assert.Equal(t, cert.Version3, theirs.Cert.Version(), "tunnel %d is hybrid", i)
|
||||||
|
|
||||||
|
myControl.CloseTunnel(theirVpn[0].Addr(), true)
|
||||||
|
theirControl.CloseTunnel(myVpn[0].Addr(), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridRaceWithClassical has the initiator's classical attempt cross its
|
||||||
|
// hybrid one: the hybrid msg2 is held back until the classical msg1 has reached
|
||||||
|
// the responder, and the classical msg2 is lost, so the initiator commits to the
|
||||||
|
// hybrid handshake.
|
||||||
|
//
|
||||||
|
// Arriving within the crossing window, the classical msg1 goes unanswered and
|
||||||
|
// the responder ends up with the hybrid tunnel alone. Held past the window, as on
|
||||||
|
// a very slow path, the responder completes it, and must still take the hybrid
|
||||||
|
// tunnel when msg3 arrives even though the classical one is newer.
|
||||||
|
func TestHybridRaceWithClassical(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
delay time.Duration
|
||||||
|
answered bool
|
||||||
|
}{
|
||||||
|
{"within the window", 0, false},
|
||||||
|
{"past the window", 500 * time.Millisecond, true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
// Both trust the v2 form of the CA too, or the classical attempt could not complete.
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
ca2, _ := ca.v2()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, _, _, r := newHybridPairWithCA(t, ca,
|
||||||
|
withCA(pqConfig(true, true, false), ca.ca, ca2),
|
||||||
|
withCA(pqConfig(false, true, false), ca.ca, ca2),
|
||||||
|
)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
var heldMsg2 []*udp.Packet
|
||||||
|
var classicalMsg1 *udp.Packet
|
||||||
|
holdMsg2 := func(p *udp.Packet) bool {
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh != nil && h.MessageCounter == 2 {
|
||||||
|
heldMsg2 = append(heldMsg2, p.Copy())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, r.RouteForAllExitFuncOrTimeout(3*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if holdMsg2(p) {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh == nil && h.MessageCounter == 1 {
|
||||||
|
classicalMsg1 = p.Copy()
|
||||||
|
return router.ExitNow
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "no classical attempt")
|
||||||
|
|
||||||
|
time.Sleep(tc.delay)
|
||||||
|
r.InjectUDPPacket(myControl, theirControl, classicalMsg1)
|
||||||
|
|
||||||
|
// Lose any classical msg2, so the initiator only ever completes the hybrid
|
||||||
|
// handshake. With msg2 held the initiator keeps retrying, and a later
|
||||||
|
// classical retry may land past the window; only the crossing one counts.
|
||||||
|
var classicalMsg2 int
|
||||||
|
r.RouteForAllExitFuncOrTimeout(50*time.Millisecond, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh == nil && h.MessageCounter == 1 {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh == nil && h.MessageCounter == 2 {
|
||||||
|
classicalMsg2++
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
if holdMsg2(p) {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
if tc.answered {
|
||||||
|
require.Positive(t, classicalMsg2, "past the window the classical msg1 is answered")
|
||||||
|
require.NotNil(t, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false))
|
||||||
|
} else {
|
||||||
|
require.Zero(t, classicalMsg2, "within the window the classical msg1 goes unanswered")
|
||||||
|
require.Nil(t, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false))
|
||||||
|
}
|
||||||
|
|
||||||
|
var recvErrors int
|
||||||
|
for _, p := range heldMsg2 {
|
||||||
|
r.InjectUDPPacket(theirControl, myControl, p)
|
||||||
|
}
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 3*time.Second, func(p *udp.Packet, _ *nebula.Control) router.ExitType {
|
||||||
|
if isType(p, header.RecvError) {
|
||||||
|
recvErrors++
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "data never arrived")
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
|
||||||
|
assert.Zero(t, recvErrors)
|
||||||
|
mine := myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false)
|
||||||
|
theirs := theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false)
|
||||||
|
require.NotNil(t, mine)
|
||||||
|
require.NotNil(t, theirs)
|
||||||
|
assert.Equal(t, cert.Version3, mine.Cert.Version())
|
||||||
|
assert.Equal(t, cert.Version3, theirs.Cert.Version(), "the responder's primary tunnel is the hybrid one")
|
||||||
|
assert.Equal(t, mine.LocalIndex, theirs.RemoteIndex)
|
||||||
|
if !tc.answered {
|
||||||
|
assert.Len(t, theirControl.ListHostmapIndexes(false), 1, "and the only one")
|
||||||
|
}
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridWrongResponder gives the initiator a stale address for its peer
|
||||||
|
// that belongs to another v3 host now, so both answer its msg1. Their msg2
|
||||||
|
// bursts interleave at the initiator and each must fill its own slot: the
|
||||||
|
// tunnel to the right host comes up within two attempts, one if its answer
|
||||||
|
// completes first and two if the other host's does, which the initiator
|
||||||
|
// blocks and retries past as it would for a classical handshake. With require
|
||||||
|
// there is no classical turn to fall back on, so a single slot that emptied
|
||||||
|
// on every FragID change would run to the handshake timeout.
|
||||||
|
func TestHybridWrongResponder(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
cfg m
|
||||||
|
pqOnly bool
|
||||||
|
}{
|
||||||
|
{"taking turns", pqConfig(true, true, false), false},
|
||||||
|
{"pq only", pqRequire(), true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
myControl, myVpn, myUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "me", "10.128.0.1/24", tc.cfg)
|
||||||
|
theirControl, theirVpn, theirUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "them", "10.128.0.2/24", tc.cfg)
|
||||||
|
otherControl, otherVpn, otherUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "other", "10.128.0.3/24", tc.cfg)
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), otherUdp)
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
|
||||||
|
r := router.NewR(t, myControl, theirControl, otherControl)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
myControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
otherControl.Start()
|
||||||
|
|
||||||
|
var tally hybridTally
|
||||||
|
msg1ToThem := 0
|
||||||
|
msg2ByFragID := map[uint32]int{}
|
||||||
|
var classical []string
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 5*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
tally.observe(p)
|
||||||
|
if h, fh := handshakeOf(p); h != nil {
|
||||||
|
switch {
|
||||||
|
case fh == nil:
|
||||||
|
classical = append(classical, fmt.Sprintf("counter=%d to=%s", h.MessageCounter, p.To))
|
||||||
|
case h.MessageCounter == 1 && p.To == theirUdp:
|
||||||
|
msg1ToThem++
|
||||||
|
case h.MessageCounter == 2 && receiver == myControl:
|
||||||
|
msg2ByFragID[fh.FragID]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
t.Logf("msg1 fragments to them: %d, msg2 fragments by FragID: %v, classical: %v", msg1ToThem, msg2ByFragID, classical)
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.GreaterOrEqual(t, len(msg2ByFragID), 2, "both hosts answered")
|
||||||
|
// A responder only resends msg2 when the initiator retries, so every
|
||||||
|
// FragID staying at one burst means each interleaved answer completed in
|
||||||
|
// its own slot. (The msg1 count is not a clean measure: a restart after
|
||||||
|
// the wrong host answers leaves the old handshake's timer entry in place,
|
||||||
|
// so the new one ticks twice, a few milliseconds apart.)
|
||||||
|
for fragID, n := range msg2ByFragID {
|
||||||
|
assert.LessOrEqual(t, n, 9+3, "responder %d had to resend msg2", fragID)
|
||||||
|
}
|
||||||
|
if tc.pqOnly {
|
||||||
|
assert.Empty(t, classical, "require never offers a classical attempt")
|
||||||
|
}
|
||||||
|
|
||||||
|
assertHostInfoPair(t, myUdp, theirUdp, myVpn, theirVpn, myControl, theirControl)
|
||||||
|
assertTunnel(t, myVpn[0].Addr(), theirVpn[0].Addr(), myControl, theirControl, r)
|
||||||
|
assert.Nil(t, myControl.GetHostInfoByVpnAddr(otherVpn[0].Addr(), false), "no tunnel to the other host")
|
||||||
|
assert.Empty(t, myControl.ListHostmapIndexes(true), "no pending index may outlive the handshake")
|
||||||
|
assert.Equal(t, cert.Version3, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false).Cert.Version(), "the hybrid handshake won")
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
otherControl.Stop()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridMsg2LostDirect loses every msg2 fragment the responder sends
|
||||||
|
// directly, as a responder answering from the wrong interface would. msg1 also
|
||||||
|
// reaches it through a relay, and that copy must be answered on the relay as
|
||||||
|
// soon as it completes, not refused because the direct copy was just answered.
|
||||||
|
// The relay takes a few retry ticks to come up in this harness, so the check is
|
||||||
|
// that the first copy of msg1 through the relay is enough once it does.
|
||||||
|
func TestHybridMsg2LostDirect(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
useRelays := pqRequire()
|
||||||
|
useRelays["relay"] = m{"use_relays": true}
|
||||||
|
amRelay := pqRequire()
|
||||||
|
amRelay["relay"] = m{"am_relay": true}
|
||||||
|
myControl, myVpn, _, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "me ", "10.128.0.1/24", useRelays)
|
||||||
|
relayControl, relayVpn, relayUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "relay ", "10.128.0.128/24", amRelay)
|
||||||
|
theirControl, theirVpn, theirUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "them ", "10.128.0.2/24", useRelays)
|
||||||
|
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
myControl.InjectLightHouseAddr(relayVpn[0].Addr(), relayUdp)
|
||||||
|
myControl.InjectRelays(theirVpn[0].Addr(), []netip.Addr{relayVpn[0].Addr()})
|
||||||
|
relayControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
|
||||||
|
r := router.NewR(t, myControl, relayControl, theirControl)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
myControl.Start()
|
||||||
|
relayControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
|
||||||
|
var relayedMsg1, directMsg2, relayedMsg2 int
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 10*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
h, fh := handshakeOf(p)
|
||||||
|
if h == nil || fh == nil {
|
||||||
|
return router.KeepRouting
|
||||||
|
}
|
||||||
|
outer := &header.H{}
|
||||||
|
relayed := outer.Parse(p.Data) == nil && outer.Type == header.Message && outer.Subtype == header.MessageRelay
|
||||||
|
switch {
|
||||||
|
case h.MessageCounter == 1 && relayed && receiver == theirControl:
|
||||||
|
relayedMsg1++
|
||||||
|
case h.MessageCounter == 2 && !relayed && p.From == theirUdp && receiver == myControl:
|
||||||
|
directMsg2++
|
||||||
|
return router.Drop
|
||||||
|
case h.MessageCounter == 2 && relayed && receiver == myControl:
|
||||||
|
relayedMsg2++
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
assert.GreaterOrEqual(t, directMsg2, 9, "the direct answer was sent and lost")
|
||||||
|
assert.GreaterOrEqual(t, relayedMsg2, 9, "the relayed copy of msg1 got its own answer")
|
||||||
|
assert.LessOrEqual(t, relayedMsg1, 7+3, "the first copy of msg1 through the relay is answered, no relayed retry is needed")
|
||||||
|
r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
relayControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHybridOneWayDirect loses everything the responder sends directly, not
|
||||||
|
// just msg2, so only the relay carries anything back. msg3 comes by the relay,
|
||||||
|
// since that is where msg2 got through, and the responder must then send by the
|
||||||
|
// relay too: its confirmation, and its replies. Keeping the direct address it
|
||||||
|
// learned from msg1 would lose both, and the initiator would close the tunnel as
|
||||||
|
// never confirmed.
|
||||||
|
func TestHybridOneWayDirect(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
useRelays := pqRequire()
|
||||||
|
useRelays["relay"] = m{"use_relays": true}
|
||||||
|
// The relay takes about a second to come up in this harness; 8 retries make the
|
||||||
|
// handshake timeout 3.6s, which the end of the test waits out.
|
||||||
|
useRelays["handshakes"].(m)["retries"] = 8
|
||||||
|
amRelay := pqRequire()
|
||||||
|
amRelay["relay"] = m{"am_relay": true}
|
||||||
|
myControl, myVpn, _, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "me ", "10.128.0.1/24", useRelays)
|
||||||
|
relayControl, relayVpn, relayUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "relay ", "10.128.0.128/24", amRelay)
|
||||||
|
theirControl, theirVpn, theirUdp, _ := newSimpleServer(cert.Version3, ca.ca, ca.key, "them ", "10.128.0.2/24", useRelays)
|
||||||
|
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
myControl.InjectLightHouseAddr(relayVpn[0].Addr(), relayUdp)
|
||||||
|
myControl.InjectRelays(theirVpn[0].Addr(), []netip.Addr{relayVpn[0].Addr()})
|
||||||
|
relayControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
|
||||||
|
r := router.NewR(t, myControl, relayControl, theirControl)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
myControl.Start()
|
||||||
|
relayControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
|
||||||
|
oneWay := func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if p.From == theirUdp && receiver == myControl {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}
|
||||||
|
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("Hi from me")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 10*time.Second, oneWay), "tunnel never came up")
|
||||||
|
assertUdpPacket(t, []byte("Hi from me"), theirControl.GetFromTun(true), myVpn[0].Addr(), theirVpn[0].Addr(), 80, 80)
|
||||||
|
|
||||||
|
theirControl.InjectTunPacket(BuildTunUDPPacket(myVpn[0].Addr(), 80, theirVpn[0].Addr(), 80, []byte("Hi from them")))
|
||||||
|
require.True(t, routeUntilData(t, r, myControl, 3*time.Second, oneWay), "the reply never came back")
|
||||||
|
assertUdpPacket(t, []byte("Hi from them"), myControl.GetFromTun(true), theirVpn[0].Addr(), myVpn[0].Addr(), 80, 80)
|
||||||
|
|
||||||
|
// Well past the handshake timeout, the initiator must still have the tunnel.
|
||||||
|
r.RouteForAllExitFuncOrTimeout(4*time.Second, oneWay)
|
||||||
|
mine := myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false)
|
||||||
|
require.NotNil(t, mine, "the initiator heard back, so it kept the tunnel")
|
||||||
|
assert.Equal(t, cert.Version3, mine.Cert.Version())
|
||||||
|
|
||||||
|
myControl.Stop()
|
||||||
|
relayControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
//go:build e2e_testing
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/e2e/router"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// relayedHybridTunnel brings up a hybrid tunnel whose msg2 completes by the
|
||||||
|
// relay: every direct msg2 fragment is lost, and drop decides what else is. It
|
||||||
|
// returns the controls with the tunnel up and the first packet delivered.
|
||||||
|
func relayedHybridTunnel(t *testing.T, drop func(p *udp.Packet, receiver *nebula.Control, them netip.AddrPort, me *nebula.Control) bool) (myControl, theirControl *nebula.Control, myVpn, theirVpn []netip.Prefix, myUdp, theirUdp netip.AddrPort, r *router.R) {
|
||||||
|
t.Helper()
|
||||||
|
ca := newHybridCA(t)
|
||||||
|
useRelays := pqRequire()
|
||||||
|
useRelays["relay"] = m{"use_relays": true}
|
||||||
|
useRelays["counters"] = m{"try_promote": 5}
|
||||||
|
amRelay := pqRequire()
|
||||||
|
amRelay["relay"] = m{"am_relay": true}
|
||||||
|
var relayControl *nebula.Control
|
||||||
|
var relayVpn []netip.Prefix
|
||||||
|
var relayUdp netip.AddrPort
|
||||||
|
myControl, myVpn, myUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "me ", "10.128.0.1/24", useRelays)
|
||||||
|
relayControl, relayVpn, relayUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "relay ", "10.128.0.128/24", amRelay)
|
||||||
|
theirControl, theirVpn, theirUdp, _ = newSimpleServer(cert.Version3, ca.ca, ca.key, "them ", "10.128.0.2/24", useRelays)
|
||||||
|
myControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
myControl.InjectLightHouseAddr(relayVpn[0].Addr(), relayUdp)
|
||||||
|
myControl.InjectRelays(theirVpn[0].Addr(), []netip.Addr{relayVpn[0].Addr()})
|
||||||
|
relayControl.InjectLightHouseAddr(theirVpn[0].Addr(), theirUdp)
|
||||||
|
r = router.NewR(t, myControl, relayControl, theirControl)
|
||||||
|
myControl.Start()
|
||||||
|
relayControl.Start()
|
||||||
|
theirControl.Start()
|
||||||
|
t.Cleanup(func() {
|
||||||
|
myControl.Stop()
|
||||||
|
relayControl.Stop()
|
||||||
|
theirControl.Stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("first")))
|
||||||
|
require.True(t, routeUntilData(t, r, theirControl, 10*time.Second, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
outer := &header.H{}
|
||||||
|
relayed := outer.Parse(p.Data) == nil && outer.Type == header.Message && outer.Subtype == header.MessageRelay
|
||||||
|
if h, fh := handshakeOf(p); h != nil && fh != nil && h.MessageCounter == 2 && !relayed && p.From == theirUdp && receiver == myControl {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
if drop(p, receiver, theirUdp, myControl) {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
}), "tunnel never came up")
|
||||||
|
theirControl.GetFromTun(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// exchange sends n packets each way, routing with drop, and reports how many
|
||||||
|
// arrived in each direction.
|
||||||
|
func exchange(t *testing.T, r *router.R, myControl, theirControl *nebula.Control, myVpn, theirVpn []netip.Prefix, theirUdp netip.AddrPort, n int, drop func(p *udp.Packet, receiver *nebula.Control, them netip.AddrPort, me *nebula.Control) bool) (toThem, toMe int) {
|
||||||
|
t.Helper()
|
||||||
|
route := func(to *nebula.Control) bool {
|
||||||
|
return routeUntilData(t, r, to, 300*time.Millisecond, func(p *udp.Packet, receiver *nebula.Control) router.ExitType {
|
||||||
|
if drop(p, receiver, theirUdp, myControl) {
|
||||||
|
return router.Drop
|
||||||
|
}
|
||||||
|
return router.KeepRouting
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for range n {
|
||||||
|
myControl.InjectTunPacket(BuildTunUDPPacket(theirVpn[0].Addr(), 80, myVpn[0].Addr(), 80, []byte("to them")))
|
||||||
|
if route(theirControl) {
|
||||||
|
theirControl.GetFromTun(true)
|
||||||
|
toThem++
|
||||||
|
}
|
||||||
|
theirControl.InjectTunPacket(BuildTunUDPPacket(myVpn[0].Addr(), 80, theirVpn[0].Addr(), 80, []byte("to me")))
|
||||||
|
if route(myControl) {
|
||||||
|
myControl.GetFromTun(true)
|
||||||
|
toMe++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func noDrop(*udp.Packet, *nebula.Control, netip.AddrPort, *nebula.Control) bool { return false }
|
||||||
|
|
||||||
|
// TestHybridRelayPromotion has msg2 complete by the relay although the direct
|
||||||
|
// path works both ways, as it often does on real networks where the tunnel to
|
||||||
|
// the relay is already up and the direct path still needs a punch. Both ends
|
||||||
|
// start relayed, and the usual promotion moves them to direct.
|
||||||
|
//
|
||||||
|
// With the direct path working only toward the responder, promotion's direct
|
||||||
|
// probe makes the responder roam onto it and lose its replies, hybrid or
|
||||||
|
// classical alike: roaming on receipt assumes a path works both ways. Roam path
|
||||||
|
// validation (claude/roam-path-validation) would hold the responder on the relay.
|
||||||
|
func TestHybridRelayPromotion(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
myControl, theirControl, myVpn, theirVpn, myUdp, theirUdp, r := relayedHybridTunnel(t, noDrop)
|
||||||
|
defer r.RenderFlow()
|
||||||
|
|
||||||
|
theirs := theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false)
|
||||||
|
require.NotNil(t, theirs)
|
||||||
|
assert.False(t, theirs.CurrentRemote.IsValid(), "msg3 came by the relay, so the responder starts relayed")
|
||||||
|
|
||||||
|
toThem, toMe := exchange(t, r, myControl, theirControl, myVpn, theirVpn, theirUdp, 20, noDrop)
|
||||||
|
assert.Equal(t, 20, toThem)
|
||||||
|
assert.Equal(t, 20, toMe)
|
||||||
|
assert.Equal(t, theirUdp, myControl.GetHostInfoByVpnAddr(theirVpn[0].Addr(), false).CurrentRemote, "the initiator promoted to direct")
|
||||||
|
assert.Equal(t, myUdp, theirControl.GetHostInfoByVpnAddr(myVpn[0].Addr(), false).CurrentRemote, "the responder promoted to direct")
|
||||||
|
}
|
||||||
Vendored
-720
@@ -1,720 +0,0 @@
|
|||||||
$ version
|
|
||||||
e2e-test
|
|
||||||
|
|
||||||
$ version -json
|
|
||||||
e2e-test
|
|
||||||
|
|
||||||
$ version -pretty
|
|
||||||
e2e-test
|
|
||||||
|
|
||||||
$ device-info
|
|
||||||
name= cidr=[10.222.0.1/24]
|
|
||||||
|
|
||||||
$ device-info -json
|
|
||||||
{"name":"","cidr":["10.222.0.1/24"]}
|
|
||||||
|
|
||||||
$ device-info -pretty
|
|
||||||
{
|
|
||||||
"name": "",
|
|
||||||
"cidr": [
|
|
||||||
"10.222.0.1/24"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
$ list-hostmap
|
|
||||||
|
|
||||||
$ list-hostmap -json
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-hostmap -pretty
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-hostmap -by-index
|
|
||||||
|
|
||||||
$ list-hostmap -json -by-index
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-hostmap -pretty -by-index
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-pending-hostmap
|
|
||||||
[10.222.0.9]: []
|
|
||||||
|
|
||||||
$ list-pending-hostmap -json
|
|
||||||
[{"vpnAddrs":["10.222.0.9"],"localIndex":<n>,"remoteIndex":<n>,"remoteAddrs":[],"cert":null,"messageCounter":<n>,"currentRemote":"","currentRelaysToMe":[],"currentRelaysThroughMe":[]}]
|
|
||||||
|
|
||||||
$ list-pending-hostmap -pretty
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"vpnAddrs": [
|
|
||||||
"10.222.0.9"
|
|
||||||
],
|
|
||||||
"localIndex": <n>,
|
|
||||||
"remoteIndex": <n>,
|
|
||||||
"remoteAddrs": [],
|
|
||||||
"cert": null,
|
|
||||||
"messageCounter": <n>,
|
|
||||||
"currentRemote": "",
|
|
||||||
"currentRelaysToMe": [],
|
|
||||||
"currentRelaysThroughMe": []
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
$ list-pending-hostmap -by-index
|
|
||||||
|
|
||||||
$ list-pending-hostmap -json -by-index
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-pending-hostmap -pretty -by-index
|
|
||||||
[]
|
|
||||||
|
|
||||||
$ list-lighthouse-addrmap
|
|
||||||
10.222.0.9: {}
|
|
||||||
|
|
||||||
$ list-lighthouse-addrmap -json
|
|
||||||
[{"vpnAddr":"10.222.0.9","addrs":{}}]
|
|
||||||
|
|
||||||
$ list-lighthouse-addrmap -pretty
|
|
||||||
[
|
|
||||||
{
|
|
||||||
"vpnAddr": "10.222.0.9",
|
|
||||||
"addrs": {}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
$ print-cert
|
|
||||||
{
|
|
||||||
"details": {
|
|
||||||
"curve": "CURVE25519",
|
|
||||||
"groups": [],
|
|
||||||
"isCa": false,
|
|
||||||
"issuer": "<hex>",
|
|
||||||
"name": "output-golden",
|
|
||||||
"networks": [
|
|
||||||
"10.222.0.1/24"
|
|
||||||
],
|
|
||||||
"notAfter": "<time>",
|
|
||||||
"notBefore": "<time>",
|
|
||||||
"publicKey": "<hex>",
|
|
||||||
"unsafeNetworks": []
|
|
||||||
},
|
|
||||||
"fingerprint": "<hex>",
|
|
||||||
"signature": "<hex>",
|
|
||||||
"version": 1
|
|
||||||
}
|
|
||||||
|
|
||||||
$ print-cert -json
|
|
||||||
{"details":{"curve":"CURVE25519","groups":[],"isCa":false,"issuer":"<hex>","name":"output-golden","networks":["10.222.0.1/24"],"notAfter":"<time>","notBefore":"<time>","publicKey":"<hex>","unsafeNetworks":[]},"fingerprint":"<hex>","signature":"<hex>","version":1}\ No newline at end of output
|
|
||||||
|
|
||||||
$ print-cert -pretty
|
|
||||||
{
|
|
||||||
"details": {
|
|
||||||
"curve": "CURVE25519",
|
|
||||||
"groups": [],
|
|
||||||
"isCa": false,
|
|
||||||
"issuer": "<hex>",
|
|
||||||
"name": "output-golden",
|
|
||||||
"networks": [
|
|
||||||
"10.222.0.1/24"
|
|
||||||
],
|
|
||||||
"notAfter": "<time>",
|
|
||||||
"notBefore": "<time>",
|
|
||||||
"publicKey": "<hex>",
|
|
||||||
"unsafeNetworks": []
|
|
||||||
},
|
|
||||||
"fingerprint": "<hex>",
|
|
||||||
"signature": "<hex>",
|
|
||||||
"version": 1
|
|
||||||
}\ No newline at end of output
|
|
||||||
|
|
||||||
$ print-cert -raw
|
|
||||||
-----BEGIN NEBULA CERTIFICATE-----
|
|
||||||
<pem>
|
|
||||||
-----END NEBULA CERTIFICATE-----
|
|
||||||
|
|
||||||
$ print-cert -json -raw
|
|
||||||
{"details":{"curve":"CURVE25519","groups":[],"isCa":false,"issuer":"<hex>","name":"output-golden","networks":["10.222.0.1/24"],"notAfter":"<time>","notBefore":"<time>","publicKey":"<hex>","unsafeNetworks":[]},"fingerprint":"<hex>","signature":"<hex>","version":1}\ No newline at end of output
|
|
||||||
|
|
||||||
$ print-cert -pretty -raw
|
|
||||||
{
|
|
||||||
"details": {
|
|
||||||
"curve": "CURVE25519",
|
|
||||||
"groups": [],
|
|
||||||
"isCa": false,
|
|
||||||
"issuer": "<hex>",
|
|
||||||
"name": "output-golden",
|
|
||||||
"networks": [
|
|
||||||
"10.222.0.1/24"
|
|
||||||
],
|
|
||||||
"notAfter": "<time>",
|
|
||||||
"notBefore": "<time>",
|
|
||||||
"publicKey": "<hex>",
|
|
||||||
"unsafeNetworks": []
|
|
||||||
},
|
|
||||||
"fingerprint": "<hex>",
|
|
||||||
"signature": "<hex>",
|
|
||||||
"version": 1
|
|
||||||
}\ No newline at end of output
|
|
||||||
|
|
||||||
$ print-cert 10.222.0.9
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -json 10.222.0.9
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -pretty 10.222.0.9
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert 10.222.0.250
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -json 10.222.0.250
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -pretty 10.222.0.250
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert not-an-address
|
|
||||||
The provided vpn addr could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -json not-an-address
|
|
||||||
The provided vpn addr could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-cert -pretty not-an-address
|
|
||||||
The provided vpn addr could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -json
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-pretty
|
|
||||||
pretty prints json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -pretty
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel 10.222.0.9
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -json 10.222.0.9
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-pretty
|
|
||||||
pretty prints json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -pretty 10.222.0.9
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel 10.222.0.250
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -json 10.222.0.250
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-pretty
|
|
||||||
pretty prints json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-tunnel -pretty 10.222.0.250
|
|
||||||
Could not find tunnel for vpn addr: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-relays
|
|
||||||
{"Relays":null}
|
|
||||||
|
|
||||||
$ print-relays -json
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-pretty
|
|
||||||
pretty prints json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ print-relays -pretty
|
|
||||||
{
|
|
||||||
"Relays": null
|
|
||||||
}
|
|
||||||
|
|
||||||
$ query-lighthouse
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse -json
|
|
||||||
The provided vpn address could not be parsed: -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse -pretty
|
|
||||||
The provided vpn address could not be parsed: -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse 10.222.0.9
|
|
||||||
{}
|
|
||||||
|
|
||||||
$ query-lighthouse -json 10.222.0.9
|
|
||||||
The provided vpn address could not be parsed: -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse -pretty 10.222.0.9
|
|
||||||
The provided vpn address could not be parsed: -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse not-an-address
|
|
||||||
The provided vpn address could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse -json not-an-address
|
|
||||||
The provided vpn address could not be parsed: -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ query-lighthouse -pretty not-an-address
|
|
||||||
The provided vpn address could not be parsed: -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -json
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -pretty
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level debug
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -json debug
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -pretty debug
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level bogus
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -json bogus
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -pretty bogus
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level info
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -json info
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-level -pretty info
|
|
||||||
Log level is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -json
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -pretty
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format json
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -json json
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -pretty json
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format text
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -json text
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ log-format -pretty text
|
|
||||||
Log format is not reconfigurable on this logger
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -json
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -pretty
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote 10.222.0.9
|
|
||||||
No address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -json 10.222.0.9
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -pretty 10.222.0.9
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -address 10.94.0.10:4242 10.222.0.9
|
|
||||||
Could not find tunnel for vpn address: 10.222.0.9
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -json -address 10.94.0.10:4242 10.222.0.9
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -pretty -address 10.94.0.10:4242 10.222.0.9
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -address 10.94.0.10:4242 10.222.0.250
|
|
||||||
Could not find tunnel for vpn address: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -json -address 10.94.0.10:4242 10.222.0.250
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ change-remote -pretty -address 10.94.0.10:4242 10.222.0.250
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
The new remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -json
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -pretty
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel 10.222.0.9
|
|
||||||
Tunnel already handshaking
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -json 10.222.0.9
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -pretty 10.222.0.9
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -address 10.94.0.10:4242 10.222.0.10
|
|
||||||
Created
|
|
||||||
|
|
||||||
$ create-tunnel -json -address 10.94.0.10:4242 10.222.0.10
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -pretty -address 10.94.0.10:4242 10.222.0.10
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel not-an-address
|
|
||||||
The provided vpn address could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -json not-an-address
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ create-tunnel -pretty not-an-address
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-address string
|
|
||||||
Optionally provide a real remote address, ip:port
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel
|
|
||||||
No vpn address was provided
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -json
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -pretty
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel 10.222.0.250
|
|
||||||
Could not find tunnel for vpn address: 10.222.0.250
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -json 10.222.0.250
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -pretty 10.222.0.250
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel not-an-address
|
|
||||||
The provided vpn address could not be parsed: not-an-address
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -json not-an-address
|
|
||||||
flag provided but not defined: -json
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ close-tunnel -pretty not-an-address
|
|
||||||
flag provided but not defined: -pretty
|
|
||||||
Usage:
|
|
||||||
-local-only
|
|
||||||
Disables notifying the remote that the tunnel is shutting down
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help
|
|
||||||
Available commands:
|
|
||||||
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
|
||||||
close-tunnel - Closes a tunnel for the provided vpn addr
|
|
||||||
create-tunnel - Creates a tunnel for the provided vpn address
|
|
||||||
device-info - Prints information about the network device.
|
|
||||||
help - prints available commands or help <command> for specific usage info
|
|
||||||
list-hostmap - List all known previously connected hosts
|
|
||||||
list-lighthouse-addrmap - List all lighthouse map entries
|
|
||||||
list-pending-hostmap - List all handshaking hosts
|
|
||||||
log-format - Gets or sets the current log format
|
|
||||||
log-level - Gets or sets the current log level
|
|
||||||
logout - Ends the current session
|
|
||||||
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
|
||||||
print-relays - Prints json details about all relay info
|
|
||||||
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
|
||||||
query-lighthouse - Query the lighthouses for the provided vpn address
|
|
||||||
reload - Reloads configuration from disk, same as sending HUP to the process
|
|
||||||
version - Prints the currently running version of nebula
|
|
||||||
|
|
||||||
|
|
||||||
$ help -json
|
|
||||||
Command not available -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help -pretty
|
|
||||||
Command not available -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help print-cert
|
|
||||||
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
|
||||||
-json
|
|
||||||
outputs as json
|
|
||||||
-pretty
|
|
||||||
pretty prints json, assumes -json
|
|
||||||
-raw
|
|
||||||
raw prints the PEM encoded certificate, not compatible with -json or -pretty
|
|
||||||
|
|
||||||
$ help -json print-cert
|
|
||||||
Command not available -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help -pretty print-cert
|
|
||||||
Command not available -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help logout
|
|
||||||
logout - Ends the current session
|
|
||||||
|
|
||||||
$ help -json logout
|
|
||||||
Command not available -json
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ help -pretty logout
|
|
||||||
Command not available -pretty
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ logout -h
|
|
||||||
logout - Ends the current session
|
|
||||||
|
|
||||||
$ logout -json -h
|
|
||||||
logout - Ends the current session
|
|
||||||
|
|
||||||
$ logout -pretty -h
|
|
||||||
logout - Ends the current session
|
|
||||||
|
|
||||||
$ not-a-command 10.222.0.9
|
|
||||||
did not understand: not-a-command
|
|
||||||
Available commands:
|
|
||||||
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
|
||||||
close-tunnel - Closes a tunnel for the provided vpn addr
|
|
||||||
create-tunnel - Creates a tunnel for the provided vpn address
|
|
||||||
device-info - Prints information about the network device.
|
|
||||||
help - prints available commands or help <command> for specific usage info
|
|
||||||
list-hostmap - List all known previously connected hosts
|
|
||||||
list-lighthouse-addrmap - List all lighthouse map entries
|
|
||||||
list-pending-hostmap - List all handshaking hosts
|
|
||||||
log-format - Gets or sets the current log format
|
|
||||||
log-level - Gets or sets the current log level
|
|
||||||
logout - Ends the current session
|
|
||||||
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
|
||||||
print-relays - Prints json details about all relay info
|
|
||||||
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
|
||||||
query-lighthouse - Query the lighthouses for the provided vpn address
|
|
||||||
reload - Reloads configuration from disk, same as sending HUP to the process
|
|
||||||
version - Prints the currently running version of nebula
|
|
||||||
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ not-a-command -json 10.222.0.9
|
|
||||||
did not understand: not-a-command
|
|
||||||
Available commands:
|
|
||||||
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
|
||||||
close-tunnel - Closes a tunnel for the provided vpn addr
|
|
||||||
create-tunnel - Creates a tunnel for the provided vpn address
|
|
||||||
device-info - Prints information about the network device.
|
|
||||||
help - prints available commands or help <command> for specific usage info
|
|
||||||
list-hostmap - List all known previously connected hosts
|
|
||||||
list-lighthouse-addrmap - List all lighthouse map entries
|
|
||||||
list-pending-hostmap - List all handshaking hosts
|
|
||||||
log-format - Gets or sets the current log format
|
|
||||||
log-level - Gets or sets the current log level
|
|
||||||
logout - Ends the current session
|
|
||||||
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
|
||||||
print-relays - Prints json details about all relay info
|
|
||||||
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
|
||||||
query-lighthouse - Query the lighthouses for the provided vpn address
|
|
||||||
reload - Reloads configuration from disk, same as sending HUP to the process
|
|
||||||
version - Prints the currently running version of nebula
|
|
||||||
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ not-a-command -pretty 10.222.0.9
|
|
||||||
did not understand: not-a-command
|
|
||||||
Available commands:
|
|
||||||
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
|
||||||
close-tunnel - Closes a tunnel for the provided vpn addr
|
|
||||||
create-tunnel - Creates a tunnel for the provided vpn address
|
|
||||||
device-info - Prints information about the network device.
|
|
||||||
help - prints available commands or help <command> for specific usage info
|
|
||||||
list-hostmap - List all known previously connected hosts
|
|
||||||
list-lighthouse-addrmap - List all lighthouse map entries
|
|
||||||
list-pending-hostmap - List all handshaking hosts
|
|
||||||
log-format - Gets or sets the current log format
|
|
||||||
log-level - Gets or sets the current log level
|
|
||||||
logout - Ends the current session
|
|
||||||
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
|
||||||
print-relays - Prints json details about all relay info
|
|
||||||
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
|
||||||
query-lighthouse - Query the lighthouses for the provided vpn address
|
|
||||||
reload - Reloads configuration from disk, same as sending HUP to the process
|
|
||||||
version - Prints the currently running version of nebula
|
|
||||||
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ reload
|
|
||||||
Reloading config
|
|
||||||
Config reload failed, the new config did not load: no config files found at
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ reload -json
|
|
||||||
Reloading config
|
|
||||||
Config reload failed, the new config did not load: no config files found at
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
$ reload -pretty
|
|
||||||
Reloading config
|
|
||||||
Config reload failed, the new config did not load: no config files found at
|
|
||||||
exit status 1
|
|
||||||
|
|
||||||
+2
-2
@@ -129,7 +129,7 @@ func TestCertUpgrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Logf("reload new v2-only config")
|
r.Logf("reload new v2-only config")
|
||||||
err = myC.ReloadConfigString(string(cb)).Err
|
err = myC.ReloadConfigString(string(cb))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
r.Log("yay, spin until their sees it")
|
r.Log("yay, spin until their sees it")
|
||||||
waitStart := time.Now()
|
waitStart := time.Now()
|
||||||
@@ -222,7 +222,7 @@ func TestCertDowngrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
r.Logf("reload new v1-only config")
|
r.Logf("reload new v1-only config")
|
||||||
err = myC.ReloadConfigString(string(cb)).Err
|
err = myC.ReloadConfigString(string(cb))
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
r.Log("yay, spin until their sees it")
|
r.Log("yay, spin until their sees it")
|
||||||
waitStart := time.Now()
|
waitStart := time.Now()
|
||||||
|
|||||||
+41
-20
@@ -4,11 +4,17 @@
|
|||||||
# PKI defines the location of credentials for this node. Each of these can also be inlined by using the yaml ": |" syntax.
|
# PKI defines the location of credentials for this node. Each of these can also be inlined by using the yaml ": |" syntax.
|
||||||
pki:
|
pki:
|
||||||
# The CAs that are accepted by this node. Must contain one or more certificates created by 'nebula-cert ca'
|
# The CAs that are accepted by this node. Must contain one or more certificates created by 'nebula-cert ca'
|
||||||
|
# A version 3 CA only accepts version 3 certificates, which only post-quantum hybrid handshakes use. To also
|
||||||
|
# accept version 2 certificates from it, list its v2 form as well (nebula-cert print shows its v2Fingerprint).
|
||||||
|
# Leaving the v2 form out is how a node requires post-quantum authentication.
|
||||||
ca: /etc/nebula/ca.crt
|
ca: /etc/nebula/ca.crt
|
||||||
|
# cert may hold a version 3 certificate, which also serves as its v2 form for classical handshakes. key then
|
||||||
|
# holds the version 3 key from 'nebula-cert keygen -version 3' or 'nebula-cert sign'.
|
||||||
cert: /etc/nebula/host.crt
|
cert: /etc/nebula/host.crt
|
||||||
key: /etc/nebula/host.key
|
key: /etc/nebula/host.key
|
||||||
# blocklist is a list of certificate fingerprints that we will refuse to talk to
|
# blocklist is a list of certificate fingerprints that we will refuse to talk to
|
||||||
# Entries are the hex fingerprints shown by `nebula-cert print`, an invalid entry fails startup and reload.
|
# A version 3 certificate has two fingerprints, its own and its v2 form's. Blocking the v2 one blocks both,
|
||||||
|
# blocking the v3 one leaves the v2 form usable in classical handshakes, so list both.
|
||||||
#blocklist:
|
#blocklist:
|
||||||
# - c99d4e650533b92061b09918e838a5a0a6aaee21eed1d12fd937682865936c72
|
# - c99d4e650533b92061b09918e838a5a0a6aaee21eed1d12fd937682865936c72
|
||||||
# disconnect_invalid is a toggle to force a client to be disconnected if the certificate is expired or invalid.
|
# disconnect_invalid is a toggle to force a client to be disconnected if the certificate is expired or invalid.
|
||||||
@@ -44,7 +50,7 @@ static_host_map:
|
|||||||
|
|
||||||
lighthouse:
|
lighthouse:
|
||||||
# am_lighthouse is used to enable lighthouse functionality for a node. This should ONLY be true on nodes
|
# am_lighthouse is used to enable lighthouse functionality for a node. This should ONLY be true on nodes
|
||||||
# you have configured to be lighthouses in your network. Not reloadable.
|
# you have configured to be lighthouses in your network
|
||||||
am_lighthouse: false
|
am_lighthouse: false
|
||||||
# serve_dns optionally starts a dns listener that responds to various queries and can even be
|
# serve_dns optionally starts a dns listener that responds to various queries and can even be
|
||||||
# delegated to for resolution
|
# delegated to for resolution
|
||||||
@@ -127,9 +133,8 @@ lighthouse:
|
|||||||
# Port Nebula will be listening on. The default here is 4242. For a lighthouse node, the port should be defined,
|
# Port Nebula will be listening on. The default here is 4242. For a lighthouse node, the port should be defined,
|
||||||
# however using port 0 will dynamically assign a port and is recommended for roaming nodes.
|
# however using port 0 will dynamically assign a port and is recommended for roaming nodes.
|
||||||
listen:
|
listen:
|
||||||
# To listen on only ipv4, use "0.0.0.0". Not reloadable.
|
# To listen on only ipv4, use "0.0.0.0"
|
||||||
host: "::"
|
host: "::"
|
||||||
# Not reloadable.
|
|
||||||
port: 4242
|
port: 4242
|
||||||
# Sets the max number of packets to pull from the kernel for each syscall (under systems that support recvmmsg)
|
# Sets the max number of packets to pull from the kernel for each syscall (under systems that support recvmmsg)
|
||||||
# default is 64, does not support reload
|
# default is 64, does not support reload
|
||||||
@@ -137,8 +142,7 @@ listen:
|
|||||||
# superpacket, so the receive scratch is batch * 64KiB per listening socket (~4MiB per routine at the
|
# superpacket, so the receive scratch is batch * 64KiB per listening socket (~4MiB per routine at the
|
||||||
# default of 64). Lower this to trade peak per-syscall throughput for memory on constrained hosts.
|
# default of 64). Lower this to trade peak per-syscall throughput for memory on constrained hosts.
|
||||||
#batch: 64
|
#batch: 64
|
||||||
# Linux only. Reloadable, but removing a value leaves the socket at the old size until Nebula restarts.
|
# Linux only. Configure socket buffers for the udp side (outside), leave unset to use the system defaults. Values will be doubled by the kernel
|
||||||
# Configure socket buffers for the udp side (outside), leave unset to use the system defaults. Values will be doubled by the kernel
|
|
||||||
# Default is net.core.rmem_default and net.core.wmem_default (/proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_default)
|
# Default is net.core.rmem_default and net.core.wmem_default (/proc/sys/net/core/rmem_default and /proc/sys/net/core/rmem_default)
|
||||||
# Maximum is limited by memory in the system, SO_RCVBUFFORCE and SO_SNDBUFFORCE is used to avoid having to raise the system wide
|
# Maximum is limited by memory in the system, SO_RCVBUFFORCE and SO_SNDBUFFORCE is used to avoid having to raise the system wide
|
||||||
# max, net.core.rmem_max and net.core.wmem_max
|
# max, net.core.rmem_max and net.core.wmem_max
|
||||||
@@ -182,7 +186,7 @@ listen:
|
|||||||
# Currently, this defaults to 1 which means we have 1 tun queue reader and 1
|
# Currently, this defaults to 1 which means we have 1 tun queue reader and 1
|
||||||
# UDP queue reader. Setting this above one will set IFF_MULTI_QUEUE on the tun
|
# UDP queue reader. Setting this above one will set IFF_MULTI_QUEUE on the tun
|
||||||
# device and SO_REUSEPORT on the UDP socket to allow multiple queues.
|
# device and SO_REUSEPORT on the UDP socket to allow multiple queues.
|
||||||
# This option is only supported on Linux. Not reloadable.
|
# This option is only supported on Linux.
|
||||||
#routines: 1
|
#routines: 1
|
||||||
|
|
||||||
punchy:
|
punchy:
|
||||||
@@ -206,7 +210,6 @@ punchy:
|
|||||||
|
|
||||||
# Cipher allows you to choose between the available ciphers for your network. Options are chachapoly or aes
|
# Cipher allows you to choose between the available ciphers for your network. Options are chachapoly or aes
|
||||||
# IMPORTANT: this value must be identical on ALL NODES/LIGHTHOUSES. We do not/will not support use of different ciphers simultaneously!
|
# IMPORTANT: this value must be identical on ALL NODES/LIGHTHOUSES. We do not/will not support use of different ciphers simultaneously!
|
||||||
# Not reloadable.
|
|
||||||
#cipher: aes
|
#cipher: aes
|
||||||
|
|
||||||
# Preferred ranges is used to define a hint about the local network ranges, which speeds up discovering the fastest
|
# Preferred ranges is used to define a hint about the local network ranges, which speeds up discovering the fastest
|
||||||
@@ -233,6 +236,12 @@ punchy:
|
|||||||
# Trusted SSH CA public keys. These are the public keys of the CAs that are allowed to sign SSH keys for access.
|
# Trusted SSH CA public keys. These are the public keys of the CAs that are allowed to sign SSH keys for access.
|
||||||
#trusted_cas:
|
#trusted_cas:
|
||||||
#- "ssh public key string"
|
#- "ssh public key string"
|
||||||
|
# sandbox_dir restricts file paths for profiling commands (start-cpu-profile, save-heap-profile,
|
||||||
|
# save-mutex-profile) to the specified directory. Relative paths will be resolved within this directory,
|
||||||
|
# and absolute paths outside of it will be rejected. Default is $TMP/nebula-debug.
|
||||||
|
# The directory is NOT automatically created.
|
||||||
|
# Overriding this to "" is the same as "/" and will allow overwriting any path on the host.
|
||||||
|
#sandbox_dir: /var/tmp/nebula-debug
|
||||||
|
|
||||||
# EXPERIMENTAL: relay support for networks that can't establish direct connections.
|
# EXPERIMENTAL: relay support for networks that can't establish direct connections.
|
||||||
relay:
|
relay:
|
||||||
@@ -251,25 +260,19 @@ relay:
|
|||||||
# Configure the private interface. Note: addr is baked into the nebula certificate
|
# Configure the private interface. Note: addr is baked into the nebula certificate
|
||||||
tun:
|
tun:
|
||||||
# When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root)
|
# When tun is disabled, a lighthouse can be started without a local tun interface (and therefore without root)
|
||||||
# Not reloadable.
|
|
||||||
disabled: false
|
disabled: false
|
||||||
# Name of the device. If not set, a default will be chosen by the OS.
|
# Name of the device. If not set, a default will be chosen by the OS.
|
||||||
# For macOS: if set, must be in the form `utun[0-9]+`.
|
# For macOS: if set, must be in the form `utun[0-9]+`.
|
||||||
# For OpenBSD and NetBSD: if set, must be in the form `tun[0-9]+`, otherwise the first free one is used.
|
# For NetBSD: Required to be set, must be in the form `tun[0-9]+`
|
||||||
# Not reloadable.
|
|
||||||
dev: nebula1
|
dev: nebula1
|
||||||
# Toggles forwarding of local broadcast packets, the address of which depends on the ip/mask encoded in pki.cert
|
# Toggles forwarding of local broadcast packets, the address of which depends on the ip/mask encoded in pki.cert
|
||||||
# Not reloadable.
|
|
||||||
drop_local_broadcast: false
|
drop_local_broadcast: false
|
||||||
# Toggles forwarding of multicast packets. Not reloadable.
|
# Toggles forwarding of multicast packets
|
||||||
drop_multicast: false
|
drop_multicast: false
|
||||||
# Sets the transmit queue length, if you notice lots of transmit drops on the tun it may help to raise this number. Default is 5000
|
# Sets the transmit queue length, if you notice lots of transmit drops on the tun it may help to raise this number. Default is 5000
|
||||||
# Not reloadable.
|
|
||||||
tx_queue: 5000
|
tx_queue: 5000
|
||||||
# Default MTU for every packet, safe setting is (and the default) 1300 for internet based traffic
|
# Default MTU for every packet, safe setting is (and the default) 1300 for internet based traffic
|
||||||
# Anything above 8937, here or on a route below, is capped to 8937 with a warning
|
# Anything above 8937, here or on a route below, is capped to 8937 with a warning
|
||||||
# Reloadable on Linux only. Where the tun is handed to nebula by a host app (iOS, Android, macOS network extensions)
|
|
||||||
# the host app owns the MTU.
|
|
||||||
mtu: 1300
|
mtu: 1300
|
||||||
|
|
||||||
# the use_offloads setting controls if Nebula will attempt to enable GSO and GRO for the tun device. Linux only, not reloadable.
|
# the use_offloads setting controls if Nebula will attempt to enable GSO and GRO for the tun device. Linux only, not reloadable.
|
||||||
@@ -362,7 +365,7 @@ tun:
|
|||||||
#use_system_route_table: false
|
#use_system_route_table: false
|
||||||
# Buffer size for reading routes updates. 0 means default system buffer size. (/proc/sys/net/core/rmem_default).
|
# Buffer size for reading routes updates. 0 means default system buffer size. (/proc/sys/net/core/rmem_default).
|
||||||
# If using massive routes updates, for example BGP, you may need to increase this value to avoid packet loss.
|
# If using massive routes updates, for example BGP, you may need to increase this value to avoid packet loss.
|
||||||
# SO_RCVBUFFORCE is used to avoid having to raise the system wide max. Not reloadable.
|
# SO_RCVBUFFORCE is used to avoid having to raise the system wide max
|
||||||
#use_system_route_table_buffer_size: 0
|
#use_system_route_table_buffer_size: 0
|
||||||
|
|
||||||
# Configure logging level
|
# Configure logging level
|
||||||
@@ -412,18 +415,36 @@ logging:
|
|||||||
# Handshakes are sent to all known addresses at each interval with a linear backoff,
|
# Handshakes are sent to all known addresses at each interval with a linear backoff,
|
||||||
# Wait try_interval after the 1st attempt, 2 * try_interval after the 2nd, etc, until the handshake is older than timeout
|
# Wait try_interval after the 1st attempt, 2 * try_interval after the 2nd, etc, until the handshake is older than timeout
|
||||||
# A 100ms interval with the default 10 retries will give a handshake 5.5 seconds to resolve before timing out
|
# A 100ms interval with the default 10 retries will give a handshake 5.5 seconds to resolve before timing out
|
||||||
# Not reloadable.
|
|
||||||
#try_interval: 100ms
|
#try_interval: 100ms
|
||||||
#retries: 20
|
#retries: 20
|
||||||
|
|
||||||
# query_buffer is the size of the buffer channel for querying lighthouses, drops are recorded in the lighthouse.query.dropped metric.
|
# query_buffer is the size of the buffer channel for querying lighthouses, drops are recorded in the lighthouse.query.dropped metric.
|
||||||
# Not reloadable.
|
|
||||||
#query_buffer: 1024
|
#query_buffer: 1024
|
||||||
|
|
||||||
# trigger_buffer is the size of the buffer channel for quickly sending handshakes
|
# trigger_buffer is the size of the buffer channel for quickly sending handshakes
|
||||||
# after receiving the response for lighthouse queries. Not reloadable.
|
# after receiving the response for lighthouse queries
|
||||||
#trigger_buffer: 64
|
#trigger_buffer: 64
|
||||||
|
|
||||||
|
# pq enables hybrid handshakes: ML-KEM-1024 alongside the classical key exchange, so recorded traffic stays
|
||||||
|
# secret against a future quantum computer, and authentication by the version 3 certificates' ML-KEM-1024 keys.
|
||||||
|
# Both ends need a version 3 certificate in pki.cert; without one these settings are ignored with a warning, and
|
||||||
|
# require is refused. These handshakes are too large for one packet and travel as fragments with forward error
|
||||||
|
# correction.
|
||||||
|
#pq:
|
||||||
|
# initiate starts post-quantum handshakes. Attempts alternate between post-quantum and classical until one is
|
||||||
|
# answered, so peers that do not support them still connect.
|
||||||
|
#initiate: false
|
||||||
|
# accept answers post-quantum handshakes from peers.
|
||||||
|
#accept: false
|
||||||
|
# require makes every handshake post-quantum: it implies initiate and accept, never offers a classical handshake
|
||||||
|
# and refuses classical handshakes from peers. Only enable it once every host has a version 3 certificate.
|
||||||
|
#require: false
|
||||||
|
# sticky never falls back to a classical handshake with a peer that has completed a post-quantum one since
|
||||||
|
# nebula started, which defeats an attacker who drops post-quantum handshakes to force a downgrade.
|
||||||
|
#sticky: false
|
||||||
|
# repair_delay is how long the forward error correction fragments trail the rest of a handshake message.
|
||||||
|
#repair_delay: 10ms
|
||||||
|
|
||||||
# Tunnel manager settings
|
# Tunnel manager settings
|
||||||
#tunnels:
|
#tunnels:
|
||||||
# drop_inactive controls whether inactive tunnels are maintained or dropped after the inactive_timeout period has
|
# drop_inactive controls whether inactive tunnels are maintained or dropped after the inactive_timeout period has
|
||||||
|
|||||||
@@ -764,6 +764,14 @@ func (fc *FirewallCA) match(p firewall.Packet, c *cert.CachedCertificate, caPool
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A version 3 certificate names its CA by the fingerprint of the CA's v2 form,
|
||||||
|
// which is matched above. A rule may also use the v3 CA's own fingerprint.
|
||||||
|
if c.Certificate.Version() == cert.Version3 {
|
||||||
|
if t, ok := fc.CAShas[s.Fingerprint]; ok && t.match(p, c) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return fc.CANames[s.Certificate.Name()].match(p, c)
|
return fc.CANames[s.Certificate.Name()].match(p, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+58
-1
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gaissmai/bart"
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/cert_test"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/firewall"
|
"github.com/slackhq/nebula/firewall"
|
||||||
"github.com/slackhq/nebula/iputil"
|
"github.com/slackhq/nebula/iputil"
|
||||||
@@ -1256,7 +1257,7 @@ func TestNewFirewallFromConfig(t *testing.T) {
|
|||||||
l := test.NewLogger()
|
l := test.NewLogger()
|
||||||
// Test a bad rule definition
|
// Test a bad rule definition
|
||||||
c := &dummyCert{}
|
c := &dummyCert{}
|
||||||
cs, err := newCertState(cert.Version2, nil, c, false, cert.Curve_CURVE25519, nil, "aes")
|
cs, err := newCertState(cert.Version2, nil, c, nil, false, cert.Curve_CURVE25519, nil, "aes")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
conf := config.NewC(test.NewLogger())
|
conf := config.NewC(test.NewLogger())
|
||||||
@@ -1745,3 +1746,59 @@ func resetConntrack(fw *Firewall) {
|
|||||||
fw.Conntrack.Conns = map[firewall.Packet]*conn{}
|
fw.Conntrack.Conns = map[firewall.Packet]*conn{}
|
||||||
fw.Conntrack.Unlock()
|
fw.Conntrack.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A version 3 certificate's CA answers to both its fingerprints in ca_sha rules: the v2 form's, which is what the
|
||||||
|
// certificate names as issuer, and its own. A v2 certificate only to the v2 one.
|
||||||
|
func TestFirewall_CAShaV3(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version3, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
host, _, _, _ := cert_test.NewTestCert(cert.Version3, cert.Curve_CURVE25519, ca, caKey, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("1.2.3.4/24")}, nil, nil)
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
require.NoError(t, cp.AddCA(ca))
|
||||||
|
require.NoError(t, cp.AddCA(cert.V2Form(ca)))
|
||||||
|
v3Sha, err := ca.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
v2Sha, err := cert.V2Form(ca).Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
myVpnNetworksTable := new(bart.Lite)
|
||||||
|
myVpnNetworksTable.Insert(netip.MustParsePrefix("1.1.1.1/8"))
|
||||||
|
p := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("1.2.3.4"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("1.2.3.4"),
|
||||||
|
LocalPort: 10,
|
||||||
|
RemotePort: 90,
|
||||||
|
Protocol: iputil.IPProtocolUDP,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
c cert.Certificate
|
||||||
|
caSha string
|
||||||
|
match bool
|
||||||
|
}{
|
||||||
|
{"v3 by v2 sha", host, v2Sha, true},
|
||||||
|
{"v3 by v3 sha", host, v3Sha, true},
|
||||||
|
{"v2 form by v2 sha", cert.V2Form(host), v2Sha, true},
|
||||||
|
{"v2 form by v3 sha", cert.V2Form(host), v3Sha, false},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
cc, err := cp.VerifyCertificate(time.Now(), tc.c)
|
||||||
|
require.NoError(t, err)
|
||||||
|
h := HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{peerCert: cc},
|
||||||
|
vpnAddrs: []netip.Addr{netip.MustParseAddr("1.2.3.4")},
|
||||||
|
}
|
||||||
|
h.buildNetworks(myVpnNetworksTable, tc.c)
|
||||||
|
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, tc.c)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, nil, "", "", "", "", tc.caSha))
|
||||||
|
err = fw.Drop(p, true, &h, cp, nil)
|
||||||
|
if tc.match {
|
||||||
|
require.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
require.ErrorIs(t, err, ErrNoMatchingRule)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
module github.com/slackhq/nebula
|
module github.com/slackhq/nebula
|
||||||
|
|
||||||
go 1.27.2
|
go 1.27.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
dario.cat/mergo v1.0.2
|
dario.cat/mergo v1.0.2
|
||||||
filippo.io/bigmod v0.1.0
|
filippo.io/bigmod v0.1.0
|
||||||
github.com/DefinedNet/netlink v1.3.2-dn.1
|
github.com/DefinedNet/netlink v1.3.2-dn.1
|
||||||
|
github.com/DefinedNet/noise v0.0.0-00010101000000-000000000000
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
|
||||||
github.com/flynn/noise v1.1.0
|
github.com/armon/go-radix v1.0.0
|
||||||
github.com/gaissmai/bart v0.29.0
|
github.com/gaissmai/bart v0.29.0
|
||||||
github.com/gogo/protobuf v1.3.2
|
github.com/gogo/protobuf v1.3.2
|
||||||
github.com/google/gopacket v1.1.19
|
github.com/google/gopacket v1.1.19
|
||||||
@@ -32,7 +33,7 @@ require (
|
|||||||
golang.zx2c4.com/wireguard/windows v1.0.1
|
golang.zx2c4.com/wireguard/windows v1.0.1
|
||||||
google.golang.org/protobuf v1.36.12
|
google.golang.org/protobuf v1.36.12
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -44,6 +45,7 @@ require (
|
|||||||
github.com/prometheus/common v0.70.1 // indirect
|
github.com/prometheus/common v0.70.1 // indirect
|
||||||
github.com/prometheus/procfs v0.21.1 // indirect
|
github.com/prometheus/procfs v0.21.1 // indirect
|
||||||
github.com/vishvananda/netns v0.0.5 // indirect
|
github.com/vishvananda/netns v0.0.5 // indirect
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc // indirect
|
golang.org/x/time v0.5.0 // indirect
|
||||||
golang.org/x/time v0.15.0 // indirect
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
replace github.com/DefinedNet/noise => /home/jack/work/git/DefinedNet-noise
|
||||||
@@ -12,6 +12,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
|
|||||||
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
||||||
|
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
||||||
|
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
@@ -21,8 +23,6 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
|
|||||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
|
|
||||||
github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
|
|
||||||
github.com/gaissmai/bart v0.29.0 h1:wO6HGE8g9YE0Wm0bCpYxwRzfQ4+fbJKOhL64e5ACGCI=
|
github.com/gaissmai/bart v0.29.0 h1:wO6HGE8g9YE0Wm0bCpYxwRzfQ4+fbJKOhL64e5ACGCI=
|
||||||
github.com/gaissmai/bart v0.29.0/go.mod h1:GREWQfTLRWz/c5FTOsIw+KkscuFkIV5t8Rp7Nd1Td5c=
|
github.com/gaissmai/bart v0.29.0/go.mod h1:GREWQfTLRWz/c5FTOsIw+KkscuFkIV5t8Rp7Nd1Td5c=
|
||||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||||
@@ -152,11 +152,8 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf
|
|||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
|
||||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc h1:TS73t7x3KarrNd5qAipmspBDS1rkMcgVG/fS1aRb4Rc=
|
|
||||||
golang.org/x/exp v0.0.0-20250711185948-6ae5c78190dc/go.mod h1:A+z0yzpGtvnG90cToK5n2tu8UJVP2XUATh+r+sfOOOc=
|
|
||||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
||||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
@@ -170,7 +167,6 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
|
|||||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
|
||||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
||||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
@@ -192,19 +188,17 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
|
|||||||
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
|
||||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||||
@@ -243,5 +237,5 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980 h1:ribyrTYQ/QYeUIs8x8bVLYwGyepa7RnHLqJzegT5YYk=
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe h1:fre4i6mv4iBuz5lCMOzHD1rH1ljqHWSICFmZRbbgp3g=
|
||||||
gvisor.dev/gvisor v0.0.0-20261014193016-04d31b385980/go.mod h1:pK/dapF5Bf3KGW7/kd5CI5IC4lYh9FMFY1qETAIGLXk=
|
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe/go.mod h1:sxc3Uvk/vHcd3tj7/DHVBoR5wvWT/MmRq2pj7HRJnwU=
|
||||||
+43
-17
@@ -1,9 +1,10 @@
|
|||||||
package handshake
|
package handshake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/mlkem"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -11,40 +12,65 @@ import (
|
|||||||
// at a given cert version. Version and Curve are read from Cert; the public
|
// at a given cert version. Version and Curve are read from Cert; the public
|
||||||
// half of the static keypair likewise comes from Cert.PublicKey().
|
// half of the static keypair likewise comes from Cert.PublicKey().
|
||||||
type Credential struct {
|
type Credential struct {
|
||||||
Cert cert.Certificate // the certificate
|
Cert cert.Certificate // the certificate
|
||||||
Bytes []byte // pre-marshaled certificate bytes
|
Bytes []byte // pre-marshaled certificate bytes
|
||||||
privateKey []byte // static private key (public half lives in Cert)
|
privateKey []byte // static private key (public half lives in Cert)
|
||||||
cipherSuite noise.CipherSuite // pre-built cipher suite (DH + cipher + hash)
|
publicKey []byte // static public key, the classical half for a v3 Cert
|
||||||
|
kemKey noise.KEMKey // static KEM keypair of the Cert's suite, only for a v3 Cert
|
||||||
|
kemDK *mlkem.DecapsulationKey1024 // kemKey expanded, when the suite's KEM is ML-KEM-1024
|
||||||
|
cipherSuite noise.CipherSuite // pre-built cipher suite (DH + cipher + hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCredential creates a Credential with all material needed for handshake
|
// NewCredential creates a Credential with all material needed for handshake
|
||||||
// participation. The cipherSuite should be pre-built by the caller with the
|
// participation. The cipherSuite should be pre-built by the caller with the
|
||||||
// appropriate DH function, cipher, and hash.
|
// appropriate DH function, cipher, and hash.
|
||||||
|
//
|
||||||
|
// For a version 3 certificate privateKey is the composite private key,
|
||||||
|
// classical || the seed of its suite's KEM, matching the composite
|
||||||
|
// Cert.PublicKey().
|
||||||
func NewCredential(
|
func NewCredential(
|
||||||
c cert.Certificate,
|
c cert.Certificate,
|
||||||
hsBytes []byte,
|
hsBytes []byte,
|
||||||
privateKey []byte,
|
privateKey []byte,
|
||||||
cipherSuite noise.CipherSuite,
|
cipherSuite noise.CipherSuite,
|
||||||
) *Credential {
|
) *Credential {
|
||||||
return &Credential{
|
hc := &Credential{
|
||||||
Cert: c,
|
Cert: c,
|
||||||
Bytes: hsBytes,
|
Bytes: hsBytes,
|
||||||
privateKey: privateKey,
|
privateKey: privateKey,
|
||||||
|
publicKey: c.PublicKey(),
|
||||||
cipherSuite: cipherSuite,
|
cipherSuite: cipherSuite,
|
||||||
}
|
}
|
||||||
|
if suite := cert.PQSuiteOf(c); suite != cert.PQSuiteUnknown {
|
||||||
|
// The pki checked the private key against the certificate, so both split.
|
||||||
|
hc.publicKey, hc.kemKey.Public, _ = suite.SplitPublicKey(false, c.PublicKey())
|
||||||
|
hc.privateKey, hc.kemKey.Private, _ = suite.SplitPrivateKey(false, privateKey)
|
||||||
|
// Expand the static KEM key once rather than in every handshake.
|
||||||
|
if st, ok := HybridSubtype(suite); ok && subtypeInfos[st].kem == KEMMLKEM1024 {
|
||||||
|
hc.kemDK, _ = mlkem.NewDecapsulationKey1024(hc.kemKey.Private)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hc
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildHandshakeState creates a noise.HandshakeState from this credential.
|
// buildHandshakeState creates a noise.HandshakeState from this credential. A
|
||||||
func (hc *Credential) buildHandshakeState(initiator bool, pattern noise.HandshakePattern) (*noise.HandshakeState, error) {
|
// hybrid pattern also gets its KEM and the static KEM key.
|
||||||
return noise.NewHandshakeState(noise.Config{
|
func (hc *Credential) buildHandshakeState(initiator bool, info subtypeInfo) (*noise.HandshakeState, error) {
|
||||||
CipherSuite: hc.cipherSuite,
|
cfg := noise.Config{
|
||||||
Random: rand.Reader,
|
CipherSuite: hc.cipherSuite,
|
||||||
Pattern: pattern,
|
Random: rand.Reader,
|
||||||
Initiator: initiator,
|
Pattern: info.pattern,
|
||||||
StaticKeypair: noise.DHKey{Private: hc.privateKey, Public: hc.Cert.PublicKey()},
|
Initiator: initiator,
|
||||||
PresharedKey: []byte{},
|
StaticKeypair: noise.DHKey{Private: hc.privateKey, Public: hc.publicKey},
|
||||||
PresharedKeyPlacement: 0,
|
}
|
||||||
})
|
if info.kem != nil {
|
||||||
|
cfg.KEMFunc = info.kem
|
||||||
|
cfg.StaticKEMKeypair = hc.kemKey
|
||||||
|
if info.kem == KEMMLKEM1024 {
|
||||||
|
cfg.KEMFunc = &mlkem1024Keys{static: hc.kemDK, staticSeed: hc.kemKey.Private}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return noise.NewHandshakeState(cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetCredentialFunc returns the handshake credential for the given version,
|
// GetCredentialFunc returns the handshake credential for the given version,
|
||||||
|
|||||||
@@ -19,4 +19,6 @@ var (
|
|||||||
ErrAsymmetricCipherKeys = errors.New("noise produced only one cipher key")
|
ErrAsymmetricCipherKeys = errors.New("noise produced only one cipher key")
|
||||||
ErrMultiMessageUnsupported = errors.New("multi-message handshake patterns are not yet supported by the manager")
|
ErrMultiMessageUnsupported = errors.New("multi-message handshake patterns are not yet supported by the manager")
|
||||||
ErrSubtypeMismatch = errors.New("packet subtype does not match handshake machine subtype")
|
ErrSubtypeMismatch = errors.New("packet subtype does not match handshake machine subtype")
|
||||||
|
ErrCertVersionMismatch = errors.New("handshake subtype does not allow this certificate version")
|
||||||
|
ErrPQSuiteMismatch = errors.New("handshake subtype does not use the KEM of this certificate's post-quantum suite")
|
||||||
)
|
)
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
// Package fec implements the systematic Reed-Solomon erasure code used to carry
|
||||||
|
// handshake messages that do not fit in a single UDP packet.
|
||||||
|
//
|
||||||
|
// A message is split into K equally sized data symbols (the last one zero
|
||||||
|
// padded). Symbol indexes 0..K-1 are the data symbols themselves; indexes
|
||||||
|
// K..MaxSymbols-1 are repair symbols. Any K distinct symbols reconstruct the
|
||||||
|
// message. Repair symbols are generated on demand, so a sender can emit fresh
|
||||||
|
// ones on every retransmit and a receiver can merge everything it has seen.
|
||||||
|
package fec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MaxSymbols is the number of distinct symbols, data and repair together, one
|
||||||
|
// message can be encoded into. It is bounded by the size of GF(2^8).
|
||||||
|
const MaxSymbols = 256
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrInvalidLayout = errors.New("invalid fec layout")
|
||||||
|
ErrIndexRange = errors.New("symbol index out of range")
|
||||||
|
ErrSymbolSize = errors.New("symbol has the wrong size")
|
||||||
|
ErrNotEnough = errors.New("not enough symbols to decode")
|
||||||
|
ErrCorrupt = errors.New("decoded message has non-zero padding")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Layout describes how a message is split into symbols. Both ends derive it
|
||||||
|
// from the message length alone, so it never has to be sent.
|
||||||
|
type Layout struct {
|
||||||
|
MsgLen int // length of the message in bytes
|
||||||
|
K int // number of data symbols
|
||||||
|
SymbolSize int // size of every symbol, data and repair
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLayout returns the layout for a message of msgLen bytes carried in symbols
|
||||||
|
// of at most maxSymbolSize bytes. Symbols are balanced, so padding is always
|
||||||
|
// less than K bytes.
|
||||||
|
func NewLayout(msgLen, maxSymbolSize int) (Layout, error) {
|
||||||
|
if msgLen <= 0 || maxSymbolSize <= 0 {
|
||||||
|
return Layout{}, ErrInvalidLayout
|
||||||
|
}
|
||||||
|
k := (msgLen + maxSymbolSize - 1) / maxSymbolSize
|
||||||
|
if k >= MaxSymbols {
|
||||||
|
return Layout{}, fmt.Errorf("%w: %d data symbols", ErrInvalidLayout, k)
|
||||||
|
}
|
||||||
|
return Layout{
|
||||||
|
MsgLen: msgLen,
|
||||||
|
K: k,
|
||||||
|
SymbolSize: (msgLen + k - 1) / k,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encoder produces symbols for one message.
|
||||||
|
type Encoder struct {
|
||||||
|
layout Layout
|
||||||
|
data [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewEncoder splits msg into data symbols. They alias msg, all but a padded
|
||||||
|
// last symbol, so msg must not change while the Encoder is in use.
|
||||||
|
func NewEncoder(msg []byte, maxSymbolSize int) (*Encoder, error) {
|
||||||
|
l, err := NewLayout(len(msg), maxSymbolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := make([][]byte, l.K)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = msg[i*l.SymbolSize : min((i+1)*l.SymbolSize, len(msg))]
|
||||||
|
}
|
||||||
|
if last := data[l.K-1]; len(last) < l.SymbolSize {
|
||||||
|
data[l.K-1] = append(make([]byte, 0, l.SymbolSize), last...)[:l.SymbolSize]
|
||||||
|
}
|
||||||
|
return &Encoder{layout: l, data: data}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Encoder) Layout() Layout {
|
||||||
|
return e.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
// Symbol appends the symbol with the given index to dst and returns the result.
|
||||||
|
func (e *Encoder) Symbol(dst []byte, index int) ([]byte, error) {
|
||||||
|
if index < 0 || index >= MaxSymbols {
|
||||||
|
return nil, ErrIndexRange
|
||||||
|
}
|
||||||
|
if index < e.layout.K {
|
||||||
|
return append(dst, e.data[index]...), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
start := len(dst)
|
||||||
|
dst = append(dst, make([]byte, e.layout.SymbolSize)...)
|
||||||
|
out := dst[start:]
|
||||||
|
for col, d := range e.data {
|
||||||
|
mulAdd(out, d, coefficient(index, col))
|
||||||
|
}
|
||||||
|
return dst, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decoder collects symbols for one message until it can reconstruct it.
|
||||||
|
// It stores at most K symbols, so its memory is bounded by the layout no matter
|
||||||
|
// how many symbols arrive.
|
||||||
|
//
|
||||||
|
// TODO(in-place decode): for receivers that already hold state for the message,
|
||||||
|
// the initiator's msg2 slots and a pending responder's msg3, store into one
|
||||||
|
// buffer of K*SymbolSize (plus room for the Nebula header) allocated from the
|
||||||
|
// layout: data symbol i at offset i*SymbolSize, repair symbols parked in the
|
||||||
|
// slots of data symbols not seen yet (moved to another empty slot if that data
|
||||||
|
// symbol arrives; one always exists while fewer than K are held). Decode then
|
||||||
|
// folds the known columns into each parked repair symbol in place and runs
|
||||||
|
// Gauss-Jordan across those rows in place; a Cauchy submatrix needs no
|
||||||
|
// pivoting, so each solution lands in the slot of its own column and the
|
||||||
|
// buffer is the packet. That removes the per-symbol copies, about 10KB and a
|
||||||
|
// dozen allocations per msg2, at the
|
||||||
|
// cost of holding K*SymbolSize from the first fragment and destroying the
|
||||||
|
// input on a failed decode. It is not for the responder's msg1 table, which
|
||||||
|
// must never allocate from a claimed size.
|
||||||
|
type Decoder struct {
|
||||||
|
layout Layout
|
||||||
|
have [MaxSymbols]bool
|
||||||
|
syms map[int][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDecoder returns a decoder for a message of msgLen bytes that was encoded
|
||||||
|
// with the same maxSymbolSize.
|
||||||
|
func NewDecoder(msgLen, maxSymbolSize int) (*Decoder, error) {
|
||||||
|
l, err := NewLayout(msgLen, maxSymbolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Decoder{layout: l, syms: make(map[int][]byte, l.K)}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Decoder) Layout() Layout {
|
||||||
|
return d.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stored returns the number of symbols held.
|
||||||
|
func (d *Decoder) Stored() int {
|
||||||
|
return len(d.syms)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Has reports whether the symbol with the given index is held.
|
||||||
|
func (d *Decoder) Has(index int) bool {
|
||||||
|
return index >= 0 && index < MaxSymbols && d.have[index]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seen returns which symbol indexes are held.
|
||||||
|
func (d *Decoder) Seen() [MaxSymbols]bool {
|
||||||
|
return d.have
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ready reports whether enough symbols have arrived to decode.
|
||||||
|
func (d *Decoder) Ready() bool {
|
||||||
|
return len(d.syms) >= d.layout.K
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add stores a copy of the symbol. Duplicates, and anything beyond the first K
|
||||||
|
// distinct symbols, are ignored. It returns whether the decoder is ready.
|
||||||
|
func (d *Decoder) Add(index int, sym []byte) (bool, error) {
|
||||||
|
if index < 0 || index >= MaxSymbols {
|
||||||
|
return d.Ready(), ErrIndexRange
|
||||||
|
}
|
||||||
|
if len(sym) != d.layout.SymbolSize {
|
||||||
|
return d.Ready(), ErrSymbolSize
|
||||||
|
}
|
||||||
|
if d.have[index] || d.Ready() {
|
||||||
|
return d.Ready(), nil
|
||||||
|
}
|
||||||
|
d.have[index] = true
|
||||||
|
d.syms[index] = append([]byte(nil), sym...)
|
||||||
|
return d.Ready(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode reconstructs the message from the stored symbols and appends it to
|
||||||
|
// dst. Decoding needs the padding of the last symbol too, so dst has to have
|
||||||
|
// room for K*SymbolSize bytes or it is grown to.
|
||||||
|
func (d *Decoder) Decode(dst []byte) ([]byte, error) {
|
||||||
|
if !d.Ready() {
|
||||||
|
return nil, ErrNotEnough
|
||||||
|
}
|
||||||
|
l := d.layout
|
||||||
|
|
||||||
|
start := len(dst)
|
||||||
|
dst = slices.Grow(dst, l.K*l.SymbolSize)[:start+l.K*l.SymbolSize]
|
||||||
|
out := dst[start:]
|
||||||
|
var missing, repair []int
|
||||||
|
for i := range l.K {
|
||||||
|
if s, ok := d.syms[i]; ok {
|
||||||
|
copy(out[i*l.SymbolSize:], s)
|
||||||
|
} else {
|
||||||
|
clear(out[i*l.SymbolSize : (i+1)*l.SymbolSize])
|
||||||
|
missing = append(missing, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(missing) > 0 {
|
||||||
|
for i := l.K; i < MaxSymbols && len(repair) < len(missing); i++ {
|
||||||
|
if d.have[i] {
|
||||||
|
repair = append(repair, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(repair) < len(missing) {
|
||||||
|
return nil, ErrNotEnough
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each repair symbol r satisfies r = sum_col coefficient(r, col) * data[col].
|
||||||
|
// Move the known data columns to the left hand side, leaving a square
|
||||||
|
// Cauchy system in the missing columns.
|
||||||
|
rhs := make([][]byte, len(repair))
|
||||||
|
m := make([][]byte, len(repair))
|
||||||
|
for ri, row := range repair {
|
||||||
|
rhs[ri] = append([]byte(nil), d.syms[row]...)
|
||||||
|
m[ri] = make([]byte, len(missing))
|
||||||
|
mi := 0
|
||||||
|
for col := range l.K {
|
||||||
|
if mi < len(missing) && missing[mi] == col {
|
||||||
|
m[ri][mi] = coefficient(row, col)
|
||||||
|
mi++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
mulAdd(rhs[ri], out[col*l.SymbolSize:(col+1)*l.SymbolSize], coefficient(row, col))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !invert(m) {
|
||||||
|
// Unreachable for a Cauchy matrix; kept so a broken invariant fails loudly.
|
||||||
|
return nil, errors.New("fec: singular decode matrix")
|
||||||
|
}
|
||||||
|
for mi, col := range missing {
|
||||||
|
dst := out[col*l.SymbolSize : (col+1)*l.SymbolSize]
|
||||||
|
for ri := range repair {
|
||||||
|
mulAdd(dst, rhs[ri], m[mi][ri])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, b := range out[l.MsgLen:] {
|
||||||
|
if b != 0 {
|
||||||
|
return nil, ErrCorrupt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dst[:start+l.MsgLen], nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,326 @@
|
|||||||
|
package fec
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"math/rand/v2"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// slowMul is carry-less multiplication reduced by gfPoly, used to check the tables.
|
||||||
|
func slowMul(a, b byte) byte {
|
||||||
|
var p int
|
||||||
|
x, y := int(a), int(b)
|
||||||
|
for y > 0 {
|
||||||
|
if y&1 != 0 {
|
||||||
|
p ^= x
|
||||||
|
}
|
||||||
|
x <<= 1
|
||||||
|
if x&0x100 != 0 {
|
||||||
|
x ^= gfPoly
|
||||||
|
}
|
||||||
|
y >>= 1
|
||||||
|
}
|
||||||
|
return byte(p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGF(t *testing.T) {
|
||||||
|
for a := range 256 {
|
||||||
|
for b := range 256 {
|
||||||
|
require.Equal(t, slowMul(byte(a), byte(b)), gfMul(byte(a), byte(b)), "%d*%d", a, b)
|
||||||
|
}
|
||||||
|
if a != 0 {
|
||||||
|
require.Equal(t, byte(1), gfMul(byte(a), gfInv(byte(a))), "inv(%d)", a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
src := make([]byte, 256)
|
||||||
|
for i := range src {
|
||||||
|
src[i] = byte(i)
|
||||||
|
}
|
||||||
|
for c := range 256 {
|
||||||
|
dst := make([]byte, 256)
|
||||||
|
mulAdd(dst, src, byte(c))
|
||||||
|
for i := range src {
|
||||||
|
require.Equal(t, gfMul(byte(c), src[i]), dst[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLayout(t *testing.T) {
|
||||||
|
for msgLen := 1; msgLen <= 20000; msgLen++ {
|
||||||
|
l, err := NewLayout(msgLen, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, (msgLen+1175)/1176, l.K)
|
||||||
|
require.LessOrEqual(t, l.SymbolSize, 1176)
|
||||||
|
require.GreaterOrEqual(t, l.K*l.SymbolSize, msgLen)
|
||||||
|
require.Less(t, l.K*l.SymbolSize-msgLen, l.K, "padding must be less than one byte per symbol")
|
||||||
|
}
|
||||||
|
|
||||||
|
l, err := NewLayout(65535, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 56, l.K)
|
||||||
|
|
||||||
|
_, err = NewLayout(0, 1176)
|
||||||
|
require.ErrorIs(t, err, ErrInvalidLayout)
|
||||||
|
_, err = NewLayout(100, 0)
|
||||||
|
require.ErrorIs(t, err, ErrInvalidLayout)
|
||||||
|
_, err = NewLayout(256, 1)
|
||||||
|
require.ErrorIs(t, err, ErrInvalidLayout)
|
||||||
|
_, err = NewLayout(255, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func randMsg(r *rand.Rand, n int) []byte {
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = byte(r.Uint32())
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeWith(t *testing.T, enc *Encoder, msgLen, maxSym int, indexes []int) []byte {
|
||||||
|
t.Helper()
|
||||||
|
dec, err := NewDecoder(msgLen, maxSym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, idx := range indexes {
|
||||||
|
sym, err := enc.Symbol(nil, idx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = dec.Add(idx, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
require.True(t, dec.Ready())
|
||||||
|
// Decode after a prefix, into capacity holding garbage, which must neither
|
||||||
|
// disturb the prefix nor leak into the message.
|
||||||
|
dst := make([]byte, 3, 3+dec.Layout().K*dec.Layout().SymbolSize)
|
||||||
|
for i := range dst[:cap(dst)] {
|
||||||
|
dst[:cap(dst)][i] = 0xa5
|
||||||
|
}
|
||||||
|
out, err := dec.Decode(dst)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, []byte{0xa5, 0xa5, 0xa5}, out[:3])
|
||||||
|
require.Equal(t, &dst[:1][0], &out[:1][0], "decoded in place")
|
||||||
|
return out[3:]
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestExhaustiveSubsets decodes every k-subset of the first k+m symbols for
|
||||||
|
// small codes, which checks the MDS property directly.
|
||||||
|
func TestExhaustiveSubsets(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(1, 2))
|
||||||
|
const maxSym = 16
|
||||||
|
for k := 1; k <= 10; k++ {
|
||||||
|
for m := 1; m <= 5; m++ {
|
||||||
|
msgLen := (k-1)*maxSym + 1 + r.IntN(maxSym)
|
||||||
|
msg := randMsg(r, msgLen)
|
||||||
|
enc, err := NewEncoder(msg, maxSym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, k, enc.Layout().K)
|
||||||
|
|
||||||
|
n := k + m
|
||||||
|
for mask := 0; mask < 1<<n; mask++ {
|
||||||
|
var idx []int
|
||||||
|
for i := range n {
|
||||||
|
if mask&(1<<i) != 0 {
|
||||||
|
idx = append(idx, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(idx) != k {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
require.Equal(t, msg, decodeWith(t, enc, msgLen, maxSym, idx), "k=%d m=%d idx=%v", k, m, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRandomHighIndexes draws symbols from the whole index space, the shape
|
||||||
|
// retransmits produce once fresh repair symbols are in play.
|
||||||
|
func TestRandomHighIndexes(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(3, 4))
|
||||||
|
for range 2000 {
|
||||||
|
msgLen := 1 + r.IntN(65535)
|
||||||
|
msg := randMsg(r, msgLen)
|
||||||
|
enc, err := NewEncoder(msg, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
k := enc.Layout().K
|
||||||
|
|
||||||
|
idx := r.Perm(MaxSymbols)[:k]
|
||||||
|
require.Equal(t, msg, decodeWith(t, enc, msgLen, 1176, idx), "k=%d idx=%v", k, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecoderBounds(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(5, 6))
|
||||||
|
msg := randMsg(r, 3000)
|
||||||
|
enc, err := NewEncoder(msg, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
l := enc.Layout()
|
||||||
|
require.Equal(t, 3, l.K)
|
||||||
|
require.Equal(t, 1000, l.SymbolSize)
|
||||||
|
|
||||||
|
dec, err := NewDecoder(3000, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = dec.Decode(nil)
|
||||||
|
require.ErrorIs(t, err, ErrNotEnough)
|
||||||
|
|
||||||
|
_, err = dec.Add(-1, make([]byte, 1000))
|
||||||
|
require.ErrorIs(t, err, ErrIndexRange)
|
||||||
|
_, err = dec.Add(MaxSymbols, make([]byte, 1000))
|
||||||
|
require.ErrorIs(t, err, ErrIndexRange)
|
||||||
|
_, err = dec.Add(0, make([]byte, 999))
|
||||||
|
require.ErrorIs(t, err, ErrSymbolSize)
|
||||||
|
|
||||||
|
s5, _ := enc.Symbol(nil, 5)
|
||||||
|
ready, err := dec.Add(5, s5)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, ready)
|
||||||
|
|
||||||
|
// A duplicate index is ignored, even with different contents.
|
||||||
|
ready, err = dec.Add(5, make([]byte, 1000))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.False(t, ready)
|
||||||
|
|
||||||
|
s0, _ := enc.Symbol(nil, 0)
|
||||||
|
s9, _ := enc.Symbol(nil, 9)
|
||||||
|
_, err = dec.Add(0, s0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
ready, err = dec.Add(9, s9)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ready)
|
||||||
|
|
||||||
|
// Symbols beyond K are not stored.
|
||||||
|
ready, err = dec.Add(1, make([]byte, 1000))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ready)
|
||||||
|
require.Len(t, dec.syms, 3)
|
||||||
|
|
||||||
|
out, err := dec.Decode(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, msg, out)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCorruptPadding(t *testing.T) {
|
||||||
|
// 3001 bytes in 3 symbols of 1001 leaves 2 bytes of padding in the last one.
|
||||||
|
dec, err := NewDecoder(3001, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 1001, dec.Layout().SymbolSize)
|
||||||
|
|
||||||
|
last := make([]byte, 1001)
|
||||||
|
last[1000] = 1
|
||||||
|
for i, s := range [][]byte{make([]byte, 1001), make([]byte, 1001), last} {
|
||||||
|
_, err = dec.Add(i, s)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
_, err = dec.Decode(nil)
|
||||||
|
require.ErrorIs(t, err, ErrCorrupt)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestKnownAnswer pins the encoding. Both ends of a handshake must produce
|
||||||
|
// identical repair symbols, so any change here is a wire format change.
|
||||||
|
func TestKnownAnswer(t *testing.T) {
|
||||||
|
msg := make([]byte, 3000)
|
||||||
|
for i := range msg {
|
||||||
|
msg[i] = byte(i*7 + 3)
|
||||||
|
}
|
||||||
|
enc, err := NewEncoder(msg, 1176)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Cross-checked against an independent bit-serial GF(2^8) implementation.
|
||||||
|
want := map[int]string{
|
||||||
|
3: "52124f58276ec6338d3b3f4eed273bc7cabfe4f0f467928962b046ad1b348f50",
|
||||||
|
4: "a50928b04738981985a7d4a17ffaf8d7e30014ca64fc53c99800ffb61aec4aa7",
|
||||||
|
5: "ada4c95a7facbc542aaef584e6f0d081b7b0d0eed69ded03b57d0753a119cfc9",
|
||||||
|
255: "839066cdf94f963b223fb1c1c1360f698c14f6349b25653ff5b521791a4021b1",
|
||||||
|
}
|
||||||
|
for idx, h := range want {
|
||||||
|
sym, err := enc.Symbol(nil, idx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
sum := sha256.Sum256(sym)
|
||||||
|
assert.Equal(t, h, hex.EncodeToString(sum[:]), "symbol %d", idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func FuzzRoundTrip(f *testing.F) {
|
||||||
|
f.Add([]byte("hello"), uint16(2), uint64(0))
|
||||||
|
f.Add(make([]byte, 9000), uint16(1176), uint64(7))
|
||||||
|
f.Fuzz(func(t *testing.T, msg []byte, maxSym uint16, seed uint64) {
|
||||||
|
if len(msg) == 0 || maxSym == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
enc, err := NewEncoder(msg, int(maxSym))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r := rand.New(rand.NewPCG(seed, seed))
|
||||||
|
idx := r.Perm(MaxSymbols)[:enc.Layout().K]
|
||||||
|
require.Equal(t, msg, decodeWith(t, enc, len(msg), int(maxSym), idx))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FuzzDecoder feeds arbitrary symbols to a decoder; it must never panic and
|
||||||
|
// must either fail or return a message of the declared length.
|
||||||
|
func FuzzDecoder(f *testing.F) {
|
||||||
|
f.Add(uint16(3000), []byte{0, 5, 9, 200}, []byte("some symbol bytes"))
|
||||||
|
f.Fuzz(func(t *testing.T, msgLen uint16, indexes []byte, fill []byte) {
|
||||||
|
dec, err := NewDecoder(int(msgLen), 1176)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
size := dec.Layout().SymbolSize
|
||||||
|
for i, idx := range indexes {
|
||||||
|
sym := make([]byte, size)
|
||||||
|
for j := range sym {
|
||||||
|
if len(fill) > 0 {
|
||||||
|
sym[j] = fill[(i+j)%len(fill)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, err := dec.Add(int(idx), sym); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out, err := dec.Decode(nil)
|
||||||
|
if err == nil {
|
||||||
|
require.Len(t, out, int(msgLen))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// BenchmarkRepair and BenchmarkDecode use a v3-sized msg2 (~9.8KB, k=9) with
|
||||||
|
// three repair symbols, decoding with three data symbols missing.
|
||||||
|
func BenchmarkRepair(b *testing.B) {
|
||||||
|
msg := randMsg(rand.New(rand.NewPCG(7, 8)), 9800)
|
||||||
|
enc, err := NewEncoder(msg, 1176)
|
||||||
|
require.NoError(b, err)
|
||||||
|
buf := make([]byte, 0, 1176)
|
||||||
|
b.SetBytes(int64(len(msg)))
|
||||||
|
for b.Loop() {
|
||||||
|
for idx := 9; idx < 12; idx++ {
|
||||||
|
buf, _ = enc.Symbol(buf[:0], idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkDecode(b *testing.B) {
|
||||||
|
msg := randMsg(rand.New(rand.NewPCG(7, 8)), 9800)
|
||||||
|
enc, err := NewEncoder(msg, 1176)
|
||||||
|
require.NoError(b, err)
|
||||||
|
idx := []int{0, 1, 2, 3, 5, 6, 9, 10, 11}
|
||||||
|
syms := make([][]byte, len(idx))
|
||||||
|
for i, x := range idx {
|
||||||
|
syms[i], _ = enc.Symbol(nil, x)
|
||||||
|
}
|
||||||
|
b.SetBytes(int64(len(msg)))
|
||||||
|
for b.Loop() {
|
||||||
|
dec, _ := NewDecoder(len(msg), 1176)
|
||||||
|
for i, x := range idx {
|
||||||
|
_, _ = dec.Add(x, syms[i])
|
||||||
|
}
|
||||||
|
if _, err := dec.Decode(nil); err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package fec
|
||||||
|
|
||||||
|
// Arithmetic over GF(2^8) with the reducing polynomial x^8+x^4+x^3+x^2+1 (0x11d).
|
||||||
|
// Addition is XOR. Multiplication uses log/exp tables; the exp table is doubled so
|
||||||
|
// the sum of two logs never needs reducing mod 255.
|
||||||
|
|
||||||
|
const gfPoly = 0x11d
|
||||||
|
|
||||||
|
var (
|
||||||
|
gfExp [510]byte
|
||||||
|
gfLog [256]byte
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
x := 1
|
||||||
|
for i := range 255 {
|
||||||
|
gfExp[i] = byte(x)
|
||||||
|
gfExp[i+255] = byte(x)
|
||||||
|
gfLog[x] = byte(i)
|
||||||
|
x <<= 1
|
||||||
|
if x&0x100 != 0 {
|
||||||
|
x ^= gfPoly
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func gfMul(a, b byte) byte {
|
||||||
|
if a == 0 || b == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return gfExp[int(gfLog[a])+int(gfLog[b])]
|
||||||
|
}
|
||||||
|
|
||||||
|
// gfInv returns the multiplicative inverse of a. a must be non-zero.
|
||||||
|
func gfInv(a byte) byte {
|
||||||
|
return gfExp[255-int(gfLog[a])]
|
||||||
|
}
|
||||||
|
|
||||||
|
// mulAdd computes dst ^= c*src element-wise. dst must be at least as long as src.
|
||||||
|
func mulAdd(dst, src []byte, c byte) {
|
||||||
|
switch c {
|
||||||
|
case 0:
|
||||||
|
return
|
||||||
|
case 1:
|
||||||
|
for i, s := range src {
|
||||||
|
dst[i] ^= s
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var t [256]byte
|
||||||
|
lc := int(gfLog[c])
|
||||||
|
for s := 1; s < 256; s++ {
|
||||||
|
t[s] = gfExp[int(gfLog[s])+lc]
|
||||||
|
}
|
||||||
|
for i, s := range src {
|
||||||
|
dst[i] ^= t[s]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// coefficient returns the generator matrix entry for repair symbol index row and
|
||||||
|
// data symbol index col. Repair rows are a Cauchy matrix 1/(x_row + y_col) with
|
||||||
|
// x_row = row and y_col = col: rows are always >= k and columns < k so the two
|
||||||
|
// sets are disjoint, which makes every square submatrix invertible and the
|
||||||
|
// systematic code [I; C] MDS. A row depends only on its index, never on how many
|
||||||
|
// repair symbols exist, so new repair symbols can be generated at any time.
|
||||||
|
func coefficient(row, col int) byte {
|
||||||
|
return gfInv(byte(row) ^ byte(col))
|
||||||
|
}
|
||||||
|
|
||||||
|
// invert inverts the n x n matrix m in place with Gauss-Jordan elimination.
|
||||||
|
// It returns false if m is singular.
|
||||||
|
func invert(m [][]byte) bool {
|
||||||
|
n := len(m)
|
||||||
|
inv := make([][]byte, n)
|
||||||
|
for i := range inv {
|
||||||
|
inv[i] = make([]byte, n)
|
||||||
|
inv[i][i] = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
for col := range n {
|
||||||
|
pivot := -1
|
||||||
|
for r := col; r < n; r++ {
|
||||||
|
if m[r][col] != 0 {
|
||||||
|
pivot = r
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if pivot < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m[col], m[pivot] = m[pivot], m[col]
|
||||||
|
inv[col], inv[pivot] = inv[pivot], inv[col]
|
||||||
|
|
||||||
|
scale := gfInv(m[col][col])
|
||||||
|
for c := range n {
|
||||||
|
m[col][c] = gfMul(m[col][c], scale)
|
||||||
|
inv[col][c] = gfMul(inv[col][c], scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
for r := 0; r < n; r++ {
|
||||||
|
if r == col || m[r][col] == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
f := m[r][col]
|
||||||
|
for c := range n {
|
||||||
|
m[r][c] ^= gfMul(f, m[col][c])
|
||||||
|
inv[r][c] ^= gfMul(f, inv[col][c])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(m, inv)
|
||||||
|
return true
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package fragment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/handshake/fec"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrMismatch is returned when a fragment disagrees with the message it claims to
|
||||||
|
// belong to. The caller should discard the whole assembly.
|
||||||
|
var ErrMismatch = errors.New("fragment does not match its message")
|
||||||
|
|
||||||
|
// Assembly collects the fragments of one incoming message. Its memory is bounded
|
||||||
|
// by the message length: at most K symbols are kept however many arrive.
|
||||||
|
//
|
||||||
|
// An Assembly is not safe for concurrent use.
|
||||||
|
type Assembly struct {
|
||||||
|
fragID uint32
|
||||||
|
msgLen uint16
|
||||||
|
dec *fec.Decoder
|
||||||
|
received int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAssembly starts an assembly for the message described by h. The fragment
|
||||||
|
// itself still has to be passed to Add.
|
||||||
|
func NewAssembly(h Header) (*Assembly, error) {
|
||||||
|
dec, err := fec.NewDecoder(int(h.MsgLen), MaxSymbolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Assembly{fragID: h.FragID, msgLen: h.MsgLen, dec: dec}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add records a fragment parsed by Parse. It returns true once enough symbols
|
||||||
|
// are held to call Decode.
|
||||||
|
func (a *Assembly) Add(h Header, sym []byte) (bool, error) {
|
||||||
|
if h.FragID != a.fragID || h.MsgLen != a.msgLen {
|
||||||
|
return a.dec.Ready(), ErrMismatch
|
||||||
|
}
|
||||||
|
a.received += header.Len + HeaderLen + len(sym)
|
||||||
|
return a.dec.Add(int(h.Index), sym)
|
||||||
|
}
|
||||||
|
|
||||||
|
// wouldStore returns how many bytes Add would retain for this fragment.
|
||||||
|
func (a *Assembly) wouldStore(h Header, sym []byte) int {
|
||||||
|
if h.FragID != a.fragID || h.MsgLen != a.msgLen || a.dec.Ready() || a.dec.Has(int(h.Index)) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return len(sym)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode reconstructs the message and appends it to dst, see fec.Decoder.Decode.
|
||||||
|
// It requires Add to have reported ready.
|
||||||
|
func (a *Assembly) Decode(dst []byte) ([]byte, error) {
|
||||||
|
return a.dec.Decode(dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stored returns the number of symbol bytes held.
|
||||||
|
func (a *Assembly) Stored() int {
|
||||||
|
return a.dec.Stored() * a.dec.Layout().SymbolSize
|
||||||
|
}
|
||||||
|
|
||||||
|
// Received returns the number of packet bytes received for this message,
|
||||||
|
// duplicates included. It is the basis for the amplification budget.
|
||||||
|
func (a *Assembly) Received() int {
|
||||||
|
return a.received
|
||||||
|
}
|
||||||
@@ -0,0 +1,682 @@
|
|||||||
|
package fragment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
|
"math/rand/v2"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
const testSubtype = header.MessageSubType(9)
|
||||||
|
|
||||||
|
type testKey struct {
|
||||||
|
src string
|
||||||
|
fragID uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func randMsg(r *rand.Rand, n int) []byte {
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = byte(r.Uint32())
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func testConfig() TableConfig {
|
||||||
|
return TableConfig{
|
||||||
|
MaxBytes: 1 << 20,
|
||||||
|
MaxEntries: 64,
|
||||||
|
MaxPerSource: 4,
|
||||||
|
MaxMsgLen: 16384,
|
||||||
|
TTL: 2 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkInvariants verifies the table's bookkeeping against its contents.
|
||||||
|
func checkInvariants[K, S comparable](t *testing.T, tb *Table[K, S]) {
|
||||||
|
t.Helper()
|
||||||
|
tb.mu.Lock()
|
||||||
|
defer tb.mu.Unlock()
|
||||||
|
|
||||||
|
require.Len(t, tb.order, len(tb.entries))
|
||||||
|
require.LessOrEqual(t, len(tb.entries), tb.cfg.MaxEntries)
|
||||||
|
bytes := 0
|
||||||
|
perSource := map[S]int{}
|
||||||
|
for i, key := range tb.order {
|
||||||
|
e, ok := tb.entries[key]
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, i, e.pos)
|
||||||
|
bytes += e.a.Stored()
|
||||||
|
perSource[e.source]++
|
||||||
|
}
|
||||||
|
require.Equal(t, bytes, tb.bytes)
|
||||||
|
require.LessOrEqual(t, tb.bytes, tb.cfg.MaxBytes)
|
||||||
|
require.Equal(t, perSource, tb.perSource)
|
||||||
|
for _, n := range perSource {
|
||||||
|
require.LessOrEqual(t, n, tb.cfg.MaxPerSource)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func parse(t *testing.T, p []byte) (header.H, Header, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
var nh header.H
|
||||||
|
require.NoError(t, nh.Parse(p))
|
||||||
|
h, sym, err := Parse(p[header.Len:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
return nh, h, sym
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParse(t *testing.T) {
|
||||||
|
h := Header{FragID: 0xdeadbeef, MsgLen: 3000, Index: 7}
|
||||||
|
b := h.Encode(nil)
|
||||||
|
require.Len(t, b, HeaderLen)
|
||||||
|
b = append(b, make([]byte, 1000)...)
|
||||||
|
|
||||||
|
got, sym, err := Parse(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, h, got)
|
||||||
|
assert.Len(t, sym, 1000)
|
||||||
|
|
||||||
|
_, _, err = Parse(b[:HeaderLen-1])
|
||||||
|
require.ErrorIs(t, err, ErrShortFragment)
|
||||||
|
_, _, err = Parse(b[:len(b)-1])
|
||||||
|
require.ErrorIs(t, err, ErrSymbolSize)
|
||||||
|
_, _, err = Parse(append(b, 0))
|
||||||
|
require.ErrorIs(t, err, ErrSymbolSize)
|
||||||
|
|
||||||
|
bad := Header{FragID: 0, MsgLen: 3000}.Encode(nil)
|
||||||
|
_, _, err = Parse(append(bad, make([]byte, 1000)...))
|
||||||
|
require.ErrorIs(t, err, ErrFragID)
|
||||||
|
|
||||||
|
bad = Header{FragID: 1, MsgLen: 0}.Encode(nil)
|
||||||
|
_, _, err = Parse(bad)
|
||||||
|
require.ErrorIs(t, err, ErrMsgLen)
|
||||||
|
|
||||||
|
bad = Header{FragID: 1, MsgLen: 3000, Flags: 0x02}.Encode(nil)
|
||||||
|
_, _, err = Parse(append(bad, make([]byte, 1000)...))
|
||||||
|
require.ErrorIs(t, err, ErrFlags)
|
||||||
|
|
||||||
|
t.Run("cookie", func(t *testing.T) {
|
||||||
|
h := Header{FragID: 7, MsgLen: 3000, Index: 4, Flags: FlagCookie}
|
||||||
|
for i := range h.Cookie {
|
||||||
|
h.Cookie[i] = byte(i + 1)
|
||||||
|
}
|
||||||
|
b := h.Encode(nil)
|
||||||
|
require.Len(t, b, HeaderLen+CookieLen)
|
||||||
|
b = append(b, make([]byte, 1000)...)
|
||||||
|
|
||||||
|
got, sym, err := Parse(b)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, h, got)
|
||||||
|
assert.True(t, got.HasCookie())
|
||||||
|
assert.Len(t, sym, 1000)
|
||||||
|
|
||||||
|
_, _, err = Parse(b[:HeaderLen+CookieLen-1])
|
||||||
|
require.ErrorIs(t, err, ErrNoCookie)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDefaultRepair(t *testing.T) {
|
||||||
|
for k, want := range map[int]int{1: 2, 2: 2, 6: 2, 7: 3, 9: 3, 10: 4, 14: 5} {
|
||||||
|
assert.Equal(t, want, DefaultRepair(k), "k=%d", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSender(t *testing.T) {
|
||||||
|
_, err := NewSender(nil, testSubtype, 0, 1, 1)
|
||||||
|
require.ErrorIs(t, err, ErrMsgLen)
|
||||||
|
_, err = NewSender(make([]byte, 65536), testSubtype, 0, 1, 1)
|
||||||
|
require.ErrorIs(t, err, ErrMsgLen)
|
||||||
|
_, err = NewSender(make([]byte, 10), testSubtype, 0, 1, 0)
|
||||||
|
require.ErrorIs(t, err, ErrFragID)
|
||||||
|
|
||||||
|
msg := randMsg(rand.New(rand.NewPCG(1, 1)), 9800)
|
||||||
|
s, err := NewSender(msg, testSubtype, 77, 2, 1234)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, 9, s.Layout().K)
|
||||||
|
|
||||||
|
indexes := func(ps [][]byte) []int {
|
||||||
|
var out []int
|
||||||
|
for _, p := range ps {
|
||||||
|
require.LessOrEqual(t, len(p), MaxPacketLen)
|
||||||
|
nh, h, _ := parse(t, p)
|
||||||
|
assert.Equal(t, header.Handshake, nh.Type)
|
||||||
|
assert.Equal(t, testSubtype, nh.Subtype)
|
||||||
|
assert.Equal(t, uint32(77), nh.RemoteIndex)
|
||||||
|
assert.Equal(t, uint64(2), nh.MessageCounter)
|
||||||
|
assert.Equal(t, uint32(1234), h.FragID)
|
||||||
|
assert.Equal(t, uint16(9800), h.MsgLen)
|
||||||
|
out = append(out, int(h.Index))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
now, later, err := s.Attempt(3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8}, indexes(now))
|
||||||
|
assert.Equal(t, []int{9, 10, 11}, indexes(later))
|
||||||
|
|
||||||
|
// A retry offers only indexes the receiver has not seen.
|
||||||
|
now, later, err = s.Attempt(3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []int{12, 13, 14, 15, 16, 17, 18, 19, 20}, indexes(now))
|
||||||
|
assert.Equal(t, []int{21, 22, 23}, indexes(later))
|
||||||
|
|
||||||
|
// A sender rebuilt from the same message resumes where it was told to.
|
||||||
|
s2, err := NewSender(msg, testSubtype, 77, 2, 1234)
|
||||||
|
require.NoError(t, err)
|
||||||
|
s2.SetNext(s.Next())
|
||||||
|
now2, _, err := s2.Attempt(0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []int{24, 25, 26, 27, 28, 29, 30, 31, 32}, indexes(now2))
|
||||||
|
s2.SetNext(-1)
|
||||||
|
assert.Equal(t, 255, s2.Next())
|
||||||
|
|
||||||
|
// A cookie rides on every later packet, which still fits MaxPacketLen even
|
||||||
|
// with a full size symbol.
|
||||||
|
full, err := NewSender(make([]byte, 2*MaxSymbolSize), testSubtype, 0, 1, 9)
|
||||||
|
require.NoError(t, err)
|
||||||
|
full.SetCookie(Cookie{1, 2, 3})
|
||||||
|
pkts, _, err := full.Attempt(1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, p := range pkts {
|
||||||
|
assert.Len(t, p, MaxPacketLen)
|
||||||
|
_, h, _ := parse(t, p)
|
||||||
|
assert.True(t, h.HasCookie())
|
||||||
|
assert.Equal(t, Cookie{1, 2, 3}, h.Cookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Indexes wrap once the space is used up.
|
||||||
|
for range 19 {
|
||||||
|
_, _, err = s.Attempt(3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
now, _, err = s.Attempt(3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []int{252, 253, 254, 255, 0, 1, 2, 3, 4}, indexes(now))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestLossy runs whole messages through heavy loss, reordering and duplication,
|
||||||
|
// merging across attempts, and checks the table ends empty.
|
||||||
|
func TestLossy(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(2, 2))
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
|
||||||
|
for i := range 300 {
|
||||||
|
msg := randMsg(r, 1+r.IntN(16384))
|
||||||
|
fragID := uint32(i + 1)
|
||||||
|
s, err := NewSender(msg, testSubtype, 0, 1, fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var got *Result
|
||||||
|
for attempt := 0; got == nil; attempt++ {
|
||||||
|
require.Less(t, attempt, 20, "message never completed")
|
||||||
|
a, b, err := s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
var pkts [][]byte
|
||||||
|
for _, p := range append(a, b...) {
|
||||||
|
if r.Float64() < 0.3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
pkts = append(pkts, p)
|
||||||
|
if r.Float64() < 0.2 {
|
||||||
|
pkts = append(pkts, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
r.Shuffle(len(pkts), func(i, j int) { pkts[i], pkts[j] = pkts[j], pkts[i] })
|
||||||
|
|
||||||
|
for _, p := range pkts {
|
||||||
|
_, h, sym := parse(t, p)
|
||||||
|
res, err := tb.Add(testKey{"a", h.FragID}, "a", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if res != nil {
|
||||||
|
// The table keeps no memory of completed messages, so a late
|
||||||
|
// duplicate can complete one again; the handshake layer
|
||||||
|
// recognises the repeat.
|
||||||
|
require.True(t, bytes.Equal(msg, res.Msg))
|
||||||
|
require.Len(t, res.Packet, header.Len+len(msg))
|
||||||
|
require.Equal(t, &res.Packet[header.Len], &res.Msg[0], "Msg follows room for a header")
|
||||||
|
got = res
|
||||||
|
}
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
now = now.Add(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
require.Positive(t, got.Received)
|
||||||
|
// Late fragments of a completed message start a fresh entry; drop it.
|
||||||
|
tb.Expire(now.Add(time.Hour))
|
||||||
|
}
|
||||||
|
assert.Zero(t, tb.Len())
|
||||||
|
assert.Zero(t, tb.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestConcurrent spreads each message's fragments across goroutines, as
|
||||||
|
// multiple UDP reader routines would.
|
||||||
|
func TestConcurrent(t *testing.T) {
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
r := rand.New(rand.NewPCG(3, 3))
|
||||||
|
|
||||||
|
const msgs = 50
|
||||||
|
want := make(map[uint32][]byte, msgs)
|
||||||
|
var pkts [][]byte
|
||||||
|
for id := uint32(1); id <= msgs; id++ {
|
||||||
|
msg := randMsg(r, 1+r.IntN(16384))
|
||||||
|
want[id] = msg
|
||||||
|
s, err := NewSender(msg, testSubtype, 0, 1, id)
|
||||||
|
require.NoError(t, err)
|
||||||
|
a, b, err := s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
pkts = append(pkts, a...)
|
||||||
|
pkts = append(pkts, b...)
|
||||||
|
}
|
||||||
|
r.Shuffle(len(pkts), func(i, j int) { pkts[i], pkts[j] = pkts[j], pkts[i] })
|
||||||
|
|
||||||
|
var mu sync.Mutex
|
||||||
|
done := map[uint32]bool{}
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for w := range 8 {
|
||||||
|
wg.Go(func() {
|
||||||
|
for i := w; i < len(pkts); i += 8 {
|
||||||
|
h, sym, err := Parse(pkts[i][16:])
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
src := string(rune('a' + h.FragID))
|
||||||
|
res, err := tb.Add(testKey{src, h.FragID}, src, now, h, sym)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if res != nil {
|
||||||
|
if !bytes.Equal(want[h.FragID], res.Msg) {
|
||||||
|
t.Errorf("message %d decoded wrong", h.FragID)
|
||||||
|
}
|
||||||
|
mu.Lock()
|
||||||
|
done[h.FragID] = true
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
assert.Len(t, done, msgs)
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstFragment(t *testing.T, msgLen int, fragID uint32) (Header, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
s, err := NewSender(make([]byte, msgLen), testSubtype, 0, 1, fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
now, _, err := s.Attempt(0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, h, sym := parse(t, now[0])
|
||||||
|
return h, sym
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTableLimits(t *testing.T) {
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
|
||||||
|
t.Run("max message length", func(t *testing.T) {
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
h, sym := firstFragment(t, 16385, 1)
|
||||||
|
_, err := tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.ErrorIs(t, err, ErrTooLarge)
|
||||||
|
assert.Zero(t, tb.Len())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("per source quota", func(t *testing.T) {
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.MaxPerSource = 2
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
for id := uint32(1); id <= 3; id++ {
|
||||||
|
h, sym := firstFragment(t, 5000, id)
|
||||||
|
_, err := tb.Add(testKey{"a", id}, "a", now, h, sym)
|
||||||
|
if id <= 2 {
|
||||||
|
require.NoError(t, err)
|
||||||
|
} else {
|
||||||
|
require.ErrorIs(t, err, ErrSourceQuota)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
h, sym := firstFragment(t, 5000, 4)
|
||||||
|
_, err := tb.Add(testKey{"b", 4}, "b", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 3, tb.Len())
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("max entries evicts", func(t *testing.T) {
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.MaxEntries = 4
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
for id := uint32(1); id <= 10; id++ {
|
||||||
|
h, sym := firstFragment(t, 5000, id)
|
||||||
|
src := string(rune('a' + id))
|
||||||
|
_, err := tb.Add(testKey{src, id}, src, now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 4, tb.Len())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("max bytes evicts others", func(t *testing.T) {
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.MaxBytes = 3 * 1000
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
for id := uint32(1); id <= 10; id++ {
|
||||||
|
h, sym := firstFragment(t, 5000, id) // 5 symbols of 1000
|
||||||
|
src := string(rune('a' + id))
|
||||||
|
_, err := tb.Add(testKey{src, id}, src, now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 3000, tb.Bytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("no room removes new entry", func(t *testing.T) {
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.MaxBytes = 500
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
h, sym := firstFragment(t, 5000, 1)
|
||||||
|
_, err := tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.ErrorIs(t, err, ErrNoRoom)
|
||||||
|
assert.Zero(t, tb.Len())
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("duplicates are not charged", func(t *testing.T) {
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
h, sym := firstFragment(t, 5000, 1)
|
||||||
|
for range 5 {
|
||||||
|
_, err := tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
assert.Equal(t, 1000, tb.Bytes())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("mismatch drops the entry", func(t *testing.T) {
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
h, sym := firstFragment(t, 5000, 1)
|
||||||
|
_, err := tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
h2, sym2 := firstFragment(t, 6000, 1)
|
||||||
|
_, err = tb.Add(testKey{"a", 1}, "a", now, h2, sym2)
|
||||||
|
require.ErrorIs(t, err, ErrMismatch)
|
||||||
|
assert.Zero(t, tb.Len())
|
||||||
|
assert.Zero(t, tb.Bytes())
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("load", func(t *testing.T) {
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.MaxEntries = 4
|
||||||
|
cfg.MaxBytes = 10000
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
assert.Zero(t, tb.Load())
|
||||||
|
h, sym := firstFragment(t, 5000, 1)
|
||||||
|
_, err := tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.InDelta(t, 0.25, tb.Load(), 0.001, "one of four entries")
|
||||||
|
h, sym = firstFragment(t, 5000, 2)
|
||||||
|
_, err = tb.Add(testKey{"b", 2}, "b", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.InDelta(t, 0.5, tb.Load(), 0.001)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("ttl", func(t *testing.T) {
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
s, err := NewSender(make([]byte, 5000), testSubtype, 0, 1, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
pkts, _, err := s.Attempt(0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, h, sym := parse(t, pkts[0])
|
||||||
|
_, err = tb.Add(testKey{"a", 1}, "a", now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// Activity within the TTL keeps the entry and slides its deadline.
|
||||||
|
_, h, sym = parse(t, pkts[1])
|
||||||
|
_, err = tb.Add(testKey{"a", 1}, "a", now.Add(1500*time.Millisecond), h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 2000, tb.Bytes())
|
||||||
|
|
||||||
|
tb.Expire(now.Add(3 * time.Second))
|
||||||
|
assert.Equal(t, 1, tb.Len())
|
||||||
|
|
||||||
|
// A stale entry is restarted rather than merged.
|
||||||
|
_, h, sym = parse(t, pkts[2])
|
||||||
|
_, err = tb.Add(testKey{"a", 1}, "a", now.Add(10*time.Second), h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1000, tb.Bytes())
|
||||||
|
|
||||||
|
tb.Expire(now.Add(time.Minute))
|
||||||
|
assert.Zero(t, tb.Len())
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func FuzzParse(f *testing.F) {
|
||||||
|
f.Add(append(Header{FragID: 1, MsgLen: 3000}.Encode(nil), make([]byte, 1000)...))
|
||||||
|
f.Add([]byte{0, 0, 0, 1, 0, 1, 0, 0, 7})
|
||||||
|
f.Fuzz(func(t *testing.T, b []byte) {
|
||||||
|
h, sym, err := Parse(b)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
require.Equal(t, b, append(h.Encode(nil), sym...))
|
||||||
|
l, err := h.Layout()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.LessOrEqual(t, l.SymbolSize, MaxSymbolSize)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FuzzTable drives a small table with arbitrary fragments and checks its
|
||||||
|
// bookkeeping after every step.
|
||||||
|
func FuzzTable(f *testing.F) {
|
||||||
|
f.Add([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
|
||||||
|
f.Fuzz(func(t *testing.T, ops []byte) {
|
||||||
|
tb := NewTable[testKey, string](TableConfig{
|
||||||
|
MaxBytes: 4000,
|
||||||
|
MaxEntries: 3,
|
||||||
|
MaxPerSource: 2,
|
||||||
|
MaxMsgLen: 6000,
|
||||||
|
TTL: time.Second,
|
||||||
|
})
|
||||||
|
now := time.Unix(0, 0)
|
||||||
|
for len(ops) >= 4 {
|
||||||
|
src := string(rune('a' + ops[0]%4))
|
||||||
|
fragID := uint32(ops[1]%5) + 1
|
||||||
|
msgLen := uint16(ops[2])*32 + 1
|
||||||
|
index := ops[3]
|
||||||
|
ops = ops[4:]
|
||||||
|
|
||||||
|
h := Header{FragID: fragID, MsgLen: msgLen, Index: index}
|
||||||
|
l, err := h.Layout()
|
||||||
|
require.NoError(t, err)
|
||||||
|
sym := make([]byte, l.SymbolSize)
|
||||||
|
if index%7 == 0 {
|
||||||
|
now = now.Add(600 * time.Millisecond)
|
||||||
|
tb.Expire(now)
|
||||||
|
}
|
||||||
|
res, err := tb.Add(testKey{src, fragID}, src, now, h, sym)
|
||||||
|
if res != nil {
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, res.Msg, int(msgLen))
|
||||||
|
}
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestTableAnswered sends many large messages from one source the way a responder
|
||||||
|
// sees back to back handshakes: each attempt's data fragments complete the
|
||||||
|
// message and its repair fragments trail. Remembering answered messages keeps the
|
||||||
|
// stragglers from piling up against the source quota, and a retry with fresh
|
||||||
|
// symbols still reports the message again.
|
||||||
|
func TestTableAnswered(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(3, 3))
|
||||||
|
tb := NewTable[testKey, string](testConfig())
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
const msgLen = 8009 // a hybrid msg1: K=7 data symbols, 3 repair
|
||||||
|
|
||||||
|
sendAll := func(key testKey, pkts [][]byte) []*Result {
|
||||||
|
var out []*Result
|
||||||
|
for _, p := range pkts {
|
||||||
|
_, h, sym := parse(t, p)
|
||||||
|
res, err := tb.Add(key, key.src, now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if res != nil {
|
||||||
|
out = append(out, res)
|
||||||
|
}
|
||||||
|
checkInvariants(t, tb)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := range 3 * testConfig().MaxPerSource {
|
||||||
|
msg := randMsg(r, msgLen)
|
||||||
|
key := testKey{"a", uint32(i + 1)}
|
||||||
|
s, err := NewSender(msg, testSubtype, 0, 1, key.fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
data, repair, err := s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Len(t, data, 7)
|
||||||
|
require.Len(t, repair, 3)
|
||||||
|
|
||||||
|
res := sendAll(key, data)
|
||||||
|
require.Len(t, res, 1, "message %d was refused", i)
|
||||||
|
require.Equal(t, msg, res[0].Msg)
|
||||||
|
tb.Answered(key, res[0], now)
|
||||||
|
|
||||||
|
assert.Empty(t, sendAll(key, repair), "stragglers are not a new message")
|
||||||
|
assert.Zero(t, tb.Len(), "and hold no entry")
|
||||||
|
now = now.Add(100 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A retry offers fresh symbols, and once K of them arrive it is a repeat.
|
||||||
|
msg := randMsg(r, msgLen)
|
||||||
|
key := testKey{"b", 99}
|
||||||
|
s, err := NewSender(msg, testSubtype, 0, 1, key.fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
data, repair, err := s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
res := sendAll(key, data)
|
||||||
|
require.Len(t, res, 1)
|
||||||
|
tb.Answered(key, res[0], now)
|
||||||
|
require.Empty(t, sendAll(key, repair))
|
||||||
|
|
||||||
|
data, repair, err = s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
res = sendAll(key, append(data, repair...))
|
||||||
|
require.Len(t, res, 1)
|
||||||
|
assert.True(t, res[0].Repeat)
|
||||||
|
assert.Nil(t, res[0].Msg)
|
||||||
|
assert.Equal(t, sha256.Sum256(msg), res[0].Hash)
|
||||||
|
assert.Positive(t, res[0].Received)
|
||||||
|
|
||||||
|
// Forgotten, the message is reassembled from scratch again.
|
||||||
|
tb.Forget(key)
|
||||||
|
data, _, err = s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
res = sendAll(key, data)
|
||||||
|
require.Len(t, res, 1)
|
||||||
|
assert.False(t, res[0].Repeat)
|
||||||
|
assert.Equal(t, msg, res[0].Msg)
|
||||||
|
|
||||||
|
// Answered messages expire with the TTL like entries do.
|
||||||
|
tb.Answered(key, res[0], now)
|
||||||
|
tb.Expire(now.Add(time.Hour))
|
||||||
|
tb.mu.Lock()
|
||||||
|
assert.Empty(t, tb.answered)
|
||||||
|
tb.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestTableRepeatInterval checks that an answered message is reported as a
|
||||||
|
// repeat at most once per RepeatInterval on one key, while another key, a
|
||||||
|
// second path the same message arrives by, keeps its own clock.
|
||||||
|
func TestTableRepeatInterval(t *testing.T) {
|
||||||
|
r := rand.New(rand.NewPCG(4, 4))
|
||||||
|
cfg := testConfig()
|
||||||
|
cfg.RepeatInterval = 100 * time.Millisecond
|
||||||
|
tb := NewTable[testKey, string](cfg)
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
msg := randMsg(r, 8009) // K=7, 3 repair
|
||||||
|
|
||||||
|
attempt := func(s *Sender) [][]byte {
|
||||||
|
data, repair, err := s.Attempt(DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
return append(data, repair...)
|
||||||
|
}
|
||||||
|
repeats := func(key testKey, pkts [][]byte) int {
|
||||||
|
n := 0
|
||||||
|
for _, p := range pkts {
|
||||||
|
_, h, sym := parse(t, p)
|
||||||
|
res, err := tb.Add(key, key.src, now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if res != nil {
|
||||||
|
require.True(t, res.Repeat)
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
answer := func(key testKey, s *Sender) {
|
||||||
|
var res *Result
|
||||||
|
for _, p := range attempt(s) {
|
||||||
|
_, h, sym := parse(t, p)
|
||||||
|
got, err := tb.Add(key, key.src, now, h, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if got != nil {
|
||||||
|
res = got
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.NotNil(t, res)
|
||||||
|
tb.Answered(key, res, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two paths carry the same message. Each has its own sender here because
|
||||||
|
// the initiator offers the same symbols on every path.
|
||||||
|
direct := testKey{"direct", 1}
|
||||||
|
relayed := testKey{"relay", 1}
|
||||||
|
sd, err := NewSender(msg, testSubtype, 0, 1, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
sr, err := NewSender(msg, testSubtype, 0, 1, 1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
answer(direct, sd)
|
||||||
|
answer(relayed, sr)
|
||||||
|
|
||||||
|
// A retry right after the answer carries K fresh symbols, but the clock on
|
||||||
|
// that path has not run out.
|
||||||
|
now = now.Add(10 * time.Millisecond)
|
||||||
|
assert.Zero(t, repeats(direct, attempt(sd)), "a repeat inside the interval is not reported")
|
||||||
|
|
||||||
|
// Once it has, the first fresh symbol of the next retry is enough: the
|
||||||
|
// fresh symbols kept counting.
|
||||||
|
now = now.Add(100 * time.Millisecond)
|
||||||
|
retry := attempt(sd)
|
||||||
|
assert.Equal(t, 1, repeats(direct, retry[:1]), "the held repeat is reported at once")
|
||||||
|
assert.Zero(t, repeats(direct, retry[1:]), "and only once per retry")
|
||||||
|
|
||||||
|
// The other path has its own clock, which only its own answer started.
|
||||||
|
assert.Equal(t, 1, repeats(relayed, attempt(sr)), "a repeat on another path is independent")
|
||||||
|
assert.Zero(t, repeats(relayed, attempt(sr)), "and is itself clocked")
|
||||||
|
|
||||||
|
// Without an interval every K fresh symbols is a repeat, which is what the
|
||||||
|
// other tests rely on.
|
||||||
|
tb = NewTable[testKey, string](testConfig())
|
||||||
|
s2, err := NewSender(msg, testSubtype, 0, 1, 2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
k2 := testKey{"a", 2}
|
||||||
|
answer(k2, s2)
|
||||||
|
assert.Equal(t, 1, repeats(k2, attempt(s2)))
|
||||||
|
assert.Equal(t, 1, repeats(k2, attempt(s2)))
|
||||||
|
}
|
||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package fragment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/handshake/fec"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultRepair is the starting number of repair symbols per attempt for a
|
||||||
|
// message of k data symbols. Small messages get proportionally more repair.
|
||||||
|
func DefaultRepair(k int) int {
|
||||||
|
return max(2, (k+2)/3)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sender produces the fragment packets of one outgoing handshake message.
|
||||||
|
//
|
||||||
|
// The first attempt sends the K data symbols followed by repair symbols. Every
|
||||||
|
// later attempt sends symbols with indexes the receiver has not been offered yet,
|
||||||
|
// so whatever it already holds still counts toward K. Symbols are a deterministic
|
||||||
|
// function of the message, which keeps retransmits idempotent. Once all
|
||||||
|
// fec.MaxSymbols indexes are used the sequence wraps and repeats.
|
||||||
|
//
|
||||||
|
// A Sender is not safe for concurrent use.
|
||||||
|
type Sender struct {
|
||||||
|
enc *fec.Encoder
|
||||||
|
subtype header.MessageSubType
|
||||||
|
remoteIndex uint32
|
||||||
|
counter uint64
|
||||||
|
fragID uint32
|
||||||
|
next int
|
||||||
|
cookie *Cookie
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSender prepares msg for sending. subtype, remoteIndex and counter fill the
|
||||||
|
// Nebula header of every fragment; fragID is the sender's local index. The
|
||||||
|
// Sender keeps msg, which must not change while it is in use.
|
||||||
|
func NewSender(msg []byte, subtype header.MessageSubType, remoteIndex uint32, counter uint64, fragID uint32) (*Sender, error) {
|
||||||
|
if len(msg) == 0 || len(msg) > math.MaxUint16 {
|
||||||
|
return nil, ErrMsgLen
|
||||||
|
}
|
||||||
|
if fragID == 0 {
|
||||||
|
return nil, ErrFragID
|
||||||
|
}
|
||||||
|
enc, err := fec.NewEncoder(msg, MaxSymbolSize)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Sender{
|
||||||
|
enc: enc,
|
||||||
|
subtype: subtype,
|
||||||
|
remoteIndex: remoteIndex,
|
||||||
|
counter: counter,
|
||||||
|
fragID: fragID,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Sender) Layout() fec.Layout {
|
||||||
|
return s.enc.Layout()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetNext sets the index the next attempt starts from, so a sender rebuilt from
|
||||||
|
// a cached message can carry on offering fresh symbols.
|
||||||
|
func (s *Sender) SetNext(index int) {
|
||||||
|
s.next = ((index % fec.MaxSymbols) + fec.MaxSymbols) % fec.MaxSymbols
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetCookie attaches a cookie from a responder under load to every later packet.
|
||||||
|
func (s *Sender) SetCookie(c Cookie) {
|
||||||
|
s.cookie = &c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next returns the index the next attempt starts from.
|
||||||
|
func (s *Sender) Next() int {
|
||||||
|
return s.next
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt returns the packets for one transmission attempt carrying K+repair
|
||||||
|
// symbols. now should be sent immediately and later after the repair delay, so
|
||||||
|
// a tail drop at the end of the first burst does not also take the repair.
|
||||||
|
func (s *Sender) Attempt(repair int) (now, later [][]byte, err error) {
|
||||||
|
if repair < 0 {
|
||||||
|
return nil, nil, errors.New("negative repair count")
|
||||||
|
}
|
||||||
|
k := s.enc.Layout().K
|
||||||
|
now = make([][]byte, 0, k)
|
||||||
|
later = make([][]byte, 0, repair)
|
||||||
|
// One allocation backs every packet of the attempt.
|
||||||
|
pktCap := header.Len + HeaderLen + CookieLen + s.enc.Layout().SymbolSize
|
||||||
|
buf := make([]byte, (k+repair)*pktCap)
|
||||||
|
for i := range k + repair {
|
||||||
|
p, err := s.packet(buf[i*pktCap:i*pktCap:(i+1)*pktCap], s.next)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
s.next = (s.next + 1) % fec.MaxSymbols
|
||||||
|
if i < k {
|
||||||
|
now = append(now, p)
|
||||||
|
} else {
|
||||||
|
later = append(later, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return now, later, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// packet builds the packet for the symbol at index in the capacity of p, which
|
||||||
|
// must have room for it.
|
||||||
|
func (s *Sender) packet(p []byte, index int) ([]byte, error) {
|
||||||
|
l := s.enc.Layout()
|
||||||
|
p = header.Encode(p[:header.Len], header.Version, header.Handshake, s.subtype, s.remoteIndex, s.counter)
|
||||||
|
h := Header{
|
||||||
|
FragID: s.fragID,
|
||||||
|
MsgLen: uint16(l.MsgLen),
|
||||||
|
Index: uint8(index),
|
||||||
|
}
|
||||||
|
if s.cookie != nil {
|
||||||
|
h.Flags |= FlagCookie
|
||||||
|
h.Cookie = *s.cookie
|
||||||
|
}
|
||||||
|
p = h.Encode(p)
|
||||||
|
return s.enc.Symbol(p, index)
|
||||||
|
}
|
||||||
@@ -0,0 +1,342 @@
|
|||||||
|
package fragment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"errors"
|
||||||
|
"math/rand/v2"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/handshake/fec"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrTooLarge = errors.New("fragmented message exceeds the size limit")
|
||||||
|
ErrSourceQuota = errors.New("source has too many messages in progress")
|
||||||
|
ErrNoRoom = errors.New("no room for fragment")
|
||||||
|
)
|
||||||
|
|
||||||
|
// TableConfig bounds a Table.
|
||||||
|
type TableConfig struct {
|
||||||
|
MaxBytes int // symbol bytes held across all entries
|
||||||
|
MaxEntries int // messages in progress across all sources
|
||||||
|
MaxPerSource int // messages in progress per source
|
||||||
|
MaxMsgLen int // largest message accepted
|
||||||
|
TTL time.Duration // an entry idle this long is dropped
|
||||||
|
|
||||||
|
// RepeatInterval is how often, at most, an answered message is reported as a
|
||||||
|
// repeat on one key. Zero reports every K fresh symbols.
|
||||||
|
RepeatInterval time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Table reassembles messages from senders that have not authenticated yet, the
|
||||||
|
// responder's view of msg1. Every resource is capped. When a cap is hit a
|
||||||
|
// random entry is evicted; random eviction is harder to aim than LRU, which a
|
||||||
|
// flood can use to flush specific entries.
|
||||||
|
//
|
||||||
|
// K identifies one message, typically the source plus FragID. S identifies the
|
||||||
|
// source for quotas, typically the address masked to /32 or /64.
|
||||||
|
//
|
||||||
|
// A message the caller answered is remembered, see Answered, so the fragments
|
||||||
|
// still arriving for it do not start it over. Without that, the repair
|
||||||
|
// fragments trailing a message whose data fragments already completed it would
|
||||||
|
// open a new entry that, for K above the repair count, never completes: it holds
|
||||||
|
// symbols and a source's quota until it expires. A retry of an answered message
|
||||||
|
// is reported as a repeat, at most once per RepeatInterval on that key. Keys
|
||||||
|
// include the path a message arrives by, so the same message arriving by two
|
||||||
|
// paths is two keys with two clocks.
|
||||||
|
//
|
||||||
|
// A Table is safe for concurrent use.
|
||||||
|
type Table[K, S comparable] struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
cfg TableConfig
|
||||||
|
entries map[K]*tableEntry[K, S]
|
||||||
|
order []K
|
||||||
|
perSource map[S]int
|
||||||
|
bytes int
|
||||||
|
answered map[K]*answeredEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
// answeredEntry is what is kept of an answered message: no symbols, only which
|
||||||
|
// indexes have been seen, so a retry's fresh symbols can be told apart from
|
||||||
|
// stragglers of an attempt already decoded.
|
||||||
|
type answeredEntry struct {
|
||||||
|
hash [sha256.Size]byte
|
||||||
|
msgLen uint16
|
||||||
|
k int
|
||||||
|
seen [fec.MaxSymbols]bool
|
||||||
|
fresh int
|
||||||
|
received int
|
||||||
|
lastSeen time.Time
|
||||||
|
lastRepeat time.Time // when it was answered, or last reported as a repeat
|
||||||
|
}
|
||||||
|
|
||||||
|
type tableEntry[K, S comparable] struct {
|
||||||
|
a *Assembly
|
||||||
|
source S
|
||||||
|
lastSeen time.Time
|
||||||
|
pos int
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTable[K, S comparable](cfg TableConfig) *Table[K, S] {
|
||||||
|
return &Table[K, S]{
|
||||||
|
cfg: cfg,
|
||||||
|
entries: make(map[K]*tableEntry[K, S]),
|
||||||
|
perSource: make(map[S]int),
|
||||||
|
answered: make(map[K]*answeredEntry),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result describes a completed message.
|
||||||
|
type Result struct {
|
||||||
|
Msg []byte // the decoded message, nil for a repeat
|
||||||
|
Packet []byte // header.Len bytes for the caller's Nebula header, then Msg
|
||||||
|
Hash [sha256.Size]byte // sha256 of the message
|
||||||
|
Received int // packet bytes received for it, for the amplification budget
|
||||||
|
|
||||||
|
// Repeat means a message marked Answered was sent again: as many fresh
|
||||||
|
// symbols as it has data symbols arrived, which a retry offers and stragglers
|
||||||
|
// of an attempt already decoded do not.
|
||||||
|
Repeat bool
|
||||||
|
|
||||||
|
msgLen uint16
|
||||||
|
k int
|
||||||
|
seen [fec.MaxSymbols]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add records a fragment parsed by Parse. When the message is complete its entry
|
||||||
|
// is removed and the result returned; otherwise the result is nil. A decode
|
||||||
|
// error means the message is lost and must be retransmitted.
|
||||||
|
func (t *Table[K, S]) Add(key K, source S, now time.Time, h Header, sym []byte) (*Result, error) {
|
||||||
|
if int(h.MsgLen) > t.cfg.MaxMsgLen {
|
||||||
|
return nil, ErrTooLarge
|
||||||
|
}
|
||||||
|
|
||||||
|
t.mu.Lock()
|
||||||
|
if a, ok := t.answered[key]; ok {
|
||||||
|
if now.Sub(a.lastSeen) <= t.cfg.TTL {
|
||||||
|
res := a.add(now, h, sym, t.cfg.RepeatInterval)
|
||||||
|
t.mu.Unlock()
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
delete(t.answered, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, ok := t.entries[key]
|
||||||
|
if ok && now.Sub(e.lastSeen) > t.cfg.TTL {
|
||||||
|
t.remove(key, e)
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
var err error
|
||||||
|
e, err = t.insert(key, source, h)
|
||||||
|
if err != nil {
|
||||||
|
t.mu.Unlock()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.lastSeen = now
|
||||||
|
|
||||||
|
if !t.makeRoom(key, e.a.wouldStore(h, sym)) {
|
||||||
|
if e.a.Stored() == 0 {
|
||||||
|
t.remove(key, e)
|
||||||
|
}
|
||||||
|
t.mu.Unlock()
|
||||||
|
return nil, ErrNoRoom
|
||||||
|
}
|
||||||
|
|
||||||
|
before := e.a.Stored()
|
||||||
|
ready, err := e.a.Add(h, sym)
|
||||||
|
t.bytes += e.a.Stored() - before
|
||||||
|
if err != nil || !ready {
|
||||||
|
if errors.Is(err, ErrMismatch) || (err != nil && e.a.Stored() == 0) {
|
||||||
|
t.remove(key, e)
|
||||||
|
}
|
||||||
|
t.mu.Unlock()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
t.remove(key, e)
|
||||||
|
t.mu.Unlock()
|
||||||
|
|
||||||
|
pkt, err := e.a.Decode(make([]byte, header.Len))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
msg := pkt[header.Len:]
|
||||||
|
return &Result{
|
||||||
|
Msg: msg,
|
||||||
|
Packet: pkt,
|
||||||
|
Hash: sha256.Sum256(msg),
|
||||||
|
Received: e.a.Received(),
|
||||||
|
msgLen: e.a.msgLen,
|
||||||
|
k: e.a.dec.Layout().K,
|
||||||
|
seen: e.a.dec.Seen(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *answeredEntry) add(now time.Time, h Header, sym []byte, interval time.Duration) *Result {
|
||||||
|
if h.MsgLen != a.msgLen {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
a.lastSeen = now
|
||||||
|
a.received += header.Len + HeaderLen + len(sym)
|
||||||
|
if a.seen[h.Index] {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
a.seen[h.Index] = true
|
||||||
|
a.fresh++
|
||||||
|
if a.fresh < a.k || now.Sub(a.lastRepeat) < interval {
|
||||||
|
// Not enough fresh symbols yet, or this key was answered within the
|
||||||
|
// interval. The fresh symbols keep counting, so the first one to arrive
|
||||||
|
// once the interval is up reports the repeat.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
r := &Result{Hash: a.hash, Received: a.received, Repeat: true}
|
||||||
|
a.fresh = 0
|
||||||
|
a.received = 0
|
||||||
|
a.lastRepeat = now
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// Answered records that the caller answered res, the result of Add for key, so
|
||||||
|
// later fragments of it are counted rather than reassembled. A message is kept
|
||||||
|
// for the TTL after its last fragment, outside the entry, byte and source caps;
|
||||||
|
// at most MaxEntries are kept.
|
||||||
|
func (t *Table[K, S]) Answered(key K, res *Result, now time.Time) {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
// A straggler may have opened an entry while the caller worked.
|
||||||
|
if e, ok := t.entries[key]; ok {
|
||||||
|
t.remove(key, e)
|
||||||
|
}
|
||||||
|
if res.Repeat {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := t.answered[key]; !ok && len(t.answered) >= t.cfg.MaxEntries {
|
||||||
|
for k := range t.answered {
|
||||||
|
delete(t.answered, k)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.answered[key] = &answeredEntry{hash: res.Hash, msgLen: res.msgLen, k: res.k, seen: res.seen, lastSeen: now, lastRepeat: now}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forget drops what Answered recorded for key, so its fragments are reassembled
|
||||||
|
// again.
|
||||||
|
func (t *Table[K, S]) Forget(key K) {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
delete(t.answered, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Expire drops every entry idle for longer than the TTL.
|
||||||
|
func (t *Table[K, S]) Expire(now time.Time) {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
for i := 0; i < len(t.order); {
|
||||||
|
key := t.order[i]
|
||||||
|
e := t.entries[key]
|
||||||
|
if now.Sub(e.lastSeen) > t.cfg.TTL {
|
||||||
|
// remove swaps the last entry into position i, so look at i again.
|
||||||
|
t.remove(key, e)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
for key, a := range t.answered {
|
||||||
|
if now.Sub(a.lastSeen) > t.cfg.TTL {
|
||||||
|
delete(t.answered, key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Len returns the number of messages in progress.
|
||||||
|
func (t *Table[K, S]) Len() int {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return len(t.entries)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load returns how full the table is, from 0 to 1, by whichever of its byte and
|
||||||
|
// entry limits is closer.
|
||||||
|
func (t *Table[K, S]) Load() float64 {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return max(float64(t.bytes)/float64(t.cfg.MaxBytes), float64(len(t.entries))/float64(t.cfg.MaxEntries))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bytes returns the number of symbol bytes held.
|
||||||
|
func (t *Table[K, S]) Bytes() int {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return t.bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, S]) insert(key K, source S, h Header) (*tableEntry[K, S], error) {
|
||||||
|
if t.perSource[source] >= t.cfg.MaxPerSource {
|
||||||
|
return nil, ErrSourceQuota
|
||||||
|
}
|
||||||
|
a, err := NewAssembly(h)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for len(t.entries) >= t.cfg.MaxEntries {
|
||||||
|
if !t.evictRandom(nil) {
|
||||||
|
return nil, ErrNoRoom
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e := &tableEntry[K, S]{a: a, source: source, pos: len(t.order)}
|
||||||
|
t.entries[key] = e
|
||||||
|
t.order = append(t.order, key)
|
||||||
|
t.perSource[source]++
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// makeRoom evicts random entries other than keep until n more bytes fit.
|
||||||
|
func (t *Table[K, S]) makeRoom(keep K, n int) bool {
|
||||||
|
for t.bytes+n > t.cfg.MaxBytes {
|
||||||
|
if !t.evictRandom(&keep) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, S]) evictRandom(keep *K) bool {
|
||||||
|
candidates := len(t.order)
|
||||||
|
if keep != nil {
|
||||||
|
if _, ok := t.entries[*keep]; ok {
|
||||||
|
candidates--
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if candidates <= 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
key := t.order[rand.IntN(len(t.order))]
|
||||||
|
if keep != nil && key == *keep {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.remove(key, t.entries[key])
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Table[K, S]) remove(key K, e *tableEntry[K, S]) {
|
||||||
|
last := len(t.order) - 1
|
||||||
|
moved := t.order[last]
|
||||||
|
t.order[e.pos] = moved
|
||||||
|
t.entries[moved].pos = e.pos
|
||||||
|
t.order = t.order[:last]
|
||||||
|
delete(t.entries, key)
|
||||||
|
|
||||||
|
t.bytes -= e.a.Stored()
|
||||||
|
if t.perSource[e.source] <= 1 {
|
||||||
|
delete(t.perSource, e.source)
|
||||||
|
} else {
|
||||||
|
t.perSource[e.source]--
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
// Package fragment carries handshake messages that do not fit in one UDP
|
||||||
|
// packet. A message is Reed-Solomon coded into equally sized symbols (see
|
||||||
|
// handshake/fec) and each symbol travels in its own packet:
|
||||||
|
//
|
||||||
|
// Nebula header (16) | Fragment header (8) | [cookie (16)] | symbol
|
||||||
|
//
|
||||||
|
// The fragment header is FragID u32 | MsgLen u16 | Index u8 | Flags u8, all big
|
||||||
|
// endian. Both ends derive the symbol layout from MsgLen, so any MsgLen-derived
|
||||||
|
// value never has to be sent. FlagCookie marks a cookie a responder under load
|
||||||
|
// asked for; room for it is always reserved, so a fragment never outgrows
|
||||||
|
// MaxPacketLen.
|
||||||
|
package fragment
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/handshake/fec"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// HeaderLen is the size of the fragment header that follows the Nebula header.
|
||||||
|
HeaderLen = 8
|
||||||
|
|
||||||
|
// CookieLen is the size of the cookie a fragment carries when FlagCookie is set.
|
||||||
|
CookieLen = 16
|
||||||
|
|
||||||
|
// FlagCookie marks a fragment that carries a cookie after its header.
|
||||||
|
FlagCookie uint8 = 0x01
|
||||||
|
|
||||||
|
// MaxPacketLen bounds every fragment packet, Nebula header included. A relayed
|
||||||
|
// fragment gains a 16 byte header and a 16 byte tag, which lands exactly on
|
||||||
|
// the 1232 byte UDP payload available on an IPv6 minimum MTU path.
|
||||||
|
MaxPacketLen = 1200
|
||||||
|
|
||||||
|
// MaxSymbolSize is the largest symbol that fits in MaxPacketLen alongside a cookie.
|
||||||
|
MaxSymbolSize = MaxPacketLen - header.Len - HeaderLen - CookieLen
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrShortFragment = errors.New("fragment is too short")
|
||||||
|
ErrFragID = errors.New("fragment has a zero FragID")
|
||||||
|
ErrMsgLen = errors.New("fragment has a zero MsgLen")
|
||||||
|
ErrFlags = errors.New("fragment has reserved flags set")
|
||||||
|
ErrNoCookie = errors.New("fragment is too short for its cookie")
|
||||||
|
ErrSymbolSize = errors.New("fragment symbol size does not match its MsgLen")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cookie proves a sender received a responder's reply at its address.
|
||||||
|
type Cookie [CookieLen]byte
|
||||||
|
|
||||||
|
// Header is the fragment header.
|
||||||
|
type Header struct {
|
||||||
|
FragID uint32 // the sender's local index for this handshake, never zero
|
||||||
|
MsgLen uint16 // length of the whole message
|
||||||
|
Index uint8 // symbol index, data below K and repair from K
|
||||||
|
Flags uint8 // FlagCookie, other bits reserved and zero
|
||||||
|
Cookie Cookie // valid when Flags has FlagCookie
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Header) HasCookie() bool {
|
||||||
|
return h.Flags&FlagCookie != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encode appends the header, and its cookie if it has one, to b.
|
||||||
|
func (h Header) Encode(b []byte) []byte {
|
||||||
|
b = binary.BigEndian.AppendUint32(b, h.FragID)
|
||||||
|
b = binary.BigEndian.AppendUint16(b, h.MsgLen)
|
||||||
|
b = append(b, h.Index, h.Flags)
|
||||||
|
if h.HasCookie() {
|
||||||
|
b = append(b, h.Cookie[:]...)
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Layout returns the symbol layout the header's MsgLen implies.
|
||||||
|
func (h Header) Layout() (fec.Layout, error) {
|
||||||
|
return fec.NewLayout(int(h.MsgLen), MaxSymbolSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse validates the bytes following the Nebula header and splits them into
|
||||||
|
// the fragment header and the symbol. The symbol aliases b.
|
||||||
|
func Parse(b []byte) (Header, []byte, error) {
|
||||||
|
if len(b) < HeaderLen {
|
||||||
|
return Header{}, nil, ErrShortFragment
|
||||||
|
}
|
||||||
|
h := Header{
|
||||||
|
FragID: binary.BigEndian.Uint32(b[0:4]),
|
||||||
|
MsgLen: binary.BigEndian.Uint16(b[4:6]),
|
||||||
|
Index: b[6],
|
||||||
|
Flags: b[7],
|
||||||
|
}
|
||||||
|
if h.FragID == 0 {
|
||||||
|
return Header{}, nil, ErrFragID
|
||||||
|
}
|
||||||
|
if h.MsgLen == 0 {
|
||||||
|
return Header{}, nil, ErrMsgLen
|
||||||
|
}
|
||||||
|
if h.Flags&^FlagCookie != 0 {
|
||||||
|
return Header{}, nil, ErrFlags
|
||||||
|
}
|
||||||
|
b = b[HeaderLen:]
|
||||||
|
if h.HasCookie() {
|
||||||
|
if len(b) < CookieLen {
|
||||||
|
return Header{}, nil, ErrNoCookie
|
||||||
|
}
|
||||||
|
copy(h.Cookie[:], b)
|
||||||
|
b = b[CookieLen:]
|
||||||
|
}
|
||||||
|
|
||||||
|
l, err := h.Layout()
|
||||||
|
if err != nil {
|
||||||
|
return Header{}, nil, err
|
||||||
|
}
|
||||||
|
sym := b
|
||||||
|
if len(sym) != l.SymbolSize {
|
||||||
|
return Header{}, nil, ErrSymbolSize
|
||||||
|
}
|
||||||
|
return h, sym, nil
|
||||||
|
}
|
||||||
@@ -26,4 +26,7 @@ message NebulaHandshakeDetails {
|
|||||||
uint32 CertVersion = 8;
|
uint32 CertVersion = 8;
|
||||||
// reserved for WIP multiport
|
// reserved for WIP multiport
|
||||||
reserved 6, 7;
|
reserved 6, 7;
|
||||||
|
// 9 carried an ephemeral ML-KEM key in ix_psk2_mlkem1024, a handshake that
|
||||||
|
// was never released
|
||||||
|
reserved 9;
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
ct "github.com/slackhq/nebula/cert_test"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
|
|||||||
@@ -0,0 +1,257 @@
|
|||||||
|
package handshake
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/mlkem"
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/DefinedNet/noise"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
ct "github.com/slackhq/nebula/cert_test"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/noiseutil"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func hybridCipherSuite(curve cert.Curve) noise.CipherSuite {
|
||||||
|
if curve == cert.Curve_P256 {
|
||||||
|
return noise.NewCipherSuite(noiseutil.DHP256, noise.CipherAESGCM, noise.HashSHA256)
|
||||||
|
}
|
||||||
|
return noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newHybridCertState builds a peer holding a v3 certificate, with credentials for it and for its v2 form, the way
|
||||||
|
// the pki does.
|
||||||
|
func newHybridCertState(t *testing.T, curve cert.Curve, ca cert.Certificate, caKey []byte, name string, network string) (*testCertState, []byte) {
|
||||||
|
t.Helper()
|
||||||
|
c, _, rawPrivKey, _ := ct.NewTestCert(
|
||||||
|
cert.Version3, curve, ca, caKey,
|
||||||
|
name, ca.NotBefore(), ca.NotAfter(), []netip.Prefix{netip.MustParsePrefix(network)}, nil, nil,
|
||||||
|
)
|
||||||
|
priv, _, _, err := cert.UnmarshalPrivateKeyFromPEM(rawPrivKey)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return hybridCertStateFor(t, curve, c, priv), priv
|
||||||
|
}
|
||||||
|
|
||||||
|
func hybridCertStateFor(t *testing.T, curve cert.Curve, c cert.Certificate, priv []byte) *testCertState {
|
||||||
|
t.Helper()
|
||||||
|
hs3, err := c.MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
v2 := cert.V2Form(c)
|
||||||
|
hs2, err := v2.MarshalForHandshakes()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ncs := hybridCipherSuite(curve)
|
||||||
|
return &testCertState{
|
||||||
|
version: cert.Version3,
|
||||||
|
creds: map[cert.Version]*Credential{
|
||||||
|
cert.Version3: NewCredential(c, hs3, priv, ncs),
|
||||||
|
cert.Version2: NewCredential(v2, hs2, priv[:len(priv)-cert.MLKEM1024PrivateKeySize], ncs),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridMachine(t *testing.T, cs *testCertState, verifier CertVerifier, initiator bool, localIndex uint32) *Machine {
|
||||||
|
t.Helper()
|
||||||
|
m, err := NewMachine(
|
||||||
|
cert.Version3, cs.getCredential,
|
||||||
|
verifier, func() (uint32, error) { return localIndex, nil },
|
||||||
|
initiator, header.HandshakeIXHybridMLKEM1024,
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
type hybridPeers struct {
|
||||||
|
pool *cert.CAPool
|
||||||
|
ca cert.Certificate
|
||||||
|
caKey []byte
|
||||||
|
initCS, respCS *testCertState
|
||||||
|
initPriv, respPriv []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridPeers(t *testing.T, curve cert.Curve) hybridPeers {
|
||||||
|
t.Helper()
|
||||||
|
ca, _, caKey, _ := ct.NewTestCaCert(cert.Version3, curve, time.Now().Add(-time.Hour), time.Now().Add(time.Hour), nil, nil, nil)
|
||||||
|
pool := cert.NewCAPool()
|
||||||
|
require.NoError(t, pool.AddCA(ca))
|
||||||
|
initCS, initPriv := newHybridCertState(t, curve, ca, caKey, "initiator", "10.0.0.1/24")
|
||||||
|
respCS, respPriv := newHybridCertState(t, curve, ca, caKey, "responder", "10.0.0.2/24")
|
||||||
|
return hybridPeers{pool: pool, ca: ca, caKey: caKey, initCS: initCS, respCS: respCS, initPriv: initPriv, respPriv: respPriv}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHybridIX(t *testing.T) {
|
||||||
|
for _, curve := range []cert.Curve{cert.Curve_CURVE25519, cert.Curve_P256} {
|
||||||
|
t.Run(curve.String(), func(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, curve)
|
||||||
|
v := testVerifier(p.pool)
|
||||||
|
initM := newHybridMachine(t, p.initCS, v, true, 1000)
|
||||||
|
respM := newHybridMachine(t, p.respCS, v, false, 2000)
|
||||||
|
|
||||||
|
msg1, err := initM.Initiate(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// The responder answers msg1 but can not complete until msg3.
|
||||||
|
msg2, respResult, err := respM.ProcessPacket(nil, msg1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Nil(t, respResult)
|
||||||
|
require.NotEmpty(t, msg2)
|
||||||
|
|
||||||
|
// The initiator completes by writing msg3.
|
||||||
|
msg3, initResult, err := initM.ProcessPacket(nil, msg2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, initResult)
|
||||||
|
require.NotEmpty(t, msg3)
|
||||||
|
|
||||||
|
out, respResult, err := respM.ProcessPacket(nil, msg3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Nil(t, out)
|
||||||
|
require.NotNil(t, respResult)
|
||||||
|
|
||||||
|
t.Logf("msg1 %d B, msg2 %d B, msg3 %d B", len(msg1), len(msg2), len(msg3))
|
||||||
|
for i, msg := range [][]byte{msg1, msg2, msg3} {
|
||||||
|
h := &header.H{}
|
||||||
|
require.NoError(t, h.Parse(msg))
|
||||||
|
assert.Equal(t, header.HandshakeIXHybridMLKEM1024, h.Subtype)
|
||||||
|
assert.Equal(t, uint64(i+1), h.MessageCounter)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, uint32(2000), initResult.RemoteIndex)
|
||||||
|
assert.Equal(t, uint32(1000), respResult.RemoteIndex)
|
||||||
|
assert.Equal(t, cert.Version3, initResult.RemoteCert.Certificate.Version())
|
||||||
|
assert.Equal(t, cert.Version3, respResult.RemoteCert.Certificate.Version())
|
||||||
|
assert.Equal(t, "responder", initResult.RemoteCert.Certificate.Name())
|
||||||
|
assert.Equal(t, "initiator", respResult.RemoteCert.Certificate.Name())
|
||||||
|
assert.Equal(t, cert.Version3, initResult.MyCert.Version())
|
||||||
|
assert.Equal(t, uint64(3), initResult.MessageIndex)
|
||||||
|
assert.Equal(t, uint64(3), respResult.MessageIndex)
|
||||||
|
|
||||||
|
// The keys pair up in both directions.
|
||||||
|
for _, pair := range [][2]*Result{{initResult, respResult}, {respResult, initResult}} {
|
||||||
|
ct, err := pair[0].EKey.Encrypt(nil, nil, []byte("hello"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
pt, err := pair[1].DKey.Decrypt(nil, nil, ct)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, []byte("hello"), pt)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A hybrid handshake needs v3 certificates, and a v3 certificate needs a hybrid handshake.
|
||||||
|
func TestHybridIX_CertVersions(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, cert.Curve_CURVE25519)
|
||||||
|
v := testVerifier(p.pool)
|
||||||
|
|
||||||
|
_, err := NewMachine(cert.Version2, p.initCS.getCredential, v, func() (uint32, error) { return 1, nil }, true, header.HandshakeIXHybridMLKEM1024)
|
||||||
|
require.ErrorIs(t, err, ErrCertVersionMismatch)
|
||||||
|
_, err = NewMachine(cert.Version3, p.initCS.getCredential, v, func() (uint32, error) { return 1, nil }, true, header.HandshakeIXPSK0)
|
||||||
|
require.ErrorIs(t, err, ErrCertVersionMismatch)
|
||||||
|
|
||||||
|
// The v2 form still does a classical handshake, here with a peer that only has v2.
|
||||||
|
ca2 := cert.V2Form(p.ca)
|
||||||
|
pool2 := cert.NewCAPool()
|
||||||
|
require.NoError(t, pool2.AddCA(ca2))
|
||||||
|
classicalCAKey := p.caKey[:len(p.caKey)-cert.MLDSA87PrivateKeySize]
|
||||||
|
v2Peer := newTestCertState(t, ca2, classicalCAKey, "v2peer", []netip.Prefix{netip.MustParsePrefix("10.0.0.3/24")})
|
||||||
|
p.initCS.version = cert.Version2
|
||||||
|
initResult, respResult := doFullHandshake(t, p.initCS, v2Peer, pool2)
|
||||||
|
assert.Equal(t, cert.Version2, initResult.RemoteCert.Certificate.Version())
|
||||||
|
assert.Equal(t, cert.Version2, respResult.RemoteCert.Certificate.Version())
|
||||||
|
assert.Equal(t, cert.Version2, initResult.MyCert.Version())
|
||||||
|
}
|
||||||
|
|
||||||
|
// A responder that only trusts the v2 form of the CA can not verify a v3 certificate.
|
||||||
|
func TestHybridIX_V2TrustOnly(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, cert.Curve_CURVE25519)
|
||||||
|
pool2 := cert.NewCAPool()
|
||||||
|
require.NoError(t, pool2.AddCA(cert.V2Form(p.ca)))
|
||||||
|
|
||||||
|
initM := newHybridMachine(t, p.initCS, testVerifier(p.pool), true, 1000)
|
||||||
|
respM := newHybridMachine(t, p.respCS, testVerifier(pool2), false, 2000)
|
||||||
|
msg1, err := initM.Initiate(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, _, err = respM.ProcessPacket(nil, msg1)
|
||||||
|
require.ErrorIs(t, err, cert.ErrCaNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A corrupted msg3 is dropped without killing the responder, and the real one still completes.
|
||||||
|
func TestHybridIX_CorruptMsg3(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, cert.Curve_CURVE25519)
|
||||||
|
v := testVerifier(p.pool)
|
||||||
|
initM := newHybridMachine(t, p.initCS, v, true, 1000)
|
||||||
|
respM := newHybridMachine(t, p.respCS, v, false, 2000)
|
||||||
|
|
||||||
|
msg1, err := initM.Initiate(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
msg2, _, err := respM.ProcessPacket(nil, msg1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
msg3, _, err := initM.ProcessPacket(nil, msg2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
bad := append([]byte(nil), msg3...)
|
||||||
|
bad[header.Len+10] ^= 1
|
||||||
|
_, res, err := respM.ProcessPacket(nil, bad)
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Nil(t, res)
|
||||||
|
require.False(t, respM.Failed())
|
||||||
|
|
||||||
|
_, res, err = respM.ProcessPacket(nil, msg3)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// withKEMKey returns a peer holding c and its classical private key, but some other ML-KEM key: someone who broke
|
||||||
|
// the classical key, say with a quantum computer, and wants to be that peer.
|
||||||
|
func withKEMKey(t *testing.T, curve cert.Curve, c cert.Certificate, priv []byte) *testCertState {
|
||||||
|
t.Helper()
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
require.NoError(t, err)
|
||||||
|
forged := append(append([]byte(nil), priv[:len(priv)-cert.MLKEM1024PrivateKeySize]...), dk.Bytes()...)
|
||||||
|
return hybridCertStateFor(t, curve, c, forged)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The classical private key alone does not get anyone through as the initiator: msg2 is unreadable without the
|
||||||
|
// initiator's ML-KEM key.
|
||||||
|
func TestHybridIX_ImpersonateInitiator(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, cert.Curve_CURVE25519)
|
||||||
|
v := testVerifier(p.pool)
|
||||||
|
victim := p.initCS.creds[cert.Version3].Cert
|
||||||
|
attacker := withKEMKey(t, cert.Curve_CURVE25519, victim, p.initPriv)
|
||||||
|
|
||||||
|
initM := newHybridMachine(t, attacker, v, true, 1000)
|
||||||
|
respM := newHybridMachine(t, p.respCS, v, false, 2000)
|
||||||
|
msg1, err := initM.Initiate(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
msg2, _, err := respM.ProcessPacket(nil, msg1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, res, err := initM.ProcessPacket(nil, msg2)
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Nil(t, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The classical private key alone does not get anyone through as the responder either: the initiator completes,
|
||||||
|
// but msg3 is unreadable without the responder's ML-KEM key, so no packet under the new keys can ever come back.
|
||||||
|
func TestHybridIX_ImpersonateResponder(t *testing.T) {
|
||||||
|
p := newHybridPeers(t, cert.Curve_CURVE25519)
|
||||||
|
v := testVerifier(p.pool)
|
||||||
|
victim := p.respCS.creds[cert.Version3].Cert
|
||||||
|
attacker := withKEMKey(t, cert.Curve_CURVE25519, victim, p.respPriv)
|
||||||
|
|
||||||
|
initM := newHybridMachine(t, p.initCS, v, true, 1000)
|
||||||
|
respM := newHybridMachine(t, attacker, v, false, 2000)
|
||||||
|
msg1, err := initM.Initiate(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
msg2, _, err := respM.ProcessPacket(nil, msg1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
msg3, initResult, err := initM.ProcessPacket(nil, msg2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, initResult)
|
||||||
|
|
||||||
|
_, res, err := respM.ProcessPacket(nil, msg3)
|
||||||
|
require.Error(t, err)
|
||||||
|
require.Nil(t, res)
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package handshake
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/mlkem"
|
||||||
|
"crypto/subtle"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/DefinedNet/noise"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrInvalidKEMKey = errors.New("peer sent an invalid ML-KEM encapsulation key")
|
||||||
|
|
||||||
|
// KEMMLKEM1024 is ML-KEM-1024 for the noise library's hybrid patterns. Private
|
||||||
|
// keys are the 64 byte seed, the same form v3 private keys store.
|
||||||
|
var KEMMLKEM1024 noise.KEMFunc = kemMLKEM1024{}
|
||||||
|
|
||||||
|
type kemMLKEM1024 struct{}
|
||||||
|
|
||||||
|
// GenerateKeypair ignores random, crypto/mlkem always draws from crypto/rand.
|
||||||
|
func (kemMLKEM1024) GenerateKeypair(_ io.Reader) (noise.KEMKey, error) {
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
if err != nil {
|
||||||
|
return noise.KEMKey{}, fmt.Errorf("mlkem1024: GenerateKey: %w", err)
|
||||||
|
}
|
||||||
|
return noise.KEMKey{Private: dk.Bytes(), Public: dk.EncapsulationKey().Bytes()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kemMLKEM1024) Encapsulate(pubkey []byte) ([]byte, []byte, error) {
|
||||||
|
ek, err := mlkem.NewEncapsulationKey1024(pubkey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("%w: %w", ErrInvalidKEMKey, err)
|
||||||
|
}
|
||||||
|
ss, ct := ek.Encapsulate()
|
||||||
|
return ct, ss, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kemMLKEM1024) Decapsulate(privkey, ciphertext []byte) ([]byte, error) {
|
||||||
|
dk, err := mlkem.NewDecapsulationKey1024(privkey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("mlkem1024: NewDecapsulationKey: %w", err)
|
||||||
|
}
|
||||||
|
return dk.Decapsulate(ciphertext)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mlkem1024Keys is KEMMLKEM1024 for one handshake. It decapsulates with the
|
||||||
|
// keys it holds expanded, the static one from the Credential and the ephemeral
|
||||||
|
// one it generated, rather than expanding them again from the seeds noise hands
|
||||||
|
// back; any other private key is expanded as usual.
|
||||||
|
type mlkem1024Keys struct {
|
||||||
|
kemMLKEM1024
|
||||||
|
static *mlkem.DecapsulationKey1024
|
||||||
|
staticSeed []byte
|
||||||
|
ephemeral *mlkem.DecapsulationKey1024
|
||||||
|
ephemeralSeed []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *mlkem1024Keys) GenerateKeypair(_ io.Reader) (noise.KEMKey, error) {
|
||||||
|
dk, err := mlkem.GenerateKey1024()
|
||||||
|
if err != nil {
|
||||||
|
return noise.KEMKey{}, fmt.Errorf("mlkem1024: GenerateKey: %w", err)
|
||||||
|
}
|
||||||
|
k.ephemeral, k.ephemeralSeed = dk, dk.Bytes()
|
||||||
|
return noise.KEMKey{Private: k.ephemeralSeed, Public: dk.EncapsulationKey().Bytes()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k *mlkem1024Keys) Decapsulate(privkey, ciphertext []byte) ([]byte, error) {
|
||||||
|
switch {
|
||||||
|
case k.static != nil && subtle.ConstantTimeCompare(privkey, k.staticSeed) == 1:
|
||||||
|
return k.static.Decapsulate(ciphertext)
|
||||||
|
case k.ephemeral != nil && subtle.ConstantTimeCompare(privkey, k.ephemeralSeed) == 1:
|
||||||
|
return k.ephemeral.Decapsulate(ciphertext)
|
||||||
|
}
|
||||||
|
return k.kemMLKEM1024.Decapsulate(privkey, ciphertext)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kemMLKEM1024) PublicKeyLen() int { return mlkem.EncapsulationKeySize1024 }
|
||||||
|
func (kemMLKEM1024) CiphertextLen() int { return mlkem.CiphertextSize1024 }
|
||||||
|
func (kemMLKEM1024) KEMName() string { return "MLKEM1024" }
|
||||||
+68
-21
@@ -6,7 +6,7 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
)
|
)
|
||||||
@@ -63,6 +63,7 @@ type Machine struct {
|
|||||||
msgs []msgFlags
|
msgs []msgFlags
|
||||||
myVersion cert.Version
|
myVersion cert.Version
|
||||||
subtype header.MessageSubType
|
subtype header.MessageSubType
|
||||||
|
hybrid bool
|
||||||
indexAllocated bool
|
indexAllocated bool
|
||||||
remoteCertSet bool
|
remoteCertSet bool
|
||||||
payloadSet bool
|
payloadSet bool
|
||||||
@@ -86,12 +87,24 @@ func NewMachine(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Version 3 certificates go with hybrid patterns and nothing else, the
|
||||||
|
// classical subtype uses the v2 form instead.
|
||||||
|
hybrid := info.kem != nil
|
||||||
|
if hybrid != (version == cert.Version3) {
|
||||||
|
return nil, fmt.Errorf("%w: %v", ErrCertVersionMismatch, version)
|
||||||
|
}
|
||||||
|
|
||||||
cred := getCred(version)
|
cred := getCred(version)
|
||||||
if cred == nil {
|
if cred == nil {
|
||||||
return nil, fmt.Errorf("%w: %v", ErrNoCredential, version)
|
return nil, fmt.Errorf("%w: %v", ErrNoCredential, version)
|
||||||
}
|
}
|
||||||
|
if hybrid {
|
||||||
|
if err := checkHybridSuite(subtype, cred.Cert); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hs, err := cred.buildHandshakeState(initiator, info.pattern)
|
hs, err := cred.buildHandshakeState(initiator, info)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("build noise state: %w", err)
|
return nil, fmt.Errorf("build noise state: %w", err)
|
||||||
}
|
}
|
||||||
@@ -100,6 +113,7 @@ func NewMachine(
|
|||||||
hs: hs,
|
hs: hs,
|
||||||
subtype: subtype,
|
subtype: subtype,
|
||||||
msgs: info.msgs,
|
msgs: info.msgs,
|
||||||
|
hybrid: hybrid,
|
||||||
getCred: getCred,
|
getCred: getCred,
|
||||||
allocIndex: allocIndex,
|
allocIndex: allocIndex,
|
||||||
verifier: verifier,
|
verifier: verifier,
|
||||||
@@ -121,6 +135,12 @@ func (m *Machine) Subtype() header.MessageSubType {
|
|||||||
return m.subtype
|
return m.subtype
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PeerCert returns the peer's verified certificate once a message carrying it
|
||||||
|
// has been read, which for a three message pattern is before completion.
|
||||||
|
func (m *Machine) PeerCert() *cert.CachedCertificate {
|
||||||
|
return m.result.RemoteCert
|
||||||
|
}
|
||||||
|
|
||||||
// MessageIndex returns the noise handshake message index, which equals the
|
// MessageIndex returns the noise handshake message index, which equals the
|
||||||
// wire counter of the most recently sent or received message.
|
// wire counter of the most recently sent or received message.
|
||||||
func (m *Machine) MessageIndex() int {
|
func (m *Machine) MessageIndex() int {
|
||||||
@@ -137,7 +157,8 @@ func (m *Machine) requireComplete() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// myMsgFlags returns the flags for the current outgoing message.
|
// myMsgFlags returns the flags for the current outgoing message, which are
|
||||||
|
// also the flags of the next incoming message before it is read.
|
||||||
func (m *Machine) myMsgFlags() msgFlags {
|
func (m *Machine) myMsgFlags() msgFlags {
|
||||||
idx := m.hs.MessageIndex()
|
idx := m.hs.MessageIndex()
|
||||||
if idx < len(m.msgs) {
|
if idx < len(m.msgs) {
|
||||||
@@ -220,12 +241,8 @@ func (m *Machine) ProcessPacket(out, packet []byte) ([]byte, *Result, error) {
|
|||||||
return nil, nil, ErrInitiateNotCalled
|
return nil, nil, ErrInitiateNotCalled
|
||||||
}
|
}
|
||||||
|
|
||||||
// The (eKey, dKey) ordering here is correct for IX, where the initiator
|
msg, cs1, cs2, err := m.hs.ReadMessage(nil, packet[header.Len:])
|
||||||
// completes the handshake by reading the responder's stage-2 message.
|
eKey, dKey := m.cipherStates(cs1, cs2)
|
||||||
// noise returns (cs1, cs2) where cs1 is the initiator->responder cipher.
|
|
||||||
// For 3-message patterns where a responder finishes by reading the final
|
|
||||||
// message, this ordering would be wrong; revisit when XX/pqIX lands.
|
|
||||||
msg, eKey, dKey, err := m.hs.ReadMessage(nil, packet[header.Len:])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Noise ReadMessage failed. The noise library checkpoints and rolls back
|
// Noise ReadMessage failed. The noise library checkpoints and rolls back
|
||||||
// on failure, so the Machine is still alive. The caller can retry with
|
// on failure, so the Machine is still alive. The caller can retry with
|
||||||
@@ -255,7 +272,7 @@ func (m *Machine) ProcessPacket(out, packet []byte) ([]byte, *Result, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadMessage didn't complete, produce the next outgoing message
|
// ReadMessage didn't complete, produce the next outgoing message
|
||||||
out, dk, ek, err := m.buildResponse(out)
|
out, ek, dk, err := m.buildResponse(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.failed = true
|
m.failed = true
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@@ -275,6 +292,15 @@ func (m *Machine) ProcessPacket(out, packet []byte) ([]byte, *Result, error) {
|
|||||||
return out, nil, nil
|
return out, nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cipherStates orders the cipher states noise returns, (cs1, cs2) with cs1 the
|
||||||
|
// initiator to responder direction, into this side's (eKey, dKey).
|
||||||
|
func (m *Machine) cipherStates(cs1, cs2 *noise.CipherState) (*noise.CipherState, *noise.CipherState) {
|
||||||
|
if m.result.Initiator {
|
||||||
|
return cs1, cs2
|
||||||
|
}
|
||||||
|
return cs2, cs1
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Machine) completed(eKey, dKey *noise.CipherState) *Result {
|
func (m *Machine) completed(eKey, dKey *noise.CipherState) *Result {
|
||||||
m.result.EKey = eKey
|
m.result.EKey = eKey
|
||||||
m.result.DKey = dKey
|
m.result.DKey = dKey
|
||||||
@@ -339,16 +365,36 @@ func (m *Machine) processPayload(msg []byte, flags msgFlags) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkHybridSuite checks that a version 3 certificate's suite uses the KEM of
|
||||||
|
// the hybrid subtype.
|
||||||
|
func checkHybridSuite(subtype header.MessageSubType, c cert.Certificate) error {
|
||||||
|
suite := cert.PQSuiteOf(c)
|
||||||
|
if st, ok := HybridSubtype(suite); !ok || st != subtype {
|
||||||
|
return fmt.Errorf("%w: %v", ErrPQSuiteMismatch, suite)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Machine) validateCert(payload Payload) error {
|
func (m *Machine) validateCert(payload Payload) error {
|
||||||
|
// A v3 certificate only ever comes with a hybrid pattern, and a hybrid
|
||||||
|
// pattern only ever with one. The composite static key would not recombine
|
||||||
|
// with anything else anyway, but say so plainly.
|
||||||
|
if m.hybrid != (cert.Version(payload.CertVersion) == cert.Version3) {
|
||||||
|
m.failed = true
|
||||||
|
return fmt.Errorf("%w: %v", ErrCertVersionMismatch, payload.CertVersion)
|
||||||
|
}
|
||||||
|
|
||||||
cred := m.getCred(m.myVersion)
|
cred := m.getCred(m.myVersion)
|
||||||
if cred == nil {
|
if cred == nil {
|
||||||
m.failed = true
|
m.failed = true
|
||||||
return fmt.Errorf("%w: %v", ErrNoCredential, m.myVersion)
|
return fmt.Errorf("%w: %v", ErrNoCredential, m.myVersion)
|
||||||
}
|
}
|
||||||
|
// PeerStatic builds a fresh composite key on every call for hybrid patterns.
|
||||||
|
peerStatic := m.hs.PeerStatic()
|
||||||
rc, err := cert.Recombine(
|
rc, err := cert.Recombine(
|
||||||
cert.Version(payload.CertVersion),
|
cert.Version(payload.CertVersion),
|
||||||
payload.Cert,
|
payload.Cert,
|
||||||
m.hs.PeerStatic(),
|
peerStatic,
|
||||||
cred.Cert.Curve(),
|
cred.Cert.Curve(),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -356,11 +402,18 @@ func (m *Machine) validateCert(payload Payload) error {
|
|||||||
return fmt.Errorf("recombine cert: %w", err)
|
return fmt.Errorf("recombine cert: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(rc.PublicKey(), m.hs.PeerStatic()) {
|
if !bytes.Equal(rc.PublicKey(), peerStatic) {
|
||||||
m.failed = true
|
m.failed = true
|
||||||
return ErrPublicKeyMismatch
|
return ErrPublicKeyMismatch
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if m.hybrid {
|
||||||
|
if err := checkHybridSuite(m.subtype, rc); err != nil {
|
||||||
|
m.failed = true
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Version negotiation, if the peer sent a different version and we have it, switch
|
// Version negotiation, if the peer sent a different version and we have it, switch
|
||||||
if rc.Version() != m.myVersion {
|
if rc.Version() != m.myVersion {
|
||||||
if m.getCred(rc.Version()) != nil {
|
if m.getCred(rc.Version()) != nil {
|
||||||
@@ -438,17 +491,11 @@ func (m *Machine) buildResponse(out []byte) ([]byte, *noise.CipherState, *noise.
|
|||||||
|
|
||||||
// noise.WriteMessage appends the encrypted handshake message to out,
|
// noise.WriteMessage appends the encrypted handshake message to out,
|
||||||
// reusing capacity when present.
|
// reusing capacity when present.
|
||||||
//
|
out, cs1, cs2, err := m.hs.WriteMessage(out, hsBytes)
|
||||||
// The (dKey, eKey) ordering here is correct for IX, where the responder
|
|
||||||
// completes the handshake by writing the stage-2 message. noise returns
|
|
||||||
// (cs1, cs2) where cs1 is the initiator->responder cipher (which is the
|
|
||||||
// responder's decrypt key). For 3-message patterns where an initiator
|
|
||||||
// finishes by writing the final message, this ordering would be wrong;
|
|
||||||
// revisit when XX/pqIX lands.
|
|
||||||
out, dKey, eKey, err := m.hs.WriteMessage(out, hsBytes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, fmt.Errorf("noise WriteMessage: %w", err)
|
return nil, nil, nil, fmt.Errorf("noise WriteMessage: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return out, dKey, eKey, nil
|
eKey, dKey := m.cipherStates(cs1, cs2)
|
||||||
|
return out, eKey, dKey, nil
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
ct "github.com/slackhq/nebula/cert_test"
|
ct "github.com/slackhq/nebula/cert_test"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
|
|||||||
+48
-1
@@ -3,7 +3,8 @@ package handshake
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,6 +19,11 @@ type msgFlags struct {
|
|||||||
type subtypeInfo struct {
|
type subtypeInfo struct {
|
||||||
pattern noise.HandshakePattern
|
pattern noise.HandshakePattern
|
||||||
msgs []msgFlags
|
msgs []msgFlags
|
||||||
|
|
||||||
|
// kem is the KEM of a hybrid pattern, nil for any other. Hybrid patterns
|
||||||
|
// carry composite static keys and need version 3 certificates on both
|
||||||
|
// sides, in suites using this KEM; every other pattern refuses them.
|
||||||
|
kem noise.KEMFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// subtypeInfos defines the noise pattern and message content layout for each
|
// subtypeInfos defines the noise pattern and message content layout for each
|
||||||
@@ -32,6 +38,27 @@ var subtypeInfos = map[header.MessageSubType]subtypeInfo{
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Hybrid IX (github.com/DefinedNet/noise, see its PQ_RESEARCH.md):
|
||||||
|
// msg1 (I->R): e, s payload + cert
|
||||||
|
// msg2 (R->I): ekem, skem, e, ee, se, s, es payload + cert
|
||||||
|
// msg3 (I->R): skem
|
||||||
|
// e and s are composite, classical || ML-KEM-1024, except the responder's e:
|
||||||
|
// it follows the responder's ekem, which already completed the ephemeral KEM
|
||||||
|
// exchange, so it carries only the classical key. The static ML-KEM key is
|
||||||
|
// the post-quantum half of the v3 certificate's public key, so it travels in
|
||||||
|
// s and the certificate leaves it out. The responder authenticates the
|
||||||
|
// initiator when msg3 decrypts, and the initiator the responder when the
|
||||||
|
// first packet under the new keys does.
|
||||||
|
header.HandshakeIXHybridMLKEM1024: {
|
||||||
|
pattern: noise.HandshakeHybridIX,
|
||||||
|
kem: KEMMLKEM1024,
|
||||||
|
msgs: []msgFlags{
|
||||||
|
{expectsPayload: true, expectsCert: true},
|
||||||
|
{expectsPayload: true, expectsCert: true},
|
||||||
|
{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
// XX: 3 messages
|
// XX: 3 messages
|
||||||
// msg1 (I->R): payload only
|
// msg1 (I->R): payload only
|
||||||
// msg2 (R->I): payload + cert
|
// msg2 (R->I): payload + cert
|
||||||
@@ -46,6 +73,26 @@ var subtypeInfos = map[header.MessageSubType]subtypeInfo{
|
|||||||
//},
|
//},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hybridSubtypes maps the suite of a version 3 certificate to the hybrid
|
||||||
|
// subtype for its KEM. Peers need the same KEM, not the same suite, so suites
|
||||||
|
// sharing a KEM share a subtype; the suite itself travels in the certificate.
|
||||||
|
var hybridSubtypes = map[cert.PQSuite]header.MessageSubType{
|
||||||
|
cert.PQSuite_MLKEM1024_MLDSA87: header.HandshakeIXHybridMLKEM1024,
|
||||||
|
}
|
||||||
|
|
||||||
|
// HybridSubtype returns the hybrid handshake subtype for the KEM of a version 3
|
||||||
|
// certificate's suite.
|
||||||
|
func HybridSubtype(suite cert.PQSuite) (header.MessageSubType, bool) {
|
||||||
|
st, ok := hybridSubtypes[suite]
|
||||||
|
return st, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsHybrid reports whether subtype is a hybrid handshake, one that needs version
|
||||||
|
// 3 certificates.
|
||||||
|
func IsHybrid(subtype header.MessageSubType) bool {
|
||||||
|
return subtypeInfos[subtype].kem != nil
|
||||||
|
}
|
||||||
|
|
||||||
func subtypeInfoFor(subtype header.MessageSubType) (subtypeInfo, error) {
|
func subtypeInfoFor(subtype header.MessageSubType) (subtypeInfo, error) {
|
||||||
if info, ok := subtypeInfos[subtype]; ok {
|
if info, ok := subtypeInfos[subtype]; ok {
|
||||||
return info, nil
|
return info, nil
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package handshake
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|||||||
+28
-19
@@ -1,8 +1,10 @@
|
|||||||
package handshake
|
package handshake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"math"
|
"math"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"google.golang.org/protobuf/encoding/protowire"
|
"google.golang.org/protobuf/encoding/protowire"
|
||||||
)
|
)
|
||||||
@@ -35,36 +37,43 @@ const (
|
|||||||
// with NebulaHandshake{Details: NebulaHandshakeDetails{...}}.
|
// with NebulaHandshake{Details: NebulaHandshakeDetails{...}}.
|
||||||
// Returns out (which may be nil), with the marshalled Payload appended to it.
|
// Returns out (which may be nil), with the marshalled Payload appended to it.
|
||||||
func MarshalPayload(out []byte, p Payload) []byte {
|
func MarshalPayload(out []byte, p Payload) []byte {
|
||||||
var details []byte
|
// Everything but the certificate fits on the stack, which leaves the
|
||||||
|
// certificate the only variable size and out grown just once.
|
||||||
if len(p.Cert) > 0 {
|
var scalars [4 * (1 + binary.MaxVarintLen64)]byte
|
||||||
details = protowire.AppendTag(details, fieldCert, protowire.BytesType)
|
rest := scalars[:0]
|
||||||
details = protowire.AppendBytes(details, p.Cert)
|
|
||||||
}
|
|
||||||
if p.InitiatorIndex != 0 {
|
if p.InitiatorIndex != 0 {
|
||||||
details = protowire.AppendTag(details, fieldInitiatorIndex, protowire.VarintType)
|
rest = protowire.AppendTag(rest, fieldInitiatorIndex, protowire.VarintType)
|
||||||
details = protowire.AppendVarint(details, uint64(p.InitiatorIndex))
|
rest = protowire.AppendVarint(rest, uint64(p.InitiatorIndex))
|
||||||
}
|
}
|
||||||
if p.ResponderIndex != 0 {
|
if p.ResponderIndex != 0 {
|
||||||
details = protowire.AppendTag(details, fieldResponderIndex, protowire.VarintType)
|
rest = protowire.AppendTag(rest, fieldResponderIndex, protowire.VarintType)
|
||||||
details = protowire.AppendVarint(details, uint64(p.ResponderIndex))
|
rest = protowire.AppendVarint(rest, uint64(p.ResponderIndex))
|
||||||
}
|
}
|
||||||
if p.Time != 0 {
|
if p.Time != 0 {
|
||||||
details = protowire.AppendTag(details, fieldTime, protowire.VarintType)
|
rest = protowire.AppendTag(rest, fieldTime, protowire.VarintType)
|
||||||
details = protowire.AppendVarint(details, p.Time)
|
rest = protowire.AppendVarint(rest, p.Time)
|
||||||
}
|
}
|
||||||
if p.CertVersion != 0 {
|
if p.CertVersion != 0 {
|
||||||
details = protowire.AppendTag(details, fieldCertVersion, protowire.VarintType)
|
rest = protowire.AppendTag(rest, fieldCertVersion, protowire.VarintType)
|
||||||
details = protowire.AppendVarint(details, uint64(p.CertVersion))
|
rest = protowire.AppendVarint(rest, uint64(p.CertVersion))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
detailsLen := len(rest)
|
||||||
|
if len(p.Cert) > 0 {
|
||||||
|
detailsLen += protowire.SizeTag(fieldCert) + protowire.SizeBytes(len(p.Cert))
|
||||||
|
}
|
||||||
|
out = slices.Grow(out, protowire.SizeTag(1)+protowire.SizeBytes(detailsLen))
|
||||||
out = protowire.AppendTag(out, 1, protowire.BytesType)
|
out = protowire.AppendTag(out, 1, protowire.BytesType)
|
||||||
out = protowire.AppendBytes(out, details)
|
out = protowire.AppendVarint(out, uint64(detailsLen))
|
||||||
|
if len(p.Cert) > 0 {
|
||||||
return out
|
out = protowire.AppendTag(out, fieldCert, protowire.BytesType)
|
||||||
|
out = protowire.AppendBytes(out, p.Cert)
|
||||||
|
}
|
||||||
|
return append(out, rest...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalPayload decodes a protobuf-encoded NebulaHandshake message.
|
// UnmarshalPayload decodes a protobuf-encoded NebulaHandshake message. The
|
||||||
|
// returned Cert aliases b.
|
||||||
func UnmarshalPayload(b []byte) (Payload, error) {
|
func UnmarshalPayload(b []byte) (Payload, error) {
|
||||||
var p Payload
|
var p Payload
|
||||||
|
|
||||||
@@ -119,7 +128,7 @@ func unmarshalPayloadDetails(p *Payload, b []byte) error {
|
|||||||
if n < 0 {
|
if n < 0 {
|
||||||
return errInvalidHandshakeDetails
|
return errInvalidHandshakeDetails
|
||||||
}
|
}
|
||||||
p.Cert = append([]byte(nil), v...)
|
p.Cert = v
|
||||||
b = b[n:]
|
b = b[n:]
|
||||||
case fieldInitiatorIndex:
|
case fieldInitiatorIndex:
|
||||||
if typ != protowire.VarintType {
|
if typ != protowire.VarintType {
|
||||||
|
|||||||
+236
-57
@@ -10,12 +10,15 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rcrowley/go-metrics"
|
"github.com/rcrowley/go-metrics"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/handshake"
|
"github.com/slackhq/nebula/handshake"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/ratelimit"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,6 +52,7 @@ type HandshakeConfig struct {
|
|||||||
tryInterval time.Duration
|
tryInterval time.Duration
|
||||||
retries int64
|
retries int64
|
||||||
triggerBuffer int
|
triggerBuffer int
|
||||||
|
pq pqHandshakeConfig
|
||||||
|
|
||||||
messageMetrics *MessageMetrics
|
messageMetrics *MessageMetrics
|
||||||
}
|
}
|
||||||
@@ -74,6 +78,20 @@ type HandshakeManager struct {
|
|||||||
|
|
||||||
// can be used to trigger outbound handshake for the given vpnIp
|
// can be used to trigger outbound handshake for the given vpnIp
|
||||||
trigger chan netip.Addr
|
trigger chan netip.Addr
|
||||||
|
|
||||||
|
// pq holds the post-quantum settings, which change on reload.
|
||||||
|
pq atomic.Pointer[pqHandshakeConfig]
|
||||||
|
// pqTable reassembles post-quantum msg1s from senders that have not authenticated yet.
|
||||||
|
pqTable *fragment.Table[pqFragKey, pqFragSource]
|
||||||
|
// pqPeers holds vpn addresses that completed a post-quantum handshake, for sticky mode.
|
||||||
|
pqPeers sync.Map
|
||||||
|
// pqAnswered recognises repeats of post-quantum msg1s already answered.
|
||||||
|
pqAnswered *pqAnswered
|
||||||
|
// pqCookies and pqCookieLimiter serve cookies while pqTable is under load.
|
||||||
|
pqCookies *pqCookies
|
||||||
|
pqCookieLimiter *ratelimit.Limiter
|
||||||
|
// hybrid holds hybrid handshakes waiting on their third message.
|
||||||
|
hybrid hybridState
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandshakeHostInfo struct {
|
type HandshakeHostInfo struct {
|
||||||
@@ -89,6 +107,11 @@ type HandshakeHostInfo struct {
|
|||||||
|
|
||||||
hostinfo *HostInfo
|
hostinfo *HostInfo
|
||||||
machine *handshake.Machine // The handshake state machine, set during stage 0 (initiator) or beginHandshake (responder multi-message)
|
machine *handshake.Machine // The handshake state machine, set during stage 0 (initiator) or beginHandshake (responder multi-message)
|
||||||
|
pq *pqAttempt // The initiator's post-quantum attempt, pending alongside the classical one
|
||||||
|
lastPQ bool // Whether the latest attempt was post-quantum
|
||||||
|
noPQ bool // The post-quantum attempt failed for good, only classical is left
|
||||||
|
|
||||||
|
responder *hybridResponder // Set for a responder's hybrid handshake waiting for msg3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hh *HandshakeHostInfo) cachePacket(l *slog.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
|
func (hh *HandshakeHostInfo) cachePacket(l *slog.Logger, t header.MessageType, st header.MessageSubType, packet []byte, f packetCallback, m *cachedPacketMetrics) {
|
||||||
@@ -117,7 +140,11 @@ func (hh *HandshakeHostInfo) cachePacket(l *slog.Logger, t header.MessageType, s
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewHandshakeManager(l *slog.Logger, mainHostMap *HostMap, lightHouse *LightHouse, outside udp.Conn, config HandshakeConfig) *HandshakeManager {
|
func NewHandshakeManager(l *slog.Logger, mainHostMap *HostMap, lightHouse *LightHouse, outside udp.Conn, config HandshakeConfig) *HandshakeManager {
|
||||||
return &HandshakeManager{
|
// A retrying initiator is answered again at most once per retry interval on
|
||||||
|
// each path its msg1 arrives by.
|
||||||
|
tableCfg := pqTableConfig
|
||||||
|
tableCfg.RepeatInterval = config.tryInterval
|
||||||
|
hm := &HandshakeManager{
|
||||||
vpnIps: map[netip.Addr]*HandshakeHostInfo{},
|
vpnIps: map[netip.Addr]*HandshakeHostInfo{},
|
||||||
indexes: map[uint32]*HandshakeHostInfo{},
|
indexes: map[uint32]*HandshakeHostInfo{},
|
||||||
mainHostMap: mainHostMap,
|
mainHostMap: mainHostMap,
|
||||||
@@ -130,7 +157,17 @@ func NewHandshakeManager(l *slog.Logger, mainHostMap *HostMap, lightHouse *Light
|
|||||||
metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
|
metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
|
||||||
metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
|
metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
|
||||||
l: l,
|
l: l,
|
||||||
|
pqTable: fragment.NewTable[pqFragKey, pqFragSource](tableCfg),
|
||||||
|
pqAnswered: newPQAnswered(hsTimeout(config.retries, config.tryInterval)),
|
||||||
|
hybrid: newHybridState(),
|
||||||
}
|
}
|
||||||
|
pq := config.pq
|
||||||
|
hm.pq.Store(&pq)
|
||||||
|
hm.pqCookies = newPQCookies(time.Now())
|
||||||
|
// Cookie replies are tiny, but a flood of spoofed fragments must not turn
|
||||||
|
// into a flood of replies.
|
||||||
|
hm.pqCookieLimiter, _ = ratelimit.New(256, time.Millisecond)
|
||||||
|
return hm
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hm *HandshakeManager) Run(ctx context.Context) {
|
func (hm *HandshakeManager) Run(ctx context.Context) {
|
||||||
@@ -145,6 +182,10 @@ func (hm *HandshakeManager) Run(ctx context.Context) {
|
|||||||
hm.handleOutbound(vpnIP, true)
|
hm.handleOutbound(vpnIP, true)
|
||||||
case now := <-clockSource.C:
|
case now := <-clockSource.C:
|
||||||
hm.NextOutboundHandshakeTimerTick(now)
|
hm.NextOutboundHandshakeTimerTick(now)
|
||||||
|
hm.pqTable.Expire(now)
|
||||||
|
hm.pqAnswered.expire(now)
|
||||||
|
hm.expireHybridResponders(now)
|
||||||
|
hm.tickHybridConfirms(now)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,8 +194,8 @@ func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *head
|
|||||||
// Gate on known handshake subtypes. Unknown subtypes (or future ones we
|
// Gate on known handshake subtypes. Unknown subtypes (or future ones we
|
||||||
// don't yet support) are dropped here rather than silently routed through
|
// don't yet support) are dropped here rather than silently routed through
|
||||||
// the IX path. Add a case when introducing a new pattern.
|
// the IX path. Add a case when introducing a new pattern.
|
||||||
switch h.Subtype {
|
switch {
|
||||||
case header.HandshakeIXPSK0:
|
case h.Subtype == header.HandshakeIXPSK0, handshake.IsHybrid(h.Subtype):
|
||||||
// supported
|
// supported
|
||||||
default:
|
default:
|
||||||
hm.l.Debug("dropping handshake with unsupported subtype",
|
hm.l.Debug("dropping handshake with unsupported subtype",
|
||||||
@@ -170,6 +211,11 @@ func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *head
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if header.IsFragmented(h.Subtype) {
|
||||||
|
hm.handleFragment(via, packet, h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// First message of a new handshake. The wire format requires RemoteIndex
|
// First message of a new handshake. The wire format requires RemoteIndex
|
||||||
// to be zero here (the initiator has no responder index to fill in yet),
|
// to be zero here (the initiator has no responder index to fill in yet),
|
||||||
// and generateIndex never allocates 0, so any non-zero RemoteIndex on a
|
// and generateIndex never allocates 0, so any non-zero RemoteIndex on a
|
||||||
@@ -181,7 +227,11 @@ func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *head
|
|||||||
"from", via, "remoteIndex", h.RemoteIndex)
|
"from", via, "remoteIndex", h.RemoteIndex)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hm.beginHandshake(via, packet, h)
|
if hm.pqConfig().require {
|
||||||
|
hm.l.Debug("dropping classical handshake, post-quantum handshakes are required", "from", via)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.beginHandshake(via, packet, h, 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +280,9 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
"style": header.SubTypeName(header.Handshake, hh.machine.Subtype()),
|
"style": header.SubTypeName(header.Handshake, hh.machine.Subtype()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
if hh.pq != nil {
|
||||||
|
fields = append(fields, "pqInitiatorIndex", hh.pq.index)
|
||||||
|
}
|
||||||
hh.hostinfo.logger(hm.l).Info("Handshake timed out", fields...)
|
hh.hostinfo.logger(hm.l).Info("Handshake timed out", fields...)
|
||||||
hm.metricTimedOut.Inc(1)
|
hm.metricTimedOut.Inc(1)
|
||||||
hm.DeleteHostInfo(hostinfo)
|
hm.DeleteHostInfo(hostinfo)
|
||||||
@@ -239,25 +292,45 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
// Increment the counter to increase our delay, linear backoff
|
// Increment the counter to increase our delay, linear backoff
|
||||||
hh.counter++
|
hh.counter++
|
||||||
|
|
||||||
// Check if we have a handshake packet to transmit yet
|
// Pick this attempt's style and make sure its first message exists. A PQ
|
||||||
if !hh.ready {
|
// attempt sends msg1 as fragments: now goes out immediately, later after the
|
||||||
if !hm.buildStage0Packet(hh) {
|
// repair delay, and every attempt offers fresh repair symbols.
|
||||||
|
var now, later [][]byte
|
||||||
|
var machine *handshake.Machine
|
||||||
|
hh.lastPQ = hm.usePQ(hh, vpnIp, lighthouseTriggered)
|
||||||
|
if hh.lastPQ {
|
||||||
|
if hh.pq == nil && !hm.buildPQAttempt(hh) {
|
||||||
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
var err error
|
||||||
|
now, later, err = hh.pq.sender.Attempt(fragment.DefaultRepair(hh.pq.sender.Layout().K))
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(hm.l).Error("Failed to fragment handshake", "error", err)
|
||||||
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
machine = hh.pq.machine
|
||||||
|
} else {
|
||||||
|
if !hh.ready {
|
||||||
|
if !hm.buildStage0Packet(hh) {
|
||||||
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: this hardcodes "always retransmit stage 0", which is correct for
|
||||||
|
// IX (the initiator only ever sends one packet, msg1) but wrong the
|
||||||
|
// moment a 3+ message pattern lands. The retry loop should resend the
|
||||||
|
// most recent outgoing message, not always stage 0. That implies
|
||||||
|
// HandshakeHostInfo tracking a single "currentOutbound" packet (bytes +
|
||||||
|
// header metadata) that gets replaced as the handshake progresses,
|
||||||
|
// instead of indexing into HandshakePacket.
|
||||||
|
now = [][]byte{hostinfo.HandshakePacket[handshakePacketStage0]}
|
||||||
|
machine = hh.machine
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this hardcodes "always retransmit stage 0", which is correct for
|
|
||||||
// IX (the initiator only ever sends one packet, msg1) but wrong the
|
|
||||||
// moment a 3+ message pattern lands. The retry loop should resend the
|
|
||||||
// most recent outgoing message, not always stage 0. That implies
|
|
||||||
// HandshakeHostInfo tracking a single "currentOutbound" packet (bytes +
|
|
||||||
// header metadata) that gets replaced as the handshake progresses,
|
|
||||||
// instead of indexing into HandshakePacket.
|
|
||||||
stage0 := hostinfo.HandshakePacket[handshakePacketStage0]
|
|
||||||
hsFields := m{
|
hsFields := m{
|
||||||
"stage": uint64(hh.machine.MessageIndex()),
|
"stage": uint64(machine.MessageIndex()),
|
||||||
"style": header.SubTypeName(header.Handshake, hh.machine.Subtype()),
|
"style": header.SubTypeName(header.Handshake, machine.Subtype()),
|
||||||
}
|
}
|
||||||
|
|
||||||
remotes := hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())
|
remotes := hostinfo.remotes.CopyAddrs(hm.mainHostMap.GetPreferredRanges())
|
||||||
@@ -285,8 +358,10 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
// Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
|
// Send the handshake to all known ips, stage 2 takes care of assigning the hostinfo.remote based on the first to reply
|
||||||
var sentTo []netip.AddrPort
|
var sentTo []netip.AddrPort
|
||||||
hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr netip.AddrPort, _ bool) {
|
hostinfo.remotes.ForEach(hm.mainHostMap.GetPreferredRanges(), func(addr netip.AddrPort, _ bool) {
|
||||||
hm.messageMetrics.Tx(header.Handshake, hh.machine.Subtype(), 1)
|
hm.messageMetrics.Tx(header.Handshake, machine.Subtype(), int64(len(now)+len(later)))
|
||||||
err := hm.outside.WriteTo(stage0, addr)
|
err := hm.sendFragmented(now, later, func(p []byte) error {
|
||||||
|
return hm.outside.WriteTo(p, addr)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// These repeat every attempt, so match the success log below and only shout when the remotes changed
|
// These repeat every attempt, so match the success log below and only shout when the remotes changed
|
||||||
level := slog.LevelDebug
|
level := slog.LevelDebug
|
||||||
@@ -322,7 +397,7 @@ func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
hm.f.relayManager.StartRelays(hm.f, vpnIp, hh, stage0)
|
hm.f.relayManager.StartRelays(hm.f, vpnIp, hh, now, later)
|
||||||
|
|
||||||
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
||||||
if !lighthouseTriggered {
|
if !lighthouseTriggered {
|
||||||
@@ -424,6 +499,13 @@ var (
|
|||||||
// ErrLocalIndexCollision if we already have an entry in the main or pending
|
// ErrLocalIndexCollision if we already have an entry in the main or pending
|
||||||
// hostmap for the hostinfo.localIndexId.
|
// hostmap for the hostinfo.localIndexId.
|
||||||
func (hm *HandshakeManager) CheckAndComplete(hostinfo *HostInfo, handshakePacket uint8, f *Interface) (*HostInfo, error) {
|
func (hm *HandshakeManager) CheckAndComplete(hostinfo *HostInfo, handshakePacket uint8, f *Interface) (*HostInfo, error) {
|
||||||
|
return hm.checkAndComplete(hostinfo, handshakePacket, f, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkAndComplete is CheckAndComplete. committed means the initiator has shown it
|
||||||
|
// uses this tunnel, as a hybrid msg3 does, so an existing tunnel is replaced even
|
||||||
|
// when its handshake is newer.
|
||||||
|
func (hm *HandshakeManager) checkAndComplete(hostinfo *HostInfo, handshakePacket uint8, f *Interface, committed bool) (*HostInfo, error) {
|
||||||
hm.mainHostMap.Lock()
|
hm.mainHostMap.Lock()
|
||||||
defer hm.mainHostMap.Unlock()
|
defer hm.mainHostMap.Unlock()
|
||||||
hm.Lock()
|
hm.Lock()
|
||||||
@@ -440,7 +522,7 @@ func (hm *HandshakeManager) CheckAndComplete(hostinfo *HostInfo, handshakePacket
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Is this a newer handshake?
|
// Is this a newer handshake?
|
||||||
if existingHostInfo.lastHandshakeTime >= hostinfo.lastHandshakeTime && !existingHostInfo.ConnectionState.initiator {
|
if !committed && existingHostInfo.lastHandshakeTime >= hostinfo.lastHandshakeTime && !existingHostInfo.ConnectionState.initiator {
|
||||||
return existingHostInfo, ErrExistingHostInfo
|
return existingHostInfo, ErrExistingHostInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,6 +614,10 @@ func (hm *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
|
|||||||
func (hm *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
|
func (hm *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
|
||||||
for _, addr := range hostinfo.vpnAddrs {
|
for _, addr := range hostinfo.vpnAddrs {
|
||||||
if cur, ok := hm.vpnIps[addr]; ok && cur.hostinfo == hostinfo {
|
if cur, ok := hm.vpnIps[addr]; ok && cur.hostinfo == hostinfo {
|
||||||
|
// A PQ attempt holds its own index alongside the hostinfo's.
|
||||||
|
if idx := cur.pqIndex(); idx != 0 && hm.indexes[idx] == cur {
|
||||||
|
delete(hm.indexes, idx)
|
||||||
|
}
|
||||||
delete(hm.vpnIps, addr)
|
delete(hm.vpnIps, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -642,9 +728,8 @@ func hsTimeout(tries int64, interval time.Duration) time.Duration {
|
|||||||
return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
|
return time.Duration(tries / 2 * ((2 * int64(interval)) + (tries-1)*int64(interval)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildStage0Packet creates the initial handshake packet for the initiator.
|
// initiatingVersion picks the certificate version to initiate a handshake with.
|
||||||
func (hm *HandshakeManager) buildStage0Packet(hh *HandshakeHostInfo) bool {
|
func (hm *HandshakeManager) initiatingVersion(hh *HandshakeHostInfo, cs *CertState) cert.Version {
|
||||||
cs := hm.f.pki.getCertState()
|
|
||||||
v := cs.DefaultVersion()
|
v := cs.DefaultVersion()
|
||||||
if hh.initiatingVersionOverride != cert.VersionPre1 {
|
if hh.initiatingVersionOverride != cert.VersionPre1 {
|
||||||
v = hh.initiatingVersionOverride
|
v = hh.initiatingVersionOverride
|
||||||
@@ -656,6 +741,13 @@ func (hm *HandshakeManager) buildStage0Packet(hh *HandshakeHostInfo) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildStage0Packet creates the initial handshake packet for the initiator.
|
||||||
|
func (hm *HandshakeManager) buildStage0Packet(hh *HandshakeHostInfo) bool {
|
||||||
|
cs := hm.f.pki.getCertState()
|
||||||
|
v := hm.initiatingVersion(hh, cs)
|
||||||
|
|
||||||
cred := cs.GetCredential(v)
|
cred := cs.GetCredential(v)
|
||||||
if cred == nil {
|
if cred == nil {
|
||||||
@@ -694,11 +786,40 @@ func (hm *HandshakeManager) buildStage0Packet(hh *HandshakeHostInfo) bool {
|
|||||||
// beginHandshake handles an incoming handshake packet that doesn't match any
|
// beginHandshake handles an incoming handshake packet that doesn't match any
|
||||||
// existing pending handshake. It creates a new responder Machine and processes
|
// existing pending handshake. It creates a new responder Machine and processes
|
||||||
// the first message.
|
// the first message.
|
||||||
func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *header.H) {
|
//
|
||||||
|
// received is the number of bytes a fragmented msg1 arrived in, which bounds the
|
||||||
|
// response sent back before the address is validated; 0 for unfragmented ones.
|
||||||
|
func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *header.H, received int) {
|
||||||
f := hm.f
|
f := hm.f
|
||||||
cs := f.pki.getCertState()
|
cs := f.pki.getCertState()
|
||||||
|
|
||||||
v := cs.DefaultVersion()
|
v := cs.DefaultVersion()
|
||||||
|
allocIndex := func() (uint32, error) { return generateIndex(f.l) }
|
||||||
|
|
||||||
|
// A hybrid responder answers with its v3 certificate and has to wait for msg3,
|
||||||
|
// so its index is registered right away for msg3 to find.
|
||||||
|
var pending *HandshakeHostInfo
|
||||||
|
if handshake.IsHybrid(h.Subtype) {
|
||||||
|
v = cert.Version3
|
||||||
|
pending = &HandshakeHostInfo{
|
||||||
|
hostinfo: &HostInfo{
|
||||||
|
HandshakePacket: make(map[uint8][]byte, 0),
|
||||||
|
relayState: RelayState{
|
||||||
|
relays: nil,
|
||||||
|
relayForByAddr: map[netip.Addr]*Relay{},
|
||||||
|
relayForByIdx: map[uint32]*Relay{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
startTime: time.Now(),
|
||||||
|
responder: &hybridResponder{},
|
||||||
|
}
|
||||||
|
// The index is visible to msg3 as soon as it is allocated, so hold the
|
||||||
|
// entry until it is filled in.
|
||||||
|
pending.Lock()
|
||||||
|
defer pending.Unlock()
|
||||||
|
allocIndex = func() (uint32, error) { return hm.allocateIndex(pending) }
|
||||||
|
}
|
||||||
|
|
||||||
if cs.GetCredential(v) == nil {
|
if cs.GetCredential(v) == nil {
|
||||||
f.l.Error("Unable to handshake with host because no certificate is available",
|
f.l.Error("Unable to handshake with host because no certificate is available",
|
||||||
"from", via, "certVersion", v)
|
"from", via, "certVersion", v)
|
||||||
@@ -707,8 +828,8 @@ func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *head
|
|||||||
|
|
||||||
machine, err := handshake.NewMachine(
|
machine, err := handshake.NewMachine(
|
||||||
v, cs.GetCredential,
|
v, cs.GetCredential,
|
||||||
hm.certVerifier(), func() (uint32, error) { return generateIndex(f.l) },
|
hm.certVerifier(), allocIndex,
|
||||||
false, header.HandshakeIXPSK0,
|
false, h.Subtype,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.l.Error("Failed to create handshake machine", "from", via, "error", err)
|
f.l.Error("Failed to create handshake machine", "from", via, "error", err)
|
||||||
@@ -717,10 +838,18 @@ func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *head
|
|||||||
|
|
||||||
response, result, err := machine.ProcessPacket(nil, packet)
|
response, result, err := machine.ProcessPacket(nil, packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if pending != nil {
|
||||||
|
hm.DeleteHostInfo(pending.hostinfo)
|
||||||
|
}
|
||||||
f.l.Error("Failed to process handshake packet", "from", via, "error", err)
|
f.l.Error("Failed to process handshake packet", "from", via, "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result == nil && pending != nil {
|
||||||
|
hm.awaitMsg3(via, pending, machine, packet, response, received)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if result == nil {
|
if result == nil {
|
||||||
// Multi-message pattern: the responder Machine would need to be
|
// Multi-message pattern: the responder Machine would need to be
|
||||||
// registered in hm.indexes so a future inbound packet finds it via
|
// registered in hm.indexes so a future inbound packet finds it via
|
||||||
@@ -745,6 +874,13 @@ func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *head
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A classical attempt crossing the peer's hybrid one goes unanswered.
|
||||||
|
if !handshake.IsHybrid(machine.Subtype()) && hm.crossesHybrid(vpnAddrs, time.Now()) {
|
||||||
|
f.l.Debug("Ignoring classical handshake that crossed a hybrid one",
|
||||||
|
"vpnAddrs", vpnAddrs, "from", via, "window", hm.hybridWindow())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
connState, err := newConnectionStateFromResult(result)
|
connState, err := newConnectionStateFromResult(result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.l.Error("Discarding handshake with an invalid message index", "error", err, "vpnAddrs", vpnAddrs)
|
f.l.Error("Discarding handshake with an invalid message index", "error", err, "vpnAddrs", vpnAddrs)
|
||||||
@@ -798,11 +934,15 @@ func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *head
|
|||||||
|
|
||||||
existing, err := hm.CheckAndComplete(hostinfo, handshakePacketStage0, f)
|
existing, err := hm.CheckAndComplete(hostinfo, handshakePacketStage0, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hm.handleCheckAndCompleteError(err, existing, hostinfo, via)
|
hm.handleCheckAndCompleteError(err, existing, hostinfo, via, machine.Subtype(), received)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hm.sendHandshakeResponse(via, response, hostinfo, false)
|
hm.sendHandshakeResponse(via, response, hostinfo, false, received)
|
||||||
|
if header.IsFragmented(machine.Subtype()) {
|
||||||
|
hm.pqAnswered.add(packet[header.Len:], hostinfo, time.Now())
|
||||||
|
}
|
||||||
|
hm.notePQPeer(machine.Subtype(), vpnAddrs)
|
||||||
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
|
||||||
|
|
||||||
// Don't wait for UpdateWorker
|
// Don't wait for UpdateWorker
|
||||||
@@ -813,23 +953,32 @@ func (hm *HandshakeManager) beginHandshake(via ViaSender, packet []byte, h *head
|
|||||||
|
|
||||||
// continueHandshake feeds an incoming packet to an existing pending handshake Machine.
|
// continueHandshake feeds an incoming packet to an existing pending handshake Machine.
|
||||||
func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostInfo, packet []byte) {
|
func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostInfo, packet []byte) {
|
||||||
f := hm.f
|
|
||||||
|
|
||||||
hh.Lock()
|
hh.Lock()
|
||||||
defer hh.Unlock()
|
defer hh.Unlock()
|
||||||
|
|
||||||
// Re-verify hh is still tracked. Between queryIndex returning and us taking
|
if !hm.stillPending(hh, hh.hostinfo.localIndexId) {
|
||||||
// hh.Lock, handleOutbound may have timed out and deleted it. Once we hold
|
|
||||||
// hh.Lock no other deleter can race our index: handleOutbound also takes
|
|
||||||
// hh.Lock first, and handleRecvError targets a main-hostmap entry with a
|
|
||||||
// different localIndexId.
|
|
||||||
hm.RLock()
|
|
||||||
cur, ok := hm.indexes[hh.hostinfo.localIndexId]
|
|
||||||
hm.RUnlock()
|
|
||||||
if !ok || cur != hh {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
hm.processResponse(via, hh, hh.machine, packet)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stillPending re-verifies hh is still tracked under index. Between queryIndex
|
||||||
|
// returning and the caller taking hh.Lock, handleOutbound may have timed out and
|
||||||
|
// deleted it. Once hh.Lock is held no other deleter can race the index:
|
||||||
|
// handleOutbound also takes hh.Lock first, and handleRecvError targets a
|
||||||
|
// main-hostmap entry with a different localIndexId. The caller must hold hh.Lock.
|
||||||
|
func (hm *HandshakeManager) stillPending(hh *HandshakeHostInfo, index uint32) bool {
|
||||||
|
hm.RLock()
|
||||||
|
defer hm.RUnlock()
|
||||||
|
cur, ok := hm.indexes[index]
|
||||||
|
return ok && cur == hh
|
||||||
|
}
|
||||||
|
|
||||||
|
// processResponse runs a responder's message through one of the initiator's
|
||||||
|
// pending attempts and completes the tunnel when the handshake finishes. The
|
||||||
|
// caller must hold hh.Lock.
|
||||||
|
func (hm *HandshakeManager) processResponse(via ViaSender, hh *HandshakeHostInfo, machine *handshake.Machine, packet []byte) {
|
||||||
|
f := hm.f
|
||||||
hostinfo := hh.hostinfo
|
hostinfo := hh.hostinfo
|
||||||
if !via.IsRelayed {
|
if !via.IsRelayed {
|
||||||
if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
|
if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, via.UdpAddr.Addr()) {
|
||||||
@@ -839,7 +988,6 @@ func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostIn
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
machine := hh.machine
|
|
||||||
if machine == nil {
|
if machine == nil {
|
||||||
f.l.Error("No handshake machine available for continuation",
|
f.l.Error("No handshake machine available for continuation",
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via)
|
"vpnAddrs", hostinfo.vpnAddrs, "from", via)
|
||||||
@@ -850,22 +998,28 @@ func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostIn
|
|||||||
response, result, err := machine.ProcessPacket(nil, packet)
|
response, result, err := machine.ProcessPacket(nil, packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Recoverable errors are routine noise, log at Debug. Fatal errors get a Warn.
|
// Recoverable errors are routine noise, log at Debug. Fatal errors get a Warn.
|
||||||
if machine.Failed() {
|
if !machine.Failed() {
|
||||||
|
f.l.Debug("Failed to process handshake packet",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
||||||
|
} else if hh.pq != nil && machine == hh.pq.machine && !hm.pqConfig().require {
|
||||||
|
// A post-quantum attempt can fail where the classical one would not, a v3
|
||||||
|
// certificate from a CA only trusted in its v2 form for one. Keep going
|
||||||
|
// with classical alone.
|
||||||
|
f.l.Warn("Failed to process post-quantum handshake packet, continuing with classical",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
||||||
|
hm.abandonPQ(hh)
|
||||||
|
} else {
|
||||||
f.l.Warn("Failed to process handshake packet, abandoning",
|
f.l.Warn("Failed to process handshake packet, abandoning",
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
||||||
hm.DeleteHostInfo(hostinfo)
|
hm.DeleteHostInfo(hostinfo)
|
||||||
} else {
|
|
||||||
f.l.Debug("Failed to process handshake packet",
|
|
||||||
"vpnAddrs", hostinfo.vpnAddrs, "from", via, "error", err)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if response != nil {
|
|
||||||
hm.sendHandshakeResponse(via, response, hostinfo, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result == nil {
|
if result == nil {
|
||||||
|
if response != nil {
|
||||||
|
hm.sendHandshakeResponse(via, response, hostinfo, false, 0)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -975,7 +1129,16 @@ func (hm *HandshakeManager) continueHandshake(via ViaSender, hh *HandshakeHostIn
|
|||||||
hostinfo.vpnAddrs = vpnAddrs
|
hostinfo.vpnAddrs = vpnAddrs
|
||||||
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
||||||
|
|
||||||
|
// The classical and PQ attempts each hold an index; keep the one that won.
|
||||||
|
hm.settleIndex(hh, result.LocalIndex)
|
||||||
hm.Complete(hostinfo, f)
|
hm.Complete(hostinfo, f)
|
||||||
|
hm.notePQPeer(machine.Subtype(), vpnAddrs)
|
||||||
|
|
||||||
|
// A three message pattern finishes with our msg3, which goes out before any
|
||||||
|
// cached packets do and keeps going until the responder is heard from.
|
||||||
|
if response != nil {
|
||||||
|
hm.startHybridConfirm(via, hostinfo, response, result.MessageIndex)
|
||||||
|
}
|
||||||
|
|
||||||
if len(hh.packetStore) > 0 {
|
if len(hh.packetStore) > 0 {
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
@@ -1054,25 +1217,30 @@ func (hm *HandshakeManager) validatePeerCert(via ViaSender, remoteCert *cert.Cac
|
|||||||
// cached is true when msg is a stored response being retransmitted because
|
// cached is true when msg is a stored response being retransmitted because
|
||||||
// the peer's stage-1 retransmit landed (the ErrAlreadySeen path); false on a
|
// the peer's stage-1 retransmit landed (the ErrAlreadySeen path); false on a
|
||||||
// fresh response.
|
// fresh response.
|
||||||
func (hm *HandshakeManager) sendHandshakeResponse(via ViaSender, msg []byte, hostinfo *HostInfo, cached bool) {
|
//
|
||||||
|
// received is the number of bytes a fragmented msg1 arrived in; it bounds what a
|
||||||
|
// fragmented response may send back. It is ignored for unfragmented subtypes.
|
||||||
|
func (hm *HandshakeManager) sendHandshakeResponse(via ViaSender, msg []byte, hostinfo *HostInfo, cached bool, received int) {
|
||||||
if msg == nil {
|
if msg == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f := hm.f
|
f := hm.f
|
||||||
f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
|
subtype := header.MessageSubType(msg[1])
|
||||||
|
|
||||||
// Common log fields. peerCert may be nil during intermediate
|
// Common log fields. peerCert may be nil during intermediate
|
||||||
// multi-message flows (handshake hasn't completed yet); skip the cert
|
// multi-message flows (handshake hasn't completed yet); skip the cert
|
||||||
// block if so.
|
// block if so.
|
||||||
logFields := []any{
|
logFields := []any{
|
||||||
"vpnAddrs", hostinfo.vpnAddrs,
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
"handshake", m{"stage": uint64(2), "style": header.SubTypeName(header.Handshake, header.HandshakeIXPSK0)},
|
"handshake", m{"stage": uint64(2), "style": header.SubTypeName(header.Handshake, subtype)},
|
||||||
"cached", cached,
|
"cached", cached,
|
||||||
"initiatorIndex", hostinfo.remoteIndexId,
|
"initiatorIndex", hostinfo.remoteIndexId,
|
||||||
"responderIndex", hostinfo.localIndexId,
|
"responderIndex", hostinfo.localIndexId,
|
||||||
}
|
}
|
||||||
if peerCert := hostinfo.ConnectionState.peerCert; peerCert != nil {
|
if hostinfo.ConnectionState == nil {
|
||||||
|
// A hybrid responder answers msg1 before it has keys.
|
||||||
|
} else if peerCert := hostinfo.ConnectionState.peerCert; peerCert != nil {
|
||||||
logFields = append(logFields,
|
logFields = append(logFields,
|
||||||
"certName", peerCert.Certificate.Name(),
|
"certName", peerCert.Certificate.Name(),
|
||||||
"certVersion", peerCert.Certificate.Version(),
|
"certVersion", peerCert.Certificate.Version(),
|
||||||
@@ -1081,6 +1249,17 @@ func (hm *HandshakeManager) sendHandshakeResponse(via ViaSender, msg []byte, hos
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if header.IsFragmented(subtype) {
|
||||||
|
fields := append(logFields, "from", via)
|
||||||
|
if err := hm.sendFragmentedResponse(via, msg, hostinfo, received); err != nil {
|
||||||
|
f.l.Debug("Did not send handshake message", append(fields, "error", err)...)
|
||||||
|
} else {
|
||||||
|
f.l.Info("Handshake message sent", fields...)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f.messageMetrics.Tx(header.Handshake, subtype, 1)
|
||||||
if !via.IsRelayed {
|
if !via.IsRelayed {
|
||||||
fields := append(logFields, "from", via)
|
fields := append(logFields, "from", via)
|
||||||
err := f.outside.WriteTo(msg, via.UdpAddr)
|
err := f.outside.WriteTo(msg, via.UdpAddr)
|
||||||
@@ -1107,10 +1286,10 @@ func (hm *HandshakeManager) sendHandshakeResponse(via ViaSender, msg []byte, hos
|
|||||||
// This only fires from the responder-side beginHandshake path, after the
|
// This only fires from the responder-side beginHandshake path, after the
|
||||||
// peer cert has been validated and ConnectionState populated, so peerCert
|
// peer cert has been validated and ConnectionState populated, so peerCert
|
||||||
// is always non-nil for the cases that log it.
|
// is always non-nil for the cases that log it.
|
||||||
func (hm *HandshakeManager) handleCheckAndCompleteError(err error, existing, hostinfo *HostInfo, via ViaSender) {
|
func (hm *HandshakeManager) handleCheckAndCompleteError(err error, existing, hostinfo *HostInfo, via ViaSender, subtype header.MessageSubType, received int) {
|
||||||
f := hm.f
|
f := hm.f
|
||||||
peerCert := hostinfo.ConnectionState.peerCert
|
peerCert := hostinfo.ConnectionState.peerCert
|
||||||
hsFields := m{"stage": uint64(1), "style": header.SubTypeName(header.Handshake, header.HandshakeIXPSK0)}
|
hsFields := m{"stage": uint64(1), "style": header.SubTypeName(header.Handshake, subtype)}
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
case ErrAlreadySeen:
|
case ErrAlreadySeen:
|
||||||
@@ -1121,7 +1300,7 @@ func (hm *HandshakeManager) handleCheckAndCompleteError(err error, existing, hos
|
|||||||
// ephemeral keys; a freshly-built one would have different keys and break
|
// ephemeral keys; a freshly-built one would have different keys and break
|
||||||
// the tunnel even though both sides "completed" the handshake.
|
// the tunnel even though both sides "completed" the handshake.
|
||||||
if msg := existing.HandshakePacket[handshakePacketStage2]; msg != nil {
|
if msg := existing.HandshakePacket[handshakePacketStage2]; msg != nil {
|
||||||
hm.sendHandshakeResponse(via, msg, existing, true)
|
hm.sendHandshakeResponse(via, msg, existing, true, received)
|
||||||
}
|
}
|
||||||
|
|
||||||
case ErrExistingHostInfo:
|
case ErrExistingHostInfo:
|
||||||
|
|||||||
@@ -0,0 +1,455 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/handshake"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Hybrid handshakes (ix_hybrid_mlkem1024) take three messages, so unlike IX the
|
||||||
|
// responder has to wait for one more message and the initiator finishes by
|
||||||
|
// sending rather than receiving:
|
||||||
|
//
|
||||||
|
// - The responder answers msg1 with msg2 and keeps the handshake pending under
|
||||||
|
// its local index until msg3 arrives. Data for that index is dropped quietly
|
||||||
|
// meanwhile, it must not draw a RecvError. Once msg3 verifies the tunnel
|
||||||
|
// goes live and an empty TestReply tells the initiator so.
|
||||||
|
// - The initiator has the keys as soon as it writes msg3, so the tunnel goes
|
||||||
|
// live then, readable only by the real responder. msg3 is resent with fresh
|
||||||
|
// symbols until something from the responder decrypts, and if nothing does
|
||||||
|
// within the handshake timeout the tunnel is closed.
|
||||||
|
|
||||||
|
const (
|
||||||
|
// hybridMsg3MaxLen bounds the msg3 a pending responder reassembles. msg3 is
|
||||||
|
// a ciphertext and two tags, about 1.6KB.
|
||||||
|
hybridMsg3MaxLen = 4096
|
||||||
|
)
|
||||||
|
|
||||||
|
// hybridResponder is the responder half of a pending hybrid handshake, waiting
|
||||||
|
// for msg3.
|
||||||
|
type hybridResponder struct {
|
||||||
|
msg3 *fragment.Assembly
|
||||||
|
expires time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// hybridConfirm is an initiator's completed hybrid handshake that has not yet
|
||||||
|
// heard from the responder.
|
||||||
|
type hybridConfirm struct {
|
||||||
|
hostinfo *HostInfo
|
||||||
|
subtype header.MessageSubType
|
||||||
|
sender *fragment.Sender
|
||||||
|
send func([]byte) error
|
||||||
|
handshakeLen uint64 // messages the handshake took, which the replay window was seeded with
|
||||||
|
deadline time.Time
|
||||||
|
next time.Time
|
||||||
|
attempts int
|
||||||
|
}
|
||||||
|
|
||||||
|
// hybridState holds the hybrid handshakes waiting on their last step.
|
||||||
|
type hybridState struct {
|
||||||
|
// responders maps local indexes to pending responders. Guarded by the
|
||||||
|
// HandshakeManager lock; the entries are also in its indexes.
|
||||||
|
responders map[uint32]*HandshakeHostInfo
|
||||||
|
|
||||||
|
confirmsMu sync.Mutex
|
||||||
|
confirms map[*HostInfo]*hybridConfirm
|
||||||
|
|
||||||
|
// answered holds when this responder answered each peer's latest new
|
||||||
|
// hybrid msg1, for the crossing window.
|
||||||
|
answeredMu sync.Mutex
|
||||||
|
answered map[netip.Addr]time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHybridState() hybridState {
|
||||||
|
return hybridState{
|
||||||
|
responders: map[uint32]*HandshakeHostInfo{},
|
||||||
|
confirms: map[*HostInfo]*hybridConfirm{},
|
||||||
|
answered: map[netip.Addr]time.Time{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A peer that sent a hybrid msg1 alternates, and its first classical attempt
|
||||||
|
// follows anywhere from right away to three retry intervals later: the retry
|
||||||
|
// timer is set one interval out, lands two slots ahead on the timer wheel, and
|
||||||
|
// the wheel only moves on whole ticks of the manager's clock. Answering that
|
||||||
|
// classical msg1 races the hybrid handshake and leaves a classical tunnel behind
|
||||||
|
// on the responder whichever wins. So for four retry intervals after answering a
|
||||||
|
// peer's hybrid msg1, its classical msg1s go unanswered; the crossing one arrives
|
||||||
|
// within that wherever the round trip, since both travel the same path. The
|
||||||
|
// window is short on purpose: an initiator whose hybrid attempt fails on its own
|
||||||
|
// side, say one that does not trust our v3 CA yet, still gets a classical answer
|
||||||
|
// to a later attempt. Blocking for as long as any hybrid state remains would
|
||||||
|
// refuse it until its handshake timed out, and its next handshake would start
|
||||||
|
// with a hybrid msg1 again.
|
||||||
|
|
||||||
|
// hybridWindow is how long classical msg1s go unanswered after a hybrid msg1.
|
||||||
|
func (hm *HandshakeManager) hybridWindow() time.Duration {
|
||||||
|
return 4 * hm.config.tryInterval
|
||||||
|
}
|
||||||
|
|
||||||
|
// noteHybridMsg1 opens the crossing window for a peer whose new hybrid msg1 this
|
||||||
|
// responder answered.
|
||||||
|
func (hm *HandshakeManager) noteHybridMsg1(vpnAddrs []netip.Addr, now time.Time) {
|
||||||
|
hm.hybrid.answeredMu.Lock()
|
||||||
|
defer hm.hybrid.answeredMu.Unlock()
|
||||||
|
for _, a := range vpnAddrs {
|
||||||
|
hm.hybrid.answered[a] = now
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// crossesHybrid reports whether a classical msg1 from a peer arrived within the
|
||||||
|
// crossing window of its hybrid msg1.
|
||||||
|
func (hm *HandshakeManager) crossesHybrid(vpnAddrs []netip.Addr, now time.Time) bool {
|
||||||
|
hm.hybrid.answeredMu.Lock()
|
||||||
|
defer hm.hybrid.answeredMu.Unlock()
|
||||||
|
for _, a := range vpnAddrs {
|
||||||
|
if at, ok := hm.hybrid.answered[a]; ok && now.Sub(at) < hm.hybridWindow() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// awaitMsg3 keeps a responder's hybrid handshake pending after msg1, and sends
|
||||||
|
// msg2. hh is already in the indexes under the local index the Machine
|
||||||
|
// allocated.
|
||||||
|
func (hm *HandshakeManager) awaitMsg3(via ViaSender, hh *HandshakeHostInfo, machine *handshake.Machine, packet, response []byte, received int) {
|
||||||
|
f := hm.f
|
||||||
|
hostinfo := hh.hostinfo
|
||||||
|
|
||||||
|
remoteCert := machine.PeerCert()
|
||||||
|
if remoteCert == nil {
|
||||||
|
f.l.Error("Handshake did not produce a peer certificate", "from", via)
|
||||||
|
hm.DeleteHostInfo(hostinfo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vpnAddrs, anyVpnAddrsInCommon, ok := hm.validatePeerCert(via, remoteCert)
|
||||||
|
if !ok {
|
||||||
|
hm.DeleteHostInfo(hostinfo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var rh header.H
|
||||||
|
if err := rh.Parse(response); err != nil {
|
||||||
|
hm.DeleteHostInfo(hostinfo)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
hostinfo.vpnAddrs = vpnAddrs
|
||||||
|
hostinfo.remoteIndexId = rh.RemoteIndex
|
||||||
|
// A hybrid msg1 is always fragmented, so packet is the one the fragment
|
||||||
|
// table reassembled, not the listener's buffer, and nothing else holds it.
|
||||||
|
hostinfo.HandshakePacket[handshakePacketStage0] = packet[header.Len:]
|
||||||
|
hostinfo.HandshakePacket[handshakePacketStage2] = response
|
||||||
|
hostinfo.remotes = f.lightHouse.QueryCache(vpnAddrs)
|
||||||
|
if !via.IsRelayed {
|
||||||
|
hostinfo.SetRemote(via.UdpAddr)
|
||||||
|
}
|
||||||
|
hh.machine = machine
|
||||||
|
hh.responder.expires = now.Add(hsTimeout(hm.config.retries, hm.config.tryInterval))
|
||||||
|
|
||||||
|
hm.Lock()
|
||||||
|
hm.hybrid.responders[hostinfo.localIndexId] = hh
|
||||||
|
hm.Unlock()
|
||||||
|
|
||||||
|
msg := "Handshake message received"
|
||||||
|
if !anyVpnAddrsInCommon {
|
||||||
|
msg = "Handshake message received, but no vpnNetworks in common."
|
||||||
|
}
|
||||||
|
f.l.Info(msg,
|
||||||
|
"vpnAddrs", vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", remoteCert.Certificate.Name(),
|
||||||
|
"certVersion", remoteCert.Certificate.Version(),
|
||||||
|
"fingerprint", remoteCert.Fingerprint,
|
||||||
|
"issuer", remoteCert.Certificate.Issuer(),
|
||||||
|
"initiatorIndex", hostinfo.remoteIndexId,
|
||||||
|
"responderIndex", hostinfo.localIndexId,
|
||||||
|
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
||||||
|
)
|
||||||
|
|
||||||
|
hm.sendHandshakeResponse(via, response, hostinfo, false, received)
|
||||||
|
hm.pqAnswered.add(packet[header.Len:], hostinfo, now)
|
||||||
|
hm.noteHybridMsg1(vpnAddrs, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
// continueHybridResponder collects msg3 fragments for a pending responder and
|
||||||
|
// completes the tunnel once they decode and verify.
|
||||||
|
func (hm *HandshakeManager) continueHybridResponder(via ViaSender, h *header.H, fh fragment.Header, sym []byte) {
|
||||||
|
hh := hm.queryIndex(h.RemoteIndex)
|
||||||
|
if hh == nil || hh.responder == nil {
|
||||||
|
hm.reconfirmHybrid(h.RemoteIndex)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if fh.MsgLen > hybridMsg3MaxLen {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hh.Lock()
|
||||||
|
defer hh.Unlock()
|
||||||
|
if !hm.stillPending(hh, h.RemoteIndex) || fh.FragID != hh.hostinfo.remoteIndexId {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
r := hh.responder
|
||||||
|
if r.msg3 == nil {
|
||||||
|
a, err := fragment.NewAssembly(fh)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.msg3 = a
|
||||||
|
}
|
||||||
|
ready, err := r.msg3.Add(fh, sym)
|
||||||
|
if errors.Is(err, fragment.ErrMismatch) {
|
||||||
|
r.msg3 = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil || !ready {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pkt, err := r.msg3.Decode(make([]byte, header.Len))
|
||||||
|
r.msg3 = nil
|
||||||
|
if err != nil {
|
||||||
|
hm.l.Debug("failed to decode handshake fragments", "from", via, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, result, err := hh.machine.ProcessPacket(nil, reassembled(h, pkt))
|
||||||
|
if err != nil {
|
||||||
|
if hh.machine.Failed() {
|
||||||
|
hm.l.Warn("Failed to process handshake packet, abandoning",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "from", via, "error", err)
|
||||||
|
hm.forgetHybridResponder(hh)
|
||||||
|
} else {
|
||||||
|
hm.l.Debug("Failed to process handshake packet",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "from", via, "error", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.completeHybridResponder(via, hh, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
// completeHybridResponder brings up the tunnel of a responder whose msg3 verified.
|
||||||
|
// The caller must hold hh.Lock.
|
||||||
|
func (hm *HandshakeManager) completeHybridResponder(via ViaSender, hh *HandshakeHostInfo, result *handshake.Result) {
|
||||||
|
f := hm.f
|
||||||
|
hostinfo := hh.hostinfo
|
||||||
|
machine := hh.machine
|
||||||
|
defer hm.forgetHybridResponder(hh)
|
||||||
|
|
||||||
|
cs, err := newConnectionStateFromResult(result)
|
||||||
|
if err != nil {
|
||||||
|
f.l.Error("Discarding handshake with an invalid message index", "error", err, "vpnAddrs", hostinfo.vpnAddrs)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hostinfo.ConnectionState = cs
|
||||||
|
hostinfo.remoteIndexId = result.RemoteIndex
|
||||||
|
hostinfo.lastHandshakeTime = result.HandshakeTime
|
||||||
|
// The initiator sends msg3 by the path its msg2 got through, so that path
|
||||||
|
// works both ways: send by it. A relayed msg3 also means the direct address
|
||||||
|
// learned from msg1 may only work toward us, so forget it and let traffic,
|
||||||
|
// the confirmation first, go by the relay.
|
||||||
|
if !via.IsRelayed {
|
||||||
|
hostinfo.SetRemote(via.UdpAddr)
|
||||||
|
} else {
|
||||||
|
hostinfo.remote.Store(nil)
|
||||||
|
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
||||||
|
}
|
||||||
|
remoteCert := result.RemoteCert
|
||||||
|
hostinfo.buildNetworks(f.myVpnNetworksTable, remoteCert.Certificate)
|
||||||
|
|
||||||
|
// The initiator only sends msg3 once it settled on this handshake, so this
|
||||||
|
// tunnel wins even over a newer one: a classical attempt that crossed it, say,
|
||||||
|
// which the initiator has already given up on.
|
||||||
|
existing, err := hm.checkAndComplete(hostinfo, handshakePacketStage0, f, true)
|
||||||
|
if err != nil {
|
||||||
|
hm.handleCheckAndCompleteError(err, existing, hostinfo, via, machine.Subtype(), 0)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f.l.Info("Handshake completed",
|
||||||
|
"vpnAddrs", hostinfo.vpnAddrs,
|
||||||
|
"from", via,
|
||||||
|
"certName", remoteCert.Certificate.Name(),
|
||||||
|
"certVersion", remoteCert.Certificate.Version(),
|
||||||
|
"fingerprint", remoteCert.Fingerprint,
|
||||||
|
"issuer", remoteCert.Certificate.Issuer(),
|
||||||
|
"initiatorIndex", result.RemoteIndex,
|
||||||
|
"responderIndex", result.LocalIndex,
|
||||||
|
"handshake", m{"stage": uint64(machine.MessageIndex()), "style": header.SubTypeName(header.Handshake, machine.Subtype())},
|
||||||
|
"durationNs", time.Since(hh.startTime).Nanoseconds(),
|
||||||
|
)
|
||||||
|
|
||||||
|
// Let the initiator know, even if there is no traffic to do it.
|
||||||
|
hostinfo.handshakeResendAt.Store(time.Now().UnixNano())
|
||||||
|
hm.sendHybridConfirmation(hostinfo)
|
||||||
|
|
||||||
|
hm.notePQPeer(machine.Subtype(), hostinfo.vpnAddrs)
|
||||||
|
hostinfo.remotes.RefreshFromHandshake(hostinfo.vpnAddrs)
|
||||||
|
if f.lightHouse.IsAnyLighthouseAddr(hostinfo.vpnAddrs) {
|
||||||
|
f.lightHouse.TriggerUpdate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HandshakeManager) sendHybridConfirmation(hostinfo *HostInfo) {
|
||||||
|
// The reply is empty, so even relayed it is just two headers and two tags.
|
||||||
|
cs := hostinfo.ConnectionState
|
||||||
|
out := make([]byte, 2*(header.Len+cs.eKey.Overhead()))
|
||||||
|
hm.f.send(header.Test, header.TestReply, cs, hostinfo, []byte{}, make([]byte, 12, 12), out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// reconfirmHybrid answers a msg3 for a hybrid tunnel this responder already
|
||||||
|
// completed: the initiator is still resending it, so the confirmation was lost.
|
||||||
|
// Replies are rate limited to one per retry interval and go to the tunnel's
|
||||||
|
// remote, never to wherever the fragment claims to come from.
|
||||||
|
func (hm *HandshakeManager) reconfirmHybrid(index uint32) {
|
||||||
|
hostinfo := hm.mainHostMap.QueryIndex(index)
|
||||||
|
if hostinfo == nil || hostinfo.ConnectionState == nil || hostinfo.ConnectionState.initiator {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if pc := hostinfo.ConnectionState.peerCert; pc == nil || pc.Certificate.Version() != cert.Version3 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now().UnixNano()
|
||||||
|
last := hostinfo.handshakeResendAt.Load()
|
||||||
|
if now-last < int64(hm.config.tryInterval) || !hostinfo.handshakeResendAt.CompareAndSwap(last, now) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.sendHybridConfirmation(hostinfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// forgetHybridResponder releases a pending responder's index.
|
||||||
|
func (hm *HandshakeManager) forgetHybridResponder(hh *HandshakeHostInfo) {
|
||||||
|
hm.Lock()
|
||||||
|
defer hm.Unlock()
|
||||||
|
idx := hh.hostinfo.localIndexId
|
||||||
|
if hm.indexes[idx] == hh {
|
||||||
|
delete(hm.indexes, idx)
|
||||||
|
}
|
||||||
|
if hm.hybrid.responders[idx] == hh {
|
||||||
|
delete(hm.hybrid.responders, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// isHybridResponder reports whether index belongs to a responder still waiting
|
||||||
|
// for msg3, whose initiator may already be sending data.
|
||||||
|
func (hm *HandshakeManager) isHybridResponder(index uint32) bool {
|
||||||
|
hm.RLock()
|
||||||
|
defer hm.RUnlock()
|
||||||
|
_, ok := hm.hybrid.responders[index]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// expireHybridResponders drops responders that never got their msg3.
|
||||||
|
func (hm *HandshakeManager) expireHybridResponders(now time.Time) {
|
||||||
|
hm.hybrid.answeredMu.Lock()
|
||||||
|
for a, at := range hm.hybrid.answered {
|
||||||
|
if now.Sub(at) >= hm.hybridWindow() {
|
||||||
|
delete(hm.hybrid.answered, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hm.hybrid.answeredMu.Unlock()
|
||||||
|
|
||||||
|
hm.Lock()
|
||||||
|
defer hm.Unlock()
|
||||||
|
for idx, hh := range hm.hybrid.responders {
|
||||||
|
if now.After(hh.responder.expires) {
|
||||||
|
if hm.indexes[idx] == hh {
|
||||||
|
delete(hm.indexes, idx)
|
||||||
|
}
|
||||||
|
delete(hm.hybrid.responders, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// startHybridConfirm sends an initiator's msg3 and keeps resending it until the
|
||||||
|
// responder is heard from on the new tunnel.
|
||||||
|
func (hm *HandshakeManager) startHybridConfirm(via ViaSender, hostinfo *HostInfo, msg3 []byte, handshakeLen uint64) {
|
||||||
|
var h header.H
|
||||||
|
if err := h.Parse(msg3); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s, err := fragment.NewSender(msg3[header.Len:], h.Subtype, h.RemoteIndex, h.MessageCounter, hostinfo.localIndexId)
|
||||||
|
if err != nil {
|
||||||
|
hostinfo.logger(hm.l).Error("Failed to fragment handshake", "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
c := &hybridConfirm{
|
||||||
|
hostinfo: hostinfo,
|
||||||
|
subtype: h.Subtype,
|
||||||
|
sender: s,
|
||||||
|
send: hm.sendTo(via),
|
||||||
|
handshakeLen: handshakeLen,
|
||||||
|
deadline: now.Add(hsTimeout(hm.config.retries, hm.config.tryInterval)),
|
||||||
|
next: now.Add(hm.config.tryInterval),
|
||||||
|
attempts: 1,
|
||||||
|
}
|
||||||
|
hm.hybrid.confirmsMu.Lock()
|
||||||
|
hm.hybrid.confirms[hostinfo] = c
|
||||||
|
hm.hybrid.confirmsMu.Unlock()
|
||||||
|
hm.sendHybridMsg3(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HandshakeManager) sendHybridMsg3(c *hybridConfirm) {
|
||||||
|
pkts, later, err := c.sender.Attempt(fragment.DefaultRepair(c.sender.Layout().K))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.messageMetrics.Tx(header.Handshake, c.subtype, int64(len(pkts)+len(later)))
|
||||||
|
// No repair delay: the packets cached during the handshake go out right
|
||||||
|
// behind msg3, and the responder drops whatever arrives before msg3
|
||||||
|
// completes. With the repair trailing, losing either data fragment would
|
||||||
|
// cost them. msg3 is four packets, too short for the tail drop the delay
|
||||||
|
// guards against.
|
||||||
|
if err := hm.sendFragmentedAfter(pkts, later, c.send, 0); err != nil {
|
||||||
|
c.hostinfo.logger(hm.l).Debug("Failed to send handshake message", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// tickHybridConfirms resends msg3 to responders not heard from yet, and closes
|
||||||
|
// tunnels whose responder never was.
|
||||||
|
func (hm *HandshakeManager) tickHybridConfirms(now time.Time) {
|
||||||
|
var resend, closing []*hybridConfirm
|
||||||
|
hm.hybrid.confirmsMu.Lock()
|
||||||
|
for hostinfo, c := range hm.hybrid.confirms {
|
||||||
|
switch {
|
||||||
|
case hm.mainHostMap.QueryIndex(hostinfo.localIndexId) != hostinfo:
|
||||||
|
delete(hm.hybrid.confirms, hostinfo)
|
||||||
|
case hostinfo.ConnectionState.heardFromPeer(c.handshakeLen):
|
||||||
|
delete(hm.hybrid.confirms, hostinfo)
|
||||||
|
case now.After(c.deadline):
|
||||||
|
delete(hm.hybrid.confirms, hostinfo)
|
||||||
|
closing = append(closing, c)
|
||||||
|
case !now.Before(c.next):
|
||||||
|
c.attempts++
|
||||||
|
c.next = now.Add(hm.config.tryInterval * time.Duration(c.attempts))
|
||||||
|
resend = append(resend, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hm.hybrid.confirmsMu.Unlock()
|
||||||
|
|
||||||
|
for _, c := range resend {
|
||||||
|
if hm.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
c.hostinfo.logger(hm.l).Debug("Resending handshake message, responder not heard from yet", "attempts", c.attempts)
|
||||||
|
}
|
||||||
|
hm.sendHybridMsg3(c)
|
||||||
|
}
|
||||||
|
for _, c := range closing {
|
||||||
|
c.hostinfo.logger(hm.l).Info("Responder never confirmed the handshake, closing tunnel",
|
||||||
|
"localIndex", c.hostinfo.localIndexId, "remoteIndex", c.hostinfo.remoteIndexId)
|
||||||
|
hm.f.closeTunnel(c.hostinfo)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,838 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/handshake"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// DefaultPQRepairDelay is how long repair fragments trail the data fragments.
|
||||||
|
DefaultPQRepairDelay = 10 * time.Millisecond
|
||||||
|
|
||||||
|
// pqAmplification bounds what a responder sends in reply to an unvalidated
|
||||||
|
// address, as a multiple of what it received from it.
|
||||||
|
pqAmplification = 3
|
||||||
|
)
|
||||||
|
|
||||||
|
// pqMaxMsgLen bounds the fragmented handshake messages a node reassembles:
|
||||||
|
// msg1 in the responder's table and msg2 in an initiator's slots. The largest
|
||||||
|
// is msg2 at about 9.6KB, which leaves room for certificates carrying many
|
||||||
|
// names and routes.
|
||||||
|
const pqMaxMsgLen = 16384
|
||||||
|
|
||||||
|
// pqTableConfig bounds the responder's reassembly of msg1 fragments from senders
|
||||||
|
// that have not authenticated yet.
|
||||||
|
var pqTableConfig = fragment.TableConfig{
|
||||||
|
MaxBytes: 4 << 20,
|
||||||
|
MaxEntries: 1024,
|
||||||
|
MaxPerSource: 4,
|
||||||
|
MaxMsgLen: pqMaxMsgLen,
|
||||||
|
TTL: 3 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
type pqHandshakeConfig struct {
|
||||||
|
initiate bool // start PQ handshakes
|
||||||
|
accept bool // answer PQ handshakes
|
||||||
|
require bool // only PQ handshakes, in either direction
|
||||||
|
sticky bool // never fall back to classical for a peer that completed PQ
|
||||||
|
repairDelay time.Duration // delay between data and repair fragments
|
||||||
|
}
|
||||||
|
|
||||||
|
func pqHandshakeConfigFrom(c *config.C) pqHandshakeConfig {
|
||||||
|
cfg := pqHandshakeConfig{
|
||||||
|
initiate: c.GetBool("handshakes.pq.initiate", false),
|
||||||
|
accept: c.GetBool("handshakes.pq.accept", false),
|
||||||
|
require: c.GetBool("handshakes.pq.require", false),
|
||||||
|
sticky: c.GetBool("handshakes.pq.sticky", false),
|
||||||
|
repairDelay: c.GetDuration("handshakes.pq.repair_delay", DefaultPQRepairDelay),
|
||||||
|
}
|
||||||
|
if cfg.require {
|
||||||
|
cfg.initiate = true
|
||||||
|
cfg.accept = true
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqConfig returns the post-quantum settings in effect.
|
||||||
|
func (hm *HandshakeManager) pqConfig() *pqHandshakeConfig {
|
||||||
|
return hm.pq.Load()
|
||||||
|
}
|
||||||
|
|
||||||
|
// warnPQWithoutV3 says so when post-quantum settings are on but the node has no
|
||||||
|
// version 3 certificate: post-quantum handshakes are hybrid only, so they do
|
||||||
|
// nothing until it has one. require is refused outright when loading pki.
|
||||||
|
func warnPQWithoutV3(l *slog.Logger, cfg pqHandshakeConfig, cs *CertState) {
|
||||||
|
if _, ok := cs.hybridSubtype(); ok || !(cfg.initiate || cfg.accept || cfg.sticky) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
l.Warn("handshakes.pq has no effect without a version 3 certificate in pki.cert",
|
||||||
|
"initiate", cfg.initiate, "accept", cfg.accept, "sticky", cfg.sticky)
|
||||||
|
}
|
||||||
|
|
||||||
|
// reloadPQ applies changed post-quantum settings. Handshakes already in flight
|
||||||
|
// keep going; the new settings apply to the next attempt and packet.
|
||||||
|
func (hm *HandshakeManager) reloadPQ(c *config.C) {
|
||||||
|
if !c.HasChanged("handshakes.pq") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
old := hm.pqConfig()
|
||||||
|
cfg := pqHandshakeConfigFrom(c)
|
||||||
|
cs := hm.f.pki.getCertState()
|
||||||
|
if _, ok := cs.hybridSubtype(); cfg.require && !ok {
|
||||||
|
hm.l.Error("Refusing handshakes.pq.require without a version 3 certificate in pki.cert, keeping the previous settings")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
warnPQWithoutV3(hm.l, cfg, cs)
|
||||||
|
hm.pq.Store(&cfg)
|
||||||
|
hm.l.Info("Post-quantum handshake settings changed",
|
||||||
|
"initiate", cfg.initiate, "oldInitiate", old.initiate,
|
||||||
|
"accept", cfg.accept, "oldAccept", old.accept,
|
||||||
|
"require", cfg.require, "oldRequire", old.require,
|
||||||
|
"sticky", cfg.sticky, "oldSticky", old.sticky,
|
||||||
|
"repairDelay", cfg.repairDelay, "oldRepairDelay", old.repairDelay,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqMsg2Slots is how many responders' msg2s one attempt reassembles at once.
|
||||||
|
// Two hosts answer the same msg1 when a stale lighthouse address for the peer
|
||||||
|
// belongs to another node now. Both answers name this attempt in RemoteIndex
|
||||||
|
// and differ only in FragID, the responder's own index. A single assembly that
|
||||||
|
// emptied on a FragID change let the two interleaved bursts wipe each other
|
||||||
|
// out, and since hearing any msg2 fragment stops the classical turns, the
|
||||||
|
// retries only repeated it (up to three extra attempts in e2e). One slot per
|
||||||
|
// FragID lets whichever answer completes first be processed; a wrong host's
|
||||||
|
// answer then takes the existing block-and-retry path.
|
||||||
|
const pqMsg2Slots = 2
|
||||||
|
|
||||||
|
// msg2Slot reassembles one responder's msg2.
|
||||||
|
type msg2Slot struct {
|
||||||
|
fragID uint32
|
||||||
|
msgLen uint16
|
||||||
|
a *fragment.Assembly
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqAttempt is the initiator's post-quantum attempt for a pending handshake. It
|
||||||
|
// lives alongside the classical attempt in HandshakeHostInfo and has its own
|
||||||
|
// local index, so whichever answer arrives first wins.
|
||||||
|
type pqAttempt struct {
|
||||||
|
machine *handshake.Machine
|
||||||
|
subtype header.MessageSubType
|
||||||
|
index uint32
|
||||||
|
sender *fragment.Sender
|
||||||
|
msg2 [pqMsg2Slots]msg2Slot // an array, not a map: one slot is used nearly always
|
||||||
|
cookieResendAt time.Time // last resend prompted by a cookie reply
|
||||||
|
heard bool // a msg2 fragment or cookie reply arrived, so the peer speaks PQ
|
||||||
|
}
|
||||||
|
|
||||||
|
// slotFor returns the assembly collecting msg2 fragments with fh's FragID,
|
||||||
|
// starting one if there is none. A responder never changes its FragID or
|
||||||
|
// MsgLen, so a fragment whose MsgLen disagrees with its slot is a stray or a
|
||||||
|
// forgery and starts that slot over. When every slot is taken the one holding
|
||||||
|
// the least is replaced: a third responder is vanishingly rare, and a forged
|
||||||
|
// FragID could already spoil an attempt. It returns nil if no assembly can be
|
||||||
|
// made for fh.
|
||||||
|
func (pq *pqAttempt) slotFor(fh fragment.Header) *fragment.Assembly {
|
||||||
|
victim := -1
|
||||||
|
for i := range pq.msg2 {
|
||||||
|
s := &pq.msg2[i]
|
||||||
|
if s.a != nil && s.fragID == fh.FragID {
|
||||||
|
if s.msgLen == fh.MsgLen {
|
||||||
|
return s.a
|
||||||
|
}
|
||||||
|
victim = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if victim < 0 {
|
||||||
|
for i := range pq.msg2 {
|
||||||
|
if pq.msg2[i].a == nil {
|
||||||
|
victim = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if victim < 0 {
|
||||||
|
victim = 0
|
||||||
|
for i := 1; i < len(pq.msg2); i++ {
|
||||||
|
if pq.msg2[i].a.Stored() < pq.msg2[victim].a.Stored() {
|
||||||
|
victim = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
a, err := fragment.NewAssembly(fh)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
pq.msg2[victim] = msg2Slot{fragID: fh.FragID, msgLen: fh.MsgLen, a: a}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
// dropSlot releases the slot holding a.
|
||||||
|
func (pq *pqAttempt) dropSlot(a *fragment.Assembly) {
|
||||||
|
for i := range pq.msg2 {
|
||||||
|
if pq.msg2[i].a == a {
|
||||||
|
pq.msg2[i] = msg2Slot{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqFragKey identifies one msg1 being reassembled.
|
||||||
|
type pqFragKey struct {
|
||||||
|
addr netip.AddrPort
|
||||||
|
relay uint32
|
||||||
|
fragID uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqFragSource groups msg1 reassemblies for the per-source quota: an IPv4 /32,
|
||||||
|
// an IPv6 /64, or one relay channel.
|
||||||
|
type pqFragSource struct {
|
||||||
|
prefix netip.Prefix
|
||||||
|
relay uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func pqFragKeyFor(via ViaSender, fragID uint32) (pqFragKey, pqFragSource) {
|
||||||
|
if via.IsRelayed {
|
||||||
|
return pqFragKey{relay: via.relay.LocalIndex, fragID: fragID},
|
||||||
|
pqFragSource{relay: via.relay.LocalIndex}
|
||||||
|
}
|
||||||
|
addr := via.UdpAddr.Addr().Unmap()
|
||||||
|
bits := 64
|
||||||
|
if addr.Is4() {
|
||||||
|
bits = 32
|
||||||
|
}
|
||||||
|
prefix, _ := addr.Prefix(bits)
|
||||||
|
return pqFragKey{addr: via.UdpAddr, fragID: fragID}, pqFragSource{prefix: prefix}
|
||||||
|
}
|
||||||
|
|
||||||
|
// reassembled turns a decoded message into the packet the handshake Machine
|
||||||
|
// expects, by writing the header of one of its fragments into the header.Len
|
||||||
|
// bytes left for it in front of the message.
|
||||||
|
func reassembled(h *header.H, pkt []byte) []byte {
|
||||||
|
return header.Encode(pkt, header.Version, header.Handshake, h.Subtype, h.RemoteIndex, h.MessageCounter)[:len(pkt)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// usePQ decides whether this outbound attempt is post-quantum. A peer that does
|
||||||
|
// not speak PQ silently drops it, so timer retries alternate between PQ and
|
||||||
|
// classical, starting with PQ. An attempt triggered early by a lighthouse reply
|
||||||
|
// is sending to addresses nothing has been sent to yet, and a new path gets the
|
||||||
|
// PQ attempt first. With require, or for a peer that completed a PQ handshake
|
||||||
|
// when sticky is on, every attempt is PQ. Once anything has come back for the PQ
|
||||||
|
// attempt the peer evidently speaks PQ, and a classical attempt would only race
|
||||||
|
// it, so classical stops getting turns.
|
||||||
|
func (hm *HandshakeManager) usePQ(hh *HandshakeHostInfo, vpnIp netip.Addr, triggered bool) bool {
|
||||||
|
cfg := hm.pqConfig()
|
||||||
|
if _, ok := hm.f.pki.getCertState().hybridSubtype(); !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case !cfg.initiate, hh.noPQ:
|
||||||
|
return false
|
||||||
|
case cfg.require:
|
||||||
|
return true
|
||||||
|
case hh.pq != nil && hh.pq.heard:
|
||||||
|
return true
|
||||||
|
case cfg.sticky:
|
||||||
|
if _, ok := hm.pqPeers.Load(vpnIp); ok {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hh.counter <= 1 || triggered || !hh.lastPQ
|
||||||
|
}
|
||||||
|
|
||||||
|
// relayEstablished pushes the pending handshake with target through a relay that
|
||||||
|
// just came up, rather than leaving it for the next retry. Nothing has gone over
|
||||||
|
// the relay yet, so like any new path it gets the PQ attempt first, which then
|
||||||
|
// counts as the PQ turn: the next retry is classical, as if it had been sent on
|
||||||
|
// schedule. Without this, relays coming up while the first attempt is in flight
|
||||||
|
// handed every relayed handshake to the classical retry.
|
||||||
|
func (hm *HandshakeManager) relayEstablished(target netip.Addr, relayHI *HostInfo, relay *Relay) {
|
||||||
|
hh := hm.queryVpnIp(target)
|
||||||
|
if hh == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hh.Lock()
|
||||||
|
defer hh.Unlock()
|
||||||
|
if hm.queryVpnIp(target) != hh {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var now, later [][]byte
|
||||||
|
switch {
|
||||||
|
case hh.pq != nil:
|
||||||
|
var err error
|
||||||
|
now, later, err = hh.pq.sender.Attempt(fragment.DefaultRepair(hh.pq.sender.Layout().K))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hh.lastPQ = true
|
||||||
|
case hh.ready:
|
||||||
|
now = [][]byte{hh.hostinfo.HandshakePacket[handshakePacketStage0]}
|
||||||
|
default:
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
f := hm.f
|
||||||
|
hh.hostinfo.logger(hm.l).Info("Send handshake via new relay", "relay", relayHI.vpnAddrs[0])
|
||||||
|
_ = hm.sendFragmented(now, later, func(p []byte) error {
|
||||||
|
f.sendHandshakeVia(relayHI, relay, p)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendHandshakeVia sends a handshake packet through a relay, with buffers sized
|
||||||
|
// for the packet rather than the mtu: a relayed post-quantum handshake is
|
||||||
|
// dozens of fragments.
|
||||||
|
func (f *Interface) sendHandshakeVia(via *HostInfo, relay *Relay, p []byte) {
|
||||||
|
out := make([]byte, header.Len+len(p)+via.ConnectionState.eKey.Overhead())
|
||||||
|
f.SendVia(via, relay, p, make([]byte, 12), out, false, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildPQAttempt creates the initiator's PQ Machine and msg1 sender. A node with
|
||||||
|
// a v3 certificate speaks the hybrid handshake for its suite's KEM, anyone else
|
||||||
|
// the ephemeral KEM one.
|
||||||
|
func (hm *HandshakeManager) buildPQAttempt(hh *HandshakeHostInfo) bool {
|
||||||
|
cs := hm.f.pki.getCertState()
|
||||||
|
v := hm.initiatingVersion(hh, cs)
|
||||||
|
subtype, ok := cs.hybridSubtype()
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
v = cert.Version3
|
||||||
|
if cs.GetCredential(v) == nil {
|
||||||
|
hm.f.l.Error("Unable to handshake with host because no certificate is available",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "certVersion", v)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
pq := &pqAttempt{subtype: subtype}
|
||||||
|
machine, err := handshake.NewMachine(
|
||||||
|
v, cs.GetCredential,
|
||||||
|
hm.certVerifier(), func() (uint32, error) { return hm.allocatePQIndex(hh, pq) },
|
||||||
|
true, subtype,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
hm.f.l.Error("Failed to create handshake machine",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "error", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := machine.Initiate(nil)
|
||||||
|
if err != nil {
|
||||||
|
hm.f.l.Error("Failed to initiate handshake",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "error", err)
|
||||||
|
hm.releasePQIndex(hh, pq)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
sender, err := fragment.NewSender(msg[header.Len:], subtype, 0, 1, pq.index)
|
||||||
|
if err != nil {
|
||||||
|
hm.f.l.Error("Failed to fragment handshake",
|
||||||
|
"vpnAddrs", hh.hostinfo.vpnAddrs, "error", err)
|
||||||
|
hm.releasePQIndex(hh, pq)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
pq.machine = machine
|
||||||
|
pq.sender = sender
|
||||||
|
hh.pq = pq
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocatePQIndex registers a local index for the PQ attempt. Unlike
|
||||||
|
// allocateIndex it leaves hostinfo.localIndexId alone; that is settled when one
|
||||||
|
// of the attempts completes.
|
||||||
|
func (hm *HandshakeManager) allocatePQIndex(hh *HandshakeHostInfo, pq *pqAttempt) (uint32, error) {
|
||||||
|
hm.mainHostMap.RLock()
|
||||||
|
defer hm.mainHostMap.RUnlock()
|
||||||
|
hm.Lock()
|
||||||
|
defer hm.Unlock()
|
||||||
|
|
||||||
|
for range 32 {
|
||||||
|
index, err := generateIndex(hm.l)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, inPending := hm.indexes[index]
|
||||||
|
_, inMain := hm.mainHostMap.Indexes[index]
|
||||||
|
|
||||||
|
if !inMain && !inPending {
|
||||||
|
pq.index = index
|
||||||
|
hm.indexes[index] = hh
|
||||||
|
return index, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, errors.New("failed to generate unique localIndexId")
|
||||||
|
}
|
||||||
|
|
||||||
|
// abandonPQ drops a failed PQ attempt, leaving classical attempts only. The
|
||||||
|
// caller must hold hh.Lock.
|
||||||
|
func (hm *HandshakeManager) abandonPQ(hh *HandshakeHostInfo) {
|
||||||
|
hm.releasePQIndex(hh, hh.pq)
|
||||||
|
hh.pq = nil
|
||||||
|
hh.noPQ = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HandshakeManager) releasePQIndex(hh *HandshakeHostInfo, pq *pqAttempt) {
|
||||||
|
hm.Lock()
|
||||||
|
defer hm.Unlock()
|
||||||
|
if pq.index != 0 && hm.indexes[pq.index] == hh {
|
||||||
|
delete(hm.indexes, pq.index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// settleIndex makes winner the hostinfo's local index and releases the index of
|
||||||
|
// the attempt that lost.
|
||||||
|
func (hm *HandshakeManager) settleIndex(hh *HandshakeHostInfo, winner uint32) {
|
||||||
|
hm.Lock()
|
||||||
|
defer hm.Unlock()
|
||||||
|
for _, idx := range []uint32{hh.hostinfo.localIndexId, hh.pqIndex()} {
|
||||||
|
if idx != 0 && idx != winner && hm.indexes[idx] == hh {
|
||||||
|
delete(hm.indexes, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hh.hostinfo.localIndexId = winner
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hh *HandshakeHostInfo) pqIndex() uint32 {
|
||||||
|
if hh.pq == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return hh.pq.index
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleFragment routes one fragment of a PQ handshake message.
|
||||||
|
func (hm *HandshakeManager) handleFragment(via ViaSender, packet []byte, h *header.H) {
|
||||||
|
if h.MessageCounter == pqCookieCounter {
|
||||||
|
hm.handleCookie(via, packet, h)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fh, sym, err := fragment.Parse(packet[header.Len:])
|
||||||
|
if err != nil {
|
||||||
|
hm.l.Debug("dropping malformed handshake fragment", "from", via, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch h.MessageCounter {
|
||||||
|
case 1:
|
||||||
|
cfg := hm.pqConfig()
|
||||||
|
if !cfg.accept {
|
||||||
|
hm.l.Debug("dropping post-quantum handshake, not accepting them", "from", via)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mine, hasV3 := hm.f.pki.getCertState().hybridSubtype()
|
||||||
|
switch {
|
||||||
|
case !hasV3:
|
||||||
|
hm.l.Debug("dropping hybrid handshake, no v3 certificate to answer it with", "from", via)
|
||||||
|
return
|
||||||
|
case h.Subtype != mine:
|
||||||
|
// The peer's suite uses another KEM. Like a peer without PQ, it
|
||||||
|
// falls back to classical unless either side requires PQ.
|
||||||
|
hm.l.Debug("dropping hybrid handshake, our v3 certificate's suite uses another KEM", "from", via,
|
||||||
|
"style", header.SubTypeName(header.Handshake, h.Subtype), "ours", header.SubTypeName(header.Handshake, mine))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if h.RemoteIndex != 0 {
|
||||||
|
hm.l.Debug("dropping stage-1 handshake with non-zero RemoteIndex",
|
||||||
|
"from", via, "remoteIndex", h.RemoteIndex)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
key, src := pqFragKeyFor(via, fh.FragID)
|
||||||
|
if hm.pqTable.Load() >= pqCookieLoad && !(fh.HasCookie() && hm.pqCookies.valid(key, fh.Cookie, now)) {
|
||||||
|
hm.sendCookie(via, h.Subtype, fh, key, len(packet), now)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := hm.pqTable.Add(key, src, now, fh, sym)
|
||||||
|
if err != nil {
|
||||||
|
hm.l.Debug("dropping handshake fragment", "from", via, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if res == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if existing := hm.pqAnswered.lookup(res.Hash, time.Now()); existing != nil {
|
||||||
|
// Already answered: resend with fresh symbols on the path this copy came by.
|
||||||
|
if msg := existing.HandshakePacket[handshakePacketStage2]; msg != nil {
|
||||||
|
hm.sendHandshakeResponse(via, msg, existing, true, res.Received)
|
||||||
|
}
|
||||||
|
hm.pqTable.Answered(key, res, now)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if res.Repeat {
|
||||||
|
// Answered once, but long enough ago to be forgotten: start over.
|
||||||
|
hm.pqTable.Forget(key)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.beginHandshake(via, reassembled(h, res.Packet), h, res.Received)
|
||||||
|
if hm.pqAnswered.lookup(res.Hash, now) != nil {
|
||||||
|
hm.pqTable.Answered(key, res, now)
|
||||||
|
}
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
if hh := hm.queryIndex(h.RemoteIndex); hh != nil {
|
||||||
|
hm.continuePQ(via, hh, h, fh, sym)
|
||||||
|
}
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
if handshake.IsHybrid(h.Subtype) {
|
||||||
|
hm.continueHybridResponder(via, h, fh, sym)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// continuePQ collects msg2 fragments for the initiator's PQ attempt, one slot
|
||||||
|
// per responder FragID, and processes the first msg2 to complete.
|
||||||
|
func (hm *HandshakeManager) continuePQ(via ViaSender, hh *HandshakeHostInfo, h *header.H, fh fragment.Header, sym []byte) {
|
||||||
|
if int(fh.MsgLen) > pqMaxMsgLen {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hh.Lock()
|
||||||
|
defer hh.Unlock()
|
||||||
|
|
||||||
|
pq := hh.pq
|
||||||
|
if pq == nil || pq.index != h.RemoteIndex || !hm.stillPending(hh, pq.index) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pq.heard = true
|
||||||
|
|
||||||
|
a := pq.slotFor(fh)
|
||||||
|
if a == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ready, err := a.Add(fh, sym)
|
||||||
|
if err != nil || !ready {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pkt, err := a.Decode(make([]byte, header.Len))
|
||||||
|
pq.dropSlot(a)
|
||||||
|
if err != nil {
|
||||||
|
hm.l.Debug("failed to decode handshake fragments", "from", via, "error", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.processResponse(via, hh, pq.machine, reassembled(h, pkt))
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendFragmented sends packets to a peer: now immediately and later after the
|
||||||
|
// repair delay. It returns the first error from the immediate sends; errors from
|
||||||
|
// delayed sends are only logged.
|
||||||
|
func (hm *HandshakeManager) sendFragmented(now, later [][]byte, send func([]byte) error) error {
|
||||||
|
return hm.sendFragmentedAfter(now, later, send, hm.pqConfig().repairDelay)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendFragmentedAfter is sendFragmented with the given repair delay.
|
||||||
|
func (hm *HandshakeManager) sendFragmentedAfter(now, later [][]byte, send func([]byte) error, delay time.Duration) error {
|
||||||
|
var first error
|
||||||
|
for _, p := range now {
|
||||||
|
if err := send(p); err != nil && first == nil {
|
||||||
|
first = err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sendLater := func() {
|
||||||
|
for _, p := range later {
|
||||||
|
if err := send(p); err != nil {
|
||||||
|
hm.l.Debug("Failed to send handshake repair fragment", "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case len(later) == 0:
|
||||||
|
case delay <= 0:
|
||||||
|
sendLater()
|
||||||
|
default:
|
||||||
|
time.AfterFunc(delay, sendLater)
|
||||||
|
}
|
||||||
|
return first
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendFragmentedResponse sends the responder's msg2 as fragments, each send
|
||||||
|
// offering symbols the hostinfo has not sent before. The clock on resends is
|
||||||
|
// per path and lives in the fragment table: a copy of msg1 arriving by a second
|
||||||
|
// path (the other address family, a relay) is answered on that path at once,
|
||||||
|
// which carries the handshake when the first path only works one way, and a
|
||||||
|
// retry on a path is reported as a repeat at most once per retry interval.
|
||||||
|
// received is how many bytes the triggering msg1 took, which bounds what may be
|
||||||
|
// sent back before the address is validated.
|
||||||
|
func (hm *HandshakeManager) sendFragmentedResponse(via ViaSender, msg []byte, hostinfo *HostInfo, received int) error {
|
||||||
|
var h header.H
|
||||||
|
if err := h.Parse(msg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s, err := fragment.NewSender(msg[header.Len:], h.Subtype, h.RemoteIndex, h.MessageCounter, hostinfo.localIndexId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l := s.Layout()
|
||||||
|
repair := fragment.DefaultRepair(l.K)
|
||||||
|
if received > 0 {
|
||||||
|
pktLen := header.Len + fragment.HeaderLen + l.SymbolSize
|
||||||
|
budget := pqAmplification * received / pktLen
|
||||||
|
if budget < l.K {
|
||||||
|
return errors.New("handshake response exceeds the amplification budget")
|
||||||
|
}
|
||||||
|
repair = min(repair, budget-l.K)
|
||||||
|
}
|
||||||
|
|
||||||
|
n := uint32(l.K + repair)
|
||||||
|
s.SetNext(int(hostinfo.handshakeNextSymbol.Add(n) - n))
|
||||||
|
pkts, later, err := s.Attempt(repair)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if via.IsRelayed {
|
||||||
|
if via.relay == nil {
|
||||||
|
return errors.New("both addr and via.relay are nil")
|
||||||
|
}
|
||||||
|
// We received a valid handshake on this relay, so make sure the relay
|
||||||
|
// state reflects that, in case it had been marked Disestablished.
|
||||||
|
hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
|
||||||
|
via.relayHI.relayState.UpdateRelayForByIdxState(via.relay.LocalIndex, Established)
|
||||||
|
}
|
||||||
|
hm.f.messageMetrics.Tx(header.Handshake, h.Subtype, int64(len(pkts)+len(later)))
|
||||||
|
return hm.sendFragmented(pkts, later, hm.sendTo(via))
|
||||||
|
}
|
||||||
|
|
||||||
|
// notePQPeer remembers peers that completed a PQ handshake, for sticky mode.
|
||||||
|
func (hm *HandshakeManager) notePQPeer(subtype header.MessageSubType, vpnAddrs []netip.Addr) {
|
||||||
|
if !hm.pqConfig().sticky || !header.IsFragmented(subtype) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, a := range vpnAddrs {
|
||||||
|
hm.pqPeers.Store(a, struct{}{})
|
||||||
|
}
|
||||||
|
if hm.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
hm.l.Debug("Remembering post-quantum peer", "vpnAddrs", vpnAddrs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqAnswered remembers the msg1s a responder recently answered, keyed by hash,
|
||||||
|
// so a repeat is recognised before any noise, KEM or certificate work. Repeats
|
||||||
|
// are routine: the trailing repair fragments of an attempt rebuild a msg1 that
|
||||||
|
// its data fragments already completed, and a retrying initiator offers fresh
|
||||||
|
// symbols for the same msg1. Entries only exist for completed handshakes, which
|
||||||
|
// need a valid certificate, and live as long as an initiator keeps retrying.
|
||||||
|
type pqAnswered struct {
|
||||||
|
sync.Mutex
|
||||||
|
ttl time.Duration
|
||||||
|
entries map[[sha256.Size]byte]pqAnsweredEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
type pqAnsweredEntry struct {
|
||||||
|
hostinfo *HostInfo
|
||||||
|
at time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
const pqAnsweredMax = 4096
|
||||||
|
|
||||||
|
func newPQAnswered(ttl time.Duration) *pqAnswered {
|
||||||
|
return &pqAnswered{ttl: ttl, entries: map[[sha256.Size]byte]pqAnsweredEntry{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *pqAnswered) add(msg1 []byte, hostinfo *HostInfo, now time.Time) {
|
||||||
|
a.Lock()
|
||||||
|
defer a.Unlock()
|
||||||
|
if len(a.entries) >= pqAnsweredMax {
|
||||||
|
for k := range a.entries {
|
||||||
|
delete(a.entries, k)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.entries[sha256.Sum256(msg1)] = pqAnsweredEntry{hostinfo: hostinfo, at: now}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lookup finds the hostinfo that answered the msg1 with this sha256.
|
||||||
|
func (a *pqAnswered) lookup(hash [sha256.Size]byte, now time.Time) *HostInfo {
|
||||||
|
a.Lock()
|
||||||
|
defer a.Unlock()
|
||||||
|
e, ok := a.entries[hash]
|
||||||
|
if !ok || now.Sub(e.at) > a.ttl {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return e.hostinfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *pqAnswered) expire(now time.Time) {
|
||||||
|
a.Lock()
|
||||||
|
defer a.Unlock()
|
||||||
|
for k, e := range a.entries {
|
||||||
|
if now.Sub(e.at) > a.ttl {
|
||||||
|
delete(a.entries, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A responder whose msg1 table is under load stops keeping state for senders
|
||||||
|
// that have not shown they receive at their address. It answers a fragment
|
||||||
|
// without a valid cookie with a cookie reply and keeps nothing; the initiator
|
||||||
|
// resends with the cookie attached. This is WireGuard's under-load cookie, QUIC's
|
||||||
|
// Retry and SCTP's state cookie: one extra round trip, only while under attack.
|
||||||
|
const (
|
||||||
|
// pqCookieLoad is the table load above which msg1 fragments need a cookie.
|
||||||
|
pqCookieLoad = 0.5
|
||||||
|
|
||||||
|
// pqCookieCounter marks a cookie reply in the header's message counter.
|
||||||
|
// Handshake messages count from 1.
|
||||||
|
pqCookieCounter = 0
|
||||||
|
|
||||||
|
// pqCookieRotate is how often the cookie secret changes. A cookie stays
|
||||||
|
// valid for between one and two rotations.
|
||||||
|
pqCookieRotate = 2 * time.Minute
|
||||||
|
|
||||||
|
pqCookieReplyLen = header.Len + fragment.CookieLen
|
||||||
|
)
|
||||||
|
|
||||||
|
// pqCookies mints and checks cookies: an HMAC over the sender's address (or relay
|
||||||
|
// channel) and FragID under a rotating secret, with the previous secret still
|
||||||
|
// accepted.
|
||||||
|
type pqCookies struct {
|
||||||
|
sync.Mutex
|
||||||
|
secrets [2][32]byte
|
||||||
|
rotatedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPQCookies(now time.Time) *pqCookies {
|
||||||
|
c := &pqCookies{rotatedAt: now}
|
||||||
|
_, _ = rand.Read(c.secrets[0][:])
|
||||||
|
_, _ = rand.Read(c.secrets[1][:])
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pqCookies) rotate(now time.Time) {
|
||||||
|
if now.Sub(c.rotatedAt) < pqCookieRotate {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.secrets[1] = c.secrets[0]
|
||||||
|
_, _ = rand.Read(c.secrets[0][:])
|
||||||
|
c.rotatedAt = now
|
||||||
|
}
|
||||||
|
|
||||||
|
func cookieMAC(secret []byte, key pqFragKey) fragment.Cookie {
|
||||||
|
mac := hmac.New(sha256.New, secret)
|
||||||
|
addr := key.addr.Addr().As16()
|
||||||
|
mac.Write(addr[:])
|
||||||
|
var b [10]byte
|
||||||
|
binary.BigEndian.PutUint16(b[0:], key.addr.Port())
|
||||||
|
binary.BigEndian.PutUint32(b[2:], key.relay)
|
||||||
|
binary.BigEndian.PutUint32(b[6:], key.fragID)
|
||||||
|
mac.Write(b[:])
|
||||||
|
var c fragment.Cookie
|
||||||
|
copy(c[:], mac.Sum(nil))
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pqCookies) make(key pqFragKey, now time.Time) fragment.Cookie {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
c.rotate(now)
|
||||||
|
return cookieMAC(c.secrets[0][:], key)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *pqCookies) valid(key pqFragKey, cookie fragment.Cookie, now time.Time) bool {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
c.rotate(now)
|
||||||
|
for _, s := range c.secrets {
|
||||||
|
want := cookieMAC(s[:], key)
|
||||||
|
if hmac.Equal(want[:], cookie[:]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendCookie answers a msg1 fragment that arrived without a valid cookie while
|
||||||
|
// under load. The reply is never larger than what triggered it, and replies are
|
||||||
|
// rate limited as a whole.
|
||||||
|
func (hm *HandshakeManager) sendCookie(via ViaSender, subtype header.MessageSubType, fh fragment.Header, key pqFragKey, received int, now time.Time) {
|
||||||
|
if received < pqCookieReplyLen {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := hm.pqCookieLimiter.Allow(); !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c := hm.pqCookies.make(key, now)
|
||||||
|
reply := make([]byte, header.Len, pqCookieReplyLen)
|
||||||
|
header.Encode(reply, header.Version, header.Handshake, subtype, fh.FragID, pqCookieCounter)
|
||||||
|
reply = append(reply, c[:]...)
|
||||||
|
hm.f.messageMetrics.Tx(header.Handshake, subtype, 1)
|
||||||
|
if err := hm.sendTo(via)(reply); err != nil {
|
||||||
|
hm.l.Debug("Failed to send handshake cookie", "to", via, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleCookie attaches a responder's cookie to the PQ attempt it names and
|
||||||
|
// resends right away, at most once per retry interval, rather than waiting for
|
||||||
|
// the next attempt.
|
||||||
|
func (hm *HandshakeManager) handleCookie(via ViaSender, packet []byte, h *header.H) {
|
||||||
|
if len(packet) != pqCookieReplyLen {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hh := hm.queryIndex(h.RemoteIndex)
|
||||||
|
if hh == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hh.Lock()
|
||||||
|
defer hh.Unlock()
|
||||||
|
|
||||||
|
pq := hh.pq
|
||||||
|
if pq == nil || pq.index != h.RemoteIndex || !hm.stillPending(hh, pq.index) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var c fragment.Cookie
|
||||||
|
copy(c[:], packet[header.Len:])
|
||||||
|
pq.sender.SetCookie(c)
|
||||||
|
pq.heard = true
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
if now.Sub(pq.cookieResendAt) < hm.config.tryInterval {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pq.cookieResendAt = now
|
||||||
|
pkts, later, err := pq.sender.Attempt(fragment.DefaultRepair(pq.sender.Layout().K))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hm.messageMetrics.Tx(header.Handshake, pq.subtype, int64(len(pkts)+len(later)))
|
||||||
|
if err := hm.sendFragmented(pkts, later, hm.sendTo(via)); err != nil {
|
||||||
|
hm.l.Debug("Failed to resend handshake with cookie", "to", via, "error", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sendTo returns a function sending a packet back the way via came: directly, or
|
||||||
|
// through the relay it arrived on.
|
||||||
|
func (hm *HandshakeManager) sendTo(via ViaSender) func([]byte) error {
|
||||||
|
f := hm.f
|
||||||
|
if !via.IsRelayed {
|
||||||
|
return func(p []byte) error {
|
||||||
|
return f.outside.WriteTo(p, via.UdpAddr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return func(p []byte) error {
|
||||||
|
if via.relay == nil {
|
||||||
|
return errors.New("both addr and via.relay are nil")
|
||||||
|
}
|
||||||
|
f.sendHandshakeVia(via.relayHI, via.relay, p)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,594 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"math/rand/v2"
|
||||||
|
"net/netip"
|
||||||
|
"sync"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/cert_test"
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/handshake/fragment"
|
||||||
|
"github.com/slackhq/nebula/header"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/slackhq/nebula/udp"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// recordingConn captures what is written to it.
|
||||||
|
type recordingConn struct {
|
||||||
|
udp.NoopConn
|
||||||
|
sync.Mutex
|
||||||
|
sent [][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *recordingConn) WriteTo(b []byte, _ netip.AddrPort) error {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
c.sent = append(c.sent, append([]byte(nil), b...))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *recordingConn) take() [][]byte {
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
s := c.sent
|
||||||
|
c.sent = nil
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPQTestManager(t *testing.T) (*HandshakeManager, *recordingConn) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
conn := &recordingConn{}
|
||||||
|
cfg := defaultHandshakeConfig
|
||||||
|
cfg.pq = pqHandshakeConfig{initiate: true, accept: true}
|
||||||
|
hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), conn, cfg)
|
||||||
|
hm.f = &Interface{
|
||||||
|
handshakeManager: hm,
|
||||||
|
outside: conn,
|
||||||
|
messageMetrics: newMessageMetricsOnlyRecvError(),
|
||||||
|
pki: newV3TestPKI(t),
|
||||||
|
l: l,
|
||||||
|
}
|
||||||
|
return hm, conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// newV3TestPKI holds a version 3 certificate, which post-quantum handshakes need.
|
||||||
|
func newV3TestPKI(t *testing.T) *PKI {
|
||||||
|
t.Helper()
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version3, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
c, _, keyPEM, _ := cert_test.NewTestCert(cert.Version3, cert.Curve_CURVE25519, ca, caKey, "me", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}, nil, nil)
|
||||||
|
key, _, curve, err := cert.UnmarshalPrivateKeyFromPEM(keyPEM)
|
||||||
|
require.NoError(t, err)
|
||||||
|
cs, err := newCertState(cert.Version2, nil, cert.V2Form(c), c, false, curve, key, "aes")
|
||||||
|
require.NoError(t, err)
|
||||||
|
p := &PKI{}
|
||||||
|
p.cs.Store(cs)
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
// fragmentIndexes checks every packet is a fragment of the expected response and
|
||||||
|
// returns their symbol indexes.
|
||||||
|
func fragmentIndexes(t *testing.T, pkts [][]byte, fragID, remoteIndex uint32) []int {
|
||||||
|
t.Helper()
|
||||||
|
var out []int
|
||||||
|
for _, p := range pkts {
|
||||||
|
require.LessOrEqual(t, len(p), fragment.MaxPacketLen)
|
||||||
|
var h header.H
|
||||||
|
require.NoError(t, h.Parse(p))
|
||||||
|
assert.Equal(t, header.HandshakeIXHybridMLKEM1024, h.Subtype)
|
||||||
|
assert.Equal(t, remoteIndex, h.RemoteIndex)
|
||||||
|
assert.Equal(t, uint64(2), h.MessageCounter)
|
||||||
|
fh, _, err := fragment.Parse(p[header.Len:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, fragID, fh.FragID)
|
||||||
|
out = append(out, int(fh.Index))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSendFragmentedResponse(t *testing.T) {
|
||||||
|
hm, conn := newPQTestManager(t)
|
||||||
|
via := ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.1:4242")}
|
||||||
|
hostinfo := &HostInfo{localIndexId: 77}
|
||||||
|
|
||||||
|
// A 2000 byte message is 2 symbols of 1000, so packets of 1024 bytes.
|
||||||
|
msg := make([]byte, header.Len, header.Len+2000)
|
||||||
|
header.Encode(msg, header.Version, header.Handshake, header.HandshakeIXHybridMLKEM1024, 5, 2)
|
||||||
|
r := rand.New(rand.NewPCG(1, 1))
|
||||||
|
for range 2000 {
|
||||||
|
msg = append(msg, byte(r.Uint32()))
|
||||||
|
}
|
||||||
|
const pktLen = header.Len + fragment.HeaderLen + 1000
|
||||||
|
|
||||||
|
t.Run("first send", func(t *testing.T) {
|
||||||
|
require.NoError(t, hm.sendFragmentedResponse(via, msg, hostinfo, 0))
|
||||||
|
assert.Equal(t, []int{0, 1, 2, 3}, fragmentIndexes(t, conn.take(), 77, 5))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("a repeat is answered at once with fresh symbols", func(t *testing.T) {
|
||||||
|
// No clock gates a resend: a repeat already cost the initiator K fresh
|
||||||
|
// symbols on that path, and the amplification budget bounds the answer.
|
||||||
|
require.NoError(t, hm.sendFragmentedResponse(via, msg, hostinfo, 10*pktLen))
|
||||||
|
assert.Equal(t, []int{4, 5, 6, 7}, fragmentIndexes(t, conn.take(), 77, 5))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("amplification budget trims repair", func(t *testing.T) {
|
||||||
|
// 3x of a little over one packet allows 3 packets: both data and 1 repair.
|
||||||
|
require.NoError(t, hm.sendFragmentedResponse(via, msg, hostinfo, pktLen+10))
|
||||||
|
assert.Equal(t, []int{8, 9, 10}, fragmentIndexes(t, conn.take(), 77, 5))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("amplification budget below the data refuses", func(t *testing.T) {
|
||||||
|
require.Error(t, hm.sendFragmentedResponse(via, msg, hostinfo, pktLen/2))
|
||||||
|
assert.Empty(t, conn.take())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQAnswered(t *testing.T) {
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
a := newPQAnswered(5 * time.Second)
|
||||||
|
hi := &HostInfo{}
|
||||||
|
|
||||||
|
assert.Nil(t, a.lookup(sha256.Sum256([]byte("msg1")), now))
|
||||||
|
a.add([]byte("msg1"), hi, now)
|
||||||
|
assert.Same(t, hi, a.lookup(sha256.Sum256([]byte("msg1")), now.Add(time.Second)))
|
||||||
|
assert.Nil(t, a.lookup(sha256.Sum256([]byte("other")), now))
|
||||||
|
assert.Nil(t, a.lookup(sha256.Sum256([]byte("msg1")), now.Add(6*time.Second)), "stale entries are not answered")
|
||||||
|
|
||||||
|
a.expire(now.Add(6 * time.Second))
|
||||||
|
assert.Empty(t, a.entries)
|
||||||
|
|
||||||
|
for i := range pqAnsweredMax + 10 {
|
||||||
|
a.add([]byte{byte(i), byte(i >> 8)}, hi, now)
|
||||||
|
}
|
||||||
|
assert.Len(t, a.entries, pqAnsweredMax)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQFragKeyFor(t *testing.T) {
|
||||||
|
k1, s1 := pqFragKeyFor(ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.1:4242")}, 9)
|
||||||
|
k2, s2 := pqFragKeyFor(ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.1:4243")}, 9)
|
||||||
|
assert.NotEqual(t, k1, k2, "different ports are different messages")
|
||||||
|
assert.Equal(t, s1, s2, "but share an IPv4 /32 quota")
|
||||||
|
|
||||||
|
_, s3 := pqFragKeyFor(ViaSender{UdpAddr: netip.MustParseAddrPort("[2001:db8::1]:1")}, 9)
|
||||||
|
_, s4 := pqFragKeyFor(ViaSender{UdpAddr: netip.MustParseAddrPort("[2001:db8::ffff]:1")}, 9)
|
||||||
|
assert.Equal(t, s3, s4, "an IPv6 /64 shares a quota")
|
||||||
|
|
||||||
|
_, s5 := pqFragKeyFor(ViaSender{UdpAddr: netip.MustParseAddrPort("[::ffff:192.0.2.1]:1")}, 9)
|
||||||
|
assert.Equal(t, s1, s5, "v4-mapped addresses count as IPv4")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQCookies(t *testing.T) {
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
c := newPQCookies(now)
|
||||||
|
key := pqFragKey{addr: netip.MustParseAddrPort("192.0.2.1:4242"), fragID: 9}
|
||||||
|
|
||||||
|
cookie := c.make(key, now)
|
||||||
|
assert.True(t, c.valid(key, cookie, now))
|
||||||
|
|
||||||
|
for _, other := range []pqFragKey{
|
||||||
|
{addr: netip.MustParseAddrPort("192.0.2.2:4242"), fragID: 9},
|
||||||
|
{addr: netip.MustParseAddrPort("192.0.2.1:4243"), fragID: 9},
|
||||||
|
{addr: netip.MustParseAddrPort("192.0.2.1:4242"), fragID: 10},
|
||||||
|
{addr: netip.MustParseAddrPort("192.0.2.1:4242"), relay: 1, fragID: 9},
|
||||||
|
} {
|
||||||
|
assert.False(t, c.valid(other, cookie, now), "%+v", other)
|
||||||
|
}
|
||||||
|
|
||||||
|
now = now.Add(pqCookieRotate)
|
||||||
|
assert.True(t, c.valid(key, cookie, now), "the previous secret is still accepted")
|
||||||
|
assert.NotEqual(t, cookie, c.make(key, now), "new cookies come from the new secret")
|
||||||
|
|
||||||
|
now = now.Add(pqCookieRotate)
|
||||||
|
assert.False(t, c.valid(key, cookie, now), "two rotations retire a cookie")
|
||||||
|
}
|
||||||
|
|
||||||
|
// pqMsg1Fragment builds the first fragment of a msg1, optionally with a cookie.
|
||||||
|
func pqMsg1Fragment(t *testing.T, fragID uint32, cookie *fragment.Cookie) ([]byte, *header.H) {
|
||||||
|
t.Helper()
|
||||||
|
s, err := fragment.NewSender(make([]byte, 2000), header.HandshakeIXHybridMLKEM1024, 0, 1, fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
if cookie != nil {
|
||||||
|
s.SetCookie(*cookie)
|
||||||
|
}
|
||||||
|
pkts, _, err := s.Attempt(0)
|
||||||
|
require.NoError(t, err)
|
||||||
|
var h header.H
|
||||||
|
require.NoError(t, h.Parse(pkts[0]))
|
||||||
|
return pkts[0], &h
|
||||||
|
}
|
||||||
|
|
||||||
|
// fillPQTable puts the responder's msg1 table at the cookie threshold.
|
||||||
|
func fillPQTable(t *testing.T, hm *HandshakeManager) {
|
||||||
|
t.Helper()
|
||||||
|
now := time.Now()
|
||||||
|
for i := 0; hm.pqTable.Load() < pqCookieLoad; i++ {
|
||||||
|
p, _ := pqMsg1Fragment(t, uint32(i+1), nil)
|
||||||
|
fh, sym, err := fragment.Parse(p[header.Len:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
addr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{198, 18, byte(i >> 8), byte(i)}), 4242)
|
||||||
|
key, src := pqFragKeyFor(ViaSender{UdpAddr: addr}, fh.FragID)
|
||||||
|
_, err = hm.pqTable.Add(key, src, now, fh, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQCookieUnderLoad(t *testing.T) {
|
||||||
|
hm, conn := newPQTestManager(t)
|
||||||
|
via := ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.1:4242")}
|
||||||
|
|
||||||
|
t.Run("no cookie needed below the threshold", func(t *testing.T) {
|
||||||
|
p, h := pqMsg1Fragment(t, 5000, nil)
|
||||||
|
before := hm.pqTable.Len()
|
||||||
|
hm.handleFragment(via, p, h)
|
||||||
|
assert.Empty(t, conn.take())
|
||||||
|
assert.Equal(t, before+1, hm.pqTable.Len())
|
||||||
|
})
|
||||||
|
|
||||||
|
fillPQTable(t, hm)
|
||||||
|
|
||||||
|
var cookie fragment.Cookie
|
||||||
|
t.Run("a fragment without a cookie gets one and leaves no state", func(t *testing.T) {
|
||||||
|
p, h := pqMsg1Fragment(t, 7, nil)
|
||||||
|
before := hm.pqTable.Len()
|
||||||
|
hm.handleFragment(via, p, h)
|
||||||
|
assert.Equal(t, before, hm.pqTable.Len())
|
||||||
|
|
||||||
|
sent := conn.take()
|
||||||
|
require.Len(t, sent, 1)
|
||||||
|
reply := sent[0]
|
||||||
|
require.Len(t, reply, pqCookieReplyLen)
|
||||||
|
var rh header.H
|
||||||
|
require.NoError(t, rh.Parse(reply))
|
||||||
|
assert.Equal(t, header.Handshake, rh.Type)
|
||||||
|
assert.Equal(t, header.HandshakeIXHybridMLKEM1024, rh.Subtype)
|
||||||
|
assert.Equal(t, uint32(7), rh.RemoteIndex, "the reply names the initiator's FragID")
|
||||||
|
assert.Equal(t, uint64(pqCookieCounter), rh.MessageCounter)
|
||||||
|
copy(cookie[:], reply[header.Len:])
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("a forged cookie gets a fresh one", func(t *testing.T) {
|
||||||
|
bad := cookie
|
||||||
|
bad[0] ^= 1
|
||||||
|
p, h := pqMsg1Fragment(t, 7, &bad)
|
||||||
|
before := hm.pqTable.Len()
|
||||||
|
hm.handleFragment(via, p, h)
|
||||||
|
assert.Equal(t, before, hm.pqTable.Len())
|
||||||
|
assert.Len(t, conn.take(), 1)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("the cookie gets the fragment in", func(t *testing.T) {
|
||||||
|
p, h := pqMsg1Fragment(t, 7, &cookie)
|
||||||
|
before := hm.pqTable.Len()
|
||||||
|
hm.handleFragment(via, p, h)
|
||||||
|
assert.Equal(t, before+1, hm.pqTable.Len())
|
||||||
|
assert.Empty(t, conn.take())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("a cookie is bound to its address", func(t *testing.T) {
|
||||||
|
p, h := pqMsg1Fragment(t, 7, &cookie)
|
||||||
|
hm.handleFragment(ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.99:4242")}, p, h)
|
||||||
|
assert.Len(t, conn.take(), 1, "another address gets its own cookie instead")
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("a reply never outgrows what prompted it", func(t *testing.T) {
|
||||||
|
p, h := pqMsg1Fragment(t, 8, nil)
|
||||||
|
hm.sendCookie(via, header.HandshakeIXHybridMLKEM1024, fragment.Header{FragID: 8}, pqFragKey{}, pqCookieReplyLen-1, time.Now())
|
||||||
|
assert.Empty(t, conn.take())
|
||||||
|
hm.handleFragment(via, p, h)
|
||||||
|
assert.Len(t, conn.take(), 1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPQHandleCookie(t *testing.T) {
|
||||||
|
hm, conn := newPQTestManager(t)
|
||||||
|
via := ViaSender{UdpAddr: netip.MustParseAddrPort("192.0.2.1:4242")}
|
||||||
|
|
||||||
|
s, err := fragment.NewSender(make([]byte, 2000), header.HandshakeIXHybridMLKEM1024, 0, 1, 99)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, _, err = s.Attempt(2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
hh := &HandshakeHostInfo{hostinfo: &HostInfo{}, pq: &pqAttempt{index: 99, sender: s}}
|
||||||
|
hm.indexes[99] = hh
|
||||||
|
|
||||||
|
reply := make([]byte, header.Len, pqCookieReplyLen)
|
||||||
|
header.Encode(reply, header.Version, header.Handshake, header.HandshakeIXHybridMLKEM1024, 99, pqCookieCounter)
|
||||||
|
reply = append(reply, bytes16(0xab)...)
|
||||||
|
var h header.H
|
||||||
|
require.NoError(t, h.Parse(reply))
|
||||||
|
|
||||||
|
hm.handleFragment(via, reply, &h)
|
||||||
|
assert.True(t, hh.pq.heard, "a cookie reply shows the peer speaks PQ")
|
||||||
|
sent := conn.take()
|
||||||
|
require.Len(t, sent, 4, "an immediate resend of fresh symbols")
|
||||||
|
for i, p := range sent {
|
||||||
|
fh, _, err := fragment.Parse(p[header.Len:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.True(t, fh.HasCookie())
|
||||||
|
assert.Equal(t, fragment.Cookie(bytes16(0xab)), fh.Cookie)
|
||||||
|
assert.Equal(t, uint8(4+i), fh.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
hm.handleFragment(via, reply, &h)
|
||||||
|
assert.Empty(t, conn.take(), "immediate resends are rate limited")
|
||||||
|
|
||||||
|
t.Run("replies for other indexes are ignored", func(t *testing.T) {
|
||||||
|
other := append([]byte(nil), reply...)
|
||||||
|
header.Encode(other, header.Version, header.Handshake, header.HandshakeIXHybridMLKEM1024, 98, pqCookieCounter)
|
||||||
|
var oh header.H
|
||||||
|
require.NoError(t, oh.Parse(other))
|
||||||
|
hh.pq.cookieResendAt = time.Time{}
|
||||||
|
hm.handleFragment(via, other, &oh)
|
||||||
|
assert.Empty(t, conn.take())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func bytes16(b byte) []byte {
|
||||||
|
out := make([]byte, 16)
|
||||||
|
for i := range out {
|
||||||
|
out[i] = b
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestReloadPQ(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
c := config.NewC(l)
|
||||||
|
require.NoError(t, c.LoadString(`handshakes: {pq: {initiate: false}}`))
|
||||||
|
cfg := defaultHandshakeConfig
|
||||||
|
cfg.pq = pqHandshakeConfigFrom(c)
|
||||||
|
hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, cfg)
|
||||||
|
hm.f = &Interface{pki: newV3TestPKI(t)}
|
||||||
|
c.RegisterReloadCallback(hm.reloadPQ)
|
||||||
|
|
||||||
|
assert.Equal(t, pqHandshakeConfig{repairDelay: DefaultPQRepairDelay}, *hm.pqConfig())
|
||||||
|
|
||||||
|
require.NoError(t, c.ReloadConfigString(`handshakes: {pq: {initiate: true, accept: true, sticky: true, repair_delay: 25ms}}`))
|
||||||
|
assert.Equal(t, pqHandshakeConfig{initiate: true, accept: true, sticky: true, repairDelay: 25 * time.Millisecond}, *hm.pqConfig())
|
||||||
|
|
||||||
|
before := hm.pqConfig()
|
||||||
|
require.NoError(t, c.ReloadConfigString(`handshakes: {try_interval: 1s, pq: {initiate: true, accept: true, sticky: true, repair_delay: 25ms}}`))
|
||||||
|
assert.Same(t, before, hm.pqConfig(), "an unrelated change leaves the settings alone")
|
||||||
|
|
||||||
|
require.NoError(t, c.ReloadConfigString(`handshakes: {}`))
|
||||||
|
assert.Equal(t, pqHandshakeConfig{repairDelay: DefaultPQRepairDelay}, *hm.pqConfig(), "removing the section restores the defaults")
|
||||||
|
|
||||||
|
require.NoError(t, c.ReloadConfigString(`handshakes: {pq: {require: true}}`))
|
||||||
|
assert.Equal(t, pqHandshakeConfig{initiate: true, accept: true, require: true, repairDelay: DefaultPQRepairDelay}, *hm.pqConfig(), "require implies initiate and accept")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsePQ(t *testing.T) {
|
||||||
|
hm, _ := newPQTestManager(t)
|
||||||
|
peer := netip.MustParseAddr("10.1.0.1")
|
||||||
|
set := func(cfg pqHandshakeConfig) { hm.pq.Store(&cfg) }
|
||||||
|
|
||||||
|
// attempts plays a run of attempts, given whether each was triggered early,
|
||||||
|
// and returns which were PQ.
|
||||||
|
attempts := func(triggered ...bool) []bool {
|
||||||
|
hh := &HandshakeHostInfo{}
|
||||||
|
var out []bool
|
||||||
|
for _, tr := range triggered {
|
||||||
|
hh.counter++
|
||||||
|
hh.lastPQ = hm.usePQ(hh, peer, tr)
|
||||||
|
out = append(out, hh.lastPQ)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
set(pqHandshakeConfig{})
|
||||||
|
assert.Equal(t, []bool{false, false, false}, attempts(false, false, false), "off")
|
||||||
|
|
||||||
|
set(pqHandshakeConfig{initiate: true})
|
||||||
|
assert.Equal(t, []bool{true, false, true, false}, attempts(false, false, false, false), "timer retries alternate, PQ first")
|
||||||
|
assert.Equal(t, []bool{true, true, false, true, false}, attempts(false, true, false, true, false), "an early trigger is a new path, which gets PQ first")
|
||||||
|
assert.Equal(t, []bool{true, true, false}, attempts(true, true, false), "a triggered first attempt is PQ")
|
||||||
|
|
||||||
|
t.Run("hearing back stops the classical turns", func(t *testing.T) {
|
||||||
|
hh := &HandshakeHostInfo{pq: &pqAttempt{}}
|
||||||
|
hh.counter, hh.lastPQ = 1, true
|
||||||
|
hh.counter++
|
||||||
|
assert.False(t, hm.usePQ(hh, peer, false), "unheard, the classical attempt gets its turn")
|
||||||
|
hh.pq.heard = true
|
||||||
|
assert.True(t, hm.usePQ(hh, peer, false))
|
||||||
|
})
|
||||||
|
|
||||||
|
set(pqHandshakeConfig{initiate: true, accept: true, require: true})
|
||||||
|
assert.Equal(t, []bool{true, true, true, true}, attempts(false, false, true, false), "require never offers classical")
|
||||||
|
|
||||||
|
set(pqHandshakeConfig{initiate: true, sticky: true})
|
||||||
|
assert.Equal(t, []bool{true, false}, attempts(false, false), "sticky alternates until the peer has done PQ")
|
||||||
|
hm.notePQPeer(header.HandshakeIXHybridMLKEM1024, []netip.Addr{peer})
|
||||||
|
assert.Equal(t, []bool{true, true, true}, attempts(false, false, false), "then only PQ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// msg2Fragments returns one attempt's packets carrying msg as a responder's msg2
|
||||||
|
// under fragID, data symbols first and then repair.
|
||||||
|
func msg2Fragments(t *testing.T, msg []byte, fragID uint32) [][]byte {
|
||||||
|
t.Helper()
|
||||||
|
s, err := fragment.NewSender(msg, header.HandshakeIXHybridMLKEM1024, 7, 2, fragID)
|
||||||
|
require.NoError(t, err)
|
||||||
|
now, later, err := s.Attempt(fragment.DefaultRepair(s.Layout().K))
|
||||||
|
require.NoError(t, err)
|
||||||
|
return append(now, later...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// feedMsg2 puts one packet through an attempt's slots.
|
||||||
|
func feedMsg2(t *testing.T, pq *pqAttempt, p []byte) (*fragment.Assembly, fragment.Header, bool) {
|
||||||
|
t.Helper()
|
||||||
|
fh, sym, err := fragment.Parse(p[header.Len:])
|
||||||
|
require.NoError(t, err)
|
||||||
|
a := pq.slotFor(fh)
|
||||||
|
require.NotNil(t, a)
|
||||||
|
ready, err := a.Add(fh, sym)
|
||||||
|
require.NoError(t, err)
|
||||||
|
return a, fh, ready
|
||||||
|
}
|
||||||
|
|
||||||
|
func slotsUsed(pq *pqAttempt) (n int, ids []uint32) {
|
||||||
|
for _, s := range pq.msg2 {
|
||||||
|
if s.a != nil {
|
||||||
|
n++
|
||||||
|
ids = append(ids, s.fragID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n, ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPQMsg2Slots interleaves two responders' msg2 bursts, the shape two hosts
|
||||||
|
// answering the same msg1 produce on a LAN, and checks each fills its own slot.
|
||||||
|
func TestPQMsg2Slots(t *testing.T) {
|
||||||
|
msgB := make([]byte, 9625) // the hybrid msg2: k = 9
|
||||||
|
msgC := make([]byte, 9625)
|
||||||
|
for i := range msgB {
|
||||||
|
msgB[i], msgC[i] = 'b', 'c'
|
||||||
|
}
|
||||||
|
pktsB := msg2Fragments(t, msgB, 1001)
|
||||||
|
pktsC := msg2Fragments(t, msgC, 2002)
|
||||||
|
require.Len(t, pktsB, 12)
|
||||||
|
|
||||||
|
// B0 B1 C0 B2 B3 B4 C1 C2 B5 B6 B7 B8 C3: no run of one FragID reaches nine,
|
||||||
|
// so a single slot emptied on every change would never complete.
|
||||||
|
order := []struct {
|
||||||
|
fromC bool
|
||||||
|
i int
|
||||||
|
}{{false, 0}, {false, 1}, {true, 0}, {false, 2}, {false, 3}, {false, 4}, {true, 1}, {true, 2}, {false, 5}, {false, 6}, {false, 7}, {false, 8}, {true, 3}}
|
||||||
|
|
||||||
|
pq := &pqAttempt{}
|
||||||
|
var got []byte
|
||||||
|
for n, o := range order {
|
||||||
|
p := pktsB[o.i]
|
||||||
|
if o.fromC {
|
||||||
|
p = pktsC[o.i]
|
||||||
|
}
|
||||||
|
a, _, ready := feedMsg2(t, pq, p)
|
||||||
|
if !ready {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
require.Equal(t, 11, n, "B completes on its ninth symbol")
|
||||||
|
msg, err := a.Decode(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
got = msg
|
||||||
|
pq.dropSlot(a)
|
||||||
|
}
|
||||||
|
require.Equal(t, msgB, got)
|
||||||
|
|
||||||
|
n, ids := slotsUsed(pq)
|
||||||
|
assert.Equal(t, 1, n)
|
||||||
|
assert.Equal(t, []uint32{2002}, ids, "B's slot was released, C's kept")
|
||||||
|
|
||||||
|
// C's four symbols survived B's completion, so five more finish it.
|
||||||
|
var c *fragment.Assembly
|
||||||
|
var ready bool
|
||||||
|
for i := 4; i < 9; i++ {
|
||||||
|
c, _, ready = feedMsg2(t, pq, pktsC[i])
|
||||||
|
assert.Equal(t, i == 8, ready, "symbol %d", i)
|
||||||
|
}
|
||||||
|
msg, err := c.Decode(nil)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, msgC, msg)
|
||||||
|
|
||||||
|
t.Run("a third responder takes the slot holding the least", func(t *testing.T) {
|
||||||
|
pq := &pqAttempt{}
|
||||||
|
feedMsg2(t, pq, pktsB[0])
|
||||||
|
feedMsg2(t, pq, pktsB[1])
|
||||||
|
feedMsg2(t, pq, pktsC[0])
|
||||||
|
pktsD := msg2Fragments(t, msgC, 3003)
|
||||||
|
d, fh, _ := feedMsg2(t, pq, pktsD[0])
|
||||||
|
_, ids := slotsUsed(pq)
|
||||||
|
assert.ElementsMatch(t, []uint32{1001, 3003}, ids)
|
||||||
|
|
||||||
|
l, err := fh.Layout()
|
||||||
|
require.NoError(t, err)
|
||||||
|
b, _, _ := feedMsg2(t, pq, pktsB[2])
|
||||||
|
assert.Equal(t, 3*l.SymbolSize, b.Stored(), "B kept its symbols")
|
||||||
|
assert.Equal(t, l.SymbolSize, d.Stored())
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("a different MsgLen under the same FragID starts the slot over", func(t *testing.T) {
|
||||||
|
pq := &pqAttempt{}
|
||||||
|
feedMsg2(t, pq, pktsB[0])
|
||||||
|
feedMsg2(t, pq, pktsB[1])
|
||||||
|
shorter := msg2Fragments(t, msgB[:9000], 1001)
|
||||||
|
a, fh, _ := feedMsg2(t, pq, shorter[0])
|
||||||
|
l, err := fh.Layout()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, l.SymbolSize, a.Stored())
|
||||||
|
n, ids := slotsUsed(pq)
|
||||||
|
assert.Equal(t, 1, n)
|
||||||
|
assert.Equal(t, []uint32{1001}, ids)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("an oversize claim is dropped before it takes a slot", func(t *testing.T) {
|
||||||
|
hm, _ := newPQTestManager(t)
|
||||||
|
hh := &HandshakeHostInfo{hostinfo: &HostInfo{}, pq: &pqAttempt{index: 42}}
|
||||||
|
hm.indexes[42] = hh
|
||||||
|
fh := fragment.Header{FragID: 1001, MsgLen: pqMaxMsgLen + 1}
|
||||||
|
l, err := fh.Layout()
|
||||||
|
require.NoError(t, err)
|
||||||
|
hm.continuePQ(ViaSender{}, hh, &header.H{RemoteIndex: 42, MessageCounter: 2}, fh, make([]byte, l.SymbolSize))
|
||||||
|
n, _ := slotsUsed(hh.pq)
|
||||||
|
assert.Zero(t, n)
|
||||||
|
assert.False(t, hh.pq.heard)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Post-quantum handshakes are hybrid only, so without a version 3 certificate
|
||||||
|
// there is nothing to initiate and require is refused.
|
||||||
|
func TestPQNeedsV3(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
c2, _, keyPEM, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, "me", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}, nil, nil)
|
||||||
|
key, _, curve, err := cert.UnmarshalPrivateKeyFromPEM(keyPEM)
|
||||||
|
require.NoError(t, err)
|
||||||
|
cs, err := newCertState(cert.Version2, nil, c2, nil, false, curve, key, "aes")
|
||||||
|
require.NoError(t, err)
|
||||||
|
v2PKI := &PKI{}
|
||||||
|
v2PKI.cs.Store(cs)
|
||||||
|
|
||||||
|
hm := NewHandshakeManager(l, newHostMap(l), newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
|
||||||
|
hm.f = &Interface{pki: v2PKI}
|
||||||
|
hm.pq.Store(&pqHandshakeConfig{initiate: true, accept: true})
|
||||||
|
assert.False(t, hm.usePQ(&HandshakeHostInfo{counter: 1}, netip.MustParseAddr("10.0.0.2"), false), "no v3 certificate, no hybrid attempt")
|
||||||
|
|
||||||
|
conf := config.NewC(l)
|
||||||
|
require.NoError(t, conf.LoadString(`handshakes: {pq: {initiate: true}}`))
|
||||||
|
conf.RegisterReloadCallback(hm.reloadPQ)
|
||||||
|
require.NoError(t, conf.ReloadConfigString(`handshakes: {pq: {require: true}}`))
|
||||||
|
assert.False(t, hm.pqConfig().require, "require without a v3 certificate is refused on reload")
|
||||||
|
|
||||||
|
// And when loading the pki.
|
||||||
|
certPEM, err := c2.MarshalPEM()
|
||||||
|
require.NoError(t, err)
|
||||||
|
conf = config.NewC(l)
|
||||||
|
conf.Settings["pki"] = map[string]any{"cert": string(certPEM), "key": string(keyPEM)}
|
||||||
|
conf.Settings["handshakes"] = map[string]any{"pq": map[string]any{"require": true}}
|
||||||
|
_, err = newCertStateFromConfig(conf, "aes")
|
||||||
|
require.ErrorContains(t, err, "handshakes.pq.require needs a version 3 certificate")
|
||||||
|
}
|
||||||
|
|
||||||
|
// After answering a peer's hybrid msg1, its classical msg1s go unanswered for
|
||||||
|
// four retry intervals, and only for that peer.
|
||||||
|
func TestHybridCrossingWindow(t *testing.T) {
|
||||||
|
hm, _ := newPQTestManager(t)
|
||||||
|
peer := []netip.Addr{netip.MustParseAddr("10.1.0.1")}
|
||||||
|
other := []netip.Addr{netip.MustParseAddr("10.1.0.2")}
|
||||||
|
now := time.Unix(1000, 0)
|
||||||
|
w := 4 * hm.config.tryInterval
|
||||||
|
assert.Equal(t, w, hm.hybridWindow())
|
||||||
|
|
||||||
|
assert.False(t, hm.crossesHybrid(peer, now))
|
||||||
|
hm.noteHybridMsg1(peer, now)
|
||||||
|
assert.True(t, hm.crossesHybrid(peer, now.Add(w-time.Millisecond)))
|
||||||
|
assert.False(t, hm.crossesHybrid(peer, now.Add(w)))
|
||||||
|
assert.False(t, hm.crossesHybrid(other, now))
|
||||||
|
|
||||||
|
hm.expireHybridResponders(now.Add(w))
|
||||||
|
hm.hybrid.answeredMu.Lock()
|
||||||
|
assert.Empty(t, hm.hybrid.answered, "expired windows are forgotten")
|
||||||
|
hm.hybrid.answeredMu.Unlock()
|
||||||
|
}
|
||||||
+17
-2
@@ -67,8 +67,22 @@ const (
|
|||||||
const (
|
const (
|
||||||
HandshakeIXPSK0 MessageSubType = 0
|
HandshakeIXPSK0 MessageSubType = 0
|
||||||
HandshakeXXPSK0 MessageSubType = 1
|
HandshakeXXPSK0 MessageSubType = 1
|
||||||
|
|
||||||
|
// 2 was ix_psk2_mlkem1024, an ephemeral ML-KEM exchange over IX on version 2
|
||||||
|
// certificates that was never released. Do not reuse it.
|
||||||
|
|
||||||
|
// HandshakeIXHybridMLKEM1024 is the hybrid IX of github.com/DefinedNet/noise:
|
||||||
|
// IX with X25519 or P256 and ML-KEM-1024 side by side, authenticated by
|
||||||
|
// version 3 certificates. It takes three messages and travels as fragments.
|
||||||
|
HandshakeIXHybridMLKEM1024 MessageSubType = 3
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// IsFragmented reports whether handshake messages of this subtype travel as
|
||||||
|
// fragments.
|
||||||
|
func IsFragmented(st MessageSubType) bool {
|
||||||
|
return st == HandshakeIXHybridMLKEM1024
|
||||||
|
}
|
||||||
|
|
||||||
var ErrHeaderTooShort = errors.New("header is too short")
|
var ErrHeaderTooShort = errors.New("header is too short")
|
||||||
|
|
||||||
var subTypeTestMap = map[MessageSubType]string{
|
var subTypeTestMap = map[MessageSubType]string{
|
||||||
@@ -88,7 +102,8 @@ var subTypeMap = map[MessageType]*map[MessageSubType]string{
|
|||||||
Test: &subTypeTestMap,
|
Test: &subTypeTestMap,
|
||||||
CloseTunnel: &subTypeNoneMap,
|
CloseTunnel: &subTypeNoneMap,
|
||||||
Handshake: {
|
Handshake: {
|
||||||
HandshakeIXPSK0: "ix_psk0",
|
HandshakeIXPSK0: "ix_psk0",
|
||||||
|
HandshakeIXHybridMLKEM1024: "ix_hybrid_mlkem1024",
|
||||||
},
|
},
|
||||||
Control: &subTypeNoneMap,
|
Control: &subTypeNoneMap,
|
||||||
}
|
}
|
||||||
@@ -199,7 +214,7 @@ func IsValidSubType(t MessageType, s MessageSubType) bool {
|
|||||||
case Message:
|
case Message:
|
||||||
return s == MessageNone || s == MessageRelay
|
return s == MessageNone || s == MessageRelay
|
||||||
case Handshake:
|
case Handshake:
|
||||||
return s == HandshakeIXPSK0
|
return s == HandshakeIXPSK0 || s == HandshakeIXHybridMLKEM1024
|
||||||
case Test:
|
case Test:
|
||||||
return s == TestReply || s == TestRequest
|
return s == TestReply || s == TestRequest
|
||||||
case Control, CloseTunnel, RecvError, LightHouse:
|
case Control, CloseTunnel, RecvError, LightHouse:
|
||||||
|
|||||||
@@ -96,7 +96,8 @@ func TestTypeMap(t *testing.T) {
|
|||||||
Test: &subTypeTestMap,
|
Test: &subTypeTestMap,
|
||||||
CloseTunnel: &subTypeNoneMap,
|
CloseTunnel: &subTypeNoneMap,
|
||||||
Handshake: {
|
Handshake: {
|
||||||
HandshakeIXPSK0: "ix_psk0",
|
HandshakeIXPSK0: "ix_psk0",
|
||||||
|
HandshakeIXHybridMLKEM1024: "ix_hybrid_mlkem1024",
|
||||||
},
|
},
|
||||||
Control: &subTypeNoneMap,
|
Control: &subTypeNoneMap,
|
||||||
}, subTypeMap)
|
}, subTypeMap)
|
||||||
@@ -122,6 +123,9 @@ func TestIsValidSubType(t *testing.T) {
|
|||||||
assert.False(t, IsValidSubType(Message, 2))
|
assert.False(t, IsValidSubType(Message, 2))
|
||||||
|
|
||||||
assert.True(t, IsValidSubType(Handshake, HandshakeIXPSK0))
|
assert.True(t, IsValidSubType(Handshake, HandshakeIXPSK0))
|
||||||
|
assert.True(t, IsValidSubType(Handshake, HandshakeIXHybridMLKEM1024))
|
||||||
|
// 2 was the unreleased ix_psk2_mlkem1024 and stays reserved.
|
||||||
|
assert.False(t, IsValidSubType(Handshake, 2))
|
||||||
// HandshakeXXPSK0 is defined but not a wire-valid subtype.
|
// HandshakeXXPSK0 is defined but not a wire-valid subtype.
|
||||||
assert.False(t, IsValidSubType(Handshake, HandshakeXXPSK0))
|
assert.False(t, IsValidSubType(Handshake, HandshakeXXPSK0))
|
||||||
|
|
||||||
|
|||||||
+11
-7
@@ -260,6 +260,12 @@ type HostInfo struct {
|
|||||||
// We need these to avoid replayed handshake packets creating new hostinfos which causes churn
|
// We need these to avoid replayed handshake packets creating new hostinfos which causes churn
|
||||||
HandshakePacket map[uint8][]byte
|
HandshakePacket map[uint8][]byte
|
||||||
|
|
||||||
|
// A responder resending a fragmented handshake response offers symbols it has
|
||||||
|
// not sent before. Once complete, a hybrid responder confirms again for a
|
||||||
|
// resent msg3 at most once per retry interval.
|
||||||
|
handshakeResendAt atomic.Int64 // unix nanos of the last confirmation
|
||||||
|
handshakeNextSymbol atomic.Uint32 // next fragment symbol index to offer
|
||||||
|
|
||||||
// nextLHQuery is the earliest we can ask the lighthouse for new information.
|
// nextLHQuery is the earliest we can ask the lighthouse for new information.
|
||||||
// This is used to limit lighthouse re-queries in chatty clients
|
// This is used to limit lighthouse re-queries in chatty clients
|
||||||
nextLHQuery atomic.Int64
|
nextLHQuery atomic.Int64
|
||||||
@@ -316,9 +322,9 @@ type cachedPacketMetrics struct {
|
|||||||
func NewHostMapFromConfig(l *slog.Logger, c *config.C) *HostMap {
|
func NewHostMapFromConfig(l *slog.Logger, c *config.C) *HostMap {
|
||||||
hm := newHostMap(l)
|
hm := newHostMap(l)
|
||||||
|
|
||||||
hm.reload(c, true, nil)
|
hm.reload(c, true)
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
hm.reload(c, false, r)
|
hm.reload(c, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
||||||
@@ -337,8 +343,7 @@ func newHostMap(l *slog.Logger) *HostMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// r is nil on the initial load
|
func (hm *HostMap) reload(c *config.C, initial bool) {
|
||||||
func (hm *HostMap) reload(c *config.C, initial bool, r *config.Reload) {
|
|
||||||
if initial || c.HasChanged("preferred_ranges") {
|
if initial || c.HasChanged("preferred_ranges") {
|
||||||
var preferredRanges []netip.Prefix
|
var preferredRanges []netip.Prefix
|
||||||
rawPreferredRanges := c.GetStringSlice("preferred_ranges", []string{})
|
rawPreferredRanges := c.GetStringSlice("preferred_ranges", []string{})
|
||||||
@@ -349,9 +354,8 @@ func (hm *HostMap) reload(c *config.C, initial bool, r *config.Reload) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
hm.l.Warn("Failed to parse preferred ranges, ignoring",
|
hm.l.Warn("Failed to parse preferred ranges, ignoring",
|
||||||
"error", err,
|
"error", err,
|
||||||
"range", rawPreferredRange,
|
"range", rawPreferredRanges,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -447,16 +447,3 @@ func TestHostInfo_markOutOnly(t *testing.T) {
|
|||||||
assert.True(t, h.markOut(6), "a relayed send must not consume the epoch edge")
|
assert.True(t, h.markOut(6), "a relayed send must not consume the epoch edge")
|
||||||
assert.False(t, h.markOut(6))
|
assert.False(t, h.markOut(6))
|
||||||
}
|
}
|
||||||
|
|
||||||
// A preferred range that doesn't parse is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestHostMap_reloadBadPreferredRangeFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("preferred_ranges: [10.0.0.0/8]\n"))
|
|
||||||
hm := NewHostMapFromConfig(l, c)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("preferred_ranges: [10.0.0.0/8, nope]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")}, hm.GetPreferredRanges())
|
|
||||||
}
|
|
||||||
+5
-7
@@ -509,11 +509,10 @@ func (f *Interface) flushSendBatch(sb *batch.SendBatch, q int) {
|
|||||||
|
|
||||||
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
func (f *Interface) RegisterConfigChangeCallbacks(c *config.C) {
|
||||||
c.RegisterReloadCallback(f.reloadFirewall)
|
c.RegisterReloadCallback(f.reloadFirewall)
|
||||||
for _, reload := range []func(*config.C){
|
c.RegisterReloadCallback(f.reloadSendRecvError)
|
||||||
f.reloadSendRecvError, f.reloadAcceptRecvError, f.reloadDisconnectInvalid, f.reloadMisc,
|
c.RegisterReloadCallback(f.reloadAcceptRecvError)
|
||||||
} {
|
c.RegisterReloadCallback(f.reloadDisconnectInvalid)
|
||||||
c.RegisterReloadCallback(func(c *config.C, _ *config.Reload) { reload(c) })
|
c.RegisterReloadCallback(f.reloadMisc)
|
||||||
}
|
|
||||||
|
|
||||||
for _, udpConn := range f.writers {
|
for _, udpConn := range f.writers {
|
||||||
c.RegisterReloadCallback(udpConn.ReloadConfig)
|
c.RegisterReloadCallback(udpConn.ReloadConfig)
|
||||||
@@ -530,7 +529,7 @@ func (f *Interface) reloadDisconnectInvalid(c *config.C) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Interface) reloadFirewall(c *config.C, r *config.Reload) {
|
func (f *Interface) reloadFirewall(c *config.C) {
|
||||||
cs := f.pki.getCertState()
|
cs := f.pki.getCertState()
|
||||||
curCert := cs.getCertificate(cert.Version2)
|
curCert := cs.getCertificate(cert.Version2)
|
||||||
if curCert == nil {
|
if curCert == nil {
|
||||||
@@ -549,7 +548,6 @@ func (f *Interface) reloadFirewall(c *config.C, r *config.Reload) {
|
|||||||
fw, err := NewFirewallFromConfig(f.l, cs, c)
|
fw, err := NewFirewallFromConfig(f.l, cs, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
f.l.Error("Error while creating firewall during reload", "error", err)
|
f.l.Error("Error while creating firewall during reload", "error", err)
|
||||||
r.Failed()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-26
@@ -65,10 +65,10 @@ func TestReloadFirewall_CertUnsafeNetworksChanged(t *testing.T) {
|
|||||||
pki.cs.Store(&CertState{v2Cert: c2, initiatingVersion: cert.Version2})
|
pki.cs.Store(&CertState{v2Cert: c2, initiatingVersion: cert.Version2})
|
||||||
|
|
||||||
// Reload with the same YAML so HasChanged("firewall") reports false.
|
// Reload with the same YAML so HasChanged("firewall") reports false.
|
||||||
require.NoError(t, cfg.ReloadConfigString(rawYAML).Err)
|
require.NoError(t, cfg.ReloadConfigString(rawYAML))
|
||||||
require.False(t, cfg.HasChanged("firewall"))
|
require.False(t, cfg.HasChanged("firewall"))
|
||||||
|
|
||||||
f.reloadFirewall(cfg, nil)
|
f.reloadFirewall(cfg)
|
||||||
|
|
||||||
assert.NotSame(t, fw, f.firewall, "firewall pointer should have been replaced")
|
assert.NotSame(t, fw, f.firewall, "firewall pointer should have been replaced")
|
||||||
assert.Equal(t, newUnsafe, f.firewall.unsafeNetworks)
|
assert.Equal(t, newUnsafe, f.firewall.unsafeNetworks)
|
||||||
@@ -113,30 +113,8 @@ func TestReloadFirewall_NoChange(t *testing.T) {
|
|||||||
l: l,
|
l: l,
|
||||||
}
|
}
|
||||||
|
|
||||||
require.NoError(t, cfg.ReloadConfigString(rawYAML).Err)
|
require.NoError(t, cfg.ReloadConfigString(rawYAML))
|
||||||
f.reloadFirewall(cfg, nil)
|
f.reloadFirewall(cfg)
|
||||||
|
|
||||||
assert.Same(t, fw, f.firewall, "firewall should not have been replaced")
|
assert.Same(t, fw, f.firewall, "firewall should not have been replaced")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rules that don't parse keep the old firewall, and the reload says it could not apply them
|
|
||||||
func TestReloadFirewall_BadRulesFailTheReload(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
pki := &PKI{}
|
|
||||||
pki.cs.Store(&CertState{v2Cert: &dummyCert{version: cert.Version2, networks: []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}}, initiatingVersion: cert.Version2})
|
|
||||||
|
|
||||||
rules := func(proto string) string {
|
|
||||||
return "firewall:\n outbound:\n - port: any\n proto: any\n host: any\n inbound:\n - port: any\n proto: " + proto + "\n host: any\n"
|
|
||||||
}
|
|
||||||
cfg := config.NewC(l)
|
|
||||||
require.NoError(t, cfg.LoadString(rules("any")))
|
|
||||||
fw, err := NewFirewallFromConfig(l, pki.getCertState(), cfg)
|
|
||||||
require.NoError(t, err)
|
|
||||||
f := &Interface{pki: pki, firewall: fw, l: l}
|
|
||||||
cfg.RegisterReloadCallback(f.reloadFirewall)
|
|
||||||
|
|
||||||
res := cfg.ReloadConfigString(rules("nope"))
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Same(t, fw, f.firewall)
|
|
||||||
}
|
|
||||||
+6
-12
@@ -138,21 +138,18 @@ func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, c
|
|||||||
h.metrics = newLighthouseMetrics()
|
h.metrics = newLighthouseMetrics()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := h.reload(c, true, nil)
|
err := h.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C) {
|
||||||
r.RestartRequiredIfChanged("lighthouse.am_lighthouse", "listen.port", "handshakes.query_buffer", "stats.lighthouse_metrics")
|
err := h.reload(c, false)
|
||||||
err := h.reload(c, false, r)
|
|
||||||
switch v := err.(type) {
|
switch v := err.(type) {
|
||||||
case *util.ContextualError:
|
case *util.ContextualError:
|
||||||
v.Log(l)
|
v.Log(l)
|
||||||
r.Failed()
|
|
||||||
case error:
|
case error:
|
||||||
l.Error("failed to reload lighthouse", "error", err)
|
l.Error("failed to reload lighthouse", "error", err)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -193,8 +190,7 @@ func (lh *LightHouse) GetUpdateInterval() int64 {
|
|||||||
return lh.interval.Load()
|
return lh.interval.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
// r is nil on the initial load
|
func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
||||||
func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error {
|
|
||||||
if initial || c.HasChanged("lighthouse.advertise_addrs") {
|
if initial || c.HasChanged("lighthouse.advertise_addrs") {
|
||||||
rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
|
rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
|
||||||
advAddrs := make([]netip.AddrPort, 0)
|
advAddrs := make([]netip.AddrPort, 0)
|
||||||
@@ -229,7 +225,6 @@ func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error
|
|||||||
"addr", rawAddr,
|
"addr", rawAddr,
|
||||||
"entry", i+1,
|
"entry", i+1,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,7 +383,6 @@ func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error
|
|||||||
"relay", v,
|
"relay", v,
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
} else {
|
} else {
|
||||||
lh.l.Info("Read relay from config", "relay", v)
|
lh.l.Info("Read relay from config", "relay", v)
|
||||||
relaysForMe = append(relaysForMe, configRIP)
|
relaysForMe = append(relaysForMe, configRIP)
|
||||||
@@ -845,7 +839,7 @@ func (lh *LightHouse) innerQueryServer(addr netip.Addr, nb, out []byte) {
|
|||||||
for _, lhVpnAddr := range lighthouses {
|
for _, lhVpnAddr := range lighthouses {
|
||||||
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
||||||
if hi != nil {
|
if hi != nil {
|
||||||
v = hi.ConnectionState.myCert.Version()
|
v = protocolVersion(hi.ConnectionState.myCert.Version())
|
||||||
} else {
|
} else {
|
||||||
v = lh.ifce.GetCertState().initiatingVersion
|
v = lh.ifce.GetCertState().initiatingVersion
|
||||||
}
|
}
|
||||||
@@ -1007,7 +1001,7 @@ func (lh *LightHouse) SendUpdate() {
|
|||||||
var v cert.Version
|
var v cert.Version
|
||||||
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
hi := lh.ifce.GetHostInfo(lhVpnAddr)
|
||||||
if hi != nil {
|
if hi != nil {
|
||||||
v = hi.ConnectionState.myCert.Version()
|
v = protocolVersion(hi.ConnectionState.myCert.Version())
|
||||||
} else {
|
} else {
|
||||||
v = lh.ifce.GetCertState().initiatingVersion
|
v = lh.ifce.GetCertState().initiatingVersion
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-59
@@ -87,15 +87,15 @@ func TestReloadLighthouseInterval(t *testing.T) {
|
|||||||
lh.ifce = &mockEncWriter{}
|
lh.ifce = &mockEncWriter{}
|
||||||
|
|
||||||
// The first one routine is kicked off by main.go currently, lets make sure that one dies
|
// The first one routine is kicked off by main.go currently, lets make sure that one dies
|
||||||
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5").Err)
|
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5"))
|
||||||
assert.Equal(t, int64(5), lh.interval.Load())
|
assert.Equal(t, int64(5), lh.interval.Load())
|
||||||
|
|
||||||
// Subsequent calls are killed off by the LightHouse.Reload function
|
// Subsequent calls are killed off by the LightHouse.Reload function
|
||||||
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10").Err)
|
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10"))
|
||||||
assert.Equal(t, int64(10), lh.interval.Load())
|
assert.Equal(t, int64(10), lh.interval.Load())
|
||||||
|
|
||||||
// If this completes then nothing is stealing our reload routine
|
// If this completes then nothing is stealing our reload routine
|
||||||
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11").Err)
|
require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11"))
|
||||||
assert.Equal(t, int64(11), lh.interval.Load())
|
assert.Equal(t, int64(11), lh.interval.Load())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +296,7 @@ func TestLighthouse_reload(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
c.ReloadConfigString(string(rc))
|
c.ReloadConfigString(string(rc))
|
||||||
|
|
||||||
err = lh.reload(c, false, nil)
|
err = lh.reload(c, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,7 +334,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc, err := yaml.Marshal(nc)
|
rc, err := yaml.Marshal(nc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
rl := lh.Query(staticHost)
|
rl := lh.Query(staticHost)
|
||||||
require.NotNil(t, rl)
|
require.NotNil(t, rl)
|
||||||
@@ -350,7 +350,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc, err = yaml.Marshal(nc)
|
rc, err = yaml.Marshal(nc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
rl = lh.Query(staticHost)
|
rl = lh.Query(staticHost)
|
||||||
require.NotNil(t, rl)
|
require.NotNil(t, rl)
|
||||||
@@ -358,7 +358,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.1.1.1:4242")}, rl.CopyAddrs([]netip.Prefix{}))
|
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.1.1.1:4242")}, rl.CopyAddrs([]netip.Prefix{}))
|
||||||
|
|
||||||
// Reload with the same config. An unchanged entry must not duplicate.
|
// Reload with the same config. An unchanged entry must not duplicate.
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
rl = lh.Query(staticHost)
|
rl = lh.Query(staticHost)
|
||||||
require.NotNil(t, rl)
|
require.NotNil(t, rl)
|
||||||
@@ -373,7 +373,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc, err = yaml.Marshal(nc)
|
rc, err = yaml.Marshal(nc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
// Add a second host alongside the first. Both should be present, neither duplicated.
|
// Add a second host alongside the first. Both should be present, neither duplicated.
|
||||||
nc = map[string]any{
|
nc = map[string]any{
|
||||||
@@ -384,7 +384,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc, err = yaml.Marshal(nc)
|
rc, err = yaml.Marshal(nc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
rl = lh.Query(staticHost)
|
rl = lh.Query(staticHost)
|
||||||
require.NotNil(t, rl)
|
require.NotNil(t, rl)
|
||||||
@@ -406,7 +406,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
|||||||
}
|
}
|
||||||
rc, err = yaml.Marshal(nc)
|
rc, err = yaml.Marshal(nc)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NoError(t, c.ReloadConfigString(string(rc)).Err)
|
require.NoError(t, c.ReloadConfigString(string(rc)))
|
||||||
|
|
||||||
_, isStatic := lh.GetStaticHostList()[staticHost]
|
_, isStatic := lh.GetStaticHostList()[staticHost]
|
||||||
assert.False(t, isStatic)
|
assert.False(t, isStatic)
|
||||||
@@ -966,13 +966,13 @@ func TestLighthouse_reloadRelaysTriggersUpdate(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Empty(t, lh.updateTrigger)
|
assert.Empty(t, lh.updateTrigger)
|
||||||
|
|
||||||
require.NoError(t, c.ReloadConfigString(base+"relay:\n relays: [10.128.0.6]\n").Err)
|
require.NoError(t, c.ReloadConfigString(base+"relay:\n relays: [10.128.0.6]\n"))
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.6")}, lh.GetRelaysForMe())
|
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.6")}, lh.GetRelaysForMe())
|
||||||
require.Len(t, lh.updateTrigger, 1)
|
require.Len(t, lh.updateTrigger, 1)
|
||||||
<-lh.updateTrigger
|
<-lh.updateTrigger
|
||||||
|
|
||||||
// The config changed but what we advertise did not, a bad entry is skipped
|
// The config changed but what we advertise did not, a bad entry is skipped
|
||||||
require.NoError(t, c.ReloadConfigString(base+"relay:\n relays: [10.128.0.6, nope]\n").Err)
|
require.NoError(t, c.ReloadConfigString(base+"relay:\n relays: [10.128.0.6, nope]\n"))
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.6")}, lh.GetRelaysForMe())
|
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.6")}, lh.GetRelaysForMe())
|
||||||
assert.Empty(t, lh.updateTrigger)
|
assert.Empty(t, lh.updateTrigger)
|
||||||
}
|
}
|
||||||
@@ -988,57 +988,12 @@ func TestLighthouse_reloadAmRelayAlone(t *testing.T) {
|
|||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.5")}, lh.GetRelaysForMe())
|
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.5")}, lh.GetRelaysForMe())
|
||||||
|
|
||||||
// Relays aren't allowed to have relays, flipping am_relay alone must drop them and tell the lighthouses
|
// Relays aren't allowed to have relays, flipping am_relay alone must drop them and tell the lighthouses
|
||||||
require.NoError(t, c.ReloadConfigString(base+" am_relay: true\n").Err)
|
require.NoError(t, c.ReloadConfigString(base+" am_relay: true\n"))
|
||||||
assert.Empty(t, lh.GetRelaysForMe())
|
assert.Empty(t, lh.GetRelaysForMe())
|
||||||
require.Len(t, lh.updateTrigger, 1)
|
require.Len(t, lh.updateTrigger, 1)
|
||||||
<-lh.updateTrigger
|
<-lh.updateTrigger
|
||||||
|
|
||||||
require.NoError(t, c.ReloadConfigString(base+" am_relay: false\n").Err)
|
require.NoError(t, c.ReloadConfigString(base+" am_relay: false\n"))
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.5")}, lh.GetRelaysForMe())
|
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.5")}, lh.GetRelaysForMe())
|
||||||
require.Len(t, lh.updateTrigger, 1)
|
require.Len(t, lh.updateTrigger, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLighthouse_reloadRestartRequired(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
base := "lighthouse:\n am_lighthouse: true\n"
|
|
||||||
require.NoError(t, c.LoadString(base+"listen:\n port: 4242\n"))
|
|
||||||
|
|
||||||
_, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(netip.MustParsePrefix("10.128.0.1/24")), nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString(base + "listen:\n port: 4242\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.False(t, res.RestartRequired)
|
|
||||||
res = c.ReloadConfigString(base + "listen:\n port: 4243\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.RestartRequired)
|
|
||||||
}
|
|
||||||
|
|
||||||
// A relay that doesn't parse is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestLighthouse_reloadBadRelayFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("listen:\n port: 4242\n"))
|
|
||||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(netip.MustParsePrefix("10.128.0.1/24")), nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("listen:\n port: 4242\nrelay:\n relays: [10.128.0.2, nope]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.2")}, lh.GetRelaysForMe())
|
|
||||||
}
|
|
||||||
|
|
||||||
// An advertise_addrs entry inside our own network is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestLighthouse_reloadAdvertiseAddrInNetworkFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("listen:\n port: 4242\n"))
|
|
||||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(netip.MustParsePrefix("10.128.0.1/24")), nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("listen:\n port: 4242\nlighthouse:\n advertise_addrs: [\"1.2.3.4:4242\", \"10.128.0.5:4242\"]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.2.3.4:4242")}, lh.GetAdvertiseAddrs())
|
|
||||||
}
|
|
||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/commands"
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/cpupick"
|
"github.com/slackhq/nebula/cpupick"
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
"github.com/slackhq/nebula/noiseutil"
|
||||||
@@ -72,18 +71,14 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
}
|
}
|
||||||
l.Info("Firewall started", "firewallHashes", fw.GetRuleHashes())
|
l.Info("Firewall started", "firewallHashes", fw.GetRuleHashes())
|
||||||
|
|
||||||
// The command registry is shared by every surface that exposes these commands, so a
|
ssh, err := sshd.NewSSHServer(ctx, l.With("subsystem", "sshd"))
|
||||||
// command cannot reach one of them without reaching the others.
|
|
||||||
cmdRegistry := commands.New()
|
|
||||||
|
|
||||||
ssh, err := sshd.NewSSHServer(ctx, l.With("subsystem", "sshd"), cmdRegistry)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, util.ContextualizeIfNeeded("Error while creating SSH server", err)
|
return nil, util.ContextualizeIfNeeded("Error while creating SSH server", err)
|
||||||
}
|
}
|
||||||
wireSSHReload(l, ssh, c)
|
wireSSHReload(l, ssh, c)
|
||||||
var sshStart func()
|
var sshStart func()
|
||||||
if c.GetBool("sshd.enabled", false) {
|
if c.GetBool("sshd.enabled", false) {
|
||||||
sshStart, err = configSSH(l, ssh, c, nil)
|
sshStart, err = configSSH(l, ssh, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warn("Failed to configure sshd, ssh debugging will not be available", "error", err)
|
l.Warn("Failed to configure sshd, ssh debugging will not be available", "error", err)
|
||||||
sshStart = nil
|
sshStart = nil
|
||||||
@@ -202,7 +197,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, util.NewContextualError("Failed to open udp listener", m{"queue": i}, err)
|
return nil, util.NewContextualError("Failed to open udp listener", m{"queue": i}, err)
|
||||||
}
|
}
|
||||||
udpServer.ReloadConfig(c, nil)
|
udpServer.ReloadConfig(c)
|
||||||
udpConns[i] = udpServer
|
udpConns[i] = udpServer
|
||||||
|
|
||||||
// If port is dynamic, discover it before the next pass through the for loop
|
// If port is dynamic, discover it before the next pass through the for loop
|
||||||
@@ -237,9 +232,12 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
retries: int64(c.GetInt("handshakes.retries", DefaultHandshakeRetries)),
|
retries: int64(c.GetInt("handshakes.retries", DefaultHandshakeRetries)),
|
||||||
triggerBuffer: c.GetInt("handshakes.trigger_buffer", DefaultHandshakeTriggerBuffer),
|
triggerBuffer: c.GetInt("handshakes.trigger_buffer", DefaultHandshakeTriggerBuffer),
|
||||||
messageMetrics: messageMetrics,
|
messageMetrics: messageMetrics,
|
||||||
|
pq: pqHandshakeConfigFrom(c),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
warnPQWithoutV3(l, handshakeConfig.pq, pki.getCertState())
|
||||||
handshakeManager := NewHandshakeManager(l, hostMap, lightHouse, udpConns[0], handshakeConfig)
|
handshakeManager := NewHandshakeManager(l, hostMap, lightHouse, udpConns[0], handshakeConfig)
|
||||||
|
c.RegisterReloadCallback(handshakeManager.reloadPQ)
|
||||||
lightHouse.handshakeTrigger = handshakeManager.trigger
|
lightHouse.handshakeTrigger = handshakeManager.trigger
|
||||||
|
|
||||||
ds, err := newDnsServerFromConfig(ctx, l, pki, hostMap, c)
|
ds, err := newDnsServerFromConfig(ctx, l, pki, hostMap, c)
|
||||||
@@ -316,16 +314,6 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
lightHouse.ifce = ifce
|
lightHouse.ifce = ifce
|
||||||
|
|
||||||
ifce.RegisterConfigChangeCallbacks(c)
|
ifce.RegisterConfigChangeCallbacks(c)
|
||||||
// These are only read at startup
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
|
||||||
r.RestartRequiredIfChanged(
|
|
||||||
"routines", "tun.routines", "listen.routines", "firewall.conntrack.routine_cache_timeout",
|
|
||||||
"listen.port", "listen.host", "stats.message_metrics",
|
|
||||||
"handshakes.try_interval", "handshakes.retries", "handshakes.trigger_buffer",
|
|
||||||
"tun.drop_local_broadcast", "tun.drop_multicast",
|
|
||||||
)
|
|
||||||
r.RestartRequiredIfChanged(osStartupOnlyKeys...)
|
|
||||||
})
|
|
||||||
ifce.reloadDisconnectInvalid(c)
|
ifce.reloadDisconnectInvalid(c)
|
||||||
ifce.reloadSendRecvError(c)
|
ifce.reloadSendRecvError(c)
|
||||||
ifce.reloadAcceptRecvError(c)
|
ifce.reloadAcceptRecvError(c)
|
||||||
@@ -348,14 +336,13 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
attachCommands(l, c, cmdRegistry, ifce)
|
attachCommands(l, c, ssh, ifce)
|
||||||
|
|
||||||
networkChanges := udp.NewNetworkChangeMonitor(ctx, l, c)
|
networkChanges := udp.NewNetworkChangeMonitor(ctx, l, c)
|
||||||
|
|
||||||
return &Control{
|
return &Control{
|
||||||
state: StateReady,
|
state: StateReady,
|
||||||
f: ifce,
|
f: ifce,
|
||||||
commands: cmdRegistry,
|
|
||||||
l: l,
|
l: l,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
//go:build linux && !android && !e2e_testing
|
|
||||||
|
|
||||||
package nebula
|
|
||||||
|
|
||||||
// osStartupOnlyKeys are startup only keys that only matter on linux: udp batch size, udp offloads and thread pinning
|
|
||||||
var osStartupOnlyKeys = []string{"listen.batch", "listen.udp_offloads", "tun.pin_threads", "tun.pin_threads_key", "tun.cpu_affinity"}
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
//go:build !linux || android || e2e_testing
|
|
||||||
|
|
||||||
package nebula
|
|
||||||
|
|
||||||
// osStartupOnlyKeys is empty, none of the OS specific startup keys matter here
|
|
||||||
var osStartupOnlyKeys []string
|
|
||||||
+1
-1
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CipherStateAESGCM is the data-plane wrapper for the AES-GCM AEAD cipher.
|
// CipherStateAESGCM is the data-plane wrapper for the AES-GCM AEAD cipher.
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@ package noiseutil
|
|||||||
import (
|
import (
|
||||||
"crypto/boring"
|
"crypto/boring"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
)
|
)
|
||||||
|
|
||||||
var CipherAESGCM noise.CipherFunc = CipherAESGCMFIPS140
|
var CipherAESGCM noise.CipherFunc = CipherAESGCMFIPS140
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CipherStateChaChaPoly is the data-plane wrapper for the ChaCha20-Poly1305 AEAD cipher.
|
// CipherStateChaChaPoly is the data-plane wrapper for the ChaCha20-Poly1305 AEAD cipher.
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/flynn/noise"
|
"github.com/DefinedNet/noise"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RejectHeadroom is the wrap gap for senders racing the counter, sized large enough for any routine count.
|
// RejectHeadroom is the wrap gap for senders racing the counter, sized large enough for any routine count.
|
||||||
|
|||||||
Loaded 100 of 157 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user