Changeset 391 for python/trunk/Doc/library/ssl.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/library/ssl.rst
r2 r391 1 :mod:`ssl` --- SSL wrapper for socket objects2 ============================================= 1 :mod:`ssl` --- TLS/SSL wrapper for socket objects 2 ================================================= 3 3 4 4 .. module:: ssl 5 :synopsis: SSL wrapper for socket objects5 :synopsis: TLS/SSL wrapper for socket objects 6 6 7 7 .. 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 8 14 9 15 .. versionadded:: 2.6 10 16 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 -------------- 17 20 18 21 This module provides access to Transport Layer Security (often known as "Secure … … 51 54 :exc:`IOError`. 52 55 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) 54 57 55 58 Takes an instance ``sock`` of :class:`socket.socket`, and returns an instance … … 101 104 use. Typically, the server chooses a particular protocol version, and the 102 105 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 106 108 versions. 107 109 … … 114 116 *client* / **server** **SSLv2** **SSLv3** **SSLv23** **TLSv1** 115 117 ------------------------ --------- --------- ---------- --------- 116 *SSLv2* yes no yes *no117 *SSLv3* yesyes yes no118 *SSLv2* yes no yes no 119 *SSLv3* no yes yes no 118 120 *SSLv23* yes no yes no 119 121 *TLSv1* no no yes yes 120 122 ======================== ========= ========= ========== ========= 121 123 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>`_. 124 138 125 139 The parameter ``do_handshake_on_connect`` specifies whether to do the SSL … … 135 149 normal EOF in response to unexpected EOF errors raised from the underlying 136 150 socket; if :const:`False`, it will raise the exceptions back to the caller. 151 152 .. versionchanged:: 2.7 153 New optional argument *ciphers*. 137 154 138 155 .. function:: RAND_status() … … 223 240 Selects SSL version 2 as the channel encryption protocol. 224 241 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 225 249 .. data:: PROTOCOL_SSLv23 226 250 … … 241 265 sides can speak it. 242 266 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 243 297 244 298 SSLSocket Objects 245 299 ----------------- 246 300 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. 301 SSL 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 320 However, since the SSL (and TLS) protocol has its own framing atop 321 of TCP, the SSL sockets abstraction can, in certain respects, diverge from 322 the specification of normal, OS-level sockets. 323 324 SSL sockets also have the following additional methods and attributes: 255 325 256 326 .. method:: SSLSocket.getpeercert(binary_form=False) … … 259 329 returns ``None``. 260 330 261 If the parameter ``binary_form``is :const:`False`, and a certificate was331 If the ``binary_form`` parameter is :const:`False`, and a certificate was 262 332 received from the peer, this method returns a :class:`dict` instance. If the 263 333 certificate was not validated, the dict is empty. If the certificate was … … 285 355 provided, this method returns the DER-encoded form of the entire certificate 286 356 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`). 291 367 292 368 .. method:: SSLSocket.cipher() … … 308 384 s.do_handshake() 309 385 break 310 except ssl.SSLError ,err:386 except ssl.SSLError as err: 311 387 if err.args[0] == ssl.SSL_ERROR_WANT_READ: 312 388 select.select([s], [], []) … … 457 533 458 534 try: 459 import ssl535 import ssl 460 536 except ImportError: 461 pass537 pass 462 538 else: 463 [ do something that requires SSL support ]539 ... # do something that requires SSL support 464 540 465 541 Client-side operation … … 534 610 535 611 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() 543 623 544 624 Then you'd read data from the ``connstream`` and do something with it till you … … 546 626 547 627 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 559 637 560 638 And go back to listening for new client connections. … … 564 642 565 643 Class :class:`socket.socket` 566 567 568 ` Introducing SSL and Certificates using OpenSSL <http://old.pseudonym.org/ssl/wwwj-index.html>`_569 Frederick J. Hirsch644 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 570 648 571 649 `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.