2019-03-11 16:56:48 +00:00
|
|
|
package cmd
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-12-25 18:36:01 +00:00
|
|
|
"crypto"
|
2018-12-06 21:50:17 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
2021-12-25 18:36:01 +00:00
|
|
|
"encoding/pem"
|
2024-01-27 22:44:14 +00:00
|
|
|
"errors"
|
2021-12-25 18:36:01 +00:00
|
|
|
"fmt"
|
2018-12-06 21:50:17 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
|
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
2022-02-13 11:28:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2018-12-06 21:50:17 +00:00
|
|
|
"golang.org/x/net/idna"
|
2021-12-25 18:36:01 +00:00
|
|
|
"software.sslmate.com/src/go-pkcs12"
|
2018-12-06 21:50:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
baseCertificatesFolderName = "certificates"
|
|
|
|
baseArchivesFolderName = "archives"
|
|
|
|
)
|
|
|
|
|
2023-05-05 22:14:07 +00:00
|
|
|
const (
|
|
|
|
issuerExt = ".issuer.crt"
|
|
|
|
certExt = ".crt"
|
|
|
|
keyExt = ".key"
|
|
|
|
pemExt = ".pem"
|
|
|
|
pfxExt = ".pfx"
|
|
|
|
resourceExt = ".json"
|
|
|
|
)
|
|
|
|
|
2022-08-22 15:05:31 +00:00
|
|
|
// CertificatesStorage a certificates' storage.
|
2018-12-06 21:50:17 +00:00
|
|
|
//
|
|
|
|
// rootPath:
|
|
|
|
//
|
2022-08-22 15:05:31 +00:00
|
|
|
// ./.lego/certificates/
|
|
|
|
// │ └── root certificates directory
|
|
|
|
// └── "path" option
|
2018-12-06 21:50:17 +00:00
|
|
|
//
|
|
|
|
// archivePath:
|
|
|
|
//
|
2022-08-22 15:05:31 +00:00
|
|
|
// ./.lego/archives/
|
|
|
|
// │ └── archived certificates directory
|
|
|
|
// └── "path" option
|
2018-12-06 21:50:17 +00:00
|
|
|
type CertificatesStorage struct {
|
|
|
|
rootPath string
|
|
|
|
archivePath string
|
|
|
|
pem bool
|
2021-12-25 18:36:01 +00:00
|
|
|
pfx bool
|
|
|
|
pfxPassword string
|
2024-01-27 22:44:14 +00:00
|
|
|
pfxFormat string
|
2018-12-06 21:50:17 +00:00
|
|
|
filename string // Deprecated
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCertificatesStorage create a new certificates storage.
|
|
|
|
func NewCertificatesStorage(ctx *cli.Context) *CertificatesStorage {
|
2024-09-20 17:47:50 +00:00
|
|
|
pfxFormat := ctx.String(flgPFXFormat)
|
2024-01-27 22:44:14 +00:00
|
|
|
|
|
|
|
switch pfxFormat {
|
|
|
|
case "DES", "RC2", "SHA256":
|
|
|
|
default:
|
|
|
|
log.Fatalf("Invalid PFX format: %s", pfxFormat)
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
return &CertificatesStorage{
|
2024-09-20 17:47:50 +00:00
|
|
|
rootPath: filepath.Join(ctx.String(flgPath), baseCertificatesFolderName),
|
|
|
|
archivePath: filepath.Join(ctx.String(flgPath), baseArchivesFolderName),
|
|
|
|
pem: ctx.Bool(flgPEM),
|
|
|
|
pfx: ctx.Bool(flgPFX),
|
|
|
|
pfxPassword: ctx.String(flgPFXPass),
|
2024-01-27 22:44:14 +00:00
|
|
|
pfxFormat: pfxFormat,
|
2024-09-20 17:47:50 +00:00
|
|
|
filename: ctx.String(flgFilename),
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) CreateRootFolder() {
|
|
|
|
err := createNonExistingFolder(s.rootPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not check/create path: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) CreateArchiveFolder() {
|
|
|
|
err := createNonExistingFolder(s.archivePath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not check/create path: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) GetRootPath() string {
|
|
|
|
return s.rootPath
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) SaveResource(certRes *certificate.Resource) {
|
|
|
|
domain := certRes.Domain
|
|
|
|
|
|
|
|
// We store the certificate, private key and metadata in different files
|
|
|
|
// as web servers would not be able to work with a combined file.
|
2023-05-05 22:14:07 +00:00
|
|
|
err := s.WriteFile(domain, certExt, certRes.Certificate)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to save Certificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if certRes.IssuerCertificate != nil {
|
2023-05-05 22:14:07 +00:00
|
|
|
err = s.WriteFile(domain, issuerExt, certRes.IssuerCertificate)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to save IssuerCertificate for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 18:36:01 +00:00
|
|
|
// if we were given a CSR, we don't know the private key
|
2018-12-06 21:50:17 +00:00
|
|
|
if certRes.PrivateKey != nil {
|
2021-12-25 18:36:01 +00:00
|
|
|
err = s.WriteCertificateFiles(domain, certRes)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to save PrivateKey for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
2021-12-25 18:36:01 +00:00
|
|
|
} else if s.pem || s.pfx {
|
|
|
|
// we don't have the private key; can't write the .pem or .pfx file
|
|
|
|
log.Fatalf("Unable to save PEM or PFX without private key for domain %s. Are you using a CSR?", domain)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(certRes, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to marshal CertResource for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:14:07 +00:00
|
|
|
err = s.WriteFile(domain, resourceExt, jsonBytes)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Unable to save CertResource for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) ReadResource(domain string) certificate.Resource {
|
2023-05-05 22:14:07 +00:00
|
|
|
raw, err := s.ReadFile(domain, resourceExt)
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error while loading the meta data for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resource certificate.Resource
|
|
|
|
if err = json.Unmarshal(raw, &resource); err != nil {
|
|
|
|
log.Fatalf("Error while marshaling the meta data for domain %s\n\t%v", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) ExistsFile(domain, extension string) bool {
|
2020-04-11 12:57:06 +00:00
|
|
|
filePath := s.GetFileName(domain, extension)
|
2018-12-06 21:50:17 +00:00
|
|
|
|
|
|
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
} else if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) ReadFile(domain, extension string) ([]byte, error) {
|
2021-08-25 09:44:11 +00:00
|
|
|
return os.ReadFile(s.GetFileName(domain, extension))
|
2020-04-11 12:57:06 +00:00
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
|
2020-04-11 12:57:06 +00:00
|
|
|
func (s *CertificatesStorage) GetFileName(domain, extension string) string {
|
|
|
|
filename := sanitizedDomain(domain) + extension
|
|
|
|
return filepath.Join(s.rootPath, filename)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) ReadCertificate(domain, extension string) ([]*x509.Certificate, error) {
|
|
|
|
content, err := s.ReadFile(domain, extension)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The input may be a bundle or a single certificate.
|
|
|
|
return certcrypto.ParsePEMBundle(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) WriteFile(domain, extension string, data []byte) error {
|
|
|
|
var baseFileName string
|
|
|
|
if s.filename != "" {
|
|
|
|
baseFileName = s.filename
|
|
|
|
} else {
|
|
|
|
baseFileName = sanitizedDomain(domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := filepath.Join(s.rootPath, baseFileName+extension)
|
|
|
|
|
2021-08-25 09:44:11 +00:00
|
|
|
return os.WriteFile(filePath, data, filePerm)
|
2018-12-06 21:50:17 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 18:36:01 +00:00
|
|
|
func (s *CertificatesStorage) WriteCertificateFiles(domain string, certRes *certificate.Resource) error {
|
2023-05-05 22:14:07 +00:00
|
|
|
err := s.WriteFile(domain, keyExt, certRes.PrivateKey)
|
2021-12-25 18:36:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to save key file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.pem {
|
2023-05-05 22:14:07 +00:00
|
|
|
err = s.WriteFile(domain, pemExt, bytes.Join([][]byte{certRes.Certificate, certRes.PrivateKey}, nil))
|
2021-12-25 18:36:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to save PEM file: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.pfx {
|
|
|
|
err = s.WritePFXFile(domain, certRes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to save PFX file: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *CertificatesStorage) WritePFXFile(domain string, certRes *certificate.Resource) error {
|
|
|
|
certPemBlock, _ := pem.Decode(certRes.Certificate)
|
|
|
|
if certPemBlock == nil {
|
|
|
|
return fmt.Errorf("unable to parse Certificate for domain %s", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, err := x509.ParseCertificate(certPemBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to load Certificate for domain %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:44:14 +00:00
|
|
|
certChain, err := getCertificateChain(certRes)
|
2021-12-25 18:36:01 +00:00
|
|
|
if err != nil {
|
2024-01-27 22:44:14 +00:00
|
|
|
return fmt.Errorf("unable to get certificate chain for domain %s: %w", domain, err)
|
2021-12-25 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keyPemBlock, _ := pem.Decode(certRes.PrivateKey)
|
|
|
|
if keyPemBlock == nil {
|
|
|
|
return fmt.Errorf("unable to parse PrivateKey for domain %s", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
var privateKey crypto.Signer
|
|
|
|
var keyErr error
|
|
|
|
|
|
|
|
switch keyPemBlock.Type {
|
|
|
|
case "RSA PRIVATE KEY":
|
|
|
|
privateKey, keyErr = x509.ParsePKCS1PrivateKey(keyPemBlock.Bytes)
|
|
|
|
if keyErr != nil {
|
|
|
|
return fmt.Errorf("unable to load RSA PrivateKey for domain %s: %w", domain, keyErr)
|
|
|
|
}
|
|
|
|
case "EC PRIVATE KEY":
|
|
|
|
privateKey, keyErr = x509.ParseECPrivateKey(keyPemBlock.Bytes)
|
|
|
|
if keyErr != nil {
|
|
|
|
return fmt.Errorf("unable to load EC PrivateKey for domain %s: %w", domain, keyErr)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported PrivateKey type '%s' for domain %s", keyPemBlock.Type, domain)
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:44:14 +00:00
|
|
|
encoder, err := getPFXEncoder(s.pfxFormat)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("PFX encoder: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pfxBytes, err := encoder.Encode(privateKey, cert, certChain, s.pfxPassword)
|
2021-12-25 18:36:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to encode PFX data for domain %s: %w", domain, err)
|
|
|
|
}
|
|
|
|
|
2023-05-05 22:14:07 +00:00
|
|
|
return s.WriteFile(domain, pfxExt, pfxBytes)
|
2021-12-25 18:36:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
func (s *CertificatesStorage) MoveToArchive(domain string) error {
|
2023-05-05 22:14:07 +00:00
|
|
|
baseFilename := filepath.Join(s.rootPath, sanitizedDomain(domain))
|
|
|
|
|
|
|
|
matches, err := filepath.Glob(baseFilename + ".*")
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, oldFile := range matches {
|
2023-05-05 22:14:07 +00:00
|
|
|
if strings.TrimSuffix(oldFile, filepath.Ext(oldFile)) != baseFilename && oldFile != baseFilename+issuerExt {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
date := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
filename := date + "." + filepath.Base(oldFile)
|
|
|
|
newFile := filepath.Join(s.archivePath, filename)
|
|
|
|
|
|
|
|
err = os.Rename(oldFile, newFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:44:14 +00:00
|
|
|
func getCertificateChain(certRes *certificate.Resource) ([]*x509.Certificate, error) {
|
|
|
|
chainCertPemBlock, rest := pem.Decode(certRes.IssuerCertificate)
|
|
|
|
if chainCertPemBlock == nil {
|
|
|
|
return nil, errors.New("unable to parse Issuer Certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
var certChain []*x509.Certificate
|
|
|
|
for chainCertPemBlock != nil {
|
|
|
|
chainCert, err := x509.ParseCertificate(chainCertPemBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to parse Chain Certificate: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
certChain = append(certChain, chainCert)
|
|
|
|
chainCertPemBlock, rest = pem.Decode(rest) // Try decoding the next pem block
|
|
|
|
}
|
|
|
|
|
|
|
|
return certChain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPFXEncoder(pfxFormat string) (*pkcs12.Encoder, error) {
|
|
|
|
var encoder *pkcs12.Encoder
|
|
|
|
switch pfxFormat {
|
|
|
|
case "SHA256":
|
|
|
|
encoder = pkcs12.Modern2023
|
|
|
|
case "DES":
|
|
|
|
encoder = pkcs12.LegacyDES
|
|
|
|
case "RC2":
|
|
|
|
encoder = pkcs12.LegacyRC2
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid PFX format: %s", pfxFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoder, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// sanitizedDomain Make sure no funny chars are in the cert names (like wildcards ;)).
|
2018-12-06 21:50:17 +00:00
|
|
|
func sanitizedDomain(domain string) string {
|
2023-05-02 17:02:18 +00:00
|
|
|
safe, err := idna.ToASCII(strings.NewReplacer(":", "-", "*", "_").Replace(domain))
|
2018-12-06 21:50:17 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return safe
|
|
|
|
}
|