get genica.bundle and extract certificates
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 10 Nov 2010 11:15:20 +0000 (12:15 +0100)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Wed, 10 Nov 2010 11:15:20 +0000 (12:15 +0100)
scripts/get_genica_bundle.py [new file with mode: 0755]

diff --git a/scripts/get_genica_bundle.py b/scripts/get_genica_bundle.py
new file mode 100755 (executable)
index 0000000..3eb6780
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+import os
+import re
+import urllib2
+import subprocess
+
+BUNDLE_URL="https://www.emulab.net/genica.bundle"
+
+def get_bundle():
+    u = urllib2.urlopen(BUNDLE_URL)
+    upath = urllib2.urlparse.urlsplit(u.url)[2]
+    bundle_fname = os.path.basename(upath)
+    open(bundle_fname, 'w').write(u.read())
+    print "Downloaded:", bundle_fname
+    return bundle_fname
+
+def get_CN(cert):
+    p = subprocess.Popen(['openssl', 'x509', '-issuer'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+    p.stdin.write(cert)
+    m = re.search("(CN=)(.*)([\/ ])", p.stdout.readline())
+    if m:
+        return m.group(2)
+
+def write_cert(cert):
+    cn = get_CN(cert)
+    if not cn: return
+    fname = '%s.gid' % cn
+    open(fname, 'w').write(cert)
+    print "Wrote:", fname
+
+def extract(fname):
+    bundle = open(fname, 'r')
+    in_cert = False
+    cert = ""
+    for line in bundle:
+        if line.startswith("-----BEGIN CERTIFICATE-----"):
+            in_cert = True
+        elif line.startswith("-----END CERTIFICATE-----"):
+            in_cert = False
+            cert += line
+            write_cert(cert)
+            cert = ""
+
+        if in_cert:
+            cert += line
+
+if __name__ == "__main__":
+    bundle = get_bundle()
+    extract(bundle)