common.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package acme
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "net/url"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "go.step.sm/crypto/jose"
  10. "go.step.sm/crypto/randutil"
  11. )
  12. // Provisioner is an interface that implements a subset of the provisioner.Interface --
  13. // only those methods required by the ACME api/authority.
  14. type Provisioner interface {
  15. AuthorizeSign(ctx context.Context, token string) ([]provisioner.SignOption, error)
  16. GetName() string
  17. DefaultTLSCertDuration() time.Duration
  18. GetOptions() *provisioner.Options
  19. }
  20. // MockProvisioner for testing
  21. type MockProvisioner struct {
  22. Mret1 interface{}
  23. Merr error
  24. MgetName func() string
  25. MauthorizeSign func(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  26. MdefaultTLSCertDuration func() time.Duration
  27. MgetOptions func() *provisioner.Options
  28. }
  29. // GetName mock
  30. func (m *MockProvisioner) GetName() string {
  31. if m.MgetName != nil {
  32. return m.MgetName()
  33. }
  34. return m.Mret1.(string)
  35. }
  36. // AuthorizeSign mock
  37. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  38. if m.MauthorizeSign != nil {
  39. return m.MauthorizeSign(ctx, ott)
  40. }
  41. return m.Mret1.([]provisioner.SignOption), m.Merr
  42. }
  43. // DefaultTLSCertDuration mock
  44. func (m *MockProvisioner) DefaultTLSCertDuration() time.Duration {
  45. if m.MdefaultTLSCertDuration != nil {
  46. return m.MdefaultTLSCertDuration()
  47. }
  48. return m.Mret1.(time.Duration)
  49. }
  50. func (m *MockProvisioner) GetOptions() *provisioner.Options {
  51. if m.MgetOptions != nil {
  52. return m.MgetOptions()
  53. }
  54. return m.Mret1.(*provisioner.Options)
  55. }
  56. // ContextKey is the key type for storing and searching for ACME request
  57. // essentials in the context of a request.
  58. type ContextKey string
  59. const (
  60. // AccContextKey account key
  61. AccContextKey = ContextKey("acc")
  62. // BaseURLContextKey baseURL key
  63. BaseURLContextKey = ContextKey("baseURL")
  64. // JwsContextKey jws key
  65. JwsContextKey = ContextKey("jws")
  66. // JwkContextKey jwk key
  67. JwkContextKey = ContextKey("jwk")
  68. // PayloadContextKey payload key
  69. PayloadContextKey = ContextKey("payload")
  70. // ProvisionerContextKey provisioner key
  71. ProvisionerContextKey = ContextKey("provisioner")
  72. )
  73. // AccountFromContext searches the context for an ACME account. Returns the
  74. // account or an error.
  75. func AccountFromContext(ctx context.Context) (*Account, error) {
  76. val, ok := ctx.Value(AccContextKey).(*Account)
  77. if !ok || val == nil {
  78. return nil, AccountDoesNotExistErr(nil)
  79. }
  80. return val, nil
  81. }
  82. // BaseURLFromContext returns the baseURL if one is stored in the context.
  83. func BaseURLFromContext(ctx context.Context) *url.URL {
  84. val, ok := ctx.Value(BaseURLContextKey).(*url.URL)
  85. if !ok || val == nil {
  86. return nil
  87. }
  88. return val
  89. }
  90. // JwkFromContext searches the context for a JWK. Returns the JWK or an error.
  91. func JwkFromContext(ctx context.Context) (*jose.JSONWebKey, error) {
  92. val, ok := ctx.Value(JwkContextKey).(*jose.JSONWebKey)
  93. if !ok || val == nil {
  94. return nil, ServerInternalErr(errors.Errorf("jwk expected in request context"))
  95. }
  96. return val, nil
  97. }
  98. // JwsFromContext searches the context for a JWS. Returns the JWS or an error.
  99. func JwsFromContext(ctx context.Context) (*jose.JSONWebSignature, error) {
  100. val, ok := ctx.Value(JwsContextKey).(*jose.JSONWebSignature)
  101. if !ok || val == nil {
  102. return nil, ServerInternalErr(errors.Errorf("jws expected in request context"))
  103. }
  104. return val, nil
  105. }
  106. // ProvisionerFromContext searches the context for a provisioner. Returns the
  107. // provisioner or an error.
  108. func ProvisionerFromContext(ctx context.Context) (Provisioner, error) {
  109. val := ctx.Value(ProvisionerContextKey)
  110. if val == nil {
  111. return nil, ServerInternalErr(errors.Errorf("provisioner expected in request context"))
  112. }
  113. pval, ok := val.(Provisioner)
  114. if !ok || pval == nil {
  115. return nil, ServerInternalErr(errors.Errorf("provisioner in context is not an ACME provisioner"))
  116. }
  117. return pval, nil
  118. }
  119. // SignAuthority is the interface implemented by a CA authority.
  120. type SignAuthority interface {
  121. Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  122. LoadProvisionerByID(string) (provisioner.Interface, error)
  123. }
  124. // Identifier encodes the type that an order pertains to.
  125. type Identifier struct {
  126. Type string `json:"type"`
  127. Value string `json:"value"`
  128. }
  129. var (
  130. // StatusValid -- valid
  131. StatusValid = "valid"
  132. // StatusInvalid -- invalid
  133. StatusInvalid = "invalid"
  134. // StatusPending -- pending; e.g. an Order that is not ready to be finalized.
  135. StatusPending = "pending"
  136. // StatusDeactivated -- deactivated; e.g. for an Account that is not longer valid.
  137. StatusDeactivated = "deactivated"
  138. // StatusReady -- ready; e.g. for an Order that is ready to be finalized.
  139. StatusReady = "ready"
  140. //statusExpired = "expired"
  141. //statusActive = "active"
  142. //statusProcessing = "processing"
  143. )
  144. var idLen = 32
  145. func randID() (val string, err error) {
  146. val, err = randutil.Alphanumeric(idLen)
  147. if err != nil {
  148. return "", ServerInternalErr(errors.Wrap(err, "error generating random alphanumeric ID"))
  149. }
  150. return val, nil
  151. }
  152. // Clock that returns time in UTC rounded to seconds.
  153. type Clock int
  154. // Now returns the UTC time rounded to seconds.
  155. func (c *Clock) Now() time.Time {
  156. return time.Now().UTC().Round(time.Second)
  157. }
  158. var clock = new(Clock)