acme.go 11 KB

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