ocspsign.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Package ocsp implements the HTTP handler for the ocsp commands.
  2. package ocsp
  3. import (
  4. "crypto"
  5. "net/http"
  6. "encoding/base64"
  7. "encoding/json"
  8. "io/ioutil"
  9. "time"
  10. "github.com/cloudflare/cfssl/api"
  11. "github.com/cloudflare/cfssl/errors"
  12. "github.com/cloudflare/cfssl/helpers"
  13. "github.com/cloudflare/cfssl/log"
  14. "github.com/cloudflare/cfssl/ocsp"
  15. )
  16. // A Handler accepts requests with a certficate parameter
  17. // (which should be PEM-encoded) and returns a signed ocsp
  18. // response.
  19. type Handler struct {
  20. signer ocsp.Signer
  21. }
  22. // NewHandler returns a new http.Handler that handles a ocspsign request.
  23. func NewHandler(s ocsp.Signer) http.Handler {
  24. return &api.HTTPHandler{
  25. Handler: &Handler{
  26. signer: s,
  27. },
  28. Methods: []string{"POST"},
  29. }
  30. }
  31. // This type is meant to be unmarshalled from JSON
  32. type jsonSignRequest struct {
  33. Certificate string `json:"certificate"`
  34. Status string `json:"status"`
  35. Reason int `json:"reason,omitempty"`
  36. RevokedAt string `json:"revoked_at,omitempty"`
  37. IssuerHash string `json:"issuer_hash,omitempty"`
  38. }
  39. var nameToHash = map[string]crypto.Hash{
  40. "MD5": crypto.MD5,
  41. "SHA1": crypto.SHA1,
  42. "SHA256": crypto.SHA256,
  43. "SHA384": crypto.SHA384,
  44. "SHA512": crypto.SHA512,
  45. }
  46. // Handle responds to requests for a ocsp signature. It creates and signs
  47. // a ocsp response for the provided certificate and status. If the status
  48. // is revoked then it also adds reason and revoked_at. The response is
  49. // base64 encoded.
  50. func (h *Handler) Handle(w http.ResponseWriter, r *http.Request) error {
  51. body, err := ioutil.ReadAll(r.Body)
  52. if err != nil {
  53. return err
  54. }
  55. r.Body.Close()
  56. // Default the status to good so it matches the cli
  57. req := &jsonSignRequest{
  58. Status: "good",
  59. }
  60. err = json.Unmarshal(body, req)
  61. if err != nil {
  62. return errors.NewBadRequestString("Unable to parse sign request")
  63. }
  64. cert, err := helpers.ParseCertificatePEM([]byte(req.Certificate))
  65. if err != nil {
  66. log.Error("Error from ParseCertificatePEM", err)
  67. return errors.NewBadRequestString("Malformed certificate")
  68. }
  69. signReq := ocsp.SignRequest{
  70. Certificate: cert,
  71. Status: req.Status,
  72. }
  73. // We need to convert the time from being a string to a time.Time
  74. if req.Status == "revoked" {
  75. signReq.Reason = req.Reason
  76. // "now" is accepted and the default on the cli so default that here
  77. if req.RevokedAt == "" || req.RevokedAt == "now" {
  78. signReq.RevokedAt = time.Now()
  79. } else {
  80. signReq.RevokedAt, err = time.Parse("2006-01-02", req.RevokedAt)
  81. if err != nil {
  82. return errors.NewBadRequestString("Malformed revocation time")
  83. }
  84. }
  85. }
  86. if req.IssuerHash != "" {
  87. issuerHash, ok := nameToHash[req.IssuerHash]
  88. if !ok {
  89. return errors.NewBadRequestString("Unsupported hash algorithm in request")
  90. }
  91. signReq.IssuerHash = issuerHash
  92. }
  93. resp, err := h.signer.Sign(signReq)
  94. if err != nil {
  95. return err
  96. }
  97. b64Resp := base64.StdEncoding.EncodeToString(resp)
  98. result := map[string]string{"ocspResponse": b64Resp}
  99. return api.SendResponse(w, result)
  100. }