authority.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package acme
  2. import (
  3. "crypto"
  4. "crypto/x509"
  5. "encoding/base64"
  6. "net"
  7. "net/http"
  8. "net/url"
  9. "time"
  10. "github.com/pkg/errors"
  11. "github.com/smallstep/certificates/authority/provisioner"
  12. "github.com/smallstep/cli/jose"
  13. "github.com/smallstep/nosql"
  14. )
  15. // Interface is the acme authority interface.
  16. type Interface interface {
  17. DeactivateAccount(provisioner.Interface, string) (*Account, error)
  18. FinalizeOrder(provisioner.Interface, string, string, *x509.CertificateRequest) (*Order, error)
  19. GetAccount(provisioner.Interface, string) (*Account, error)
  20. GetAccountByKey(provisioner.Interface, *jose.JSONWebKey) (*Account, error)
  21. GetAuthz(provisioner.Interface, string, string) (*Authz, error)
  22. GetCertificate(string, string) ([]byte, error)
  23. GetDirectory(provisioner.Interface) *Directory
  24. GetLink(Link, string, bool, ...string) string
  25. GetOrder(provisioner.Interface, string, string) (*Order, error)
  26. GetOrdersByAccount(provisioner.Interface, string) ([]string, error)
  27. LoadProvisionerByID(string) (provisioner.Interface, error)
  28. NewAccount(provisioner.Interface, AccountOptions) (*Account, error)
  29. NewNonce() (string, error)
  30. NewOrder(provisioner.Interface, OrderOptions) (*Order, error)
  31. UpdateAccount(provisioner.Interface, string, []string) (*Account, error)
  32. UseNonce(string) error
  33. ValidateChallenge(provisioner.Interface, string, string, *jose.JSONWebKey) (*Challenge, error)
  34. }
  35. // Authority is the layer that handles all ACME interactions.
  36. type Authority struct {
  37. db nosql.DB
  38. dir *directory
  39. signAuth SignAuthority
  40. }
  41. // NewAuthority returns a new Authority that implements the ACME interface.
  42. func NewAuthority(db nosql.DB, dns, prefix string, signAuth SignAuthority) *Authority {
  43. return &Authority{
  44. db: db, dir: newDirectory(dns, prefix), signAuth: signAuth,
  45. }
  46. }
  47. // GetLink returns the requested link from the directory.
  48. func (a *Authority) GetLink(typ Link, provID string, abs bool, inputs ...string) string {
  49. return a.dir.getLink(typ, provID, abs, inputs...)
  50. }
  51. // GetDirectory returns the ACME directory object.
  52. func (a *Authority) GetDirectory(p provisioner.Interface) *Directory {
  53. name := url.PathEscape(p.GetName())
  54. return &Directory{
  55. NewNonce: a.dir.getLink(NewNonceLink, name, true),
  56. NewAccount: a.dir.getLink(NewAccountLink, name, true),
  57. NewOrder: a.dir.getLink(NewOrderLink, name, true),
  58. RevokeCert: a.dir.getLink(RevokeCertLink, name, true),
  59. KeyChange: a.dir.getLink(KeyChangeLink, name, true),
  60. }
  61. }
  62. // LoadProvisionerByID calls out to the SignAuthority interface to load a
  63. // provisioner by ID.
  64. func (a *Authority) LoadProvisionerByID(id string) (provisioner.Interface, error) {
  65. return a.signAuth.LoadProvisionerByID(id)
  66. }
  67. // NewNonce generates, stores, and returns a new ACME nonce.
  68. func (a *Authority) NewNonce() (string, error) {
  69. n, err := newNonce(a.db)
  70. if err != nil {
  71. return "", err
  72. }
  73. return n.ID, nil
  74. }
  75. // UseNonce consumes the given nonce if it is valid, returns error otherwise.
  76. func (a *Authority) UseNonce(nonce string) error {
  77. return useNonce(a.db, nonce)
  78. }
  79. // NewAccount creates, stores, and returns a new ACME account.
  80. func (a *Authority) NewAccount(p provisioner.Interface, ao AccountOptions) (*Account, error) {
  81. acc, err := newAccount(a.db, ao)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return acc.toACME(a.db, a.dir, p)
  86. }
  87. // UpdateAccount updates an ACME account.
  88. func (a *Authority) UpdateAccount(p provisioner.Interface, id string, contact []string) (*Account, error) {
  89. acc, err := getAccountByID(a.db, id)
  90. if err != nil {
  91. return nil, ServerInternalErr(err)
  92. }
  93. if acc, err = acc.update(a.db, contact); err != nil {
  94. return nil, err
  95. }
  96. return acc.toACME(a.db, a.dir, p)
  97. }
  98. // GetAccount returns an ACME account.
  99. func (a *Authority) GetAccount(p provisioner.Interface, id string) (*Account, error) {
  100. acc, err := getAccountByID(a.db, id)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return acc.toACME(a.db, a.dir, p)
  105. }
  106. // DeactivateAccount deactivates an ACME account.
  107. func (a *Authority) DeactivateAccount(p provisioner.Interface, id string) (*Account, error) {
  108. acc, err := getAccountByID(a.db, id)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if acc, err = acc.deactivate(a.db); err != nil {
  113. return nil, err
  114. }
  115. return acc.toACME(a.db, a.dir, p)
  116. }
  117. func keyToID(jwk *jose.JSONWebKey) (string, error) {
  118. kid, err := jwk.Thumbprint(crypto.SHA256)
  119. if err != nil {
  120. return "", ServerInternalErr(errors.Wrap(err, "error generating jwk thumbprint"))
  121. }
  122. return base64.RawURLEncoding.EncodeToString(kid), nil
  123. }
  124. // GetAccountByKey returns the ACME associated with the jwk id.
  125. func (a *Authority) GetAccountByKey(p provisioner.Interface, jwk *jose.JSONWebKey) (*Account, error) {
  126. kid, err := keyToID(jwk)
  127. if err != nil {
  128. return nil, err
  129. }
  130. acc, err := getAccountByKeyID(a.db, kid)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return acc.toACME(a.db, a.dir, p)
  135. }
  136. // GetOrder returns an ACME order.
  137. func (a *Authority) GetOrder(p provisioner.Interface, accID, orderID string) (*Order, error) {
  138. o, err := getOrder(a.db, orderID)
  139. if err != nil {
  140. return nil, err
  141. }
  142. if accID != o.AccountID {
  143. return nil, UnauthorizedErr(errors.New("account does not own order"))
  144. }
  145. if o, err = o.updateStatus(a.db); err != nil {
  146. return nil, err
  147. }
  148. return o.toACME(a.db, a.dir, p)
  149. }
  150. // GetOrdersByAccount returns the list of order urls owned by the account.
  151. func (a *Authority) GetOrdersByAccount(p provisioner.Interface, id string) ([]string, error) {
  152. oids, err := getOrderIDsByAccount(a.db, id)
  153. if err != nil {
  154. return nil, err
  155. }
  156. var ret = []string{}
  157. for _, oid := range oids {
  158. o, err := getOrder(a.db, oid)
  159. if err != nil {
  160. return nil, ServerInternalErr(err)
  161. }
  162. if o.Status == StatusInvalid {
  163. continue
  164. }
  165. ret = append(ret, a.dir.getLink(OrderLink, URLSafeProvisionerName(p), true, o.ID))
  166. }
  167. return ret, nil
  168. }
  169. // NewOrder generates, stores, and returns a new ACME order.
  170. func (a *Authority) NewOrder(p provisioner.Interface, ops OrderOptions) (*Order, error) {
  171. order, err := newOrder(a.db, ops)
  172. if err != nil {
  173. return nil, Wrap(err, "error creating order")
  174. }
  175. return order.toACME(a.db, a.dir, p)
  176. }
  177. // FinalizeOrder attempts to finalize an order and generate a new certificate.
  178. func (a *Authority) FinalizeOrder(p provisioner.Interface, accID, orderID string, csr *x509.CertificateRequest) (*Order, error) {
  179. o, err := getOrder(a.db, orderID)
  180. if err != nil {
  181. return nil, err
  182. }
  183. if accID != o.AccountID {
  184. return nil, UnauthorizedErr(errors.New("account does not own order"))
  185. }
  186. o, err = o.finalize(a.db, csr, a.signAuth, p)
  187. if err != nil {
  188. return nil, Wrap(err, "error finalizing order")
  189. }
  190. return o.toACME(a.db, a.dir, p)
  191. }
  192. // GetAuthz retrieves and attempts to update the status on an ACME authz
  193. // before returning.
  194. func (a *Authority) GetAuthz(p provisioner.Interface, accID, authzID string) (*Authz, error) {
  195. az, err := getAuthz(a.db, authzID)
  196. if err != nil {
  197. return nil, err
  198. }
  199. if accID != az.getAccountID() {
  200. return nil, UnauthorizedErr(errors.New("account does not own authz"))
  201. }
  202. az, err = az.updateStatus(a.db)
  203. if err != nil {
  204. return nil, Wrap(err, "error updating authz status")
  205. }
  206. return az.toACME(a.db, a.dir, p)
  207. }
  208. // ValidateChallenge attempts to validate the challenge.
  209. func (a *Authority) ValidateChallenge(p provisioner.Interface, accID, chID string, jwk *jose.JSONWebKey) (*Challenge, error) {
  210. ch, err := getChallenge(a.db, chID)
  211. if err != nil {
  212. return nil, err
  213. }
  214. if accID != ch.getAccountID() {
  215. return nil, UnauthorizedErr(errors.New("account does not own challenge"))
  216. }
  217. client := http.Client{
  218. Timeout: time.Duration(30 * time.Second),
  219. }
  220. ch, err = ch.validate(a.db, jwk, validateOptions{
  221. httpGet: client.Get,
  222. lookupTxt: net.LookupTXT,
  223. })
  224. if err != nil {
  225. return nil, Wrap(err, "error attempting challenge validation")
  226. }
  227. return ch.toACME(a.db, a.dir, p)
  228. }
  229. // GetCertificate retrieves the Certificate by ID.
  230. func (a *Authority) GetCertificate(accID, certID string) ([]byte, error) {
  231. cert, err := getCert(a.db, certID)
  232. if err != nil {
  233. return nil, err
  234. }
  235. if accID != cert.AccountID {
  236. return nil, UnauthorizedErr(errors.New("account does not own certificate"))
  237. }
  238. return cert.toACME(a.db, a.dir)
  239. }