Skip to content
pi-c Artifact [606f1a8ffc]
Login

Artifact [606f1a8ffc]

Artifact 606f1a8ffc27a100c9ad3b8abd62311b88a4405e2b639df19b0931963d223076:


#!/bin/sh
# Regenerates src/ca_bundle.inc: Mozilla's trusted root certificates (as curl
# publishes them, https://curl.se/ca/cacert.pem), compiled into pic as the
# fallback when no system bundle exists (a minimal host or a VM without a
# userland). Needs curl. Usage: sh tools/ca-bundle.sh [CACERT_PEM]
set -eu
cd "$(dirname "$0")/.."
tmp=$(mktemp -d "${TMPDIR:-/tmp}/pic-ca.XXXXXX")
trap 'rm -rf -- "$tmp"' EXIT HUP INT TERM
if [ $# -ge 1 ]; then cp "$1" "$tmp/ca.pem"; else curl -fsS -m 60 -o "$tmp/ca.pem" https://curl.se/ca/cacert.pem; fi
sum=$(shasum -a 256 "$tmp/ca.pem" | cut -d' ' -f1)
dated=$(sed -n 's/^## Certificate data from Mozilla as of: //p' "$tmp/ca.pem" | head -1)
{
    printf '/* Generated by tools/ca-bundle.sh from https://curl.se/ca/cacert.pem (Mozilla roots as of\n'
    printf ' * %s; sha256 %s). Do not edit: regenerate. */\n' "$dated" "$sum"
    printf 'static const char *const builtin_anchors[] = {\n'
    awk '/-----BEGIN CERTIFICATE-----/ { inside = 1; printf "    \"" }
         inside { printf "%s\\n", $0 }
         /-----END CERTIFICATE-----/ { inside = 0; printf "\",\n" }' "$tmp/ca.pem"
    printf '};\n'
} > "$tmp/out"
n=$(grep -c 'BEGIN CERTIFICATE' "$tmp/out")
[ "$n" -gt 100 ] || { echo "ca-bundle: only $n certificates; refusing" >&2; exit 1; }
mv "$tmp/out" src/ca_bundle.inc
printf 'ca-bundle: %s certificates written to src/ca_bundle.inc\n' "$n"