#!/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. """ Copies an authenticated result tree from an rcynic run into the format expected by most OpenSSL-based programs: a collection of "PEM" format files with names in the form that OpenSSL's -CApath lookup routines expect. This can be useful for validating RPKI objects which are not distributed as part of the repository system. """ import os import sys import rpki.config from rpki.rcynicdb.iterator import authenticated_objects def check_dir(s): if os.path.isdir(s): return os.path.abspath(s) else: raise argparse.ArgumentTypeError("{!r} is not a directory".format(s)) cfg = rpki.config.argparser(doc = __doc__) cfg.argparser.add_argument("-v", "--verbose", action = "store_true", help = "whistle while you work") cfg.argparser.add_argument("rcynic_dir", nargs = "?", type = check_dir, help = "rcynic authenticated output directory") cfg.argparser.add_argument("output_dir", help = "name of output directory to create") args = cfg.argparser.parse_args() if not os.path.isdir(args.output_dir): os.makedirs(args.output_dir) def store(uri, obj, fmt): for i in xrange(1000000): fn = os.path.join(args.output_dir, fmt.format(i)) if os.path.exists(fn): continue with open(fn, "w") as f: f.write(obj.pemWrite()) if args.verbose: print fn, "<=", uri return else: sys.exit("No path name available for {} ({})".format(uri, fn)) for uri, cer in authenticated_objects(uri_suffix = ".cer"): store(uri, cer, "{:08x}.{{:d}}".format(cer.getSubjectHash())) for uri, crl in authenticated_objects(uri_suffix = ".crl"): store(uri, crl, "{:08x}.r{{:d}}".format(crl.getIssuerHash()))