2021-02-19 16:37:28 -07:00
|
|
|
package certificate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2021-02-20 16:23:08 -07:00
|
|
|
"crypto/x509/pkix"
|
2021-02-19 16:37:28 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-02-23 07:03:38 -07:00
|
|
|
// A Store represents a certificate store.
|
|
|
|
// It generates certificates as needed and automatically rotates expired certificates.
|
2021-02-19 16:37:28 -07:00
|
|
|
// The zero value for Store is an empty store ready to use.
|
|
|
|
//
|
2021-02-23 07:03:38 -07:00
|
|
|
// Certificate scopes must be registered with Register before calling Get or Load.
|
|
|
|
// This prevents the Store from creating or loading unnecessary certificates.
|
2021-02-23 06:36:27 -07:00
|
|
|
//
|
2021-02-19 16:37:28 -07:00
|
|
|
// Store is safe for concurrent use by multiple goroutines.
|
|
|
|
type Store struct {
|
|
|
|
// CreateCertificate, if not nil, is called to create a new certificate
|
2021-02-20 16:23:08 -07:00
|
|
|
// to replace a missing or expired certificate. If CreateCertificate
|
|
|
|
// is nil, a certificate with a duration of 1 year will be created.
|
2021-02-23 06:36:27 -07:00
|
|
|
// The provided scope is suitable for use in a certificate's DNSNames.
|
2021-02-19 16:37:28 -07:00
|
|
|
CreateCertificate func(scope string) (tls.Certificate, error)
|
|
|
|
|
|
|
|
certs map[string]tls.Certificate
|
|
|
|
path string
|
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:36:27 -07:00
|
|
|
// Register registers the provided scope with the certificate store.
|
|
|
|
// The scope can either be a hostname or a wildcard pattern (e.g. "*.example.com").
|
|
|
|
// To accept all hostnames, use the special pattern "*".
|
2021-02-19 16:37:28 -07:00
|
|
|
func (s *Store) Register(scope string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.certs == nil {
|
|
|
|
s.certs = make(map[string]tls.Certificate)
|
|
|
|
}
|
|
|
|
s.certs[scope] = tls.Certificate{}
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:36:27 -07:00
|
|
|
// Add adds a certificate with the given scope to the certificate store.
|
2021-02-23 07:03:38 -07:00
|
|
|
// If a certificate for the given scope already exists, Add will overwrite it.
|
2021-02-19 16:37:28 -07:00
|
|
|
func (s *Store) Add(scope string, cert tls.Certificate) error {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
if s.certs == nil {
|
|
|
|
s.certs = make(map[string]tls.Certificate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse certificate if not already parsed
|
|
|
|
if cert.Leaf == nil {
|
|
|
|
parsed, err := x509.ParseCertificate(cert.Certificate[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cert.Leaf = parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.path != "" {
|
2021-02-23 06:36:27 -07:00
|
|
|
certPath := filepath.Join(s.path, scope+".crt")
|
|
|
|
keyPath := filepath.Join(s.path, scope+".key")
|
2021-02-19 16:37:28 -07:00
|
|
|
if err := Write(cert, certPath, keyPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.certs[scope] = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:36:27 -07:00
|
|
|
// Get retrieves a certificate for the given hostname.
|
2021-02-23 07:03:38 -07:00
|
|
|
// If no matching scope has been registered, Get returns an error.
|
|
|
|
// Get generates new certificates as needed and rotates expired certificates.
|
2021-02-23 07:10:19 -07:00
|
|
|
//
|
|
|
|
// Get is suitable for use in a gemini.Server's GetCertificate field.
|
2021-02-23 06:36:27 -07:00
|
|
|
func (s *Store) Get(hostname string) (*tls.Certificate, error) {
|
2021-02-19 16:37:28 -07:00
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
2021-02-23 06:36:27 -07:00
|
|
|
cert, ok := s.certs[hostname]
|
2021-02-19 16:37:28 -07:00
|
|
|
if !ok {
|
|
|
|
// Try wildcard
|
2021-02-23 06:36:27 -07:00
|
|
|
wildcard := strings.SplitN(hostname, ".", 2)
|
2021-02-19 16:37:28 -07:00
|
|
|
if len(wildcard) == 2 {
|
2021-02-23 06:36:27 -07:00
|
|
|
hostname = "*." + wildcard[1]
|
|
|
|
cert, ok = s.certs[hostname]
|
2021-02-19 16:37:28 -07:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 06:52:12 -07:00
|
|
|
if !ok {
|
|
|
|
// Try "*"
|
|
|
|
cert, ok = s.certs["*"]
|
|
|
|
}
|
2021-02-19 16:37:28 -07:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unrecognized scope")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the certificate is empty or expired, generate a new one.
|
|
|
|
if cert.Leaf == nil || cert.Leaf.NotAfter.Before(time.Now()) {
|
2021-02-20 16:23:08 -07:00
|
|
|
var err error
|
2021-02-23 06:36:27 -07:00
|
|
|
cert, err = s.createCertificate(hostname)
|
2021-02-20 16:23:08 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-23 06:36:27 -07:00
|
|
|
if err := s.Add(hostname, cert); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to add certificate for %s: %w", hostname, err)
|
2021-02-19 16:37:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cert, nil
|
|
|
|
}
|
|
|
|
|
2021-02-20 16:23:08 -07:00
|
|
|
func (s *Store) createCertificate(scope string) (tls.Certificate, error) {
|
|
|
|
if s.CreateCertificate != nil {
|
|
|
|
return s.CreateCertificate(scope)
|
|
|
|
}
|
|
|
|
return Create(CreateOptions{
|
|
|
|
DNSNames: []string{scope},
|
|
|
|
Subject: pkix.Name{
|
|
|
|
CommonName: scope,
|
|
|
|
},
|
|
|
|
Duration: 365 * 24 * time.Hour,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:37:28 -07:00
|
|
|
// Load loads certificates from the provided path.
|
|
|
|
// New certificates will be written to this path.
|
|
|
|
// The path should lead to a directory containing certificates
|
|
|
|
// and private keys named "scope.crt" and "scope.key" respectively,
|
|
|
|
// where "scope" is the scope of the certificate.
|
2021-02-23 07:03:38 -07:00
|
|
|
// Certificates with scopes that have not been registered will be ignored.
|
2021-02-19 16:37:28 -07:00
|
|
|
func (s *Store) Load(path string) error {
|
|
|
|
matches, err := filepath.Glob(filepath.Join(path, "*.crt"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, crtPath := range matches {
|
2021-02-23 06:36:27 -07:00
|
|
|
scope := strings.TrimSuffix(filepath.Base(crtPath), ".crt")
|
|
|
|
if _, ok := s.certs[scope]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-19 16:37:28 -07:00
|
|
|
keyPath := strings.TrimSuffix(crtPath, ".crt") + ".key"
|
|
|
|
cert, err := tls.LoadX509KeyPair(crtPath, keyPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.Add(scope, cert)
|
|
|
|
}
|
|
|
|
s.SetPath(path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entries returns a map of scopes to certificates.
|
|
|
|
func (s *Store) Entries() map[string]tls.Certificate {
|
|
|
|
s.mu.RLock()
|
|
|
|
defer s.mu.RUnlock()
|
|
|
|
certs := make(map[string]tls.Certificate)
|
|
|
|
for key := range s.certs {
|
|
|
|
certs[key] = s.certs[key]
|
|
|
|
}
|
|
|
|
return certs
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPath sets the path that new certificates will be written to.
|
|
|
|
func (s *Store) SetPath(path string) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
s.path = path
|
|
|
|
}
|