diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/rpki/config.py | 35 | ||||
-rw-r--r-- | scripts/rpki/https.py | 5 | ||||
-rw-r--r-- | scripts/rpki/x509.py | 30 |
3 files changed, 67 insertions, 3 deletions
diff --git a/scripts/rpki/config.py b/scripts/rpki/config.py new file mode 100644 index 00000000..8e15201b --- /dev/null +++ b/scripts/rpki/config.py @@ -0,0 +1,35 @@ +# $Id$ + +"""Configuration file parsing utilities. + +Layered on top of stock Python ConfigParser module. +""" + +import ConfigParser + +class parser(ConfigParser.RawConfigParser): + + def __init__(self, file=None): + super(parser, self).__init__() + if file: + self.read(file) + + def multiget(self, section, option): + """Parse OpenSSL-style foo.0, foo.1, ... subscripted options. + + Returns a list of values matching the specified option name. + """ + matches = [] + for key, value in self.items(): + name, index = key.rsplit(".", 1) + if name == option and index.isdigit(): + matches.append(tuple(int(index), value)) + matches.sort() + return [match[1] for match in matches] + + def get(self, section, option, default=None): + """Get an option, perhaps with a default value.""" + if default is None or self.has_option(section, option): + return super(parser, self).get(section, option) + else: + return default diff --git a/scripts/rpki/https.py b/scripts/rpki/https.py index 01b7849e..238c794d 100644 --- a/scripts/rpki/https.py +++ b/scripts/rpki/https.py @@ -25,9 +25,8 @@ class CertInfo(object): def __init__(self, myname=None): if myname is not None: - f = open(self.cert_dir + myname + "-EE.key", "r") - self.privateKey = tlslite.api.parsePEMKey(f.read(), private=True) - f.close() + keypair = rpki.x509.RSA_Keypair(PEM_file = self.cert_dir+myname+"-EE.key") + self.privateKey = keypair.get_tlslite() chain = rpki.x509.X509_chain() chain.load_from_PEM(glob.glob(self.cert_dir + myname + "-*.cer")) diff --git a/scripts/rpki/x509.py b/scripts/rpki/x509.py index 5f9788c9..966191a6 100644 --- a/scripts/rpki/x509.py +++ b/scripts/rpki/x509.py @@ -266,3 +266,33 @@ class PKCS10_Request(DER_object): req.fromString(self.get_DER()) self.POWpkix = req return self.POWpkix + +class RSA_Keypair(DER_object): + """Class to hold an RSA key pair. + + This may need to be split into public and private key classes. + """ + + formats = ("DER", "POW", "tlslite") + pem_converter = PEM_converter("RSA PRIVATE KEY") + + def get_DER(self): + assert not self.empty() + if self.DER: + return self.DER + if self.POW: + self.DER = self.POW.derWrite() + return self.get_DER() + raise RuntimeError + + def get_POW(self): + assert not self.empty() + if not self.POW: + self.POW = POW.derRead(POW.RSA_PRIVATE_KEY, self.get_DER()) + return self.POW + + def get_tlslite(self): + assert not self.empty() + if not self.tlslite: + self.tlslite = tlslite.api.parsePEMKey(self.get_PEM(), private=True) + return self.tlslite |