2018-10-05 21:48:36 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-29 19:52:13 +00:00
|
|
|
"context"
|
2019-02-20 20:18:13 +00:00
|
|
|
"crypto/dsa"
|
2019-02-20 03:45:52 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rsa"
|
2018-10-05 21:48:36 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"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"
|
2019-03-05 08:07:13 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2019-03-06 23:01:16 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-02-20 03:45:52 +00:00
|
|
|
"github.com/smallstep/certificates/logging"
|
2018-10-05 21:48:36 +00:00
|
|
|
"github.com/smallstep/cli/crypto/tlsutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2019-03-05 08:07:13 +00:00
|
|
|
AuthorizeSign(ott string) ([]provisioner.SignOption, error)
|
2018-10-05 21:48:36 +00:00
|
|
|
GetTLSOptions() *tlsutil.TLSOptions
|
|
|
|
Root(shasum string) (*x509.Certificate, error)
|
2019-03-07 23:14:18 +00:00
|
|
|
Sign(cr *x509.CertificateRequest, opts provisioner.Options, signOpts ...provisioner.SignOption) (*x509.Certificate, *x509.Certificate, error)
|
2019-01-08 01:48:56 +00:00
|
|
|
Renew(peer *x509.Certificate) (*x509.Certificate, *x509.Certificate, error)
|
2019-03-05 08:07:13 +00:00
|
|
|
LoadProvisionerByCertificate(*x509.Certificate) (provisioner.Interface, error)
|
2019-05-27 00:41:10 +00:00
|
|
|
LoadProvisionerByID(string) (provisioner.Interface, error)
|
2019-03-07 21:07:39 +00:00
|
|
|
GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
|
2019-03-05 08:07:13 +00:00
|
|
|
Revoke(*authority.RevokeOptions) error
|
2018-10-09 02:06:30 +00:00
|
|
|
GetEncryptedKey(kid string) (string, error)
|
2019-01-12 03:08:08 +00:00
|
|
|
GetRoots() (federation []*x509.Certificate, err error)
|
|
|
|
GetFederation() ([]*x509.Certificate, error)
|
2018-10-05 21:48:36 +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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignRequest is the request body for a certificate signature request.
|
|
|
|
type SignRequest struct {
|
|
|
|
CsrPEM CertificateRequest `json:"csr"`
|
|
|
|
OTT string `json:"ott"`
|
2019-03-23 01:55:28 +00:00
|
|
|
NotAfter TimeDuration `json:"notAfter"`
|
|
|
|
NotBefore TimeDuration `json:"notBefore"`
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:48:36 +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 {
|
|
|
|
return BadRequest(errors.New("missing csr"))
|
|
|
|
}
|
|
|
|
if err := s.CsrPEM.CertificateRequest.CheckSignature(); err != nil {
|
|
|
|
return BadRequest(errors.Wrap(err, "invalid csr"))
|
|
|
|
}
|
|
|
|
if s.OTT == "" {
|
|
|
|
return BadRequest(errors.New("missing ott"))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SignResponse is the response object of the certificate signature request.
|
|
|
|
type SignResponse struct {
|
|
|
|
ServerPEM Certificate `json:"crt"`
|
|
|
|
CaPEM Certificate `json:"ca"`
|
|
|
|
TLSOptions *tlsutil.TLSOptions `json:"tlsOptions,omitempty"`
|
|
|
|
TLS *tls.ConnectionState `json:"-"`
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
type caHandler struct {
|
|
|
|
Authority Authority
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new RouterHandler with the CA endpoints.
|
|
|
|
func New(authority Authority) RouterHandler {
|
|
|
|
return &caHandler{
|
|
|
|
Authority: authority,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *caHandler) Route(r Router) {
|
|
|
|
r.MethodFunc("GET", "/health", h.Health)
|
|
|
|
r.MethodFunc("GET", "/root/{sha}", h.Root)
|
|
|
|
r.MethodFunc("POST", "/sign", h.Sign)
|
|
|
|
r.MethodFunc("POST", "/renew", h.Renew)
|
2019-03-05 08:07:13 +00:00
|
|
|
r.MethodFunc("POST", "/revoke", h.Revoke)
|
2018-10-09 02:06:30 +00:00
|
|
|
r.MethodFunc("GET", "/provisioners", h.Provisioners)
|
|
|
|
r.MethodFunc("GET", "/provisioners/{kid}/encrypted-key", h.ProvisionerKey)
|
2019-01-08 01:48:56 +00:00
|
|
|
r.MethodFunc("GET", "/roots", h.Roots)
|
2019-01-05 00:51:37 +00:00
|
|
|
r.MethodFunc("GET", "/federation", h.Federation)
|
2019-09-26 20:22:07 +00:00
|
|
|
// SSH CA
|
|
|
|
r.MethodFunc("GET", "/ssh/sign", h.SignSSH)
|
|
|
|
r.MethodFunc("GET", "/ssh/keys", h.SSHKeys)
|
|
|
|
|
2018-10-24 23:31:28 +00:00
|
|
|
// For compatibility with old code:
|
|
|
|
r.MethodFunc("POST", "/re-sign", h.Renew)
|
2019-07-24 01:46:43 +00:00
|
|
|
r.MethodFunc("POST", "/sign-ssh", h.SignSSH)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Health is an HTTP handler that returns the status of the server.
|
|
|
|
func (h *caHandler) Health(w http.ResponseWriter, r *http.Request) {
|
|
|
|
JSON(w, HealthResponse{Status: "ok"})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root is an HTTP handler that using the SHA256 from the URL, returns the root
|
|
|
|
// certificate for the given SHA256.
|
|
|
|
func (h *caHandler) Root(w http.ResponseWriter, r *http.Request) {
|
|
|
|
sha := chi.URLParam(r, "sha")
|
|
|
|
sum := strings.ToLower(strings.Replace(sha, "-", "", -1))
|
|
|
|
// Load root certificate with the
|
|
|
|
cert, err := h.Authority.Root(sum)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, NotFound(errors.Wrapf(err, "%s was not found", r.RequestURI)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
JSON(w, &RootResponse{RootPEM: Certificate{cert}})
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (h *caHandler) Sign(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var body SignRequest
|
|
|
|
if err := ReadJSON(r.Body, &body); err != nil {
|
|
|
|
WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
|
|
|
|
return
|
|
|
|
}
|
2019-02-20 20:34:40 +00:00
|
|
|
|
|
|
|
logOtt(w, body.OTT)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err := body.Validate(); err != nil {
|
|
|
|
WriteError(w, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:14:18 +00:00
|
|
|
opts := provisioner.Options{
|
2018-10-19 05:26:39 +00:00
|
|
|
NotBefore: body.NotBefore,
|
|
|
|
NotAfter: body.NotAfter,
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
signOpts, err := h.Authority.AuthorizeSign(body.OTT)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, Unauthorized(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-07 23:14:18 +00:00
|
|
|
cert, root, err := h.Authority.Sign(body.CsrPEM.CertificateRequest, opts, signOpts...)
|
2018-10-05 21:48:36 +00:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, Forbidden(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:34:40 +00:00
|
|
|
logCertificate(w, cert)
|
2019-05-27 00:41:10 +00:00
|
|
|
JSONStatus(w, &SignResponse{
|
2018-10-05 21:48:36 +00:00
|
|
|
ServerPEM: Certificate{cert},
|
|
|
|
CaPEM: Certificate{root},
|
|
|
|
TLSOptions: h.Authority.GetTLSOptions(),
|
2019-05-27 00:41:10 +00:00
|
|
|
}, http.StatusCreated)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if r.TLS == nil || len(r.TLS.PeerCertificates) == 0 {
|
|
|
|
WriteError(w, BadRequest(errors.New("missing peer certificate")))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cert, root, err := h.Authority.Renew(r.TLS.PeerCertificates[0])
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, Forbidden(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:34:40 +00:00
|
|
|
logCertificate(w, cert)
|
2019-05-27 00:41:10 +00:00
|
|
|
JSONStatus(w, &SignResponse{
|
2018-10-05 21:48:36 +00:00
|
|
|
ServerPEM: Certificate{cert},
|
|
|
|
CaPEM: Certificate{root},
|
|
|
|
TLSOptions: h.Authority.GetTLSOptions(),
|
2019-05-27 00:41:10 +00:00
|
|
|
}, http.StatusCreated)
|
2018-10-05 21:48:36 +00:00
|
|
|
}
|
2018-10-09 02:06:30 +00:00
|
|
|
|
|
|
|
// Provisioners returns the list of provisioners configured in the authority.
|
|
|
|
func (h *caHandler) Provisioners(w http.ResponseWriter, r *http.Request) {
|
2018-10-26 01:53:13 +00:00
|
|
|
cursor, limit, err := parseCursor(r)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, BadRequest(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p, next, err := h.Authority.GetProvisioners(cursor, limit)
|
2018-10-09 02:06:30 +00:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, InternalServerError(err))
|
|
|
|
return
|
|
|
|
}
|
2018-10-26 01:53:13 +00:00
|
|
|
JSON(w, &ProvisionersResponse{
|
|
|
|
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.
|
|
|
|
func (h *caHandler) ProvisionerKey(w http.ResponseWriter, r *http.Request) {
|
|
|
|
kid := chi.URLParam(r, "kid")
|
|
|
|
key, err := h.Authority.GetEncryptedKey(kid)
|
|
|
|
if err != nil {
|
|
|
|
WriteError(w, NotFound(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
JSON(w, &ProvisionerKeyResponse{key})
|
|
|
|
}
|
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.
|
2019-01-08 01:48:56 +00:00
|
|
|
func (h *caHandler) Roots(w http.ResponseWriter, r *http.Request) {
|
2019-01-12 03:08:08 +00:00
|
|
|
roots, err := h.Authority.GetRoots()
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, Forbidden(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := make([]Certificate, len(roots))
|
|
|
|
for i := range roots {
|
|
|
|
certs[i] = Certificate{roots[i]}
|
|
|
|
}
|
|
|
|
|
2019-05-27 00:41:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
// Federation returns all the public certificates in the federation.
|
2019-01-05 00:51:37 +00:00
|
|
|
func (h *caHandler) Federation(w http.ResponseWriter, r *http.Request) {
|
2019-01-12 03:08:08 +00:00
|
|
|
federated, err := h.Authority.GetFederation()
|
2019-01-05 00:51:37 +00:00
|
|
|
if err != nil {
|
|
|
|
WriteError(w, Forbidden(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
certs := make([]Certificate, len(federated))
|
|
|
|
for i := range federated {
|
|
|
|
certs[i] = Certificate{federated[i]}
|
|
|
|
}
|
|
|
|
|
2019-05-27 00:41:10 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{}{
|
|
|
|
"serial": cert.SerialNumber,
|
|
|
|
"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 {
|
|
|
|
if ext.Id.Equal(oidStepProvisioner) {
|
|
|
|
val := &stepProvisioner{}
|
|
|
|
rest, err := asn1.Unmarshal(ext.Value, val)
|
|
|
|
if err != nil || len(rest) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m["provisioner"] = fmt.Sprintf("%s (%s)", val.Name, val.CredentialID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rl.WithFields(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:53:13 +00:00
|
|
|
func parseCursor(r *http.Request) (cursor string, limit int, err error) {
|
|
|
|
q := r.URL.Query()
|
|
|
|
cursor = q.Get("cursor")
|
|
|
|
if v := q.Get("limit"); len(v) > 0 {
|
|
|
|
limit, err = strconv.Atoi(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", 0, errors.Wrapf(err, "error converting %s to integer", v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2019-02-20 03:45:52 +00:00
|
|
|
|
|
|
|
// TODO: add support for Ed25519 once it's supported
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
}
|