client: pin the server certificate by SHA-256, not by file

-server-cert-sha256 <hex> replaces -server-cert <file>: the connection is
accepted only if the SHA-256 of the server's certificate (DER) matches,
colons optional, case-insensitive. No certificate or key file to handle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Git Sagar 2026-10-28 15:36:01 -03:00
parent bb92a9b954
commit 94716ac989
4 changed files with 26 additions and 28 deletions

View file

@ -57,7 +57,7 @@ pkg/tap/
## CLI flags ## CLI flags
Required: `-host`, `-user` Required: `-host`, `-user`
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) Optional: `-pass`, `-port` (443), `-hub` (DEFAULT), `-tap` (auto), `-mac`, `-plain-password`, `-insecure`, `-server-cert-sha256` (pin the server certificate by its SHA-256: 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 ## SoftEther protocol pitfalls

View file

@ -2,18 +2,18 @@
// //
// Usage: // Usage:
// //
// softether-go -host <server> -user <username> -pass <password> [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert cert.pem] // softether-go -host <server> -user <username> -pass <password> [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert-sha256 HEX]
package main package main
import ( import (
"crypto/x509" "encoding/hex"
"encoding/pem"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net" "net"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
@ -32,7 +32,7 @@ func main() {
tapName := flag.String("tap", "", "TAP interface name (empty = kernel-assigned)") 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)") 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") 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") serverCertSHA256 := flag.String("server-cert-sha256", "", "Accept only the server certificate with this SHA-256 (hex, colons optional), e.g. a self-signed one")
noDHCP := flag.Bool("no-dhcp", false, "Disable built-in DHCP client") noDHCP := flag.Bool("no-dhcp", false, "Disable built-in DHCP client")
reconnectDelay := flag.Int("reconnect-delay-secs", 5, "Delay between reconnection attempts in seconds") 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") acceptDefaultGW := flag.Bool("accept-default-gateway", false, "Install DHCP-provided gateway as default route")
@ -45,23 +45,17 @@ func main() {
flag.Parse() flag.Parse()
if *host == "" || *username == "" { if *host == "" || *username == "" {
fmt.Fprintf(os.Stderr, "Usage: %s -host <server> -user <username> -pass <password> [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert cert.pem]\n", os.Args[0]) fmt.Fprintf(os.Stderr, "Usage: %s -host <server> -user <username> -pass <password> [-hub HUB] [-port 443] [-tap tap0] [-insecure | -server-cert-sha256 HEX]\n", os.Args[0])
os.Exit(1) os.Exit(1)
} }
var serverCert []byte var certPin []byte
if *serverCertFile != "" { if *serverCertSHA256 != "" {
b, err := os.ReadFile(*serverCertFile) b, err := hex.DecodeString(strings.ReplaceAll(*serverCertSHA256, ":", ""))
if err != nil { if err != nil || len(b) != 32 {
log.Fatalf("server-cert: %v", err) log.Fatalf("server-cert-sha256: want 32 bytes of hex (64 digits, colons optional), got %q", *serverCertSHA256)
} }
if blk, _ := pem.Decode(b); blk != nil { certPin = b
b = blk.Bytes
}
if _, err := x509.ParseCertificate(b); err != nil {
log.Fatalf("server-cert %s: %v", *serverCertFile, err)
}
serverCert = b
} }
cfg := client.Config{ cfg := client.Config{
@ -72,7 +66,7 @@ func main() {
Password: *password, Password: *password,
PlainPassword: *plainPass, PlainPassword: *plainPass,
InsecureSkipVerify: *insecure, InsecureSkipVerify: *insecure,
ServerCert: serverCert, ServerCertSHA256: certPin,
} }
serverIP := netcfg.ResolveHost(*host) serverIP := netcfg.ResolveHost(*host)

View file

@ -40,7 +40,7 @@ type Config struct {
Password string // Password (hashed or sent plain depending on PlainPassword) Password string // Password (hashed or sent plain depending on PlainPassword)
PlainPassword bool // If true, send password as plaintext (AuthType 2) PlainPassword bool // If true, send password as plaintext (AuthType 2)
InsecureSkipVerify bool // Skip TLS certificate verification InsecureSkipVerify bool // Skip TLS certificate verification
ServerCert []byte // Pinned server certificate (DER); nil = CA and hostname verification ServerCertSHA256 []byte // Pinned SHA-256 of the server certificate (DER); nil = CA and hostname verification
} }
// Session represents an established SoftEther VPN session. // Session represents an established SoftEther VPN session.
@ -61,7 +61,7 @@ func Connect(cfg Config) (*Session, error) {
} }
// Step 1: TLS connection // Step 1: TLS connection
conn, err := protocol.DialTLS(cfg.Host, cfg.Port, cfg.InsecureSkipVerify, cfg.ServerCert) conn, err := protocol.DialTLS(cfg.Host, cfg.Port, cfg.InsecureSkipVerify, cfg.ServerCertSHA256)
if err != nil { if err != nil {
return nil, fmt.Errorf("connect: %w", err) return nil, fmt.Errorf("connect: %w", err)
} }

View file

@ -3,6 +3,7 @@ package protocol
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"crypto/sha256"
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"errors" "errors"
@ -35,11 +36,11 @@ type Conn struct {
// SoftEther servers listen on port 443 (or 5555, etc.) and accept TLS connections // SoftEther servers listen on port 443 (or 5555, etc.) and accept TLS connections
// that look like normal HTTPS traffic. // that look like normal HTTPS traffic.
// //
// serverCert (DER) pins the server: the connection is accepted only if the server // serverCertSHA256 pins the server: the connection is accepted only if the SHA-256
// presents exactly this certificate, with no CA or hostname check — what the // of the certificate it presents (DER) is this, with no CA or hostname check — what
// SoftEther client does with CheckServerCert + ServerCert, and the only way to // the SoftEther client does with CheckServerCert + ServerCert, and the only way to
// verify a self-signed server certificate that names no host. // verify a self-signed server certificate that names no host.
func DialTLS(host string, port int, insecureSkipVerify bool, serverCert []byte) (*Conn, error) { func DialTLS(host string, port int, insecureSkipVerify bool, serverCertSHA256 []byte) (*Conn, error) {
addr := net.JoinHostPort(host, strconv.Itoa(port)) addr := net.JoinHostPort(host, strconv.Itoa(port))
raw, err := net.Dial("tcp", addr) raw, err := net.Dial("tcp", addr)
if err != nil { if err != nil {
@ -50,11 +51,14 @@ func DialTLS(host string, port int, insecureSkipVerify bool, serverCert []byte)
ServerName: host, ServerName: host,
InsecureSkipVerify: insecureSkipVerify, InsecureSkipVerify: insecureSkipVerify,
} }
if serverCert != nil { if serverCertSHA256 != nil {
cfg.InsecureSkipVerify = true // replaced by the pin check below cfg.InsecureSkipVerify = true // replaced by the pin check below
cfg.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error { cfg.VerifyPeerCertificate = func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
if len(rawCerts) == 0 || !bytes.Equal(rawCerts[0], serverCert) { if len(rawCerts) == 0 {
return errors.New("server certificate does not match the pinned certificate") return errors.New("server sent no certificate")
}
if sum := sha256.Sum256(rawCerts[0]); !bytes.Equal(sum[:], serverCertSHA256) {
return fmt.Errorf("server certificate sha256 %X does not match the pinned one", sum)
} }
return nil return nil
} }