From 0d7c8181f07e1c34ef3a9a8740c8ce2d7b982f74 Mon Sep 17 00:00:00 2001 From: Magnus Ahltorp Date: Wed, 19 Aug 2015 09:55:02 +0200 Subject: Script for converting database to new format --- tools/convertdb.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tools/convertdb.py (limited to 'tools/convertdb.py') diff --git a/tools/convertdb.py b/tools/convertdb.py new file mode 100644 index 0000000..c036843 --- /dev/null +++ b/tools/convertdb.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Copyright (c) 2014, NORDUnet A/S. +# See LICENSE for licensing information. + +import argparse +import urllib2 +import urllib +import json +import base64 +import sys +import struct +import hashlib +import itertools +from certtools import * +from mergetools import * +import zipfile +import os +import time +import shutil + +def write_file(fn, contents): + tempname = fn + ".new" + open(tempname, 'w').write(contents) + shutil.move(tempname, fn) + +def unpack_entry(entry): + pieces = [] + while len(entry): + (length,) = struct.unpack(">I", entry[0:4]) + data = entry[4:4+length] + entry = entry[4+length:] + pieces.append(data) + return pieces + +def read_old_entry(entry, hash): + unpacked = unpack_entry(entry) + mtl = unpacked[0] + assert hash == get_leaf_hash(mtl) + (leafcert, timestamp, issuer_key_hash) = unpack_mtl(mtl) + certchain = decode_certificate_chain(unpacked[1]) + if issuer_key_hash: + leafcert = certchain[0] + certchain = certchain[1:] + certtype = "PRC1" + else: + certtype = "EEC1" + return (mtl, leafcert, certtype, certchain) + +def convertentry(entry, hash): + (mtl, leafcert, certtype, chain) = read_old_entry(entry, hash) + entry = tlv_encodelist([("MTL1", mtl), + (certtype, leafcert), + ("CHN1", tlv_encodelist([("X509", cert) for cert in chain]))]) + return wrap_entry(entry) + +parser = argparse.ArgumentParser(description='') +parser.add_argument('path', help="Path to database to convert") +args = parser.parse_args() + +for (dirpath, dirnames, filenames) in os.walk(args.path): + for filename in filenames: + fullpath = dirpath + "/" + filename + entry = open(fullpath).read() + entry = convertentry(entry, base64.b16decode(filename.upper())) + if entry != None: + print "writing new entry for", filename + write_file(fullpath, entry) + else: + print "not writing new entry for", filename -- cgit v1.1