Skip to content

Commit

Permalink
when we get a tls connection with an unrecognized sni hostname/ip, ca…
Browse files Browse the repository at this point in the history
…use an alert "unrecognized name" rather than "internal error"

more helpful error for users trying to debug whats going on.

problem pointed out by arnt, thanks!
  • Loading branch information
mjl- committed Apr 8, 2024
1 parent ecf6163 commit 89a9a8b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions autotls/autotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,27 @@ func Load(name, acmeDir, contactEmail, directoryURL string, eabKeyID string, eab
loggingGetCertificate := func(hello *tls.ClientHelloInfo) (*tls.Certificate, error) {
log := mlog.New("autotls", nil).WithContext(hello.Context())

// We handle missing invalid hostnames/ip's by returning a nil certificate and nil
// error, which crypto/tls turns into a TLS alert "unrecognized name", which can be
// interpreted by clients as a hint that they are using the wrong hostname, or a
// certificate is missing.

// Handle missing SNI to prevent logging an error below.
// At startup, during config initialization, we already adjust the tls config to
// inject the listener hostname if there isn't one in the TLS client hello. This is
// common for SMTP STARTTLS connections, which often do not care about the
// verification of the certificate.
if hello.ServerName == "" {
log.Debug("tls request without sni servername, rejecting", slog.Any("localaddr", hello.Conn.LocalAddr()), slog.Any("supportedprotos", hello.SupportedProtos))
return nil, fmt.Errorf("sni server name required")
return nil, nil
}

cert, err := m.GetCertificate(hello)
if err != nil {
if errors.Is(err, errHostNotAllowed) {
log.Debugx("requesting certificate", err, slog.String("host", hello.ServerName))
} else {
log.Errorx("requesting certificate", err, slog.String("host", hello.ServerName))
}
if err != nil && errors.Is(err, errHostNotAllowed) {
log.Debugx("requesting certificate", err, slog.String("host", hello.ServerName))
return nil, nil
} else if err != nil {
log.Errorx("requesting certificate", err, slog.String("host", hello.ServerName))
}
return cert, err
}
Expand Down

0 comments on commit 89a9a8b

Please sign in to comment.