Revert "certificate.Store: Allow using '*' in DNSNames"

This reverts commit de0b93a4f6.
This commit is contained in:
Adnan Maolood 2021-03-04 19:26:13 -05:00
parent d6d02e398e
commit c9e2af98f3

View File

@ -33,9 +33,10 @@ type Store struct {
// The provided scope is suitable for use in a certificate's DNSNames. // The provided scope is suitable for use in a certificate's DNSNames.
CreateCertificate func(scope string) (tls.Certificate, error) CreateCertificate func(scope string) (tls.Certificate, error)
certs map[string]tls.Certificate scopes map[string]struct{}
path string certs map[string]tls.Certificate
mu sync.RWMutex path string
mu sync.RWMutex
} }
// Register registers the provided scope with the certificate store. // Register registers the provided scope with the certificate store.
@ -47,10 +48,10 @@ type Store struct {
func (s *Store) Register(scope string) { func (s *Store) Register(scope string) {
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
if s.certs == nil { if s.scopes == nil {
s.certs = make(map[string]tls.Certificate) s.scopes = make(map[string]struct{})
} }
s.certs[scope] = tls.Certificate{} s.scopes[scope] = struct{}{}
} }
// Add registers the certificate for the given scope. // Add registers the certificate for the given scope.
@ -104,24 +105,24 @@ func (s *Store) write(scope string, cert tls.Certificate) error {
// Get is suitable for use in a gemini.Server's GetCertificate field. // Get is suitable for use in a gemini.Server's GetCertificate field.
func (s *Store) Get(hostname string) (*tls.Certificate, error) { func (s *Store) Get(hostname string) (*tls.Certificate, error) {
s.mu.RLock() s.mu.RLock()
cert, ok := s.certs[hostname] _, ok := s.scopes[hostname]
if !ok { if !ok {
// Try wildcard // Try wildcard
wildcard := strings.SplitN(hostname, ".", 2) wildcard := strings.SplitN(hostname, ".", 2)
if len(wildcard) == 2 { if len(wildcard) == 2 {
hostname = "*." + wildcard[1] hostname = "*." + wildcard[1]
cert, ok = s.certs[hostname] _, ok = s.scopes[hostname]
} }
} }
if !ok { if !ok {
// Try "*" // Try "*"
hostname = "*" _, ok = s.scopes["*"]
cert, ok = s.certs[hostname]
} }
if !ok { if !ok {
s.mu.RUnlock() s.mu.RUnlock()
return nil, errors.New("unrecognized scope") return nil, errors.New("unrecognized scope")
} }
cert := s.certs[hostname]
s.mu.RUnlock() s.mu.RUnlock()
// If the certificate is empty or expired, generate a new one. // If the certificate is empty or expired, generate a new one.