WireGuard / wgctrl-go

Package wgctrl enables control of WireGuard interfaces on multiple platforms.
https://godoc.org/golang.zx2c4.com/wireguard/wgctrl
MIT License
727 stars 85 forks source link

Bug in `func (k Key) PublicKey() Key`? #81

Closed ehudkaldor closed 4 years ago

ehudkaldor commented 4 years ago

I'm trying to use wctrl-go in a go program, but i am getting weird results.

the only usage for wgctrl-go i have currently is to generate keys, which i then put in the /etc/wireguard/wg0.conf. it's done with the following code:

    var privKey wgtypes.Key
    if privKey, err := wgtypes.GeneratePrivateKey(); err == nil {
        bouncerConfig.BouncerPrivKey = privKey.String()
        log.Printf("Bouncer private key: %v\n", bouncerConfig.BouncerPrivKey)
    } else {
        log.Println("ERROR: error generating private key", err)
        return nil, err
    }
    bouncerConfig.BouncerPubKey = privKey.PublicKey().String()
    log.Printf("Bouncer public key: %v\n", bouncerConfig.BouncerPubKey)

when i run it, this is what's logged:

2020-02-04 22:57:56.832801500  2020/02/04 22:57:56 Bouncer private key: +LRL+OLjKPNkdoqohPuIib6uYpIdl0GhR4PYS2oIGFs=
2020-02-04 22:57:56.832810500  2020/02/04 22:57:56 Bouncer public key: L+V9o0fNYkMVKNqsX7spBzD/9oSvxM/C7ZCZX1jLO3Q=

here's the weird part: if I copy the private key and generate a public key from it using wg, i get a totally different key:

bouncer2:/# echo "+LRL+OLjKPNkdoqohPuIib6uYpIdl0GhR4PYS2oIGFs=" | wg pubkey
a549Q66azW/EDHnrg1PmL7d4ruTO0/M2ti3Yo+dn8SI=
bouncer2:/#

And if I use it, the key generated by the go code does not work. this is on the the other node:

[Interface]
Address = 10.20.30.11
PrivateKey = eDSeLcnjtIZvBG0bkdnL4VF2KhkZ0jwIPHdGScv+fnw=
DNS = 1.1.1.1
MTU = 1500
SaveConfig = true

[Peer]
PublicKey = L+V9o0fNYkMVKNqsX7spBzD/9oSvxM/C7ZCZX1jLO3Q=
AllowedIPs = 10.20.30.10/32
Endpoint = 172.17.0.4:27182

/ # ip l del wg0; wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.20.30.11 dev wg0
[#] ip link set mtu 1500 up dev wg0
[#] resolvconf -a wg0 -m 0 -x
[#] ip -4 route add 10.20.30.10/32 dev wg0
/ # ping 10.20.30.10
PING 10.20.30.10 (10.20.30.10): 56 data bytes
^C
--- 10.20.30.10 ping statistics ---
2 packets transmitted, 0 packets received, 100% packet loss

and if i use the public key generated with wg on the command line, it works:

/ # cat /etc/wireguard/wg0.conf
[Interface]
Address = 10.20.30.11
PrivateKey = eDSeLcnjtIZvBG0bkdnL4VF2KhkZ0jwIPHdGScv+fnw=
DNS = 1.1.1.1
MTU = 1500
SaveConfig = true

[Peer]
PublicKey = a549Q66azW/EDHnrg1PmL7d4ruTO0/M2ti3Yo+dn8SI=
AllowedIPs = 10.20.30.10/32
Endpoint = 172.17.0.4:27182

/ # ip l del wg0; wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.20.30.11 dev wg0
[#] ip link set mtu 1500 up dev wg0
[#] resolvconf -a wg0 -m 0 -x
[#] ip -4 route add 10.20.30.10/32 dev wg0
/ # ping 10.20.30.10
PING 10.20.30.10 (10.20.30.10): 56 data bytes
64 bytes from 10.20.30.10: seq=0 ttl=64 time=2.854 ms
64 bytes from 10.20.30.10: seq=1 ttl=64 time=0.992 ms
64 bytes from 10.20.30.10: seq=2 ttl=64 time=0.484 ms
^C
--- 10.20.30.10 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.484/1.443/2.854 ms

am i doing something wrong?

mdlayher commented 4 years ago

You're shadowing privKey by declaring it again in the if statement. Generate the key and do the error check after.

priv, err := generate()
if err // ...
ehudkaldor commented 4 years ago

damn me, it is. thank you, and closing

mdlayher commented 4 years ago

No worries!