4
0

sign.go 2.5 KB

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