2020-03-01 13:08:09 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/elliptic"
|
2021-08-03 09:55:21 +00:00
|
|
|
"fmt"
|
2020-03-01 13:08:09 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2021-08-03 09:55:21 +00:00
|
|
|
"path/filepath"
|
2020-03-01 13:08:09 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
2022-04-05 21:57:23 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2020-03-01 13:08:09 +00:00
|
|
|
)
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
// NewCertificatesCmd returns a new Certificates Cmd.
|
|
|
|
func NewCertificatesCmd() (cmd *cobra.Command) {
|
|
|
|
cmd = &cobra.Command{
|
|
|
|
Use: "certificates",
|
|
|
|
Short: "Commands related to certificate generation",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
}
|
2020-03-01 13:08:09 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
cmd.PersistentFlags().StringSlice("host", []string{}, "Comma-separated hostnames and IPs to generate a certificate for")
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
err := cmd.MarkPersistentFlagRequired("host")
|
2020-03-01 13:08:09 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
cmd.AddCommand(newCertificatesGenerateCmd())
|
2020-03-01 13:08:09 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
return cmd
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
func newCertificatesGenerateCmd() (cmd *cobra.Command) {
|
|
|
|
cmd = &cobra.Command{
|
|
|
|
Use: "generate",
|
|
|
|
Short: "Generate a self-signed certificate",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: cmdCertificatesGenerateRun,
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
cmd.Flags().String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011")
|
|
|
|
cmd.Flags().Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
|
|
|
|
cmd.Flags().Bool("ca", false, "Whether this cert should be its own Certificate Authority")
|
|
|
|
cmd.Flags().Int("rsa-bits", 2048, "Size of RSA key to generate. Ignored if --ecdsa-curve is set")
|
|
|
|
cmd.Flags().String("ecdsa-curve", "", "ECDSA curve to use to generate a key. Valid values are P224, P256 (recommended), P384, P521")
|
|
|
|
cmd.Flags().Bool("ed25519", false, "Generate an Ed25519 key")
|
|
|
|
cmd.Flags().String("dir", "", "Target directory where the certificate and keys will be stored")
|
|
|
|
|
|
|
|
return cmd
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
func cmdCertificatesGenerateRun(cmd *cobra.Command, _ []string) {
|
2020-03-01 13:08:09 +00:00
|
|
|
// implementation retrieved from https://golang.org/src/crypto/tls/generate_cert.go
|
2021-08-03 09:55:21 +00:00
|
|
|
ecdsaCurve, err := cmd.Flags().GetString("ecdsa-curve")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse ecdsa-curve flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
ed25519Key, err := cmd.Flags().GetBool("ed25519")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse ed25519 flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
rsaBits, err := cmd.Flags().GetInt("rsa-bits")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse rsa-bits flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, err := cmd.Flags().GetStringSlice("host")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse host flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
validFrom, err := cmd.Flags().GetString("start-date")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse start-date flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
validFor, err := cmd.Flags().GetDuration("duration")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse duration flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
isCA, err := cmd.Flags().GetBool("ca")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse ca flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
certificateTargetDirectory, err := cmd.Flags().GetString("dir")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to parse dir flag: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
cmdCertificatesGenerateRunExtended(hosts, ecdsaCurve, validFrom, certificateTargetDirectory, ed25519Key, isCA, rsaBits, validFor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdCertificatesGenerateRunExtended(hosts []string, ecdsaCurve, validFrom, certificateTargetDirectory string, ed25519Key, isCA bool, rsaBits int, validFor time.Duration) {
|
2022-04-04 23:57:47 +00:00
|
|
|
certPath := filepath.Join(certificateTargetDirectory, "cert.pem")
|
|
|
|
keyPath := filepath.Join(certificateTargetDirectory, "key.pem")
|
2020-03-01 13:08:09 +00:00
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
var (
|
|
|
|
notBefore time.Time
|
|
|
|
err error
|
|
|
|
)
|
2021-08-03 09:55:21 +00:00
|
|
|
|
|
|
|
switch len(validFrom) {
|
|
|
|
case 0:
|
2020-03-01 13:08:09 +00:00
|
|
|
notBefore = time.Now()
|
2021-08-03 09:55:21 +00:00
|
|
|
default:
|
2020-03-01 13:08:09 +00:00
|
|
|
notBefore, err = time.Parse("Jan 2 15:04:05 2006", validFrom)
|
|
|
|
if err != nil {
|
2022-04-04 23:57:47 +00:00
|
|
|
log.Fatalf("Failed to parse start date: %v", err)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
var privateKeyBuilder utils.PrivateKeyBuilder
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2021-08-03 09:55:21 +00:00
|
|
|
switch ecdsaCurve {
|
|
|
|
case "":
|
|
|
|
if ed25519Key {
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.Ed25519KeyBuilder{}
|
2021-08-03 09:55:21 +00:00
|
|
|
} else {
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.RSAKeyBuilder{}.WithKeySize(rsaBits)
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
|
|
|
case "P224":
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.ECDSAKeyBuilder{}.WithCurve(elliptic.P224())
|
2021-08-03 09:55:21 +00:00
|
|
|
case "P256":
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.ECDSAKeyBuilder{}.WithCurve(elliptic.P256())
|
2021-08-03 09:55:21 +00:00
|
|
|
case "P384":
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.ECDSAKeyBuilder{}.WithCurve(elliptic.P384())
|
2021-08-03 09:55:21 +00:00
|
|
|
case "P521":
|
2022-04-04 23:57:47 +00:00
|
|
|
privateKeyBuilder = utils.ECDSAKeyBuilder{}.WithCurve(elliptic.P521())
|
2021-08-03 09:55:21 +00:00
|
|
|
default:
|
2022-04-04 23:57:47 +00:00
|
|
|
log.Fatalf("Failed to generate private key: unrecognized elliptic curve: \"%s\"", ecdsaCurve)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
certBytes, keyBytes, err := utils.GenerateCertificate(privateKeyBuilder, hosts, notBefore, validFor, isCA)
|
2020-03-01 13:08:09 +00:00
|
|
|
if err != nil {
|
2022-04-04 23:57:47 +00:00
|
|
|
log.Fatal(err)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
err = os.WriteFile(certPath, certBytes, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to write %s for writing: %v", certPath, err)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
fmt.Printf("Certificate written to %s\n", certPath)
|
2020-03-01 13:08:09 +00:00
|
|
|
|
2022-04-04 23:57:47 +00:00
|
|
|
err = os.WriteFile(keyPath, keyBytes, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to write %s for writing: %v", certPath, err)
|
2021-08-03 09:55:21 +00:00
|
|
|
}
|
2022-04-04 23:57:47 +00:00
|
|
|
|
|
|
|
fmt.Printf("Private Key written to %s\n", keyPath)
|
2020-03-01 13:08:09 +00:00
|
|
|
}
|