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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
"""
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 certificate and BPKI EE certificate (cannot do latter
without corresponding BPKI EE PKCS #10). Whack all this together and
generate some XML thing (format still in flux, see schema).
$Id$
Copyright (C) 2009 Internet Systems Consortium ("ISC")
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
import subprocess, csv, re, os, getopt, sys, ConfigParser
from xml.etree.ElementTree import Element, SubElement, ElementTree
namespace = "http://www.hactrn.net/uris/rpki/myrpki/"
class comma_set(set):
def __str__(self):
return ",".join(self)
class roa_request(object):
v4re = re.compile("^([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]+(-[0-9]+)?$", re.I)
v6re = re.compile("^([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}/[0-9]+(-[0-9]+)?$", re.I)
def __init__(self, asn):
self.asn = asn
self.v4 = comma_set()
self.v6 = comma_set()
def add(self, prefix):
if self.v4re.match(prefix):
self.v4.add(prefix)
elif self.v6re.match(prefix):
self.v6.add(prefix)
else:
raise RuntimeError, 'Bad prefix syntax: "%s"' % prefix
def xml(self, e):
return SubElement(e, "roa_request",
asn = self.asn,
v4 = str(self.v4),
v6 = str(self.v6))
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)
@classmethod
def from_csv(cls, roa_csv_file):
self = cls()
# format: p/n-m asn
for pnm, asn in csv_open(roa_csv_file):
self.add(asn = asn, prefix = pnm)
return self
class child(object):
v4re = re.compile("^(([0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]+)|(([0-9]{1,3}\.){3}[0-9]{1,3}-([0-9]{1,3}\.){3}[0-9]{1,3})$", re.I)
v6re = re.compile("^(([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}/[0-9]+)|(([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4}-([0-9a-f]{0,4}:){0,15}[0-9a-f]{0,4})$", re.I)
def __init__(self, handle):
self.handle = handle
self.asns = comma_set()
self.v4 = comma_set()
self.v6 = comma_set()
self.validity = None
def add(self, prefix = None, asn = None, validity = None):
if prefix is not None:
if self.v4re.match(prefix):
self.v4.add(prefix)
elif self.v6re.match(prefix):
self.v6.add(prefix)
else:
raise RuntimeError, 'Bad prefix syntax: "%s"' % 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),
v4 = str(self.v4),
v6 = str(self.v6))
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)
@classmethod
def from_csv(cls, validity_csv_file, prefix_csv_file, asn_csv_file):
self = cls()
# childname date
for handle, date in csv_open(validity_csv_file):
self.add(handle = handle, validity = date)
# childname p/n
for handle, pn in csv_open(prefix_csv_file):
self.add(handle = handle, prefix = pn)
# childname asn
for handle, asn in csv_open(asn_csv_file):
self.add(handle = handle, asn = asn)
return self
def csv_open(filename, delimiter = "\t", dialect = None):
return csv.reader(open(filename, "rb"), dialect = dialect, delimiter = delimiter)
def PEMElement(e, tag, filename):
e = SubElement(e, tag)
e.text = "".join(p.strip() for p in open(filename).readlines()[1:-1])
def bpki_ca(e, bpki_ca_key_file, bpki_ca_cert_file, cfg_file):
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_cert_file):
subprocess.check_call(("openssl", "req", "-new", "-sha256", "-x509", "-verbose",
"-config", cfg_file,
"-extensions", "req_x509_ext",
"-key", bpki_ca_key_file,
"-out", bpki_ca_cert_file))
PEMElement(e, "bpki_ca_certificate", bpki_ca_cert_file)
def bpki_ee(e, bpki_ee_req_file, bpki_ee_cert_file, bpki_ca_cert_file, bpki_ca_key_file):
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 bpki_crl(e, bpki_crl_file, bpki_index_file, cfg_file):
if not os.path.exists(bpki_crl_file):
if not os.path.exists(bpki_index_file):
open(bpki_index_file, "w").close()
subprocess.check_call(("openssl", "ca", "-batch", "-verbose", "-gencrl",
"-out", bpki_crl_file,
"-config", cfg_file))
PEMElement(e, "bpki_crl", bpki_crl_file)
def extract_resources():
pass
def main():
cfg_file = "myrpki.conf"
myrpki_section = "myrpki"
opts, argv = getopt.getopt(sys.argv[1:], "c:h:?", ["config=", "help"])
for o, a in opts:
if o in ("-h", "--help", "-?"):
print __doc__
sys.exit(0)
elif o in ("-c", "--config"):
cfg_file = a
if argv:
raise RuntimeError, "Unexpected arguments %s" % argv
cfg = ConfigParser.RawConfigParser()
cfg.read(cfg_file)
my_handle = cfg.get(myrpki_section, "handle")
roa_csv_file = cfg.get(myrpki_section, "roa_csv")
validity_csv_file = cfg.get(myrpki_section, "validity_csv")
prefix_csv_file = cfg.get(myrpki_section, "prefix_csv")
asn_csv_file = cfg.get(myrpki_section, "asn_csv")
bpki_ca_cert_file = cfg.get(myrpki_section, "bpki_ca_certificate")
bpki_ca_key_file = cfg.get(myrpki_section, "bpki_ca_key")
bpki_ee_cert_file = cfg.get(myrpki_section, "bpki_ee_certificate")
bpki_ee_req_file = cfg.get(myrpki_section, "bpki_ee_pkcs10")
bpki_crl_file = cfg.get(myrpki_section, "bpki_crl")
bpki_index_file = cfg.get(myrpki_section, "bpki_index")
output_filename = cfg.get(myrpki_section, "output_filename")
relaxng_schema = cfg.get(myrpki_section, "relaxng_schema")
roas = roa_requests.from_csv(roa_csv_file)
kids = children.from_csv(validity_csv_file, prefix_csv_file, asn_csv_file)
e = Element("myrpki", xmlns = namespace, version = "1", handle = my_handle)
roas.xml(e)
kids.xml(e)
bpki_ca(e,
bpki_ca_key_file = bpki_ca_key_file,
bpki_ca_cert_file = bpki_ca_cert_file,
cfg_file = cfg_file)
bpki_ee(e,
bpki_ee_req_file = bpki_ee_req_file,
bpki_ee_cert_file = bpki_ee_cert_file,
bpki_ca_cert_file = bpki_ca_cert_file,
bpki_ca_key_file = bpki_ca_key_file)
bpki_crl(e,
bpki_crl_file = bpki_crl_file,
bpki_index_file = bpki_index_file,
cfg_file = cfg_file)
ElementTree(e).write(output_filename + ".tmp")
os.rename(output_filename + ".tmp", output_filename)
if __name__ == "__main__":
main()
|