2018-01-02 19:59:09 +00:00
|
|
|
#!/usr/bin/python3
|
2018-01-07 21:43:14 +00:00
|
|
|
|
2018-01-08 22:56:08 +00:00
|
|
|
from base64 import b64decode
|
|
|
|
from hashlib import sha256
|
2018-01-07 21:43:14 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
|
2018-01-08 22:56:08 +00:00
|
|
|
REPO = 'certifi/python-certifi'
|
2018-01-07 23:56:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def fetch_certdata():
|
2018-01-08 22:56:08 +00:00
|
|
|
r = requests.get('https://api.github.com/repos/%s/git/refs/heads/master' % REPO)
|
2018-01-07 23:56:39 +00:00
|
|
|
assert(r.status_code == 200)
|
2018-01-08 22:56:08 +00:00
|
|
|
commithash = r.json()['object']['sha']
|
2018-01-07 23:56:39 +00:00
|
|
|
|
2018-01-08 22:56:08 +00:00
|
|
|
r = requests.get('https://raw.githubusercontent.com/%s/%s/certifi/cacert.pem' % (REPO, commithash))
|
2018-01-07 23:56:39 +00:00
|
|
|
assert(r.status_code == 200)
|
|
|
|
certdata = r.text
|
|
|
|
|
|
|
|
return commithash, certdata
|
2018-01-07 21:43:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_certdata(data):
|
|
|
|
certs = {}
|
|
|
|
lines = [x.strip() for x in data.split('\n')]
|
|
|
|
label = None
|
|
|
|
value = None
|
|
|
|
for line in lines:
|
2018-01-08 22:56:08 +00:00
|
|
|
if line.startswith('# Label: '):
|
|
|
|
assert(label is None)
|
|
|
|
assert(value is None)
|
2018-01-07 21:43:14 +00:00
|
|
|
label = line.split('"')[1]
|
2018-01-08 22:56:08 +00:00
|
|
|
elif line == '-----BEGIN CERTIFICATE-----':
|
2018-01-07 21:43:14 +00:00
|
|
|
assert(label is not None)
|
2018-01-08 22:56:08 +00:00
|
|
|
assert(value is None)
|
2018-01-07 21:43:14 +00:00
|
|
|
value = ''
|
2018-01-08 22:56:08 +00:00
|
|
|
elif line == '-----END CERTIFICATE-----':
|
2018-01-07 21:43:14 +00:00
|
|
|
assert(label is not None)
|
2018-01-08 22:56:08 +00:00
|
|
|
assert(value is not None)
|
|
|
|
certs[label] = b64decode(value)
|
|
|
|
label, value = None, None
|
|
|
|
else:
|
|
|
|
if value is not None:
|
|
|
|
value += line
|
|
|
|
|
2018-01-07 21:43:14 +00:00
|
|
|
return certs
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2018-01-07 23:56:39 +00:00
|
|
|
commithash, certdata = fetch_certdata()
|
|
|
|
|
2018-01-08 22:56:08 +00:00
|
|
|
print('# fetched from https://github.com/%s' % REPO)
|
2018-01-07 23:56:39 +00:00
|
|
|
print('# commit %s' % commithash)
|
2018-01-07 21:43:14 +00:00
|
|
|
|
2018-01-07 23:56:39 +00:00
|
|
|
certs = process_certdata(certdata)
|
|
|
|
|
|
|
|
size = sum([len(x) for x in certs.values()])
|
|
|
|
print('# certs: %d | digests size: %d | total size: %d' % (len(certs), len(certs) * 32, size))
|
2018-01-07 21:43:14 +00:00
|
|
|
|
|
|
|
print('cert_bundle = [')
|
|
|
|
for k, v in certs.items():
|
2018-01-08 22:56:08 +00:00
|
|
|
h = sha256(v)
|
2018-01-07 21:43:14 +00:00
|
|
|
print(' # %s' % k)
|
2018-01-08 22:56:08 +00:00
|
|
|
print(' # %s' % h.hexdigest())
|
|
|
|
print(' %s,' % h.digest())
|
2018-01-07 21:43:14 +00:00
|
|
|
print(']')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|