@@ -201,13 +201,9 @@ instead.
201201 .. note ::
202202
203203 Which connections succeed will vary depending on the version of
204- OpenSSL. For instance, in some older versions of OpenSSL (such
205- as 0.9.7l on OS X 10.4), an SSLv2 client could not connect to an
206- SSLv23 server. Another example: beginning with OpenSSL 1.0.0,
207- an SSLv23 client will not actually attempt SSLv2 connections
208- unless you explicitly enable SSLv2 ciphers; for example, you
209- might specify ``"ALL" `` or ``"SSLv2" `` as the *ciphers * parameter
210- to enable them.
204+ OpenSSL. For example, beginning with OpenSSL 1.0.0, an SSLv23 client
205+ will not actually attempt SSLv2 connections unless you explicitly
206+ enable SSLv2 ciphers (which is not recommended, as SSLv2 is broken).
211207
212208 The *ciphers * parameter sets the available ciphers for this SSL object.
213209 It should be a string in the `OpenSSL cipher list format
@@ -534,6 +530,11 @@ Constants
534530
535531 .. versionadded :: 3.4
536532
533+ .. data :: PROTOCOL_SSLv23
534+
535+ Selects the highest protocol version that both the client and server support.
536+ Despite the name, this option can select "TLS" protocols as well as "SSL".
537+
537538.. data :: PROTOCOL_SSLv2
538539
539540 Selects SSL version 2 as the channel encryption protocol.
@@ -545,17 +546,13 @@ Constants
545546
546547 SSL version 2 is insecure. Its use is highly discouraged.
547548
548- .. data :: PROTOCOL_SSLv23
549+ .. data :: PROTOCOL_SSLv3
549550
550- Selects SSL version 2 or 3 as the channel encryption protocol. This is a
551- setting to use with servers for maximum compatibility with the other end of
552- an SSL connection, but it may cause the specific ciphers chosen for the
553- encryption to be of fairly low quality.
551+ Selects SSL version 3 as the channel encryption protocol.
554552
555- .. data :: PROTOCOL_SSLv3
553+ .. warning ::
556554
557- Selects SSL version 3 as the channel encryption protocol. For clients, this
558- is the maximally compatible SSL variant.
555+ SSL version 3 is insecure. Its use is highly discouraged.
559556
560557.. data :: PROTOCOL_TLSv1
561558
@@ -570,9 +567,9 @@ Constants
570567
571568.. data :: PROTOCOL_TLSv1_2
572569
573- Selects TLS version 1.2 as the channel encryption protocol. This is the most
574- modern version, and probably the best choice for maximum protection, if both
575- sides can speak it. Available only with openssl version 1.0.1+.
570+ Selects TLS version 1.2 as the channel encryption protocol. This is the
571+ most modern version, and probably the best choice for maximum protection,
572+ if both sides can speak it. Available only with openssl version 1.0.1+.
576573
577574 .. versionadded :: 3.4
578575
@@ -667,9 +664,8 @@ Constants
667664.. data :: HAS_SNI
668665
669666 Whether the OpenSSL library has built-in support for the *Server Name
670- Indication * extension to the SSLv3 and TLSv1 protocols (as defined in
671- :rfc: `4366 `). When true, you can use the *server_hostname * argument to
672- :meth: `SSLContext.wrap_socket `.
667+ Indication * extension (as defined in :rfc: `4366 `). When true, you can
668+ use the *server_hostname * argument to :meth: `SSLContext.wrap_socket `.
673669
674670 .. versionadded :: 3.2
675671
@@ -1474,118 +1470,100 @@ should use the following idiom::
14741470Client-side operation
14751471^^^^^^^^^^^^^^^^^^^^^
14761472
1477- This example connects to an SSL server and prints the server's certificate::
1478-
1479- import socket, ssl, pprint
1480-
1481- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
1482- # require a certificate from the server
1483- ssl_sock = ssl.wrap_socket(s,
1484- ca_certs="/etc/ca_certs_file",
1485- cert_reqs=ssl.CERT_REQUIRED)
1486- ssl_sock.connect(('www.verisign.com', 443))
1487-
1488- pprint.pprint(ssl_sock.getpeercert())
1489- # note that closing the SSLSocket will also close the underlying socket
1490- ssl_sock.close()
1491-
1492- As of January 6, 2012, the certificate printed by this program looks like
1493- this::
1494-
1495- {'issuer': ((('countryName', 'US'),),
1496- (('organizationName', 'VeriSign, Inc.'),),
1497- (('organizationalUnitName', 'VeriSign Trust Network'),),
1498- (('organizationalUnitName',
1499- 'Terms of use at https://www.verisign.com/rpa (c)06'),),
1500- (('commonName',
1501- 'VeriSign Class 3 Extended Validation SSL SGC CA'),)),
1502- 'notAfter': 'May 25 23:59:59 2012 GMT',
1503- 'notBefore': 'May 26 00:00:00 2010 GMT',
1504- 'serialNumber': '53D2BEF924A7245E83CA01E46CAA2477',
1505- 'subject': ((('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
1506- (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
1507- (('businessCategory', 'V1.0, Clause 5.(b)'),),
1508- (('serialNumber', '2497886'),),
1509- (('countryName', 'US'),),
1510- (('postalCode', '94043'),),
1511- (('stateOrProvinceName', 'California'),),
1512- (('localityName', 'Mountain View'),),
1513- (('streetAddress', '487 East Middlefield Road'),),
1514- (('organizationName', 'VeriSign, Inc.'),),
1515- (('organizationalUnitName', ' Production Security Services'),),
1516- (('commonName', 'www.verisign.com'),)),
1517- 'subjectAltName': (('DNS', 'www.verisign.com'),
1518- ('DNS', 'verisign.com'),
1519- ('DNS', 'www.verisign.net'),
1520- ('DNS', 'verisign.net'),
1521- ('DNS', 'www.verisign.mobi'),
1522- ('DNS', 'verisign.mobi'),
1523- ('DNS', 'www.verisign.eu'),
1524- ('DNS', 'verisign.eu')),
1525- 'version': 3}
1473+ This example creates a SSL context with the recommended security settings
1474+ for client sockets, including automatic certificate verification::
1475+
1476+ >>> context = ssl.create_default_context()
15261477
1527- This other example first creates an SSL context, instructs it to verify
1528- certificates sent by peers, and feeds it a set of recognized certificate
1529- authorities (CA )::
1478+ If you prefer to tune security settings yourself, you might create
1479+ a context from scratch (but beware that you might not get the settings
1480+ right )::
15301481
15311482 >>> context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
15321483 >>> context.verify_mode = ssl.CERT_REQUIRED
1484+ >>> context.check_hostname = True
15331485 >>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
15341486
1535- (it is assumed your operating system places a bundle of all CA certificates
1536- in ``/etc/ssl/certs/ca-bundle.crt ``; if not, you'll get an error and have
1537- to adjust the location)
1487+ (this snippet assumes your operating system places a bundle of all CA
1488+ certificates in ``/etc/ssl/certs/ca-bundle.crt ``; if not, you'll get an
1489+ error and have to adjust the location)
15381490
15391491When you use the context to connect to a server, :const: `CERT_REQUIRED `
15401492validates the server certificate: it ensures that the server certificate
15411493was signed with one of the CA certificates, and checks the signature for
15421494correctness::
15431495
1544- >>> conn = context.wrap_socket(socket.socket(socket.AF_INET))
1545- >>> conn.connect(("linuxfr.org", 443))
1496+ >>> conn = context.wrap_socket(socket.socket(socket.AF_INET),
1497+ ... server_hostname="www.python.org")
1498+ >>> conn.connect(("www.python.org", 443))
15461499
1547- You should then fetch the certificate and check its fields for conformity ::
1500+ You may then fetch the certificate::
15481501
15491502 >>> cert = conn.getpeercert()
1550- >>> ssl.match_hostname(cert, "linuxfr.org")
15511503
15521504Visual inspection shows that the certificate does identify the desired service
1553- (that is, the HTTPS host ``linuxfr .org ``)::
1505+ (that is, the HTTPS host ``www.python .org ``)::
15541506
15551507 >>> pprint.pprint(cert)
1556- {'issuer': ((('organizationName', 'CAcert Inc.'),),
1557- (('organizationalUnitName', 'http://www.CAcert.org'),),
1558- (('commonName', 'CAcert Class 3 Root'),)),
1559- 'notAfter': 'Jun 7 21:02:24 2013 GMT',
1560- 'notBefore': 'Jun 8 21:02:24 2011 GMT',
1561- 'serialNumber': 'D3E9',
1562- 'subject': ((('commonName', 'linuxfr.org'),),),
1563- 'subjectAltName': (('DNS', 'linuxfr.org'),
1564- ('othername', '<unsupported>'),
1565- ('DNS', 'linuxfr.org'),
1566- ('othername', '<unsupported>'),
1567- ('DNS', 'dev.linuxfr.org'),
1568- ('othername', '<unsupported>'),
1569- ('DNS', 'prod.linuxfr.org'),
1570- ('othername', '<unsupported>'),
1571- ('DNS', 'alpha.linuxfr.org'),
1572- ('othername', '<unsupported>'),
1573- ('DNS', '*.linuxfr.org'),
1574- ('othername', '<unsupported>')),
1508+ {'OCSP': ('http://ocsp.digicert.com',),
1509+ 'caIssuers': ('http://cacerts.digicert.com/DigiCertSHA2ExtendedValidationServerCA.crt',),
1510+ 'crlDistributionPoints': ('http://crl3.digicert.com/sha2-ev-server-g1.crl',
1511+ 'http://crl4.digicert.com/sha2-ev-server-g1.crl'),
1512+ 'issuer': ((('countryName', 'US'),),
1513+ (('organizationName', 'DigiCert Inc'),),
1514+ (('organizationalUnitName', 'www.digicert.com'),),
1515+ (('commonName', 'DigiCert SHA2 Extended Validation Server CA'),)),
1516+ 'notAfter': 'Sep 9 12:00:00 2016 GMT',
1517+ 'notBefore': 'Sep 5 00:00:00 2014 GMT',
1518+ 'serialNumber': '01BB6F00122B177F36CAB49CEA8B6B26',
1519+ 'subject': ((('businessCategory', 'Private Organization'),),
1520+ (('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
1521+ (('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
1522+ (('serialNumber', '3359300'),),
1523+ (('streetAddress', '16 Allen Rd'),),
1524+ (('postalCode', '03894-4801'),),
1525+ (('countryName', 'US'),),
1526+ (('stateOrProvinceName', 'NH'),),
1527+ (('localityName', 'Wolfeboro,'),),
1528+ (('organizationName', 'Python Software Foundation'),),
1529+ (('commonName', 'www.python.org'),)),
1530+ 'subjectAltName': (('DNS', 'www.python.org'),
1531+ ('DNS', 'python.org'),
1532+ ('DNS', 'pypi.python.org'),
1533+ ('DNS', 'docs.python.org'),
1534+ ('DNS', 'testpypi.python.org'),
1535+ ('DNS', 'bugs.python.org'),
1536+ ('DNS', 'wiki.python.org'),
1537+ ('DNS', 'hg.python.org'),
1538+ ('DNS', 'mail.python.org'),
1539+ ('DNS', 'packaging.python.org'),
1540+ ('DNS', 'pythonhosted.org'),
1541+ ('DNS', 'www.pythonhosted.org'),
1542+ ('DNS', 'test.pythonhosted.org'),
1543+ ('DNS', 'us.pycon.org'),
1544+ ('DNS', 'id.python.org')),
15751545 'version': 3}
15761546
1577- Now that you are assured of its authenticity , you can proceed to talk with
1578- the server::
1547+ Now the SSL channel is established and the certificate verified , you can
1548+ proceed to talk with the server::
15791549
15801550 >>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
15811551 >>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
1582- [b'HTTP/1.1 302 Found',
1583- b'Date: Sun, 16 May 2010 13:43:28 GMT',
1584- b'Server: Apache/2.2',
1585- b'Location: https://linuxfr.org/pub/',
1586- b'Vary: Accept-Encoding',
1552+ [b'HTTP/1.1 200 OK',
1553+ b'Date: Sat, 18 Oct 2014 18:27:20 GMT',
1554+ b'Server: nginx',
1555+ b'Content-Type: text/html; charset=utf-8',
1556+ b'X-Frame-Options: SAMEORIGIN',
1557+ b'Content-Length: 45679',
1558+ b'Accept-Ranges: bytes',
1559+ b'Via: 1.1 varnish',
1560+ b'Age: 2188',
1561+ b'X-Served-By: cache-lcy1134-LCY',
1562+ b'X-Cache: HIT',
1563+ b'X-Cache-Hits: 11',
1564+ b'Vary: Cookie',
1565+ b'Strict-Transport-Security: max-age=63072000; includeSubDomains',
15871566 b'Connection: close',
1588- b'Content-Type: text/html; charset=iso-8859-1',
15891567 b'',
15901568 b'']
15911569
@@ -1603,7 +1581,7 @@ waiting for clients to connect::
16031581
16041582 import socket, ssl
16051583
1606- context = ssl.SSLContext (ssl.PROTOCOL_SSLv23 )
1584+ context = ssl.create_default_context (ssl.Purpose.CLIENT_AUTH )
16071585 context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
16081586
16091587 bindsocket = socket.socket()
@@ -1764,16 +1742,18 @@ to specify :const:`CERT_REQUIRED` and similarly check the client certificate.
17641742Protocol versions
17651743'''''''''''''''''
17661744
1767- SSL version 2 is considered insecure and is therefore dangerous to use. If
1768- you want maximum compatibility between clients and servers, it is recommended
1769- to use :const: `PROTOCOL_SSLv23 ` as the protocol version and then disable
1770- SSLv2 explicitly using the :data: `SSLContext.options ` attribute::
1745+ SSL versions 2 and 3 are considered insecure and are therefore dangerous to
1746+ use. If you want maximum compatibility between clients and servers, it is
1747+ recommended to use :const: `PROTOCOL_SSLv23 ` as the protocol version and then
1748+ disable SSLv2 and SSLv3 explicitly using the :data: `SSLContext.options `
1749+ attribute::
17711750
17721751 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
17731752 context.options |= ssl.OP_NO_SSLv2
1753+ context.options |= ssl.OP_NO_SSLv3
17741754
1775- The SSL context created above will allow SSLv3 and TLSv1 ( and later, if
1776- supported by your system) connections, but not SSLv2 .
1755+ The SSL context created above will only allow TLSv1 and later ( if
1756+ supported by your system) connections.
17771757
17781758Cipher selection
17791759''''''''''''''''
0 commit comments