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:
parent
bb92a9b954
commit
94716ac989
4 changed files with 26 additions and 28 deletions
|
|
@ -3,6 +3,7 @@ package protocol
|
|||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
|
|
@ -35,11 +36,11 @@ type Conn struct {
|
|||
// SoftEther servers listen on port 443 (or 5555, etc.) and accept TLS connections
|
||||
// that look like normal HTTPS traffic.
|
||||
//
|
||||
// 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
|
||||
// serverCertSHA256 pins the server: the connection is accepted only if the SHA-256
|
||||
// of the certificate it presents (DER) is this, 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) {
|
||||
func DialTLS(host string, port int, insecureSkipVerify bool, serverCertSHA256 []byte) (*Conn, error) {
|
||||
addr := net.JoinHostPort(host, strconv.Itoa(port))
|
||||
raw, err := net.Dial("tcp", addr)
|
||||
if err != nil {
|
||||
|
|
@ -50,11 +51,14 @@ func DialTLS(host string, port int, insecureSkipVerify bool, serverCert []byte)
|
|||
ServerName: host,
|
||||
InsecureSkipVerify: insecureSkipVerify,
|
||||
}
|
||||
if serverCert != nil {
|
||||
if serverCertSHA256 != 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")
|
||||
if len(rawCerts) == 0 {
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue