forked from TrueCloudLab/certificates
00634fb648
* api/render: initial implementation of the package * acme/api: refactored to support api/render * authority/admin: refactored to support api/render * ca: refactored to support api/render * api: refactored to support api/render * api/render: implemented Error * api: refactored to support api/render.Error * acme/api: refactored to support api/render.Error * authority/admin: refactored to support api/render.Error * ca: refactored to support api/render.Error * ca: fixed broken tests * api/render, api/log: moved error logging to this package * acme: refactored Error so that it implements render.RenderableError * authority/admin: refactored Error so that it implements render.RenderableError * api/render: implemented RenderableError * api/render: added test coverage for Error * api/render: implemented statusCodeFromError * api: refactored RootsPEM to work with render.Error * acme, authority/admin: fixed pointer receiver name for consistency * api/render, errs: moved StatusCoder & StackTracer to the render package
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/smallstep/certificates/api/render"
|
|
"github.com/smallstep/certificates/errs"
|
|
)
|
|
|
|
const (
|
|
authorizationHeader = "Authorization"
|
|
bearerScheme = "Bearer"
|
|
)
|
|
|
|
// Renew uses the information of certificate in the TLS connection to create a
|
|
// new one.
|
|
func (h *caHandler) Renew(w http.ResponseWriter, r *http.Request) {
|
|
cert, err := h.getPeerCertificate(r)
|
|
if err != nil {
|
|
render.Error(w, err)
|
|
return
|
|
}
|
|
|
|
certChain, err := h.Authority.Renew(cert)
|
|
if err != nil {
|
|
render.Error(w, errs.Wrap(http.StatusInternalServerError, err, "cahandler.Renew"))
|
|
return
|
|
}
|
|
certChainPEM := certChainToPEM(certChain)
|
|
var caPEM Certificate
|
|
if len(certChainPEM) > 1 {
|
|
caPEM = certChainPEM[1]
|
|
}
|
|
|
|
LogCertificate(w, certChain[0])
|
|
render.JSONStatus(w, &SignResponse{
|
|
ServerPEM: certChainPEM[0],
|
|
CaPEM: caPEM,
|
|
CertChainPEM: certChainPEM,
|
|
TLSOptions: h.Authority.GetTLSOptions(),
|
|
}, http.StatusCreated)
|
|
}
|
|
|
|
func (h *caHandler) getPeerCertificate(r *http.Request) (*x509.Certificate, error) {
|
|
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
|
|
return r.TLS.PeerCertificates[0], nil
|
|
}
|
|
if s := r.Header.Get(authorizationHeader); s != "" {
|
|
if parts := strings.SplitN(s, bearerScheme+" ", 2); len(parts) == 2 {
|
|
return h.Authority.AuthorizeRenewToken(r.Context(), parts[1])
|
|
}
|
|
}
|
|
return nil, errs.BadRequest("missing client certificate")
|
|
}
|