gencrl.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Package gencrl implements the HTTP handler for the gencrl commands.
  2. package gencrl
  3. import (
  4. "crypto/rand"
  5. "crypto/x509/pkix"
  6. "encoding/json"
  7. "github.com/cloudflare/cfssl/api"
  8. "github.com/cloudflare/cfssl/errors"
  9. "github.com/cloudflare/cfssl/helpers"
  10. "github.com/cloudflare/cfssl/log"
  11. "io/ioutil"
  12. "math/big"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // This type is meant to be unmarshalled from JSON
  19. type jsonCRLRequest struct {
  20. Certificate string `json:"certificate"`
  21. SerialNumber []string `json:"serialNumber"`
  22. PrivateKey string `json:"issuingKey"`
  23. ExpiryTime string `json:"expireTime"`
  24. }
  25. // Handle responds to requests for crl generation. It creates this crl
  26. // based off of the given certificate, serial numbers, and private key
  27. func gencrlHandler(w http.ResponseWriter, r *http.Request) error {
  28. var revokedCerts []pkix.RevokedCertificate
  29. var oneWeek = time.Duration(604800) * time.Second
  30. var newExpiryTime = time.Now()
  31. body, err := ioutil.ReadAll(r.Body)
  32. if err != nil {
  33. return err
  34. }
  35. r.Body.Close()
  36. req := &jsonCRLRequest{}
  37. err = json.Unmarshal(body, req)
  38. if err != nil {
  39. log.Error(err)
  40. }
  41. if req.ExpiryTime != "" {
  42. expiryTime := strings.TrimSpace(req.ExpiryTime)
  43. expiryInt, err := strconv.ParseInt(expiryTime, 0, 32)
  44. if err != nil {
  45. return err
  46. }
  47. newExpiryTime = time.Now().Add((time.Duration(expiryInt) * time.Second))
  48. }
  49. if req.ExpiryTime == "" {
  50. newExpiryTime = time.Now().Add(oneWeek)
  51. }
  52. if err != nil {
  53. return err
  54. }
  55. cert, err := helpers.ParseCertificatePEM([]byte(req.Certificate))
  56. if err != nil {
  57. log.Error("error from ParseCertificatePEM", err)
  58. return errors.NewBadRequestString("malformed certificate")
  59. }
  60. for _, value := range req.SerialNumber {
  61. tempBigInt := new(big.Int)
  62. tempBigInt.SetString(value, 10)
  63. tempCert := pkix.RevokedCertificate{
  64. SerialNumber: tempBigInt,
  65. RevocationTime: time.Now(),
  66. }
  67. revokedCerts = append(revokedCerts, tempCert)
  68. }
  69. key, err := helpers.ParsePrivateKeyPEM([]byte(req.PrivateKey))
  70. if err != nil {
  71. log.Debug("malformed private key %v", err)
  72. return errors.NewBadRequestString("malformed Private Key")
  73. }
  74. result, err := cert.CreateCRL(rand.Reader, key, revokedCerts, time.Now(), newExpiryTime)
  75. if err != nil {
  76. log.Debug("unable to create CRL: %v", err)
  77. return err
  78. }
  79. return api.SendResponse(w, result)
  80. }
  81. // NewHandler returns a new http.Handler that handles a crl generation request.
  82. func NewHandler() http.Handler {
  83. return api.HTTPHandler{
  84. Handler: api.HandlerFunc(gencrlHandler),
  85. Methods: []string{"POST"},
  86. }
  87. }