Skip to content

Commit 9eea189

Browse files
author
pjkersha
committed
* Added support for key file pass-phrase for make_ssl_context func in 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
1 parent fea79af commit 9eea189

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

ndg/httpsclient/ssl_context_util.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def make_ssl_context_from_config(ssl_config=False, url=None):
3636

3737

3838
def make_ssl_context(key_file=None, cert_file=None, pem_file=None, ca_dir=None,
39-
verify_peer=False, url=None, method=SSL.SSLv23_METHOD):
39+
verify_peer=False, url=None, method=SSL.SSLv23_METHOD,
40+
key_file_passphrase=None):
4041
"""
4142
Creates SSL context containing certificate and key file locations.
4243
"""
@@ -45,11 +46,16 @@ def make_ssl_context(key_file=None, cert_file=None, pem_file=None, ca_dir=None,
4546
# Key file defaults to certificate file if present.
4647
if cert_file:
4748
ssl_context.use_certificate_file(cert_file)
49+
50+
if key_file_passphrase:
51+
passwd_cb = lambda max_passphrase_len, set_prompt, userdata: \
52+
key_file_passphrase
53+
ssl_context.set_passwd_cb(passwd_cb)
54+
4855
if key_file:
4956
ssl_context.use_privatekey_file(key_file)
50-
else:
51-
if cert_file:
52-
ssl_context.use_privatekey_file(cert_file)
57+
elif cert_file:
58+
ssl_context.use_privatekey_file(cert_file)
5359

5460
if pem_file or ca_dir:
5561
ssl_context.load_verify_locations(pem_file, ca_dir)
@@ -70,6 +76,7 @@ def _callback(conn, x509, errnum, errdepth, preverify_ok):
7076
ssl_context.set_verify(SSL.VERIFY_PEER, verify_callback)
7177
else:
7278
ssl_context.set_verify(SSL.VERIFY_NONE, verify_callback)
79+
7380
return ssl_context
7481

7582

ndg/httpsclient/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def fetch_stream_from_url(url, config, data=None, handlers=None):
109109
@type url: basestring
110110
@param config: SSL context configuration
111111
@type config: Configuration
112+
@param data: HTTP POST data
113+
@type data: str
114+
@param handlers: list of custom urllib2 handlers to add to the request
115+
@type handlers: iterable
112116
@return: data retrieved from URL or None
113117
@rtype: file derived type
114118
"""
@@ -141,6 +145,7 @@ def open_url(url, config, data=None, handlers=None):
141145
cj = config.cookie
142146
else:
143147
cj = cookielib.CookieJar()
148+
144149
# Use a cookie processor that accumulates cookies when redirects occur so
145150
# that an application can redirect for authentication and retain both any
146151
# cookies for the application and the security system (c.f.,

0 commit comments

Comments
 (0)