mirror of
https://github.com/slackhq/nebula.git
synced 2025-12-06 02:30:57 -08:00
Cert interface (#1212)
This commit is contained in:
parent
16eaae306a
commit
08ac65362e
49 changed files with 2862 additions and 2833 deletions
|
|
@ -4,7 +4,6 @@ import (
|
|||
"context"
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
|
|
@ -47,7 +46,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
|||
cs := &CertState{
|
||||
RawCertificate: []byte{},
|
||||
PrivateKey: []byte{},
|
||||
Certificate: &cert.NebulaCertificate{},
|
||||
Certificate: &dummyCert{},
|
||||
RawCertificateNoKey: []byte{},
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +79,7 @@ func Test_NewConnectionManagerTest(t *testing.T) {
|
|||
remoteIndexId: 9901,
|
||||
}
|
||||
hostinfo.ConnectionState = &ConnectionState{
|
||||
myCert: &cert.NebulaCertificate{},
|
||||
myCert: &dummyCert{},
|
||||
H: &noise.HandshakeState{},
|
||||
}
|
||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||
|
|
@ -130,7 +129,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||
cs := &CertState{
|
||||
RawCertificate: []byte{},
|
||||
PrivateKey: []byte{},
|
||||
Certificate: &cert.NebulaCertificate{},
|
||||
Certificate: &dummyCert{},
|
||||
RawCertificateNoKey: []byte{},
|
||||
}
|
||||
|
||||
|
|
@ -163,7 +162,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||
remoteIndexId: 9901,
|
||||
}
|
||||
hostinfo.ConnectionState = &ConnectionState{
|
||||
myCert: &cert.NebulaCertificate{},
|
||||
myCert: &dummyCert{},
|
||||
H: &noise.HandshakeState{},
|
||||
}
|
||||
nc.hostMap.unlockedAddHostInfo(hostinfo, ifce)
|
||||
|
|
@ -206,10 +205,7 @@ func Test_NewConnectionManagerTest2(t *testing.T) {
|
|||
func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
||||
now := time.Now()
|
||||
l := test.NewLogger()
|
||||
ipNet := net.IPNet{
|
||||
IP: net.IPv4(172, 1, 1, 2),
|
||||
Mask: net.IPMask{255, 255, 255, 0},
|
||||
}
|
||||
|
||||
vpncidr := netip.MustParsePrefix("172.1.1.1/24")
|
||||
localrange := netip.MustParsePrefix("10.1.1.1/24")
|
||||
vpnIp := netip.MustParseAddr("172.1.1.2")
|
||||
|
|
@ -219,41 +215,38 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||
|
||||
// Generate keys for CA and peer's cert.
|
||||
pubCA, privCA, _ := ed25519.GenerateKey(rand.Reader)
|
||||
caCert := cert.NebulaCertificate{
|
||||
Details: cert.NebulaCertificateDetails{
|
||||
Name: "ca",
|
||||
NotBefore: now,
|
||||
NotAfter: now.Add(1 * time.Hour),
|
||||
IsCA: true,
|
||||
PublicKey: pubCA,
|
||||
},
|
||||
tbs := &cert.TBSCertificate{
|
||||
Version: 1,
|
||||
Name: "ca",
|
||||
IsCA: true,
|
||||
NotBefore: now,
|
||||
NotAfter: now.Add(1 * time.Hour),
|
||||
PublicKey: pubCA,
|
||||
}
|
||||
|
||||
assert.NoError(t, caCert.Sign(cert.Curve_CURVE25519, privCA))
|
||||
ncp := &cert.NebulaCAPool{
|
||||
CAs: cert.NewCAPool().CAs,
|
||||
}
|
||||
ncp.CAs["ca"] = &caCert
|
||||
caCert, err := tbs.Sign(nil, cert.Curve_CURVE25519, privCA)
|
||||
assert.NoError(t, err)
|
||||
ncp := cert.NewCAPool()
|
||||
assert.NoError(t, ncp.AddCA(caCert))
|
||||
|
||||
pubCrt, _, _ := ed25519.GenerateKey(rand.Reader)
|
||||
peerCert := cert.NebulaCertificate{
|
||||
Details: cert.NebulaCertificateDetails{
|
||||
Name: "host",
|
||||
Ips: []*net.IPNet{&ipNet},
|
||||
Subnets: []*net.IPNet{},
|
||||
NotBefore: now,
|
||||
NotAfter: now.Add(60 * time.Second),
|
||||
PublicKey: pubCrt,
|
||||
IsCA: false,
|
||||
Issuer: "ca",
|
||||
},
|
||||
tbs = &cert.TBSCertificate{
|
||||
Version: 1,
|
||||
Name: "host",
|
||||
Networks: []netip.Prefix{vpncidr},
|
||||
NotBefore: now,
|
||||
NotAfter: now.Add(60 * time.Second),
|
||||
PublicKey: pubCrt,
|
||||
}
|
||||
assert.NoError(t, peerCert.Sign(cert.Curve_CURVE25519, privCA))
|
||||
peerCert, err := tbs.Sign(caCert, cert.Curve_CURVE25519, privCA)
|
||||
assert.NoError(t, err)
|
||||
|
||||
cachedPeerCert, err := ncp.VerifyCertificate(now.Add(time.Second), peerCert)
|
||||
|
||||
cs := &CertState{
|
||||
RawCertificate: []byte{},
|
||||
PrivateKey: []byte{},
|
||||
Certificate: &cert.NebulaCertificate{},
|
||||
Certificate: &dummyCert{},
|
||||
RawCertificateNoKey: []byte{},
|
||||
}
|
||||
|
||||
|
|
@ -282,8 +275,8 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||
hostinfo := &HostInfo{
|
||||
vpnIp: vpnIp,
|
||||
ConnectionState: &ConnectionState{
|
||||
myCert: &cert.NebulaCertificate{},
|
||||
peerCert: &peerCert,
|
||||
myCert: &dummyCert{},
|
||||
peerCert: cachedPeerCert,
|
||||
H: &noise.HandshakeState{},
|
||||
},
|
||||
}
|
||||
|
|
@ -303,3 +296,114 @@ func Test_NewConnectionManagerTest_DisconnectInvalid(t *testing.T) {
|
|||
invalid = nc.isInvalidCertificate(nextTick, hostinfo)
|
||||
assert.True(t, invalid)
|
||||
}
|
||||
|
||||
type dummyCert struct {
|
||||
version cert.Version
|
||||
curve cert.Curve
|
||||
groups []string
|
||||
isCa bool
|
||||
issuer string
|
||||
name string
|
||||
networks []netip.Prefix
|
||||
notAfter time.Time
|
||||
notBefore time.Time
|
||||
publicKey []byte
|
||||
signature []byte
|
||||
unsafeNetworks []netip.Prefix
|
||||
}
|
||||
|
||||
func (d *dummyCert) Version() cert.Version {
|
||||
return d.version
|
||||
}
|
||||
|
||||
func (d *dummyCert) Curve() cert.Curve {
|
||||
return d.curve
|
||||
}
|
||||
|
||||
func (d *dummyCert) Groups() []string {
|
||||
return d.groups
|
||||
}
|
||||
|
||||
func (d *dummyCert) IsCA() bool {
|
||||
return d.isCa
|
||||
}
|
||||
|
||||
func (d *dummyCert) Issuer() string {
|
||||
return d.issuer
|
||||
}
|
||||
|
||||
func (d *dummyCert) Name() string {
|
||||
return d.name
|
||||
}
|
||||
|
||||
func (d *dummyCert) Networks() []netip.Prefix {
|
||||
return d.networks
|
||||
}
|
||||
|
||||
func (d *dummyCert) NotAfter() time.Time {
|
||||
return d.notAfter
|
||||
}
|
||||
|
||||
func (d *dummyCert) NotBefore() time.Time {
|
||||
return d.notBefore
|
||||
}
|
||||
|
||||
func (d *dummyCert) PublicKey() []byte {
|
||||
return d.publicKey
|
||||
}
|
||||
|
||||
func (d *dummyCert) Signature() []byte {
|
||||
return d.signature
|
||||
}
|
||||
|
||||
func (d *dummyCert) UnsafeNetworks() []netip.Prefix {
|
||||
return d.unsafeNetworks
|
||||
}
|
||||
|
||||
func (d *dummyCert) MarshalForHandshakes() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) Sign(curve cert.Curve, key []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) CheckSignature(key []byte) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *dummyCert) Expired(t time.Time) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (d *dummyCert) CheckRootConstraints(signer cert.Certificate) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) VerifyPrivateKey(curve cert.Curve, key []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (d *dummyCert) Marshal() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) MarshalPEM() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) Fingerprint() (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) MarshalJSON() ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (d *dummyCert) Copy() cert.Certificate {
|
||||
return d
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue