crl.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package api
  2. import (
  3. "encoding/pem"
  4. "net/http"
  5. "time"
  6. "github.com/smallstep/certificates/api/render"
  7. "github.com/smallstep/certificates/errs"
  8. )
  9. // CRL is an HTTP handler that returns the current CRL in DER or PEM format
  10. func CRL(w http.ResponseWriter, r *http.Request) {
  11. crlInfo, err := mustAuthority(r.Context()).GetCertificateRevocationList()
  12. if err != nil {
  13. render.Error(w, r, err)
  14. return
  15. }
  16. if crlInfo == nil {
  17. render.Error(w, r, errs.New(http.StatusNotFound, "no CRL available"))
  18. return
  19. }
  20. expires := crlInfo.ExpiresAt
  21. if expires.IsZero() {
  22. expires = time.Now()
  23. }
  24. w.Header().Add("Expires", expires.Format(time.RFC1123))
  25. _, formatAsPEM := r.URL.Query()["pem"]
  26. if formatAsPEM {
  27. w.Header().Add("Content-Type", "application/x-pem-file")
  28. w.Header().Add("Content-Disposition", "attachment; filename=\"crl.pem\"")
  29. _ = pem.Encode(w, &pem.Block{
  30. Type: "X509 CRL",
  31. Bytes: crlInfo.Data,
  32. })
  33. } else {
  34. w.Header().Add("Content-Type", "application/pkix-crl")
  35. w.Header().Add("Content-Disposition", "attachment; filename=\"crl.der\"")
  36. w.Write(crlInfo.Data) //nolint:gosec // writing CRL binary data
  37. }
  38. }