#!/bin/bash # 取出 relay.xxmxxs.top 证书链上的根/中间证书,准备打进 APK。 # # 背景:手表报 SSLHandshakeException: Chain validation failed # 链路是 xxmxxs.top <- GTS WE1 <- GTS Root R4 <- GlobalSign Root CA # 廉价手表的根证书库常常缺 GTS Root R4, # 于是找不到信任锚 -> 握手失败。 # 把这两个根打进 APK,校验时"系统库 + 内置"任一通过即可。 set -u OUT=/root/ca-bundle mkdir -p "$OUT" cd "$OUT" echo "===== 抓完整证书链 =====" echo | openssl s_client -showcerts -connect relay.xxmxxs.top:443 \ -servername relay.xxmxxs.top 2>/dev/null > /root/chain.txt grep -c 'BEGIN CERTIFICATE' /root/chain.txt | sed 's/^/ 链上证书数: /' # 把每张证书拆出来 awk '/BEGIN CERTIFICATE/,/END CERTIFICATE/' /root/chain.txt > /root/chain.pem csplit -z -f "$OUT/cert" -b "%02d.pem" /root/chain.pem \ '/-----BEGIN CERTIFICATE-----/' '{*}' >/dev/null 2>&1 || true echo echo "===== 每张证书的信息 =====" for f in "$OUT"/cert*.pem; do [ -f "$f" ] || continue echo " --- $(basename "$f") ---" openssl x509 -in "$f" -noout -subject -issuer 2>/dev/null \ | sed 's/^/ /' done echo echo "===== 找 GTS Root R4 的自签名证书 =====" # GTS Root R4 是通过 GlobalSign 交叉签名的; # 真正需要内置的是**自签名的根**(GTS Root R4 self-signed 与 GlobalSign Root CA)。 for candidate in \ "https://i.pki.goog/r4.crt" \ "https://pki.goog/repo/certs/gtsr4.pem" \ "https://pki.goog/repo/certs/gsr2.pem"; do name=$(basename "$candidate") if curl -s --max-time 20 -o "$OUT/$name" "$candidate" 2>/dev/null \ && [ -s "$OUT/$name" ]; then # 有的下载下来是 DER,转成 PEM if ! grep -q 'BEGIN CERTIFICATE' "$OUT/$name" 2>/dev/null; then openssl x509 -inform DER -in "$OUT/$name" \ -out "$OUT/${name}.pem" 2>/dev/null \ && rm -f "$OUT/$name" fi echo " ✓ $candidate" openssl x509 -in "$OUT/${name}.pem" -noout -subject -issuer 2>/dev/null \ | sed 's/^/ /' else echo " ✗ $candidate" fi done echo echo "===== 系统里现成的根证书(Debian 上有)=====" for name in "GlobalSign_Root_CA.pem" "gtsrootr4.pem" "GTS_Root_R4.pem"; do found=$(find /etc/ssl/certs /usr/share/ca-certificates -name "$name" 2>/dev/null | head -1) [ -n "$found" ] && echo " ✓ $found" done echo echo "===== 汇总可用的 PEM =====" ls -la "$OUT"/*.pem 2>/dev/null | sed 's/^/ /'