Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / download-ca-bundle.sh
download-ca-bundle.sh216 lines · 8.8 KiBRaw
| 1 | #!/usr/bin/env zsh |
| 2 | set -e |
| 3 | |
| 4 | # Reduced CA set hardening — downloads specific roots from Mozilla bundle and direct sources, |
| 5 | # converts PEM -> DER for bundling in library:network assets/ca/. |
| 6 | # |
| 7 | # Why: first-party Cloudflare domains only need ISRG + GTS, reducing blast radius of rogue CA. |
| 8 | # Appstore STANDARD needs DigiCert + ISRG + GTS (F-Droid + GitHub + Play Store via GTS). |
| 9 | # |
| 10 | # Nothing fetched here is trusted on its own. Both sources — the Mozilla bundle and the |
| 11 | # direct-URL fallback — are converted inside $TMP_DIR and only moved into assets/ca/ when |
| 12 | # the result matches its PINS entry below. The pins are the SHA-256 of the .der files as |
| 13 | # committed, so the anchors this script can produce are exactly the anchors already in the |
| 14 | # tree, and rolling one is a reviewable edit to both the .der and its pin in the same diff: |
| 15 | # |
| 16 | # sha256sum library/network/src/main/assets/ca/<label>.der |
| 17 | |
| 18 | OUT_DIR="${0:A:h:h}/library/network/src/main/assets/ca" |
| 19 | BUNDLE_URL="https://curl.se/ca/cacert.pem" |
| 20 | TMP_DIR=$(mktemp -d) |
| 21 | trap 'rm -rf "$TMP_DIR"' EXIT |
| 22 | |
| 23 | clean=0 |
| 24 | if [[ "${1:-}" == "--clean" ]]; then |
| 25 | clean=1 |
| 26 | fi |
| 27 | |
| 28 | mkdir -p "$OUT_DIR" |
| 29 | |
| 30 | echo "Downloading Mozilla CA bundle ($BUNDLE_URL)..." |
| 31 | curl -LfsS -o "$TMP_DIR/cacert.pem" "$BUNDLE_URL" || { echo "curl failed for bundle"; exit 1; } |
| 32 | |
| 33 | # Split bundle into individual certs |
| 34 | echo "Splitting bundle..." |
| 35 | awk -v tmpdir="$TMP_DIR" ' |
| 36 | BEGIN { n=0; out="" } |
| 37 | /BEGIN CERTIFICATE/ { out=tmpdir"/cert-"n".pem"; n++ } |
| 38 | { if(out!="") print > out } |
| 39 | /END CERTIFICATE/ { out="" } |
| 40 | ' "$TMP_DIR/cacert.pem" |
| 41 | |
| 42 | typeset -A ROOTS |
| 43 | # FIRST_PARTY: ISRG X1/X2 + GTS R1-R4 |
| 44 | ROOTS[isrgrootx1]="ISRG Root X1" |
| 45 | ROOTS[isrgrootx2]="ISRG Root X2" |
| 46 | ROOTS[gts-root-r1]="GTS Root R1" |
| 47 | ROOTS[gts-root-r2]="GTS Root R2" |
| 48 | ROOTS[gts-root-r3]="GTS Root R3" |
| 49 | ROOTS[gts-root-r4]="GTS Root R4" |
| 50 | # STANDARD adds DigiCert, Baltimore, Amazon, Sectigo |
| 51 | ROOTS[digicert-global-g2]="DigiCert Global Root G2" |
| 52 | ROOTS[digicert-global-g3]="DigiCert Global Root G3" |
| 53 | ROOTS[baltimore-cybertrust]="Baltimore CyberTrust Root" |
| 54 | ROOTS[amazon-root-ca1]="Amazon Root CA 1" |
| 55 | ROOTS[amazon-root-ca2]="Amazon Root CA 2" |
| 56 | ROOTS[amazon-root-ca3]="Amazon Root CA 3" |
| 57 | ROOTS[amazon-root-ca4]="Amazon Root CA 4" |
| 58 | ROOTS[usertrust-rsa]="USERTrust RSA Certification Authority" |
| 59 | # EXTENDED |
| 60 | ROOTS[microsoft-rsa-2017]="Microsoft RSA Root Certificate Authority 2017" |
| 61 | ROOTS[apple-root-g2]="Apple Root CA - G2" |
| 62 | ROOTS[apple-root-g3]="Apple Root CA - G3" |
| 63 | ROOTS[apple-ist-ca2-g1]="Apple IST CA 2 - G1" |
| 64 | ROOTS[globalsign-root-r3]="GlobalSign Root CA - R3" |
| 65 | ROOTS[godaddy-root-g2]="Go Daddy Root Certificate Authority - G2" |
| 66 | |
| 67 | # SHA-256 of each committed assets/ca/<label>.der. A label with no pin is never installed. |
| 68 | typeset -A PINS |
| 69 | PINS[amazon-root-ca1]="8ecde6884f3d87b1125ba31ac3fcb13d7016de7f57cc904fe1cb97c6ae98196e" |
| 70 | PINS[amazon-root-ca2]="1ba5b2aa8c65401a82960118f80bec4f62304d83cec4713a19c39c011ea46db4" |
| 71 | PINS[amazon-root-ca3]="18ce6cfe7bf14e60b2e347b8dfe868cb31d02ebb3ada271569f50343b46db3a4" |
| 72 | PINS[amazon-root-ca4]="e35d28419ed02025cfa69038cd623962458da5c695fbdea3c22b0bfb25897092" |
| 73 | PINS[apple-ist-ca2-g1]="b0d40aa5f024f98e7adc0b10f19764f71030cfaf3dcc4ddc6600869499c9baaa" |
| 74 | PINS[apple-root-g2]="c2b9b042dd57830e7d117dac55ac8ae19407d38e41d88f3215bc3a890444a050" |
| 75 | PINS[apple-root-g3]="63343abfb89a6a03ebb57e9b3f5fa7be7c4f5c756f3017b3a8c488c3653e9179" |
| 76 | PINS[baltimore-cybertrust]="16af57a9f676b0ab126095aa5ebadef22ab31119d644ac95cd4b93dbf3f26aeb" |
| 77 | PINS[digicert-global-g2]="cb3ccbb76031e5e0138f8dd39a23f9de47ffc35e43c1144cea27d46a5ab1cb5f" |
| 78 | PINS[digicert-global-g3]="31ad6648f8104138c738f39ea4320133393e3a18cc02296ef97c2ac9ef6731d0" |
| 79 | PINS[globalsign-root-r3]="cbb522d7b7f127ad6a0113865bdf1cd4102e7d0759af635a7cf4720dc963c53b" |
| 80 | # godaddy-root-g2.der is committed PEM-encoded despite the extension, so a freshly |
| 81 | # converted DER will not match this pin until that asset is itself re-encoded. |
| 82 | PINS[godaddy-root-g2]="500329abac100a953a7396b54b36be57d333022f17401bc948248ea179cf1784" |
| 83 | PINS[gts-root-r1]="d947432abde7b7fa90fc2e6b59101b1280e0e1c7e4e40fa3c6887fff57a7f4cf" |
| 84 | PINS[gts-root-r2]="8d25cd97229dbf70356bda4eb3cc734031e24cf00fafcfd32dc76eb5841c7ea8" |
| 85 | PINS[gts-root-r3]="34d8a73ee208d9bcdb0d956520934b4e40e69482596e8b6f73c8426b010a6f48" |
| 86 | PINS[gts-root-r4]="349dfa4058c5e263123b398ae795573c4e1313c83fe68f93556cd5e8031b3c7d" |
| 87 | PINS[isrgrootx1]="96bcec06264976f37460779acf28c5a7cfe8a3c0aae11a8ffcee05c0bddf08c6" |
| 88 | PINS[isrgrootx2]="69729b8e15a86efc177a57afb7171dfc64add28c2fca8cf1507e34453ccb1470" |
| 89 | PINS[microsoft-rsa-2017]="c741f70f4b2a8d88bf2e71c14122ef53ef10eba0cfa5e64cfa20f418853073e0" |
| 90 | PINS[usertrust-rsa]="e793c9b02fd8aa13e21c31228accb08119643b749c898964b1746d46c3d4cbd2" |
| 91 | |
| 92 | # Fallback sources for roots the bundle may drop. Partial by design: a root with no entry |
| 93 | # here simply has to come from the bundle. Every fetch is pin-checked either way. |
| 94 | typeset -A URLS |
| 95 | URLS[isrgrootx1]="https://letsencrypt.org/certs/isrgrootx1.pem" |
| 96 | URLS[isrgrootx2]="https://letsencrypt.org/certs/isrg-root-x2.pem" |
| 97 | URLS[gts-root-r1]="https://pki.goog/roots/gtsr1.pem" |
| 98 | URLS[gts-root-r2]="https://pki.goog/roots/gtsr2.pem" |
| 99 | URLS[gts-root-r3]="https://pki.goog/roots/gtsr3.pem" |
| 100 | URLS[gts-root-r4]="https://pki.goog/roots/gtsr4.pem" |
| 101 | URLS[digicert-global-g2]="https://cacerts.digicert.com/DigiCertGlobalRootG2.crt" |
| 102 | URLS[digicert-global-g3]="https://cacerts.digicert.com/DigiCertGlobalRootG3.crt" |
| 103 | URLS[baltimore-cybertrust]="https://cacerts.digicert.com/BaltimoreCyberTrustRoot.crt" |
| 104 | URLS[amazon-root-ca1]="https://www.amazontrust.com/repository/AmazonRootCA1.pem" |
| 105 | URLS[amazon-root-ca2]="https://www.amazontrust.com/repository/AmazonRootCA2.pem" |
| 106 | URLS[amazon-root-ca3]="https://www.amazontrust.com/repository/AmazonRootCA3.pem" |
| 107 | URLS[amazon-root-ca4]="https://www.amazontrust.com/repository/AmazonRootCA4.pem" |
| 108 | |
| 109 | sha256_of() { |
| 110 | if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 |
| 111 | elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1 |
| 112 | else openssl dgst -sha256 "$1" | awk '{print $NF}' |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | find_pem_in_bundle() { |
| 117 | local label=$1 pattern=$2 |
| 118 | for cert in "$TMP_DIR"/cert-*.pem; do |
| 119 | if openssl x509 -in "$cert" -noout -subject 2>/dev/null | grep -q "$pattern"; then |
| 120 | cp "$cert" "$TMP_DIR/$label.pem" |
| 121 | echo " Found $label <- $pattern" |
| 122 | return 0 |
| 123 | fi |
| 124 | done |
| 125 | # fallback text grep |
| 126 | for cert in "$TMP_DIR"/cert-*.pem; do |
| 127 | if openssl x509 -in "$cert" -noout -text 2>/dev/null | grep -q "$pattern"; then |
| 128 | cp "$cert" "$TMP_DIR/$label.pem" |
| 129 | echo " Found $label <- $pattern (text)" |
| 130 | return 0 |
| 131 | fi |
| 132 | done |
| 133 | return 1 |
| 134 | } |
| 135 | |
| 136 | success=0 |
| 137 | fail=0 |
| 138 | for label in ${(k)ROOTS}; do |
| 139 | pattern="${ROOTS[$label]}" |
| 140 | if find_pem_in_bundle "$label" "$pattern"; then |
| 141 | success=$((success+1)) |
| 142 | else |
| 143 | if [[ -n "${URLS[$label]:-}" ]]; then |
| 144 | echo " Trying direct URL for $label: ${URLS[$label]}" |
| 145 | if curl -LfsS -o "$TMP_DIR/$label.pem" "${URLS[$label]}"; then |
| 146 | echo " Fetched $label via URL" |
| 147 | success=$((success+1)) |
| 148 | else |
| 149 | echo " FAIL to fetch $label via URL" >&2 |
| 150 | fail=$((fail+1)) |
| 151 | fi |
| 152 | else |
| 153 | echo " MISSING $label ($pattern) — will remain system-trust fallback" |
| 154 | fail=$((fail+1)) |
| 155 | fi |
| 156 | fi |
| 157 | done |
| 158 | |
| 159 | echo "Converting PEM -> DER in $TMP_DIR, installing only on pin match..." |
| 160 | mkdir -p "$TMP_DIR/der" |
| 161 | installed=0 |
| 162 | rejected=0 |
| 163 | for label in ${(k)ROOTS}; do |
| 164 | pem="$TMP_DIR/$label.pem" |
| 165 | [[ -f "$pem" ]] || continue |
| 166 | der="$TMP_DIR/der/$label.der" |
| 167 | if ! openssl x509 -in "$pem" -outform der -out "$der" 2>/dev/null; then |
| 168 | # maybe DER input |
| 169 | if ! openssl x509 -in "$pem" -inform der -outform der -out "$der" 2>/dev/null; then |
| 170 | echo " FAIL converting $pem" >&2 |
| 171 | rm -f "$der" |
| 172 | rejected=$((rejected+1)) |
| 173 | continue |
| 174 | fi |
| 175 | fi |
| 176 | want="${PINS[$label]:-}" |
| 177 | got=$(sha256_of "$der") |
| 178 | if [[ -z "$want" ]]; then |
| 179 | echo " UNPINNED $label — not written (got $got)" >&2 |
| 180 | rejected=$((rejected+1)) |
| 181 | elif [[ "$got" != "$want" ]]; then |
| 182 | printf ' MISMATCH %s\n want %s\n got %s\n' "$label" "$want" "$got" >&2 |
| 183 | echo " Not written. Inspect the certificate, then commit the new .der and its pin together." >&2 |
| 184 | rejected=$((rejected+1)) |
| 185 | else |
| 186 | mv -f "$der" "$OUT_DIR/$label.der" |
| 187 | echo " $label.der ok $got" |
| 188 | installed=$((installed+1)) |
| 189 | fi |
| 190 | done |
| 191 | |
| 192 | # Pruning happens last and only on a clean run, so a failed fetch can never leave the |
| 193 | # shipped trust set short of an anchor. |
| 194 | if (( clean )); then |
| 195 | if (( fail == 0 && rejected == 0 )); then |
| 196 | for der in "$OUT_DIR"/*.der(N); do |
| 197 | label="${${der:t}%.der}" |
| 198 | if [[ -z "${PINS[$label]:-}" ]]; then |
| 199 | rm -f "$der" |
| 200 | echo "Removed unpinned $label.der" |
| 201 | fi |
| 202 | done |
| 203 | else |
| 204 | echo "--clean skipped: run had failures, not pruning $OUT_DIR" >&2 |
| 205 | fi |
| 206 | fi |
| 207 | |
| 208 | echo "" |
| 209 | echo "Result: $success found, $fail missing, $installed installed, $rejected rejected" |
| 210 | ls -lh "$OUT_DIR" 2>/dev/null |
| 211 | echo "" |
| 212 | echo "Verify: openssl x509 -in $OUT_DIR/isrgrootx1.der -inform der -noout -subject || true" |
| 213 | |
| 214 | if (( fail || rejected )); then |
| 215 | exit 1 |
| 216 | fi |