aboutsummaryrefslogtreecommitdiff
path: root/myrpki/xml-parse-test.py
blob: 27c2c0cfc20941dc17e7b96c9ae8cc3c6877b47c (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
# $Id$

import lxml.etree, rpki.resource_set, base64, subprocess

rng = lxml.etree.RelaxNG(lxml.etree.parse("myrpki.rng"))

tree = lxml.etree.parse("myrpki.xml").getroot()

if False:
  print lxml.etree.tostring(tree, pretty_print = True, encoding = "us-ascii", xml_declaration = True)

rng.assertValid(tree)

def showitems(x):
  if False:
    for k, v in x.items():
      if v:
        print " ", k, v

def tag(t):
  return "{http://www.hactrn.net/uris/rpki/myrpki/}" + t

print "My handle:", tree.get("handle")

print "Children:"
for x in tree.getiterator(tag("child")):
  print " ", x
  print "  Handle:", x.get("handle")
  print "  ASNS:  ", rpki.resource_set.resource_set_as(x.get("asns"))
  print "  IPv4:  ", rpki.resource_set.resource_set_ipv4(x.get("v4"))
  print "  Valid: ", x.get("valid_until")
  showitems(x)
print

print "ROA requests:"
for x in tree.getiterator(tag("roa_request")):
  print " ", x
  print "  ASN: ", x.get("asn")
  print "  IPv4:", rpki.resource_set.roa_prefix_set_ipv4(x.get("v4"))
  print "  IPv6:", rpki.resource_set.roa_prefix_set_ipv6(x.get("v6"))
  showitems(x)
print

def showpem(label, b64, kind):
  cmd = ("openssl", kind, "-noout", "-text", "-inform", "DER")
  p = subprocess.Popen(cmd, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
  text = p.communicate(input = base64.b64decode(b64))[0]
  if p.returncode != 0:
    raise subprocess.CalledProcessError(returncode = p.returncode, cmd = cmd)
  print label, text

ca = tree.findtext(tag("bpki_ca_certificate"))
if ca:
  showpem("CA", ca, "x509")

ee = tree.findtext(tag("bpki_ee_certificate"))
if ee:
  showpem("EE", ee, "x509")

crl = tree.findtext(tag("bpki_crl"))
if crl:
  showpem("CA", crl, "crl")