authz.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package acme
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "time"
  6. "github.com/pkg/errors"
  7. "github.com/smallstep/certificates/authority/provisioner"
  8. "github.com/smallstep/nosql"
  9. )
  10. var defaultExpiryDuration = time.Hour * 24
  11. // Authz is a subset of the Authz type containing only those attributes
  12. // required for responses in the ACME protocol.
  13. type Authz struct {
  14. Identifier Identifier `json:"identifier"`
  15. Status string `json:"status"`
  16. Expires string `json:"expires"`
  17. Challenges []*Challenge `json:"challenges"`
  18. Wildcard bool `json:"wildcard"`
  19. ID string `json:"-"`
  20. }
  21. // ToLog enables response logging.
  22. func (a *Authz) ToLog() (interface{}, error) {
  23. b, err := json.Marshal(a)
  24. if err != nil {
  25. return nil, ServerInternalErr(errors.Wrap(err, "error marshaling authz for logging"))
  26. }
  27. return string(b), nil
  28. }
  29. // GetID returns the Authz ID.
  30. func (a *Authz) GetID() string {
  31. return a.ID
  32. }
  33. // authz is the interface that the various authz types must implement.
  34. type authz interface {
  35. save(nosql.DB, authz) error
  36. clone() *baseAuthz
  37. getID() string
  38. getAccountID() string
  39. getType() string
  40. getIdentifier() Identifier
  41. getStatus() string
  42. getExpiry() time.Time
  43. getWildcard() bool
  44. getChallenges() []string
  45. getCreated() time.Time
  46. updateStatus(db nosql.DB) (authz, error)
  47. toACME(nosql.DB, *directory, provisioner.Interface) (*Authz, error)
  48. }
  49. // baseAuthz is the base authz type that others build from.
  50. type baseAuthz struct {
  51. ID string `json:"id"`
  52. AccountID string `json:"accountID"`
  53. Identifier Identifier `json:"identifier"`
  54. Status string `json:"status"`
  55. Expires time.Time `json:"expires"`
  56. Challenges []string `json:"challenges"`
  57. Wildcard bool `json:"wildcard"`
  58. Created time.Time `json:"created"`
  59. Error *Error `json:"error"`
  60. }
  61. func newBaseAuthz(accID string, identifier Identifier) (*baseAuthz, error) {
  62. id, err := randID()
  63. if err != nil {
  64. return nil, err
  65. }
  66. now := clock.Now()
  67. ba := &baseAuthz{
  68. ID: id,
  69. AccountID: accID,
  70. Status: StatusPending,
  71. Created: now,
  72. Expires: now.Add(defaultExpiryDuration),
  73. Identifier: identifier,
  74. }
  75. if strings.HasPrefix(identifier.Value, "*.") {
  76. ba.Wildcard = true
  77. ba.Identifier = Identifier{
  78. Value: strings.TrimPrefix(identifier.Value, "*."),
  79. Type: identifier.Type,
  80. }
  81. }
  82. return ba, nil
  83. }
  84. // getID returns the ID of the authz.
  85. func (ba *baseAuthz) getID() string {
  86. return ba.ID
  87. }
  88. // getAccountID returns the Account ID that created the authz.
  89. func (ba *baseAuthz) getAccountID() string {
  90. return ba.AccountID
  91. }
  92. // getType returns the type of the authz.
  93. func (ba *baseAuthz) getType() string {
  94. return ba.Identifier.Type
  95. }
  96. // getIdentifier returns the identifier for the authz.
  97. func (ba *baseAuthz) getIdentifier() Identifier {
  98. return ba.Identifier
  99. }
  100. // getStatus returns the status of the authz.
  101. func (ba *baseAuthz) getStatus() string {
  102. return ba.Status
  103. }
  104. // getWildcard returns true if the authz identifier has a '*', false otherwise.
  105. func (ba *baseAuthz) getWildcard() bool {
  106. return ba.Wildcard
  107. }
  108. // getChallenges returns the authz challenge IDs.
  109. func (ba *baseAuthz) getChallenges() []string {
  110. return ba.Challenges
  111. }
  112. // getExpiry returns the expiration time of the authz.
  113. func (ba *baseAuthz) getExpiry() time.Time {
  114. return ba.Expires
  115. }
  116. // getCreated returns the created time of the authz.
  117. func (ba *baseAuthz) getCreated() time.Time {
  118. return ba.Created
  119. }
  120. // toACME converts the internal Authz type into the public acmeAuthz type for
  121. // presentation in the ACME protocol.
  122. func (ba *baseAuthz) toACME(db nosql.DB, dir *directory, p provisioner.Interface) (*Authz, error) {
  123. var chs = make([]*Challenge, len(ba.Challenges))
  124. for i, chID := range ba.Challenges {
  125. ch, err := getChallenge(db, chID)
  126. if err != nil {
  127. return nil, err
  128. }
  129. chs[i], err = ch.toACME(db, dir, p)
  130. if err != nil {
  131. return nil, err
  132. }
  133. }
  134. return &Authz{
  135. Identifier: ba.Identifier,
  136. Status: ba.getStatus(),
  137. Challenges: chs,
  138. Wildcard: ba.getWildcard(),
  139. Expires: ba.Expires.Format(time.RFC3339),
  140. ID: ba.ID,
  141. }, nil
  142. }
  143. func (ba *baseAuthz) save(db nosql.DB, old authz) error {
  144. var (
  145. err error
  146. oldB, newB []byte
  147. )
  148. if old == nil {
  149. oldB = nil
  150. } else {
  151. if oldB, err = json.Marshal(old); err != nil {
  152. return ServerInternalErr(errors.Wrap(err, "error marshaling old authz"))
  153. }
  154. }
  155. if newB, err = json.Marshal(ba); err != nil {
  156. return ServerInternalErr(errors.Wrap(err, "error marshaling new authz"))
  157. }
  158. _, swapped, err := db.CmpAndSwap(authzTable, []byte(ba.ID), oldB, newB)
  159. switch {
  160. case err != nil:
  161. return ServerInternalErr(errors.Wrapf(err, "error storing authz"))
  162. case !swapped:
  163. return ServerInternalErr(errors.Errorf("error storing authz; " +
  164. "value has changed since last read"))
  165. default:
  166. return nil
  167. }
  168. }
  169. func (ba *baseAuthz) clone() *baseAuthz {
  170. u := *ba
  171. return &u
  172. }
  173. func (ba *baseAuthz) parent() authz {
  174. return &dnsAuthz{ba}
  175. }
  176. // updateStatus attempts to update the status on a baseAuthz and stores the
  177. // updating object if necessary.
  178. func (ba *baseAuthz) updateStatus(db nosql.DB) (authz, error) {
  179. newAuthz := ba.clone()
  180. now := time.Now().UTC()
  181. switch ba.Status {
  182. case StatusInvalid:
  183. return ba.parent(), nil
  184. case StatusValid:
  185. return ba.parent(), nil
  186. case StatusPending:
  187. // check expiry
  188. if now.After(ba.Expires) {
  189. newAuthz.Status = StatusInvalid
  190. newAuthz.Error = MalformedErr(errors.New("authz has expired"))
  191. break
  192. }
  193. var isValid = false
  194. for _, chID := range ba.Challenges {
  195. ch, err := getChallenge(db, chID)
  196. if err != nil {
  197. return ba, err
  198. }
  199. if ch.getStatus() == StatusValid {
  200. isValid = true
  201. break
  202. }
  203. }
  204. if !isValid {
  205. return ba.parent(), nil
  206. }
  207. newAuthz.Status = StatusValid
  208. newAuthz.Error = nil
  209. default:
  210. return nil, ServerInternalErr(errors.Errorf("unrecognized authz status: %s", ba.Status))
  211. }
  212. if err := newAuthz.save(db, ba); err != nil {
  213. return ba, err
  214. }
  215. return newAuthz.parent(), nil
  216. }
  217. // unmarshalAuthz unmarshals an authz type into the correct sub-type.
  218. func unmarshalAuthz(data []byte) (authz, error) {
  219. var getType struct {
  220. Identifier Identifier `json:"identifier"`
  221. }
  222. if err := json.Unmarshal(data, &getType); err != nil {
  223. return nil, ServerInternalErr(errors.Wrap(err, "error unmarshaling authz type"))
  224. }
  225. switch getType.Identifier.Type {
  226. case "dns":
  227. var ba baseAuthz
  228. if err := json.Unmarshal(data, &ba); err != nil {
  229. return nil, ServerInternalErr(errors.Wrap(err, "error unmarshaling authz type into dnsAuthz"))
  230. }
  231. return &dnsAuthz{&ba}, nil
  232. default:
  233. return nil, ServerInternalErr(errors.Errorf("unexpected authz type %s",
  234. getType.Identifier.Type))
  235. }
  236. }
  237. // dnsAuthz represents a dns acme authorization.
  238. type dnsAuthz struct {
  239. *baseAuthz
  240. }
  241. // newAuthz returns a new acme authorization object based on the identifier
  242. // type.
  243. func newAuthz(db nosql.DB, accID string, identifier Identifier) (a authz, err error) {
  244. switch identifier.Type {
  245. case "dns":
  246. a, err = newDNSAuthz(db, accID, identifier)
  247. default:
  248. err = MalformedErr(errors.Errorf("unexpected authz type %s",
  249. identifier.Type))
  250. }
  251. return
  252. }
  253. // newDNSAuthz returns a new dns acme authorization object.
  254. func newDNSAuthz(db nosql.DB, accID string, identifier Identifier) (authz, error) {
  255. ba, err := newBaseAuthz(accID, identifier)
  256. if err != nil {
  257. return nil, err
  258. }
  259. ba.Challenges = []string{}
  260. if !ba.Wildcard {
  261. // http challenges are only permitted if the DNS is not a wildcard dns.
  262. ch1, err := newHTTP01Challenge(db, ChallengeOptions{
  263. AccountID: accID,
  264. AuthzID: ba.ID,
  265. Identifier: ba.Identifier})
  266. if err != nil {
  267. return nil, Wrap(err, "error creating http challenge")
  268. }
  269. ba.Challenges = append(ba.Challenges, ch1.getID())
  270. }
  271. ch2, err := newDNS01Challenge(db, ChallengeOptions{
  272. AccountID: accID,
  273. AuthzID: ba.ID,
  274. Identifier: identifier})
  275. if err != nil {
  276. return nil, Wrap(err, "error creating dns challenge")
  277. }
  278. ba.Challenges = append(ba.Challenges, ch2.getID())
  279. da := &dnsAuthz{ba}
  280. if err := da.save(db, nil); err != nil {
  281. return nil, err
  282. }
  283. return da, nil
  284. }
  285. // getAuthz retrieves and unmarshals an ACME authz type from the database.
  286. func getAuthz(db nosql.DB, id string) (authz, error) {
  287. b, err := db.Get(authzTable, []byte(id))
  288. if nosql.IsErrNotFound(err) {
  289. return nil, MalformedErr(errors.Wrapf(err, "authz %s not found", id))
  290. } else if err != nil {
  291. return nil, ServerInternalErr(errors.Wrapf(err, "error loading authz %s", id))
  292. }
  293. az, err := unmarshalAuthz(b)
  294. if err != nil {
  295. return nil, err
  296. }
  297. return az, nil
  298. }