4
0

sign_options.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "encoding/asn1"
  6. "net"
  7. "reflect"
  8. "time"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/cli/crypto/x509util"
  11. )
  12. // Options contains the options that can be passed to the Sign method.
  13. type Options struct {
  14. NotAfter time.Time `json:"notAfter"`
  15. NotBefore time.Time `json:"notBefore"`
  16. }
  17. // SignOption is the interface used to collect all extra options used in the
  18. // Sign method.
  19. type SignOption interface{}
  20. // CertificateValidator is the interface used to validate a X.509 certificate.
  21. type CertificateValidator interface {
  22. SignOption
  23. Valid(crt *x509.Certificate) error
  24. }
  25. // CertificateRequestValidator is the interface used to validate a X.509
  26. // certificate request.
  27. type CertificateRequestValidator interface {
  28. SignOption
  29. Valid(req *x509.CertificateRequest) error
  30. }
  31. // ProfileModifier is the interface used to add custom options to the profile
  32. // constructor. The options are used to modify the final certificate.
  33. type ProfileModifier interface {
  34. SignOption
  35. Option(o Options) x509util.WithOption
  36. }
  37. // profileWithOption is a wrapper against x509util.WithOption to conform the
  38. // interface.
  39. type profileWithOption x509util.WithOption
  40. func (v profileWithOption) Option(Options) x509util.WithOption {
  41. return x509util.WithOption(v)
  42. }
  43. // profileDefaultDuration is a wrapper against x509util.WithOption to conform the
  44. // interface.
  45. type profileDefaultDuration time.Duration
  46. func (v profileDefaultDuration) Option(so Options) x509util.WithOption {
  47. return x509util.WithNotBeforeAfterDuration(so.NotBefore, so.NotAfter, time.Duration(v))
  48. }
  49. // emailOnlyIdentity is a CertificateRequestValidator that checks that the only
  50. // SAN provided is the given email address.
  51. type emailOnlyIdentity string
  52. func (e emailOnlyIdentity) Valid(req *x509.CertificateRequest) error {
  53. switch {
  54. case len(req.DNSNames) > 0:
  55. return errors.New("certificate request cannot contain DNS names")
  56. case len(req.IPAddresses) > 0:
  57. return errors.New("certificate request cannot contain IP addresses")
  58. case len(req.URIs) > 0:
  59. return errors.New("certificate request cannot contain URIs")
  60. case len(req.EmailAddresses) == 0:
  61. return errors.New("certificate request does not contain any email address")
  62. case len(req.EmailAddresses) > 1:
  63. return errors.New("certificate request does not contain too many email addresses")
  64. case req.EmailAddresses[0] == "":
  65. return errors.New("certificate request cannot contain an empty email address")
  66. case req.EmailAddresses[0] != string(e):
  67. return errors.Errorf("certificate request does not contain the valid email address, got %s, want %s", req.EmailAddresses[0], e)
  68. default:
  69. return nil
  70. }
  71. }
  72. // commonNameValidator validates the common name of a certificate request.
  73. type commonNameValidator string
  74. // Valid checks that certificate request common name matches the one configured.
  75. func (v commonNameValidator) Valid(req *x509.CertificateRequest) error {
  76. if req.Subject.CommonName == "" {
  77. return errors.New("certificate request cannot contain an empty common name")
  78. }
  79. if req.Subject.CommonName != string(v) {
  80. return errors.Errorf("certificate request does not contain the valid common name, got %s, want %s", req.Subject.CommonName, v)
  81. }
  82. return nil
  83. }
  84. // dnsNamesValidator validates the DNS names SAN of a certificate request.
  85. type dnsNamesValidator []string
  86. // Valid checks that certificate request DNS Names match those configured in
  87. // the bootstrap (token) flow.
  88. func (v dnsNamesValidator) Valid(req *x509.CertificateRequest) error {
  89. want := make(map[string]bool)
  90. for _, s := range v {
  91. want[s] = true
  92. }
  93. got := make(map[string]bool)
  94. for _, s := range req.DNSNames {
  95. got[s] = true
  96. }
  97. if !reflect.DeepEqual(want, got) {
  98. return errors.Errorf("certificate request does not contain the valid DNS names - got %v, want %v", req.DNSNames, v)
  99. }
  100. return nil
  101. }
  102. // ipAddressesValidator validates the IP addresses SAN of a certificate request.
  103. type ipAddressesValidator []net.IP
  104. // Valid checks that certificate request IP Addresses match those configured in
  105. // the bootstrap (token) flow.
  106. func (v ipAddressesValidator) Valid(req *x509.CertificateRequest) error {
  107. want := make(map[string]bool)
  108. for _, ip := range v {
  109. want[ip.String()] = true
  110. }
  111. got := make(map[string]bool)
  112. for _, ip := range req.IPAddresses {
  113. got[ip.String()] = true
  114. }
  115. if !reflect.DeepEqual(want, got) {
  116. return errors.Errorf("IP Addresses claim failed - got %v, want %v", req.IPAddresses, v)
  117. }
  118. return nil
  119. }
  120. // validityValidator validates the certificate temporal validity settings.
  121. type validityValidator struct {
  122. min time.Duration
  123. max time.Duration
  124. }
  125. // newValidityValidator return a new validity validator.
  126. func newValidityValidator(min, max time.Duration) *validityValidator {
  127. return &validityValidator{min: min, max: max}
  128. }
  129. // Validate validates the certificate temporal validity settings.
  130. func (v *validityValidator) Valid(crt *x509.Certificate) error {
  131. var (
  132. na = crt.NotAfter
  133. nb = crt.NotBefore
  134. d = na.Sub(nb)
  135. now = time.Now()
  136. )
  137. if na.Before(now) {
  138. return errors.Errorf("NotAfter: %v cannot be in the past", na)
  139. }
  140. if na.Before(nb) {
  141. return errors.Errorf("NotAfter: %v cannot be before NotBefore: %v", na, nb)
  142. }
  143. if d < v.min {
  144. return errors.Errorf("requested duration of %v is less than the authorized minimum certificate duration of %v",
  145. d, v.min)
  146. }
  147. if d > v.max {
  148. return errors.Errorf("requested duration of %v is more than the authorized maximum certificate duration of %v",
  149. d, v.max)
  150. }
  151. return nil
  152. }
  153. var (
  154. stepOIDRoot = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 37476, 9000, 64}
  155. stepOIDProvisioner = append(asn1.ObjectIdentifier(nil), append(stepOIDRoot, 1)...)
  156. )
  157. type stepProvisionerASN1 struct {
  158. Type int
  159. Name []byte
  160. CredentialID []byte
  161. }
  162. type provisionerExtensionOption struct {
  163. Type int
  164. Name string
  165. CredentialID string
  166. }
  167. func newProvisionerExtensionOption(typ Type, name, credentialID string) *provisionerExtensionOption {
  168. return &provisionerExtensionOption{
  169. Type: int(typ),
  170. Name: name,
  171. CredentialID: credentialID,
  172. }
  173. }
  174. func (o *provisionerExtensionOption) Option(Options) x509util.WithOption {
  175. return func(p x509util.Profile) error {
  176. crt := p.Subject()
  177. ext, err := createProvisionerExtension(o.Type, o.Name, o.CredentialID)
  178. if err != nil {
  179. return err
  180. }
  181. crt.ExtraExtensions = append(crt.ExtraExtensions, ext)
  182. return nil
  183. }
  184. }
  185. func createProvisionerExtension(typ int, name, credentialID string) (pkix.Extension, error) {
  186. b, err := asn1.Marshal(stepProvisionerASN1{
  187. Type: typ,
  188. Name: []byte(name),
  189. CredentialID: []byte(credentialID),
  190. })
  191. if err != nil {
  192. return pkix.Extension{}, errors.Wrapf(err, "error marshaling provisioner extension")
  193. }
  194. return pkix.Extension{
  195. Id: stepOIDProvisioner,
  196. Critical: false,
  197. Value: b,
  198. }, nil
  199. }