This commit is contained in:
ufm 2022-07-03 17:52:46 +03:00
parent aba03d590e
commit 68fbfc6f4b

View file

@ -2,14 +2,14 @@ package core
import (
"context"
"crypto/ed25519"
"crypto/aes"
"crypto/cipher"
"crypto/ed25519"
"crypto/rand"
"io"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
@ -63,9 +63,13 @@ func (c *Core) _init() error {
switch len(csecret) {
case 16, 24, 32: // Generate GCM
ch, err := aes.NewCipher([]byte(csecret))
if err != nil { return fmt.Errorf("aes.NewCipher: %w", err) }
if err != nil {
return fmt.Errorf("aes.NewCipher: %w", err)
}
gcm, err = cipher.NewGCM(ch)
if err != nil { return fmt.Errorf("cipher.NewGCM: %w", err) }
if err != nil {
return fmt.Errorf("cipher.NewGCM: %w", err)
}
default:
return fmt.Errorf("Secret for %s is incorrect length. Should be 16, 24 or 32 bytes", addr)
}
@ -74,7 +78,9 @@ func (c *Core) _init() error {
c.sgcm["0"] = gcm
} else {
saddr, err := hex.DecodeString(addr)
if err != nil { return err }
if err != nil {
return err
}
if len(saddr) != ed25519.PublicKeySize {
return fmt.Errorf("PublicKey '%s' has the wrong length", addr)
}
@ -239,13 +245,17 @@ func (c *Core) ReadFrom(p []byte) (n int, from net.Addr, err error) {
case typeSessionTraffic:
// This is what we want to handle here
gcm := c.getSecretForAddr(from)
if gcm != nil { continue }
if gcm != nil {
continue
}
bs = bs[1:n]
case typeSessionEncTraffic:
// Encoded traddic. Decode first
gcm := c.getSecretForAddr(from)
if gcm == nil { continue }
if gcm == nil {
continue
}
bs, err = gcm.Open(nil, bs[1:gcm.NonceSize()+1], bs[gcm.NonceSize()+1:n], nil)
if err != nil { // If we failed to decrypt the packet, we silently skip it.
err = nil