Skip to content

Commit 462a602

Browse files
authored
Merge pull request #2276 from dcooper16/pem_fileout
Fix #1747
2 parents 827782c + 3eb8cf6 commit 462a602

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

testssl.sh

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,26 @@ strip_quote() (
872872
)"
873873
)
874874

875+
# Converts a string containing PEM encoded data to one line.
876+
pem_to_one_line() {
877+
local pem="$1"
878+
local header="" footer=""
879+
880+
if [[ "$pem" =~ .*-+BEGIN\ [A-Za-z0-9]+-+ ]]; then
881+
header="$BASH_REMATCH"
882+
pem="${pem/$header/}"
883+
fi
884+
if [[ "$pem" =~ -+END\ [A-Za-z0-9]+-+.* ]]; then
885+
footer="$BASH_REMATCH"
886+
pem="${pem/$footer/}"
887+
fi
888+
pem="$(strip_spaces "$(newline_to_spaces "$pem")")"
889+
[[ -n "$header" ]] && pem="$header\\\n$pem"
890+
[[ -n "$footer" ]] && pem+="\\\n$footer"
891+
printf -- "%s" "$pem"
892+
return 0
893+
}
894+
875895
is_ipv4addr() {
876896
local octet="(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])"
877897
local ipv4address="$octet\\.$octet\\.$octet\\.$octet"
@@ -1355,7 +1375,6 @@ fileout() {
13551375

13561376
if { "$do_pretty_json" && [[ "$1" == service ]]; } || show_finding "$severity"; then
13571377
local finding=$(strip_lf "$(newline_to_spaces "$(strip_quote "$3")")") # additional quotes will mess up screen output
1358-
finding="${finding//\\n/ }"
13591378
[[ -e "$JSONFILE" ]] && [[ ! -d "$JSONFILE" ]] && fileout_json_finding "$1" "$severity" "$finding" "$cve" "$cwe" "$hint"
13601379
"$do_csv" && [[ -n "$CSVFILE" ]] && [[ ! -d "$CSVFILE" ]] && \
13611380
fileout_csv_finding "$1" "$NODE/$NODEIP" "$PORT" "$severity" "$finding" "$cve" "$cwe" "$hint"
@@ -9181,8 +9200,7 @@ certificate_info() {
91819200
fileout "cert_fingerprintSHA256${json_postfix}" "INFO" "${cert_fingerprint_sha2}"
91829201
outln "${spaces}SHA256 ${cert_fingerprint_sha2}"
91839202

9184-
# " " needs to be converted back to lf in JSON/CSV output. watch out leading/ending line containing "CERTIFICATE"
9185-
fileout "cert${json_postfix}" "INFO" "$hostcert"
9203+
fileout "cert${json_postfix}" "INFO" "$(pem_to_one_line "$hostcert")"
91869204

91879205
[[ -z $CERT_FINGERPRINT_SHA2 ]] && \
91889206
CERT_FINGERPRINT_SHA2="$cert_fingerprint_sha2" ||
@@ -9757,7 +9775,7 @@ certificate_info() {
97579775
intermediates="${intermediates#${cert}-----END CERTIFICATE-----}"
97589776
cert="-----BEGIN CERTIFICATE-----${cert}-----END CERTIFICATE-----"
97599777

9760-
fileout "intermediate_cert <#${i}>${json_postfix}" "INFO" "$cert"
9778+
fileout "intermediate_cert <#${i}>${json_postfix}" "INFO" "$(pem_to_one_line "$cert")"
97619779
fileout "intermediate_cert_fingerprintSHA256 <#${i}>${json_postfix}" "INFO" "$(determine_cert_fingerprint_serial "$cert" "-fingerprint -sha256")"
97629780

97639781
intermediate_certs_txt[i]="$($OPENSSL x509 -text -noout 2>/dev/null <<< "$cert")"
@@ -20443,7 +20461,7 @@ fatal() {
2044320461
fi
2044420462
# Make sure we don't try to write into files when not created yet.
2044520463
# No shorthand expression to avoid errors when $CMDLINE_PARSED haven't been filled yet.
20446-
[[ $CMDLINE_PARSED == true ]] && fileout "scanProblem" "FATAL" "$1"
20464+
[[ $CMDLINE_PARSED == true ]] && fileout "scanProblem" "FATAL" "${1//\\n/ }" # See issue #2049.
2044720465
exit $2
2044820466
}
2044920467

0 commit comments

Comments
 (0)