sign.go 2.6 KB

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