sign.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package api
  2. import (
  3. "crypto/tls"
  4. "encoding/json"
  5. "net/http"
  6. "github.com/smallstep/certificates/api/read"
  7. "github.com/smallstep/certificates/authority/config"
  8. "github.com/smallstep/certificates/authority/provisioner"
  9. "github.com/smallstep/certificates/errs"
  10. )
  11. // SignRequest is the request body for a certificate signature request.
  12. type SignRequest struct {
  13. CsrPEM CertificateRequest `json:"csr"`
  14. OTT string `json:"ott"`
  15. NotAfter TimeDuration `json:"notAfter,omitempty"`
  16. NotBefore TimeDuration `json:"notBefore,omitempty"`
  17. TemplateData json.RawMessage `json:"templateData,omitempty"`
  18. }
  19. // Validate checks the fields of the SignRequest and returns nil if they are ok
  20. // or an error if something is wrong.
  21. func (s *SignRequest) Validate() error {
  22. if s.CsrPEM.CertificateRequest == nil {
  23. return errs.BadRequest("missing csr")
  24. }
  25. if err := s.CsrPEM.CertificateRequest.CheckSignature(); err != nil {
  26. return errs.BadRequestErr(err, "invalid csr")
  27. }
  28. if s.OTT == "" {
  29. return errs.BadRequest("missing ott")
  30. }
  31. return nil
  32. }
  33. // SignResponse is the response object of the certificate signature request.
  34. type SignResponse struct {
  35. ServerPEM Certificate `json:"crt"`
  36. CaPEM Certificate `json:"ca"`
  37. CertChainPEM []Certificate `json:"certChain"`
  38. TLSOptions *config.TLSOptions `json:"tlsOptions,omitempty"`
  39. TLS *tls.ConnectionState `json:"-"`
  40. }
  41. // Sign is an HTTP handler that reads a certificate request and an
  42. // one-time-token (ott) from the body and creates a new certificate with the
  43. // information in the certificate request.
  44. func (h *caHandler) Sign(w http.ResponseWriter, r *http.Request) {
  45. var body SignRequest
  46. if err := read.JSON(r.Body, &body); err != nil {
  47. WriteError(w, errs.BadRequestErr(err, "error reading request body"))
  48. return
  49. }
  50. logOtt(w, body.OTT)
  51. if err := body.Validate(); err != nil {
  52. WriteError(w, err)
  53. return
  54. }
  55. opts := provisioner.SignOptions{
  56. NotBefore: body.NotBefore,
  57. NotAfter: body.NotAfter,
  58. TemplateData: body.TemplateData,
  59. }
  60. signOpts, err := h.Authority.AuthorizeSign(body.OTT)
  61. if err != nil {
  62. WriteError(w, errs.UnauthorizedErr(err))
  63. return
  64. }
  65. certChain, err := h.Authority.Sign(body.CsrPEM.CertificateRequest, opts, signOpts...)
  66. if err != nil {
  67. WriteError(w, errs.ForbiddenErr(err, "error signing certificate"))
  68. return
  69. }
  70. certChainPEM := certChainToPEM(certChain)
  71. var caPEM Certificate
  72. if len(certChainPEM) > 1 {
  73. caPEM = certChainPEM[1]
  74. }
  75. LogCertificate(w, certChain[0])
  76. JSONStatus(w, &SignResponse{
  77. ServerPEM: certChainPEM[0],
  78. CaPEM: caPEM,
  79. CertChainPEM: certChainPEM,
  80. TLSOptions: h.Authority.GetTLSOptions(),
  81. }, http.StatusCreated)
  82. }