certinfo

package
v0.0.0-...-88bec2d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TLSTimeout is the maximum time to wait for a TLS handshake.
	TLSTimeout = 3 * time.Second
	// CertExpWarnDays is the number of days before expiration to start showing warnings.
	CertExpWarnDays = 40
)

Variables

View Source
var (
	// TLSServerName is the default ServerName to use for SNI.
	TLSServerName string
	// TLSInsecure indicates if certificate verification should be skipped globally.
	TLSInsecure bool
)

Functions

func CertsToTables

func CertsToTables(w io.Writer, certs []*x509.Certificate, filter ...[]map[int][]string)

CertsToTables formats and prints a list of x509 certificates as tables to the provided writer. An optional filter slice of maps can be provided to filter printed output by certificate index and field names.

func GetCertsFromBundle

func GetCertsFromBundle(certBundlePath string, fileReader Reader) ([]*x509.Certificate, error)

GetCertsFromBundle reads a PEM bundle from a file and returns a slice of x509 Certificates.

func GetKeyFromFile

func GetKeyFromFile(
	keyFilePath string,
	keyPwEnvVar string,
	inputReader Reader,
) (crypto.PrivateKey, error)

GetKeyFromFile reads a private key from a file and parses it using ParsePrivateKey.

func GetRootCertsFromFile

func GetRootCertsFromFile(caBundlePath string, fileReader Reader) (*x509.CertPool, error)

GetRootCertsFromFile reads a PEM bundle from a file and returns an x509 CertPool.

func GetRootCertsFromString

func GetRootCertsFromString(caBundleString string) (*x509.CertPool, error)

GetRootCertsFromString parses a PEM bundle from a string and returns an x509 CertPool.

func IsPrivateKeyEncrypted

func IsPrivateKeyEncrypted(key []byte) (bool, error)

IsPrivateKeyEncrypted checks if the given PEM key is encrypted. It returns true if encrypted, false otherwise, and an error if decoding fails.

func ParsePrivateKey

func ParsePrivateKey(keyPEM []byte, pwEnvKey string, pwReader Reader) (crypto.PrivateKey, error)

ParsePrivateKey parses a PEM-encoded private key and returns it as a crypto.PrivateKey.

Supported formats:

- PKCS#8 ("BEGIN PRIVATE KEY" / "BEGIN ENCRYPTED PRIVATE KEY") — decrypted with github.com/youmark/pkcs8

- PKCS#1 RSA ("BEGIN RSA PRIVATE KEY") — cleartext or PEM-encrypted (x509.DecryptPEMBlock)

- EC private keys ("BEGIN EC PRIVATE KEY") — cleartext or certain PKCS#8-encrypted ECDSA keys

If the PEM is encrypted the function will try to read the passphrase from the environment variable named by pwEnvKey; if that is empty it will prompt the user interactively.

The function returns a descriptive error if the PEM cannot be decoded, decryption/parsing fails, or the key format is unsupported.

func PrintCertInfo

func PrintCertInfo(cert *x509.Certificate, depth int, w io.Writer)

PrintCertInfo prints basic information about an x509 certificate to the provided writer with a specified indentation depth.

Types

type Config

type Config struct {
	// CACertsPool is the pool of root CA certificates used for verification.
	CACertsPool *x509.CertPool
	// CACertsFilePath is the path to the CA certificate bundle file.
	CACertsFilePath string
	// CertsBundle is a slice of certificates loaded from a local bundle.
	CertsBundle []*x509.Certificate
	// CertsBundleFilePath is the path to the certificate bundle file.
	CertsBundleFilePath string
	// CertsBundleFromKey indicates if the certificate was derived from a private key.
	CertsBundleFromKey bool
	// PrivKey is the loaded private key.
	PrivKey crypto.PrivateKey
	// PrivKeyFilePath is the path to the private key file.
	PrivKeyFilePath string
	// TLSEndpoint is the host:port string of the remote TLS endpoint.
	TLSEndpoint string
	// TLSEndpointHost is the hostname part of the TLS endpoint.
	TLSEndpointHost string
	// TLSEndpointPort is the port part of the TLS endpoint.
	TLSEndpointPort string
	// TLSEndpointCerts is the slice of certificates retrieved from the remote endpoint.
	TLSEndpointCerts []*x509.Certificate
	// TLSEndpointCertsFromKey indicates if the endpoint certificates were matched with a private key.
	TLSEndpointCertsFromKey bool
	// TLSServerName is the ServerName used for SNI.
	TLSServerName string
	// TLSInsecure indicates if certificate verification should be skipped.
	TLSInsecure bool
	// TLSInfoRequested indicates if negotiated TLS info and supported protocol/cipher scan was requested.
	TLSInfoRequested bool
	// NegotiatedProtocol is the TLS protocol version negotiated in the primary connection.
	NegotiatedProtocol string
	// NegotiatedCipher is the TLS cipher suite negotiated in the primary connection.
	NegotiatedCipher string
	// ProbedProtocols maps a TLS protocol name to whether the remote endpoint supports it.
	ProbedProtocols map[string]bool
	// ProbedCiphers is a slice of ciphers that were probed against the endpoint.
	ProbedCiphers []ProbedCipher
}

Config holds the configuration and results for certificate and key information retrieval.

func New

func New() (*Config, error)

New creates a new Config with the system's default certificate pool.

func (*Config) GetRemoteCerts

func (c *Config) GetRemoteCerts(ctx context.Context) error

GetRemoteCerts establishes a TLS connection to the configured endpoint and retrieves the peer certificate chain. It also performs certificate verification unless TLSInsecure is true.

func (*Config) PrintData

func (c *Config) PrintData(ctx context.Context, w io.Writer) error

PrintData prints all collected certificate and key information (local files and remote endpoints) to the provided writer in a human-readable format.

func (*Config) ProbeTLSInfo

func (c *Config) ProbeTLSInfo(ctx context.Context) error

ProbeTLSInfo concurrently scans the endpoint for supported TLS versions and cipher suites.

func (*Config) SetCaPoolFromFile

func (c *Config) SetCaPoolFromFile(filePath string, fileReader Reader) error

SetCaPoolFromFile loads a CA certificate pool from the specified PEM bundle file. Note that x509.SystemCertPool is not used in this case. All certificates from the system certificate pool are excluded.

func (*Config) SetCertsFromFile

func (c *Config) SetCertsFromFile(filePath string, fileReader Reader) error

SetCertsFromFile loads a certificate bundle from the specified PEM file.

func (*Config) SetPrivateKeyFromFile

func (c *Config) SetPrivateKeyFromFile(
	filePath string,
	keyPwEnvVar string,
	fileReader Reader,
) error

SetPrivateKeyFromFile loads a private key from the specified PEM file. If the key is encrypted, it will attempt to retrieve the passphrase from the environment variable passed as argument or from the interactive prompt.

func (*Config) SetTLSEndpoint

func (c *Config) SetTLSEndpoint(ctx context.Context, hostport string) error

SetTLSEndpoint parses a host:port string and fetches the remote certificates from that endpoint.

func (*Config) SetTLSInfoRequested

func (c *Config) SetTLSInfoRequested(requested bool) *Config

SetTLSInfoRequested sets whether to probe and print remote TLS protocol/cipher information.

func (*Config) SetTLSInsecure

func (c *Config) SetTLSInsecure(skipVerify bool) *Config

SetTLSInsecure sets whether TLS certificate verification should be skipped for the remote endpoint.

func (*Config) SetTLSServerName

func (c *Config) SetTLSServerName(serverName string) *Config

SetTLSServerName sets the ServerName to use for SNI when connecting to a remote TLS endpoint.

type InputReader

type InputReader struct{}

InputReader implements the Reader interface using standard OS calls.

func (InputReader) ReadFile

func (InputReader) ReadFile(name string) ([]byte, error)

ReadFile reads the content of a file from the filesystem.

func (InputReader) ReadPassword

func (InputReader) ReadPassword(fd int) ([]byte, error)

ReadPassword reads a password from the terminal without echoing it.

type NoPasswordPromptReader

type NoPasswordPromptReader interface {
	NoPasswordPrompt() bool
}

NoPasswordPromptReader is implemented by readers that must not use an interactive terminal prompt (for example MCP or automated tests).

type ProbedCipher

type ProbedCipher struct {
	ID        uint16
	Name      string
	Protocol  string
	Insecure  bool
	Supported bool
}

ProbedCipher holds the result of a single cipher suite probe.

type Reader

type Reader interface {
	ReadFile(name string) ([]byte, error)
	ReadPassword(fd int) ([]byte, error)
}

Reader defines an interface for reading files and passwords.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL