est.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/x509"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/linkedca"
  9. "github.com/smallstep/certificates/authority/provisioner/est"
  10. "github.com/smallstep/certificates/internal/httptransport"
  11. )
  12. // EST is the EST provisioner type, an entity that can authorize the EST flow.
  13. type EST struct {
  14. *base
  15. ID string `json:"-"`
  16. Type string `json:"type"`
  17. Name string `json:"name"`
  18. ForceCN bool `json:"forceCN,omitempty"`
  19. MinimumPublicKeyLength int `json:"minimumPublicKeyLength,omitempty"`
  20. // Authentication configures accepted client authentication. At
  21. // least one method is required; RFC 7030 3.2.3 permits requiring
  22. // HTTP authentication in addition to TLS client authentication.
  23. Authentication est.Authentication `json:"authentication"`
  24. // Operations enables EST operations. Defaults to cacerts,
  25. // csrattrs, simpleenroll and simplereenroll.
  26. Operations []est.Operation `json:"operations,omitempty"`
  27. CACerts est.CACerts `json:"cacerts,omitempty"`
  28. CSRAttributes *est.CSRAttributes `json:"csrAttributes,omitempty"`
  29. // ProofOfPossession controls tls-unique identity/PoP linking
  30. // (RFC 7030 3.5): disabled | optional | required.
  31. ProofOfPossession est.PoPMode `json:"proofOfPossession,omitempty"`
  32. DummyBool *bool
  33. DummyString string
  34. // EnableTLSClientCertificate *bool `json:"enableTlsClientCertificate,omitempty"`
  35. // ForwardedTLSClientCertHeader string `json:"forwardedTlsClientCertHeader,omitempty"`
  36. // EnableHTTPBasicAuth *bool `json:"enableHTTPBasicAuth,omitempty"`
  37. // BasicAuthUsername string `json:"basicAuthUsername,omitempty"`
  38. // BasicAuthPassword string `json:"basicAuthPassword,omitempty"`
  39. // ClientCertificateRoots []byte `json:"clientCertificateRoots,omitempty"`
  40. Options *Options `json:"options,omitempty"`
  41. Claims *Claims `json:"claims,omitempty"`
  42. ctl *Controller
  43. signer crypto.Signer
  44. signerCertificate *x509.Certificate
  45. challengeValidationController *challengeValidationController
  46. clientCertificateRootPool *x509.CertPool
  47. }
  48. // GetID returns the provisioner unique identifier.
  49. func (s *EST) GetID() string {
  50. if s.ID != "" {
  51. return s.ID
  52. }
  53. return s.GetIDForToken()
  54. }
  55. // GetIDForToken returns an identifier that will be used to load the provisioner from a token.
  56. func (s *EST) GetIDForToken() string {
  57. return "est/" + s.Name
  58. }
  59. // GetName returns the name of the provisioner.
  60. func (s *EST) GetName() string {
  61. return s.Name
  62. }
  63. // GetType returns the type of provisioner.
  64. func (s *EST) GetType() Type {
  65. return TypeEST
  66. }
  67. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  68. func (s *EST) GetEncryptedKey() (string, string, bool) {
  69. return "", "", false
  70. }
  71. // GetTokenID returns the identifier of the token. This provisioner does not support tokens.
  72. func (s *EST) GetTokenID(string) (string, error) {
  73. return "", ErrTokenFlowNotSupported
  74. }
  75. // GetOptions returns the configured provisioner options.
  76. func (s *EST) GetOptions() *Options {
  77. return s.Options
  78. }
  79. // DefaultTLSCertDuration returns the default TLS cert duration enforced by the provisioner.
  80. func (s *EST) DefaultTLSCertDuration() time.Duration {
  81. return s.ctl.Claimer.DefaultTLSCertDuration()
  82. }
  83. // newChallengeValidationController creates a new challengeValidationController
  84. // that performs challenge validation through webhooks.
  85. func newESTChallengeValidationController(client HTTPClient, tw httptransport.Wrapper, webhooks []*Webhook) *challengeValidationController {
  86. estHooks := []*Webhook{}
  87. for _, wh := range webhooks {
  88. if wh.Kind != linkedca.Webhook_ESTCHALLENGE.String() {
  89. continue
  90. }
  91. estHooks = append(estHooks, wh)
  92. }
  93. return &challengeValidationController{
  94. client: client,
  95. wrapTransport: tw,
  96. webhooks: estHooks,
  97. }
  98. }
  99. // Init initializes and validates the fields of an EST type.
  100. func (s *EST) Init(config Config) (err error) {
  101. switch {
  102. case s.Type == "":
  103. return errors.New("provisioner type cannot be empty")
  104. case s.Name == "":
  105. return errors.New("provisioner name cannot be empty")
  106. }
  107. if s.MinimumPublicKeyLength == 0 {
  108. s.MinimumPublicKeyLength = 2048
  109. }
  110. if s.MinimumPublicKeyLength%8 != 0 {
  111. return errors.Errorf("%d bits is not exactly divisible by 8", s.MinimumPublicKeyLength)
  112. }
  113. // Prepare the EST challenge validator
  114. s.challengeValidationController = newESTChallengeValidationController(
  115. config.WebhookClient,
  116. config.WrapTransport,
  117. s.GetOptions().GetWebhooks(),
  118. )
  119. // if err := s.parseClientCertificateRoots(); err != nil {
  120. // return err
  121. // }
  122. // if err := s.normalizeAuthConfig(); err != nil {
  123. // return err
  124. // }
  125. s.ctl, err = NewController(s, s.Claims, config, s.Options)
  126. return err
  127. }
  128. // AuthorizeSign does not do any verification; main validation is in the EST protocol.
  129. func (s *EST) AuthorizeSign(context.Context, string) ([]SignOption, error) {
  130. return []SignOption{
  131. s,
  132. newProvisionerExtensionOption(TypeEST, s.Name, "").WithControllerOptions(s.ctl),
  133. newForceCNOption(s.ForceCN),
  134. profileDefaultDuration(s.ctl.Claimer.DefaultTLSCertDuration()),
  135. newPublicKeyMinimumLengthValidator(s.MinimumPublicKeyLength),
  136. newValidityValidator(s.ctl.Claimer.MinTLSCertDuration(), s.ctl.Claimer.MaxTLSCertDuration()),
  137. newX509NamePolicyValidator(s.ctl.getPolicy().getX509()),
  138. s.ctl.newWebhookController(nil, linkedca.Webhook_X509),
  139. }, nil
  140. }
  141. // GetCSRAttributes returns the CSR attributes to signal to clients.
  142. func (s *EST) GetCSRAttributes(context.Context) ([]byte, error) {
  143. return nil, nil // TODO(hs): refactor
  144. }