2022-07-10 20:44:24 +08:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/hex"
|
2023-01-14 21:08:06 +08:00
|
|
|
"errors"
|
2022-07-10 20:44:24 +08:00
|
|
|
"fmt"
|
2023-01-14 21:08:06 +08:00
|
|
|
"strings"
|
2022-07-10 20:44:24 +08:00
|
|
|
"sync"
|
2023-01-14 21:08:06 +08:00
|
|
|
|
|
|
|
CN "github.com/Dreamacro/clash/common/net"
|
2023-01-13 09:55:01 +08:00
|
|
|
|
|
|
|
xtls "github.com/xtls/go"
|
2022-07-10 20:44:24 +08:00
|
|
|
)
|
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
var tlsCertificates = make([]tls.Certificate, 0)
|
2022-07-10 20:44:24 +08:00
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
var mutex sync.RWMutex
|
|
|
|
var errNotMacth error = errors.New("certificate fingerprints do not match")
|
2022-07-10 20:44:24 +08:00
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
func AddCertificate(privateKey, certificate string) error {
|
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
|
|
|
if cert, err := CN.ParseCert(certificate, privateKey); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
tlsCertificates = append(tlsCertificates, cert)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetCertificates() []tls.Certificate {
|
|
|
|
mutex.RLock()
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
return tlsCertificates
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyFingerprint(fingerprint *[32]byte) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
|
return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
|
|
|
|
// ssl pining
|
2022-07-10 21:56:33 +08:00
|
|
|
for i := range rawCerts {
|
|
|
|
rawCert := rawCerts[i]
|
|
|
|
cert, err := x509.ParseCertificate(rawCert)
|
|
|
|
if err == nil {
|
2023-01-14 21:08:06 +08:00
|
|
|
hash := sha256.Sum256(cert.Raw)
|
|
|
|
if bytes.Equal(fingerprint[:], hash[:]) {
|
2022-07-10 21:56:33 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 21:08:06 +08:00
|
|
|
return errNotMacth
|
2022-07-10 21:56:33 +08:00
|
|
|
}
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
|
|
|
|
2022-07-10 21:56:33 +08:00
|
|
|
func convertFingerprint(fingerprint string) (*[32]byte, error) {
|
2023-01-14 21:08:06 +08:00
|
|
|
fingerprint = strings.TrimSpace(strings.Replace(fingerprint, ":", "", -1))
|
2022-07-11 13:44:27 +08:00
|
|
|
fpByte, err := hex.DecodeString(fingerprint)
|
2022-07-10 20:44:24 +08:00
|
|
|
if err != nil {
|
2022-07-10 21:56:33 +08:00
|
|
|
return nil, err
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fpByte) != 32 {
|
2022-07-10 21:56:33 +08:00
|
|
|
return nil, fmt.Errorf("fingerprint string length error,need sha25 fingerprint")
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
2022-07-10 21:56:33 +08:00
|
|
|
return (*[32]byte)(fpByte), nil
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetDefaultTLSConfig() *tls.Config {
|
2023-01-14 21:08:06 +08:00
|
|
|
return GetGlobalTLSConfig(nil)
|
2022-07-10 21:56:33 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:42:28 +08:00
|
|
|
// GetSpecifiedFingerprintTLSConfig specified fingerprint
|
|
|
|
func GetSpecifiedFingerprintTLSConfig(tlsConfig *tls.Config, fingerprint string) (*tls.Config, error) {
|
2022-07-10 21:56:33 +08:00
|
|
|
if fingerprintBytes, err := convertFingerprint(fingerprint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
2023-01-14 21:08:06 +08:00
|
|
|
tlsConfig = GetGlobalTLSConfig(tlsConfig)
|
|
|
|
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
return tlsConfig, nil
|
2022-07-10 21:56:33 +08:00
|
|
|
}
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
func GetGlobalTLSConfig(tlsConfig *tls.Config) *tls.Config {
|
2022-07-10 20:44:24 +08:00
|
|
|
if tlsConfig == nil {
|
2022-07-11 12:37:27 +08:00
|
|
|
return &tls.Config{
|
2023-01-14 21:08:06 +08:00
|
|
|
Certificates: tlsCertificates,
|
2022-07-11 12:37:27 +08:00
|
|
|
}
|
2022-07-10 20:44:24 +08:00
|
|
|
}
|
2023-01-14 21:08:06 +08:00
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCertificates...)
|
2022-07-10 20:44:24 +08:00
|
|
|
return tlsConfig
|
|
|
|
}
|
2022-07-11 13:42:28 +08:00
|
|
|
|
|
|
|
// GetSpecifiedFingerprintXTLSConfig specified fingerprint
|
|
|
|
func GetSpecifiedFingerprintXTLSConfig(tlsConfig *xtls.Config, fingerprint string) (*xtls.Config, error) {
|
|
|
|
if fingerprintBytes, err := convertFingerprint(fingerprint); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
2023-01-14 21:08:06 +08:00
|
|
|
tlsConfig=GetGlobalXTLSConfig(tlsConfig)
|
|
|
|
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
|
2022-07-11 13:42:28 +08:00
|
|
|
tlsConfig.InsecureSkipVerify = true
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
func GetGlobalXTLSConfig(tlsConfig *xtls.Config) *xtls.Config {
|
|
|
|
xtlsCerts := make([]xtls.Certificate, len(tlsCertificates))
|
|
|
|
for _, cert := range tlsCertificates {
|
|
|
|
tlsSsaList := make([]xtls.SignatureScheme, len(cert.SupportedSignatureAlgorithms))
|
|
|
|
for _, ssa := range cert.SupportedSignatureAlgorithms {
|
|
|
|
tlsSsa := xtls.SignatureScheme(ssa)
|
|
|
|
tlsSsaList = append(tlsSsaList, tlsSsa)
|
|
|
|
}
|
|
|
|
xtlsCert := xtls.Certificate{
|
|
|
|
Certificate: cert.Certificate,
|
|
|
|
PrivateKey: cert.PrivateKey,
|
|
|
|
OCSPStaple: cert.OCSPStaple,
|
|
|
|
SignedCertificateTimestamps: cert.SignedCertificateTimestamps,
|
|
|
|
Leaf: cert.Leaf,
|
|
|
|
SupportedSignatureAlgorithms: tlsSsaList,
|
|
|
|
}
|
|
|
|
xtlsCerts = append(xtlsCerts, xtlsCert)
|
|
|
|
}
|
2022-07-11 13:42:28 +08:00
|
|
|
if tlsConfig == nil {
|
|
|
|
return &xtls.Config{
|
2023-01-14 21:08:06 +08:00
|
|
|
Certificates: xtlsCerts,
|
2022-07-11 13:42:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:08:06 +08:00
|
|
|
tlsConfig.Certificates = xtlsCerts
|
2022-07-11 13:42:28 +08:00
|
|
|
return tlsConfig
|
|
|
|
}
|