mirror of
https://github.com/netbirdio/netbird.git
synced 2024-12-25 08:09:30 +01:00
5de4acf2fe
This PR aims to integrate Rosenpass with NetBird. It adds a manager for Rosenpass that starts a Rosenpass server and handles the managed peers. It uses the cunicu/go-rosenpass implementation. Rosenpass will then negotiate a pre-shared key every 2 minutes and apply it to the wireguard connection. The Feature can be enabled by setting a flag during the netbird up --enable-rosenpass command. If two peers are both support and have the Rosenpass feature enabled they will create a post-quantum secure connection. If one of the peers or both don't have this feature enabled or are running an older version that does not have this feature yet, the NetBird client will fall back to a plain Wireguard connection without pre-shared keys for those connections (keeping Rosenpass negotiation for the rest). Additionally, this PR includes an update of all Github Actions workflows to use go version 1.21.0 as this is a requirement for the integration. --------- Co-authored-by: braginini <bangvalo@gmail.com> Co-authored-by: Maycon Santos <mlsmaycon@gmail.com>
127 lines
2.8 KiB
Go
127 lines
2.8 KiB
Go
package rosenpass
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
rp "cunicu.li/go-rosenpass"
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
)
|
|
|
|
type wireGuardPeer struct {
|
|
Interface string
|
|
PublicKey rp.Key
|
|
}
|
|
|
|
type NetbirdHandler struct {
|
|
ifaceName string
|
|
client *wgctrl.Client
|
|
peers map[rp.PeerID]wireGuardPeer
|
|
presharedKey [32]byte
|
|
}
|
|
|
|
func NewNetbirdHandler(preSharedKey *[32]byte, wgIfaceName string) (hdlr *NetbirdHandler, err error) {
|
|
hdlr = &NetbirdHandler{
|
|
ifaceName: wgIfaceName,
|
|
peers: map[rp.PeerID]wireGuardPeer{},
|
|
}
|
|
|
|
if preSharedKey != nil {
|
|
hdlr.presharedKey = *preSharedKey
|
|
}
|
|
|
|
if hdlr.client, err = wgctrl.New(); err != nil {
|
|
return nil, fmt.Errorf("failed to creat WireGuard client: %w", err)
|
|
}
|
|
|
|
return hdlr, nil
|
|
}
|
|
|
|
func (h *NetbirdHandler) AddPeer(pid rp.PeerID, intf string, pk rp.Key) {
|
|
h.peers[pid] = wireGuardPeer{
|
|
Interface: intf,
|
|
PublicKey: pk,
|
|
}
|
|
}
|
|
|
|
func (h *NetbirdHandler) RemovePeer(pid rp.PeerID) {
|
|
delete(h.peers, pid)
|
|
}
|
|
|
|
func (h *NetbirdHandler) HandshakeCompleted(pid rp.PeerID, key rp.Key) {
|
|
log.Debug("Handshake complete")
|
|
h.outputKey(rp.KeyOutputReasonStale, pid, key)
|
|
}
|
|
|
|
func (h *NetbirdHandler) HandshakeExpired(pid rp.PeerID) {
|
|
key, _ := rp.GeneratePresharedKey()
|
|
log.Debug("Handshake expired")
|
|
h.outputKey(rp.KeyOutputReasonStale, pid, key)
|
|
}
|
|
|
|
func (h *NetbirdHandler) outputKey(_ rp.KeyOutputReason, pid rp.PeerID, psk rp.Key) {
|
|
wg, ok := h.peers[pid]
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
device, err := h.client.Device(h.ifaceName)
|
|
if err != nil {
|
|
log.Errorf("Failed to get WireGuard device: %v", err)
|
|
return
|
|
}
|
|
config := []wgtypes.PeerConfig{
|
|
{
|
|
UpdateOnly: true,
|
|
PublicKey: wgtypes.Key(wg.PublicKey),
|
|
PresharedKey: (*wgtypes.Key)(&psk),
|
|
},
|
|
}
|
|
for _, peer := range device.Peers {
|
|
if peer.PublicKey == wgtypes.Key(wg.PublicKey) {
|
|
if publicKeyEmpty(peer.PresharedKey) || peer.PresharedKey == h.presharedKey {
|
|
log.Debugf("Restart wireguard connection to peer %s", peer.PublicKey)
|
|
config = []wgtypes.PeerConfig{
|
|
{
|
|
PublicKey: wgtypes.Key(wg.PublicKey),
|
|
PresharedKey: (*wgtypes.Key)(&psk),
|
|
Endpoint: peer.Endpoint,
|
|
AllowedIPs: peer.AllowedIPs,
|
|
},
|
|
}
|
|
err = h.client.ConfigureDevice(wg.Interface, wgtypes.Config{
|
|
Peers: []wgtypes.PeerConfig{
|
|
{
|
|
Remove: true,
|
|
PublicKey: wgtypes.Key(wg.PublicKey),
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
slog.Debug("Failed to remove peer")
|
|
return
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
if err = h.client.ConfigureDevice(wg.Interface, wgtypes.Config{
|
|
Peers: config,
|
|
}); err != nil {
|
|
log.Errorf("Failed to apply rosenpass key: %v", err)
|
|
}
|
|
}
|
|
|
|
func publicKeyEmpty(key wgtypes.Key) bool {
|
|
for _, b := range key {
|
|
if b != 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|