Files
rustls/admin/capture-certdata
Daniel McCarney 2149288539 fix: avoid hardcoded python3 path, fix prints.
Prior to this commit the admin `bench-range`, `capture-certdata` and
`format-bench` Python helpers used a hardcoded path to Python3 in their
shebang. This commit updates each to use `/usr/bin/env` to find
`python3` in the `$PATH`.

These helpers also used Python2 style `print` statements instead of the
Python3 style `print` function. This commit adds the required braces to
fix Python3 usage.
2023-03-02 10:28:10 +01:00

63 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
import subprocess
import base64
from os import path
SITES = dict(
google = 'www.google.com',
duckduckgo = 'duckduckgo.com',
github = 'github.com',
wikipedia = 'wikipedia.org',
arstechnica = 'arstechnica.com',
reddit = 'reddit.com',
hn = 'news.ycombinator.com',
servo = 'servo.org',
rustlang = 'www.rust-lang.org',
wapo = 'www.washingtonpost.com',
twitter = 'twitter.com',
stackoverflow = 'stackoverflow.com',
)
def extract_certs(lines):
buffer = None
for l in lines:
if b'-----BEGIN CERT' in l:
buffer = b''
elif b'-----END CERT' in l and buffer is not None:
yield base64.b64decode(buffer)
buffer = None
elif buffer is not None:
buffer += l
def collect(hostname):
subp = subprocess.Popen([
'openssl',
's_client',
'-showcerts',
'-connect',
hostname + ':443',
],
stderr = subprocess.PIPE,
stdout = subprocess.PIPE,
stdin = subprocess.PIPE)
stdout, stderr = subp.communicate('')
stdout = stdout.splitlines()
certs = list(extract_certs(stdout))
return certs
if __name__ == '__main__':
certfile = lambda name, i: 'rustls/src/testdata/cert-%s.%d.der' % (name, i)
for name, hostname in SITES.items():
if path.exists(certfile(name, 0)):
continue
certchain = collect(hostname)
for i, cert in enumerate(certchain):
open(certfile(name, i), 'wb').write(cert)
print('wrote', certfile(name, i))