From 142ea005773f27d56951675e3f8905337b3cbd67 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Thu, 26 Oct 2023 22:11:05 +0000 Subject: [PATCH] wireguard: fix KeyPair.Validate() PrivateKey and PublicKey are now fixed length arrays, so testing for len 0 is invalid Signed-off-by: Alejandro Mery --- pkg/wireguard/keys.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/wireguard/keys.go b/pkg/wireguard/keys.go index 532f64e..fe21f98 100644 --- a/pkg/wireguard/keys.go +++ b/pkg/wireguard/keys.go @@ -183,20 +183,14 @@ type KeyPair struct { // 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 + case kp.PrivateKey.IsZero(): + // no private key return ErrInvalidPrivateKey - case pubLen == 0: + case kp.PublicKey.IsZero(): // 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