2020-03-01 20:08:09 +07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
"encoding/pem"
|
2021-08-03 16:55:21 +07:00
|
|
|
"fmt"
|
2020-03-01 20:08:09 +07:00
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"net"
|
|
|
|
"os"
|
2021-08-03 16:55:21 +07:00
|
|
|
"path/filepath"
|
2020-03-01 20:08:09 +07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2021-08-03 16:55:21 +07: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 20:08:09 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
cmd.PersistentFlags().StringSlice("host", []string{}, "Comma-separated hostnames and IPs to generate a certificate for")
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
err := cmd.MarkPersistentFlagRequired("host")
|
2020-03-01 20:08:09 +07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
cmd.AddCommand(newCertificatesGenerateCmd())
|
2020-03-01 20:08:09 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
return cmd
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07: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 20:08:09 +07:00
|
|
|
}
|
2021-08-03 16:55:21 +07: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 20:08:09 +07:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
func cmdCertificatesGenerateRun(cmd *cobra.Command, _ []string) {
|
2020-03-01 20:08:09 +07:00
|
|
|
// implementation retrieved from https://golang.org/src/crypto/tls/generate_cert.go
|
2021-08-03 16:55:21 +07: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-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07: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-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07: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 20:08:09 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07: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) {
|
|
|
|
priv, err := getPrivateKey(ecdsaCurve, ed25519Key, rsaBits)
|
|
|
|
|
2020-03-01 20:08:09 +07:00
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Failed to generate private key: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
var notBefore time.Time
|
2021-08-03 16:55:21 +07:00
|
|
|
|
|
|
|
switch len(validFrom) {
|
|
|
|
case 0:
|
2020-03-01 20:08:09 +07:00
|
|
|
notBefore = time.Now()
|
2021-08-03 16:55:21 +07:00
|
|
|
default:
|
2020-03-01 20:08:09 +07:00
|
|
|
notBefore, err = time.Parse("Jan 2 15:04:05 2006", validFrom)
|
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Failed to parse start date: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
notAfter := notBefore.Add(validFor)
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
2020-03-01 20:08:09 +07:00
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Failed to generate serial number: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"Acme Co"},
|
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, h := range hosts {
|
|
|
|
if ip := net.ParseIP(h); ip != nil {
|
|
|
|
template.IPAddresses = append(template.IPAddresses, ip)
|
|
|
|
} else {
|
|
|
|
template.DNSNames = append(template.DNSNames, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isCA {
|
|
|
|
template.IsCA = true
|
|
|
|
template.KeyUsage |= x509.KeyUsageCertSign
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
certPath := filepath.Join(certificateTargetDirectory, "cert.pem")
|
|
|
|
|
2020-03-01 20:08:09 +07:00
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
|
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Failed to create certificate: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
writePEM(derBytes, "CERTIFICATE", certPath)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Certificate Public Key written to %s\n", certPath)
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
keyPath := filepath.Join(certificateTargetDirectory, "key.pem")
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to marshal private key: %v\n", err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
writePEM(privBytes, "PRIVATE KEY", keyPath)
|
2020-03-01 20:08:09 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Certificate Private Key written to %s\n", keyPath)
|
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
func getPrivateKey(ecdsaCurve string, ed25519Key bool, rsaBits int) (priv interface{}, err error) {
|
|
|
|
switch ecdsaCurve {
|
|
|
|
case "":
|
|
|
|
if ed25519Key {
|
|
|
|
_, priv, err = ed25519.GenerateKey(rand.Reader)
|
|
|
|
} else {
|
|
|
|
priv, err = rsa.GenerateKey(rand.Reader, rsaBits)
|
|
|
|
}
|
|
|
|
case "P224":
|
|
|
|
priv, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
|
|
|
|
case "P256":
|
|
|
|
priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
|
case "P384":
|
|
|
|
priv, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
|
|
|
case "P521":
|
|
|
|
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unrecognized elliptic curve: %q", ecdsaCurve)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
return priv, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePEM(bytes []byte, blockType, path string) {
|
|
|
|
keyOut, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2020-03-01 20:08:09 +07:00
|
|
|
if err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Failed to open %s for writing: %v\n", path, err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
if err := pem.Encode(keyOut, &pem.Block{Type: blockType, Bytes: bytes}); err != nil {
|
|
|
|
fmt.Printf("Failed to write data to %s: %v\n", path, err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2020-03-01 20:08:09 +07:00
|
|
|
if err := keyOut.Close(); err != nil {
|
2021-08-03 16:55:21 +07:00
|
|
|
fmt.Printf("Error closing %s: %v\n", path, err)
|
|
|
|
os.Exit(1)
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 16:55:21 +07:00
|
|
|
func publicKey(priv interface{}) interface{} {
|
|
|
|
switch k := priv.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
return &k.PublicKey
|
|
|
|
case ed25519.PrivateKey:
|
|
|
|
return k.Public().(ed25519.PublicKey)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2020-03-01 20:08:09 +07:00
|
|
|
}
|