Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / download-ca-bundle.sh

download-ca-bundle.sh216 lines · 8.8 KiBRaw
1#!/usr/bin/env zsh
2set -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
18OUT_DIR="${0:A:h:h}/library/network/src/main/assets/ca"
19BUNDLE_URL="https://curl.se/ca/cacert.pem"
20TMP_DIR=$(mktemp -d)
21trap 'rm -rf "$TMP_DIR"' EXIT
22
23clean=0
24if [[ "${1:-}" == "--clean" ]]; then
25 clean=1
26fi
27
28mkdir -p "$OUT_DIR"
29
30echo "Downloading Mozilla CA bundle ($BUNDLE_URL)..."
31curl -LfsS -o "$TMP_DIR/cacert.pem" "$BUNDLE_URL" || { echo "curl failed for bundle"; exit 1; }
32
33# Split bundle into individual certs
34echo "Splitting bundle..."
35awk -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
42typeset -A ROOTS
43# FIRST_PARTY: ISRG X1/X2 + GTS R1-R4
44ROOTS[isrgrootx1]="ISRG Root X1"
45ROOTS[isrgrootx2]="ISRG Root X2"
46ROOTS[gts-root-r1]="GTS Root R1"
47ROOTS[gts-root-r2]="GTS Root R2"
48ROOTS[gts-root-r3]="GTS Root R3"
49ROOTS[gts-root-r4]="GTS Root R4"
50# STANDARD adds DigiCert, Baltimore, Amazon, Sectigo
51ROOTS[digicert-global-g2]="DigiCert Global Root G2"
52ROOTS[digicert-global-g3]="DigiCert Global Root G3"
53ROOTS[baltimore-cybertrust]="Baltimore CyberTrust Root"
54ROOTS[amazon-root-ca1]="Amazon Root CA 1"
55ROOTS[amazon-root-ca2]="Amazon Root CA 2"
56ROOTS[amazon-root-ca3]="Amazon Root CA 3"
57ROOTS[amazon-root-ca4]="Amazon Root CA 4"
58ROOTS[usertrust-rsa]="USERTrust RSA Certification Authority"
59# EXTENDED
60ROOTS[microsoft-rsa-2017]="Microsoft RSA Root Certificate Authority 2017"
61ROOTS[apple-root-g2]="Apple Root CA - G2"
62ROOTS[apple-root-g3]="Apple Root CA - G3"
63ROOTS[apple-ist-ca2-g1]="Apple IST CA 2 - G1"
64ROOTS[globalsign-root-r3]="GlobalSign Root CA - R3"
65ROOTS[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.
68typeset -A PINS
69PINS[amazon-root-ca1]="8ecde6884f3d87b1125ba31ac3fcb13d7016de7f57cc904fe1cb97c6ae98196e"
70PINS[amazon-root-ca2]="1ba5b2aa8c65401a82960118f80bec4f62304d83cec4713a19c39c011ea46db4"
71PINS[amazon-root-ca3]="18ce6cfe7bf14e60b2e347b8dfe868cb31d02ebb3ada271569f50343b46db3a4"
72PINS[amazon-root-ca4]="e35d28419ed02025cfa69038cd623962458da5c695fbdea3c22b0bfb25897092"
73PINS[apple-ist-ca2-g1]="b0d40aa5f024f98e7adc0b10f19764f71030cfaf3dcc4ddc6600869499c9baaa"
74PINS[apple-root-g2]="c2b9b042dd57830e7d117dac55ac8ae19407d38e41d88f3215bc3a890444a050"
75PINS[apple-root-g3]="63343abfb89a6a03ebb57e9b3f5fa7be7c4f5c756f3017b3a8c488c3653e9179"
76PINS[baltimore-cybertrust]="16af57a9f676b0ab126095aa5ebadef22ab31119d644ac95cd4b93dbf3f26aeb"
77PINS[digicert-global-g2]="cb3ccbb76031e5e0138f8dd39a23f9de47ffc35e43c1144cea27d46a5ab1cb5f"
78PINS[digicert-global-g3]="31ad6648f8104138c738f39ea4320133393e3a18cc02296ef97c2ac9ef6731d0"
79PINS[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.
82PINS[godaddy-root-g2]="500329abac100a953a7396b54b36be57d333022f17401bc948248ea179cf1784"
83PINS[gts-root-r1]="d947432abde7b7fa90fc2e6b59101b1280e0e1c7e4e40fa3c6887fff57a7f4cf"
84PINS[gts-root-r2]="8d25cd97229dbf70356bda4eb3cc734031e24cf00fafcfd32dc76eb5841c7ea8"
85PINS[gts-root-r3]="34d8a73ee208d9bcdb0d956520934b4e40e69482596e8b6f73c8426b010a6f48"
86PINS[gts-root-r4]="349dfa4058c5e263123b398ae795573c4e1313c83fe68f93556cd5e8031b3c7d"
87PINS[isrgrootx1]="96bcec06264976f37460779acf28c5a7cfe8a3c0aae11a8ffcee05c0bddf08c6"
88PINS[isrgrootx2]="69729b8e15a86efc177a57afb7171dfc64add28c2fca8cf1507e34453ccb1470"
89PINS[microsoft-rsa-2017]="c741f70f4b2a8d88bf2e71c14122ef53ef10eba0cfa5e64cfa20f418853073e0"
90PINS[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.
94typeset -A URLS
95URLS[isrgrootx1]="https://letsencrypt.org/certs/isrgrootx1.pem"
96URLS[isrgrootx2]="https://letsencrypt.org/certs/isrg-root-x2.pem"
97URLS[gts-root-r1]="https://pki.goog/roots/gtsr1.pem"
98URLS[gts-root-r2]="https://pki.goog/roots/gtsr2.pem"
99URLS[gts-root-r3]="https://pki.goog/roots/gtsr3.pem"
100URLS[gts-root-r4]="https://pki.goog/roots/gtsr4.pem"
101URLS[digicert-global-g2]="https://cacerts.digicert.com/DigiCertGlobalRootG2.crt"
102URLS[digicert-global-g3]="https://cacerts.digicert.com/DigiCertGlobalRootG3.crt"
103URLS[baltimore-cybertrust]="https://cacerts.digicert.com/BaltimoreCyberTrustRoot.crt"
104URLS[amazon-root-ca1]="https://www.amazontrust.com/repository/AmazonRootCA1.pem"
105URLS[amazon-root-ca2]="https://www.amazontrust.com/repository/AmazonRootCA2.pem"
106URLS[amazon-root-ca3]="https://www.amazontrust.com/repository/AmazonRootCA3.pem"
107URLS[amazon-root-ca4]="https://www.amazontrust.com/repository/AmazonRootCA4.pem"
108
109sha256_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
116find_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
136success=0
137fail=0
138for 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
157done
158
159echo "Converting PEM -> DER in $TMP_DIR, installing only on pin match..."
160mkdir -p "$TMP_DIR/der"
161installed=0
162rejected=0
163for 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
190done
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.
194if (( 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
206fi
207
208echo ""
209echo "Result: $success found, $fail missing, $installed installed, $rejected rejected"
210ls -lh "$OUT_DIR" 2>/dev/null
211echo ""
212echo "Verify: openssl x509 -in $OUT_DIR/isrgrootx1.der -inform der -noout -subject || true"
213
214if (( fail || rejected )); then
215 exit 1
216fi