Previous. Write a Python code to send cookies to a given server and access cookies from the response of a server
What is the difficulty level of this exercise?
Easy Medium HardTest your Programming skills with w3resource's quiz
Share this Tutorial / Exercise on . Facebook and Twitter
Python. Tips of the Day
Getting rid of unwanted characters
You can get rid of whitespaces or any specific character using strip methods in Python. You can use either plain strip for both sides, lstrip for the left side and rstrip for the right side only
This module provides access to Transport Layer Security [often known as “Secure Sockets Layer”] encryption and peer authentication facilities for network sockets, both client-side and server-side. This module uses the OpenSSL library. It is available on all modern Unix systems, Windows, macOS, and probably additional platforms, as long as OpenSSL is installed on that platform
Note
Some behavior may be platform dependent, since calls are made to the operating system socket APIs. The installed version of OpenSSL may also cause variations in behavior. For example, TLSv1. 3 with OpenSSL version 1. 1. 1
Warning
Don’t use this module without reading the Security considerations . Doing so may lead to a false sense of security, as the default settings of the ssl module are not necessarily appropriate for your application.
Availability . not Emscripten, not WASI.
This module does not work or is not available on WebAssembly platforms
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv36 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv37. See WebAssembly platforms for more information.
This section documents the objects and functions in the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35 module; for more general information about TLS, SSL, and certificates, the reader is referred to the documents in the “See Also” section at the bottom
This module provides a class,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv39, which is derived from the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'0 type, and provides a socket-like wrapper that also encrypts and decrypts the data going over the socket with SSL. It supports additional methods such as
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'1, which retrieves the certificate of the other side of the connection, and
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'2, which retrieves the cipher being used for the secure connection
For more sophisticated applications, the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'3 class helps manage settings and certificates, which can then be inherited by SSL sockets created through the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 method
Changed in version 3. 5. 3. Updated to support linking with OpenSSL 1. 1. 0
Changed in version 3. 6. OpenSSL 0. 9. 8, 1. 0. 0 and 1. 0. 1 are deprecated and no longer supported. In the future the ssl module will require at least OpenSSL 1. 0. 2 or 1. 1. 0.
Changed in version 3. 10. PEP 644 has been implemented. The ssl module requires OpenSSL 1. 1. 1 or newer.
Use of deprecated constants and functions result in deprecation warnings
Functions, Constants, and Exceptions¶
Socket creation¶
Since Python 3. 2 and 2. 7. 9, it is recommended to use the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 of an
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 instance to wrap sockets as
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 objects. The helper functions
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 returns a new context with secure default settings. The old
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9 function is deprecated since it is both inefficient and has no support for server name indication [SNI] and hostname matching
Client socket example with default context and IPv4/IPv6 dual stack
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv34
Client socket example with custom context and IPv4
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35
Server socket example listening on localhost IPv4
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv36
Context creation¶
A convenience function helps create
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 objects for common purposesssl. create_default_context[purpose=Purpose. SERVER_AUTH , cafile=None , capath=None , cadata=None] ¶
Return a new
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 object with default settings for the given purpose. The settings are chosen by the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35 module, and usually represent a higher security level than when calling the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 constructor directly
cafile, capath, cadata represent optional CA certificates to trust for certificate verification, as in
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314. If all three are
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315, this function can choose to trust the system’s default CA certificates instead
The settings are.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318, and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 with high encryption cipher suites without RC4 and without unauthenticated cipher suites. Passing
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv370 as purpose sets
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 and either loads CA certificates [when at least one of cafile, capath or cadata is given] or uses
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373 to load default CA certificates
When
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv374 is supported and the environment variable
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv375 is set,
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 enables key logging
Note
The protocol, options, cipher and other settings may change to more restrictive values anytime without prior deprecation. The values represent a fair balance between compatibility and security
Nếu ứng dụng của bạn cần các cài đặt cụ thể, bạn nên tạo một
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 và tự áp dụng các cài đặt đó
Note
If you find that when certain older clients or servers attempt to connect with a
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 created by this function that they get an error stating “Protocol or cipher suite mismatch”, it may be that they only support SSL3. 0 which this function excludes using the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319. SSL3. 0 is widely considered to be completely broken. Nếu bạn vẫn muốn tiếp tục sử dụng chức năng này nhưng vẫn cho phép SSL 3. 0 kết nối, bạn có thể kích hoạt lại chúng bằng cách sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
Mới trong phiên bản 3. 4
Đã thay đổi trong phiên bản 3. 4. 4. RC4 đã bị xóa khỏi chuỗi mật mã mặc định.
Đã thay đổi trong phiên bản 3. 6. ChaCha20/Poly1305 đã được thêm vào chuỗi mật mã mặc định.
3DES đã bị xóa khỏi chuỗi mật mã mặc định
Đã thay đổi trong phiên bản 3. 8. Đã thêm hỗ trợ ghi khóa vào
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv375.
Đã thay đổi trong phiên bản 3. 10. The context now uses
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 protocol instead of generic
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13.
Exceptions¶
exception ssl. SSLError ¶Raised to signal an error from the underlying SSL implementation [currently provided by the OpenSSL library]. This signifies some problem in the higher-level encryption and authentication layer that’s superimposed on the underlying network connection. This error is a subtype of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'14. The error code and message of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 instances are provided by the OpenSSL library
Changed in version 3. 3.
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 used to be a subtype of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'17. library ¶
A string mnemonic designating the OpenSSL submodule in which the error occurred, such as
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'18,
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'19 or
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'80. The range of possible values depends on the OpenSSL version
New in version 3. 3
reason ¶A string mnemonic designating the reason this error occurred, for example
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'81. The range of possible values depends on the OpenSSL version
New in version 3. 3
exception ssl. SSLZeroReturnError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised when trying to read or write and the SSL connection has been closed cleanly. Note that this doesn’t mean that the underlying transport [read TCP] has been closed
New in version 3. 3
exception ssl. SSLWantReadError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised by a non-blocking SSL socket when trying to read or write data, but more data needs to be received on the underlying TCP transport before the request can be fulfilled.
New in version 3. 3
ngoại lệ ssl. SSLWantWriteError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised by a non-blocking SSL socket when trying to read or write data, but more data needs to be sent on the underlying TCP transport before the request can be fulfilled.
New in version 3. 3
exception ssl. SSLSyscallError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised when a system error was encountered while trying to fulfill an operation on a SSL socket. Unfortunately, there is no easy way to inspect the original errno number
New in version 3. 3
exception ssl. SSLEOFError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised when the SSL connection has been terminated abruptly. Generally, you shouldn’t try to reuse the underlying transport when this error is encountered
New in version 3. 3
exception ssl. SSLCertVerificationError ¶A subclass of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 raised when certificate validation has failed
New in version 3. 7
verify_code ¶A numeric error number that denotes the verification error
verify_message ¶A human readable string of the verification error
exception ssl. CertificateError ¶Bí danh cho
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'88
Changed in version 3. 7. Ngoại lệ hiện là bí danh cho
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'88.
Random generation¶
ssl. RAND_bytes[num] ¶Trả về num byte giả ngẫu nhiên mạnh về mật mã. Raises an
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 if the PRNG has not been seeded with enough data or if the operation is not supported by the current RAND method.
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'91 can be used to check the status of the PRNG and
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'92 can be used to seed the PRNG
For almost all applications
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'93 is preferable
Read the Wikipedia article, Cryptographically secure pseudorandom number generator [CSPRNG], to get the requirements of a cryptographically strong generator
New in version 3. 3
ssl. RAND_pseudo_bytes[num] ¶Return [bytes, is_cryptographic]. bytes are num pseudo-random bytes, is_cryptographic is
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 if the bytes generated are cryptographically strong. Raises an
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 if the operation is not supported by the current RAND method
Generated pseudo-random byte sequences will be unique if they are of sufficient length, but are not necessarily unpredictable. They can be used for non-cryptographic purposes and for certain purposes in cryptographic protocols, but usually not for key generation etc
For almost all applications
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'93 is preferable
New in version 3. 3
Deprecated since version 3. 6. OpenSSL has deprecated
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'97, use
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'98 instead. ssl. RAND_status[] ¶
Return
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 if the SSL pseudo-random number generator has been seeded with ‘enough’ randomness, and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500 otherwise. You can use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3501 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3502 to increase the randomness of the pseudo-random number generatorssl. RAND_add[byte , entropy]¶
Trộn các byte đã cho vào trình tạo số giả ngẫu nhiên SSL. Tham số entropy [số float] là giới hạn dưới của entropy chứa trong chuỗi [vì vậy bạn luôn có thể sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3503]. Xem RFC 1750 để biết thêm thông tin về các nguồn entropy
Đã thay đổi trong phiên bản 3. 5. Có thể ghi đối tượng giống như byte hiện đã được chấp nhận.
Xử lý chứng chỉ¶
ssl. match_hostname[cert , hostname]¶Xác minh rằng chứng chỉ [ở định dạng đã giải mã được trả về bởi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3504] khớp với tên máy chủ đã cho. Các quy tắc được áp dụng là những quy tắc để kiểm tra danh tính của máy chủ HTTPS như được nêu trong RFC 2818, RFC 5280 và RFC 6125. Ngoài HTTPS, chức năng này phải phù hợp để kiểm tra danh tính của máy chủ trong các giao thức dựa trên SSL khác nhau như FTPS, IMAPS, POPS và các giao thức khác
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3505 được nâng lên khi thất bại. Khi thành công, chức năng không trả về gì
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
Mới trong phiên bản 3. 2
Đã thay đổi trong phiên bản 3. 3. 3. Chức năng hiện tuân theo RFC 6125, phần 6. 4. 3 và không khớp với nhiều ký tự đại diện [e. g.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3506 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3507] hoặc ký tự đại diện bên trong đoạn tên miền quốc tế hóa [IDN]. Các nhãn IDN A như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3508 vẫn được hỗ trợ, nhưng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3509 không còn khớp với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3510.
Đã thay đổi trong phiên bản 3. 5. Tính năng khớp địa chỉ IP, khi xuất hiện trong trường subjectAltName của chứng chỉ, hiện đã được hỗ trợ.
Đã thay đổi trong phiên bản 3. 7. Chức năng này không còn được sử dụng cho các kết nối TLS. Kết hợp tên máy chủ hiện được thực hiện bởi OpenSSL.
Cho phép ký tự đại diện khi nó ở ngoài cùng bên trái và là ký tự duy nhất trong phân đoạn đó. Các ký tự đại diện một phần như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3511 không còn được hỗ trợ
Không dùng nữa kể từ phiên bản 3. 7
ssl. cert_time_to_seconds[cert_time] ¶Trả về thời gian tính bằng giây kể từ Epoch, được cung cấp chuỗi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3512 đại diện cho ngày “not Before” hoặc “not After” từ chứng chỉ ở định dạng strptime
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3513 [ngôn ngữ C]
Đây là một ví dụ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv31
Ngày “not Before” hoặc “not After” phải sử dụng GMT [RFC 5280]
Đã thay đổi trong phiên bản 3. 5. Hiểu thời gian đầu vào là thời gian tính theo UTC như được chỉ định bởi múi giờ ‘GMT’ trong chuỗi đầu vào. Múi giờ địa phương đã được sử dụng trước đây. Trả về một số nguyên [không có phân số của giây ở định dạng đầu vào]
ssl. get_server_certificate[addr , ssl_version=PROTOCOL_TLS_CLIENT , ca_certs=None[ , timeout]] ¶Cung cấp địa chỉ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3514 của máy chủ được bảo vệ SSL, dưới dạng một cặp [tên máy chủ, số cổng], tìm nạp chứng chỉ của máy chủ và trả về dưới dạng chuỗi được mã hóa PEM. If
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3515 is specified, uses that version of the SSL protocol to attempt to connect to the server. Nếu
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3516 được chỉ định, thì đó phải là một tệp chứa danh sách các chứng chỉ gốc, cùng định dạng như được sử dụng cho cùng một tham số trong
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4. Cuộc gọi sẽ cố gắng xác thực chứng chỉ máy chủ dựa trên bộ chứng chỉ gốc đó và sẽ không thành công nếu nỗ lực xác thực không thành công. Thời gian chờ có thể được chỉ định với tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3518
Đã thay đổi trong phiên bản 3. 3. Chức năng này hiện tương thích với IPv6.
Đã thay đổi trong phiên bản 3. 5. Ssl_version mặc định được thay đổi từ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3519 thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13 để tương thích tối đa với các máy chủ hiện đại.
Đã thay đổi trong phiên bản 3. 10. Tham số thời gian chờ đã được thêm vào.
ssl. DER_cert_to_PEM_cert[DER_cert_byte] ¶Đưa ra một chứng chỉ dưới dạng một đốm byte được mã hóa DER, trả về một phiên bản chuỗi được mã hóa PEM của cùng một chứng chỉ
ssl. PEM_cert_to_DER_cert[PEM_cert_string] ¶Cấp chứng chỉ dưới dạng chuỗi ASCII PEM, trả về chuỗi byte được mã hóa DER cho cùng chứng chỉ đó
ssl. get_default_verify_paths[] ¶Trả về một bộ được đặt tên với các đường dẫn đến cafile và capath mặc định của OpenSSL. Các đường dẫn giống như được sử dụng bởi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3521. Giá trị trả về là một bộ có tên
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3522.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
523 - đường dẫn đến cafile đã giải quyết hoặcctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
15 nếu tệp không tồn tại,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
525 - giải quyết đường dẫn đến capath hoặcctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
15 nếu thư mục không tồn tại,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
527 - Khóa môi trường của OpenSSL trỏ đến cafile,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
528 - đường dẫn được mã hóa cứng đến cafile,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
529 - Khóa môi trường của OpenSSL trỏ đến capath,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
530 - đường dẫn được mã hóa cứng đến thư mục capath
Mới trong phiên bản 3. 4
ssl. enum_certificates[store_name] ¶Truy xuất chứng chỉ từ cửa hàng chứng chỉ hệ thống của Windows. store_name có thể là một trong số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3531,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3532 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3533. Windows cũng có thể cung cấp các cửa hàng chứng chỉ bổ sung
Hàm trả về một danh sách các bộ dữ liệu [cert_bytes, encoding_type, trust]. encoding_type chỉ định mã hóa của cert_bytes. Nó là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3534 cho X. 509 ASN. 1 dữ liệu hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3535 cho PKCS#7 ASN. 1 dữ liệu. Trust chỉ định mục đích của chứng chỉ dưới dạng một bộ OIDS hoặc chính xác là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 nếu chứng chỉ đáng tin cậy cho mọi mục đích
Thí dụ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv37
Tính khả dụng . các cửa sổ.
Mới trong phiên bản 3. 4
ssl. enum_crls[store_name] ¶Truy xuất CRL từ cửa hàng chứng chỉ hệ thống của Windows. store_name có thể là một trong số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3531,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3532 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3533. Windows cũng có thể cung cấp các cửa hàng chứng chỉ bổ sung
Hàm trả về một danh sách các bộ dữ liệu [cert_bytes, encoding_type, trust]. encoding_type chỉ định mã hóa của cert_bytes. Nó là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3534 cho X. 509 ASN. 1 dữ liệu hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3535 cho PKCS#7 ASN. 1 dữ liệu
Tính khả dụng . các cửa sổ.
Mới trong phiên bản 3. 4
ssl. wrap_socket[sock , tệp khóa=None, certfile=None, server_side=False, cert_reqs=CERT_NONE, ssl_version=PROTOCOL_TLS, ca_certs=None, do_handshake_on_connect=True, suppress_ragged_eofs=True, ciphers=None]¶Lấy một phiên bản
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3542 của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'0 và trả về một phiên bản của
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv39, một kiểu con của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'0, bao bọc ổ cắm bên dưới trong ngữ cảnh SSL.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3542 phải là ổ cắm
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3547;
Bên trong, chức năng tạo một
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 với giao thức ssl_version và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3549 được đặt thành cert_reqs. Nếu các tham số keyfile, certfile, ca_certs hoặc ciphers được đặt thì các giá trị được chuyển đến
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3550,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3552
Các đối số server_side, do_handshake_on_connect, và booster_ragged_eofs có cùng ý nghĩa như
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4
Không dùng nữa kể từ phiên bản 3. 7. Kể từ Python 3. 2 và 2. 7. 9, nên sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 thay vì
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9. Chức năng cấp cao nhất bị hạn chế và tạo ra một ổ cắm máy khách không an toàn mà không có dấu hiệu tên máy chủ hoặc tên máy chủ phù hợp.
Hằng số¶
ssl. CERT_NONE ¶Tất cả các hằng số hiện là bộ sưu tập
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3556 hoặcctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3557Mới trong phiên bản 3. 6
Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3558 hoặc tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3559 thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9. Ngoại trừ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316, nó là chế độ mặc định. Với các ổ cắm phía máy khách, hầu như mọi chứng chỉ đều được chấp nhận. Các lỗi xác thực, chẳng hạn như chứng chỉ không đáng tin cậy hoặc hết hạn, sẽ bị bỏ qua và không hủy bỏ quá trình bắt tay TLS/SSL
Ở chế độ máy chủ, không có chứng chỉ nào được yêu cầu từ máy khách, vì vậy máy khách không gửi bất kỳ chứng chỉ nào để xác thực chứng chỉ ứng dụng khách
Xem thảo luận về Cân nhắc về bảo mật bên dưới.
ssl. CERT_OPTIONAL ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3558 hoặc tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3559 thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9. Ở chế độ máy khách,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3565 có ý nghĩa tương tự như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372. Thay vào đó, nên sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 cho các ổ cắm phía máy khách
Ở chế độ máy chủ, yêu cầu chứng chỉ ứng dụng khách được gửi đến ứng dụng khách. Máy khách có thể bỏ qua yêu cầu hoặc gửi chứng chỉ để thực hiện xác thực chứng chỉ máy khách TLS. Nếu khách hàng chọn gửi chứng chỉ, nó đã được xác minh. Bất kỳ lỗi xác minh nào sẽ hủy bỏ bắt tay TLS ngay lập tức
Việc sử dụng cài đặt này yêu cầu phải chuyển một bộ chứng chỉ CA hợp lệ, cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314 hoặc dưới dạng giá trị của tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3516 cho
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9ssl. CERT_REQUIRED ¶
Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3558 hoặc tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3559 thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9. Trong chế độ này, các chứng chỉ được yêu cầu từ phía bên kia của kết nối ổ cắm; . Chế độ này không đủ để xác minh chứng chỉ ở chế độ máy khách vì nó không khớp với tên máy chủ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 cũng phải được bật để xác minh tính xác thực của chứng chỉ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 và bật
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 theo mặc định
Với ổ cắm máy chủ, chế độ này cung cấp xác thực chứng chỉ ứng dụng khách TLS bắt buộc. Yêu cầu chứng chỉ ứng dụng khách được gửi đến ứng dụng khách và ứng dụng khách phải cung cấp chứng chỉ hợp lệ và đáng tin cậy
Việc sử dụng cài đặt này yêu cầu phải chuyển một bộ chứng chỉ CA hợp lệ, cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314 hoặc dưới dạng giá trị của tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3516 cho
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9lớp ssl. Chế độ xác minh ¶
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3556 bộ sưu tập các hằng số CERT_*
Mới trong phiên bản 3. 6
ssl. VERIFY_DEFAULT ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. Trong chế độ này, danh sách thu hồi chứng chỉ [CRL] không được chọn. Theo mặc định, OpenSSL không yêu cầu cũng như không xác minh CRL
Mới trong phiên bản 3. 4
ssl. VERIFY_CRL_CHECK_LEAF ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. Trong chế độ này, chỉ có chứng chỉ ngang hàng được kiểm tra nhưng không có chứng chỉ CA trung gian nào được kiểm tra. Chế độ yêu cầu CRL hợp lệ được ký bởi nhà phát hành chứng chỉ ngang hàng [CA tổ tiên trực tiếp của nó]. Nếu không có CRL thích hợp nào được tải với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3585, quá trình xác thực sẽ không thành công
Mới trong phiên bản 3. 4
ssl. VERIFY_CRL_CHECK_CHAIN ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. Ở chế độ này, CRL của tất cả các chứng chỉ trong chuỗi chứng chỉ ngang hàng đều được kiểm tra
Mới trong phiên bản 3. 4
ssl. VERIFY_X509_STRICT ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583 để vô hiệu hóa cách giải quyết cho X bị hỏng. 509 giấy chứng nhận
Mới trong phiên bản 3. 4
ssl. VERIFY_ALLOW_PROXY_CERTS ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583 để cho phép xác minh chứng chỉ proxy
Mới trong phiên bản 3. 10
ssl. VERIFY_X509_TRUSTED_FIRST ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. Nó hướng dẫn OpenSSL ưu tiên các chứng chỉ đáng tin cậy khi xây dựng chuỗi tin cậy để xác thực chứng chỉ. Cờ này được bật theo mặc định
Mới trong phiên bản 3. 4. 4
ssl. VERIFY_X509_PARTIAL_CHAIN ¶Giá trị có thể có cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. Nó hướng dẫn OpenSSL chấp nhận các CA trung gian trong cửa hàng ủy thác để được coi là neo tin cậy, giống như cách chứng chỉ CA gốc tự ký. Điều này giúp có thể tin cậy các chứng chỉ do CA trung gian cấp mà không cần phải tin tưởng vào CA gốc tổ tiên của nó
Mới trong phiên bản 3. 10
lớp ssl. Xác minh cờ ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3557 bộ sưu tập các hằng số VERIFY_*
Mới trong phiên bản 3. 6
ssl. PROTOCOL_TLS ¶Chọn phiên bản giao thức cao nhất mà cả máy khách và máy chủ đều hỗ trợ. Mặc dù tên, tùy chọn này có thể chọn cả giao thức “SSL” và “TLS”
Mới trong phiên bản 3. 6
Không dùng nữa kể từ phiên bản 3. 10. Máy khách và máy chủ TLS yêu cầu các cài đặt mặc định khác nhau để liên lạc an toàn. Hằng số giao thức TLS chung không được dùng để thay thế cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317. ssl. PROTOCOL_TLS_CLIENT ¶
Tự động đàm phán phiên bản giao thức cao nhất mà cả máy khách và máy chủ đều hỗ trợ, đồng thời định cấu hình các kết nối phía máy khách theo ngữ cảnh. Giao thức cho phép
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 theo mặc định
Mới trong phiên bản 3. 6
ssl. PROTOCOL_TLS_SERVER ¶Tự động đàm phán phiên bản giao thức cao nhất mà cả máy khách và máy chủ đều hỗ trợ, đồng thời định cấu hình các kết nối phía máy chủ ngữ cảnh
Mới trong phiên bản 3. 6
ssl. PROTOCOL_SSLv23 ¶Bí danh cho
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13
Không dùng nữa kể từ phiên bản 3. 6. Sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13 thay thế. ssl. PROTOCOL_SSLv2 ¶
Chọn SSL phiên bản 2 làm giao thức mã hóa kênh
Giao thức này không khả dụng nếu OpenSSL được biên dịch với tùy chọn
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3598
Warning
SSL phiên bản 2 không an toàn. Việc sử dụng nó rất không được khuyến khích
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã xóa hỗ trợ cho SSLv2.
ssl. PROTOCOL_SSLv3 ¶Chọn SSL phiên bản 3 làm giao thức mã hóa kênh
Giao thức này không khả dụng nếu OpenSSL được biên dịch với tùy chọn
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3599
Warning
SSL phiên bản 3 không an toàn. Việc sử dụng nó rất không được khuyến khích
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã ngừng sử dụng tất cả các giao thức dành riêng cho phiên bản. Thay vào đó, hãy sử dụng giao thức mặc định
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603. ssl. PROTOCOL_TLSv1 ¶
Chọn phiên bản TLS 1. 0 làm giao thức mã hóa kênh
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã ngừng sử dụng tất cả các giao thức dành riêng cho phiên bản.
ssl. PROTOCOL_TLSv1_1 ¶Chọn phiên bản TLS 1. 1 làm giao thức mã hóa kênh. Chỉ khả dụng với openssl phiên bản 1. 0. 1+
Mới trong phiên bản 3. 4
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã ngừng sử dụng tất cả các giao thức dành riêng cho phiên bản.
ssl. PROTOCOL_TLSv1_2 ¶Chọn phiên bản TLS 1. 2 làm giao thức mã hóa kênh. Chỉ khả dụng với openssl phiên bản 1. 0. 1+
Mới trong phiên bản 3. 4
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã ngừng sử dụng tất cả các giao thức dành riêng cho phiên bản.
ssl. OP_ALL ¶Cho phép giải pháp thay thế cho các lỗi khác nhau có trong các triển khai SSL khác. Tùy chọn này được đặt theo mặc định. Nó không nhất thiết phải đặt các cờ giống như hằng số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3604 của OpenSSL
Mới trong phiên bản 3. 2
ssl. OP_NO_SSLv2 ¶Ngăn kết nối SSLv2. Tùy chọn này chỉ được áp dụng cùng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. Nó ngăn các đồng nghiệp chọn SSLv2 làm phiên bản giao thức
Mới trong phiên bản 3. 2
Không dùng nữa kể từ phiên bản 3. 6. SSLv2 không được dùng nữa
ssl. OP_NO_SSLv3 ¶Ngăn chặn kết nối SSLv3. Tùy chọn này chỉ được áp dụng cùng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. Nó ngăn các đồng nghiệp chọn SSLv3 làm phiên bản giao thức
Mới trong phiên bản 3. 2
Không dùng nữa kể từ phiên bản 3. 6. SSLv3 không được dùng nữa
ssl. OP_NO_TLSv1 ¶Ngăn kết nối TLSv1. Tùy chọn này chỉ được áp dụng cùng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. Nó ngăn các đồng nghiệp chọn TLSv1 làm phiên bản giao thức
Mới trong phiên bản 3. 2
Không dùng nữa kể từ phiên bản 3. 7. Tùy chọn này không được dùng nữa kể từ OpenSSL 1. 1. 0, hãy sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 mới để thay thế. ssl. OP_NO_TLSv1_1 ¶
Ngăn chặn TLSv1. 1 kết nối. Tùy chọn này chỉ được áp dụng cùng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. Nó ngăn các đồng nghiệp chọn TLSv1. 1 là phiên bản giao thức. Chỉ khả dụng với openssl phiên bản 1. 0. 1+
Mới trong phiên bản 3. 4
Không dùng nữa kể từ phiên bản 3. 7. The option is deprecated since OpenSSL 1. 1. 0.
ssl. OP_NO_TLSv1_2 ¶Ngăn chặn TLSv1. 2 kết nối. Tùy chọn này chỉ được áp dụng cùng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. It prevents the peers from choosing TLSv1. 2 là phiên bản giao thức. Chỉ khả dụng với openssl phiên bản 1. 0. 1+
Mới trong phiên bản 3. 4
Không dùng nữa kể từ phiên bản 3. 7. The option is deprecated since OpenSSL 1. 1. 0.
ssl. OP_NO_TLSv1_3 ¶Prevents a TLSv1. 3 connection. This option is only applicable in conjunction with
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13. It prevents the peers from choosing TLSv1. 3 as the protocol version. TLS 1. 3 is available with OpenSSL 1. 1. 1 or later. When Python has been compiled against an older version of OpenSSL, the flag defaults to 0
New in version 3. 7
Deprecated since version 3. 7. The option is deprecated since OpenSSL 1. 1. 0. It was added to 2. 7. 15, 3. 6. 3 and 3. 7. 0 for backwards compatibility with OpenSSL 1. 0. 2.
ssl. OP_NO_RENEGOTIATION ¶Disable all renegotiation in TLSv1. 2 and earlier. Do not send HelloRequest messages, and ignore renegotiation requests via ClientHello
This option is only available with OpenSSL 1. 1. 0h and later
New in version 3. 7
ssl. OP_CIPHER_SERVER_PREFERENCE ¶Use the server’s cipher ordering preference, rather than the client’s. This option has no effect on client sockets and SSLv2 server sockets
New in version 3. 3
ssl. OP_SINGLE_DH_USE ¶Prevents re-use of the same DH key for distinct SSL sessions. This improves forward secrecy but requires more computational resources. This option only applies to server sockets
New in version 3. 3
ssl. OP_SINGLE_ECDH_USE ¶Prevents re-use of the same ECDH key for distinct SSL sessions. This improves forward secrecy but requires more computational resources. This option only applies to server sockets
New in version 3. 3
ssl. OP_ENABLE_MIDDLEBOX_COMPAT ¶Send dummy Change Cipher Spec [CCS] messages in TLS 1. 3 handshake to make a TLS 1. 3 connection look more like a TLS 1. 2 connection
This option is only available with OpenSSL 1. 1. 1 and later
New in version 3. 8
ssl. OP_NO_COMPRESSION ¶Disable compression on the SSL channel. This is useful if the application protocol supports its own compression scheme
New in version 3. 3
class ssl. Options ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3557 collection of OP_* constantsssl. OP_NO_TICKET ¶
Ngăn phía khách hàng yêu cầu vé phiên
Mới trong phiên bản 3. 6
ssl. OP_IGNORE_UNEXPECTED_EOF ¶Ignore unexpected shutdown of TLS connections
This option is only available with OpenSSL 3. 0. 0 and later
Mới trong phiên bản 3. 10
ssl. HAS_ALPN ¶Whether the OpenSSL library has built-in support for the Application-Layer Protocol Negotiation TLS extension as described in RFC 7301
New in version 3. 5
ssl. HAS_NEVER_CHECK_COMMON_NAME ¶Whether the OpenSSL library has built-in support not checking subject common name and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3614 is writeable
New in version 3. 7
ssl. HAS_ECDH ¶Liệu thư viện OpenSSL có hỗ trợ tích hợp sẵn cho trao đổi khóa Diffie-Hellman dựa trên Đường cong Elliptic hay không. Điều này phải đúng trừ khi tính năng này bị vô hiệu hóa rõ ràng bởi nhà phân phối
New in version 3. 3
ssl. HAS_SNI ¶Liệu thư viện OpenSSL có hỗ trợ tích hợp sẵn cho phần mở rộng Chỉ định tên máy chủ hay không [như được định nghĩa trong RFC 6066]
Mới trong phiên bản 3. 2
ssl. HAS_NPN ¶Thư viện OpenSSL có hỗ trợ tích hợp sẵn cho Đàm phán giao thức tiếp theo như được mô tả trong Đàm phán giao thức lớp ứng dụng hay không. Khi đúng, bạn có thể sử dụng phương pháp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3615 để quảng cáo giao thức nào bạn muốn hỗ trợ
New in version 3. 3
ssl. HAS_SSLv2 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho SSL 2 hay không. 0 giao thức
New in version 3. 7
ssl. HAS_SSLv3 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho SSL 3 hay không. 0 giao thức
New in version 3. 7
ssl. HAS_TLSv1 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho TLS 1 hay không. 0 giao thức
New in version 3. 7
ssl. HAS_TLSv1_1 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho TLS 1 hay không. 1 giao thức
New in version 3. 7
ssl. HAS_TLSv1_2 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho TLS 1 hay không. 2 giao thức
New in version 3. 7
ssl. HAS_TLSv1_3 ¶Thư viện OpenSSL có hỗ trợ tích hợp cho TLS 1 hay không. 3 protocol
New in version 3. 7
ssl. CHANNEL_BINDING_TYPES ¶Danh sách các loại liên kết kênh TLS được hỗ trợ. Các chuỗi trong danh sách này có thể được sử dụng làm đối số cho
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3616
New in version 3. 3
ssl. OPENSSL_VERSION ¶Chuỗi phiên bản của thư viện OpenSSL được trình thông dịch tải
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'1
Mới trong phiên bản 3. 2
ssl. OPENSSL_VERSION_INFO ¶A tuple of five integers representing version information about the OpenSSL library
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8
Mới trong phiên bản 3. 2
ssl. OPENSSL_VERSION_NUMBER ¶The raw version number of the OpenSSL library, as a single integer
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9
Mới trong phiên bản 3. 2
ssl. ALERT_DESCRIPTION_HANDSHAKE_FAILURE ¶ ssl. ALERT_DESCRIPTION_INTERNAL_ERROR ¶ ALERT_DESCRIPTION_*Alert Descriptions from RFC 5246 and others. The IANA TLS Alert Registry contains this list and references to the RFCs where their meaning is defined
Used as the return value of the callback function in
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3617
Mới trong phiên bản 3. 4
class ssl. AlertDescription ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3556 collection of ALERT_DESCRIPTION_* constants
Mới trong phiên bản 3. 6
Purpose. SERVER_AUTH ¶Option for
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373. This value indicates that the context may be used to authenticate web servers [therefore, it will be used to create client-side sockets]
Mới trong phiên bản 3. 4
Purpose. CLIENT_AUTH ¶Option for
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373. This value indicates that the context may be used to authenticate web clients [therefore, it will be used to create server-side sockets]
Mới trong phiên bản 3. 4
class ssl. SSLErrorNumber ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3556 collection of SSL_ERROR_* constants
Mới trong phiên bản 3. 6
class ssl. TLSVersion ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3556 collection of SSL and TLS versions for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602
New in version 3. 7
TLSVersion. MINIMUM_SUPPORTED ¶ TLSVersion. MAXIMUM_SUPPORTED ¶The minimum or maximum supported SSL or TLS version. These are magic constants. Their values don’t reflect the lowest and highest available TLS/SSL versions
TLSVersion. SSLv3 ¶ TLSVersion. TLSv1 ¶ TLSVersion. TLSv1_1 ¶ TLSVersion. TLSv1_2 ¶ TLSVersion. TLSv1_3 ¶SSL 3. 0 to TLS 1. 3
Không dùng nữa kể từ phiên bản 3. 10. All
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3627 members except
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3628 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3629 are deprecated.
SSL Sockets¶
class ssl. SSLSocket[socket. socket] ¶SSL sockets provide the following methods of Socket Objects .
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
630ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
631ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
632ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
633ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
634ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
635ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
636,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
637ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
638,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
639ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
640,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
641,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
642ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
643ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
644ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
645,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
646 [but passing a non-zeroctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
647 argument is not allowed]ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
648,ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
649 [with the same limitation]ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
650 [butctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
651 will be used for plain-text sockets only, elsectx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
648 will be used]ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
653
However, since the SSL [and TLS] protocol has its own framing atop of TCP, the SSL sockets abstraction can, in certain respects, diverge from the specification of normal, OS-level sockets. See especially the notes on non-blocking sockets .
Instances of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 must be created using the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 method
Changed in version 3. 5. Phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3650 đã được thêm vào.
Changed in version 3. 5. The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3653 does not reset the socket timeout each time bytes are received or sent. The socket timeout is now to maximum total duration of the shutdown.
Deprecated since version 3. 6. It is deprecated to create a
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 instance directly, use
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 to wrap a socket.
Changed in version 3. 7.
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 instances must to created with
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9. In earlier versions, it was possible to create instances directly. This was never documented or officially supported.
Changed in version 3. 10. Python now uses
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3662 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3663 internally. The functions support reading and writing of data larger than 2 GB. Writing zero-length data no longer fails with a protocol violation error.
SSL sockets also have the following additional methods and attributes
SSLSocket. read[len=1024 , buffer=None] ¶Read up to len bytes of data from the SSL socket and return the result as a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3664 instance. If buffer is specified, then read into the buffer instead, and return the number of bytes read
Raise
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3665 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3666 if the socket is non-blocking and the read would block.
As at any time a re-negotiation is possible, a call to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3667 can also cause write operations
Changed in version 3. 5. The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration to read up to len bytes.
Deprecated since version 3. 6. Sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3645 thay vì
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3667. SSLSocket. write[buf] ¶
Write buf to the SSL socket and return the number of bytes written. The buf argument must be an object supporting the buffer interface
Nâng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3665 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3666 nếu ổ cắm không chặn và việc ghi sẽ chặn.
As at any time a re-negotiation is possible, a call to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672 can also cause read operations
Changed in version 3. 5. The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration to write buf.
Deprecated since version 3. 6. Use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3648 instead of
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672.
Note
The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3667 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672 methods are the low-level methods that read and write unencrypted, application-level data and decrypt/encrypt it to encrypted, wire-level data. These methods require an active SSL connection, i. e. the handshake was completed and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3677 was not called
Normally you should use the socket API methods like
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3645 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3648 instead of these methodsSSLSocket. do_handshake[] ¶
Perform the SSL setup handshake
Đã thay đổi trong phiên bản 3. 4. The handshake method also performs
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680 when the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 attribute of the socket’s
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3682 is true.
Changed in version 3. 5. The socket timeout is no more reset each time bytes are received or sent. The socket timeout is now to maximum total duration of the handshake.
Changed in version 3. 7. Hostname or IP address is matched by OpenSSL during handshake. The function
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680 is no longer used. In case OpenSSL refuses a hostname or IP address, the handshake is aborted early and a TLS alert message is send to the peer. SSLSocket. getpeercert[binary_form=False] ¶
If there is no certificate for the peer on the other end of the connection, return
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315. If the SSL handshake hasn’t been done yet, raise
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685
If the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3686 parameter is
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500, and a certificate was received from the peer, this method returns a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3688 instance. If the certificate was not validated, the dict is empty. If the certificate was validated, it returns a dict with several keys, amongst them
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3689 [the principal for which the certificate was issued] and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3690 [the principal issuing the certificate]. If a certificate contains an instance of the Subject Alternative Name extension [see RFC 3280], there will also be a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3691 key in the dictionary
The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3689 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3690 fields are tuples containing the sequence of relative distinguished names [RDNs] given in the certificate’s data structure for the respective fields, and each RDN is a sequence of name-value pairs. Here is a real-world example
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv350
Note
To validate a certificate for a particular service, you can use the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680 function
If the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3686 parameter is
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94, and a certificate was provided, this method returns the DER-encoded form of the entire certificate as a sequence of bytes, or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 if the peer did not provide a certificate. Whether the peer provides a certificate depends on the SSL socket’s role
for a client SSL socket, the server will always provide a certificate, regardless of whether validation was required;
for a server SSL socket, the client will only provide a certificate when requested by the server; therefore
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1 will returnctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
15 if you usedctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
00 [rather thanctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
565 orctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
72]
Changed in version 3. 2. The returned dictionary includes additional items such as
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3690 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv304.
Changed in version 3. 4.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685 is raised when the handshake isn’t done. The returned dictionary includes additional X509v3 extension items such as
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv306,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv307 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv308 URIs.
Changed in version 3. 9. IPv6 address strings no longer have a trailing new line.
SSLSocket. cipher[] ¶Returns a three-value tuple containing the name of the cipher being used, the version of the SSL protocol that defines its use, and the number of secret bits being used. If no connection has been established, returns
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315SSLSocket. shared_ciphers[] ¶
Return the list of ciphers shared by the client during the handshake. Each entry of the returned list is a three-value tuple containing the name of the cipher, the version of the SSL protocol that defines its use, and the number of secret bits the cipher uses.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv310 returns
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 if no connection has been established or the socket is a client socket
New in version 3. 5
SSLSocket. compression[] ¶Return the compression algorithm being used as a string, or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 if the connection isn’t compressed
If the higher-level protocol supports its own compression mechanism, you can use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv313 to disable SSL-level compression
New in version 3. 3
SSLSocket. get_channel_binding[cb_type='tls-unique'] ¶Get channel binding data for current connection, as a bytes object. Returns
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 if not connected or the handshake has not been completed
The cb_type parameter allow selection of the desired channel binding type. Valid channel binding types are listed in the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 list. Hiện tại chỉ hỗ trợ liên kết kênh 'tls-unique', được xác định bởi RFC 5929.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685 will be raised if an unsupported channel binding type is requested
New in version 3. 3
SSLSocket. selected_alpn_protocol[] ¶Return the protocol that was selected during the TLS handshake. If
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 was not called, if the other party does not support ALPN, if this socket does not support any of the client’s proposed protocols, or if the handshake has not happened yet,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 is returned
New in version 3. 5
SSLSocket. selected_npn_protocol[] ¶Return the higher-level protocol that was selected during the TLS/SSL handshake. If
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3615 was not called, or if the other party does not support NPN, or if the handshake has not yet happened, this will return
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315
New in version 3. 3
Deprecated since version 3. 10. NPN has been superseded by ALPN
SSLSocket. unwrap[] ¶Performs the SSL shutdown handshake, which removes the TLS layer from the underlying socket, and returns the underlying socket object. This can be used to go from encrypted operation over a connection to unencrypted. The returned socket should always be used for further communication with the other side of the connection, rather than the original socket
SSLSocket. verify_client_post_handshake[] ¶Requests post-handshake authentication [PHA] from a TLS 1. 3 client. PHA can only be initiated for a TLS 1. 3 connection from a server-side socket, after the initial TLS handshake and with PHA enabled on both sides, see
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv321
The method does not perform a cert exchange immediately. The server-side sends a CertificateRequest during the next write event and expects the client to respond with a certificate on the next read event
If any precondition isn’t met [e. g. not TLS 1. 3, PHA not enabled], an
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 is raised
Note
Only available with OpenSSL 1. 1. 1 and TLS 1. 3 enabled. Without TLS 1. 3 support, the method raises
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv323
New in version 3. 8
SSLSocket. version[] ¶Return the actual SSL protocol version negotiated by the connection as a string, or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 if no secure connection is established. As of this writing, possible return values include
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv325,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv326,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv327,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv328 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv329. Recent OpenSSL versions may define more return values
New in version 3. 5
SSLSocket. pending[] ¶Returns the number of already decrypted bytes available for read, pending on the connection
SLSỔ cắm. context ¶The
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 object this SSL socket is tied to. Nếu ổ cắm SSL được tạo bằng hàm
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9 không dùng nữa [chứ không phải
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4], thì đây là một đối tượng ngữ cảnh tùy chỉnh được tạo cho ổ cắm SSL này
Mới trong phiên bản 3. 2
SSLSocket. server_side ¶Một boolean là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 cho ổ cắm phía máy chủ và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500 cho ổ cắm phía máy khách
Mới trong phiên bản 3. 2
SLSỔ cắm. server_hostname ¶Tên máy chủ của máy chủ. Loại
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv335 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 cho ổ cắm phía máy chủ hoặc nếu tên máy chủ không được chỉ định trong hàm tạo
Mới trong phiên bản 3. 2
Đã thay đổi trong phiên bản 3. 7. Thuộc tính bây giờ luôn là văn bản ASCII. Khi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv337 là tên miền quốc tế hóa [IDN], thuộc tính này hiện lưu trữ dạng nhãn A [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv338], thay vì dạng nhãn U [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv339]. SLSỔ cắm. phiên ¶
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv340 cho kết nối SSL này. Phiên khả dụng cho ổ cắm phía máy khách và máy chủ sau khi bắt tay TLS đã được thực hiện. Đối với ổ cắm máy khách, phiên có thể được đặt trước khi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv341 được gọi để sử dụng lại phiên
Mới trong phiên bản 3. 6
SLSỔ cắm. session_reused ¶Mới trong phiên bản 3. 6
Bối cảnh SSL¶
Mới trong phiên bản 3. 2
Bối cảnh SSL chứa nhiều dữ liệu tồn tại lâu hơn so với các kết nối SSL đơn lẻ, chẳng hạn như tùy chọn cấu hình SSL, [các] chứng chỉ và [các] khóa riêng tư. Nó cũng quản lý bộ đệm của các phiên SSL cho ổ cắm phía máy chủ, để tăng tốc các kết nối lặp lại từ cùng một máy khách
lớp ssl. SSLContext[giao thức=Không có]¶Tạo ngữ cảnh SSL mới. Bạn có thể vượt qua giao thức phải là một trong các hằng số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv342 được xác định trong mô-đun này. Tham số chỉ định phiên bản nào của giao thức SSL sẽ sử dụng. Thông thường, máy chủ chọn một phiên bản giao thức cụ thể và máy khách phải thích ứng với lựa chọn của máy chủ. Hầu hết các phiên bản không tương thích với các phiên bản khác. Nếu không được chỉ định, giá trị mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13;
Đây là bảng hiển thị phiên bản nào trong máy khách [phía dưới] có thể kết nối với phiên bản nào trong máy chủ [dọc trên cùng]
máy khách / máy chủ
SSLv2
SSLv3
TLS 3
TLSv1
TLSv1. 1
TLSv1. 2
SSLv2
Vâng
không
không 1
không
không
không
SSLv3
không
Vâng
số 2
không
không
không
TLS [SSLv23] 3
không 1
số 2
Vâng
Vâng
Vâng
Vâng
TLSv1
không
không
Vâng
Vâng
không
không
TLSv1. 1
không
không
Vâng
không
Vâng
không
TLSv1. 2
không
không
Vâng
không
không
Vâng
chú thích
1[1,2]>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 tắt SSLv2 với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 theo mặc định2[1,2]
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 tắt SSLv3 với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 theo mặc định3[1,2]
TLS 1. 3 sẽ khả dụng với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13 trong OpenSSL >= 1. 1. 1. Không có hằng số PROTOCOL dành riêng cho chỉ TLS 1. 3
Xem thêm
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 cho phép mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35 chọn cài đặt bảo mật cho một mục đích nhất định
Đã thay đổi trong phiên bản 3. 6. Bối cảnh được tạo với các giá trị mặc định an toàn. Các tùy chọn
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv313,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv352, ________ 053, ________ 054, ________ 218 [ngoại trừ ________ 056] và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 [ngoại trừ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3519] được đặt theo mặc định. Danh sách bộ mật mã ban đầu chỉ chứa các mật mã
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv359, không có mật mã
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv360 và không có mật mã
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv361 [ngoại trừ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv356].
Không dùng nữa kể từ phiên bản 3. 10. ______46 không có đối số giao thức không được dùng nữa. Lớp ngữ cảnh sẽ yêu cầu giao thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 trong tương lai.
Đã thay đổi trong phiên bản 3. 10. Bộ mật mã mặc định hiện chỉ bao gồm các mật mã AES và ChaCha20 an toàn với chuyển tiếp bí mật và bảo mật cấp 2. Khóa RSA và DH có ít hơn 2048 bit và khóa ECC có ít hơn 224 bit đều bị cấm.
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 sử dụng TLS 1. 2 là phiên bản TLS tối thiểu.
Các đối tượng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 có các phương thức và thuộc tính sauSSLContext. cert_store_stats[] ¶
Lấy thống kê về số lượng X đã nạp. 509 chứng chỉ, số lượng X. 509 chứng chỉ được gắn cờ là chứng chỉ CA và danh sách thu hồi chứng chỉ là từ điển
Ví dụ về bối cảnh có một chứng chỉ CA và một chứng chỉ khác
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv351
Mới trong phiên bản 3. 4
SSLContext. load_cert_chain[certfile , keyfile=None, password=None]¶Tải khóa cá nhân và chứng chỉ tương ứng. Chuỗi certfile phải là đường dẫn đến một tệp duy nhất ở định dạng PEM chứa chứng chỉ cũng như bất kỳ số lượng chứng chỉ CA nào cần thiết để thiết lập tính xác thực của chứng chỉ. Chuỗi tệp khóa, nếu có, phải trỏ đến tệp chứa khóa riêng. Nếu không, khóa riêng cũng sẽ được lấy từ certfile. Xem phần thảo luận về Chứng chỉ để biết thêm thông tin về cách lưu trữ chứng chỉ trong tệp chứng chỉ.
Đối số mật khẩu có thể là một chức năng để gọi để lấy mật khẩu để giải mã khóa riêng. Nó sẽ chỉ được gọi nếu khóa riêng được mã hóa và cần có mật khẩu. Nó sẽ được gọi mà không có đối số và nó sẽ trả về một chuỗi, byte hoặc bytearray. Nếu giá trị trả về là một chuỗi, nó sẽ được mã hóa dưới dạng UTF-8 trước khi sử dụng nó để giải mã khóa. Ngoài ra, một giá trị chuỗi, byte hoặc bytearray có thể được cung cấp trực tiếp dưới dạng đối số mật khẩu. Nó sẽ bị bỏ qua nếu khóa riêng không được mã hóa và không cần mật khẩu
Nếu đối số mật khẩu không được chỉ định và mật khẩu được yêu cầu, cơ chế nhắc mật khẩu tích hợp sẵn của OpenSSL sẽ được sử dụng để nhắc người dùng nhập mật khẩu một cách tương tác
Một
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 được nâng lên nếu khóa riêng không khớp với chứng chỉ
Đã thay đổi trong phiên bản 3. 3. Mật khẩu đối số tùy chọn mới.
SSLContext. load_default_certs[mục đích=Mục đích. SERVER_AUTH] ¶Tải một bộ chứng chỉ “cơ quan cấp chứng chỉ” [CA] mặc định từ các vị trí mặc định. Trên Windows, nó tải các chứng chỉ CA từ các cửa hàng hệ thống
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3531 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3532. Trên tất cả các hệ thống, nó gọi là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3521. Trong tương lai, phương thức này cũng có thể tải chứng chỉ CA từ các vị trí khác
Cờ mục đích chỉ định loại chứng chỉ CA nào được tải. Cài đặt mặc định
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv374 tải chứng chỉ, được gắn cờ và tin cậy để xác thực máy chủ web TLS [ổ cắm phía máy khách].
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv375 tải chứng chỉ CA để xác minh chứng chỉ ứng dụng khách ở phía máy chủ
Mới trong phiên bản 3. 4
SSLContext. load_verify_locations[cafile=Không có, capath=None, cadata=None]¶Tải một bộ chứng chỉ "cơ quan chứng nhận" [CA] được sử dụng để xác thực chứng chỉ của các đồng nghiệp khác khi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 khác với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300. Ít nhất một cafile hoặc capath phải được chỉ định
Phương pháp này cũng có thể tải danh sách hủy bỏ chứng chỉ [CRL] ở định dạng PEM hoặc DER. Để sử dụng CRL,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583 phải được định cấu hình đúng cách
Chuỗi cafile, nếu có, là đường dẫn đến tệp chứng chỉ CA được nối ở định dạng PEM. Xem phần thảo luận về Chứng chỉ để biết thêm thông tin về cách sắp xếp các chứng chỉ trong tệp này.
Chuỗi capath, nếu có, là đường dẫn đến thư mục chứa một số chứng chỉ CA ở định dạng PEM, tuân theo bố cục cụ thể của OpenSSL
Đối tượng cadata, nếu có, là một chuỗi ASCII của một hoặc nhiều chứng chỉ được mã hóa PEM hoặc một đối tượng dạng byte của . Giống như với capath, các dòng bổ sung xung quanh chứng chỉ được mã hóa PEM bị bỏ qua nhưng phải có ít nhất một chứng chỉ.
Đã thay đổi trong phiên bản 3. 4. Catadata đối số tùy chọn mới
SSLContext. get_ca_certs[binary_form=Sai]¶Nhận danh sách các chứng chỉ “cơ quan chứng nhận” [CA] đã tải. Nếu tham số
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3686 là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500 thì mỗi mục trong danh sách là một lệnh giống như đầu ra của
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3504. Mặt khác, phương thức trả về danh sách các chứng chỉ được mã hóa DER. Danh sách được trả về không chứa chứng chỉ từ capath trừ khi chứng chỉ được yêu cầu và tải bởi kết nối SSL
Note
Chứng chỉ trong thư mục capath không được tải trừ khi chúng đã được sử dụng ít nhất một lần
Mới trong phiên bản 3. 4
SSLContext. get_ciphers[] ¶Nhận danh sách các mật mã được kích hoạt. Danh sách theo thứ tự ưu tiên mật mã. Xem
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3552
Thí dụ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv352
Mới trong phiên bản 3. 6
SSLContext. set_default_verify_paths[] ¶Tải một bộ chứng chỉ "cơ quan chứng nhận" [CA] mặc định từ đường dẫn hệ thống tệp được xác định khi xây dựng thư viện OpenSSL. Thật không may, không có cách nào dễ dàng để biết liệu phương pháp này có thành công hay không. không có lỗi nào được trả về nếu không tìm thấy chứng chỉ nào. When the OpenSSL library is provided as part of the operating system, though, it is likely to be configured properly
SSLContext. set_ciphers[ciphers] ¶Đặt mật mã có sẵn cho ổ cắm được tạo bằng ngữ cảnh này. Nó phải là một chuỗi ở định dạng danh sách mật mã OpenSSL. Nếu không thể chọn mật mã nào [vì các tùy chọn thời gian biên dịch hoặc cấu hình khác cấm sử dụng tất cả các mật mã được chỉ định], một
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 sẽ được nâng lên
Note
when connected, the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv384 method of SSL sockets will give the currently selected cipher
TLS 1. 3 bộ mật mã không thể bị vô hiệu hóa với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv385SSLContext. set_alpn_protocols[giao thức] ¶
Chỉ định giao thức nào ổ cắm sẽ quảng cáo trong quá trình bắt tay SSL/TLS. Nó phải là một danh sách các chuỗi ASCII, như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv386, được sắp xếp theo sở thích. Việc lựa chọn giao thức sẽ diễn ra trong quá trình bắt tay và sẽ diễn ra theo RFC 7301. Sau khi bắt tay thành công, phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv387 sẽ trả về giao thức đã thỏa thuận
Phương pháp này sẽ tăng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv323 nếu
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv389 là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500
New in version 3. 5
SSLContext. set_npn_protocols[giao thức] ¶Chỉ định giao thức nào ổ cắm sẽ quảng cáo trong quá trình bắt tay SSL/TLS. Nó phải là một danh sách các chuỗi, như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv386, được sắp xếp theo sở thích. Việc lựa chọn một giao thức sẽ diễn ra trong quá trình bắt tay và sẽ diễn ra theo Thỏa thuận giao thức lớp ứng dụng. Sau khi bắt tay thành công, phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv392 sẽ trả về giao thức đã thỏa thuận
Phương pháp này sẽ tăng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv323 nếu
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv394 là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500
New in version 3. 3
Deprecated since version 3. 10. NPN has been superseded by ALPN
SSLContext. sni_callback ¶Đăng ký chức năng gọi lại sẽ được gọi sau khi máy chủ SSL/TLS nhận được thông báo bắt tay Xin chào của Máy khách TLS khi máy khách TLS chỉ định chỉ báo tên máy chủ. Cơ chế chỉ báo tên máy chủ được chỉ định trong RFC 6066 phần 3 - Chỉ định tên máy chủ
Chỉ có thể đặt một cuộc gọi lại cho mỗi
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6. Nếu sni_callback được đặt thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 thì chức năng gọi lại bị tắt. Gọi chức năng này lần sau sẽ vô hiệu hóa cuộc gọi lại đã đăng ký trước đó
Hàm gọi lại sẽ được gọi với ba đối số; . The server name argument is text. Đối với tên miền quốc tế hóa, tên máy chủ là IDN nhãn A [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv338]
Cách sử dụng điển hình của lệnh gọi lại này là thay đổi thuộc tính
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'03 của
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv39 thành một đối tượng mới thuộc loại
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 đại diện cho chuỗi chứng chỉ khớp với tên máy chủ
Do giai đoạn đàm phán ban đầu của kết nối TLS, chỉ các phương thức và thuộc tính hạn chế mới có thể sử dụng được như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv387 và
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'03. Các phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3504,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv384 và
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'09 yêu cầu kết nối TLS đã vượt ra ngoài TLS Client Hello và do đó sẽ không trả về các giá trị có ý nghĩa cũng như không thể gọi chúng một cách an toàn
The sni_callback function must return
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 to allow the TLS negotiation to continue. Nếu yêu cầu lỗi TLS, có thể trả về hằng số
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'11. Các giá trị trả về khác sẽ dẫn đến lỗi nghiêm trọng TLS với
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'12
Nếu một ngoại lệ được đưa ra từ chức năng sni_callback, kết nối TLS sẽ chấm dứt với thông báo cảnh báo TLS nghiêm trọng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13
Phương pháp này sẽ tăng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv323 nếu thư viện OpenSSL đã xác định OPENSSL_NO_TLSEXT khi nó được xây dựng
New in version 3. 7
SSLContext. set_servername_callback[server_name_callback] ¶Đây là API kế thừa được giữ lại để tương thích ngược. Khi có thể, bạn nên sử dụng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15 để thay thế. Server_name_callback đã cho tương tự như sni_callback, ngoại trừ khi tên máy chủ là tên miền quốc tế hóa được mã hóa IDN, server_name_callback nhận được nhãn chữ U đã giải mã [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv339]
Nếu có lỗi giải mã trên tên máy chủ, kết nối TLS sẽ chấm dứt với thông báo cảnh báo TLS nghiêm trọng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'12 cho máy khách
Mới trong phiên bản 3. 4
SSLContext. load_dh_params[dhfile] ¶Tải các tham số tạo khóa cho trao đổi khóa Diffie-Hellman [DH]. Sử dụng trao đổi khóa DH cải thiện tính bảo mật về phía trước với chi phí tài nguyên tính toán [cả trên máy chủ và máy khách]. Tham số dhfile phải là đường dẫn đến tệp chứa tham số DH ở định dạng PEM
Cài đặt này không áp dụng cho ổ cắm máy khách. Bạn cũng có thể sử dụng tùy chọn
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv353 để tăng cường bảo mật hơn nữa
New in version 3. 3
SSLContext. set_ecdh_curve[curve_name] ¶Đặt tên đường cong cho trao đổi khóa Diffie-Hellman [ECDH] dựa trên đường cong Elliptic. ECDH nhanh hơn đáng kể so với DH thông thường trong khi được cho là an toàn. Tham số tên đường cong phải là một chuỗi mô tả một đường cong elip nổi tiếng, ví dụ:
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'19 cho một đường cong được hỗ trợ rộng rãi
Cài đặt này không áp dụng cho ổ cắm máy khách. Bạn cũng có thể sử dụng tùy chọn
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv354 để tăng cường bảo mật hơn nữa
Phương pháp này không khả dụng nếu
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'21 là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500
New in version 3. 3
Xem thêm
SSL/TLS & Bảo mật chuyển tiếp hoàn hảoVincent Bernat
SSLContext. wrap_socket[sock , server_side=False , do_handshake_on_connect=True , suppress_ragged_eofs=True , server_hostname=None , session=None] ¶Quấn một ổ cắm Python hiện có và trả về một phiên bản của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'23 [mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7]. Ổ cắm SSL được trả lại được gắn với ngữ cảnh, cài đặt và chứng chỉ của nó. tất phải là ổ cắm
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3547;
Tham số
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'26 là một boolean xác định xem hành vi phía máy chủ hay phía máy khách được mong muốn từ ổ cắm này
Đối với các ổ cắm phía máy khách, việc xây dựng bối cảnh là lười biếng; . Đối với ổ cắm phía máy chủ, nếu ổ cắm không có thiết bị ngang hàng từ xa, thì ổ cắm đó được coi là ổ cắm đang lắng nghe và gói SSL phía máy chủ được thực hiện tự động trên các kết nối máy khách được chấp nhận thông qua phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3630. Phương pháp này có thể tăng
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'15
Trên các kết nối máy khách, tham số tùy chọn server_hostname chỉ định tên máy chủ của dịch vụ mà chúng tôi đang kết nối tới. Điều này cho phép một máy chủ duy nhất lưu trữ nhiều dịch vụ dựa trên SSL với các chứng chỉ riêng biệt, khá giống với máy chủ ảo HTTP. Chỉ định server_hostname sẽ tăng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685 nếu server_side là đúng
Tham số
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'31 chỉ định liệu có tự động thực hiện bắt tay SSL sau khi thực hiện
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'32 hay không hoặc chương trình ứng dụng sẽ gọi nó một cách rõ ràng hay không bằng cách gọi phương thức
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'33. Việc gọi
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'33 một cách rõ ràng giúp chương trình kiểm soát hành vi chặn của I/O ổ cắm liên quan đến quá trình bắt tay
Tham số
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'35 chỉ định cách phương thức
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'36 sẽ báo hiệu EOF không mong muốn từ đầu kia của kết nối. Nếu được chỉ định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 [mặc định], nó sẽ trả về một EOF bình thường [một đối tượng byte trống] để phản hồi các lỗi EOF không mong muốn được phát sinh từ ổ cắm bên dưới;
phiên, xem
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'39
Changed in version 3. 5. Luôn cho phép server_hostname được chuyển, ngay cả khi OpenSSL không có SNI.
Đã thay đổi trong phiên bản 3. 6. đối số phiên đã được thêm vào.
Đã thay đổi trong phiên bản 3. 7. Phương thức trả về một thể hiện của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'23 thay vì
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 được mã hóa cứng. SSLContext. sslsocket_class ¶
Kiểu trả về của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4, mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7. Thuộc tính có thể được ghi đè trên phiên bản của lớp để trả về một lớp con tùy chỉnh của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7
New in version 3. 7
SSLContext. wrap_bio[đến , đi, server_side=False, server_hostname=None, session=None]¶Bọc các đối tượng BIO vào và ra và trả về một thể hiện của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'45 [mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46]. Các thủ tục SSL sẽ đọc dữ liệu đầu vào từ BIO gửi đến và ghi dữ liệu vào BIO gửi đi
Các tham số server_side, server_hostname và session có cùng ý nghĩa như trong
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4
Đã thay đổi trong phiên bản 3. 6. đối số phiên đã được thêm vào.
Đã thay đổi trong phiên bản 3. 7. Phương thức trả về một thể hiện của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'45 thay vì
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46 được mã hóa cứng. SSLContext. sslobject_class ¶
Kiểu trả về của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'50, mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46. Thuộc tính có thể được ghi đè trên phiên bản của lớp để trả về một lớp con tùy chỉnh của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46
New in version 3. 7
SSLContext. session_stats[] ¶Nhận số liệu thống kê về các phiên SSL được tạo hoặc quản lý bởi ngữ cảnh này. Một từ điển được trả về ánh xạ tên của từng phần thông tin thành các giá trị số của chúng. Ví dụ: đây là tổng số lần truy cập và bỏ lỡ trong bộ đệm phiên kể từ khi ngữ cảnh được tạo
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv353SSLContext. check_hostname ¶
Có khớp với tên máy chủ của chứng chỉ ngang hàng trong
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'33 hay không. Ngữ cảnh của
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 phải được đặt thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3565 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 và bạn phải chuyển server_hostname thành
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9 để khớp với tên máy chủ. Cho phép kiểm tra tên máy chủ tự động đặt
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 từ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300 thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372. Không thể đặt lại thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300 miễn là tính năng kiểm tra tên máy chủ được bật. Giao thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 cho phép kiểm tra tên máy chủ theo mặc định. Với các giao thức khác, việc kiểm tra tên máy chủ phải được bật một cách rõ ràng
Thí dụ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv354
Mới trong phiên bản 3. 4
Đã thay đổi trong phiên bản 3. 7.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 hiện được tự động thay đổi thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 khi tính năng kiểm tra tên máy chủ được bật và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300. Trước đây, hoạt động tương tự sẽ thất bại với một
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685. SSLContext. keylog_filename ¶
Ghi các khóa TLS vào tệp nhật ký khóa, bất cứ khi nào tài liệu khóa được tạo hoặc nhận. Tệp keylog chỉ được thiết kế cho mục đích gỡ lỗi. Định dạng tệp được chỉ định bởi NSS và được sử dụng bởi nhiều bộ phân tích lưu lượng như Wireshark. Tệp nhật ký được mở ở chế độ chỉ nối thêm. Ghi được đồng bộ hóa giữa các luồng, nhưng không phải giữa các quy trình
New in version 3. 8
SSLContext. maximum_version ¶Thành viên enum
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3627 đại diện cho phiên bản TLS được hỗ trợ cao nhất. Giá trị mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'69. Thuộc tính chỉ đọc đối với các giao thức khác ngoài
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'13,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317
Các thuộc tính
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'73,
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'74 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3549 đều ảnh hưởng đến các phiên bản SSL và TLS được hỗ trợ của ngữ cảnh. Việc thực hiện không ngăn chặn sự kết hợp không hợp lệ. For example a context with
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'76 in
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'77 and
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'73 set to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3628 will not be able to establish a TLS 1. 2 kết nối
New in version 3. 7
SSLContext. minimum_version ¶Giống như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 ngoại trừ đây là phiên bản được hỗ trợ thấp nhất hoặc
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'81
New in version 3. 7
SSLContext. num_tickets ¶Kiểm soát số lượng TLS 1. 3 vé phiên của ngữ cảnh
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317. Cài đặt không ảnh hưởng đến TLS 1. 0 đến 1. 2 kết nối
New in version 3. 8
SSLContext. tùy chọn ¶Một số nguyên đại diện cho tập hợp các tùy chọn SSL được bật trong ngữ cảnh này. Giá trị mặc định là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'83, nhưng bạn có thể chỉ định các tùy chọn khác, chẳng hạn như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 bằng cách ORing chúng lại với nhau
Đã thay đổi trong phiên bản 3. 6. ______1549 trả về cờ
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'86.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv355
Không dùng nữa kể từ phiên bản 3. 7. Tất cả các tùy chọn
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'87 và
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'88 không được dùng nữa kể từ Python 3. 7. Sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 để thay thế. SSLContext. post_handshake_auth ¶
Enable TLS 1. 3 xác thực ứng dụng khách sau bắt tay. Xác thực sau bắt tay bị tắt theo mặc định và máy chủ chỉ có thể yêu cầu chứng chỉ máy khách TLS trong quá trình bắt tay ban đầu. Khi được bật, máy chủ có thể yêu cầu chứng chỉ máy khách TLS bất kỳ lúc nào sau khi bắt tay
Khi được bật trên các ổ cắm phía máy khách, máy khách sẽ báo hiệu cho máy chủ rằng nó hỗ trợ xác thực sau bắt tay
Khi được bật trên các ổ cắm phía máy chủ, cũng phải đặt
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3558 thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3565 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372. Trao đổi chứng chỉ ứng dụng khách thực tế bị trì hoãn cho đến khi
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94 được gọi và một số I/O được thực hiện
New in version 3. 8
SSLContext. giao thức ¶Phiên bản giao thức được chọn khi xây dựng ngữ cảnh. Thuộc tính này là chỉ đọc
SSLContext. hostname_checks_common_name ¶Liệu
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 có quay trở lại để xác minh tên chung chủ đề của chứng chỉ trong trường hợp không có phần mở rộng tên thay thế chủ đề hay không [mặc định. thật]
New in version 3. 7
Đã thay đổi trong phiên bản 3. 10. Cờ không có tác dụng với OpenSSL trước phiên bản 1. 1. 1k. Trăn 3. 8. 9, 3. 9. 3 và 3. 10 bao gồm cách giải quyết cho các phiên bản trước.
SSLContext. cấp_bảo mật ¶Một số nguyên đại diện cho mức độ bảo mật cho bối cảnh. Thuộc tính này là chỉ đọc
Mới trong phiên bản 3. 10
SSLContext. verify_flags ¶Cờ cho các hoạt động xác minh chứng chỉ. Bạn có thể đặt các cờ như
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'96 bằng cách ORing chúng lại với nhau. Theo mặc định, OpenSSL không yêu cầu cũng như không xác minh danh sách thu hồi chứng chỉ [CRL]
Mới trong phiên bản 3. 4
Đã thay đổi trong phiên bản 3. 6. ______1583 trả về cờ
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'98.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv356SSLContext. verify_mode ¶
Có nên thử xác minh chứng chỉ của các đồng nghiệp khác hay không và cách xử lý nếu xác minh không thành công. This attribute must be one of
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3565 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372
Đã thay đổi trong phiên bản 3. 6. ______1558 trả về
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3103 enum.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv357
Certificates¶
Certificates in general are part of a public-key / private-key system. In this system, each principal, [which may be a machine, or a person, or an organization] is assigned a unique two-part encryption key. One part of the key is public, and is called the public key; the other part is kept secret, and is called the private key. Hai phần có liên quan với nhau, ở chỗ nếu bạn mã hóa tin nhắn bằng một trong các phần, bạn có thể giải mã nó bằng phần còn lại và chỉ với phần còn lại
A certificate contains information about two principals. It contains the name of a subject, and the subject’s public key. It also contains a statement by a second principal, the issuer, that the subject is who they claim to be, and that this is indeed the subject’s public key. The issuer’s statement is signed with the issuer’s private key, which only the issuer knows. However, anyone can verify the issuer’s statement by finding the issuer’s public key, decrypting the statement with it, and comparing it to the other information in the certificate. The certificate also contains information about the time period over which it is valid. This is expressed as two fields, called “notBefore” and “notAfter”
In the Python use of certificates, a client or server can use a certificate to prove who they are. The other side of a network connection can also be required to produce a certificate, and that certificate can be validated to the satisfaction of the client or server that requires such validation. The connection attempt can be set to raise an exception if the validation fails. Việc xác thực được thực hiện tự động bởi khung OpenSSL cơ bản; . But the application does usually need to provide sets of certificates to allow this process to take place
Python sử dụng tệp để chứa chứng chỉ. Chúng phải được định dạng là “PEM” [xem RFC 1422], là dạng mã hóa cơ sở 64 được bao bọc bởi dòng tiêu đề và dòng cuối trang
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv358
Certificate chains¶
The Python files which contain certificates can contain a sequence of certificates, sometimes called a certificate chain. This chain should start with the specific certificate for the principal who “is” the client or server, and then the certificate for the issuer of that certificate, and then the certificate for the issuer of that certificate, and so on up the chain till you get to a certificate which is self-signed, that is, a certificate which has the same subject and issuer, sometimes called a root certificate. The certificates should just be concatenated together in the certificate file. For example, suppose we had a three certificate chain, from our server certificate to the certificate of the certification authority that signed our server certificate, to the root certificate of the agency which issued the certification authority’s certificate
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv359
CA certificates¶
Nếu bạn định yêu cầu xác thực mặt kia của chứng chỉ kết nối, bạn cần cung cấp tệp “CA certs”, chứa đầy chuỗi chứng chỉ cho mỗi nhà phát hành mà bạn sẵn sàng tin tưởng. Again, this file just contains these chains concatenated together. For validation, Python will use the first chain it finds in the file which matches. The platform’s certificates file can be used by calling
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373, this is done automatically with
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8
Combined key and certificate¶
Often the private key is stored in the same file as the certificate; in this case, only the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3106 parameter to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3550 and
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'9 needs to be passed. If the private key is stored with the certificate, it should come before the first certificate in the certificate chain
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv360
Chứng chỉ tự ký¶
If you are going to create a server that provides SSL-encrypted connection services, you will need to acquire a certificate for that service. There are many ways of acquiring appropriate certificates, such as buying one from a certification authority. Another common practice is to generate a self-signed certificate. Cách đơn giản nhất để làm điều này là với gói OpenSSL, sử dụng một cái gì đó như sau
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv361
The disadvantage of a self-signed certificate is that it is its own root certificate, and no one else will have it in their cache of known [and trusted] root certificates
Examples¶
Testing for SSL support¶
To test for the presence of SSL support in a Python installation, user code should use the following idiom
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv362
Client-side operation¶
This example creates a SSL context with the recommended security settings for client sockets, including automatic certificate verification
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv363
If you prefer to tune security settings yourself, you might create a context from scratch [but beware that you might not get the settings right]
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv364
[this snippet assumes your operating system places a bundle of all CA certificates in
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3109; if not, you’ll get an error and have to adjust the location]
The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 protocol configures the context for cert validation and hostname verification.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 is set to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 is set to
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'94. All other protocols create SSL contexts with insecure defaults
When you use the context to connect to a server,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3575 validate the server certificate. it ensures that the server certificate was signed with one of the CA certificates, checks the signature for correctness, and verifies other properties like validity and identity of the hostname
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv365
You may then fetch the certificate
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv366
Visual inspection shows that the certificate does identify the desired service [that is, the HTTPS host
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3117]
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv367
Now the SSL channel is established and the certificate verified, you can proceed to talk with the server
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv368
Xem thảo luận về Cân nhắc về bảo mật bên dưới.
Server-side operation¶
For server operation, typically you’ll need to have a server certificate, and private key, each in a file. Trước tiên, bạn sẽ tạo ngữ cảnh giữ khóa và chứng chỉ để khách hàng có thể kiểm tra tính xác thực của bạn. Then you’ll open a socket, bind it to a port, call
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3643 on it, and start waiting for clients to connect
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv369
When a client connects, you’ll call
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3630 on the socket to get the new socket from the other end, and use the context’s
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'4 method to create a server-side SSL socket for the connection
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv30
Then you’ll read data from the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3121 and do something with it till you are finished with the client [or the client is finished with you]
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv31
And go back to listening for new client connections [of course, a real server would probably handle each client connection in a separate thread, or put the sockets in non-blocking mode and use an event loop].
Notes on non-blocking sockets¶
SSL sockets behave slightly different than regular sockets in non-blocking mode. When working with non-blocking sockets, there are thus several things you need to be aware of
Hầu hết các phương pháp
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
7 sẽ tăngctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
666 hoặcctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
665 thay vìctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
125 nếu thao tác I/O sẽ chặn.ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
665 will be raised if a read operation on the underlying socket is necessary, andctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
666 for a write operation on the underlying socket. Note that attempts to write to an SSL socket may require reading from the underlying socket first, and attempts to read from the SSL socket may require a prior write to the underlying socketChanged in version 3. 5. In earlier Python versions, the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
128 method returned zero instead of raisingctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
666 orctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
665.Calling
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 tells you that the OS-level socket can be read from [or written to], but it does not imply that there is sufficient data at the upper SSL layer. For example, only part of an SSL frame might have arrived. Therefore, you must be ready to handle>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
36 andctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
128 failures, and retry after another call toctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131Conversely, since the SSL layer has its own framing, a SSL socket may still have data available for reading without
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 being aware of it. Therefore, you should first call>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
36 to drain any potentially available data, and then only block on actx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 call if still necessary[of course, similar provisions apply when using other primitives such as
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
138, or those in thectx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
139 module]The SSL handshake itself will be non-blocking. the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
33 method has to be retried until it returns successfully. Here is a synopsis usingctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 to wait for the socket’s readinessctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
2
Xem thêm
Mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3142 hỗ trợ ổ cắm SSL không chặn và cung cấp API cấp cao hơn. It polls for events using the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3139 module and handles
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3666,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3665 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3125 exceptions. It runs the SSL handshake asynchronously as well.
Hỗ trợ BIO bộ nhớ¶
New in version 3. 5
Kể từ khi mô-đun SSL được giới thiệu trong Python 2. 6, the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 class has provided two related but distinct areas of functionality
SSL protocol handling
Network IO
The network IO API is identical to that provided by
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'0, from which
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 also inherits. This allows an SSL socket to be used as a drop-in replacement for a regular socket, making it very easy to add SSL support to an existing application
Kết hợp xử lý giao thức SSL và IO mạng thường hoạt động tốt, nhưng có một số trường hợp không hoạt động. Một ví dụ là các khung IO không đồng bộ muốn sử dụng một mô hình ghép kênh IO khác với mô hình “chọn/thăm dò trên một bộ mô tả tệp” [dựa trên mức độ sẵn sàng] được giả định bởi
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'0 và bởi các quy trình IO của ổ cắm OpenSSL nội bộ. This is mostly relevant for platforms like Windows where this model is not efficient. Với mục đích này, một biến thể giảm phạm vi của
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 được gọi là
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46 được cung cấplớp ssl. SSLObject ¶
A reduced-scope variant of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7 representing an SSL protocol instance that does not contain any network IO methods. This class is typically used by framework authors that want to implement asynchronous IO for SSL through memory buffers
Lớp này triển khai giao diện trên đầu đối tượng SSL cấp thấp do OpenSSL triển khai. This object captures the state of an SSL connection but does not provide any network IO itself. IO needs to be performed through separate “BIO” objects which are OpenSSL’s IO abstraction layer
This class has no public constructor. An
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46 instance must be created using the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3155 method. This method will create the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46 instance and bind it to a pair of BIOs. The incoming BIO is used to pass data from Python to the SSL protocol instance, while the outgoing BIO is used to pass data the other way around
The following methods are available
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
682>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
26ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
37>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
39ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
161ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
667ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
672>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
1ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
165ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
166>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
2ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
10ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
169ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
170ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
41ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
172ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
173ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
174ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
175
When compared to
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'7, this object lacks the following features
Any form of network IO;
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
645 andctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
648 read and write only to the underlyingctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
179 buffersThere is no do_handshake_on_connect machinery. You must always manually call
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
41 to start the handshakeThere is no handling of suppress_ragged_eofs. All end-of-file conditions that are in violation of the protocol are reported via the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
181 exceptionThe method
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
173 call does not return anything, unlike for an SSL socket where it returns the underlying socketThe server_name_callback callback passed to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
617 will get an>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
46 instance instead of a>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
7 instance as its first parameter
Some notes related to the use of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46
All IO on an
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
46 is non-blocking . This means that for examplectx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
667 will raise anctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
665 if it needs more data than the incoming BIO has available.There is no module-level
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
155 call like there is for>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
9. An>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
46 is always created via an>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'
6
Đã thay đổi trong phiên bản 3. 7.
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'46 instances must to created with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3155. In earlier versions, it was possible to create instances directly. This was never documented or officially supported.
An SSLObject communicates with the outside world using memory buffers. The class
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3179 provides a memory buffer that can be used for this purpose. It wraps an OpenSSL memory BIO [Basic IO] objectclass ssl. MemoryBIO ¶
A memory buffer that can be used to pass data between Python and an SSL protocol instance
pending ¶Return the number of bytes currently in the memory buffer
eof ¶A boolean indicating whether the memory BIO is current at the end-of-file position
read[n=- 1] ¶Read up to n bytes from the memory buffer. If n is not specified or negative, all bytes are returned
write[buf] ¶Write the bytes from buf to the memory BIO. The buf argument must be an object supporting the buffer protocol
The return value is the number of bytes written, which is always equal to the length of buf
write_eof[] ¶Write an EOF marker to the memory BIO. After this method has been called, it is illegal to call
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672. The attribute
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3198 will become true after all data currently in the buffer has been read
SSL session¶
Mới trong phiên bản 3. 6
class ssl. SSLSession ¶Session object used by
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'39id ¶ time ¶ timeout ¶ ticket_lifetime_hint ¶ has_ticket ¶
Security considerations¶
Best defaults¶
For client use, if you don’t have any special requirements for your security policy, it is highly recommended that you use the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'8 function to create your SSL context. It will load the system’s trusted CA certificates, enable certificate validation and hostname checking, and try to choose reasonably secure protocol and cipher settings
For example, here is how you would use the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3701 class to create a trusted, secure connection to a SMTP server
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv33
If a client certificate is needed for the connection, it can be added with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3550
By contrast, if you create the SSL context by calling the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 constructor yourself, it will not have certificate validation nor hostname checking enabled by default. If you do so, please read the paragraphs below to achieve a good security level
Manual settings¶
Verifying certificates¶
When calling the
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'6 constructor directly,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300 is the default. Since it does not authenticate the other peer, it can be insecure, especially in client mode where most of time you would like to ensure the authenticity of the server you’re talking to. Therefore, when in client mode, it is highly recommended to use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372. However, it is in itself not sufficient; you also have to check that the server certificate, which can be obtained by calling
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3504, matches the desired service. For many protocols and applications, the service can be identified by the hostname; in this case, the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680 function can be used. This common check is automatically performed when
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3709 is enabled
Changed in version 3. 7. Hostname matchings is now performed by OpenSSL. Python no longer uses
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680.
In server mode, if you want to authenticate your clients using the SSL layer [rather than using a higher-level authentication mechanism], you’ll also have to specify
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 and similarly check the client certificate
Protocol versions¶
SSL versions 2 and 3 are considered insecure and are therefore dangerous to use. If you want maximum compatibility between clients and servers, it is recommended to use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 as the protocol version. SSLv2 and SSLv3 are disabled by default
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv34
The SSL context created above will only allow TLSv1. 2 trở lên [nếu được hệ thống của bạn hỗ trợ] kết nối với máy chủ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 implies certificate validation and hostname checks by default. You have to load certificates into the context
Cipher selection¶
Nếu bạn có các yêu cầu bảo mật nâng cao, có thể tinh chỉnh các mật mã được kích hoạt khi đàm phán phiên SSL thông qua phương pháp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3552. Starting from Python 3. 2. 3, the ssl module disables certain weak ciphers by default, but you may want to further restrict the cipher choice. Be sure to read OpenSSL’s documentation about the cipher list format. If you want to check which ciphers are enabled by a given cipher list, use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3716 or the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3717 command on your system
Multi-processing¶
Nếu sử dụng mô-đun này như một phần của ứng dụng đa xử lý [ví dụ: sử dụng mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3718 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3719], hãy lưu ý rằng trình tạo số ngẫu nhiên bên trong của OpenSSL không xử lý đúng quy trình rẽ nhánh. Applications must change the PRNG state of the parent process if they use any SSL feature with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3720. Any successful call of
>>> cert = {'subject': [[['commonName', 'example.com'],],]} >>> ssl.match_hostname[cert, "example.com"] >>> ssl.match_hostname[cert, "example.org"] Traceback [most recent call last]: File "", line 1, in File "/home/py3k/Lib/ssl.py", line 130, in match_hostname ssl.CertificateError: hostname 'example.org' doesn't match 'example.com'92,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3722 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3723 is sufficient
TLS 1. 3¶
New in version 3. 7
The TLS 1. 3 hoạt động hơi khác so với phiên bản trước của TLS/SSL. Some new TLS 1. 3 features are not yet available
TLS 1. 3 uses a disjunct set of cipher suites. All AES-GCM and ChaCha20 cipher suites are enabled by default. The method
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
552 cannot enable or disable any TLS 1. 3 ciphers yet, butctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
716 returns themSession tickets are no longer sent as part of the initial handshake and are handled differently.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
726 andctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
40 are not compatible with TLS 1. 3Client-side certificates are also no longer verified during the initial handshake. A server can request a certificate at any time. Clients process certificate requests while they send or receive application data from the server
TLS 1. 3 features like early data, deferred TLS client cert request, signature algorithm configuration, and rekeying are not supported yet
Does Python requests use TLS?
Requests uses the Python standard library ssl module under the hood - this supports various versions of SSL and TLS . You can tell requests to use a specific protocol [like TLSv1] by creating an HTTPAdapter that customizes the PoolManager instance that gets created.How does Python implement SSL?
Now that we know how to verify the validity of TLS/SSL on a website, let's see how we can use it on a Python web server. .Create the private RSA keyGenerate a certificate signing request [CSR] using the private keySign the CSR request to create the certificateWhat is an SSL error Python?
SSL certificate_verify_failed errors typically occur as a result of outdated Python default certificates or invalid root certificates . If you're a website owner and you're receiving this error, it could be because you're not using a valid SSL certificate.Python tìm chứng chỉ SSL ở đâu?
By default, the Python ssl module uses the system CA certificate bundle - /etc/pki/tls/certs/ca-bundle