certificate.Store: Allow using '*' in DNSNames

This isn't exactly a valid DNSName, but it reduces the number of
certificates that need to be created. Clients should either accept it or
skip checking DNSNames.
This commit is contained in:
Adnan Maolood 2021-03-04 16:40:16 -05:00
parent ce649ecc66
commit de0b93a4f6

View File

@ -33,7 +33,6 @@ 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)
scopes map[string]struct{}
certs map[string]tls.Certificate certs map[string]tls.Certificate
path string path string
mu sync.RWMutex mu sync.RWMutex
@ -48,10 +47,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.scopes == nil { if s.certs == nil {
s.scopes = make(map[string]struct{}) s.certs = make(map[string]tls.Certificate)
} }
s.scopes[scope] = struct{}{} s.certs[scope] = tls.Certificate{}
} }
// Add registers the certificate for the given scope. // Add registers the certificate for the given scope.
@ -105,24 +104,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()
_, ok := s.scopes[hostname] cert, ok := s.certs[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]
_, ok = s.scopes[hostname] cert, ok = s.certs[hostname]
} }
} }
if !ok { if !ok {
// Try "*" // Try "*"
_, ok = s.scopes["*"] hostname = "*"
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.