Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

210 linhas
6.0 KiB

  1. package acme
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. )
  7. // ACME server response statuses used to describe Authorization and Challenge states.
  8. const (
  9. StatusUnknown = "unknown"
  10. StatusPending = "pending"
  11. StatusProcessing = "processing"
  12. StatusValid = "valid"
  13. StatusInvalid = "invalid"
  14. StatusRevoked = "revoked"
  15. )
  16. // CRLReasonCode identifies the reason for a certificate revocation.
  17. type CRLReasonCode int
  18. // CRL reason codes as defined in RFC 5280.
  19. const (
  20. CRLReasonUnspecified CRLReasonCode = 0
  21. CRLReasonKeyCompromise CRLReasonCode = 1
  22. CRLReasonCACompromise CRLReasonCode = 2
  23. CRLReasonAffiliationChanged CRLReasonCode = 3
  24. CRLReasonSuperseded CRLReasonCode = 4
  25. CRLReasonCessationOfOperation CRLReasonCode = 5
  26. CRLReasonCertificateHold CRLReasonCode = 6
  27. CRLReasonRemoveFromCRL CRLReasonCode = 8
  28. CRLReasonPrivilegeWithdrawn CRLReasonCode = 9
  29. CRLReasonAACompromise CRLReasonCode = 10
  30. )
  31. var (
  32. // ErrAuthorizationFailed indicates that an authorization for an identifier
  33. // did not succeed.
  34. ErrAuthorizationFailed = errors.New("acme: identifier authorization failed")
  35. // ErrUnsupportedKey is returned when an unsupported key type is encountered.
  36. ErrUnsupportedKey = errors.New("acme: unknown key type; only RSA and ECDSA are supported")
  37. )
  38. // Error is an ACME error, defined in Problem Details for HTTP APIs doc
  39. // http://tools.ietf.org/html/draft-ietf-appsawg-http-problem.
  40. type Error struct {
  41. // StatusCode is The HTTP status code generated by the origin server.
  42. StatusCode int
  43. // ProblemType is a URI reference that identifies the problem type,
  44. // typically in a "urn:acme:error:xxx" form.
  45. ProblemType string
  46. // Detail is a human-readable explanation specific to this occurrence of the problem.
  47. Detail string
  48. // Header is the original server error response headers.
  49. Header http.Header
  50. }
  51. func (e *Error) Error() string {
  52. return fmt.Sprintf("%d %s: %s", e.StatusCode, e.ProblemType, e.Detail)
  53. }
  54. // Account is a user account. It is associated with a private key.
  55. type Account struct {
  56. // URI is the account unique ID, which is also a URL used to retrieve
  57. // account data from the CA.
  58. URI string
  59. // Contact is a slice of contact info used during registration.
  60. Contact []string
  61. // The terms user has agreed to.
  62. // A value not matching CurrentTerms indicates that the user hasn't agreed
  63. // to the actual Terms of Service of the CA.
  64. AgreedTerms string
  65. // Actual terms of a CA.
  66. CurrentTerms string
  67. // Authz is the authorization URL used to initiate a new authz flow.
  68. Authz string
  69. // Authorizations is a URI from which a list of authorizations
  70. // granted to this account can be fetched via a GET request.
  71. Authorizations string
  72. // Certificates is a URI from which a list of certificates
  73. // issued for this account can be fetched via a GET request.
  74. Certificates string
  75. }
  76. // Directory is ACME server discovery data.
  77. type Directory struct {
  78. // RegURL is an account endpoint URL, allowing for creating new
  79. // and modifying existing accounts.
  80. RegURL string
  81. // AuthzURL is used to initiate Identifier Authorization flow.
  82. AuthzURL string
  83. // CertURL is a new certificate issuance endpoint URL.
  84. CertURL string
  85. // RevokeURL is used to initiate a certificate revocation flow.
  86. RevokeURL string
  87. // Term is a URI identifying the current terms of service.
  88. Terms string
  89. // Website is an HTTP or HTTPS URL locating a website
  90. // providing more information about the ACME server.
  91. Website string
  92. // CAA consists of lowercase hostname elements, which the ACME server
  93. // recognises as referring to itself for the purposes of CAA record validation
  94. // as defined in RFC6844.
  95. CAA []string
  96. }
  97. // Challenge encodes a returned CA challenge.
  98. type Challenge struct {
  99. // Type is the challenge type, e.g. "http-01", "tls-sni-02", "dns-01".
  100. Type string
  101. // URI is where a challenge response can be posted to.
  102. URI string
  103. // Token is a random value that uniquely identifies the challenge.
  104. Token string
  105. // Status identifies the status of this challenge.
  106. Status string
  107. }
  108. // Authorization encodes an authorization response.
  109. type Authorization struct {
  110. // URI uniquely identifies a authorization.
  111. URI string
  112. // Status identifies the status of an authorization.
  113. Status string
  114. // Identifier is what the account is authorized to represent.
  115. Identifier AuthzID
  116. // Challenges that the client needs to fulfill in order to prove possession
  117. // of the identifier (for pending authorizations).
  118. // For final authorizations, the challenges that were used.
  119. Challenges []*Challenge
  120. // A collection of sets of challenges, each of which would be sufficient
  121. // to prove possession of the identifier.
  122. // Clients must complete a set of challenges that covers at least one set.
  123. // Challenges are identified by their indices in the challenges array.
  124. // If this field is empty, the client needs to complete all challenges.
  125. Combinations [][]int
  126. }
  127. // AuthzID is an identifier that an account is authorized to represent.
  128. type AuthzID struct {
  129. Type string // The type of identifier, e.g. "dns".
  130. Value string // The identifier itself, e.g. "example.org".
  131. }
  132. // wireAuthz is ACME JSON representation of Authorization objects.
  133. type wireAuthz struct {
  134. Status string
  135. Challenges []wireChallenge
  136. Combinations [][]int
  137. Identifier struct {
  138. Type string
  139. Value string
  140. }
  141. }
  142. func (z *wireAuthz) authorization(uri string) *Authorization {
  143. a := &Authorization{
  144. URI: uri,
  145. Status: z.Status,
  146. Identifier: AuthzID{Type: z.Identifier.Type, Value: z.Identifier.Value},
  147. Combinations: z.Combinations, // shallow copy
  148. Challenges: make([]*Challenge, len(z.Challenges)),
  149. }
  150. for i, v := range z.Challenges {
  151. a.Challenges[i] = v.challenge()
  152. }
  153. return a
  154. }
  155. // wireChallenge is ACME JSON challenge representation.
  156. type wireChallenge struct {
  157. URI string `json:"uri"`
  158. Type string
  159. Token string
  160. Status string
  161. }
  162. func (c *wireChallenge) challenge() *Challenge {
  163. v := &Challenge{
  164. URI: c.URI,
  165. Type: c.Type,
  166. Token: c.Token,
  167. Status: c.Status,
  168. }
  169. if v.Status == "" {
  170. v.Status = StatusPending
  171. }
  172. return v
  173. }