feat: add fingerprint and serial to certs:report
The report described an installed certificate without carrying anything that identified it, so the only exact way to tell whether an app was serving a given certificate was to pull the full PEM back with `certs:show` and compare bytes. That moves certificate material off the server on every check, and the same command with `key` returns the private key. The new `--ssl-fingerprint` and `--ssl-serial` flags answer the same question in a single report read that discloses nothing sensitive. The sample output in the ssl documentation has also been corrected to match what the command actually prints.
This commit is contained in:
4 files changed
+196
-47
No files matched your search
+58
-25
@@ -20,14 +20,16 @@ func ReportSingleApp(appName string, format string, infoFlag string) error {
|
||||
return err
|
||||
}
|
||||
flags = map[string]common.ReportFunc{
|
||||
"--ssl-dir": reportSSLDir,
|
||||
"--ssl-enabled": reportSSLEnabled,
|
||||
"--ssl-hostnames": reportSSLHostnames,
|
||||
"--ssl-expires-at": reportSSLExpiresAt,
|
||||
"--ssl-issuer": reportSSLIssuer,
|
||||
"--ssl-starts-at": reportSSLStartsAt,
|
||||
"--ssl-subject": reportSSLSubject,
|
||||
"--ssl-verified": reportSSLVerified,
|
||||
"--ssl-dir": reportSSLDir,
|
||||
"--ssl-enabled": reportSSLEnabled,
|
||||
"--ssl-hostnames": reportSSLHostnames,
|
||||
"--ssl-expires-at": reportSSLExpiresAt,
|
||||
"--ssl-fingerprint": reportSSLFingerprint,
|
||||
"--ssl-issuer": reportSSLIssuer,
|
||||
"--ssl-serial": reportSSLSerial,
|
||||
"--ssl-starts-at": reportSSLStartsAt,
|
||||
"--ssl-subject": reportSSLSubject,
|
||||
"--ssl-verified": reportSSLVerified,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +80,21 @@ func opensslCertText(appName string) string {
|
||||
return result.Stdout
|
||||
}
|
||||
|
||||
// opensslCertOption runs "openssl x509 -in <server.crt> -noout" with the given
|
||||
// output options and returns the trimmed stdout, or an empty string when openssl
|
||||
// cannot read the certificate.
|
||||
func opensslCertOption(appName string, args ...string) string {
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
Command: "openssl",
|
||||
Args: append([]string{"x509", "-in", filepath.Join(certTLSPath(appName), "server.crt"), "-noout"}, args...),
|
||||
})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return result.StdoutContents()
|
||||
}
|
||||
|
||||
func reportSSLDir(appName string) string {
|
||||
return certTLSPath(appName)
|
||||
}
|
||||
@@ -124,6 +141,37 @@ func reportSSLStartsAt(appName string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func reportSSLFingerprint(appName string) string {
|
||||
if !isSSLEnabled(appName) {
|
||||
return ""
|
||||
}
|
||||
|
||||
return formatSSLHexField(opensslCertOption(appName, "-fingerprint", "-sha256"))
|
||||
}
|
||||
|
||||
func reportSSLSerial(appName string) string {
|
||||
if !isSSLEnabled(appName) {
|
||||
return ""
|
||||
}
|
||||
|
||||
return formatSSLHexField(opensslCertOption(appName, "-serial"))
|
||||
}
|
||||
|
||||
// formatSSLHexField extracts the value from an openssl "key=HEX" output line, as
|
||||
// printed by "-fingerprint" and "-serial". The key is discarded rather than
|
||||
// matched against a literal: OpenSSL 3.x labels the digest "sha256" where
|
||||
// OpenSSL 1.x and LibreSSL label it "SHA256". Neither a fingerprint nor a serial
|
||||
// can contain an "=", so cutting at the first one is unambiguous. Only hex
|
||||
// values may be passed through here, as uppercasing would corrupt a subject.
|
||||
func formatSSLHexField(out string) string {
|
||||
_, value, found := strings.Cut(strings.TrimSpace(out), "=")
|
||||
if !found {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.ToUpper(strings.TrimSpace(value))
|
||||
}
|
||||
|
||||
func reportSSLIssuer(appName string) string {
|
||||
if !isSSLEnabled(appName) {
|
||||
return ""
|
||||
@@ -144,15 +192,7 @@ func reportSSLSubject(appName string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
result, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
Command: "openssl",
|
||||
Args: []string{"x509", "-in", filepath.Join(certTLSPath(appName), "server.crt"), "-noout", "-subject", "-nameopt", "compat"},
|
||||
})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return formatSSLSubject(result.StdoutContents())
|
||||
return formatSSLSubject(opensslCertOption(appName, "-subject", "-nameopt", "compat"))
|
||||
}
|
||||
|
||||
// formatSSLSubject normalizes an openssl "-subject -nameopt compat" line into a
|
||||
@@ -203,14 +243,7 @@ func reportSSLHostnames(appName string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
subject := ""
|
||||
subjectResult, err := common.CallExecCommand(common.ExecCommandInput{
|
||||
Command: "openssl",
|
||||
Args: []string{"x509", "-in", filepath.Join(certTLSPath(appName), "server.crt"), "-noout", "-subject", "-nameopt", "RFC2253"},
|
||||
})
|
||||
if err == nil {
|
||||
subject = subjectResult.StdoutContents()
|
||||
}
|
||||
subject := opensslCertOption(appName, "-subject", "-nameopt", "RFC2253")
|
||||
|
||||
return strings.Join(sslHostnames(subject, opensslCertText(appName)), " ")
|
||||
}
|
||||
|
||||
@@ -112,3 +112,30 @@ func TestFormatSSLSubject(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatSSLHexField(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{"fingerprint openssl 1.x label", "SHA256 Fingerprint=B7:DF:D5:84:C6:2E:27:BF", "B7:DF:D5:84:C6:2E:27:BF"},
|
||||
{"fingerprint openssl 3.x label", "sha256 Fingerprint=B7:DF:D5:84:C6:2E:27:BF", "B7:DF:D5:84:C6:2E:27:BF"},
|
||||
{"fingerprint lowercase digest", "sha256 Fingerprint=b7:df:d5:84:c6:2e:27:bf", "B7:DF:D5:84:C6:2E:27:BF"},
|
||||
{"serial", "serial=322844AD8CD6D4FF76B05C50833AB91DEEDA2AD1", "322844AD8CD6D4FF76B05C50833AB91DEEDA2AD1"},
|
||||
{"serial shorter", "serial=C46823E5D7C09FC9", "C46823E5D7C09FC9"},
|
||||
{"serial lowercase", "serial=c46823e5d7c09fc9", "C46823E5D7C09FC9"},
|
||||
{"serial negative", "serial=-1F2E", "-1F2E"},
|
||||
{"surrounding whitespace", " serial=C46823E5D7C09FC9 \n", "C46823E5D7C09FC9"},
|
||||
{"no separator", "unable to load certificate", ""},
|
||||
{"empty", "", ""},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := formatSSLHexField(tc.in); got != tc.want {
|
||||
t.Errorf("formatSSLHexField(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user