common.go 5.1 KB

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