diff --git a/CLAUDE.md b/CLAUDE.md index 085ee9d..a3d621c 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -57,7 +57,7 @@ pkg/tap/ ## CLI flags Required: `-host`, `-user` -Optional: `-pass`, `-port` (443), `-hub` (DEFAULT), `-tap` (auto), `-mac`, `-plain-password`, `-insecure`, `-no-dhcp`, `-accept-default-gateway`, `-accept-static-routes`, `-accept-dns`, `-policy-route-table` (0=disabled), `-connmark`, `-reconnect-delay-secs` (5), `-keepalive-timeout-secs` (0=disabled) +Optional: `-pass`, `-port` (443), `-hub` (DEFAULT), `-tap` (auto), `-mac`, `-plain-password`, `-insecure`, `-server-cert` (pin a certificate file: exact match, no CA/hostname check), `-no-dhcp`, `-accept-default-gateway`, `-accept-static-routes`, `-accept-dns`, `-policy-route-table` (0=disabled), `-connmark`, `-reconnect-delay-secs` (5), `-keepalive-timeout-secs` (0=disabled) ## SoftEther protocol pitfalls diff --git a/cmd/softether-go/main.go b/cmd/softether-go/main.go index c4c25ce..90341d4 100644 --- a/cmd/softether-go/main.go +++ b/cmd/softether-go/main.go @@ -2,10 +2,12 @@ // // Usage: // -// softether-go -host -user -pass [-hub HUB] [-port 443] [-tap tap0] [-insecure] +// softether-go -host -user -pass [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert cert.pem] package main import ( + "crypto/x509" + "encoding/pem" "flag" "fmt" "log" @@ -30,6 +32,7 @@ func main() { tapName := flag.String("tap", "", "TAP interface name (empty = kernel-assigned)") macAddr := flag.String("mac", "", "TAP interface MAC address (e.g. 5E:3B:6F:63:A8:3E)") insecure := flag.Bool("insecure", false, "Skip TLS certificate verification") + serverCertFile := flag.String("server-cert", "", "Accept only this server certificate (PEM or DER file), e.g. a self-signed one") noDHCP := flag.Bool("no-dhcp", false, "Disable built-in DHCP client") reconnectDelay := flag.Int("reconnect-delay-secs", 5, "Delay between reconnection attempts in seconds") acceptDefaultGW := flag.Bool("accept-default-gateway", false, "Install DHCP-provided gateway as default route") @@ -42,10 +45,25 @@ func main() { flag.Parse() if *host == "" || *username == "" { - fmt.Fprintf(os.Stderr, "Usage: %s -host -user -pass [-hub HUB] [-port 443] [-tap tap0] [-insecure]\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "Usage: %s -host -user -pass [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert cert.pem]\n", os.Args[0]) os.Exit(1) } + var serverCert []byte + if *serverCertFile != "" { + b, err := os.ReadFile(*serverCertFile) + if err != nil { + log.Fatalf("server-cert: %v", err) + } + if blk, _ := pem.Decode(b); blk != nil { + b = blk.Bytes + } + if _, err := x509.ParseCertificate(b); err != nil { + log.Fatalf("server-cert %s: %v", *serverCertFile, err) + } + serverCert = b + } + cfg := client.Config{ Host: *host, Port: *port, @@ -54,6 +72,7 @@ func main() { Password: *password, PlainPassword: *plainPass, InsecureSkipVerify: *insecure, + ServerCert: serverCert, } serverIP := netcfg.ResolveHost(*host) diff --git a/pkg/client/client.go b/pkg/client/client.go index 6d735ce..13dca25 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -40,6 +40,7 @@ type Config struct { Password string // Password (hashed or sent plain depending on PlainPassword) PlainPassword bool // If true, send password as plaintext (AuthType 2) InsecureSkipVerify bool // Skip TLS certificate verification + ServerCert []byte // Pinned server certificate (DER); nil = CA and hostname verification } // Session represents an established SoftEther VPN session. @@ -60,7 +61,7 @@ func Connect(cfg Config) (*Session, error) { } // Step 1: TLS connection - conn, err := protocol.DialTLS(cfg.Host, cfg.Port, cfg.InsecureSkipVerify) + conn, err := protocol.DialTLS(cfg.Host, cfg.Port, cfg.InsecureSkipVerify, cfg.ServerCert) if err != nil { return nil, fmt.Errorf("connect: %w", err) } diff --git a/pkg/protocol/http.go b/pkg/protocol/http.go index 6f5dfa1..cf8c0f0 100644 --- a/pkg/protocol/http.go +++ b/pkg/protocol/http.go @@ -4,6 +4,8 @@ import ( "bufio" "bytes" "crypto/tls" + "crypto/x509" + "errors" "fmt" "io" "net" @@ -32,17 +34,32 @@ type Conn struct { // DialTLS establishes a TLS connection to a SoftEther server. // SoftEther servers listen on port 443 (or 5555, etc.) and accept TLS connections // that look like normal HTTPS traffic. -func DialTLS(host string, port int, insecureSkipVerify bool) (*Conn, error) { +// +// serverCert (DER) pins the server: the connection is accepted only if the server +// presents exactly this certificate, with no CA or hostname check — what the +// SoftEther client does with CheckServerCert + ServerCert, and the only way to +// verify a self-signed server certificate that names no host. +func DialTLS(host string, port int, insecureSkipVerify bool, serverCert []byte) (*Conn, error) { addr := net.JoinHostPort(host, strconv.Itoa(port)) raw, err := net.Dial("tcp", addr) if err != nil { return nil, fmt.Errorf("tcp dial: %w", err) } - tlsConn := tls.Client(raw, &tls.Config{ + cfg := &tls.Config{ ServerName: host, InsecureSkipVerify: insecureSkipVerify, - }) + } + if serverCert != nil { + cfg.InsecureSkipVerify = true // replaced by the pin check below + cfg.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { + if len(rawCerts) == 0 || !bytes.Equal(rawCerts[0], serverCert) { + return errors.New("server certificate does not match the pinned certificate") + } + return nil + } + } + tlsConn := tls.Client(raw, cfg) if err := tlsConn.Handshake(); err != nil { raw.Close()