aboutsummaryrefslogtreecommitdiff
path: root/rpkid/myrpki.py
blob: 4598fd68177459d37f00c5661d737dad905d80e1 (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
# $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, xml.etree.ElementTree

# 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"

class roa_request(object):

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

  requests = {}

  @classmethod
  def add(cls, asn, prefix):
    if asn not in cls.requests:
      cls.requests[asn] = cls(asn)
    cls.requests[asn].prefixes.add(prefix)

  def __str__(self):
    return self.asn + " " + ",".join(self.prefixes)

  @classmethod
  def show(cls):
    for r in cls.requests.itervalues():
      print r

class child(object):

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

  children = {}

  @classmethod
  def add(cls, handle, prefix = None, asn = None, validity = None):
    if handle not in cls.children:
      cls.children[handle] = cls(handle)
    if prefix is not None:
      cls.children[handle].prefixes.add(prefix)
    if asn is not None:
      cls.children[handle].asns.add(asn)
    if validity is not None:
      cls.children[handle].validity = validity

  def __str__(self):
    return "%s %s %s %s" % (self.handle, self.validity,
                            ",".join(self.asns),
                            ",".join(self.prefixes))

  @classmethod
  def show(cls):
    for c in cls.children.itervalues():
      print c

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

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

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

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

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

roa_request.show()

child.show()

# rest of this is yesterday's code that hasn't been converted yet

# modes: --create-bpki, --issue-bsc, --extract-resources

def create_bpki():
  name = my_handle + "bpki-ca"

  subprocess.check_call(("openssl", "genrsa", "-out", name + ".key", "2048"))

  subprocess.check_call(("openssl", "req", "-new", "-sha256",
              "-key", name + ".key", "-out", name + ".req"))

  subprocess.check_call(("openssl", "x509",  "-req",  "-sha256",
                         "-in", name + ".req", "-out", name + ".cer",
                         "-signkey", name + ".key", "-days", "360"))

  yaml_cert_out(name + ".cer", "bpki_ca")

def issue_bsc():

  ca_name = my_handle + "bpki-ca"
  ee_name = my_handle + "bpki-ee"

  subprocess.check_call(("openssl", "x509", "-req", "-sha256", "-days", "360",
                         "-CA", ca_name + ".cer", "-CAkey", ca_name + ".key",
                         "-in", ee_name + ".req", "-out", ee_name + ".cer", 
                         "-CAcreateserial"))

  yaml_cert_out(ee_name + ".cer", "bpki_ee")

def extract_resources():
  pass