Compare commits

..

1 Commits

Author SHA1 Message Date
amery a07ed61e00 zones: WIP
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-22 23:35:26 +00:00
7 changed files with 63 additions and 159 deletions
-1
View File
@@ -10,7 +10,6 @@ require (
github.com/burntSushi/toml v0.3.1 github.com/burntSushi/toml v0.3.1
github.com/mgechev/revive v1.3.2 github.com/mgechev/revive v1.3.2
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.12.0
gopkg.in/gcfg.v1 v1.2.3 gopkg.in/gcfg.v1 v1.2.3
) )
-2
View File
@@ -70,8 +70,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw= golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
+4 -4
View File
@@ -31,13 +31,13 @@ func (f *Config) Peers() int {
// InterfaceConfig represents the [Interface] section // InterfaceConfig represents the [Interface] section
type InterfaceConfig struct { type InterfaceConfig struct {
Address netip.Addr Address netip.Addr
PrivateKey PrivateKey PrivateKey BinaryKey
ListenPort uint16 ListenPort uint16
} }
// PeerConfig represents a [Peer] section // PeerConfig represents a [Peer] section
type PeerConfig struct { type PeerConfig struct {
PublicKey PublicKey PublicKey BinaryKey
Endpoint EndpointAddress Endpoint EndpointAddress
AllowedIPs []netip.Prefix AllowedIPs []netip.Prefix
} }
@@ -135,7 +135,7 @@ func (p interfaceConfig) Export() (InterfaceConfig, error) {
ListenPort: p.ListenPort, ListenPort: p.ListenPort,
} }
out.PrivateKey, err = PrivateKeyFromBase64(p.PrivateKey) out.PrivateKey, err = BinaryKeyFromBase64(p.PrivateKey)
if err != nil { if err != nil {
err = core.Wrap(err, "PrivateKey") err = core.Wrap(err, "PrivateKey")
return InterfaceConfig{}, err return InterfaceConfig{}, err
@@ -162,7 +162,7 @@ func (v *intermediateConfig) ExportPeer(i int) (PeerConfig, error) {
} }
// PublicKey // PublicKey
out.PublicKey, err = PublicKeyFromBase64(v.Peer.PublicKey[i]) out.PublicKey, err = BinaryKeyFromBase64(v.Peer.PublicKey[i])
if err != nil { if err != nil {
err = core.Wrap(err, "PublicKey") err = core.Wrap(err, "PublicKey")
return out, err return out, err
+15 -151
View File
@@ -2,171 +2,35 @@ package wireguard
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/base64" "encoding/base64"
"errors"
"golang.org/x/crypto/curve25519"
) )
const ( // BinaryKey is a binary blob
// PrivateKeySize is the length in bytes of a Wireguard Private Key type BinaryKey []byte
PrivateKeySize = 32
// PublicKeySize is the length in bytes of a Wireguard Public Key
PublicKeySize = 32
)
var ( func (k BinaryKey) String() string {
// ErrInvalidKeySize indicates the key size is wrong return base64.StdEncoding.EncodeToString(k)
ErrInvalidKeySize = errors.New("invalid key size")
// ErrInvalidPrivateKey indicates the private key is invalid
ErrInvalidPrivateKey = errors.New("invalid private key")
// ErrInvalidPublicKey indicates the public key is invalid
ErrInvalidPublicKey = errors.New("invalid public key")
)
type (
// PrivateKey is a binary Wireguard Private Key
PrivateKey []byte
// PublicKey is a binary Wireguard Public Key
PublicKey []byte
)
func (key PrivateKey) String() string {
return encodeKey(key)
}
func (pub PublicKey) String() string {
return encodeKey(pub)
} }
// IsZero tells if the key hasn't been set // IsZero tells if the key hasn't been set
func (key PrivateKey) IsZero() bool { func (k BinaryKey) IsZero() bool {
return len(key) == 0 return len(k) == 0
} }
// IsZero tells if the key hasn't been set // Equal checks if two keys are identical
func (pub PublicKey) IsZero() bool { func (k BinaryKey) Equal(alter BinaryKey) bool {
return len(pub) == 0 return bytes.Equal(k, alter)
} }
// Equal checks if two private keys are identical // BinaryKeyFromBase64 decodes a base64-based string into
func (key PrivateKey) Equal(alter PrivateKey) bool { // a [BinaryKey]
return bytes.Equal(key, alter) func BinaryKeyFromBase64(data string) (BinaryKey, error) {
}
// Equal checks if two public keys are identical
func (pub PublicKey) Equal(alter PublicKey) bool {
return bytes.Equal(pub, alter)
}
// PrivateKeyFromBase64 decodes a base64-based string into
// a [PrivateKey]
func PrivateKeyFromBase64(data string) (PrivateKey, error) {
b, err := decodeKey(data, PrivateKeySize)
return b, err
}
// PublicKeyFromBase64 decodes a base64-based string into
// a [PublicKey]
func PublicKeyFromBase64(data string) (PublicKey, error) {
b, err := decodeKey(data, PublicKeySize)
return b, err
}
func encodeKey(b []byte) string {
switch {
case len(b) == 0:
return ""
default:
return base64.StdEncoding.EncodeToString(b)
}
}
func decodeKey(data string, size int) ([]byte, error) {
b, err := base64.StdEncoding.DecodeString(data) b, err := base64.StdEncoding.DecodeString(data)
switch { return BinaryKey(b), err
case err != nil:
return []byte{}, err
case len(b) != size:
err = ErrInvalidKeySize
return []byte{}, err
default:
return b, nil
}
}
// NewPrivateKey creates a new PrivateKey
func NewPrivateKey() (PrivateKey, error) {
var s [PrivateKeySize]byte
_, err := rand.Read(s[:])
if err != nil {
return []byte{}, err
}
// apply same clamping as wireguard-go/device/noise-helpers.go
s[0] &= 0xf8
s[31] = (s[31] & 0x7f) | 0x40
return s[:], nil
}
// Public generates the corresponding PublicKey
func (key PrivateKey) Public() PublicKey {
if len(key) != PrivateKeySize {
return []byte{}
}
out := [PublicKeySize]byte{}
in := (*[PrivateKeySize]byte)(key)
curve25519.ScalarBaseMult(&out, in)
return out[:]
} }
// KeyPair holds a Key pair // KeyPair holds a Key pair
type KeyPair struct { type KeyPair struct {
PrivateKey PrivateKey PrivateKey BinaryKey
PublicKey PublicKey PublicKey BinaryKey
}
// Validate checks the PublicKey matches the PrivateKey,
// and sets the PublicKey if missing
func (kp *KeyPair) Validate() error {
keyLen := len(kp.PrivateKey)
pubLen := len(kp.PublicKey)
switch {
case keyLen != PrivateKeySize:
// bad private key
return ErrInvalidPrivateKey
case pubLen == 0:
// no public key, set it
kp.PublicKey = kp.PrivateKey.Public()
return nil
case pubLen != PublicKeySize:
// bad public key
return ErrInvalidPublicKey
case !kp.PrivateKey.Public().Equal(kp.PublicKey):
// wrong public key
return ErrInvalidPublicKey
default:
// correct public key
return nil
}
}
// NewKeyPair creates a new KeyPair for Wireguard
func NewKeyPair() (*KeyPair, error) {
key, err := NewPrivateKey()
if err != nil {
return nil, err
}
out := &KeyPair{
PrivateKey: key,
PublicKey: key.Public(),
}
return out, nil
} }
+1 -1
View File
@@ -88,7 +88,7 @@ func getRingZeroGatewayID(z *Zone) int {
firstNodeID = nodeID firstNodeID = nodeID
} }
if _, found := p.getRingInfo(0); found { if p.Gateway() {
gatewayID = nodeID gatewayID = nodeID
} }
+28
View File
@@ -47,6 +47,34 @@ func (m *Machine) ID() int {
return m.id return m.id
} }
// Gateway tells if the Machine is a ring0 gateway
func (m *Machine) Gateway() bool {
m.mu.Lock()
defer m.mu.Unlock()
if ri, found := m.getRingInfo(0); found {
return ri.Enabled
}
return false
}
func (m *Machine) SetGateway(enable bool) error {
m.mu.Lock()
defer m.mu.Unlock()
ri, found := m.getRingInfo(0)
switch {
case !found && !enable:
return nil
case !found:
return m.createRingInfo(0, true)
default:
ri.Enabled = enable
return nil
}
}
// FullName returns the Name of the machine including domain name // FullName returns the Name of the machine including domain name
func (m *Machine) FullName() string { func (m *Machine) FullName() string {
if domain := m.zone.zones.domain; domain != "" { if domain := m.zone.zones.domain; domain != "" {
+15
View File
@@ -134,3 +134,18 @@ func (m *Machine) applyZoneNodeID(zoneID, nodeID int) error {
return nil return nil
} }
func (m *Machine) createRingInfo(ring int, enabled bool) error {
keys, err := wireguard.NewKeyPair()
if err != nil {
return err
}
ri := &RingInfo{
Ring: ring,
Enabled: enabled,
Keys: keys,
}
return m.applyRingInfo(ring, ri)
}