aboutsummaryrefslogtreecommitdiff
path: root/myrpki/myrpki.py
blob: d499a5c002c30ac266ba65565918537f3e35d846 (plain) (blame)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# $Id$

# Basic plan here is to read in csv files for tabular data (roa
# requests, child asn assignments, child prefix assignments), read
# command line or magic file for my own handle, and read or generate
# pem for bpki ca cert and bpki ee cert (cannot do latter without
# corresponding bpki ee pkcs10).  whack all this together and generate
# some xml thing in format to be determined (need to write schema).

import subprocess, csv, sys, os

from xml.etree.ElementTree import Element, SubElement, ElementTree, tostring

# The following should all be configurable on command line, as perhaps
# should the csv conventions (dialect, delimiter, see csv module doc
# for all the fun one can have here).  For now, just wire all this in,
# add command line junk later.

my_handle         = "wombat"
roa_csv_file      = "roas.csv"
validity_csv_file = "validity.csv"
prefixes_csv_file = "prefixes.csv"
asns_csv_file     = "asns.csv"
bpki_ca_conf_file = "bpki-ca-cert.conf"
bpki_ca_cert_file = "bpki-ca-cert.pem"
bpki_ca_key_file  = "bpki-ca-key.pem"
bpki_ee_cert_file = "bpki-ee-cert.pem"
bpki_ee_req_file  = "bpki-ee-pkcs10.pem"

class comma_set(set):

  def __str__(self):
    return ",".join(self)

class roa_request(object):

  def __init__(self, asn):
    self.asn = asn
    self.prefixes = comma_set()

  def add(self, prefix):
    self.prefixes.add(prefix)

  def xml(self, e):
    return SubElement(e, "roa_request",
                      asn = self.asn,
                      prefixes = str(self.prefixes))

class roa_requests(dict):

  def add(self, asn, prefix):
    if asn not in self:
      self[asn] = roa_request(asn)
    self[asn].add(prefix)

  def xml(self, e):
    for r in self.itervalues():
      r.xml(e)

class child(object):

  def __init__(self, handle):
    self.handle = handle
    self.asns = comma_set()
    self.prefixes = comma_set()
    self.validity = None

  def add(self, prefix = None, asn = None, validity = None):
    if prefix is not None:
      self.prefixes.add(prefix)
    if asn is not None:
      self.asns.add(asn)
    if validity is not None:
      self.validity = validity

  def xml(self, e):
    return SubElement(e, "child",
                      handle = self.handle,
                      valid_until = self.validity,
                      asns = str(self.asns),
                      prefixes = str(self.prefixes))

class children(dict):

  def add(self, handle, prefix = None, asn = None, validity = None):
    if handle not in self:
      self[handle] = child(handle)
    self[handle].add(prefix = prefix, asn = asn, validity = validity)

  def xml(self, e):
    for c in self.itervalues():
      c.xml(e)

def csv_open(filename, delimiter = "\t", dialect = None):
  return csv.reader(open(filename, "rb"), dialect = dialect, delimiter = delimiter)

def PEMElement(e, tag, filename):
  SubElement(e, tag).text = "".join(open(filename).readlines()[1:-1])

def bpki_ca(e):

  if not os.path.exists(bpki_ca_key_file):
    subprocess.check_call(("openssl", "genrsa",
                           "-out", bpki_ca_key_file,
                           "2048"))

  if not os.path.exists(bpki_ca_conf_file):
    open(bpki_ca_conf_file, "w").write(bpki_ca_conf_fmt % { "handle" : my_handle })

  if not os.path.exists(bpki_ca_cert_file):
    subprocess.check_call(("openssl", "req", "-new", "-sha256", "-x509",
                           "-config", bpki_ca_conf_file,
                           "-extensions", "req_x509_ext",
                           "-key", bpki_ca_key_file,
                           "-out", bpki_ca_cert_file))

  PEMElement(e, "bpki_ca_certificate", bpki_ca_cert_file)

bpki_ca_conf_fmt = '''\
[req]
default_bits            = 2048
default_md		= sha256
distinguished_name	= req_dn
x509_extensions		= req_x509_ext
prompt			= no

[req_dn]
CN                      = %(handle)s

[req_x509_ext]
basicConstraints	= critical,CA:true
subjectKeyIdentifier	= hash
authorityKeyIdentifier	= keyid:always
'''

def bpki_ee(e):

  if os.path.exists(bpki_ee_req_file):

    if not os.path.exists(bpki_ee_cert_file):
      subprocess.check_call(("openssl", "x509", "-req", "-sha256", "-days", "360",
                             "-CA", bpki_ca_cert_file,
                             "-CAkey", bpki_ca_key_file,
                             "-in", bpki_ee_req_file,
                             "-out", bpki_ee_cert_file, 
                             "-CAcreateserial"))

    PEMElement(e, "bpki_ee_certificate", bpki_ee_cert_file)

def extract_resources():
  pass

roas = roa_requests()
kids = children()

# format:  p/n-m asn
for pnm, asn in csv_open(roa_csv_file):
  roas.add(asn = asn, prefix = pnm)

# childname date
for handle, date in csv_open(validity_csv_file):
  kids.add(handle = handle, validity = date)

# childname p/n
for handle, pn in csv_open(prefixes_csv_file):
  kids.add(handle = handle, prefix = pn)

# childname asn
for handle, asn in csv_open(asns_csv_file):
  kids.add(handle = handle, asn = asn)

e = Element("myrpki", handle = my_handle)
roas.xml(e)
kids.xml(e)
bpki_ca(e)
bpki_ee(e)

if True:
  ElementTree(e).write(sys.stdout)
else:
  print tostring(e)