certinfo.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Package certinfo implements the HTTP handler for the certinfo command.
  2. package certinfo
  3. import (
  4. "net/http"
  5. "github.com/cloudflare/cfssl/api"
  6. "github.com/cloudflare/cfssl/certinfo"
  7. "github.com/cloudflare/cfssl/log"
  8. )
  9. // Handler accepts requests for either remote or uploaded
  10. // certificates to be bundled, and returns a certificate bundle (or
  11. // error).
  12. type Handler struct{}
  13. // NewHandler creates a new bundler that uses the root bundle and
  14. // intermediate bundle in the trust chain.
  15. func NewHandler() http.Handler {
  16. return api.HTTPHandler{Handler: new(Handler), Methods: []string{"POST"}}
  17. }
  18. // Handle implements an http.Handler interface for the bundle handler.
  19. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) (err error) {
  20. blob, matched, err := api.ProcessRequestFirstMatchOf(r,
  21. [][]string{
  22. {"certificate"},
  23. {"domain"},
  24. })
  25. if err != nil {
  26. log.Warningf("invalid request: %v", err)
  27. return err
  28. }
  29. var cert *certinfo.Certificate
  30. switch matched[0] {
  31. case "domain":
  32. if cert, err = certinfo.ParseCertificateDomain(blob["domain"]); err != nil {
  33. log.Warningf("couldn't parse remote certificate: %v", err)
  34. return err
  35. }
  36. case "certificate":
  37. if cert, err = certinfo.ParseCertificatePEM([]byte(blob["certificate"])); err != nil {
  38. log.Warningf("bad PEM certifcate: %v", err)
  39. return err
  40. }
  41. }
  42. return api.SendResponse(w, cert)
  43. }