fix: return error for no peer certs, include IP SANs

- extractCertInfo now returns an error (ErrNoPeerCertificates) instead
  of an empty struct when there are no peer certificates
- SubjectAlternativeNames now includes both DNS names and IP addresses
  from cert.IPAddresses

Addresses review feedback on PR #7.
This commit is contained in:
user 2026-02-19 23:51:55 -08:00
parent 3fcf203485
commit 54b00f3b2a

View File

@ -27,6 +27,12 @@ var ErrUnexpectedConnType = errors.New(
"unexpected connection type", "unexpected connection type",
) )
// ErrNoPeerCertificates indicates the TLS connection had no peer
// certificates.
var ErrNoPeerCertificates = errors.New(
"no peer certificates",
)
// CertificateInfo holds information about a TLS certificate. // CertificateInfo holds information about a TLS certificate.
type CertificateInfo struct { type CertificateInfo struct {
CommonName string CommonName string
@ -144,7 +150,7 @@ func (c *Checker) CheckCertificate(
) )
} }
return c.extractCertInfo(tlsConn), nil return c.extractCertInfo(tlsConn)
} }
func (c *Checker) buildTLSConfig( func (c *Checker) buildTLSConfig(
@ -165,16 +171,20 @@ func (c *Checker) buildTLSConfig(
func (c *Checker) extractCertInfo( func (c *Checker) extractCertInfo(
conn *tls.Conn, conn *tls.Conn,
) *CertificateInfo { ) (*CertificateInfo, error) {
state := conn.ConnectionState() state := conn.ConnectionState()
if len(state.PeerCertificates) == 0 { if len(state.PeerCertificates) == 0 {
return &CertificateInfo{} return nil, ErrNoPeerCertificates
} }
cert := state.PeerCertificates[0] cert := state.PeerCertificates[0]
sans := make([]string, len(cert.DNSNames)) sans := make([]string, 0, len(cert.DNSNames)+len(cert.IPAddresses))
copy(sans, cert.DNSNames) sans = append(sans, cert.DNSNames...)
for _, ip := range cert.IPAddresses {
sans = append(sans, ip.String())
}
return &CertificateInfo{ return &CertificateInfo{
CommonName: cert.Subject.CommonName, CommonName: cert.Subject.CommonName,
@ -182,5 +192,5 @@ func (c *Checker) extractCertInfo(
NotAfter: cert.NotAfter, NotAfter: cert.NotAfter,
SubjectAlternativeNames: sans, SubjectAlternativeNames: sans,
SerialNumber: cert.SerialNumber.String(), SerialNumber: cert.SerialNumber.String(),
} }, nil
} }