2018-10-05 21:48:36 +00:00
|
|
|
package authority
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2018-10-19 05:26:39 +00:00
|
|
|
"time"
|
2018-10-05 21:48:36 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-10-19 05:26:39 +00:00
|
|
|
x509 "github.com/smallstep/cli/pkg/x509"
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
// certClaim interface is implemented by types used to validate specific claims in a
|
|
|
|
// certificate request.
|
|
|
|
type certClaim interface {
|
|
|
|
Valid(crt *x509.Certificate) error
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// ValidateClaims returns nil if all the claims are validated, it will return
|
|
|
|
// the first error if a claim fails.
|
2018-10-19 05:26:39 +00:00
|
|
|
func validateClaims(crt *x509.Certificate, claims []certClaim) (err error) {
|
2018-10-05 21:48:36 +00:00
|
|
|
for _, c := range claims {
|
2018-10-19 05:26:39 +00:00
|
|
|
if err = c.Valid(crt); err != nil {
|
2018-10-05 21:48:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// commonNameClaim validates the common name of a certificate request.
|
|
|
|
type commonNameClaim struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid checks that certificate request common name matches the one configured.
|
2018-10-19 05:26:39 +00:00
|
|
|
func (c *commonNameClaim) Valid(crt *x509.Certificate) error {
|
|
|
|
if crt.Subject.CommonName == "" {
|
2018-10-05 21:48:36 +00:00
|
|
|
return errors.New("common name cannot be empty")
|
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
if crt.Subject.CommonName != c.name {
|
|
|
|
return errors.Errorf("common name claim failed - got %s, want %s", crt.Subject.CommonName, c.name)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type dnsNamesClaim struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid checks that certificate request common name matches the one configured.
|
2018-10-19 05:26:39 +00:00
|
|
|
func (c *dnsNamesClaim) Valid(crt *x509.Certificate) error {
|
|
|
|
if len(crt.DNSNames) == 0 {
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-10-19 05:26:39 +00:00
|
|
|
for _, name := range crt.DNSNames {
|
2018-10-05 21:48:36 +00:00
|
|
|
if name != c.name {
|
|
|
|
return errors.Errorf("DNS names claim failed - got %s, want %s", name, c.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipAddressesClaim struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid checks that certificate request common name matches the one configured.
|
2018-10-19 05:26:39 +00:00
|
|
|
func (c *ipAddressesClaim) Valid(crt *x509.Certificate) error {
|
|
|
|
if len(crt.IPAddresses) == 0 {
|
2018-10-05 21:48:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's an IP validate that only that ip is in IP addresses
|
|
|
|
if requestedIP := net.ParseIP(c.name); requestedIP != nil {
|
2018-10-19 05:26:39 +00:00
|
|
|
for _, ip := range crt.IPAddresses {
|
2018-10-05 21:48:36 +00:00
|
|
|
if !ip.Equal(requestedIP) {
|
|
|
|
return errors.Errorf("IP addresses claim failed - got %s, want %s", ip, requestedIP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 05:26:39 +00:00
|
|
|
return errors.Errorf("IP addresses claim failed - got %v, want none", crt.IPAddresses)
|
|
|
|
}
|
|
|
|
|
|
|
|
// certTemporalClaim validates the certificate temporal validity settings.
|
|
|
|
type certTemporalClaim struct {
|
|
|
|
min time.Duration
|
|
|
|
max time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the certificate temporal validity settings.
|
|
|
|
func (ctc *certTemporalClaim) Valid(crt *x509.Certificate) error {
|
|
|
|
var (
|
|
|
|
na = crt.NotAfter
|
|
|
|
nb = crt.NotBefore
|
|
|
|
d = na.Sub(nb)
|
|
|
|
now = time.Now()
|
|
|
|
)
|
|
|
|
|
|
|
|
if na.Before(now) {
|
|
|
|
return errors.Errorf("NotAfter: %v cannot be in the past", na)
|
|
|
|
}
|
|
|
|
if na.Before(nb) {
|
|
|
|
return errors.Errorf("NotAfter: %v cannot be before NotBefore: %v", na, nb)
|
|
|
|
}
|
|
|
|
if d < ctc.min {
|
|
|
|
return errors.Errorf("requested duration of %v is less than the authorized minimum certificate duration of %v",
|
|
|
|
d, ctc.min)
|
|
|
|
}
|
|
|
|
if d > ctc.max {
|
|
|
|
return errors.Errorf("requested duration of %v is more than the authorized maximum certificate duration of %v",
|
|
|
|
d, ctc.max)
|
|
|
|
}
|
|
|
|
return nil
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|