File: capture-certdata

package info (click to toggle)
rust-rustls 0.23.26%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,816 kB
  • sloc: sh: 199; python: 181; makefile: 23
file content (62 lines) | stat: -rwxr-xr-x 1,602 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/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))