2023-09-22 06:45:34 +00:00
|
|
|
package ca
|
2022-07-10 12:44:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/hex"
|
2023-01-14 13:08:06 +00:00
|
|
|
"errors"
|
2022-07-10 12:44:24 +00:00
|
|
|
"fmt"
|
2023-09-22 06:45:34 +00:00
|
|
|
"os"
|
2023-01-14 13:08:06 +00:00
|
|
|
"strings"
|
2022-07-10 12:44:24 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2023-03-27 14:27:59 +00:00
|
|
|
var trustCerts []*x509.Certificate
|
2023-09-22 06:45:34 +00:00
|
|
|
var globalCertPool *x509.CertPool
|
2023-01-14 13:08:06 +00:00
|
|
|
var mutex sync.RWMutex
|
2023-06-04 03:51:30 +00:00
|
|
|
var errNotMatch = errors.New("certificate fingerprints do not match")
|
2022-07-10 12:44:24 +00:00
|
|
|
|
2023-02-25 14:01:20 +00:00
|
|
|
func AddCertificate(certificate string) error {
|
2023-01-14 13:08:06 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-02-25 14:01:20 +00:00
|
|
|
if certificate == "" {
|
|
|
|
return fmt.Errorf("certificate is empty")
|
|
|
|
}
|
2023-03-27 14:27:59 +00:00
|
|
|
if cert, err := x509.ParseCertificate([]byte(certificate)); err == nil {
|
|
|
|
trustCerts = append(trustCerts, cert)
|
|
|
|
return nil
|
|
|
|
} else {
|
2023-02-25 14:01:20 +00:00
|
|
|
return fmt.Errorf("add certificate failed")
|
2023-01-14 13:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-13 16:21:59 +00:00
|
|
|
func initializeCertPool() {
|
|
|
|
var err error
|
2023-09-22 06:45:34 +00:00
|
|
|
globalCertPool, err = x509.SystemCertPool()
|
2023-05-13 16:21:59 +00:00
|
|
|
if err != nil {
|
2023-09-22 06:45:34 +00:00
|
|
|
globalCertPool = x509.NewCertPool()
|
2023-05-13 16:21:59 +00:00
|
|
|
}
|
|
|
|
for _, cert := range trustCerts {
|
2023-09-22 06:45:34 +00:00
|
|
|
globalCertPool.AddCert(cert)
|
2023-05-13 16:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 12:38:32 +00:00
|
|
|
func ResetCertificate() {
|
2023-02-25 14:01:20 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-03-27 14:27:59 +00:00
|
|
|
trustCerts = nil
|
2023-05-13 16:21:59 +00:00
|
|
|
initializeCertPool()
|
2023-03-27 14:27:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getCertPool() *x509.CertPool {
|
2023-04-01 03:53:39 +00:00
|
|
|
if len(trustCerts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-22 06:45:34 +00:00
|
|
|
if globalCertPool == nil {
|
2023-04-01 03:53:39 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2023-09-22 06:45:34 +00:00
|
|
|
if globalCertPool != nil {
|
|
|
|
return globalCertPool
|
2023-04-01 03:53:39 +00:00
|
|
|
}
|
2023-05-13 16:21:59 +00:00
|
|
|
initializeCertPool()
|
2023-03-27 14:27:59 +00:00
|
|
|
}
|
2023-09-22 06:45:34 +00:00
|
|
|
return globalCertPool
|
2023-01-14 13:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 13:56:33 +00:00
|
|
|
for i := range rawCerts {
|
|
|
|
rawCert := rawCerts[i]
|
|
|
|
cert, err := x509.ParseCertificate(rawCert)
|
|
|
|
if err == nil {
|
2023-01-14 13:08:06 +00:00
|
|
|
hash := sha256.Sum256(cert.Raw)
|
|
|
|
if bytes.Equal(fingerprint[:], hash[:]) {
|
2022-07-10 13:56:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-04 03:51:30 +00:00
|
|
|
return errNotMatch
|
2022-07-10 13:56:33 +00:00
|
|
|
}
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 13:56:33 +00:00
|
|
|
func convertFingerprint(fingerprint string) (*[32]byte, error) {
|
2023-01-14 13:08:06 +00:00
|
|
|
fingerprint = strings.TrimSpace(strings.Replace(fingerprint, ":", "", -1))
|
2022-07-11 05:44:27 +00:00
|
|
|
fpByte, err := hex.DecodeString(fingerprint)
|
2022-07-10 12:44:24 +00:00
|
|
|
if err != nil {
|
2022-07-10 13:56:33 +00:00
|
|
|
return nil, err
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(fpByte) != 32 {
|
2023-01-31 07:26:18 +00:00
|
|
|
return nil, fmt.Errorf("fingerprint string length error,need sha256 fingerprint")
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
2022-07-10 13:56:33 +00:00
|
|
|
return (*[32]byte)(fpByte), nil
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 06:45:34 +00:00
|
|
|
// GetTLSConfig specified fingerprint, customCA and customCAString
|
|
|
|
func GetTLSConfig(tlsConfig *tls.Config, fingerprint string, customCA string, customCAString string) (*tls.Config, error) {
|
|
|
|
if tlsConfig == nil {
|
|
|
|
tlsConfig = &tls.Config{}
|
|
|
|
}
|
|
|
|
var certificate []byte
|
|
|
|
var err error
|
|
|
|
if len(customCA) > 0 {
|
|
|
|
certificate, err = os.ReadFile(customCA)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("load ca error: %w", err)
|
|
|
|
}
|
|
|
|
} else if customCAString != "" {
|
|
|
|
certificate = []byte(customCAString)
|
|
|
|
}
|
|
|
|
if len(certificate) > 0 {
|
|
|
|
certPool := x509.NewCertPool()
|
|
|
|
if !certPool.AppendCertsFromPEM(certificate) {
|
|
|
|
return nil, fmt.Errorf("failed to parse certificate:\n\n %s", certificate)
|
|
|
|
}
|
|
|
|
tlsConfig.RootCAs = certPool
|
2022-07-10 13:56:33 +00:00
|
|
|
} else {
|
2023-09-22 06:45:34 +00:00
|
|
|
tlsConfig.RootCAs = getCertPool()
|
|
|
|
}
|
|
|
|
if len(fingerprint) > 0 {
|
|
|
|
var fingerprintBytes *[32]byte
|
|
|
|
fingerprintBytes, err = convertFingerprint(fingerprint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-14 13:08:06 +00:00
|
|
|
tlsConfig = GetGlobalTLSConfig(tlsConfig)
|
|
|
|
tlsConfig.VerifyPeerCertificate = verifyFingerprint(fingerprintBytes)
|
|
|
|
tlsConfig.InsecureSkipVerify = true
|
2022-07-10 13:56:33 +00:00
|
|
|
}
|
2023-09-22 06:45:34 +00:00
|
|
|
return tlsConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSpecifiedFingerprintTLSConfig specified fingerprint
|
|
|
|
func GetSpecifiedFingerprintTLSConfig(tlsConfig *tls.Config, fingerprint string) (*tls.Config, error) {
|
|
|
|
return GetTLSConfig(tlsConfig, fingerprint, "", "")
|
2022-07-10 12:44:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 13:08:06 +00:00
|
|
|
func GetGlobalTLSConfig(tlsConfig *tls.Config) *tls.Config {
|
2023-09-22 06:45:34 +00:00
|
|
|
tlsConfig, _ = GetTLSConfig(tlsConfig, "", "", "")
|
2022-07-10 12:44:24 +00:00
|
|
|
return tlsConfig
|
|
|
|
}
|