Files

432 lines
12 KiB
Go

package cli
import (
"bytes"
"context"
"crypto"
"crypto/x509"
"fmt"
"io"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/cockroachdb/errors"
"github.com/effective-security/xlog"
"github.com/effective-security/xpki/certutil"
"github.com/effective-security/xpki/x/print"
"golang.org/x/crypto/ocsp"
)
// CertsCmd provides certificates commands
type CertsCmd struct {
Info CertInfoCmd `cmd:"" help:"print certificate info"`
Validate CertValidateCmd `cmd:"" help:"validates certificate"`
}
// CertInfoCmd specifies flags for CertInfo action
type CertInfoCmd struct {
In string `kong:"arg" required:"" help:"certificate file name"`
Out string `help:"optional, output file to save parsed certificates"`
NotAfter string `help:"optional, filter certificates by NotAfter time"`
NoExpired *bool `help:"optional, filter non-expired certificates"`
Extensions bool `help:"optional, print extensions values"`
}
// Run the command
func (a *CertInfoCmd) Run(ctx *Cli) error {
// Load PEM
pem, err := ctx.ReadFile(a.In)
if err != nil {
return errors.WithMessage(err, "unable to load PEM file")
}
list, err := certutil.ParseChainFromPEM(pem)
if err != nil {
return errors.WithMessage(err, "unable to parse PEM")
}
now := time.Now().UTC()
if a.NoExpired != nil && *a.NoExpired {
list = filterByNotAfter(list, now)
}
if a.NotAfter != "" {
d, err := time.ParseDuration(a.NotAfter)
if err != nil {
return errors.WithMessage(err, "unable to parse --not-after")
}
list = filterByAfter(list, now.Add(d))
}
print.Certificates(ctx.Writer(), list, a.Extensions)
if a.Out != "" {
f, err := os.OpenFile(a.Out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return errors.WithMessage(err, "unable to create file")
}
err = certutil.EncodeToPEM(f, true, list...)
if err != nil {
_ = f.Close()
return errors.WithMessagef(err, "unable to write certificates: %s", a.Out)
}
err = f.Close()
if err != nil {
return errors.WithMessagef(err, "unable to close file: %s", a.Out)
}
}
return nil
}
func filterByNotAfter(list []*x509.Certificate, notAfter time.Time) []*x509.Certificate {
filtered := make([]*x509.Certificate, 0, len(list))
for _, c := range list {
if c.NotAfter.After(notAfter) {
filtered = append(filtered, c)
}
}
return filtered
}
func filterByAfter(list []*x509.Certificate, notAfter time.Time) []*x509.Certificate {
filtered := make([]*x509.Certificate, 0, len(list))
for _, c := range list {
if !c.NotAfter.After(notAfter) {
filtered = append(filtered, c)
}
}
return filtered
}
// CertValidateCmd specifies flags for Validate action
type CertValidateCmd struct {
Cert string `kong:"arg" required:"" help:"certificate file name"`
CA string `help:"optional, CA bundle file"`
Root string `help:"optional, Trusted Roots file"`
Out string `help:"optional, output file to save certificate chain"`
Revocation bool `help:"optional, validate certificate revocation status"`
Proxy string `help:"optional, proxy address or DC name"`
WithAIA bool `help:"optional, enable AIA to fetch intermediates"`
}
// Run the command
func (a *CertValidateCmd) Run(ctx *Cli) error {
var err error
var certBytes, cas []byte
var roots []byte
certBytes, err = ctx.ReadFile(a.Cert)
if err != nil {
return errors.WithMessage(err, "unable to load cert")
}
if a.CA != "" {
cas, err = os.ReadFile(a.CA)
if err != nil {
return errors.WithMessage(err, "unable to load CA bundle")
}
}
if a.Root != "" {
roots, err = os.ReadFile(a.Root)
if err != nil {
return errors.WithMessage(err, "unable to load Root bundle")
}
}
timeout := time.Second * time.Duration(ctx.Timeout)
client, err := httpClient(a.Proxy, timeout)
if err != nil {
return err
}
w := ctx.Writer()
// Without an explicit trust store, anchor on the system roots rather
// than falling back to Force mode, which would accept any self-signed chain.
bundler, err := certutil.NewBundlerFromPEM(roots, cas,
certutil.WithHTTPClient(client),
certutil.WithAIA(a.WithAIA),
certutil.WithSystemRoots(a.Root == ""),
certutil.WithBundleFlavor(certutil.Optimal),
)
if err != nil {
return errors.WithMessage(err, "unable to create bundler")
}
var bundle *certutil.Bundle
var bundleStatus *certutil.BundleStatus
verified, err := bundler.ChainFromPEMContext(ctx.Context(), certBytes, nil, "")
if err == nil {
bundle, bundleStatus, err = certutil.BuildBundle(verified)
}
if err != nil {
if crt, err2 := certutil.ParseFromPEM(certBytes); err2 == nil {
print.Certificate(w, crt, false)
}
return errors.WithMessage(err, "unable to verify certificate")
}
if bundleStatus.IsUntrusted() {
_, _ = fmt.Fprintf(w, "ERROR: The cert is untrusted\n")
}
chain := bundle.Chain
if bundle.RootCert != nil {
chain = append(chain, bundle.RootCert)
}
print.Certificates(w, chain, false)
if len(bundleStatus.ExpiringSKIs) > 0 {
_, _ = fmt.Fprintf(w, "\nWARNING: Expiring SKI:\n")
for _, ski := range bundleStatus.ExpiringSKIs {
_, _ = fmt.Fprintf(w, " -- %s\n", ski)
}
}
if len(bundleStatus.Untrusted) > 0 {
_, _ = fmt.Fprintf(w, "\nWARNING: Untrusted SKI:\n")
for _, ski := range bundleStatus.Untrusted {
_, _ = fmt.Fprintf(w, " -- %s\n", ski)
}
}
if a.Out != "" {
pem := bundle.CertPEM + "\n" + bundle.CACertsPEM
err = os.WriteFile(a.Out, []byte(pem), 0664)
if err != nil {
return errors.WithStack(err)
}
}
// revInfo stores all certificates statuses
var revInfo []*certRevInfo
var revInfoMutex sync.Mutex
// For Revocation Check
if len(chain) > 0 && a.Revocation {
var wg sync.WaitGroup
for _, crt := range chain {
if bytes.Equal(crt.RawIssuer, crt.RawSubject) {
// skip root
continue
}
issuer := certutil.FindIssuer(crt, chain, nil)
if issuer == nil {
continue
}
if len(crt.OCSPServer) == 0 {
revInfoMutex.Lock()
revInfo = append(revInfo, &certRevInfo{crt: crt, status: ocsp.Unknown, revokedType: "OCSP", err: errors.New("OCSP server is not present")})
revInfoMutex.Unlock()
// fmt.Fprintf(w, "OCSP server is not present: %s\n", crt.Subject.String())
}
// Cli.Context lazily initializes; resolve it once before starting goroutines.
reqCtx := ctx.Context()
for _, url := range crt.OCSPServer {
wg.Add(1)
go func(URL string) {
defer wg.Done()
status, _, err := OCSPValidation(reqCtx, client, crt, issuer, URL)
revInfoMutex.Lock()
if err != nil || status == ocsp.Revoked {
revInfo = append(revInfo, &certRevInfo{crt: crt, status: status, err: err, url: URL, revokedType: "OCSP"})
} else {
revInfo = append(revInfo, &certRevInfo{crt: crt, status: status, url: URL, revokedType: "OCSP"})
}
revInfoMutex.Unlock()
}(url)
}
if len(crt.CRLDistributionPoints) == 0 {
revInfoMutex.Lock()
revInfo = append(revInfo, &certRevInfo{crt: crt, status: ocsp.Unknown, revokedType: "CRL", err: errors.New("CDP is not present")})
revInfoMutex.Unlock()
// fmt.Fprintf(w, "CRL endpoint is not present: %s\n", crt.Subject.String())
}
for _, url := range crt.CRLDistributionPoints {
wg.Add(1)
go func(URL string) {
defer wg.Done()
status, err := CRLValidation(reqCtx, client, crt, issuer, URL)
revInfoMutex.Lock()
if err != nil || status == ocsp.Revoked {
revInfo = append(revInfo, &certRevInfo{crt: crt, status: status, err: err, url: URL, revokedType: "CRL"})
} else {
revInfo = append(revInfo, &certRevInfo{crt: crt, status: status, url: URL, revokedType: "CRL"})
}
revInfoMutex.Unlock()
}(url)
}
wg.Wait()
}
}
// Prints all the certificate stores in revokedCerts
if a.Revocation {
aggrStatus := ocsp.Unknown
_, _ = fmt.Fprint(w, "\n============================= Revocation Info =============================\n")
for _, crtInfo := range revInfo {
if crtInfo.err != nil {
_, _ = fmt.Fprintf(w, "%s : ERROR: %s\n", crtInfo.crt.Subject.String(), crtInfo.err)
} else {
_, _ = fmt.Fprintf(w, "%s: %s: %v\n", crtInfo.crt.Subject.String(), crtInfo.revokedType, statusMap[crtInfo.status])
if crtInfo.status == ocsp.Revoked {
aggrStatus = ocsp.Revoked
}
}
}
if aggrStatus == ocsp.Revoked {
_, _ = fmt.Fprintf(w, "\nCertificate chain is revoked\n")
}
}
return nil
}
// OCSPValidation calls OCSP server and validate certificate
func OCSPValidation(ctx context.Context, client *http.Client, crt *x509.Certificate, issuer *x509.Certificate, rawURL string) (int, []byte, error) {
logger.KV(xlog.DEBUG, "fetching", "ocsp", "url", rawURL)
req, err := certutil.CreateOCSPRequest(crt, issuer, crypto.SHA256)
if err != nil {
return ocsp.Unknown, nil, err
}
der, err := postHTTP(ctx, client, rawURL, "application/ocsp-request", bytes.NewReader(req))
if err != nil {
return ocsp.Unknown, nil, err
}
res, err := ocsp.ParseResponseForCert(der, crt, issuer)
if err != nil {
logger.KV(xlog.DEBUG, "ocsp", string(der))
return ocsp.Unknown, der, errors.WithMessagef(err, "failed to parse OCSP")
}
if !res.NextUpdate.IsZero() && res.NextUpdate.Before(time.Now()) {
return ocsp.Unknown, der, errors.New("OCSP response is expired")
}
return res.Status, der, nil
}
// CRLValidation calls CRL Endpoint and check certificate in CRL
func CRLValidation(ctx context.Context, client *http.Client, crt *x509.Certificate, issuer *x509.Certificate, crlURL string) (int, error) {
logger.KV(xlog.DEBUG, "fetching", "crl", "url", crlURL)
der, err := download(ctx, client, crlURL)
if err != nil {
return ocsp.Unknown, errors.WithStack(err)
}
// ensure the CRL is valid
certList, err := x509.ParseRevocationList(der)
if err != nil {
logger.KV(xlog.DEBUG, "crl", string(der))
return ocsp.Unknown, errors.WithMessagef(err, "failed to parse CRL")
}
err = certList.CheckSignatureFrom(issuer)
if err != nil {
return ocsp.Unknown, errors.WithMessage(err, "unable to verify CRL signature")
}
for _, cert := range certList.RevokedCertificateEntries {
if crt.SerialNumber.Cmp(cert.SerialNumber) == 0 {
return ocsp.Revoked, nil
}
}
return ocsp.Good, nil
}
// certRevInfo stores the information of revoked certificate
type certRevInfo struct {
crt *x509.Certificate
status int
err error
url string
revokedType string
}
// statusMap maps status
var statusMap = map[int]string{
ocsp.Good: "good",
ocsp.Revoked: "revoked",
ocsp.Unknown: "unknown",
}
func httpClient(proxy string, timeout time.Duration) (*http.Client, error) {
if timeout == 0 {
timeout = 3 * time.Second
}
c := &http.Client{
Timeout: timeout,
}
if proxy != "" {
u, err := url.Parse(proxy)
if err != nil {
return nil, errors.Errorf("unable to parse proxy URL: %s", proxy)
}
c.Transport = &http.Transport{
Proxy: http.ProxyURL(u),
}
}
return c, nil
}
func postHTTP(ctx context.Context, client *http.Client, url string, contentType string, body io.Reader) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return nil, errors.WithMessagef(err, "unable to create request to %s", url)
}
req.Header.Set("Content-Type", contentType)
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithMessagef(err, "unable to post to %s", url)
}
defer func() {
_ = resp.Body.Close()
}()
rbody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithMessagef(err, "unable to download from %s", url)
}
return rbody, nil
}
func download(ctx context.Context, client *http.Client, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errors.WithMessagef(err, "unable to create request to %s", url)
}
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithMessagef(err, "unable to fetch from %s", url)
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithMessagef(err, "unable to download from %s", url)
}
return body, nil
}