Skip to content

Commit

Permalink
* Added support for key file pass-phrase for make_ssl_context func i…
Browse files Browse the repository at this point in the history
…n ndg.httpsclient.ssl_context_util

git-svn-id: http://proj.badc.rl.ac.uk/svn/ndg-security/trunk/ndg_httpsclient@8258 051b1e3e-aa0c-0410-b6c2-bfbade6052be
  • Loading branch information
pjkersha committed Nov 13, 2012
1 parent fea79af commit 9eea189
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ndg/httpsclient/ssl_context_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def make_ssl_context_from_config(ssl_config=False, url=None):


def make_ssl_context(key_file=None, cert_file=None, pem_file=None, ca_dir=None,
verify_peer=False, url=None, method=SSL.SSLv23_METHOD):
verify_peer=False, url=None, method=SSL.SSLv23_METHOD,
key_file_passphrase=None):
"""
Creates SSL context containing certificate and key file locations.
"""
Expand All @@ -45,11 +46,16 @@ def make_ssl_context(key_file=None, cert_file=None, pem_file=None, ca_dir=None,
# Key file defaults to certificate file if present.
if cert_file:
ssl_context.use_certificate_file(cert_file)

if key_file_passphrase:
passwd_cb = lambda max_passphrase_len, set_prompt, userdata: \
key_file_passphrase
ssl_context.set_passwd_cb(passwd_cb)

if key_file:
ssl_context.use_privatekey_file(key_file)
else:
if cert_file:
ssl_context.use_privatekey_file(cert_file)
elif cert_file:
ssl_context.use_privatekey_file(cert_file)

if pem_file or ca_dir:
ssl_context.load_verify_locations(pem_file, ca_dir)
Expand All @@ -70,6 +76,7 @@ def _callback(conn, x509, errnum, errdepth, preverify_ok):
ssl_context.set_verify(SSL.VERIFY_PEER, verify_callback)
else:
ssl_context.set_verify(SSL.VERIFY_NONE, verify_callback)

return ssl_context


Expand Down
5 changes: 5 additions & 0 deletions ndg/httpsclient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def fetch_stream_from_url(url, config, data=None, handlers=None):
@type url: basestring
@param config: SSL context configuration
@type config: Configuration
@param data: HTTP POST data
@type data: str
@param handlers: list of custom urllib2 handlers to add to the request
@type handlers: iterable
@return: data retrieved from URL or None
@rtype: file derived type
"""
Expand Down Expand Up @@ -141,6 +145,7 @@ def open_url(url, config, data=None, handlers=None):
cj = config.cookie
else:
cj = cookielib.CookieJar()

# Use a cookie processor that accumulates cookies when redirects occur so
# that an application can redirect for authentication and retain both any
# cookies for the application and the security system (c.f.,
Expand Down

0 comments on commit 9eea189

Please sign in to comment.