acme.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "encoding/pem"
  6. "fmt"
  7. "net"
  8. "strings"
  9. "time"
  10. "github.com/pkg/errors"
  11. )
  12. // ACMEChallenge represents the supported acme challenges.
  13. type ACMEChallenge string
  14. //nolint:stylecheck,revive // better names
  15. const (
  16. // HTTP_01 is the http-01 ACME challenge.
  17. HTTP_01 ACMEChallenge = "http-01"
  18. // DNS_01 is the dns-01 ACME challenge.
  19. DNS_01 ACMEChallenge = "dns-01"
  20. // TLS_ALPN_01 is the tls-alpn-01 ACME challenge.
  21. TLS_ALPN_01 ACMEChallenge = "tls-alpn-01"
  22. // DEVICE_ATTEST_01 is the device-attest-01 ACME challenge.
  23. DEVICE_ATTEST_01 ACMEChallenge = "device-attest-01"
  24. )
  25. // String returns a normalized version of the challenge.
  26. func (c ACMEChallenge) String() string {
  27. return strings.ToLower(string(c))
  28. }
  29. // Validate returns an error if the acme challenge is not a valid one.
  30. func (c ACMEChallenge) Validate() error {
  31. switch ACMEChallenge(c.String()) {
  32. case HTTP_01, DNS_01, TLS_ALPN_01, DEVICE_ATTEST_01:
  33. return nil
  34. default:
  35. return fmt.Errorf("acme challenge %q is not supported", c)
  36. }
  37. }
  38. // ACMEAttestationFormat represents the format used on a device-attest-01
  39. // challenge.
  40. type ACMEAttestationFormat string
  41. const (
  42. // APPLE is the format used to enable device-attest-01 on apple devices.
  43. APPLE ACMEAttestationFormat = "apple"
  44. // STEP is the format used to enable device-attest-01 on devices that
  45. // provide attestation certificates like the PIV interface on YubiKeys.
  46. //
  47. // TODO(mariano): should we rename this to something else.
  48. STEP ACMEAttestationFormat = "step"
  49. // TPM is the format used to enable device-attest-01 on TPMs.
  50. TPM ACMEAttestationFormat = "tpm"
  51. )
  52. // String returns a normalized version of the attestation format.
  53. func (f ACMEAttestationFormat) String() string {
  54. return strings.ToLower(string(f))
  55. }
  56. // Validate returns an error if the attestation format is not a valid one.
  57. func (f ACMEAttestationFormat) Validate() error {
  58. switch ACMEAttestationFormat(f.String()) {
  59. case APPLE, STEP, TPM:
  60. return nil
  61. default:
  62. return fmt.Errorf("acme attestation format %q is not supported", f)
  63. }
  64. }
  65. // ACME is the acme provisioner type, an entity that can authorize the ACME
  66. // provisioning flow.
  67. type ACME struct {
  68. *base
  69. ID string `json:"-"`
  70. Type string `json:"type"`
  71. Name string `json:"name"`
  72. ForceCN bool `json:"forceCN,omitempty"`
  73. // RequireEAB makes the provisioner require ACME EAB to be provided
  74. // by clients when creating a new Account. If set to true, the provided
  75. // EAB will be verified. If set to false and an EAB is provided, it is
  76. // not verified. Defaults to false.
  77. RequireEAB bool `json:"requireEAB,omitempty"`
  78. // Challenges contains the enabled challenges for this provisioner. If this
  79. // value is not set the default http-01, dns-01 and tls-alpn-01 challenges
  80. // will be enabled, device-attest-01 will be disabled.
  81. Challenges []ACMEChallenge `json:"challenges,omitempty"`
  82. // AttestationFormats contains the enabled attestation formats for this
  83. // provisioner. If this value is not set the default apple, step and tpm
  84. // will be used.
  85. AttestationFormats []ACMEAttestationFormat `json:"attestationFormats,omitempty"`
  86. // AttestationRoots contains a bundle of root certificates in PEM format
  87. // that will be used to verify the attestation certificates. If provided,
  88. // this bundle will be used even for well-known CAs like Apple and Yubico.
  89. AttestationRoots []byte `json:"attestationRoots,omitempty"`
  90. Claims *Claims `json:"claims,omitempty"`
  91. Options *Options `json:"options,omitempty"`
  92. attestationRootPool *x509.CertPool
  93. ctl *Controller
  94. }
  95. // GetID returns the provisioner unique identifier.
  96. func (p ACME) GetID() string {
  97. if p.ID != "" {
  98. return p.ID
  99. }
  100. return p.GetIDForToken()
  101. }
  102. // GetIDForToken returns an identifier that will be used to load the provisioner
  103. // from a token.
  104. func (p *ACME) GetIDForToken() string {
  105. return "acme/" + p.Name
  106. }
  107. // GetTokenID returns the identifier of the token.
  108. func (p *ACME) GetTokenID(ott string) (string, error) {
  109. return "", errors.New("acme provisioner does not implement GetTokenID")
  110. }
  111. // GetName returns the name of the provisioner.
  112. func (p *ACME) GetName() string {
  113. return p.Name
  114. }
  115. // GetType returns the type of provisioner.
  116. func (p *ACME) GetType() Type {
  117. return TypeACME
  118. }
  119. // GetEncryptedKey returns the base provisioner encrypted key if it's defined.
  120. func (p *ACME) GetEncryptedKey() (string, string, bool) {
  121. return "", "", false
  122. }
  123. // GetOptions returns the configured provisioner options.
  124. func (p *ACME) GetOptions() *Options {
  125. return p.Options
  126. }
  127. // DefaultTLSCertDuration returns the default TLS cert duration enforced by
  128. // the provisioner.
  129. func (p *ACME) DefaultTLSCertDuration() time.Duration {
  130. return p.ctl.Claimer.DefaultTLSCertDuration()
  131. }
  132. // Init initializes and validates the fields of an ACME type.
  133. func (p *ACME) Init(config Config) (err error) {
  134. switch {
  135. case p.Type == "":
  136. return errors.New("provisioner type cannot be empty")
  137. case p.Name == "":
  138. return errors.New("provisioner name cannot be empty")
  139. }
  140. for _, c := range p.Challenges {
  141. if err := c.Validate(); err != nil {
  142. return err
  143. }
  144. }
  145. for _, f := range p.AttestationFormats {
  146. if err := f.Validate(); err != nil {
  147. return err
  148. }
  149. }
  150. // Parse attestation roots.
  151. // The pool will be nil if the there are not roots.
  152. if rest := p.AttestationRoots; len(rest) > 0 {
  153. var block *pem.Block
  154. var hasCert bool
  155. p.attestationRootPool = x509.NewCertPool()
  156. for rest != nil {
  157. block, rest = pem.Decode(rest)
  158. if block == nil {
  159. break
  160. }
  161. cert, err := x509.ParseCertificate(block.Bytes)
  162. if err != nil {
  163. return errors.New("error parsing attestationRoots: malformed certificate")
  164. }
  165. p.attestationRootPool.AddCert(cert)
  166. hasCert = true
  167. }
  168. if !hasCert {
  169. return errors.New("error parsing attestationRoots: no certificates found")
  170. }
  171. }
  172. p.ctl, err = NewController(p, p.Claims, config, p.Options)
  173. return
  174. }
  175. // ACMEIdentifierType encodes ACME Identifier types
  176. type ACMEIdentifierType string
  177. const (
  178. // IP is the ACME ip identifier type
  179. IP ACMEIdentifierType = "ip"
  180. // DNS is the ACME dns identifier type
  181. DNS ACMEIdentifierType = "dns"
  182. )
  183. // ACMEIdentifier encodes ACME Order Identifiers
  184. type ACMEIdentifier struct {
  185. Type ACMEIdentifierType
  186. Value string
  187. }
  188. // AuthorizeOrderIdentifier verifies the provisioner is allowed to issue a
  189. // certificate for an ACME Order Identifier.
  190. func (p *ACME) AuthorizeOrderIdentifier(ctx context.Context, identifier ACMEIdentifier) error {
  191. x509Policy := p.ctl.getPolicy().getX509()
  192. // identifier is allowed if no policy is configured
  193. if x509Policy == nil {
  194. return nil
  195. }
  196. // assuming only valid identifiers (IP or DNS) are provided
  197. var err error
  198. switch identifier.Type {
  199. case IP:
  200. err = x509Policy.IsIPAllowed(net.ParseIP(identifier.Value))
  201. case DNS:
  202. err = x509Policy.IsDNSAllowed(identifier.Value)
  203. default:
  204. err = fmt.Errorf("invalid ACME identifier type '%s' provided", identifier.Type)
  205. }
  206. return err
  207. }
  208. // AuthorizeSign does not do any validation, because all validation is handled
  209. // in the ACME protocol. This method returns a list of modifiers / constraints
  210. // on the resulting certificate.
  211. func (p *ACME) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  212. opts := []SignOption{
  213. p,
  214. // modifiers / withOptions
  215. newProvisionerExtensionOption(TypeACME, p.Name, ""),
  216. newForceCNOption(p.ForceCN),
  217. profileDefaultDuration(p.ctl.Claimer.DefaultTLSCertDuration()),
  218. // validators
  219. defaultPublicKeyValidator{},
  220. newValidityValidator(p.ctl.Claimer.MinTLSCertDuration(), p.ctl.Claimer.MaxTLSCertDuration()),
  221. newX509NamePolicyValidator(p.ctl.getPolicy().getX509()),
  222. }
  223. return opts, nil
  224. }
  225. // AuthorizeRevoke is called just before the certificate is to be revoked by
  226. // the CA. It can be used to authorize revocation of a certificate. It
  227. // currently is a no-op.
  228. // TODO(hs): add configuration option that toggles revocation? Or change function signature to make it more useful?
  229. // Or move certain logic out of the Revoke API to here? Would likely involve some more stuff in the ctx.
  230. func (p *ACME) AuthorizeRevoke(ctx context.Context, token string) error {
  231. return nil
  232. }
  233. // AuthorizeRenew returns an error if the renewal is disabled.
  234. // NOTE: This method does not actually validate the certificate or check it's
  235. // revocation status. Just confirms that the provisioner that created the
  236. // certificate was configured to allow renewals.
  237. func (p *ACME) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
  238. return p.ctl.AuthorizeRenew(ctx, cert)
  239. }
  240. // IsChallengeEnabled checks if the given challenge is enabled. By default
  241. // http-01, dns-01 and tls-alpn-01 are enabled, to disable any of them the
  242. // Challenge provisioner property should have at least one element.
  243. func (p *ACME) IsChallengeEnabled(ctx context.Context, challenge ACMEChallenge) bool {
  244. enabledChallenges := []ACMEChallenge{
  245. HTTP_01, DNS_01, TLS_ALPN_01,
  246. }
  247. if len(p.Challenges) > 0 {
  248. enabledChallenges = p.Challenges
  249. }
  250. for _, ch := range enabledChallenges {
  251. if strings.EqualFold(string(ch), string(challenge)) {
  252. return true
  253. }
  254. }
  255. return false
  256. }
  257. // IsAttestationFormatEnabled checks if the given attestation format is enabled.
  258. // By default apple, step and tpm are enabled, to disable any of them the
  259. // AttestationFormat provisioner property should have at least one element.
  260. func (p *ACME) IsAttestationFormatEnabled(ctx context.Context, format ACMEAttestationFormat) bool {
  261. enabledFormats := []ACMEAttestationFormat{
  262. APPLE, STEP, TPM,
  263. }
  264. if len(p.AttestationFormats) > 0 {
  265. enabledFormats = p.AttestationFormats
  266. }
  267. for _, f := range enabledFormats {
  268. if strings.EqualFold(string(f), string(format)) {
  269. return true
  270. }
  271. }
  272. return false
  273. }
  274. // GetAttestationRoots returns certificate pool with the configured attestation
  275. // roots and reports if the pool contains at least one certificate.
  276. //
  277. // TODO(hs): we may not want to expose the root pool like this; call into an
  278. // interface function instead to authorize?
  279. func (p *ACME) GetAttestationRoots() (*x509.CertPool, bool) {
  280. return p.attestationRootPool, p.attestationRootPool != nil
  281. }