X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Ftrust%2Fcertificate.py;h=cb1d95b473867d60facb2b92ef664cdbf6d45352;hb=27e30f7854884928bd4850afa3c6ce5c7f93f7f4;hp=64ac865f38a5588d2c448882dfa400eba0fe3f06;hpb=0cf0d31c313a366e3f272f830bdb4f2a7308e11f;p=sfa.git diff --git a/sfa/trust/certificate.py b/sfa/trust/certificate.py index 64ac865f..cb1d95b4 100644 --- a/sfa/trust/certificate.py +++ b/sfa/trust/certificate.py @@ -34,23 +34,56 @@ # This module exports two classes: Keypair and Certificate. ## # -### $Id$ -### $URL$ -# +import functools import os import tempfile import base64 -import traceback from tempfile import mkstemp from OpenSSL import crypto import M2Crypto from M2Crypto import X509 -from sfa.util.sfalogging import sfa_logger -from sfa.util.namespace import urn_to_hrn -from sfa.util.faults import * +from sfa.util.faults import CertExpired, CertMissingParent, CertNotSignedByParent +from sfa.util.sfalogging import logger + +# this tends to generate quite some logs for little or no value +debug_verify_chain = False + +glo_passphrase_callback = None + +## +# A global callback may be implemented for requesting passphrases from the +# user. The function will be called with three arguments: +# +# keypair_obj: the keypair object that is calling the passphrase +# string: the string containing the private key that's being loaded +# x: unknown, appears to be 0, comes from pyOpenSSL and/or m2crypto +# +# The callback should return a string containing the passphrase. + +def set_passphrase_callback(callback_func): + global glo_passphrase_callback + + glo_passphrase_callback = callback_func + +## +# Sets a fixed passphrase. + +def set_passphrase(passphrase): + set_passphrase_callback( lambda k,s,x: passphrase ) + +## +# Check to see if a passphrase works for a particular private key string. +# Intended to be used by passphrase callbacks for input validation. + +def test_passphrase(string, passphrase): + try: + crypto.load_privatekey(crypto.FILETYPE_PEM, string, (lambda x: passphrase)) + return True + except: + return False def convert_public_key(key): keyconvert_path = "/usr/bin/keyconvert.py" @@ -59,7 +92,7 @@ def convert_public_key(key): # we can only convert rsa keys if "ssh-dss" in key: - return None + raise Exception, "keyconvert: dss keys are not supported" (ssh_f, ssh_fn) = tempfile.mkstemp() ssl_fn = tempfile.mktemp() @@ -73,20 +106,21 @@ def convert_public_key(key): # that it can be expected to see why it failed. # TODO: for production, cleanup the temporary files if not os.path.exists(ssl_fn): - return None + raise Exception, "keyconvert: generated certificate not found. keyconvert may have failed." k = Keypair() try: k.load_pubkey_from_file(ssl_fn) + return k except: - sfa_logger.log_exc("convert_public_key caught exception") - k = None - - # remove the temporary files - os.remove(ssh_fn) - os.remove(ssl_fn) - - return k + logger.log_exc("convert_public_key caught exception") + raise + finally: + # remove the temporary files + if os.path.exists(ssh_fn): + os.remove(ssh_fn) + if os.path.exists(ssl_fn): + os.remove(ssl_fn) ## # Public-private key pairs are implemented by the Keypair class. @@ -125,11 +159,13 @@ class Keypair: def save_to_file(self, filename): open(filename, 'w').write(self.as_pem()) + self.filename=filename ## # Load the private key from a file. Implicity the private key includes the public key. def load_from_file(self, filename): + self.filename=filename buffer = open(filename, 'r').read() self.load_from_string(buffer) @@ -137,8 +173,12 @@ class Keypair: # Load the private key from a string. Implicitly the private key includes the public key. def load_from_string(self, string): - self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, string) - self.m2key = M2Crypto.EVP.load_key_string(string) + if glo_passphrase_callback: + self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, string, functools.partial(glo_passphrase_callback, self, string) ) + self.m2key = M2Crypto.EVP.load_key_string(string, functools.partial(glo_passphrase_callback, self, string) ) + else: + self.key = crypto.load_privatekey(crypto.FILETYPE_PEM, string) + self.m2key = M2Crypto.EVP.load_key_string(string) ## # Load the public key from a string. No private key is loaded. @@ -161,6 +201,9 @@ class Keypair: ASN1.set_time(500) m2x509.set_not_before(ASN1) m2x509.set_not_after(ASN1) + # x509v3 so it can have extensions + # prob not necc since this cert itself is junk but still... + m2x509.set_version(2) junk_key = Keypair(create=True) m2x509.sign(pkey=junk_key.get_m2_pkey(), md="sha1") @@ -170,6 +213,7 @@ class Keypair: # get the pyopenssl pkey from the pyopenssl x509 self.key = pyx509.get_pubkey() + self.filename=filename ## # Load the public key from a string. No private key is loaded. @@ -208,7 +252,6 @@ class Keypair: def get_openssl_pkey(self): return self.key - ## # Given another Keypair object, return TRUE if the two keys are the same. @@ -230,6 +273,20 @@ class Keypair: def compute_hash(self, value): return self.sign_string(str(value)) + # only informative + def get_filename(self): + return getattr(self,'filename',None) + + def dump (self, *args, **kwargs): + print self.dump_string(*args, **kwargs) + + def dump_string (self): + result="" + result += "KEYPAIR: pubkey=%40s..."%self.get_pubkey_string() + filename=self.get_filename() + if filename: result += "Filename %s\n"%filename + return result + ## # The certificate class implements a general purpose X509 certificate, making # use of the appropriate pyOpenSSL or M2Crypto abstractions. It also adds @@ -249,22 +306,25 @@ class Certificate: issuerKey = None issuerSubject = None parent = None + isCA = None # will be a boolean once set separator="-----parent-----" ## # Create a certificate object. # + # @param lifeDays life of cert in days - default is 1825==5 years # @param create If create==True, then also create a blank X509 certificate. # @param subject If subject!=None, then create a blank certificate and set # it's subject name. # @param string If string!=None, load the certficate from the string. # @param filename If filename!=None, load the certficiate from the file. + # @param isCA If !=None, set whether this cert is for a CA - def __init__(self, create=False, subject=None, string=None, filename=None, intermediate=None): + def __init__(self, lifeDays=1825, create=False, subject=None, string=None, filename=None, isCA=None): self.data = {} if create or subject: - self.create() + self.create(lifeDays) if subject: self.set_subject(subject) if string: @@ -272,17 +332,20 @@ class Certificate: if filename: self.load_from_file(filename) - if intermediate: - self.set_intermediate_ca(intermediate) + # Set the CA bit if a value was supplied + if isCA != None: + self.set_is_ca(isCA) - ## # Create a blank X509 certificate and store it in this object. - def create(self): + def create(self, lifeDays=1825): self.cert = crypto.X509() + # FIXME: Use different serial #s self.cert.set_serial_number(3) - self.cert.gmtime_adj_notBefore(0) - self.cert.gmtime_adj_notAfter(60*60*24*365*5) # five years + self.cert.gmtime_adj_notBefore(0) # 0 means now + self.cert.gmtime_adj_notAfter(lifeDays*60*60*24) # five years is default + self.cert.set_version(2) # x509v3 so it can have extensions + ## # Given a pyOpenSSL X509 object, store that object inside of this @@ -299,11 +362,18 @@ class Certificate: # load it (support for the ---parent--- tag as well as normal chained certs) string = string.strip() - - - if not string.startswith('-----'): + + # If it's not in proper PEM format, wrap it + if string.count('-----BEGIN CERTIFICATE') == 0: string = '-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----' % string + # If there is a PEM cert in there, but there is some other text first + # such as the text of the certificate, skip the text + beg = string.find('-----BEGIN CERTIFICATE') + if beg > 0: + # skipping over non cert beginning + string = string[beg:] + parts = [] if string.count('-----BEGIN CERTIFICATE-----') > 1 and \ @@ -328,6 +398,7 @@ class Certificate: file = open(filename) string = file.read() self.load_from_string(string) + self.filename=filename ## # Save the certificate to a string. @@ -352,6 +423,7 @@ class Certificate: f = open(filename, 'w') f.write(string) f.close() + self.filename=filename ## # Save the certificate to a random file in /tmp/ @@ -409,6 +481,7 @@ class Certificate: else: setattr(subj, "CN", name) self.cert.set_subject(subj) + ## # Get the subject name of the certificate @@ -416,6 +489,13 @@ class Certificate: x = self.cert.get_subject() return getattr(x, which) + ## + # Get a pretty-print subject name of the certificate + + def get_printable_subject(self): + x = self.cert.get_subject() + return "[ OU: %s, CN: %s, SubjectAltName: %s ]" % (getattr(x, "OU"), getattr(x, "CN"), self.get_data()) + ## # Get the public key of the certificate. # @@ -437,9 +517,24 @@ class Certificate: return pkey def set_intermediate_ca(self, val): - self.intermediate = val + return self.set_is_ca(val) + + # Set whether this cert is for a CA. All signers and only signers should be CAs. + # The local member starts unset, letting us check that you only set it once + # @param val Boolean indicating whether this cert is for a CA + def set_is_ca(self, val): + if val is None: + return + + if self.isCA != None: + # Can't double set properties + raise Exception, "Cannot set basicConstraints CA:?? more than once. Was %s, trying to set as %s" % (self.isCA, val) + + self.isCA = val if val: self.add_extension('basicConstraints', 1, 'CA:TRUE') + else: + self.add_extension('basicConstraints', 1, 'CA:FALSE') @@ -452,6 +547,25 @@ class Certificate: # @param value string containing value of the extension def add_extension(self, name, critical, value): + oldExtVal = None + try: + oldExtVal = self.get_extension(name) + except: + # M2Crypto LookupError when the extension isn't there (yet) + pass + + # This code limits you from adding the extension with the same value + # The method comment says you shouldn't do this with the same name + # But actually it (m2crypto) appears to allow you to do this. + if oldExtVal and oldExtVal == value: + # don't add this extension again + # just do nothing as here + return + # FIXME: What if they are trying to set with a different value? + # Is this ever OK? Or should we raise an exception? +# elif oldExtVal: +# raise "Cannot add extension %s which had val %s with new val %s" % (name, oldExtVal, value) + ext = crypto.X509Extension (name, critical, value) self.cert.add_extensions([ext]) @@ -459,9 +573,11 @@ class Certificate: # Get an X509 extension from the certificate def get_extension(self, name): + # pyOpenSSL does not have a way to get extensions m2x509 = X509.load_cert_string(self.save_to_string()) value = m2x509.get_ext(name).get_value() + return value ## @@ -496,6 +612,7 @@ class Certificate: # Sign the certificate using the issuer private key and issuer subject previous set with set_issuer(). def sign(self): + logger.debug('certificate.sign') assert self.cert != None assert self.issuerSubject != None assert self.issuerKey != None @@ -569,6 +686,7 @@ class Certificate: # child. If a parent did not sign a child, then an exception is thrown. If # the bottom of the recursion is reached and the certificate does not match # a trusted root, then an exception is thrown. + # Also require that parents are CAs. # # @param Trusted_certs is a list of certificates that are trusted. # @@ -580,29 +698,106 @@ class Certificate: # verify expiration time if self.cert.has_expired(): - raise CertExpired(self.get_subject(), "client cert") - + if debug_verify_chain: + logger.debug("verify_chain: NO, Certificate %s has expired" % self.get_printable_subject()) + raise CertExpired(self.get_printable_subject(), "client cert") + # if this cert is signed by a trusted_cert, then we are set for trusted_cert in trusted_certs: if self.is_signed_by_cert(trusted_cert): - sfa_logger.debug("Cert %s signed by trusted cert %s", self.get_subject(), trusted_cert.get_subject()) # verify expiration of trusted_cert ? if not trusted_cert.cert.has_expired(): + if debug_verify_chain: + logger.debug("verify_chain: YES. Cert %s signed by trusted cert %s"%( + self.get_printable_subject(), trusted_cert.get_printable_subject())) return trusted_cert else: - sfa_logger.debug("Trusted cert %s is expired", trusted_cert.get_subject()) + if debug_verify_chain: + logger.debug("verify_chain: NO. Cert %s is signed by trusted_cert %s, but that signer is expired..."%( + self.get_printable_subject(),trusted_cert.get_printable_subject())) + raise CertExpired(self.get_printable_subject()," signer trusted_cert %s"%trusted_cert.get_printable_subject()) # if there is no parent, then no way to verify the chain if not self.parent: - sfa_logger.debug("%r has no parent"%self.get_subject()) - raise CertMissingParent(self.get_subject()) + if debug_verify_chain: + logger.debug("verify_chain: NO. %s has no parent and issuer %s is not in %d trusted roots"%\ + (self.get_printable_subject(), self.get_issuer(), len(trusted_certs))) + raise CertMissingParent(self.get_printable_subject() + \ + ": Issuer %s is not one of the %d trusted roots, and cert has no parent." %\ + (self.get_issuer(), len(trusted_certs))) # if it wasn't signed by the parent... if not self.is_signed_by_cert(self.parent): - sfa_logger.debug("%r is not signed by parent"%self.get_subject()) - return CertNotSignedByParent(self.get_subject()) + if debug_verify_chain: + logger.debug("verify_chain: NO. %s is not signed by parent %s, but by %s"%\ + (self.get_printable_subject(), + self.parent.get_printable_subject(), + self.get_issuer())) + raise CertNotSignedByParent("%s: Parent %s, issuer %s"\ + % (self.get_printable_subject(), + self.parent.get_printable_subject(), + self.get_issuer())) + + # Confirm that the parent is a CA. Only CAs can be trusted as + # signers. + # Note that trusted roots are not parents, so don't need to be + # CAs. + # Ugly - cert objects aren't parsed so we need to read the + # extension and hope there are no other basicConstraints + if not self.parent.isCA and not (self.parent.get_extension('basicConstraints') == 'CA:TRUE'): + logger.warn("verify_chain: cert %s's parent %s is not a CA" % \ + (self.get_printable_subject(), self.parent.get_printable_subject())) + raise CertNotSignedByParent("%s: Parent %s not a CA" % (self.get_printable_subject(), + self.parent.get_printable_subject())) # if the parent isn't verified... + if debug_verify_chain: + logger.debug("verify_chain: .. %s, -> verifying parent %s"%\ + (self.get_printable_subject(),self.parent.get_printable_subject())) self.parent.verify_chain(trusted_certs) return + + ### more introspection + def get_extensions(self): + # pyOpenSSL does not have a way to get extensions + triples = [] + m2x509 = X509.load_cert_string(self.save_to_string()) + nb_extensions = m2x509.get_ext_count() + logger.debug("X509 had %d extensions"%nb_extensions) + for i in range(nb_extensions): + ext=m2x509.get_ext_at(i) + triples.append( (ext.get_name(), ext.get_value(), ext.get_critical(),) ) + return triples + + def get_data_names(self): + return self.data.keys() + + def get_all_datas (self): + triples = self.get_extensions() + for name in self.get_data_names(): + triples.append( (name,self.get_data(name),'data',) ) + return triples + + # only informative + def get_filename(self): + return getattr(self,'filename',None) + + def dump (self, *args, **kwargs): + print self.dump_string(*args, **kwargs) + + def dump_string (self,show_extensions=False): + result = "" + result += "CERTIFICATE for %s\n"%self.get_printable_subject() + result += "Issued by %s\n"%self.get_issuer() + filename=self.get_filename() + if filename: result += "Filename %s\n"%filename + if show_extensions: + all_datas = self.get_all_datas() + result += " has %d extensions/data attached"%len(all_datas) + for (n, v, c) in all_datas: + if c=='data': + result += " data: %s=%s\n"%(n,v) + else: + result += " ext: %s (crit=%s)=<<<%s>>>\n"%(n,c,v) + return result