From: Menno Smits Date: Tue, 28 Jul 2015 10:48:01 +0000 (+1200) Subject: Documentation updates for TLS support X-Git-Tag: 1.0.0~1^2~51 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=805e79c59ce3f131fe47384de9be777c85ae31f8;p=imapclient.git Documentation updates for TLS support --- diff --git a/NEWS.rst b/NEWS.rst index 9830000..8961337 100644 --- a/NEWS.rst +++ b/NEWS.rst @@ -2,9 +2,26 @@ 1.0.0 ======= -XXX TLS support -XXX Removed kwargs support from constructor - replaced by ssl_context -XXX livetest config file change re DEFAULT +Enhanced TLS support +-------------------- +The way that IMAPClient establishes TLS/SSL connections has been +completed reworked. By default IMAPClient will attempt certificate +verification, certificate hostname checking, and will not use +known-insecure TLS settings and protocols. In addition, TLS parameters +are now highly configurable. + +XXX new dependencies +XXX compatibility: dropped Python 3.2 support +XXX compatibility: Removed kwargs support from constructor - replaced by ssl_context +XXX compatibility: default is to do cert checks (pointer to docs on how to disable) + +XXX Thank people who contributed + +STARTTLS support +---------------- +XXX + +XXX Thank people who contributed (Chris?) Performance optimsation for parsing message id lists ---------------------------------------------------- @@ -15,8 +32,6 @@ Other ----- * Perform quoting of Gmail labels. Thanks to Pawel Sz for the fix. - - ====== 0.13 ====== diff --git a/doc/src/index.rst b/doc/src/index.rst index c6e4fc4..c4c8178 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -107,12 +107,12 @@ clients to keep track of data related to a message. The IMAPClient package has constants for a number of commmonly used flags:: - DELETED = r'\Deleted' - SEEN = r'\Seen' - ANSWERED = r'\Answered' - FLAGGED = r'\Flagged' - DRAFT = r'\Draft' - RECENT = r'\Recent' # This flag is read-only + DELETED = br'\Deleted' + SEEN = br'\Seen' + ANSWERED = br'\Answered' + FLAGGED = br'\Flagged' + DRAFT = br'\Draft' + RECENT = br'\Recent' # This flag is read-only Any method that accepts message flags takes either a sequence containing message flags (eg. ``[DELETED, 'foo', 'Bar']``) or a single @@ -148,6 +148,44 @@ The IMAP related exceptions that will be raised by this class are: These are aliases for the imaplib.IMAP4 exceptions of the same name. Socket errors may also be raised in the case of network errors. +TLS/SSL +~~~~~~~ +IMAPClient uses sensible TLS parameter defaults for encrypted +connections and also allows for a high level of control of TLS +parameters if required. To provide a consistent API and capabilities +across Python versions the `backports.ssl `_ +library is used instead of the standard library ssl +package. backports.ssl provides an API that aims to mimic the Python +3.4 ssl package so it should be familiar to developers that have used +the ssl package in recent versions of Python. + +TLS parameters are controlled by passing a ``backports.ssl.SSLContext`` +when creating an IMAPClient instance. When ``ssl=True`` is used +without passing a SSLContext, a default context is used. The default +context avoids the use of known insecure ciphers and SSL protocol +versions, with certificate verification and hostname verification +turned on. The default context will use system installed certificate +authority trust chains, if available. + +:py:func:`IMAPClient.tls.create_default_context` returns IMAPClient's +default context. When constructing a custom context it is usually best +to start with the default context and modify it to suit your needs. + +The following example shows how to to disable certification +verification and certificate host name checks if required. + +.. literalinclude:: ../../imapclient/examples/tls_no_checks.py + +The next example shows how to create a context that will use custom CA +certificate. This is required to perform verification of a self-signed +certificate used by the IMAP server. + +.. literalinclude:: ../../imapclient/examples/tls_cacert.py + +The above examples show some of the most common TLS parameter +customisations but there are many other tweaks are possible. Consult +the Python 3 :py:mod:`ssl` package documentation for further options. + API Reference ------------- @@ -169,6 +207,12 @@ are encountered during parsing. .. automodule:: imapclient.response_types :members: +TLS Support +~~~~~~~~~~~ + +.. automodule:: imapclient.tls + :members: + Interactive Sessions -------------------- When developing program using IMAPClient is it sometimes useful to diff --git a/imapclient/examples/tls_cacert.py b/imapclient/examples/tls_cacert.py new file mode 100644 index 0000000..a67a7f5 --- /dev/null +++ b/imapclient/examples/tls_cacert.py @@ -0,0 +1,16 @@ +# Copyright (c) 2015, Menno Smits +# Released subject to the New BSD License +# Please see http://en.wikipedia.org/wiki/BSD_licenses + +from __future__ import unicode_literals +import imapclient + +HOST = 'imap.host.com' +USERNAME = 'someuser' +PASSWORD = 'secret' + +context = imapclient.create_default_context(cafile="/path/to/cacert.pem") + +server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context) +server.login(USERNAME, PASSWORD) +# ... diff --git a/imapclient/examples/tls_no_checks.py b/imapclient/examples/tls_no_checks.py new file mode 100644 index 0000000..7c2e5df --- /dev/null +++ b/imapclient/examples/tls_no_checks.py @@ -0,0 +1,23 @@ +# Copyright (c) 2015, Menno Smits +# Released subject to the New BSD License +# Please see http://en.wikipedia.org/wiki/BSD_licenses + +from __future__ import unicode_literals +import imapclient +from backports import ssl + +HOST = 'imap.host.com' +USERNAME = 'someuser' +PASSWORD = 'secret' + +context = imapclient.create_default_context() + +# don't check if certificate hostname doesn't match target hostname +context.check_hostname = False + +# don't check if the certificate is trusted by a certificate authority +context.verify_mode = ssl.CERT_NONE + +server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context) +server.login(USERNAME, PASSWORD) +# ... diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index a672ee7..c329297 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -157,8 +157,7 @@ class IMAPClient(object): The *ssl_context* argument is optional and should be a :py:class:`backports.ssl.SSLContext` object. If no SSL context - is given, a default SSL context with reasonable default - settings will be created. + is given, a SSL context with reasonable default settings will be used. You can enable checking of the hostname in the certificate presented by the server against the hostname which was used for connecting, by diff --git a/imapclient/tls.py b/imapclient/tls.py index 49b065b..18e6da0 100644 --- a/imapclient/tls.py +++ b/imapclient/tls.py @@ -3,11 +3,14 @@ # Please see http://en.wikipedia.org/wiki/BSD_licenses """ -Good TLS support for IMAPClient built on backports.ssl/pyOpenSSL. +This module contains IMAPClient's functionality related to Transport +Layer Security (TLS a.k.a. SSL). -Works with Python 2.6+ and 3.3+. +It uses ``backports.ssl`` to provide consistent TLS functionality +across Python versions. """ + __all__ = ('create_default_context',) @@ -39,7 +42,23 @@ _RESTRICTED_SERVER_CIPHERS = ( # TODO: get this into backports.ssl def create_default_context(cafile=None, capath=None, cadata=None): - """Create a SSLContext object with sensible default settings. + """Return a backports.ssl.SSLContext object configured with sensible + default settings. + + The optional *cafile* argument is path to a file of concatenated + CA certificates in PEM format. + + The optional *capath* argument is a path to a directory containing + several CA certificates in PEM format, following an OpenSSL + specific layout. + + The optional *cadata* argument is either an ASCII string of one or + more PEM-encoded certificates or a bytes-like object of + DER-encoded certificates. + + If *cafile*, *capath* and *cadata* are all None then + system-installed CA certificates will be loaded (if available). + """ # adapted from Python 3.4's ssl.create_default_context