common.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package acme
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "time"
  6. "github.com/smallstep/certificates/attestation"
  7. "github.com/smallstep/certificates/attestation/cmw"
  8. "github.com/smallstep/certificates/authority"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. )
  11. // Clock that returns time in UTC rounded to seconds.
  12. type Clock struct{}
  13. // Now returns the UTC time rounded to seconds.
  14. func (c *Clock) Now() time.Time {
  15. return time.Now().UTC().Truncate(time.Second)
  16. }
  17. var clock Clock
  18. // CertificateAuthority is the interface implemented by a CA authority.
  19. type CertificateAuthority interface {
  20. SignWithContext(ctx context.Context, cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  21. AreSANsAllowed(ctx context.Context, sans []string) error
  22. IsRevoked(sn string) (bool, error)
  23. Revoke(context.Context, *authority.RevokeOptions) error
  24. LoadProvisionerByName(string) (provisioner.Interface, error)
  25. GetBackdate() *time.Duration
  26. }
  27. // NewContext adds the given acme components to the context.
  28. func NewContext(ctx context.Context, db DB, client Client, linker Linker, fn PrerequisitesChecker) context.Context {
  29. ctx = NewDatabaseContext(ctx, db)
  30. ctx = NewClientContext(ctx, client)
  31. ctx = NewLinkerContext(ctx, linker)
  32. // Prerequisite checker is optional.
  33. if fn != nil {
  34. ctx = NewPrerequisitesCheckerContext(ctx, fn)
  35. }
  36. return ctx
  37. }
  38. // PrerequisitesChecker is a function that checks if all prerequisites for
  39. // serving ACME are met by the CA configuration.
  40. type PrerequisitesChecker func(ctx context.Context) (bool, error)
  41. // DefaultPrerequisitesChecker is the default PrerequisiteChecker and returns
  42. // always true.
  43. func DefaultPrerequisitesChecker(context.Context) (bool, error) {
  44. return true, nil
  45. }
  46. type prerequisitesKey struct{}
  47. // NewPrerequisitesCheckerContext adds the given PrerequisitesChecker to the
  48. // context.
  49. func NewPrerequisitesCheckerContext(ctx context.Context, fn PrerequisitesChecker) context.Context {
  50. return context.WithValue(ctx, prerequisitesKey{}, fn)
  51. }
  52. // PrerequisitesCheckerFromContext returns the PrerequisitesChecker in the
  53. // context.
  54. func PrerequisitesCheckerFromContext(ctx context.Context) (PrerequisitesChecker, bool) {
  55. fn, ok := ctx.Value(prerequisitesKey{}).(PrerequisitesChecker)
  56. return fn, ok && fn != nil
  57. }
  58. // Provisioner is an interface that implements a subset of the provisioner.Interface --
  59. // only those methods required by the ACME api/authority.
  60. type Provisioner interface {
  61. AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  62. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  63. AuthorizeRevoke(ctx context.Context, token string) error
  64. IsChallengeEnabled(ctx context.Context, challenge provisioner.ACMEChallenge) bool
  65. IsAttestationFormatEnabled(ctx context.Context, format provisioner.ACMEAttestationFormat) bool
  66. GetAttestationRoots() (*x509.CertPool, bool)
  67. GetID() string
  68. GetName() string
  69. DefaultTLSCertDuration() time.Duration
  70. GetOptions() *provisioner.Options
  71. GetRemoteAttestation() *provisioner.ACMERemoteAttestation
  72. IsEABRequired() bool
  73. VerifyRemoteAttestation(ctx context.Context, record *cmw.Record, defaultAudience string) (*attestation.Result, []byte, error)
  74. }
  75. type provisionerKey struct{}
  76. // NewProvisionerContext adds the given provisioner to the context.
  77. func NewProvisionerContext(ctx context.Context, v Provisioner) context.Context {
  78. return context.WithValue(ctx, provisionerKey{}, v)
  79. }
  80. // ProvisionerFromContext returns the current provisioner from the given context.
  81. func ProvisionerFromContext(ctx context.Context) (v Provisioner, ok bool) {
  82. v, ok = ctx.Value(provisionerKey{}).(Provisioner)
  83. return
  84. }
  85. // MustProvisionerFromContext returns the current provisioner from the given context.
  86. // It will panic if it's not in the context.
  87. func MustProvisionerFromContext(ctx context.Context) Provisioner {
  88. var (
  89. v Provisioner
  90. ok bool
  91. )
  92. if v, ok = ProvisionerFromContext(ctx); !ok {
  93. panic("acme provisioner is not the context")
  94. }
  95. return v
  96. }
  97. // MockProvisioner for testing
  98. type MockProvisioner struct {
  99. Mret1 any
  100. Merr error
  101. MgetID func() string
  102. MgetName func() string
  103. MauthorizeOrderIdentifier func(ctx context.Context, identifier provisioner.ACMEIdentifier) error
  104. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  105. MauthorizeRevoke func(ctx context.Context, token string) error
  106. MisChallengeEnabled func(ctx context.Context, challenge provisioner.ACMEChallenge) bool
  107. MisAttFormatEnabled func(ctx context.Context, format provisioner.ACMEAttestationFormat) bool
  108. MgetAttestationRoots func() (*x509.CertPool, bool)
  109. MdefaultTLSCertDuration func() time.Duration
  110. MgetOptions func() *provisioner.Options
  111. MgetRemoteAttestation func() *provisioner.ACMERemoteAttestation
  112. MisEABRequired func() bool
  113. MverifyRemoteAttestation func(ctx context.Context, record *cmw.Record, defaultAudience string) (*attestation.Result, []byte, error)
  114. }
  115. // GetName mock
  116. func (m *MockProvisioner) GetName() string {
  117. if m.MgetName != nil {
  118. return m.MgetName()
  119. }
  120. return m.Mret1.(string)
  121. }
  122. // AuthorizeOrderIdentifier mock
  123. func (m *MockProvisioner) AuthorizeOrderIdentifier(ctx context.Context, identifier provisioner.ACMEIdentifier) error {
  124. if m.MauthorizeOrderIdentifier != nil {
  125. return m.MauthorizeOrderIdentifier(ctx, identifier)
  126. }
  127. return m.Merr
  128. }
  129. // AuthorizeSign mock
  130. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  131. if m.MauthorizeSign != nil {
  132. return m.MauthorizeSign(ctx, ott)
  133. }
  134. return m.Mret1.([]provisioner.SignOption), m.Merr
  135. }
  136. // AuthorizeRevoke mock
  137. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, token string) error {
  138. if m.MauthorizeRevoke != nil {
  139. return m.MauthorizeRevoke(ctx, token)
  140. }
  141. return m.Merr
  142. }
  143. // IsChallengeEnabled mock
  144. func (m *MockProvisioner) IsChallengeEnabled(ctx context.Context, challenge provisioner.ACMEChallenge) bool {
  145. if m.MisChallengeEnabled != nil {
  146. return m.MisChallengeEnabled(ctx, challenge)
  147. }
  148. return m.Merr == nil
  149. }
  150. // IsAttestationFormatEnabled mock
  151. func (m *MockProvisioner) IsAttestationFormatEnabled(ctx context.Context, format provisioner.ACMEAttestationFormat) bool {
  152. if m.MisAttFormatEnabled != nil {
  153. return m.MisAttFormatEnabled(ctx, format)
  154. }
  155. return m.Merr == nil
  156. }
  157. func (m *MockProvisioner) GetAttestationRoots() (*x509.CertPool, bool) {
  158. if m.MgetAttestationRoots != nil {
  159. return m.MgetAttestationRoots()
  160. }
  161. return m.Mret1.(*x509.CertPool), m.Mret1 != nil
  162. }
  163. // DefaultTLSCertDuration mock
  164. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  165. if m.MdefaultTLSCertDuration != nil {
  166. return m.MdefaultTLSCertDuration()
  167. }
  168. return m.Mret1.(time.Duration)
  169. }
  170. // GetOptions mock
  171. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  172. if m.MgetOptions != nil {
  173. return m.MgetOptions()
  174. }
  175. return m.Mret1.(*provisioner.Options)
  176. }
  177. // GetRemoteAttestation mock
  178. func (m *MockProvisioner) GetRemoteAttestation() *provisioner.ACMERemoteAttestation {
  179. if m.MgetRemoteAttestation != nil {
  180. return m.MgetRemoteAttestation()
  181. }
  182. return nil
  183. }
  184. // IsEABRequired mock
  185. func (m *MockProvisioner) IsEABRequired() bool {
  186. if m.MisEABRequired != nil {
  187. return m.MisEABRequired()
  188. }
  189. return false
  190. }
  191. // VerifyRemoteAttestation mock
  192. func (m *MockProvisioner) VerifyRemoteAttestation(ctx context.Context, record *cmw.Record, defaultAudience string) (*attestation.Result, []byte, error) {
  193. if m.MverifyRemoteAttestation != nil {
  194. return m.MverifyRemoteAttestation(ctx, record, defaultAudience)
  195. }
  196. return nil, nil, m.Merr
  197. }
  198. // GetID mock
  199. func (m *MockProvisioner) GetID() string {
  200. if m.MgetID != nil {
  201. return m.MgetID()
  202. }
  203. return m.Mret1.(string)
  204. }