renew.go 882 B

1234567891011121314151617181920212223242526272829303132333435
  1. package api
  2. import (
  3. "net/http"
  4. "github.com/smallstep/certificates/errs"
  5. )
  6. // Renew uses the information of certificate in the TLS connection to create a
  7. // new one.
  8. func (h *caHandler) Renew(w http.ResponseWriter, r *http.Request) {
  9. if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
  10. WriteError(w, errs.BadRequest("missing client certificate"))
  11. return
  12. }
  13. certChain, err := h.Authority.Renew(r.TLS.PeerCertificates[0])
  14. if err != nil {
  15. WriteError(w, errs.InternalServerErr(err, "error renewing certificate"))
  16. return
  17. }
  18. certChainPEM := certChainToPEM(certChain)
  19. var caPEM Certificate
  20. if len(certChainPEM) > 1 {
  21. caPEM = certChainPEM[1]
  22. }
  23. LogCertificate(w, certChain[0])
  24. JSONStatus(w, &SignResponse{
  25. ServerPEM: certChainPEM[0],
  26. CaPEM: caPEM,
  27. CertChainPEM: certChainPEM,
  28. TLSOptions: h.Authority.GetTLSOptions(),
  29. }, http.StatusCreated)
  30. }