-
-
Save hellais/f2f95cf4b397fe6cb9fec7f966668a8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import lru_cache | |
from base64 import b64decode | |
import requests | |
from cryptography import x509 | |
from cryptography.hazmat.backends import default_backend | |
from cryptography.hazmat.primitives import hashes | |
from cryptography.hazmat.primitives.asymmetric import padding | |
def get_raw_msmt(measurement_uid): | |
r = requests.get(f"https://api.ooni.io/api/v1/raw_measurement?measurement_uid={measurement_uid}") | |
return r.json() | |
@lru_cache(maxsize=None) | |
def load_cert_from_url(url): | |
d = requests.get(url) | |
return x509.load_pem_x509_certificate(d.text.encode('ascii')) | |
def load_cert_from_measurement(raw_msmt): | |
raw_cert = b64decode(raw_msmt['test_keys']['tls_handshakes'][0]['peer_certificates'][0]['data']) | |
return x509.load_der_x509_certificate(raw_cert, default_backend()) | |
POTENTIAL_ROOT_CAS = [ | |
"https://crt.sh/?d=4478765041", | |
"https://crt.sh/?d=4833570779", | |
"https://crt.sh/?d=4739909320", | |
"https://crt.sh/?d=4633597326", | |
"https://crt.sh/?d=3967758934", | |
"https://crt.sh/?d=12281942153", | |
"https://crt.sh/?d=11106964945", | |
"https://crt.sh/?d=14682080594", | |
"https://crt.sh/?d=3764973485", | |
"https://isca.gov.kz/Information_Security_Certification_Authority_CA_pem.crt", | |
] | |
def find_root_ca(cert): | |
for url in POTENTIAL_ROOT_CAS: | |
issuer_cert = load_cert_from_url(url) | |
issuer_pub_key = issuer_cert.public_key() | |
try: | |
issuer_pub_key.verify( | |
cert.signature, | |
cert.tbs_certificate_bytes, | |
padding.PKCS1v15(), | |
cert.signature_hash_algorithm, | |
) | |
return (url, issuer_cert) | |
except Exception as e: | |
pass | |
return (None, None) | |
OONI_MEASUREMENTS = [ | |
"20210808015758.022737_KZ_webconnectivity_3b9213f9ee4f2d06", | |
"20210914080702.850310_KZ_webconnectivity_88ece394d9a0fcdc", | |
"20231016130600.035487_KZ_webconnectivity_4a5c38a0f8bea740", | |
"20240317052821.044604_KZ_webconnectivity_3752cbf5dac624e9", | |
"20231118140134.149173_KZ_webconnectivity_a93dfc958ab79ec2", | |
"20240418133819.497733_KZ_webconnectivity_bd3a0d69cd5e8aca", | |
"20240901151413.637888_KZ_webconnectivity_c351db70f739197a" | |
] | |
def print_cert_meta(cert, prefix=""): | |
fp = cert.fingerprint(hashes.SHA1()).hex() | |
serial = cert.serial_number | |
issuer = cert.issuer.rfc4514_string() | |
nvb = cert.not_valid_before.strftime("%Y-%m-%d %H:%M:%S") | |
nva = cert.not_valid_after.strftime("%Y-%m-%d %H:%M:%S") | |
print(f"{prefix}Fingerprint: {fp}") | |
print(f"{prefix}Serial: {serial}") | |
print(f"{prefix}Not valid before: {nvb}") | |
print(f"{prefix}Not valid after: {nva}") | |
def print_meta(msmt_uid, cert, root_ca_url, root_ca): | |
print(f"[ooni measurement](https://explorer.ooni.org/m/{msmt_uid})") | |
print("```") | |
print_cert_meta(cert) | |
print(f"Issuer: {cert.issuer.rfc4514_string()}") | |
if root_ca_url: | |
print(f"Root CA Cert: {root_ca_url}") | |
print_cert_meta(root_ca, prefix="Root CA ") | |
else: | |
print("Root CA Unknown") | |
print("```") | |
for msmt_uid in sorted(OONI_MEASUREMENTS): | |
raw_msmt = get_raw_msmt(msmt_uid) | |
cert = load_cert_from_measurement(raw_msmt) | |
root_ca_url, root_ca = find_root_ca(cert) | |
print_meta(msmt_uid, cert, root_ca_url, root_ca) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment