97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package tls
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"crypto/rsa"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"io/ioutil"
|
|
"math/big"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
// DefaultConfig is a default TLS config for global use.
|
|
DefaultConfig *tls.Config
|
|
)
|
|
|
|
// LoadTLSConfig loads the certificate from cert & key files and optional client CA file.
|
|
func LoadTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error) {
|
|
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
if pool, _ := loadCA(caFile); pool != nil {
|
|
cfg.ClientCAs = pool
|
|
cfg.ClientAuth = tls.RequireAndVerifyClientCert
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
func loadCA(caFile string) (cp *x509.CertPool, err error) {
|
|
if caFile == "" {
|
|
return
|
|
}
|
|
cp = x509.NewCertPool()
|
|
data, err := ioutil.ReadFile(caFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !cp.AppendCertsFromPEM(data) {
|
|
return nil, errors.New("AppendCertsFromPEM failed")
|
|
}
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|