sign.go 2.8 KB

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