certinfo.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Package certinfo implements the HTTP handler for the certinfo command.
  2. package certinfo
  3. import (
  4. "errors"
  5. "net/http"
  6. "github.com/cloudflare/cfssl/api"
  7. "github.com/cloudflare/cfssl/certdb"
  8. "github.com/cloudflare/cfssl/certinfo"
  9. "github.com/cloudflare/cfssl/log"
  10. )
  11. // Handler accepts requests for either remote or uploaded
  12. // certificates to be bundled, and returns a certificate bundle (or
  13. // error).
  14. type Handler struct {
  15. dbAccessor certdb.Accessor
  16. }
  17. // NewHandler creates a new bundler that uses the root bundle and
  18. // intermediate bundle in the trust chain.
  19. func NewHandler() http.Handler {
  20. return api.HTTPHandler{Handler: new(Handler), Methods: []string{"POST"}}
  21. }
  22. // NewAccessorHandler creates a new bundler with database access via the
  23. // certdb.Accessor interface. If this handler is constructed it will be possible
  24. // to lookup certificates issued earlier by the CA.
  25. func NewAccessorHandler(dbAccessor certdb.Accessor) http.Handler {
  26. return api.HTTPHandler{
  27. Handler: &Handler{
  28. dbAccessor: dbAccessor,
  29. },
  30. Methods: []string{"POST"},
  31. }
  32. }
  33. // Handle implements an http.Handler interface for the bundle handler.
  34. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) (err error) {
  35. blob, matched, err := api.ProcessRequestFirstMatchOf(r,
  36. [][]string{
  37. {"certificate"},
  38. {"domain"},
  39. {"serial", "authority_key_id"},
  40. })
  41. if err != nil {
  42. log.Warningf("invalid request: %v", err)
  43. return err
  44. }
  45. var cert *certinfo.Certificate
  46. switch matched[0] {
  47. case "domain":
  48. if cert, err = certinfo.ParseCertificateDomain(blob["domain"]); err != nil {
  49. log.Warningf("couldn't parse remote certificate: %v", err)
  50. return err
  51. }
  52. case "certificate":
  53. if cert, err = certinfo.ParseCertificatePEM([]byte(blob["certificate"])); err != nil {
  54. log.Warningf("bad PEM certificate: %v", err)
  55. return err
  56. }
  57. case "serial", "authority_key_id":
  58. if h.dbAccessor == nil {
  59. log.Warning("could not find certificates with db access")
  60. return errors.New("cannot lookup certificate from serial without db access")
  61. }
  62. if cert, err = certinfo.ParseSerialNumber(blob["serial"], blob["authority_key_id"], h.dbAccessor); err != nil {
  63. log.Warningf("couldn't find certificate: %v", err)
  64. return err
  65. }
  66. }
  67. return api.SendResponse(w, cert)
  68. }