aboutsummaryrefslogtreecommitdiff
path: root/rp/utils/scan_routercerts
blob: 74cd2b6900b33eddc8b89ef134362a9fb23d2d7e (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
#!/usr/bin/env python
# $Id$
#
# Copyright (C) 2014 Dragon Research Labs ("DRL")
#
# Permission to use, copy, modify, and/or 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 DRL DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL DRL 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.

"""
Scan rcynic validated output looking for router certificates, print
out stuff that the rpki-rtr code cares about.
"""

import os
import sys
import base64
import argparse
import rpki.POW
import rpki.oids

from rpki.rcynicdb.iterator import authenticated_objects

def check_dir(s):
    if not os.path.isdir(s):
        raise argparse.ArgumentTypeError("{!r} is not a directory".format(s))
    return s

parser = argparse.ArgumentParser(description = __doc__)
parser.add_argument("rcynic_dir", nargs = "?", type = check_dir,
                    help = "rcynic authenticated output directory")
args = parser.parse_args()

for uri, cer in authenticated_objects(args.rcynic_dir, uri_suffix = ".cer"):

    if rpki.oids.id_kp_bgpsec_router not in (cer.getEKU() or ()):
        continue

    sys.stdout.write(base64.urlsafe_b64encode(cer.getSKI()).rstrip("="))

    for min_asn, max_asn in cer.getRFC3779()[0]:
        for asn in xrange(min_asn, max_asn + 1):
            sys.stdout.write(" {}".format(asn))

    sys.stdout.write(" {}\n".format(base64.b64encode(cer.getPublicKey().derWritePublic())))