Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Doc/library/ssl.rst

    r2 r391  
    1 :mod:`ssl` --- SSL wrapper for socket objects
    2 =============================================
     1:mod:`ssl` --- TLS/SSL wrapper for socket objects
     2=================================================
    33
    44.. module:: ssl
    5    :synopsis: SSL wrapper for socket objects
     5   :synopsis: TLS/SSL wrapper for socket objects
    66
    77.. moduleauthor:: Bill Janssen <bill.janssen@gmail.com>
     8.. sectionauthor::  Bill Janssen <bill.janssen@gmail.com>
     9
     10
     11.. index:: single: OpenSSL; (use in module ssl)
     12
     13.. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer
    814
    915.. versionadded:: 2.6
    1016
    11 .. sectionauthor::  Bill Janssen <bill.janssen@gmail.com>
    12 
    13 
    14 .. index:: single: OpenSSL; (use in module ssl)
    15 
    16 .. index:: TLS, SSL, Transport Layer Security, Secure Sockets Layer
     17**Source code:** :source:`Lib/ssl.py`
     18
     19--------------
    1720
    1821This module provides access to Transport Layer Security (often known as "Secure
     
    5154   :exc:`IOError`.
    5255
    53 .. function:: wrap_socket (sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version={see docs}, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True)
     56.. function:: wrap_socket (sock, keyfile=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version={see docs}, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None)
    5457
    5558   Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance
     
    101104   use.  Typically, the server chooses a particular protocol version, and the
    102105   client must adapt to the server's choice.  Most of the versions are not
    103    interoperable with the other versions.  If not specified, for client-side
    104    operation, the default SSL version is SSLv3; for server-side operation,
    105    SSLv23.  These version selections provide the most compatibility with other
     106   interoperable with the other versions.  If not specified, the default is
     107   :data:`PROTOCOL_SSLv23`; it provides the most compatibility with other
    106108   versions.
    107109
     
    114116        *client* / **server**    **SSLv2**  **SSLv3**  **SSLv23**  **TLSv1**
    115117       ------------------------  ---------  ---------  ----------  ---------
    116         *SSLv2*                    yes        no         yes*        no
    117         *SSLv3*                    yes        yes        yes         no
     118        *SSLv2*                    yes        no         yes         no
     119        *SSLv3*                    no         yes        yes         no
    118120        *SSLv23*                   yes        no         yes         no
    119121        *TLSv1*                    no         no         yes         yes
    120122       ========================  =========  =========  ==========  =========
    121123
    122    In some older versions of OpenSSL (for instance, 0.9.7l on OS X 10.4), an
    123    SSLv2 client could not connect to an SSLv23 server.
     124   .. note::
     125
     126      Which connections succeed will vary depending on the version of
     127      OpenSSL.  For instance, in some older versions of OpenSSL (such
     128      as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an
     129      SSLv23 server.  Another example: beginning with OpenSSL 1.0.0,
     130      an SSLv23 client will not actually attempt SSLv2 connections
     131      unless you explicitly enable SSLv2 ciphers; for example, you
     132      might specify ``"ALL"`` or ``"SSLv2"`` as the *ciphers* parameter
     133      to enable them.
     134
     135   The *ciphers* parameter sets the available ciphers for this SSL object.
     136   It should be a string in the `OpenSSL cipher list format
     137   <http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT>`_.
    124138
    125139   The parameter ``do_handshake_on_connect`` specifies whether to do the SSL
     
    135149   normal EOF in response to unexpected EOF errors raised from the underlying
    136150   socket; if :const:`False`, it will raise the exceptions back to the caller.
     151
     152   .. versionchanged:: 2.7
     153      New optional argument *ciphers*.
    137154
    138155.. function:: RAND_status()
     
    223240   Selects SSL version 2 as the channel encryption protocol.
    224241
     242   This protocol is not available if OpenSSL is compiled with OPENSSL_NO_SSL2
     243   flag.
     244
     245   .. warning::
     246
     247      SSL version 2 is insecure.  Its use is highly discouraged.
     248
    225249.. data:: PROTOCOL_SSLv23
    226250
     
    241265   sides can speak it.
    242266
     267.. data:: OPENSSL_VERSION
     268
     269   The version string of the OpenSSL library loaded by the interpreter::
     270
     271    >>> ssl.OPENSSL_VERSION
     272    'OpenSSL 0.9.8k 25 Mar 2009'
     273
     274   .. versionadded:: 2.7
     275
     276.. data:: OPENSSL_VERSION_INFO
     277
     278   A tuple of five integers representing version information about the
     279   OpenSSL library::
     280
     281    >>> ssl.OPENSSL_VERSION_INFO
     282    (0, 9, 8, 11, 15)
     283
     284   .. versionadded:: 2.7
     285
     286.. data:: OPENSSL_VERSION_NUMBER
     287
     288   The raw version number of the OpenSSL library, as a single integer::
     289
     290    >>> ssl.OPENSSL_VERSION_NUMBER
     291    9470143L
     292    >>> hex(ssl.OPENSSL_VERSION_NUMBER)
     293    '0x9080bfL'
     294
     295   .. versionadded:: 2.7
     296
    243297
    244298SSLSocket Objects
    245299-----------------
    246300
    247 .. method:: SSLSocket.read([nbytes=1024])
    248 
    249    Reads up to ``nbytes`` bytes from the SSL-encrypted channel and returns them.
    250 
    251 .. method:: SSLSocket.write(data)
    252 
    253    Writes the ``data`` to the other side of the connection, using the SSL
    254    channel to encrypt.  Returns the number of bytes written.
     301SSL sockets provide the following methods of :ref:`socket-objects`:
     302
     303- :meth:`~socket.socket.accept()`
     304- :meth:`~socket.socket.bind()`
     305- :meth:`~socket.socket.close()`
     306- :meth:`~socket.socket.connect()`
     307- :meth:`~socket.socket.fileno()`
     308- :meth:`~socket.socket.getpeername()`, :meth:`~socket.socket.getsockname()`
     309- :meth:`~socket.socket.getsockopt()`, :meth:`~socket.socket.setsockopt()`
     310- :meth:`~socket.socket.gettimeout()`, :meth:`~socket.socket.settimeout()`,
     311  :meth:`~socket.socket.setblocking()`
     312- :meth:`~socket.socket.listen()`
     313- :meth:`~socket.socket.makefile()`
     314- :meth:`~socket.socket.recv()`, :meth:`~socket.socket.recv_into()`
     315  (but passing a non-zero ``flags`` argument is not allowed)
     316- :meth:`~socket.socket.send()`, :meth:`~socket.socket.sendall()` (with
     317  the same limitation)
     318- :meth:`~socket.socket.shutdown()`
     319
     320However, since the SSL (and TLS) protocol has its own framing atop
     321of TCP, the SSL sockets abstraction can, in certain respects, diverge from
     322the specification of normal, OS-level sockets.
     323
     324SSL sockets also have the following additional methods and attributes:
    255325
    256326.. method:: SSLSocket.getpeercert(binary_form=False)
     
    259329   returns ``None``.
    260330
    261    If the parameter ``binary_form`` is :const:`False`, and a certificate was
     331   If the ``binary_form`` parameter is :const:`False`, and a certificate was
    262332   received from the peer, this method returns a :class:`dict` instance.  If the
    263333   certificate was not validated, the dict is empty.  If the certificate was
     
    285355   provided, this method returns the DER-encoded form of the entire certificate
    286356   as a sequence of bytes, or :const:`None` if the peer did not provide a
    287    certificate.  This return value is independent of validation; if validation
    288    was required (:const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`), it will have
    289    been validated, but if :const:`CERT_NONE` was used to establish the
    290    connection, the certificate, if present, will not have been validated.
     357   certificate.  Whether the peer provides a certificate depends on the SSL
     358   socket's role:
     359
     360   * for a client SSL socket, the server will always provide a certificate,
     361     regardless of whether validation was required;
     362
     363   * for a server SSL socket, the client will only provide a certificate
     364     when requested by the server; therefore :meth:`getpeercert` will return
     365     :const:`None` if you used :const:`CERT_NONE` (rather than
     366     :const:`CERT_OPTIONAL` or :const:`CERT_REQUIRED`).
    291367
    292368.. method:: SSLSocket.cipher()
     
    308384                s.do_handshake()
    309385                break
    310             except ssl.SSLError, err:
     386            except ssl.SSLError as err:
    311387                if err.args[0] == ssl.SSL_ERROR_WANT_READ:
    312388                    select.select([s], [], [])
     
    457533
    458534   try:
    459       import ssl
     535       import ssl
    460536   except ImportError:
    461       pass
     537       pass
    462538   else:
    463       [ do something that requires SSL support ]
     539       ... # do something that requires SSL support
    464540
    465541Client-side operation
     
    534610
    535611   while True:
    536       newsocket, fromaddr = bindsocket.accept()
    537       connstream = ssl.wrap_socket(newsocket,
    538                                    server_side=True,
    539                                    certfile="mycertfile",
    540                                    keyfile="mykeyfile",
    541                                    ssl_version=ssl.PROTOCOL_TLSv1)
    542       deal_with_client(connstream)
     612       newsocket, fromaddr = bindsocket.accept()
     613       connstream = ssl.wrap_socket(newsocket,
     614                                    server_side=True,
     615                                    certfile="mycertfile",
     616                                    keyfile="mykeyfile",
     617                                    ssl_version=ssl.PROTOCOL_TLSv1)
     618       try:
     619           deal_with_client(connstream)
     620       finally:
     621           connstream.shutdown(socket.SHUT_RDWR)
     622           connstream.close()
    543623
    544624Then you'd read data from the ``connstream`` and do something with it till you
     
    546626
    547627   def deal_with_client(connstream):
    548 
    549       data = connstream.read()
    550       # null data means the client is finished with us
    551       while data:
    552          if not do_something(connstream, data):
    553             # we'll assume do_something returns False
    554             # when we're finished with client
    555             break
    556          data = connstream.read()
    557       # finished with client
    558       connstream.close()
     628       data = connstream.read()
     629       # null data means the client is finished with us
     630       while data:
     631           if not do_something(connstream, data):
     632               # we'll assume do_something returns False
     633               # when we're finished with client
     634               break
     635           data = connstream.read()
     636       # finished with client
    559637
    560638And go back to listening for new client connections.
     
    564642
    565643   Class :class:`socket.socket`
    566             Documentation of underlying :mod:`socket` class
    567 
    568    `Introducing SSL and Certificates using OpenSSL <http://old.pseudonym.org/ssl/wwwj-index.html>`_
    569        Frederick J. Hirsch
     644       Documentation of underlying :mod:`socket` class
     645
     646   `SSL/TLS Strong Encryption: An Introduction <http://httpd.apache.org/docs/trunk/en/ssl/ssl_intro.html>`_
     647       Intro from the Apache webserver documentation
    570648
    571649   `RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management <http://www.ietf.org/rfc/rfc1422>`_
Note: See TracChangeset for help on using the changeset viewer.