| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/netip" |
| 5 |
"testing" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/flynn/noise" |
| 9 |
"github.com/slackhq/nebula/cert" |
| 10 |
ct "github.com/slackhq/nebula/cert_test" |
| 11 |
"github.com/slackhq/nebula/handshake" |
| 12 |
"github.com/slackhq/nebula/header" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
// runTestHandshake runs a complete IX handshake between two freshly-built |
| 18 |
// peers and returns the initiator and responder Results. Used to produce |
| 19 |
// real cipher states for tests that need to exercise post-handshake glue. |
| 20 |
func runTestHandshake(t *testing.T) (initR, respR *handshake.Result) { |
| 21 |
t.Helper() |
| 22 |
|
| 23 |
ca, _, caKey, _ := ct.NewTestCaCert( |
| 24 |
cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil, |
| 25 |
) |
| 26 |
caPool := ct.NewTestCAPool(ca) |
| 27 |
|
| 28 |
makeCreds := func(name string, networks []netip.Prefix) handshake.GetCredentialFunc { |
| 29 |
c, _, rawKey, _ := ct.NewTestCert( |
| 30 |
cert.Version2, cert.Curve_CURVE25519, ca, caKey, |
| 31 |
name, ca.NotBefore(), ca.NotAfter(), networks, nil, nil, |
| 32 |
) |
| 33 |
priv, _, _, err := cert.UnmarshalPrivateKeyFromPEM(rawKey) |
| 34 |
require.NoError(t, err) |
| 35 |
hsBytes, err := c.MarshalForHandshakes() |
| 36 |
require.NoError(t, err) |
| 37 |
ncs := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256) |
| 38 |
cred := handshake.NewCredential(c, hsBytes, priv, ncs) |
| 39 |
return func(v cert.Version) *handshake.Credential { |
| 40 |
if v == cert.Version2 { |
| 41 |
return cred |
| 42 |
} |
| 43 |
return nil |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
verifier := func(c cert.Certificate) (*cert.CachedCertificate, error) { |
| 48 |
return caPool.VerifyCertificate(time.Now(), c) |
| 49 |
} |
| 50 |
|
| 51 |
initCreds := makeCreds("initiator", []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}) |
| 52 |
respCreds := makeCreds("responder", []netip.Prefix{netip.MustParsePrefix("10.0.0.2/24")}) |
| 53 |
|
| 54 |
initM, err := handshake.NewMachine( |
| 55 |
cert.Version2, initCreds, verifier, |
| 56 |
func() (uint32, error) { return 1000, nil }, |
| 57 |
true, header.HandshakeIXPSK0, |
| 58 |
) |
| 59 |
require.NoError(t, err) |
| 60 |
|
| 61 |
respM, err := handshake.NewMachine( |
| 62 |
cert.Version2, respCreds, verifier, |
| 63 |
func() (uint32, error) { return 2000, nil }, |
| 64 |
false, header.HandshakeIXPSK0, |
| 65 |
) |
| 66 |
require.NoError(t, err) |
| 67 |
|
| 68 |
msg1, err := initM.Initiate(nil) |
| 69 |
require.NoError(t, err) |
| 70 |
|
| 71 |
resp, respR, err := respM.ProcessPacket(nil, msg1) |
| 72 |
require.NoError(t, err) |
| 73 |
require.NotNil(t, respR) |
| 74 |
|
| 75 |
_, initR, err = initM.ProcessPacket(nil, resp) |
| 76 |
require.NoError(t, err) |
| 77 |
require.NotNil(t, initR) |
| 78 |
|
| 79 |
return initR, respR |
| 80 |
} |
| 81 |
|
| 82 |
func TestNewConnectionStateFromResult(t *testing.T) { |
| 83 |
initR, respR := runTestHandshake(t) |
| 84 |
|
| 85 |
t.Run("initiator", func(t *testing.T) { |
| 86 |
ci := newConnectionStateFromResult(initR) |
| 87 |
assert.True(t, ci.initiator) |
| 88 |
assert.Equal(t, initR.MyCert, ci.myCert) |
| 89 |
assert.Equal(t, initR.RemoteCert, ci.peerCert) |
| 90 |
assert.NotNil(t, ci.eKey) |
| 91 |
assert.NotNil(t, ci.dKey) |
| 92 |
|
| 93 |
// IX has 2 handshake messages; the next data-plane send is counter=3. |
| 94 |
assert.Equal(t, uint64(2), ci.messageCounter.Load(), |
| 95 |
"messageCounter must equal Result.MessageIndex so the next send is N+1") |
| 96 |
|
| 97 |
// Both handshake counters must be marked seen so they don't appear lost. |
| 98 |
// Check returns false if an index has already been recorded. |
| 99 |
assert.False(t, ci.window.Check(nil, 1), "counter 1 must already be seen") |
| 100 |
assert.False(t, ci.window.Check(nil, 2), "counter 2 must already be seen") |
| 101 |
// Counter 3 is the next data-plane message and must NOT be pre-marked. |
| 102 |
assert.True(t, ci.window.Check(nil, 3), "counter 3 must not be pre-seeded") |
| 103 |
}) |
| 104 |
|
| 105 |
t.Run("responder", func(t *testing.T) { |
| 106 |
ci := newConnectionStateFromResult(respR) |
| 107 |
assert.False(t, ci.initiator) |
| 108 |
assert.Equal(t, respR.MyCert, ci.myCert) |
| 109 |
assert.Equal(t, respR.RemoteCert, ci.peerCert) |
| 110 |
assert.NotNil(t, ci.eKey) |
| 111 |
assert.NotNil(t, ci.dKey) |
| 112 |
assert.Equal(t, uint64(2), ci.messageCounter.Load()) |
| 113 |
}) |
| 114 |
} |