acme.go 11 KB

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