Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Authors: Rob Crittenden <rcritten@redhat.com> # John Dennis <jdennis@redhat.com> # # Copyright (C) 2009 Red Hat # see file 'COPYING' for use and warranty information # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. #
check_sig, is_server, str(cert))
# Define how the cert is being used based upon the is_server flag. This may # seem backwards, but isn't. If we're a server we're trying to validate a # client cert. If we're a client we're trying to validate a server cert. intended_usage = nss.certificateUsageSSLClient else:
# If the cert fails validation it will raise an exception, the errno attribute # will be set to the error code matching the reason why the validation failed # and the strerror attribute will contain a string describing the reason. except Exception, e: root_logger.error('cert validation failed for "%s" (%s)', cert.subject, e.strerror) cert_is_valid = False return cert_is_valid
', '.join(nss.cert_usage_flags(approved_usage)), ', '.join(nss.cert_usage_flags(intended_usage)))
# Is the intended usage a proper subset of the approved usage else: cert_is_valid = False
# If this is a server, we're finished root_logger.debug('cert valid %s for "%s"', cert_is_valid, cert.subject) return cert_is_valid
# Certificate is OK. Since this is the client side of an SSL # connection, we need to verify that the name field in the cert # matches the desired hostname. This is our defense against # man-in-the-middle attacks.
# If the cert fails validation it will raise an exception except Exception, e: root_logger.error('failed verifying socket hostname "%s" matches cert subject "%s" (%s)', hostname, cert.subject, e.strerror) cert_is_valid = False return cert_is_valid
except NSPRError: return False else: nicknames = nss.get_cert_nicknames(certdb, nss.SEC_CERT_NICKNAMES_USER) for nickname in nicknames: try: cert = nss.find_cert_from_nickname(nickname, password) if cert.check_valid_times(): if cert.has_signer_in_ca_names(ca_names): priv_key = nss.find_key_by_any_cert(cert, password) return cert, priv_key except NSPRError: return False return False
socket.AF_INET: io.PR_AF_INET, socket.AF_INET6: io.PR_AF_INET6, socket.AF_UNSPEC: io.PR_AF_UNSPEC }
""" Translate a family from python socket module to nss family. """ except KeyError: raise ValueError('Uknown socket family %d\n', sock_family)
self.sock = io.Socket(family=self.family)
except Exception: raise NSPRError(error.PR_ADDRESS_NOT_SUPPORTED_ERROR, "Cannot resolve %s using family %s" % (host, io.addr_family_name(self.family)))
except Exception, e: root_logger.debug("Could not connect socket to %s, error: %s", net_addr, str(e)) root_logger.debug("Try to continue with next family...") continue
raise NSPRError(error.PR_ADDRESS_NOT_SUPPORTED_ERROR, "Could not connect to %s using any address" % host)
dbdir=None, family=socket.AF_UNSPEC, no_init=False): """ :param host: the server to connect to :param port: the port to use (default is set in HTTPConnection) :param dbdir: the NSS database directory :param family: network family to use (default AF_UNSPEC) :param no_init: do not initialize the NSS database. This requires that the database has already been initialized or the request will fail. """
raise RuntimeError("dbdir is required")
# close any open NSS database and use the new one raise e
# TODO: remove the try block once python-nss is guaranteed to contain # these values #pylint: disable=E1101 ssl_require_safe_negotiation = ssl.SSL_REQUIRE_SAFE_NEGOTIATION ssl_renegotiate_requires_xtn = ssl.SSL_RENEGOTIATE_REQUIRES_XTN
# Create the socket here so we can do things like let the caller # override the NSS callbacks # Provide a callback which notifies us when the SSL handshake is complete
# Provide a callback to verify the servers certificate nss.get_default_certdb())
return getpass.getpass("Enter password for %s: " % slot.token_name);
""" Verify callback. If we get here then the certificate is ok. """
"""Close the connection to the HTTP server."""
""" Explicitly close the connection if an error is returned after the headers are sent. This will likely mean the initial SSL handshake failed. If this isn't done then the connection is never closed and subsequent NSS activities will fail with a BUSY error. """ # FIXME: httplib uses old-style classes so super doesn't work # Python 2.7 changed the API for endheaders. This is an attempt # to work across versions httplib.HTTPConnection.endheaders(self) else: except NSPRError, e: self.close() raise e
# We would like to use HTTP 1.1 not the older HTTP 1.0 but xmlrpclib # and httplib do not play well together. httplib when the protocol # is 1.1 will add a host header in the request. But xmlrpclib # always adds a host header irregardless of the HTTP protocol # version. That means the request ends up with 2 host headers, # but Apache freaks out if it sees 2 host headers, a known Apache # issue. httplib has a mechanism to skip adding the host header # (i.e. skip_host in HTTPConnection.putrequest()) but xmlrpclib # doesn't use it. Oh well, back to 1.0 :-( # #_http_vsn = 11 #_http_vsn_str = 'HTTP/1.1'
# provide a default host, pass the X509 cert info
# urf. compensate for bad input. if port == 0: port = None self._setup(self._connection_class(host, port, strict, dbdir=dbdir, no_init=no_init))
""" Override so we can close duplicated file connection on non-200 responses. This was causing nss_shutdown() to fail with a busy error. """ (status, reason, msg) = httplib.HTTP.getreply(self) if status != 200: self.file.close() return (status, reason, msg)
#------------------------------------------------------------------------------
standard_logging_setup('nsslib.log', debug=True, filemode='a') root_logger.info("Start")
if False: conn = NSSConnection("www.verisign.com", 443, dbdir="/etc/pki/nssdb") conn.set_debuglevel(1) conn.connect() conn.request("GET", "/") response = conn.getresponse() print response.status #print response.msg print response.getheaders() data = response.read() #print data conn.close()
if True: h = NSSHTTPS("www.verisign.com", 443, dbdir="/etc/pki/nssdb") h.connect() h.putrequest('GET', '/') h.endheaders() http_status, http_reason, headers = h.getreply() print "status = %s %s" % (http_status, http_reason) print "headers:\n%s" % headers f = h.getfile() data = f.read() # Get the raw HTML f.close() #print data |