acme.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "fmt"
  6. "net"
  7. "time"
  8. "github.com/pkg/errors"
  9. )
  10. // ACME is the acme provisioner type, an entity that can authorize the ACME
  11. // provisioning flow.
  12. type ACME struct {
  13. *base
  14. ID string `json:"-"`
  15. Type string `json:"type"`
  16. Name string `json:"name"`
  17. ForceCN bool `json:"forceCN,omitempty"`
  18. // RequireEAB makes the provisioner require ACME EAB to be provided
  19. // by clients when creating a new Account. If set to true, the provided
  20. // EAB will be verified. If set to false and an EAB is provided, it is
  21. // not verified. Defaults to false.
  22. RequireEAB bool `json:"requireEAB,omitempty"`
  23. Claims *Claims `json:"claims,omitempty"`
  24. Options *Options `json:"options,omitempty"`
  25. ctl *Controller
  26. }
  27. // GetID returns the provisioner unique identifier.
  28. func (p ACME) GetID() string {
  29. if p.ID != "" {
  30. return p.ID
  31. }
  32. return p.GetIDForToken()
  33. }
  34. // GetIDForToken returns an identifier that will be used to load the provisioner
  35. // from a token.
  36. func (p *ACME) GetIDForToken() string {
  37. return "acme/" + p.Name
  38. }
  39. // GetTokenID returns the identifier of the token.
  40. func (p *ACME) GetTokenID(ott string) (string, error) {
  41. return "", errors.New("acme provisioner does not implement GetTokenID")
  42. }
  43. // GetName returns the name of the provisioner.
  44. func (p *ACME) GetName() string {
  45. return p.Name
  46. }
  47. // GetType returns the type of provisioner.
  48. func (p *ACME) GetType() Type {
  49. return TypeACME
  50. }
  51. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  52. func (p *ACME) GetEncryptedKey() (string, string, bool) {
  53. return "", "", false
  54. }
  55. // GetOptions returns the configured provisioner options.
  56. func (p *ACME) GetOptions() *Options {
  57. return p.Options
  58. }
  59. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  60. // the provisioner.
  61. func (p *ACME) DefaultTLSCertDuration() time.Duration {
  62. return p.ctl.Claimer.DefaultTLSCertDuration()
  63. }
  64. // Init initializes and validates the fields of an ACME type.
  65. func (p *ACME) Init(config Config) (err error) {
  66. switch {
  67. case p.Type == "":
  68. return errors.New("provisioner type cannot be empty")
  69. case p.Name == "":
  70. return errors.New("provisioner name cannot be empty")
  71. }
  72. p.ctl, err = NewController(p, p.Claims, config, p.Options)
  73. return
  74. }
  75. // ACMEIdentifierType encodes ACME Identifier types
  76. type ACMEIdentifierType string
  77. const (
  78. // IP is the ACME ip identifier type
  79. IP ACMEIdentifierType = "ip"
  80. // DNS is the ACME dns identifier type
  81. DNS ACMEIdentifierType = "dns"
  82. )
  83. // ACMEIdentifier encodes ACME Order Identifiers
  84. type ACMEIdentifier struct {
  85. Type ACMEIdentifierType
  86. Value string
  87. }
  88. // AuthorizeOrderIdentifier verifies the provisioner is allowed to issue a
  89. // certificate for an ACME Order Identifier.
  90. func (p *ACME) AuthorizeOrderIdentifier(ctx context.Context, identifier ACMEIdentifier) error {
  91. x509Policy := p.ctl.getPolicy().getX509()
  92. // identifier is allowed if no policy is configured
  93. if x509Policy == nil {
  94. return nil
  95. }
  96. // assuming only valid identifiers (IP or DNS) are provided
  97. var err error
  98. switch identifier.Type {
  99. case IP:
  100. err = x509Policy.IsIPAllowed(net.ParseIP(identifier.Value))
  101. case DNS:
  102. err = x509Policy.IsDNSAllowed(identifier.Value)
  103. default:
  104. err = fmt.Errorf("invalid ACME identifier type '%s' provided", identifier.Type)
  105. }
  106. return err
  107. }
  108. // AuthorizeSign does not do any validation, because all validation is handled
  109. // in the ACME protocol. This method returns a list of modifiers / constraints
  110. // on the resulting certificate.
  111. func (p *ACME) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  112. opts := []SignOption{
  113. p,
  114. // modifiers / withOptions
  115. newProvisionerExtensionOption(TypeACME, p.Name, ""),
  116. newForceCNOption(p.ForceCN),
  117. profileDefaultDuration(p.ctl.Claimer.DefaultTLSCertDuration()),
  118. // validators
  119. defaultPublicKeyValidator{},
  120. newValidityValidator(p.ctl.Claimer.MinTLSCertDuration(), p.ctl.Claimer.MaxTLSCertDuration()),
  121. newX509NamePolicyValidator(p.ctl.getPolicy().getX509()),
  122. }
  123. return opts, nil
  124. }
  125. // AuthorizeRevoke is called just before the certificate is to be revoked by
  126. // the CA. It can be used to authorize revocation of a certificate. It
  127. // currently is a no-op.
  128. // TODO(hs): add configuration option that toggles revocation? Or change function signature to make it more useful?
  129. // Or move certain logic out of the Revoke API to here? Would likely involve some more stuff in the ctx.
  130. func (p *ACME) AuthorizeRevoke(ctx context.Context, token string) error {
  131. return nil
  132. }
  133. // AuthorizeRenew returns an error if the renewal is disabled.
  134. // NOTE: This method does not actually validate the certificate or check it's
  135. // revocation status. Just confirms that the provisioner that created the
  136. // certificate was configured to allow renewals.
  137. func (p *ACME) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
  138. return p.ctl.AuthorizeRenew(ctx, cert)
  139. }