2021-01-16 20:41:39 +00:00
|
|
|
package ak
|
2020-09-02 22:04:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
// GenerateSelfSignedCert Generate a self-signed TLS Certificate, to be used as fallback
|
|
|
|
func GenerateSelfSignedCert() (tls.Certificate, error) {
|
2020-09-02 22:04:12 +00:00
|
|
|
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to generate private key: %v", err)
|
|
|
|
return tls.Certificate{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
keyUsage := x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment
|
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
notAfter := notBefore.Add(365 * 24 * time.Hour)
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to generate serial number: %v", err)
|
|
|
|
return tls.Certificate{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
2020-12-05 21:08:42 +00:00
|
|
|
Organization: []string{"authentik"},
|
|
|
|
CommonName: "authentik Proxy default certificate",
|
2020-09-02 22:04:12 +00:00
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: keyUsage,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
template.DNSNames = []string{"*"}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
|
|
|
if err != nil {
|
|
|
|
log.Warning(err)
|
|
|
|
}
|
|
|
|
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
log.Warning(err)
|
|
|
|
}
|
|
|
|
privPemByes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
|
|
|
|
return tls.X509KeyPair(pemBytes, privPemByes)
|
|
|
|
}
|