account.go 3.5 KB

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