mirror of https://github.com/go-gost/gost.git
107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"math/big"
|
|
"time"
|
|
|
|
tls_util "github.com/go-gost/gost/pkg/common/util/tls"
|
|
"github.com/go-gost/gost/pkg/config"
|
|
)
|
|
|
|
func loadServerTLSConfig(cfg *config.TLSConfig) (*tls.Config, error) {
|
|
return tls_util.LoadServerConfig(cfg.Cert, cfg.Key, cfg.CA)
|
|
}
|
|
|
|
func loadClientTLSConfig(cfg *config.TLSConfig) (*tls.Config, error) {
|
|
return tls_util.LoadClientConfig(cfg.Cert, cfg.Key, cfg.CA, cfg.Secure, cfg.ServerName)
|
|
}
|
|
|
|
func buildDefaultTLSConfig(cfg *config.TLSConfig) {
|
|
if cfg == nil {
|
|
cfg = &config.TLSConfig{
|
|
Cert: "cert.pem",
|
|
Key: "key.pem",
|
|
}
|
|
}
|
|
|
|
tlsConfig, err := loadConfig(cfg.Cert, cfg.Key)
|
|
if err != nil {
|
|
// generate random self-signed certificate.
|
|
cert, err := genCertificate()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
tlsConfig = &tls.Config{
|
|
Certificates: []tls.Certificate{cert},
|
|
}
|
|
log.Warn("load TLS certificate files failed, use random generated certificate")
|
|
} else {
|
|
log.Info("load TLS certificate files OK")
|
|
}
|
|
tls_util.DefaultConfig = tlsConfig
|
|
}
|
|
|
|
func loadConfig(certFile, keyFile string) (*tls.Config, error) {
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := &tls.Config{
|
|
Certificates: []tls.Certificate{cert},
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func genCertificate() (cert tls.Certificate, err error) {
|
|
rawCert, rawKey, err := generateKeyPair()
|
|
if err != nil {
|
|
return
|
|
}
|
|
return tls.X509KeyPair(rawCert, rawKey)
|
|
}
|
|
|
|
func generateKeyPair() (rawCert, rawKey []byte, err error) {
|
|
// Create private key and self-signed certificate
|
|
// Adapted from https://golang.org/src/crypto/tls/generate_cert.go
|
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
if err != nil {
|
|
return
|
|
}
|
|
validFor := time.Hour * 24 * 365 * 10 // ten years
|
|
notBefore := time.Now()
|
|
notAfter := notBefore.Add(validFor)
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
serialNumber, _ := rand.Int(rand.Reader, serialNumberLimit)
|
|
template := x509.Certificate{
|
|
SerialNumber: serialNumber,
|
|
Subject: pkix.Name{
|
|
Organization: []string{"gost"},
|
|
CommonName: "gost.run",
|
|
},
|
|
NotBefore: notBefore,
|
|
NotAfter: notAfter,
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
BasicConstraintsValid: true,
|
|
}
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
rawCert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
rawKey = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
|
|
|
return
|
|
}
|