@@ -8,37 +8,89 @@ if [ -z "${JQ}" ]; then
88fi
99
1010OUTPUT=internal/shared/util/tlsprofiles/mozilla_data.go
11- INPUT=https://ssl-config.mozilla.org/guidelines/5.8 .json
11+ INPUT=https://ssl-config.mozilla.org/guidelines/latest .json
1212
1313TMPFILE=" $( mktemp) "
1414trap ' rm -rf "$TMPFILE"' EXIT
1515
16- curl -L -s ${INPUT} > ${TMPFILE}
16+ curl -L -s " ${INPUT} " > " ${TMPFILE} "
1717
18- version=$( ${JQ} -r ' .version' ${TMPFILE} )
18+ # Extract stored version from current output file (may be empty on first run)
19+ STORED_VERSION=$( grep ' ^// DATA VERSION:' " ${OUTPUT} " 2> /dev/null | awk ' {print $4}' || true)
20+
21+ # Extract version from downloaded JSON and fail early if missing
22+ NEW_VERSION=$( ${JQ} -r ' .version' ${TMPFILE} )
23+ if [ -z " ${NEW_VERSION} " ] || [ " ${NEW_VERSION} " = " null" ]; then
24+ echo " ERROR: Could not read .version from ${INPUT} " >&2
25+ exit 1
26+ fi
27+
28+ if [ " ${NEW_VERSION} " = " ${STORED_VERSION} " ]; then
29+ echo " Mozilla TLS data is already at version ${NEW_VERSION} , skipping regeneration."
30+ exit 0
31+ fi
32+ echo " Updating Mozilla TLS data from version ${STORED_VERSION:- unknown} to ${NEW_VERSION} "
1933
2034cat > ${OUTPUT} << EOF
2135package tlsprofiles
2236
2337// DO NOT EDIT, GENERATED BY ${0}
2438// DATA SOURCE: ${INPUT}
25- // DATA VERSION: ${version }
39+ // DATA VERSION: ${NEW_VERSION }
2640
2741import (
2842"crypto/tls"
2943)
3044EOF
3145
3246function generate_profile {
47+ local profile=" ${1} "
48+
49+ # Validate the profile key exists before writing any output
50+ local exists
51+ exists=$( ${JQ} -r " .configurations | has(\" ${profile} \" )" " ${TMPFILE} " )
52+ if [ " ${exists} " != " true" ]; then
53+ echo " ERROR: Profile '${profile} ' not found in ${INPUT} (version ${NEW_VERSION} )" >&2
54+ echo " Available profiles: $( ${JQ} -r ' .configurations | keys | join(", ")' " ${TMPFILE} " ) " >&2
55+ exit 1
56+ fi
57+
58+ # Validate tls_versions is a non-empty array with a non-null first entry
59+ if ! ${JQ} -e " .configurations.${profile} .tls_versions | type == \" array\" and length > 0 and .[0] != null" " ${TMPFILE} " > /dev/null; then
60+ echo " ERROR: Missing or empty .configurations.${profile} .tls_versions[0] in ${INPUT} " >&2
61+ exit 1
62+ fi
63+
64+ # Validate that at least one cipher is present across ciphersuites and ciphers.iana
65+ # (modern has only ciphersuites; intermediate has both; either alone is valid)
66+ local cipher_count
67+ cipher_count=$( ${JQ} -r "
68+ [
69+ (.configurations.${profile} .ciphersuites // []),
70+ (.configurations.${profile} .ciphers.iana // [])
71+ ] | add | length" " ${TMPFILE} " )
72+ if [ " ${cipher_count} " -eq 0 ] 2> /dev/null; then
73+ echo " ERROR: Profile '${profile} ' has no ciphers in ciphersuites or ciphers.iana" >&2
74+ exit 1
75+ fi
76+
77+ # Validate tls_curves is non-empty
78+ local curve_count
79+ curve_count=$( ${JQ} -r " .configurations.${profile} .tls_curves | length" " ${TMPFILE} " )
80+ if [ " ${curve_count} " -eq 0 ] 2> /dev/null; then
81+ echo " ERROR: Profile '${profile} ' has no entries in tls_curves" >&2
82+ exit 1
83+ fi
84+
3385 cat >> ${OUTPUT} << EOF
3486
35- var ${1 } TLSProfile = tlsProfile{
87+ var ${profile } TLSProfile = tlsProfile{
3688ciphers: cipherSlice{
3789cipherNums: []uint16{
3890EOF
3991
40- ${JQ} -r " .configurations.$1 .ciphersuites. [] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
41- ${JQ} -r " .configurations.$1 .ciphers.iana[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
92+ ${JQ} -r " ( .configurations.${profile} .ciphersuites // []) [] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
93+ ${JQ} -r " ( .configurations.${profile} .ciphers.iana // []) [] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
4294
4395 cat >> ${OUTPUT} << EOF
4496},
@@ -47,9 +99,9 @@ curves: curveSlice{
4799curveNums: []tls.CurveID{
48100EOF
49101
50- ${JQ} -r " .configurations.$1 .tls_curves[] | . |= . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
102+ ${JQ} -r " .configurations.${profile} .tls_curves[] | . |= . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
51103
52- version=$( ${JQ} -r " .configurations.$1 .tls_versions[0]" ${TMPFILE} )
104+ version=$( ${JQ} -r " .configurations.${profile} .tls_versions[0]" ${TMPFILE} )
53105 version=${version/ TLSv1./ tls.VersionTLS1}
54106 version=${version/ TLSv1/ tls.VersionTLS10}
55107
63115
64116generate_profile " modern"
65117generate_profile " intermediate"
66- generate_profile " old"
67118
68- # Remove unsupported ciphers from Go's crypto/tls package (Mozilla v5.8 includes these but Go doesn't support them)
119+ # Remove unsupported ciphers from Go's crypto/tls package
69120sed -i.bak ' /TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384/d; /TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384/d; /TLS_RSA_WITH_AES_256_CBC_SHA256/d' ${OUTPUT}
70121rm -f ${OUTPUT} .bak
71122
0 commit comments