@@ -8,37 +8,91 @@ 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
1616curl -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 exists (required for Go code generation)
59+ local has_versions
60+ has_versions=$( ${JQ} -r " .configurations.${profile} | has(\" tls_versions\" )" " ${TMPFILE} " )
61+ if [ " ${has_versions} " != " true" ]; then
62+ echo " ERROR: Profile '${profile} ' is missing required field 'tls_versions'" >&2
63+ exit 1
64+ fi
65+
66+ # Validate that at least one cipher is present across ciphersuites and ciphers.iana
67+ # (modern has only ciphersuites; intermediate has both; either alone is valid)
68+ local cipher_count
69+ cipher_count=$( ${JQ} -r "
70+ [
71+ (.configurations.${profile} .ciphersuites // []),
72+ (.configurations.${profile} .ciphers.iana // [])
73+ ] | add | length" " ${TMPFILE} " )
74+ if [ " ${cipher_count} " -eq 0 ] 2> /dev/null; then
75+ echo " ERROR: Profile '${profile} ' has no ciphers in ciphersuites or ciphers.iana" >&2
76+ exit 1
77+ fi
78+
79+ # Validate tls_curves is non-empty
80+ local curve_count
81+ curve_count=$( ${JQ} -r " .configurations.${profile} .tls_curves | length" " ${TMPFILE} " )
82+ if [ " ${curve_count} " -eq 0 ] 2> /dev/null; then
83+ echo " ERROR: Profile '${profile} ' has no entries in tls_curves" >&2
84+ exit 1
85+ fi
86+
3387 cat >> ${OUTPUT} << EOF
3488
35- var ${1 } TLSProfile = tlsProfile{
89+ var ${profile } TLSProfile = tlsProfile{
3690ciphers: cipherSlice{
3791cipherNums: []uint16{
3892EOF
3993
40- ${JQ} -r " .configurations.$1 .ciphersuites.[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
41- ${JQ} -r " .configurations.$1 .ciphers.iana[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
94+ ${JQ} -r " .configurations.$profile .ciphersuites.[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
95+ ${JQ} -r " .configurations.$profile .ciphers.iana[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
4296
4397 cat >> ${OUTPUT} << EOF
4498},
@@ -47,9 +101,9 @@ curves: curveSlice{
47101curveNums: []tls.CurveID{
48102EOF
49103
50- ${JQ} -r " .configurations.$1 .tls_curves[] | . |= . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
104+ ${JQ} -r " .configurations.$profile .tls_curves[] | . |= . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
51105
52- version=$( ${JQ} -r " .configurations.$1 .tls_versions[0]" ${TMPFILE} )
106+ version=$( ${JQ} -r " .configurations.$profile .tls_versions[0]" ${TMPFILE} )
53107 version=${version/ TLSv1./ tls.VersionTLS1}
54108 version=${version/ TLSv1/ tls.VersionTLS10}
55109
63117
64118generate_profile " modern"
65119generate_profile " intermediate"
66- generate_profile " old"
67120
68- # Remove unsupported ciphers from Go's crypto/tls package (Mozilla v5.8 includes these but Go doesn't support them)
121+ # Remove unsupported ciphers from Go's crypto/tls package
69122sed -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}
70123rm -f ${OUTPUT} .bak
71124
0 commit comments