account.go 3.8 KB

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