2019-12-20 21:30:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2020-07-08 22:54:55 +00:00
|
|
|
"encoding/json"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-03-18 13:25:34 +00:00
|
|
|
"github.com/smallstep/certificates/api/read"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/config"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
"github.com/smallstep/certificates/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SignRequest is the request body for a certificate signature request.
|
|
|
|
type SignRequest struct {
|
2020-07-08 22:54:55 +00:00
|
|
|
CsrPEM CertificateRequest `json:"csr"`
|
|
|
|
OTT string `json:"ott"`
|
2020-07-30 21:59:17 +00:00
|
|
|
NotAfter TimeDuration `json:"notAfter,omitempty"`
|
|
|
|
NotBefore TimeDuration `json:"notBefore,omitempty"`
|
|
|
|
TemplateData json.RawMessage `json:"templateData,omitempty"`
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the fields of the SignRequest and returns nil if they are ok
|
|
|
|
// or an error if something is wrong.
|
|
|
|
func (s *SignRequest) Validate() error {
|
|
|
|
if s.CsrPEM.CertificateRequest == nil {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("missing csr")
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
if err := s.CsrPEM.CertificateRequest.CheckSignature(); err != nil {
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequestErr(err, "invalid csr")
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
if s.OTT == "" {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.BadRequest("missing ott")
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignResponse is the response object of the certificate signature request.
|
|
|
|
type SignResponse struct {
|
2021-05-03 19:48:20 +00:00
|
|
|
ServerPEM Certificate `json:"crt"`
|
|
|
|
CaPEM Certificate `json:"ca"`
|
|
|
|
CertChainPEM []Certificate `json:"certChain"`
|
|
|
|
TLSOptions *config.TLSOptions `json:"tlsOptions,omitempty"`
|
|
|
|
TLS *tls.ConnectionState `json:"-"`
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sign is an HTTP handler that reads a certificate request and an
|
|
|
|
// one-time-token (ott) from the body and creates a new certificate with the
|
|
|
|
// information in the certificate request.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Sign(w http.ResponseWriter, r *http.Request) {
|
2019-12-20 21:30:05 +00:00
|
|
|
var body SignRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-12-20 21:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOtt(w, body.OTT)
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-23 01:24:45 +00:00
|
|
|
opts := provisioner.SignOptions{
|
2020-07-08 22:54:55 +00:00
|
|
|
NotBefore: body.NotBefore,
|
|
|
|
NotAfter: body.NotAfter,
|
|
|
|
TemplateData: body.TemplateData,
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-05 00:35:34 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
a := mustAuthority(ctx)
|
|
|
|
|
|
|
|
ctx = provisioner.NewContextWithMethod(ctx, provisioner.SignMethod)
|
|
|
|
signOpts, err := a.Authorize(ctx, body.OTT)
|
2019-12-20 21:30:05 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.UnauthorizedErr(err))
|
2019-12-20 21:30:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
certChain, err := a.Sign(body.CsrPEM.CertificateRequest, opts, signOpts...)
|
2019-12-20 21:30:05 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error signing certificate"))
|
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)
|
|
|
|
}
|