common.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // ContextKey is the key type for storing and searching for ACME request
  13. // essentials in the context of a request.
  14. type ContextKey string
  15. const (
  16. // AccContextKey account key
  17. AccContextKey = ContextKey("acc")
  18. // BaseURLContextKey baseURL key
  19. BaseURLContextKey = ContextKey("baseURL")
  20. // JwsContextKey jws key
  21. JwsContextKey = ContextKey("jws")
  22. // JwkContextKey jwk key
  23. JwkContextKey = ContextKey("jwk")
  24. // PayloadContextKey payload key
  25. PayloadContextKey = ContextKey("payload")
  26. // ProvisionerContextKey provisioner key
  27. ProvisionerContextKey = ContextKey("provisioner")
  28. )
  29. // AccountFromContext searches the context for an ACME account. Returns the
  30. // account or an error.
  31. func AccountFromContext(ctx context.Context) (*Account, error) {
  32. val, ok := ctx.Value(AccContextKey).(*Account)
  33. if !ok || val == nil {
  34. return nil, AccountDoesNotExistErr(nil)
  35. }
  36. return val, nil
  37. }
  38. // BaseURLFromContext returns the baseURL if one is stored in the context.
  39. func BaseURLFromContext(ctx context.Context) *url.URL {
  40. val, ok := ctx.Value(BaseURLContextKey).(*url.URL)
  41. if !ok || val == nil {
  42. return nil
  43. }
  44. return val
  45. }
  46. // JwkFromContext searches the context for a JWK. Returns the JWK or an error.
  47. func JwkFromContext(ctx context.Context) (*jose.JSONWebKey, error) {
  48. val, ok := ctx.Value(JwkContextKey).(*jose.JSONWebKey)
  49. if !ok || val == nil {
  50. return nil, ServerInternalErr(errors.Errorf("jwk expected in request context"))
  51. }
  52. return val, nil
  53. }
  54. // JwsFromContext searches the context for a JWS. Returns the JWS or an error.
  55. func JwsFromContext(ctx context.Context) (*jose.JSONWebSignature, error) {
  56. val, ok := ctx.Value(JwsContextKey).(*jose.JSONWebSignature)
  57. if !ok || val == nil {
  58. return nil, ServerInternalErr(errors.Errorf("jws expected in request context"))
  59. }
  60. return val, nil
  61. }
  62. // ProvisionerFromContext searches the context for a provisioner. Returns the
  63. // provisioner or an error.
  64. func ProvisionerFromContext(ctx context.Context) (provisioner.Interface, error) {
  65. val, ok := ctx.Value(ProvisionerContextKey).(provisioner.Interface)
  66. if !ok || val == nil {
  67. return nil, ServerInternalErr(errors.Errorf("provisioner expected in request context"))
  68. }
  69. return val, nil
  70. }
  71. // SignAuthority is the interface implemented by a CA authority.
  72. type SignAuthority interface {
  73. Sign(cr *x509.CertificateRequest, opts provisioner.Options, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  74. LoadProvisionerByID(string) (provisioner.Interface, error)
  75. }
  76. // Identifier encodes the type that an order pertains to.
  77. type Identifier struct {
  78. Type string `json:"type"`
  79. Value string `json:"value"`
  80. }
  81. var (
  82. // StatusValid -- valid
  83. StatusValid = "valid"
  84. // StatusInvalid -- invalid
  85. StatusInvalid = "invalid"
  86. // StatusPending -- pending; e.g. an Order that is not ready to be finalized.
  87. StatusPending = "pending"
  88. // StatusProcessing -- processing e.g. a Challenge that is in the process of being validated.
  89. StatusProcessing = "processing"
  90. // StatusDeactivated -- deactivated; e.g. for an Account that is not longer valid.
  91. StatusDeactivated = "deactivated"
  92. // StatusReady -- ready; e.g. for an Order that is ready to be finalized.
  93. StatusReady = "ready"
  94. //statusExpired = "expired"
  95. //statusActive = "active"
  96. //statusProcessing = "processing"
  97. )
  98. var idLen = 32
  99. func randID() (val string, err error) {
  100. val, err = randutil.Alphanumeric(idLen)
  101. if err != nil {
  102. return "", ServerInternalErr(errors.Wrap(err, "error generating random alphanumeric ID"))
  103. }
  104. return val, nil
  105. }
  106. // Clock that returns time in UTC rounded to seconds.
  107. type Clock int
  108. // Now returns the UTC time rounded to seconds.
  109. func (c *Clock) Now() time.Time {
  110. return time.Now().UTC().Round(time.Second)
  111. }
  112. var clock = new(Clock)