summaryrefslogtreecommitdiff
path: root/tools/fetchallcerts.py
blob: 801e29606c8397a6aaf174074bb8d12d44b27dd4 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2014, NORDUnet A/S.
# See LICENSE for licensing information.

import urllib2
import urllib
import json
import base64
import sys
import struct
import hashlib
import itertools
from certtools import *

def extract_original_entry(entry):
    leaf_input =  base64.decodestring(entry["leaf_input"])
    (leaf_cert, timestamp) = unpack_mtl(leaf_input)
    extra_data = base64.decodestring(entry["extra_data"])
    certchain = decode_certificate_chain(extra_data)
    return [leaf_cert] + certchain

def get_entries_wrapper(baseurl, start, end):
    fetched_entries = []
    while len(fetched_entries) < (end - start + 1):
        print "fetching from", start + len(fetched_entries)
        entries = get_entries(baseurl, start + len(fetched_entries), end)["entries"]
        if len(entries) == 0:
            break
        fetched_entries.extend(entries)
    return fetched_entries

baseurl = sys.argv[1]
destination_directory = sys.argv[2]

sth = get_sth(baseurl)
tree_size = sth["tree_size"]

print tree_size

entries = get_entries_wrapper(baseurl, 0, tree_size)

print len(entries)

for entry, i in zip(entries, range(0, len(entries))):
    chain = extract_original_entry(entry)
    f = open(destination_directory + "/" + ("%06d" % i), "w")
    for cert in chain:
        print >> f, "-----BEGIN CERTIFICATE-----"
        print >> f, base64.encodestring(cert).rstrip()
        print >> f, "-----END CERTIFICATE-----"
        print >> f, ""