github.com/metacubex/mihomo@v1.18.5/component/tls/reality.go (about)

     1  package tls
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"crypto/aes"
     7  	"crypto/cipher"
     8  	"crypto/ecdh"
     9  	"crypto/ed25519"
    10  	"crypto/hmac"
    11  	"crypto/sha256"
    12  	"crypto/sha512"
    13  	"crypto/tls"
    14  	"crypto/x509"
    15  	"encoding/binary"
    16  	"errors"
    17  	"net"
    18  	"net/http"
    19  	"reflect"
    20  	"strings"
    21  	"time"
    22  	"unsafe"
    23  
    24  	"github.com/metacubex/mihomo/common/utils"
    25  	"github.com/metacubex/mihomo/log"
    26  	"github.com/metacubex/mihomo/ntp"
    27  
    28  	utls "github.com/sagernet/utls"
    29  	"github.com/zhangyunhao116/fastrand"
    30  	"golang.org/x/crypto/chacha20poly1305"
    31  	"golang.org/x/crypto/hkdf"
    32  	"golang.org/x/net/http2"
    33  )
    34  
    35  const RealityMaxShortIDLen = 8
    36  
    37  type RealityConfig struct {
    38  	PublicKey *ecdh.PublicKey
    39  	ShortID   [RealityMaxShortIDLen]byte
    40  }
    41  
    42  //go:linkname aesgcmPreferred crypto/tls.aesgcmPreferred
    43  func aesgcmPreferred(ciphers []uint16) bool
    44  
    45  func GetRealityConn(ctx context.Context, conn net.Conn, ClientFingerprint string, tlsConfig *tls.Config, realityConfig *RealityConfig) (net.Conn, error) {
    46  	retry := 0
    47  	for fingerprint, exists := GetFingerprint(ClientFingerprint); exists; retry++ {
    48  		verifier := &realityVerifier{
    49  			serverName: tlsConfig.ServerName,
    50  		}
    51  		uConfig := &utls.Config{
    52  			ServerName:             tlsConfig.ServerName,
    53  			InsecureSkipVerify:     true,
    54  			SessionTicketsDisabled: true,
    55  			VerifyPeerCertificate:  verifier.VerifyPeerCertificate,
    56  		}
    57  		clientID := utls.ClientHelloID{
    58  			Client:  fingerprint.Client,
    59  			Version: fingerprint.Version,
    60  			Seed:    fingerprint.Seed,
    61  		}
    62  		uConn := utls.UClient(conn, uConfig, clientID)
    63  		verifier.UConn = uConn
    64  		err := uConn.BuildHandshakeState()
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  
    69  		hello := uConn.HandshakeState.Hello
    70  		rawSessionID := hello.Raw[39 : 39+32] // the location of session ID
    71  		for i := range rawSessionID {         // https://github.com/golang/go/issues/5373
    72  			rawSessionID[i] = 0
    73  		}
    74  
    75  		binary.BigEndian.PutUint64(hello.SessionId, uint64(ntp.Now().Unix()))
    76  
    77  		copy(hello.SessionId[8:], realityConfig.ShortID[:])
    78  		hello.SessionId[0] = 1
    79  		hello.SessionId[1] = 8
    80  		hello.SessionId[2] = 2
    81  
    82  		//log.Debugln("REALITY hello.sessionId[:16]: %v", hello.SessionId[:16])
    83  
    84  		ecdheKey := uConn.HandshakeState.State13.EcdheKey
    85  		if ecdheKey == nil {
    86  			// WTF???
    87  			if retry > 2 {
    88  				return nil, errors.New("nil ecdheKey")
    89  			}
    90  			continue // retry
    91  		}
    92  		authKey, err := ecdheKey.ECDH(realityConfig.PublicKey)
    93  		if err != nil {
    94  			return nil, err
    95  		}
    96  		if authKey == nil {
    97  			return nil, errors.New("nil auth_key")
    98  		}
    99  		verifier.authKey = authKey
   100  		_, err = hkdf.New(sha256.New, authKey, hello.Random[:20], []byte("REALITY")).Read(authKey)
   101  		if err != nil {
   102  			return nil, err
   103  		}
   104  		var aeadCipher cipher.AEAD
   105  		if aesgcmPreferred(hello.CipherSuites) {
   106  			aesBlock, _ := aes.NewCipher(authKey)
   107  			aeadCipher, _ = cipher.NewGCM(aesBlock)
   108  		} else {
   109  			aeadCipher, _ = chacha20poly1305.New(authKey)
   110  		}
   111  		aeadCipher.Seal(hello.SessionId[:0], hello.Random[20:], hello.SessionId[:16], hello.Raw)
   112  		copy(hello.Raw[39:], hello.SessionId)
   113  		//log.Debugln("REALITY hello.sessionId: %v", hello.SessionId)
   114  		//log.Debugln("REALITY uConn.AuthKey: %v", authKey)
   115  
   116  		err = uConn.HandshakeContext(ctx)
   117  		if err != nil {
   118  			return nil, err
   119  		}
   120  
   121  		log.Debugln("REALITY Authentication: %v, AEAD: %T", verifier.verified, aeadCipher)
   122  
   123  		if !verifier.verified {
   124  			go realityClientFallback(uConn, uConfig.ServerName, clientID)
   125  			return nil, errors.New("REALITY authentication failed")
   126  		}
   127  
   128  		return uConn, nil
   129  	}
   130  	return nil, errors.New("unknown uTLS fingerprint")
   131  }
   132  
   133  func realityClientFallback(uConn net.Conn, serverName string, fingerprint utls.ClientHelloID) {
   134  	defer uConn.Close()
   135  	client := http.Client{
   136  		Transport: &http2.Transport{
   137  			DialTLSContext: func(ctx context.Context, network, addr string, config *tls.Config) (net.Conn, error) {
   138  				return uConn, nil
   139  			},
   140  		},
   141  	}
   142  	request, err := http.NewRequest("GET", "https://"+serverName, nil)
   143  	if err != nil {
   144  		return
   145  	}
   146  	request.Header.Set("User-Agent", fingerprint.Client)
   147  	request.AddCookie(&http.Cookie{Name: "padding", Value: strings.Repeat("0", fastrand.Intn(32)+30)})
   148  	response, err := client.Do(request)
   149  	if err != nil {
   150  		return
   151  	}
   152  	//_, _ = io.Copy(io.Discard, response.Body)
   153  	time.Sleep(time.Duration(5+fastrand.Int63n(10)) * time.Second)
   154  	response.Body.Close()
   155  	client.CloseIdleConnections()
   156  }
   157  
   158  type realityVerifier struct {
   159  	*utls.UConn
   160  	serverName string
   161  	authKey    []byte
   162  	verified   bool
   163  }
   164  
   165  var pOffset = utils.MustOK(reflect.TypeOf((*utls.Conn)(nil)).Elem().FieldByName("peerCertificates")).Offset
   166  
   167  func (c *realityVerifier) VerifyPeerCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
   168  	//p, _ := reflect.TypeOf(c.Conn).Elem().FieldByName("peerCertificates")
   169  	certs := *(*[]*x509.Certificate)(unsafe.Add(unsafe.Pointer(c.Conn), pOffset))
   170  	if pub, ok := certs[0].PublicKey.(ed25519.PublicKey); ok {
   171  		h := hmac.New(sha512.New, c.authKey)
   172  		h.Write(pub)
   173  		if bytes.Equal(h.Sum(nil), certs[0].Signature) {
   174  			c.verified = true
   175  			return nil
   176  		}
   177  	}
   178  	opts := x509.VerifyOptions{
   179  		DNSName:       c.serverName,
   180  		Intermediates: x509.NewCertPool(),
   181  	}
   182  	for _, cert := range certs[1:] {
   183  		opts.Intermediates.AddCert(cert)
   184  	}
   185  	if _, err := certs[0].Verify(opts); err != nil {
   186  		return err
   187  	}
   188  	return nil
   189  }