@ bdd32d2bb7ca2a1b5b8c192fbaab691d8f1ed255 | history
package ratify
import (
"crypto/x509"
"time"
)
// CheckValidity checks that at falls inside every cert's validity window.
// Each cert carries its own window, so the program is in force only where
// all of them overlap: from the latest notBefore to the earliest notAfter.
// RFC 5280 counts both ends as inside the window.
func CheckValidity(certs []*x509.Certificate, at time.Time) error {
if len(certs) == 0 {
return nil
}
start, end := 0, 0
for i, c := range certs {
if c.NotBefore.After(certs[start].NotBefore) {
start = i
}
if c.NotAfter.Before(certs[end].NotAfter) {
end = i
}
}
takesEffect, expires := certs[start].NotBefore, certs[end].NotAfter
if takesEffect.After(expires) {
return errorf(ErrLapsed, "never in force: depth %d expires %s, before depth %d takes effect %s",
end, stamp(expires), start, stamp(takesEffect))
}
if at.Before(takesEffect) {
return errorf(ErrNotInForce, "takes effect %s (depth %d)", stamp(takesEffect), start)
}
if at.After(expires) {
return errorf(ErrLapsed, "expired %s (depth %d)", stamp(expires), end)
}
return nil
}
func stamp(t time.Time) string {
return t.UTC().Format(time.RFC3339)
}