2018-10-05 21:48:36 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-29 19:52:13 +00:00
|
|
|
"context"
|
2020-07-03 10:28:15 +00:00
|
|
|
"crypto"
|
2021-02-19 04:14:20 +00:00
|
|
|
"crypto/dsa" //nolint
|
2019-02-20 03:45:52 +00:00
|
|
|
"crypto/ecdsa"
|
2021-05-07 01:09:40 +00:00
|
|
|
"crypto/ed25519"
|
2019-02-20 03:45:52 +00:00
|
|
|
"crypto/rsa"
|
2018-10-05 21:48:36 +00:00
|
|
|
"crypto/x509"
|
2019-02-20 03:45:52 +00:00
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/base64"
|
2018-10-05 21:48:36 +00:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
2019-02-20 03:45:52 +00:00
|
|
|
"fmt"
|
2018-10-05 21:48:36 +00:00
|
|
|
"net/http"
|
2018-10-26 01:53:13 +00:00
|
|
|
"strconv"
|
2018-10-05 21:48:36 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/pkg/errors"
|
2022-03-18 13:25:34 +00:00
|
|
|
|
2022-03-28 14:18:18 +00:00
|
|
|
"github.com/smallstep/certificates/api/log"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/config"
|
2019-03-06 23:01:16 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-02-20 03:45:52 +00:00
|
|
|
"github.com/smallstep/certificates/logging"
|
2018-10-05 21:48:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authority is the interface implemented by a CA authority.
|
|
|
|
type Authority interface {
|
2019-07-24 01:46:43 +00:00
|
|
|
SSHAuthority
|
2019-05-27 00:41:10 +00:00
|
|
|
// context specifies the Authorize[Sign|Revoke|etc.] method.
|
2019-07-29 19:52:13 +00:00
|
|
|
Authorize(ctx context.Context, ott string) ([]provisioner.SignOption, error)
|
2022-03-11 18:05:35 +00:00
|
|
|
AuthorizeRenewToken(ctx context.Context, ott string) (*x509.Certificate, error)
|
2021-05-03 19:48:20 +00:00
|
|
|
GetTLSOptions() *config.TLSOptions
|
2018-10-05 21:48:36 +00:00
|
|
|
Root(shasum string) (*x509.Certificate, error)
|
2020-07-23 01:24:45 +00:00
|
|
|
Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
|
2019-10-09 19:57:12 +00:00
|
|
|
Renew(peer *x509.Certificate) ([]*x509.Certificate, error)
|
2020-07-08 06:17:59 +00:00
|
|
|
Rekey(peer *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error)
|
2019-03-05 08:07:13 +00:00
|
|
|
LoadProvisionerByCertificate(*x509.Certificate) (provisioner.Interface, error)
|
2021-05-03 19:48:20 +00:00
|
|
|
LoadProvisionerByName(string) (provisioner.Interface, error)
|
2019-03-07 21:07:39 +00:00
|
|
|
GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
|
2019-10-28 18:50:43 +00:00
|
|
|
Revoke(context.Context, *authority.RevokeOptions) error
|
2018-10-09 02:06:30 +00:00
|
|
|
GetEncryptedKey(kid string) (string, error)
|
2022-03-10 04:37:41 +00:00
|
|
|
GetRoots() ([]*x509.Certificate, error)
|
2019-01-12 03:08:08 +00:00
|
|
|
GetFederation() ([]*x509.Certificate, error)
|
2019-11-21 01:01:31 +00:00
|
|
|
Version() authority.Version
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 17:38:53 +00:00
|
|
|
// mustAuthority will be replaced on unit tests.
|
|
|
|
var mustAuthority = func(ctx context.Context) Authority {
|
2022-04-27 18:38:06 +00:00
|
|
|
return authority.MustFromContext(ctx)
|
2022-04-26 19:58:40 +00:00
|
|
|
}
|
|
|
|
|
2019-03-23 01:55:28 +00:00
|
|
|
// TimeDuration is an alias of provisioner.TimeDuration
|
|
|
|
type TimeDuration = provisioner.TimeDuration
|
|
|
|
|
2019-03-25 19:35:21 +00:00
|
|
|
// NewTimeDuration returns a TimeDuration with the defined time.
|
|
|
|
func NewTimeDuration(t time.Time) TimeDuration {
|
|
|
|
return provisioner.NewTimeDuration(t)
|
|
|
|
}
|
|
|
|
|
2019-03-23 01:55:28 +00:00
|
|
|
// ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or
|
|
|
|
// time.Duration string.
|
|
|
|
func ParseTimeDuration(s string) (TimeDuration, error) {
|
|
|
|
return provisioner.ParseTimeDuration(s)
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Certificate wraps a *x509.Certificate and adds the json.Marshaler interface.
|
|
|
|
type Certificate struct {
|
|
|
|
*x509.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCertificate is a helper method that returns a Certificate from a
|
|
|
|
// *x509.Certificate.
|
|
|
|
func NewCertificate(cr *x509.Certificate) Certificate {
|
|
|
|
return Certificate{
|
|
|
|
Certificate: cr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 01:01:31 +00:00
|
|
|
// reset sets the inner x509.CertificateRequest to nil
|
|
|
|
func (c *Certificate) reset() {
|
|
|
|
if c != nil {
|
|
|
|
c.Certificate = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface. The certificate is
|
|
|
|
// quoted string using the PEM encoding.
|
|
|
|
func (c Certificate) MarshalJSON() ([]byte, error) {
|
|
|
|
if c.Certificate == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
block := pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: c.Raw,
|
|
|
|
})
|
|
|
|
return json.Marshal(string(block))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
|
|
|
|
// expected to be a quoted string using the PEM encoding.
|
|
|
|
func (c *Certificate) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding certificate")
|
|
|
|
}
|
2019-11-21 01:01:31 +00:00
|
|
|
|
|
|
|
// Make sure the inner x509.Certificate is nil
|
|
|
|
if s == "null" || s == "" {
|
|
|
|
c.reset()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
block, _ := pem.Decode([]byte(s))
|
|
|
|
if block == nil {
|
|
|
|
return errors.New("error decoding certificate")
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding certificate")
|
|
|
|
}
|
|
|
|
c.Certificate = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CertificateRequest wraps a *x509.CertificateRequest and adds the
|
|
|
|
// json.Unmarshaler interface.
|
|
|
|
type CertificateRequest struct {
|
|
|
|
*x509.CertificateRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCertificateRequest is a helper method that returns a CertificateRequest
|
|
|
|
// from a *x509.CertificateRequest.
|
|
|
|
func NewCertificateRequest(cr *x509.CertificateRequest) CertificateRequest {
|
|
|
|
return CertificateRequest{
|
|
|
|
CertificateRequest: cr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 01:01:31 +00:00
|
|
|
// reset sets the inner x509.CertificateRequest to nil
|
|
|
|
func (c *CertificateRequest) reset() {
|
|
|
|
if c != nil {
|
|
|
|
c.CertificateRequest = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface. The certificate request
|
|
|
|
// is a quoted string using the PEM encoding.
|
|
|
|
func (c CertificateRequest) MarshalJSON() ([]byte, error) {
|
|
|
|
if c.CertificateRequest == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
block := pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE REQUEST",
|
|
|
|
Bytes: c.Raw,
|
|
|
|
})
|
|
|
|
return json.Marshal(string(block))
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface. The certificate
|
|
|
|
// request is expected to be a quoted string using the PEM encoding.
|
|
|
|
func (c *CertificateRequest) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding csr")
|
|
|
|
}
|
2019-11-21 01:01:31 +00:00
|
|
|
|
|
|
|
// Make sure the inner x509.CertificateRequest is nil
|
|
|
|
if s == "null" || s == "" {
|
|
|
|
c.reset()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
block, _ := pem.Decode([]byte(s))
|
|
|
|
if block == nil {
|
|
|
|
return errors.New("error decoding csr")
|
|
|
|
}
|
|
|
|
cr, err := x509.ParseCertificateRequest(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding csr")
|
|
|
|
}
|
|
|
|
c.CertificateRequest = cr
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Router defines a common router interface.
|
|
|
|
type Router interface {
|
|
|
|
// MethodFunc adds routes for `pattern` that matches
|
|
|
|
// the `method` HTTP method.
|
|
|
|
MethodFunc(method, pattern string, h http.HandlerFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouterHandler is the interface that a HTTP handler that manages multiple
|
|
|
|
// endpoints will implement.
|
|
|
|
type RouterHandler interface {
|
|
|
|
Route(r Router)
|
|
|
|
}
|
|
|
|
|
2019-11-21 01:01:31 +00:00
|
|
|
// VersionResponse is the response object that returns the version of the
|
|
|
|
// server.
|
|
|
|
type VersionResponse struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
RequireClientAuthentication bool `json:"requireClientAuthentication,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// HealthResponse is the response object that returns the health of the server.
|
|
|
|
type HealthResponse struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// RootResponse is the response object that returns the PEM of a root certificate.
|
|
|
|
type RootResponse struct {
|
|
|
|
RootPEM Certificate `json:"ca"`
|
|
|
|
}
|
|
|
|
|
2018-10-12 06:03:00 +00:00
|
|
|
// ProvisionersResponse is the response object that returns the list of
|
2018-10-09 02:06:30 +00:00
|
|
|
// provisioners.
|
|
|
|
type ProvisionersResponse struct {
|
2019-03-07 21:07:39 +00:00
|
|
|
Provisioners provisioner.List `json:"provisioners"`
|
|
|
|
NextCursor string `json:"nextCursor"`
|
2018-10-12 06:03:00 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 23:14:18 +00:00
|
|
|
// ProvisionerKeyResponse is the response object that returns the encrypted key
|
2018-10-09 02:06:30 +00:00
|
|
|
// of a provisioner.
|
|
|
|
type ProvisionerKeyResponse struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
}
|
|
|
|
|
2019-01-08 01:48:56 +00:00
|
|
|
// RootsResponse is the response object of the roots request.
|
|
|
|
type RootsResponse struct {
|
|
|
|
Certificates []Certificate `json:"crts"`
|
|
|
|
}
|
|
|
|
|
2019-01-05 00:51:37 +00:00
|
|
|
// FederationResponse is the response object of the federation request.
|
|
|
|
type FederationResponse struct {
|
|
|
|
Certificates []Certificate `json:"crts"`
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// caHandler is the type used to implement the different CA HTTP endpoints.
|
2022-04-27 17:38:53 +00:00
|
|
|
type caHandler struct {
|
|
|
|
Authority Authority
|
|
|
|
}
|
2022-04-26 21:32:55 +00:00
|
|
|
|
|
|
|
// Route configures the http request router.
|
|
|
|
func (h *caHandler) Route(r Router) {
|
|
|
|
Route(r)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new RouterHandler with the CA endpoints.
|
2022-04-26 21:32:55 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use api.Route(r Router)
|
2021-10-08 18:59:57 +00:00
|
|
|
func New(auth Authority) RouterHandler {
|
2022-04-26 21:32:55 +00:00
|
|
|
return &caHandler{}
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 21:32:55 +00:00
|
|
|
func Route(r Router) {
|
2022-04-26 19:58:40 +00:00
|
|
|
r.MethodFunc("GET", "/version", Version)
|
|
|
|
r.MethodFunc("GET", "/health", Health)
|
|
|
|
r.MethodFunc("GET", "/root/{sha}", Root)
|
|
|
|
r.MethodFunc("POST", "/sign", Sign)
|
|
|
|
r.MethodFunc("POST", "/renew", Renew)
|
|
|
|
r.MethodFunc("POST", "/rekey", Rekey)
|
|
|
|
r.MethodFunc("POST", "/revoke", Revoke)
|
|
|
|
r.MethodFunc("GET", "/provisioners", Provisioners)
|
|
|
|
r.MethodFunc("GET", "/provisioners/{kid}/encrypted-key", ProvisionerKey)
|
|
|
|
r.MethodFunc("GET", "/roots", Roots)
|
|
|
|
r.MethodFunc("GET", "/roots.pem", RootsPEM)
|
|
|
|
r.MethodFunc("GET", "/federation", Federation)
|
2019-09-26 20:22:07 +00:00
|
|
|
// SSH CA
|
2022-04-26 19:58:40 +00:00
|
|
|
r.MethodFunc("POST", "/ssh/sign", SSHSign)
|
|
|
|
r.MethodFunc("POST", "/ssh/renew", SSHRenew)
|
|
|
|
r.MethodFunc("POST", "/ssh/revoke", SSHRevoke)
|
|
|
|
r.MethodFunc("POST", "/ssh/rekey", SSHRekey)
|
|
|
|
r.MethodFunc("GET", "/ssh/roots", SSHRoots)
|
|
|
|
r.MethodFunc("GET", "/ssh/federation", SSHFederation)
|
|
|
|
r.MethodFunc("POST", "/ssh/config", SSHConfig)
|
|
|
|
r.MethodFunc("POST", "/ssh/config/{type}", SSHConfig)
|
|
|
|
r.MethodFunc("POST", "/ssh/check-host", SSHCheckHost)
|
|
|
|
r.MethodFunc("GET", "/ssh/hosts", SSHGetHosts)
|
|
|
|
r.MethodFunc("POST", "/ssh/bastion", SSHBastion)
|
2019-09-26 20:22:07 +00:00
|
|
|
|
2018-10-24 23:31:28 +00:00
|
|
|
// For compatibility with old code:
|
2022-04-26 19:58:40 +00:00
|
|
|
r.MethodFunc("POST", "/re-sign", Renew)
|
|
|
|
r.MethodFunc("POST", "/sign-ssh", SSHSign)
|
|
|
|
r.MethodFunc("GET", "/ssh/get-hosts", SSHGetHosts)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 01:01:31 +00:00
|
|
|
// Version is an HTTP handler that returns the version of the server.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Version(w http.ResponseWriter, r *http.Request) {
|
|
|
|
v := mustAuthority(r.Context()).Version()
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, VersionResponse{
|
2019-11-21 01:01:31 +00:00
|
|
|
Version: v.Version,
|
|
|
|
RequireClientAuthentication: v.RequireClientAuthentication,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +00:00
|
|
|
// Health is an HTTP handler that returns the status of the server.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Health(w http.ResponseWriter, r *http.Request) {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, HealthResponse{Status: "ok"})
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root is an HTTP handler that using the SHA256 from the URL, returns the root
|
|
|
|
// certificate for the given SHA256.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Root(w http.ResponseWriter, r *http.Request) {
|
2018-10-05 21:48:36 +00:00
|
|
|
sha := chi.URLParam(r, "sha")
|
2021-10-08 18:59:57 +00:00
|
|
|
sum := strings.ToLower(strings.ReplaceAll(sha, "-", ""))
|
2018-10-05 21:48:36 +00:00
|
|
|
// Load root certificate with the
|
2022-04-26 19:58:40 +00:00
|
|
|
cert, err := mustAuthority(r.Context()).Root(sum)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.Wrapf(http.StatusNotFound, err, "%s was not found", r.RequestURI))
|
2018-10-05 21:48:36 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &RootResponse{RootPEM: Certificate{cert}})
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 19:57:12 +00:00
|
|
|
func certChainToPEM(certChain []*x509.Certificate) []Certificate {
|
|
|
|
certChainPEM := make([]Certificate, 0, len(certChain))
|
|
|
|
for _, c := range certChain {
|
|
|
|
certChainPEM = append(certChainPEM, Certificate{c})
|
|
|
|
}
|
|
|
|
return certChainPEM
|
|
|
|
}
|
|
|
|
|
2018-10-09 02:06:30 +00:00
|
|
|
// Provisioners returns the list of provisioners configured in the authority.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Provisioners(w http.ResponseWriter, r *http.Request) {
|
2021-05-18 23:50:54 +00:00
|
|
|
cursor, limit, err := ParseCursor(r)
|
2018-10-26 01:53:13 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2018-10-26 01:53:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
p, next, err := mustAuthority(r.Context()).GetProvisioners(cursor, limit)
|
2018-10-09 02:06:30 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2018-10-09 02:06:30 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-26 19:58:40 +00:00
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &ProvisionersResponse{
|
2018-10-26 01:53:13 +00:00
|
|
|
Provisioners: p,
|
|
|
|
NextCursor: next,
|
|
|
|
})
|
2018-10-09 02:06:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerKey returns the encrypted key of a provisioner by it's key id.
|
2022-04-26 19:58:40 +00:00
|
|
|
func ProvisionerKey(w http.ResponseWriter, r *http.Request) {
|
2018-10-09 02:06:30 +00:00
|
|
|
kid := chi.URLParam(r, "kid")
|
2022-04-26 19:58:40 +00:00
|
|
|
key, err := mustAuthority(r.Context()).GetEncryptedKey(kid)
|
2018-10-09 02:06:30 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.NotFoundErr(err))
|
2018-10-09 02:06:30 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-26 19:58:40 +00:00
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &ProvisionerKeyResponse{key})
|
2018-10-09 02:06:30 +00:00
|
|
|
}
|
2018-10-12 06:03:00 +00:00
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
// Roots returns all the root certificates for the CA.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Roots(w http.ResponseWriter, r *http.Request) {
|
|
|
|
roots, err := mustAuthority(r.Context()).GetRoots()
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error getting roots"))
|
2019-01-08 01:48:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := make([]Certificate, len(roots))
|
|
|
|
for i := range roots {
|
|
|
|
certs[i] = Certificate{roots[i]}
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSONStatus(w, &RootsResponse{
|
2019-01-08 01:48:56 +00:00
|
|
|
Certificates: certs,
|
2019-05-27 00:41:10 +00:00
|
|
|
}, http.StatusCreated)
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 14:18:18 +00:00
|
|
|
// RootsPEM returns all the root certificates for the CA in PEM format.
|
2022-04-26 19:58:40 +00:00
|
|
|
func RootsPEM(w http.ResponseWriter, r *http.Request) {
|
|
|
|
roots, err := mustAuthority(r.Context()).GetRoots()
|
2022-03-28 14:18:18 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2022-03-28 14:18:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/x-pem-file")
|
|
|
|
|
|
|
|
for _, root := range roots {
|
|
|
|
block := pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: root.Raw,
|
|
|
|
})
|
|
|
|
|
|
|
|
if _, err := w.Write(block); err != nil {
|
|
|
|
log.Error(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
// Federation returns all the public certificates in the federation.
|
2022-04-26 19:58:40 +00:00
|
|
|
func Federation(w http.ResponseWriter, r *http.Request) {
|
|
|
|
federated, err := mustAuthority(r.Context()).GetFederation()
|
2019-01-05 00:51:37 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error getting federated roots"))
|
2019-01-05 00:51:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := make([]Certificate, len(federated))
|
|
|
|
for i := range federated {
|
|
|
|
certs[i] = Certificate{federated[i]}
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSONStatus(w, &FederationResponse{
|
2019-01-05 00:51:37 +00:00
|
|
|
Certificates: certs,
|
2019-05-27 00:41:10 +00:00
|
|
|
}, http.StatusCreated)
|
2019-01-05 00:51:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 03:45:52 +00:00
|
|
|
var oidStepProvisioner = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 37476, 9000, 64, 1}
|
|
|
|
|
|
|
|
type stepProvisioner struct {
|
|
|
|
Type int
|
|
|
|
Name []byte
|
|
|
|
CredentialID []byte
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:34:40 +00:00
|
|
|
func logOtt(w http.ResponseWriter, token string) {
|
|
|
|
if rl, ok := w.(logging.ResponseLogger); ok {
|
|
|
|
rl.WithFields(map[string]interface{}{
|
|
|
|
"ott": token,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 22:50:45 +00:00
|
|
|
// LogCertificate add certificate fields to the log message.
|
|
|
|
func LogCertificate(w http.ResponseWriter, cert *x509.Certificate) {
|
2019-02-20 03:45:52 +00:00
|
|
|
if rl, ok := w.(logging.ResponseLogger); ok {
|
|
|
|
m := map[string]interface{}{
|
2021-07-14 09:25:56 +00:00
|
|
|
"serial": cert.SerialNumber.String(),
|
2019-02-20 03:45:52 +00:00
|
|
|
"subject": cert.Subject.CommonName,
|
|
|
|
"issuer": cert.Issuer.CommonName,
|
|
|
|
"valid-from": cert.NotBefore.Format(time.RFC3339),
|
|
|
|
"valid-to": cert.NotAfter.Format(time.RFC3339),
|
|
|
|
"public-key": fmtPublicKey(cert),
|
|
|
|
"certificate": base64.StdEncoding.EncodeToString(cert.Raw),
|
|
|
|
}
|
|
|
|
for _, ext := range cert.Extensions {
|
2021-10-08 18:59:57 +00:00
|
|
|
if !ext.Id.Equal(oidStepProvisioner) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
val := &stepProvisioner{}
|
|
|
|
rest, err := asn1.Unmarshal(ext.Value, val)
|
|
|
|
if err != nil || len(rest) > 0 {
|
2019-02-20 03:45:52 +00:00
|
|
|
break
|
|
|
|
}
|
2021-10-08 18:59:57 +00:00
|
|
|
if len(val.CredentialID) > 0 {
|
|
|
|
m["provisioner"] = fmt.Sprintf("%s (%s)", val.Name, val.CredentialID)
|
|
|
|
} else {
|
|
|
|
m["provisioner"] = string(val.Name)
|
|
|
|
}
|
|
|
|
break
|
2019-02-20 03:45:52 +00:00
|
|
|
}
|
|
|
|
rl.WithFields(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:50:54 +00:00
|
|
|
// ParseCursor parses the cursor and limit from the request query params.
|
|
|
|
func ParseCursor(r *http.Request) (cursor string, limit int, err error) {
|
2018-10-26 01:53:13 +00:00
|
|
|
q := r.URL.Query()
|
|
|
|
cursor = q.Get("cursor")
|
|
|
|
if v := q.Get("limit"); len(v) > 0 {
|
|
|
|
limit, err = strconv.Atoi(v)
|
|
|
|
if err != nil {
|
2021-11-19 02:17:36 +00:00
|
|
|
return "", 0, errs.BadRequestErr(err, "limit '%s' is not an integer", v)
|
2018-10-26 01:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-02-20 03:45:52 +00:00
|
|
|
|
|
|
|
func fmtPublicKey(cert *x509.Certificate) string {
|
|
|
|
var params string
|
|
|
|
switch pk := cert.PublicKey.(type) {
|
|
|
|
case *ecdsa.PublicKey:
|
|
|
|
params = pk.Curve.Params().Name
|
|
|
|
case *rsa.PublicKey:
|
2019-02-20 20:18:13 +00:00
|
|
|
params = strconv.Itoa(pk.Size() * 8)
|
2021-05-07 01:09:40 +00:00
|
|
|
case ed25519.PublicKey:
|
|
|
|
return cert.PublicKeyAlgorithm.String()
|
2019-02-20 20:18:13 +00:00
|
|
|
case *dsa.PublicKey:
|
|
|
|
params = strconv.Itoa(pk.Q.BitLen() * 8)
|
2019-02-20 03:45:52 +00:00
|
|
|
default:
|
|
|
|
params = "unknown"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s %s", cert.PublicKeyAlgorithm, params)
|
|
|
|
}
|