~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/stream

jetstream v2 in zig · mirror of https://tangled.org/zat.dev/stream

0
0
63 lines · 2.4 KB Raw History
import argparse
import hashlib
import re
import subprocess
from pathlib import Path


BLOCKED = {
    "c619e4780ed8e0abeda24ad732567de4383a7ea941338f6d1a7d1b49ac792fe9",
    "0a6c2b2ca5b364207954171987ac9d2178561569d8205457e77ca90c32ede282",
    "bd153ed7b0434f6886b17bce8bbe84ed340c7132d702a8f4fa318f756ecbd6f3",
    "8d417db2dd8bf5e3084d1e3f196d583849d81bdd4c00c70b9d39369e96b8c782",
    "ebed6475c2c2d80fcb199f3fe8dd6e6b3ce60b77eb15d4e42c3dec0c6f5b5105",
    "43bffa19d6ef651451f6841e019931e96f6f2eaeff28ab4b6f1ce4576de2717b",
    "e0254c7fcdc265e2f0f8a428dabb75ab4ae039b4872d62f5ee57f0877d5a8a41",
    "49dd353dc2b919a3770645117d0e60c16115e7d1bc259eefe734b00ea434fe5f",
    "63ecefee431d49523bc1136c4e72d0206a477086caf606cd5cf025686c109cf8",
    "5c0660a2b164910d8ff589c18e14552e9e0ec237dcefe2cb736d41a4811f81a0",
    "ec4ffa1ad0ac88081314b7aaffef6a91f97cd57291bb5ba686c5415f5eb4f6b6",
    "92c063d67e8f14db71d288e11266ffdaf69cbf115f7bab14f3bc2f68d25e0e30",
}
CERTIFICATE = re.compile(
    rb"-----BEGIN (CERTIFICATE|TRUSTED CERTIFICATE)-----.*?-----END \1-----",
    re.DOTALL,
)


def filter_bundle(bundle):
    certificates = list(CERTIFICATE.finditer(bundle))
    remainder = CERTIFICATE.sub(b"", bundle)
    if not certificates or b"-----BEGIN" in remainder or b"-----END" in remainder:
        raise ValueError("CA bundle has missing, malformed, or unexpected PEM blocks")
    kept = []
    for match in certificates:
        cert = match.group()
        public = subprocess.run(
            ["openssl", "x509", "-pubkey", "-noout"],
            input=cert, capture_output=True, check=True,
        ).stdout
        der = subprocess.run(
            ["openssl", "pkey", "-pubin", "-outform", "DER"],
            input=public, capture_output=True, check=True,
        ).stdout
        if hashlib.sha256(der).hexdigest() not in BLOCKED:
            kept.append(cert)
    if not kept:
        raise ValueError("CA bundle has no remaining trusted certificates")
    return b"\n".join(kept) + b"\n", len(certificates) - len(kept)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("source", type=Path)
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    bundle, removed = filter_bundle(args.source.read_bytes())
    with args.output.open("xb") as target:
        target.write(bundle)
    print(f"Public CA bundle: removed={removed} sha256={hashlib.sha256(bundle).hexdigest()}")


if __name__ == "__main__":
    main()