sign.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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"`
  17. NotBefore TimeDuration `json:"notBefore"`
  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 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, r, 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, r, err)
  54. return
  55. }
  56. opts := provisioner.SignOptions{
  57. NotBefore: body.NotBefore,
  58. NotAfter: body.NotAfter,
  59. TemplateData: body.TemplateData,
  60. }
  61. ctx := r.Context()
  62. a := mustAuthority(ctx)
  63. ctx = provisioner.NewContextWithMethod(ctx, provisioner.SignMethod)
  64. signOpts, err := a.Authorize(ctx, body.OTT)
  65. if err != nil {
  66. render.Error(w, r, errs.UnauthorizedErr(err))
  67. return
  68. }
  69. certChain, err := a.SignWithContext(ctx, body.CsrPEM.CertificateRequest, opts, signOpts...)
  70. if err != nil {
  71. render.Error(w, r, errs.ForbiddenErr(err, "error signing certificate"))
  72. return
  73. }
  74. certChainPEM := certChainToPEM(certChain)
  75. var caPEM Certificate
  76. if len(certChainPEM) > 1 {
  77. caPEM = certChainPEM[1]
  78. }
  79. LogCertificate(w, certChain[0])
  80. render.JSONStatus(w, r, &SignResponse{
  81. ServerPEM: certChainPEM[0],
  82. CaPEM: caPEM,
  83. CertChainPEM: certChainPEM,
  84. TLSOptions: a.GetTLSOptions(),
  85. }, http.StatusCreated)
  86. }