account.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package acme
  2. import (
  3. "crypto"
  4. "encoding/base64"
  5. "encoding/json"
  6. "time"
  7. "go.step.sm/crypto/jose"
  8. "github.com/smallstep/certificates/authority/policy"
  9. )
  10. // Account is a subset of the internal account type containing only those
  11. // attributes required for responses in the ACME protocol.
  12. type Account struct {
  13. ID string `json:"-"`
  14. Key *jose.JSONWebKey `json:"-"`
  15. Contact []string `json:"contact,omitempty"`
  16. Status Status `json:"status"`
  17. OrdersURL string `json:"orders"`
  18. ExternalAccountBinding any `json:"externalAccountBinding,omitempty"`
  19. LocationPrefix string `json:"-"`
  20. ProvisionerID string `json:"-"`
  21. ProvisionerName string `json:"-"`
  22. }
  23. // GetLocation returns the URL location of the given account.
  24. func (a *Account) GetLocation() string {
  25. if a.LocationPrefix == "" {
  26. return ""
  27. }
  28. return a.LocationPrefix + a.ID
  29. }
  30. // ToLog enables response logging.
  31. func (a *Account) ToLog() (any, error) {
  32. b, err := json.Marshal(a)
  33. if err != nil {
  34. return nil, WrapErrorISE(err, "error marshaling account for logging")
  35. }
  36. return string(b), nil
  37. }
  38. // IsValid returns true if the Account is valid.
  39. func (a *Account) IsValid() bool {
  40. return a.Status == StatusValid
  41. }
  42. // KeyToID converts a JWK to a thumbprint.
  43. func KeyToID(jwk *jose.JSONWebKey) (string, error) {
  44. kid, err := jwk.Thumbprint(crypto.SHA256)
  45. if err != nil {
  46. return "", WrapErrorISE(err, "error generating jwk thumbprint")
  47. }
  48. return base64.RawURLEncoding.EncodeToString(kid), nil
  49. }
  50. // PolicyNames contains ACME account level policy names
  51. type PolicyNames struct {
  52. DNSNames []string `json:"dns"`
  53. IPRanges []string `json:"ips"`
  54. }
  55. // X509Policy contains ACME account level X.509 policy
  56. type X509Policy struct {
  57. Allowed PolicyNames `json:"allow"`
  58. Denied PolicyNames `json:"deny"`
  59. AllowWildcardNames bool `json:"allowWildcardNames"`
  60. }
  61. // Policy is an ACME Account level policy
  62. type Policy struct {
  63. X509 X509Policy `json:"x509"`
  64. }
  65. func (p *Policy) GetAllowedNameOptions() *policy.X509NameOptions {
  66. if p == nil {
  67. return nil
  68. }
  69. return &policy.X509NameOptions{
  70. DNSDomains: p.X509.Allowed.DNSNames,
  71. IPRanges: p.X509.Allowed.IPRanges,
  72. }
  73. }
  74. func (p *Policy) GetDeniedNameOptions() *policy.X509NameOptions {
  75. if p == nil {
  76. return nil
  77. }
  78. return &policy.X509NameOptions{
  79. DNSDomains: p.X509.Denied.DNSNames,
  80. IPRanges: p.X509.Denied.IPRanges,
  81. }
  82. }
  83. // AreWildcardNamesAllowed returns if wildcard names
  84. // like *.example.com are allowed to be signed.
  85. // Defaults to false.
  86. func (p *Policy) AreWildcardNamesAllowed() bool {
  87. if p == nil {
  88. return false
  89. }
  90. return p.X509.AllowWildcardNames
  91. }
  92. // ExternalAccountKey is an ACME External Account Binding key.
  93. type ExternalAccountKey struct {
  94. ID string `json:"id"`
  95. ProvisionerID string `json:"provisionerID"`
  96. Reference string `json:"reference"`
  97. AccountID string `json:"-"`
  98. HmacKey []byte `json:"-"`
  99. CreatedAt time.Time `json:"createdAt"`
  100. BoundAt time.Time `json:"boundAt,omitzero"`
  101. Policy *Policy `json:"policy,omitempty"`
  102. }
  103. // AlreadyBound returns whether this EAK is already bound to
  104. // an ACME Account or not.
  105. func (eak *ExternalAccountKey) AlreadyBound() bool {
  106. return !eak.BoundAt.IsZero()
  107. }
  108. // BindTo binds the EAK to an Account.
  109. // It returns an error if it's already bound.
  110. func (eak *ExternalAccountKey) BindTo(account *Account) error {
  111. if eak.AlreadyBound() {
  112. return NewError(ErrorUnauthorizedType, "external account binding key with id '%s' was already bound to account '%s' on %s", eak.ID, eak.AccountID, eak.BoundAt)
  113. }
  114. eak.AccountID = account.ID
  115. eak.BoundAt = time.Now()
  116. eak.HmacKey = []byte{} // clearing the key bytes; can only be used once
  117. return nil
  118. }