2019-12-20 21:30:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-03-10 04:37:41 +00:00
|
|
|
"crypto/x509"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2022-03-10 04:37:41 +00:00
|
|
|
"strings"
|
2019-12-20 21:30:05 +00:00
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2022-11-04 23:42:07 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2022-03-10 04:37:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
authorizationHeader = "Authorization"
|
|
|
|
bearerScheme = "Bearer"
|
2019-12-20 21:30:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Renew uses the information of certificate in the TLS connection to create a
|
|
|
|
// new one.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Renew(w http.ResponseWriter, r *http.Request) {
|
2022-11-04 23:42:07 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
// Get the leaf certificate from the peer or the token.
|
|
|
|
cert, token, err := getPeerCertificate(r)
|
2022-03-10 04:37:41 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-04 23:42:07 +00:00
|
|
|
// The token can be used by RAs to renew a certificate.
|
|
|
|
if token != "" {
|
|
|
|
ctx = authority.NewTokenContext(ctx, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := mustAuthority(ctx)
|
|
|
|
certChain, err := a.RenewContext(ctx, cert, nil)
|
2019-12-20 21:30:05 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.Wrap(http.StatusInternalServerError, err, "cahandler.Renew"))
|
2019-12-20 21:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
certChainPEM := certChainToPEM(certChain)
|
|
|
|
var caPEM Certificate
|
2020-01-24 06:04:34 +00:00
|
|
|
if len(certChainPEM) > 1 {
|
2019-12-20 21:30:05 +00:00
|
|
|
caPEM = certChainPEM[1]
|
|
|
|
}
|
|
|
|
|
2020-08-12 22:50:45 +00:00
|
|
|
LogCertificate(w, certChain[0])
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSONStatus(w, &SignResponse{
|
2019-12-20 21:30:05 +00:00
|
|
|
ServerPEM: certChainPEM[0],
|
|
|
|
CaPEM: caPEM,
|
|
|
|
CertChainPEM: certChainPEM,
|
2022-04-26 19:58:40 +00:00
|
|
|
TLSOptions: a.GetTLSOptions(),
|
2019-12-20 21:30:05 +00:00
|
|
|
}, http.StatusCreated)
|
|
|
|
}
|
2022-03-10 04:37:41 +00:00
|
|
|
|
2022-11-04 23:42:07 +00:00
|
|
|
func getPeerCertificate(r *http.Request) (*x509.Certificate, string, error) {
|
2022-03-10 04:37:41 +00:00
|
|
|
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
|
2022-11-04 23:42:07 +00:00
|
|
|
return r.TLS.PeerCertificates[0], "", nil
|
2022-03-10 04:37:41 +00:00
|
|
|
}
|
|
|
|
if s := r.Header.Get(authorizationHeader); s != "" {
|
|
|
|
if parts := strings.SplitN(s, bearerScheme+" ", 2); len(parts) == 2 {
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := r.Context()
|
2022-11-04 23:42:07 +00:00
|
|
|
peer, err := mustAuthority(ctx).AuthorizeRenewToken(ctx, parts[1])
|
|
|
|
return peer, parts[1], err
|
2022-03-10 04:37:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-04 23:42:07 +00:00
|
|
|
return nil, "", errs.BadRequest("missing client certificate")
|
2022-03-10 04:37:41 +00:00
|
|
|
}
|