ocspserve.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Package ocspserve implements the ocspserve function.
  2. package ocspserve
  3. import (
  4. "errors"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "github.com/cloudflare/cfssl/cli"
  9. "github.com/cloudflare/cfssl/log"
  10. "github.com/cloudflare/cfssl/ocsp"
  11. )
  12. // Usage text of 'cfssl serve'
  13. var ocspServerUsageText = `cfssl ocspserve -- set up an HTTP server that handles OCSP requests from either a file or directly from a database (see RFC 5019)
  14. Usage of ocspserve:
  15. cfssl ocspserve [-address address] [-port port] [-responses file] [-db-config db-config]
  16. Flags:
  17. `
  18. // Flags used by 'cfssl serve'
  19. var ocspServerFlags = []string{"address", "port", "responses", "db-config"}
  20. // ocspServerMain is the command line entry point to the OCSP responder.
  21. // It sets up a new HTTP server that responds to OCSP requests.
  22. func ocspServerMain(args []string, c cli.Config) error {
  23. var src ocsp.Source
  24. // serve doesn't support arguments.
  25. if len(args) > 0 {
  26. return errors.New("argument is provided but not defined; please refer to the usage by flag -h")
  27. }
  28. if c.Responses != "" {
  29. s, err := ocsp.NewSourceFromFile(c.Responses)
  30. if err != nil {
  31. return errors.New("unable to read response file")
  32. }
  33. src = s
  34. } else if c.DBConfigFile != "" {
  35. s, err := ocsp.NewSourceFromDB(c.DBConfigFile)
  36. if err != nil {
  37. return errors.New("unable to read configuration file")
  38. }
  39. src = s
  40. } else {
  41. return errors.New(
  42. "no response file or db-config provided, please set the one of these using either -responses or -db-config flags",
  43. )
  44. }
  45. log.Info("Registering OCSP responder handler")
  46. http.Handle(c.Path, ocsp.NewResponder(src, nil))
  47. addr := net.JoinHostPort(c.Address, strconv.Itoa(c.Port))
  48. log.Info("Now listening on ", addr)
  49. return http.ListenAndServe(addr, nil)
  50. }
  51. // Command assembles the definition of Command 'ocspserve'
  52. var Command = &cli.Command{UsageText: ocspServerUsageText, Flags: ocspServerFlags, Main: ocspServerMain}