File: crl.go

package info (click to toggle)
golang-github-smallstep-certificates 0.28.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: sh: 367; makefile: 129
file content (46 lines) | stat: -rw-r--r-- 1,103 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package api

import (
	"encoding/pem"
	"net/http"
	"time"

	"github.com/smallstep/certificates/api/render"
	"github.com/smallstep/certificates/errs"
)

// CRL is an HTTP handler that returns the current CRL in DER or PEM format
func CRL(w http.ResponseWriter, r *http.Request) {
	crlInfo, err := mustAuthority(r.Context()).GetCertificateRevocationList()
	if err != nil {
		render.Error(w, r, err)
		return
	}

	if crlInfo == nil {
		render.Error(w, r, errs.New(http.StatusNotFound, "no CRL available"))
		return
	}

	expires := crlInfo.ExpiresAt
	if expires.IsZero() {
		expires = time.Now()
	}

	w.Header().Add("Expires", expires.Format(time.RFC1123))

	_, formatAsPEM := r.URL.Query()["pem"]
	if formatAsPEM {
		w.Header().Add("Content-Type", "application/x-pem-file")
		w.Header().Add("Content-Disposition", "attachment; filename=\"crl.pem\"")

		_ = pem.Encode(w, &pem.Block{
			Type:  "X509 CRL",
			Bytes: crlInfo.Data,
		})
	} else {
		w.Header().Add("Content-Type", "application/pkix-crl")
		w.Header().Add("Content-Disposition", "attachment; filename=\"crl.der\"")
		w.Write(crlInfo.Data)
	}
}