mihomo/component/tls/config.go

142 lines
3.3 KiB
Go
Raw Normal View History

2022-07-10 20:44:24 +08:00
package tls
import (
"bytes"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/hex"
"errors"
2022-07-10 20:44:24 +08:00
"fmt"
"strings"
2022-07-10 20:44:24 +08:00
"sync"
xtls "github.com/xtls/go"
2022-07-10 20:44:24 +08:00
)
2023-03-27 22:27:59 +08:00
var trustCerts []*x509.Certificate
var certPool *x509.CertPool
var mutex sync.RWMutex
var errNotMacth error = errors.New("certificate fingerprints do not match")
2022-07-10 20:44:24 +08:00
2023-02-25 22:01:20 +08:00
func AddCertificate(certificate string) error {
mutex.Lock()
defer mutex.Unlock()
2023-02-25 22:01:20 +08:00
if certificate == "" {
return fmt.Errorf("certificate is empty")
}
2023-03-27 22:27:59 +08:00
if cert, err := x509.ParseCertificate([]byte(certificate)); err == nil {
trustCerts = append(trustCerts, cert)
return nil
} else {
2023-02-25 22:01:20 +08:00
return fmt.Errorf("add certificate failed")
}
}
2023-02-26 20:38:32 +08:00
func ResetCertificate() {
2023-02-25 22:01:20 +08:00
mutex.Lock()
defer mutex.Unlock()
2023-03-27 22:27:59 +08:00
trustCerts = nil
}
func getCertPool() *x509.CertPool {
if len(trustCerts) == 0 {
return nil
}
if certPool == nil {
mutex.Lock()
defer mutex.Unlock()
if certPool != nil {
return certPool
}
certPool, err := x509.SystemCertPool()
if err == nil {
for _, cert := range trustCerts {
certPool.AddCert(cert)
}
2023-03-27 22:27:59 +08:00
}
}
return certPool
}
func verifyFingerprint(fingerprint *[32]byte) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
return func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
// ssl pining
for i := range rawCerts {
rawCert := rawCerts[i]
cert, err := x509.ParseCertificate(rawCert)
if err == nil {
hash := sha256.Sum256(cert.Raw)
if bytes.Equal(fingerprint[:], hash[:]) {
return nil
}
2022-07-10 20:44:24 +08:00
}
}
return errNotMacth
}
2022-07-10 20:44:24 +08:00
}
func convertFingerprint(fingerprint string) (*[32]byte, error) {
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 {
return nil, err
2022-07-10 20:44:24 +08:00
}
if len(fpByte) != 32 {
return nil, fmt.Errorf("fingerprint string length error,need sha256 fingerprint")
2022-07-10 20:44:24 +08:00
}
return (*[32]byte)(fpByte), nil
2022-07-10 20:44:24 +08:00
}
func GetDefaultTLSConfig() *tls.Config {
return GetGlobalTLSConfig(nil)
}
2022-07-11 13:42:28 +08:00
// GetSpecifiedFingerprintTLSConfig specified fingerprint
func GetSpecifiedFingerprintTLSConfig(tlsConfig *tls.Config, fingerprint string) (*tls.Config, error) {
if fingerprintBytes, err := convertFingerprint(fingerprint); err != nil {
return nil, err
} else {
tlsConfig = GetGlobalTLSConfig(tlsConfig)
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
tlsConfig.InsecureSkipVerify = true
return tlsConfig, nil
}
2022-07-10 20:44:24 +08:00
}
func GetGlobalTLSConfig(tlsConfig *tls.Config) *tls.Config {
2023-03-27 22:27:59 +08:00
certPool := getCertPool()
2022-07-10 20:44:24 +08:00
if tlsConfig == nil {
2022-07-11 12:37:27 +08:00
return &tls.Config{
2023-03-27 22:27:59 +08:00
RootCAs: certPool,
2022-07-11 12:37:27 +08:00
}
2022-07-10 20:44:24 +08:00
}
2023-03-27 22:27:59 +08:00
tlsConfig.RootCAs = certPool
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 {
tlsConfig = GetGlobalXTLSConfig(tlsConfig)
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
tlsConfig.InsecureSkipVerify = true
return tlsConfig, nil
2022-07-11 13:42:28 +08:00
}
}
func GetGlobalXTLSConfig(tlsConfig *xtls.Config) *xtls.Config {
2023-03-27 22:27:59 +08:00
certPool := getCertPool()
2022-07-11 13:42:28 +08:00
if tlsConfig == nil {
return &xtls.Config{
2023-03-27 22:27:59 +08:00
RootCAs: certPool,
2022-07-11 13:42:28 +08:00
}
}
2023-03-27 22:27:59 +08:00
tlsConfig.RootCAs = certPool
2022-07-11 13:42:28 +08:00
return tlsConfig
}