Mô-đun này cung cấp quyền truy cập vào các phương tiện xác thực ngang hàng và mã hóa Bảo mật tầng vận chuyển [thường được gọi là “Lớp cổng bảo mật”] cho các ổ cắm mạng, cả phía máy khách và phía máy chủ. Mô-đun này sử dụng thư viện OpenSSL. Nó có sẵn trên tất cả các hệ thống Unix hiện đại, Windows, macOS và có thể là các nền tảng bổ sung, miễn là OpenSSL được cài đặt trên nền tảng đó
Ghi chú
Một số hành vi có thể phụ thuộc vào nền tảng, vì các cuộc gọi được thực hiện tới các API socket của hệ điều hành. Phiên bản OpenSSL đã cài đặt cũng có thể gây ra các biến thể trong hành vi. Ví dụ: TLSv1. 3 với OpenSSL phiên bản 1. 1. 1
Cảnh báo
Không sử dụng mô-đun này khi chưa đọc Cân nhắc về bảo mật . Làm như vậy có thể dẫn đến cảm giác bảo mật sai lầm, vì cài đặt mặc định của mô-đun ssl không nhất thiết phải phù hợp với ứng dụng của bạn.
Tính khả dụng . không phải Emscripten, không phải WASI.
Mô-đun này không hoạt động hoặc không khả dụng trên nền tảng WebAssembly
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv36 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv37. Xem Nền tảng WebAssugging để biết thêm thông tin.
Phần này ghi lại các đối tượng và chức năng trong mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35;
Mô-đun này cung cấp một lớp,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv39, bắt nguồn từ 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'0 và cung cấp một trình bao bọc giống ổ cắm cũng mã hóa và giải mã dữ liệu đi qua ổ cắm bằng SSL. Nó hỗ trợ các phương thức bổ sung 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'1, truy xuất chứng chỉ của phía bên kia của kết nối 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'2, truy xuất mật mã được sử dụng cho kết nối an toàn
Đối với các ứng dụng tinh vi hơn, lớ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'3 giúp quản lý các cài đặt và chứng chỉ, sau đó có thể được kế thừa bởi các ổ cắm SSL được tạo thông qua 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'4
Đã thay đổi trong phiên bản 3. 5. 3. Đã cập nhật để hỗ trợ liên kết với OpenSSL 1. 1. 0
Đã thay đổi trong phiên bản 3. 6. OpenSSL 0. 9. 8, 1. 0. 0 và 1. 0. 1 không được dùng nữa và không còn được hỗ trợ. Trong tương lai, mô-đun ssl sẽ yêu cầu ít nhất OpenSSL 1. 0. 2 hoặc 1. 1. 0.
Đã thay đổi trong phiên bản 3. 10. PEP 644 đã được triển khai. Mô-đun ssl yêu cầu OpenSSL 1. 1. 1 hoặc mới hơn.
Việc sử dụng các hằng số và hàm không dùng nữa dẫn đến cảnh báo không dùng nữa
Hàm, hằng số và ngoại lệ¶
Tạo socket¶
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 của một thể 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'6 để bọc ổ cắm dưới dạng đố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'7. Hàm trợ giú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'8 trả về ngữ cảnh mới với cài đặt mặc định an toàn. 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 cũ không được dùng nữa vì nó vừa không hiệu quả vừa không hỗ trợ chỉ báo tên máy chủ [SNI] và khớp tên máy chủ
Ví dụ về ổ cắm máy khách với ngữ cảnh mặc định và ngăn xếp kép IPv4/IPv6
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv34
Ví dụ về ổ cắm máy khách với ngữ cảnh tùy chỉnh và IPv4
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35
Ví dụ ổ cắm máy chủ đang nghe trên localhost IPv4
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv36
Tạo ngữ cảnh¶
Một chức năng tiện lợi giúp tạo 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 cho các mục đích chungssl. create_default_context[mục đích=Mục đích. SERVER_AUTH , cafile=Không có, capath=None, cadata=None]¶
Trả về một đố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 mới với cài đặt mặc định cho mục đích nhất định. Các cài đặt được chọn bởi mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35 và thường thể hiện mức độ bảo mật cao hơn so với khi gọi trực tiếp hàm tạo
>>> 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
cafile, capath, cadata đại diện cho các chứng chỉ CA tùy chọn đáng tin cậy để xác minh chứng chỉ, như trong
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314. Nếu cả ba đều là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315, chức năng này có thể chọn tin cậy các chứng chỉ CA mặc định của hệ thống để thay thế
Các cài đặt là.
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,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 với bộ mật mã mã hóa cao không có RC4 và không có bộ mật mã không được xác thực. Chuyển
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv370 vì mục đích đặt
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 và tải chứng chỉ CA [khi ít nhất một trong các cafile, capath hoặc cadata được cung cấp] hoặc sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373 để tải chứng chỉ CA mặc định
Khi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv374 được hỗ trợ và biến môi trường
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv375 được đặt, thì
>>> 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 sẽ bật ghi nhật ký khóa
Ghi chú
Giao thức, tùy chọn, mật mã và các cài đặt khác có thể thay đổi thành các giá trị hạn chế hơn bất cứ lúc nào mà không cần phản đối trước. Các giá trị thể hiện sự cân bằng hợp lý giữa khả năng tương thích và bảo mật
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 đó
Ghi chú
Nếu bạn thấy rằng khi một số máy khách hoặc máy chủ cũ hơn cố gắng kết nối với 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 được tạo bởi chức năng này và chúng gặp lỗi cho biết “Bộ giao thức hoặc bộ mật mã không khớp”, thì có thể chúng chỉ hỗ trợ SSL3. 0 mà chức năng này loại trừ bằng cách sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319. SSL3. 0 được coi là hoàn toàn bị phá vỡ. 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. Bối cảnh hiện sử dụng 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 thay vì chung chung
>>> 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.
Ngoại lệ¶
ngoại lệ ssl. Lỗi SSL ¶Tăng lên để báo hiệu lỗi từ việc triển khai SSL cơ bản [hiện do thư viện OpenSSL cung cấp]. Điều này biểu thị một số vấn đề trong lớp xác thực và mã hóa cấp cao hơn được đặt chồng lên kết nối mạng bên dưới. Lỗi này là một loại phụ 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'14. Mã lỗi và thông báo của phiên bả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'15 được cung cấp bởi thư viện OpenSSL
Đã thay đổi trong phiên bản 3. 3. ______415 từng là một kiểu phụ 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'17. thư viện ¶
Một chuỗi ghi nhớ chỉ định mô hình con OpenSSL xảy ra lỗi, chẳng hạn 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'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 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'80. Phạm vi của các giá trị có thể tùy thuộc vào phiên bản OpenSSL
Mới trong phiên bản 3. 3
lý do ¶Một chuỗi ghi nhớ chỉ định lý do xảy ra lỗi này, 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'81. Phạm vi của các giá trị có thể tùy thuộc vào phiên bản OpenSSL
Mới trong phiên bản 3. 3
ngoại lệ ssl. SSLZeroReturnError ¶Một lớp 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'15 được nâng lên khi cố gắng đọc hoặc ghi và kết nối SSL đã bị đóng hoàn toàn. Lưu ý rằng điều này không có nghĩa là phương tiện vận chuyển cơ bản [đọc TCP] đã bị đóng
Mới trong phiên bản 3. 3
ngoại lệ ssl. SSLWantReadError ¶Một lớp 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'15 được tạo ra bởi ổ cắm SSL không chặn khi cố gắng đọc hoặc ghi dữ liệu nhưng cần nhận thêm dữ liệu .
Mới trong phiên bản 3. 3
ngoại lệ ssl. Lỗi SSLWantWrite ¶Một lớp 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'15 được tạo ra bởi ổ cắm SSL không chặn khi cố gắng đọc hoặc ghi dữ liệu nhưng cần gửi thêm dữ liệu .
Mới trong phiên bản 3. 3
ngoại lệ ssl. Lỗi SSLSyscall ¶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
Mới trong phiên bản 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
Mới trong phiên bản 3. 3
ngoại lệ ssl. Lỗi SSLCertVerification ¶Một lớp 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'15 được nâng lên khi xác thực chứng chỉ không thành công
Mới trong phiên bản 3. 7
verify_code ¶Một số lỗi số biểu thị lỗi xác minh
verify_message ¶Một chuỗi có thể đọc được của con người về lỗi xác minh
ngoại lệ ssl. Lỗi chứng chỉ ¶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
Đã thay đổi trong phiên bản 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.
Tạo ngẫu nhiên¶
ssl. RAND_byte[num] ¶Trả về num byte giả ngẫu nhiên mạnh về mật mã. 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 nếu PRNG chưa được tạo đủ dữ liệu hoặc nếu hoạt động không được hỗ trợ bởi phương thức RAND hiện 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'91 có thể được sử dụng để kiểm tra trạng thái của PRNG 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'92 có thể được sử dụng để tạo PRNG
Đối với hầu hết tất cả các ứng dụng, 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'93
Đọc bài viết trên Wikipedia, Trình tạo số giả ngẫu nhiên an toàn bằng mật mã [CSPRNG], để biết các yêu cầu của trình tạo mã hóa mạnh
Mới trong phiên bản 3. 3
ssl. RAND_pseudo_byte[num] ¶Trả về [byte, is_cryptographic]. byte là num byte giả ngẫu nhiên, is_cryptographic 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 byte được tạo mạnh về mặt mật mã. Tăng 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 nếu hoạt động không được hỗ trợ bởi phương thức RAND hiện tại
Các chuỗi byte giả ngẫu nhiên được tạo sẽ là duy nhất nếu chúng có đủ độ dài, nhưng không nhất thiết là không thể đoán trước. Chúng có thể được sử dụng cho các mục đích phi mã hóa và cho các mục đích nhất định trong các giao thức mã hóa, nhưng thường không dùng để tạo khóa, v.v.
Đối với hầu hết tất cả các ứng dụng, 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'93
Mới trong phiên bản 3. 3
Không dùng nữa kể từ phiên bản 3. 6. OpenSSL đã ngừng 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'97, thay vào đó hãy 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'98. ssl. RAND_status[] ¶
Trả lạ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'94 nếu trình tạo số giả ngẫu nhiên SSL đã được chọn với tính ngẫu nhiên 'đủ' và nếu không thì trả lại
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500. Bạn có thể sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3501 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3502 để tăng tính ngẫu nhiên của trình tạo số giả ngẫu nhiênssl. 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, 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. Nếu
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3515 được chỉ định, hãy sử dụng phiên bản giao thức SSL đó để cố gắng kết nối với máy chủ. 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[] ¶Returns a named tuple with paths to OpenSSL’s default cafile and capath. The paths are the same as used by
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 - resolved path to capath orctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
15 if the directory doesn’t exist,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 - hard coded path to a capath directory
Mới trong phiên bản 3. 4
ssl. enum_certificates[store_name] ¶Retrieve certificates from Windows’ system cert store. 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
The function returns a list of [cert_bytes, encoding_type, trust] tuples. 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
Ví 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
The function returns a list of [cert_bytes, encoding_type, trust] tuples. encoding_type chỉ định mã hóa của cert_bytes. It is either
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3534 for 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 , keyfile=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 must be a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3547 socket; other socket types are unsupported
Internally, function creates 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 with protocol ssl_version and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3549 set to cert_reqs. If parameters keyfile, certfile, ca_certs or ciphers are set, then the values are passed to
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, and
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.
Constants¶
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
Possible value for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3558, or the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3559 parameter 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'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
In server mode, a client certificate request is sent to the client. 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. Any verification error immediately aborts the TLS handshake
Use of this setting requires a valid set of CA certificates to be passed, either to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314 or as a value of the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3516 parameter 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'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. In this mode, certificates are required from the other side of the socket connection; 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 will be raised if no certificate is provided, or if its validation fails. 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
Use of this setting requires a valid set of CA certificates to be passed, either to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv314 or as a value of the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3516 parameter 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'9class ssl. VerifyMode ¶
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. In this mode, certificate revocation lists [CRLs] are not checked. By default OpenSSL does neither require nor verify CRLs
Mới trong phiên bản 3. 4
ssl. VERIFY_CRL_CHECK_LEAF ¶Possible value for
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 certificates
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 ¶Possible value for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583. It instructs OpenSSL to prefer trusted certificates when building the trust chain to validate a certificate. This flag is enabled by default
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. It instructs OpenSSL to accept intermediate CAs in the trust store to be treated as trust-anchors, in the same way as the self-signed root CA certificates. This makes it possible to trust certificates issued by an intermediate CA without having to trust its ancestor root CA
Mới trong phiên bản 3. 10
class ssl. Xác minh cờ ¶ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3557 collection of VERIFY_* constants
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
Cảnh báo
SSL version 2 is insecure. 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 has removed support for 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
Cảnh báo
SSL phiên bản 3 không an toàn. Its use is highly discouraged
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. Use the default protocol
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 instead. ssl. PROTOCOL_TLSv1 ¶
Selects TLS version 1. 0 as the channel encryption protocol
Deprecated since version 3. 6. OpenSSL has deprecated all version specific protocols.
ssl. PROTOCOL_TLSv1_1 ¶Selects TLS version 1. 1 as the channel encryption protocol. Available only with openssl version 1. 0. 1+
Mới trong phiên bản 3. 4
Deprecated since version 3. 6. OpenSSL has deprecated all version specific protocols.
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
Deprecated since version 3. 6. OpenSSL has deprecated all version specific protocols.
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 is deprecated
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 is deprecated
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. 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. Nó ngăn các đồng nghiệp chọn TLSv1. 1 as the protocol version. 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. Nó ngăn các đồng nghiệp chọn TLSv1. 2 as the protocol version. 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 ¶Ngăn chặn TLSv1. 3 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. 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
Mới trong phiên bản 3. 7
Deprecated since version 3. 7. Tùy chọn này không được dùng nữa kể từ OpenSSL 1. 1. 0. Nó đã được thêm vào 2. 7. 15, 3. 6. 3 and 3. 7. 0 để tương thích ngược với 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
Mới trong phiên bản 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
Mới trong phiên bản 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
Mới trong phiên bản 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
Mới trong phiên bản 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
Mới trong phiên bản 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 ¶
Prevent client side from requesting a session ticket
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
Mới trong phiên bản 3. 7
ssl. HAS_ECDH ¶Whether the OpenSSL library has built-in support for the Elliptic Curve-based Diffie-Hellman key exchange. This should be true unless the feature was explicitly disabled by the distributor
Mới trong phiên bản 3. 3
ssl. HAS_SNI ¶Whether the OpenSSL library has built-in support for the Server Name Indication extension [as defined in RFC 6066]
Mới trong phiên bản 3. 2
ssl. HAS_NPN ¶Whether the OpenSSL library has built-in support for the Next Protocol Negotiation as described in the Application Layer Protocol Negotiation. When true, you can use the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3615 method to advertise which protocols you want to support
Mới trong phiên bản 3. 3
ssl. HAS_SSLv2 ¶Whether the OpenSSL library has built-in support for the SSL 2. 0 protocol
Mới trong phiên bản 3. 7
ssl. HAS_SSLv3 ¶Whether the OpenSSL library has built-in support for the SSL 3. 0 protocol
Mới trong phiên bản 3. 7
ssl. HAS_TLSv1 ¶Whether the OpenSSL library has built-in support for the TLS 1. 0 protocol
Mới trong phiên bản 3. 7
ssl. HAS_TLSv1_1 ¶Whether the OpenSSL library has built-in support for the TLS 1. 1 protocol
Mới trong phiên bản 3. 7
ssl. HAS_TLSv1_2 ¶Whether the OpenSSL library has built-in support for the TLS 1. 2 protocol
Mới trong phiên bản 3. 7
ssl. HAS_TLSv1_3 ¶Whether the OpenSSL library has built-in support for the TLS 1. 3 protocol
Mới trong phiên bản 3. 7
ssl. CHANNEL_BINDING_TYPES ¶List of supported TLS channel binding types. Strings in this list can be used as arguments to
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3616
Mới trong phiên bản 3. 3
ssl. OPENSSL_VERSION ¶The version string of the OpenSSL library loaded by the interpreter
>>> 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 ¶Số phiên bản thô của thư viện OpenSSL, dưới dạng một số nguyê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'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
Mới trong phiên bản 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
Deprecated since version 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. The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3650 method was added.
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. Use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3645 instead of
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3667. SLSỔ cắm. 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
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 write would block.
Vì bất cứ lúc nào cũng có thể đàm phán lại, một cuộc gọi đến
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672 cũng có thể gây ra các hoạt động đọc
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.
Ghi chú
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
Changed in version 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
Ghi chú
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
Mới trong phiên bản 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. Currently only the ‘tls-unique’ channel binding, defined by RFC 5929, is supported.
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
Mới trong phiên bản 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
Mới trong phiên bản 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
Ghi chú
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
SSLSocket. 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. If the SSL socket was created using the 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'9 function [rather than
>>> 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], this is a custom context object created for this SSL socket
Mới trong phiên bản 3. 2
SSLSocket. server_side ¶A boolean which 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 for server-side sockets and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3500 for client-side sockets
Mới trong phiên bản 3. 2
SSLSocket. server_hostname ¶Hostname of the server.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv335 type, or
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv315 for server-side socket or if the hostname was not specified in the constructor
Mới trong phiên bản 3. 2
Changed in version 3. 7. The attribute is now always ASCII text. When
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv337 is an internationalized domain name [IDN], this attribute now stores the A-label form [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv338], rather than the U-label form [
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv339]. SSLSocket. session ¶
The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv340 for this SSL connection. The session is available for client and server side sockets after the TLS handshake has been performed. For client sockets the session can be set before
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv341 has been called to reuse a session
Mới trong phiên bản 3. 6
SSLSocket. session_reused ¶Mới trong phiên bản 3. 6
SSL Contexts¶
Mới trong phiên bản 3. 2
An SSL context holds various data longer-lived than single SSL connections, such as SSL configuration options, certificate[s] and private key[s]. It also manages a cache of SSL sessions for server-side sockets, in order to speed up repeated connections from the same clients
class ssl. SSLContext[protocol=None] ¶Create a new SSL context. You may pass protocol which must be one of the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv342 constants defined in this module. The parameter specifies which version of the SSL protocol to use. Typically, the server chooses a particular protocol version, and the client must adapt to the server’s choice. Most of the versions are not interoperable with the other versions. If not specified, the default 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'13; it provides the most compatibility with other versions
Here’s a table showing which versions in a client [down the side] can connect to which versions in a server [along the top]
client / server
SSLv2
SSLv3
TLS 3
TLSv1
TLSv1. 1
TLSv1. 2
SSLv2
yes
no
no 1
no
no
no
SSLv3
no
yes
no 2
no
no
no
TLS [SSLv23] 3
no 1
no 2
yes
yes
yes
yes
TLSv1
no
no
yes
yes
no
no
TLSv1. 1
no
no
yes
no
yes
no
TLSv1. 2
no
no
yes
no
no
yes
Footnotes
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 disables SSLv2 with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 by default2[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 disables SSLv3 with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 by default3[1,2]
TLS 1. 3 protocol will be available 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 in OpenSSL >= 1. 1. 1. There is no dedicated PROTOCOL constant for just TLS 1. 3
See also
>>> 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 lets the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv35 module choose security settings for a given purpose
Changed in version 3. 6. The context is created with secure default values. The options
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,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv353,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv354,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 [except for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv356], and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv319 [except for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3519] are set by default. The initial cipher suite list contains only
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv359 ciphers, no
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv360 ciphers and no
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv361 ciphers [except for
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv356].
Deprecated since version 3. 10.
>>> 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 without protocol argument is deprecated. The context class will either require
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 in the future.
Changed in version 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.
>>> 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 have the following methods and attributesSSLContext. cert_store_stats[] ¶
Get statistics about quantities of loaded X. 509 certificates, count of 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]¶Load a private key and the corresponding certificate. 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. Otherwise the private key will be taken from certfile as well. See the discussion of Certificates for more information on how the certificate is stored in the certfile.
The password argument may be a function to call to get the password for decrypting the private key. It will only be called if the private key is encrypted and a password is necessary. It will be called with no arguments, and it should return a string, bytes, or 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
If the password argument is not specified and a password is required, OpenSSL’s built-in password prompting mechanism will be used to interactively prompt the user for a password
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 if the private key doesn’t match with the certificate
Changed in version 3. 3. Mật khẩu đối số tùy chọn mới.
SSLContext. load_default_certs[purpose=Purpose. SERVER_AUTH] ¶Load a set of default “certification authority” [CA] certificates from default locations. On Windows it loads CA certs from the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3531 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3532 system stores. On all systems it calls
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3521. In the future the method may load CA certificates from other locations, too
The purpose flag specifies what kind of CA certificates are loaded. 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]¶Load a set of “certification authority” [CA] certificates used to validate other peers’ certificates when
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 is other than
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
This method can also load certification revocation lists [CRLs] in PEM or DER format. In order to make use of CRLs,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3583 must be configured properly
The cafile string, if present, is the path to a file of concatenated CA certificates in PEM format. See the discussion of Certificates for more information about how to arrange the certificates in this file.
The capath string, if present, is the path to a directory containing several CA certificates in PEM format, following an OpenSSL specific layout
The cadata object, if present, is either an ASCII string of one or more PEM-encoded certificates or a bytes-like object of DER-encoded certificates. Like with capath extra lines around PEM-encoded certificates are ignored but at least one certificate must be present.
Đã 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]¶Get a list of loaded “certification authority” [CA] certificates. 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
Ghi chú
Certificates in a capath directory aren’t loaded unless they have been used at least once
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. The list is in order of cipher priority. See
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3552
Ví 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[] ¶Load a set of default “certification authority” [CA] certificates from a filesystem path defined when building the OpenSSL library. Unfortunately, there’s no easy way to know whether this method succeeds. no error is returned if no certificates are to be found. When the OpenSSL library is provided as part of the operating system, though, it is likely to be configured properly
SSLContext. set_ciphers[ciphers] ¶Set the available ciphers for sockets created with this context. It should be a string in the OpenSSL cipher list format. If no cipher can be selected [because compile-time options or other configuration forbids use of all the specified ciphers], 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 will be raised
Ghi chú
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 cipher suites cannot be disabled with
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv385SSLContext. set_alpn_protocols[protocols] ¶
Specify which protocols the socket should advertise during the SSL/TLS handshake. It should be a list of ASCII strings, like
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv386, ordered by preference. The selection of a protocol will happen during the handshake, and will play out according to RFC 7301. After a successful handshake, the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv387 method will return the agreed-upon protocol
This method will raise
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv323 if
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv389 is
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. The selection of a protocol will happen during the handshake, and will play out according to the Application Layer Protocol Negotiation. After a successful handshake, the
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv392 method will return the agreed-upon protocol
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
Mới trong phiên bản 3. 3
Deprecated since version 3. 10. NPN has been superseded by ALPN
SSLContext. sni_callback ¶Register a callback function that will be called after the TLS Client Hello handshake message has been received by the SSL/TLS server when the TLS client specifies a server name indication. 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ủ
Only one callback can be set per
>>> 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. Calling this function a subsequent time will disable the previously registered callback
Hàm gọi lại sẽ được gọi với ba đối số; . Đối số tên máy chủ là văn bản. For internationalized domain name, the server name is an IDN A-label [
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ủ
Due to the early negotiation phase of the TLS connection, only limited methods and attributes are usable like
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv387 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'03. The
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 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'09 methods require that the TLS connection has progressed beyond the TLS Client Hello and therefore will not return meaningful values nor can they be called safely
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. If a TLS failure is required, a constant
>>> 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 can be returned. 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
If an exception is raised from the sni_callback function the TLS connection will terminate with a fatal TLS alert message
>>> 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
Mới trong phiên bản 3. 7
SSLContext. set_servername_callback[server_name_callback] ¶This is a legacy API retained for backwards compatibility. When possible, you should 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'15 instead. Server_name_callback đã cho tương tự như sni_callback, ngoại trừ khi tên máy chủ là một tên miền quốc tế hóa được mã hóa IDN, thì 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]
If there is an decoding error on the server name, the TLS connection will terminate with 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'12 fatal TLS alert message to the client
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]. Using DH key exchange improves forward secrecy at the expense of computational resources [both on the server and on the client]. The dhfile parameter should be the path to a file containing DH parameters in PEM format
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
Mới trong phiên bản 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 is significantly faster than regular DH while arguably as secure. 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
Mới trong phiên bản 3. 3
See also
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] ¶Wrap an existing Python socket sock and return an instance 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'23 [default
>>> 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ó. sock must be a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3547 socket; other socket types are unsupported
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. This allows a single server to host multiple SSL-based services with distinct certificates, quite similarly to HTTP virtual hosts. Specifying server_hostname will raise a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3685 if server_side is true
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
The parameter
>>> 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 specifies how 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'36 method should signal unexpected EOF from the other end of the connection. 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 xuất hiện từ ổ cắm bên dưới;
session, see
>>> 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
Đã thay đổi trong phiên bản 3. 5. Luôn cho phép server_hostname được chuyển, ngay cả khi OpenSSL không có SNI.
Changed in version 3. 6. đối số phiên đã được thêm vào.
Đã thay đổi trong phiên bản 3. 7. The method returns an instance 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'23 instead of hard-coded
>>> 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. SSLContext. sslsocket_class ¶
The return type 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'4, defaults 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. The attribute can be overridden on instance of class in order to return a custom 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'7
Mới trong phiên bản 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]. The SSL routines will read input data from the incoming BIO and write data to the outgoing BIO
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
Changed in version 3. 6. đối số phiên đã được thêm vào.
Đã thay đổi trong phiên bản 3. 7. The method returns an instance 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'45 instead of hard-coded
>>> 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. SSLContext. sslobject_class ¶
The return type 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'50, defaults 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'46. The attribute can be overridden on instance of class in order to return a custom 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'46
Mới trong phiên bản 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. A dictionary is returned which maps the names of each piece of information to their numeric values. For example, here is the total number of hits and misses in the session cache since the context was created
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv353SSLContext. check_hostname ¶
Whether to match the peer cert’s hostname 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'33. The context’s
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 must be set to
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, and you must pass server_hostname 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'9 in order to match the hostname. 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. The
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 protocol enables hostname checking by default. With other protocols, hostname checking must be enabled explicitly
Ví dụ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv354
Mới trong phiên bản 3. 4
Changed in version 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 ¶
Write TLS keys to a keylog file, whenever key material is generated or received. The keylog file is designed for debugging purposes only. The file format is specified by NSS and used by many traffic analyzers such as 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 ¶A
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3627 enum member representing the highest supported TLS version. The value defaults 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'69. The attribute is read-only for protocols other than
>>> 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, and
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. The implementation does not prevent invalid combination. Ví dụ: bối cảnh 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'76 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'77 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'73 được đặt thành
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3628 sẽ không thể thiết lập TLS 1. 2 connection
Mới trong phiên bản 3. 7
SSLContext. minimum_version ¶Like
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 except it is the lowest supported version 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'81
Mới trong phiên bản 3. 7
SSLContext. num_tickets ¶Control the number of TLS 1. 3 session tickets of a
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv317 context. 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. options ¶An integer representing the set of SSL options enabled on this context. The default value 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'83, but you can specify other options such as
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv318 by ORing them together
Changed in version 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. Use
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3602 and
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3603 instead. SSLContext. post_handshake_auth ¶
Enable TLS 1. 3 xác thực ứng dụng khách sau bắt tay. Post-handshake auth is disabled by default and a server can only request a TLS client certificate during the initial handshake. When enabled, a server may request a TLS client certificate at any time after the handshake
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. protocol ¶Phiên bản giao thức được chọn khi xây dựng bối 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]
Mới trong phiên bản 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 include workarounds for previous versions.
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. This attribute is read-only
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ỉ. You can set flags like
>>> 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 by ORing them together. 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. Thuộc tính này phải là một trong số
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 hoặc
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
Giấy chứng nhận¶
Certificates in general are part of a public-key / private-key system. Trong hệ thống này, mỗi hiệu trưởng, [có thể là một máy, một người hoặc một tổ chức] được gán một khóa mã hóa hai phần duy nhất. Một phần của khóa là công khai và được gọi là khóa chung; . The two parts are related, in that if you encrypt a message with one of the parts, you can decrypt it with the other part, and only with the other part
A certificate contains information about two principals. It contains the name of a subject, and the subject’s public key. Nó cũng chứa một tuyên bố của người ủy nhiệm thứ hai, người phát hành, rằng chủ thể là người mà họ tuyên bố là và đây thực sự là khóa công khai của chủ thể. Tuyên bố của tổ chức phát hành được ký bằng khóa riêng của tổ chức phát hành mà chỉ tổ chức phát hành mới biết. Tuy nhiên, bất kỳ ai cũng có thể xác minh tuyên bố của tổ chức phát hành bằng cách tìm khóa công khai của tổ chức phát hành, giải mã tuyên bố với nó và so sánh nó với thông tin khác trong chứng chỉ. Chứng chỉ cũng chứa thông tin về khoảng thời gian mà nó có hiệu lực. Điều này được thể hiện dưới dạng hai trường, được gọi là “not Before” và “not After”
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. Nỗ lực kết nối có thể được đặt để tăng ngoại lệ nếu xác thực không thành công. 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ỉ. They should be formatted as “PEM” [see RFC 1422], which is a base-64 encoded form wrapped with a header line and a footer line
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¶
If you are going to require validation of the other side of the connection’s certificate, you need to provide a “CA certs” file, filled with the certificate chains for each issuer you are willing to trust. Một lần nữa, tệp này chỉ chứa các chuỗi này được nối với nhau. Để xác thực, Python sẽ sử dụng chuỗi đầu tiên mà nó tìm thấy trong tệp phù hợp. Tệp chứng chỉ của nền tảng có thể được sử dụng bằng cách gọi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv373, điều này được thực hiện tự độ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'8
Khóa kết hợp và chứng chỉ¶
Thường thì khóa riêng được lưu trữ trong cùng một tệp với chứng chỉ; . 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ý¶
Nếu bạn định tạo một máy chủ cung cấp dịch vụ kết nối được mã hóa SSL, bạn sẽ cần phải có chứng chỉ cho dịch vụ đó. 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
Nhược điểm của chứng chỉ tự ký là nó là chứng chỉ gốc của chính nó và không ai khác sẽ có chứng chỉ đó trong bộ nhớ cache của các chứng chỉ gốc đã biết [và đáng tin cậy] của họ
Ví dụ¶
Kiểm tra hỗ trợ SSL¶
Để kiểm tra sự hiện diện của hỗ trợ SSL trong bản cài đặt Python, mã người dùng nên sử dụng thành ngữ sau
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv362
Vận hành phía máy khách¶
Ví dụ này tạo ngữ cảnh SSL với cài đặt bảo mật được đề xuất cho ổ cắm máy khách, bao gồm xác minh chứng chỉ tự động
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv363
Nếu bạn muốn tự điều chỉnh cài đặt bảo mật, bạn có thể tạo ngữ cảnh từ đầu [nhưng hãy lưu ý rằng bạn có thể không hiểu đúng cài đặt]
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv364
[đoạn mã này giả định rằng hệ điều hành của bạn đặt một gói tất cả các chứng chỉ CA trong
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3109; nếu không, bạn sẽ gặp lỗi và phải điều chỉnh vị trí]
Giao thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 định cấu hình ngữ cảnh để xác thực chứng chỉ và xác minh tên máy chủ.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv371 được đặt thành
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 được đặt 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'94. Tất cả các giao thức khác tạo bối cảnh SSL với các giá trị mặc định không an toàn
Khi bạn sử dụng ngữ cảnh để kết nối với máy chủ,
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 xác thực chứng chỉ máy chủ. nó đảm bảo rằng chứng chỉ máy chủ đã được ký bằng một trong các chứng chỉ CA, kiểm tra tính chính xác của chữ ký và xác minh các thuộc tính khác như tính hợp lệ và danh tính của tên máy chủ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv365
Sau đó, bạn có thể lấy chứng chỉ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv366
Kiểm tra trực quan cho thấy rằng chứng chỉ xác định dịch vụ mong muốn [nghĩa là máy chủ HTTPS
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
Bây giờ kênh SSL đã được thiết lập và chứng chỉ đã được xác minh, bạn có thể tiếp tục nói chuyện với máy chủ
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.
Vận hành phía máy chủ¶
Đối với hoạt động của máy chủ, thông thường, bạn sẽ cần phải có chứng chỉ máy chủ và khóa cá nhân, mỗi khóa trong một tệp. 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
Khi một máy khách kết nối, bạn sẽ gọi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3630 trên ổ cắm để lấy ổ cắm mới từ đầu bên kia và sử dụng 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'4 của ngữ cảnh để tạo ổ cắm SSL phía máy chủ cho kết nối
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¶
Ổ cắm SSL hoạt động hơi khác so với ổ cắm thông thường ở chế độ không chặn. Khi làm việc với ổ cắm không chặn, do đó, có một số điều bạn cần lưu ý
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 sẽ được nâng lên nếu thao tác đọc trên ổ cắm bên dưới là cần thiết vàctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
666 đối với thao tác ghi trên ổ cắm bên dưới. Lưu ý rằng các nỗ lực ghi vào ổ cắm SSL có thể yêu cầu đọc từ ổ cắm bên dưới trước và các nỗ lực đọc từ ổ cắm SSL có thể yêu cầu ghi trước vào ổ cắm bên dướiĐã thay đổi trong phiên bản 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.Gọi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 cho bạn biết rằng ổ cắm cấp hệ điều hành có thể được đọc từ [hoặc ghi vào], nhưng điều đó không ngụ ý rằng có đủ dữ liệu ở lớp SSL phía trên. For example, only part of an SSL frame might have arrived. Do đó, bạn phải sẵn sàng xử lý các lỗi 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'
36 vàctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
128, đồng thời thử lại sau một cuộc gọi khác tớictx = 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. Do đó, trước tiên bạn nên 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'
36 để lấy hết mọi dữ liệu có thể có sẵn, sau đó chỉ chặn cuộc gọictx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
131 nếu vẫn cần thiết[tất nhiên, các quy định tương tự cũng được áp dụng khi sử dụng các nguyên mẫu khác như
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
138 hoặc các nguyên mẫu trong mô-đunctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
139]Bản thân quá trình bắt tay SSL sẽ không bị chặn. 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
See also
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. Nó thăm dò các sự kiện bằng cách sử dụng mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3139 và xử lý các ngoại lệ
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 và
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3125. Nó cũng chạy bắt tay SSL không đồng bộ.
Memory BIO Support¶
New in version 3. 5
Kể từ khi mô-đun SSL được giới thiệu trong Python 2. 6, lớ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 đã cung cấp hai lĩnh vực chức năng liên quan nhưng riêng biệt
Xử lý giao thức SSL
Mạng IO
API IO mạng giống với API được cung cấp 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, 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'7 cũng kế thừa. Điều này cho phép một ổ cắm SSL được sử dụng như một sự thay thế thả xuống cho một ổ cắm thông thường, giúp dễ dàng thêm hỗ trợ SSL vào một ứng dụng hiện có
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. An example is async IO frameworks that want to use a different IO multiplexing model than the “select/poll on a file descriptor” [readiness based] model that is assumed 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 and by the internal OpenSSL socket IO routines. Điều này chủ yếu phù hợp với các nền tảng như Windows, nơi mô hình này không hiệu quả. 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. Lớp này thường được sử dụng bởi các tác giả khung muốn triển khai IO không đồng bộ cho SSL thông qua bộ đệm bộ nhớ
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. Đối tượng này nắm bắt trạng thái của kết nối SSL nhưng không cung cấp bất kỳ IO mạng nào. IO cần được thực hiện thông qua các đối tượng “BIO” riêng biệt là lớp trừu tượng IO của OpenSSL
Lớp này không có hàm tạo công khai. Phiên bả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'46 phải được tạo bằng phương pháp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3155. Phương pháp này sẽ tạo phiên bả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'46 và liên kết nó với một cặp BIO. BIO đến được sử dụng để truyền dữ liệu từ Python sang phiên bản giao thức SSL, trong khi BIO gửi đi được sử dụng để truyền dữ liệu theo cách khác
Các phương pháp sau đây có sẵn
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
Khi so sánh 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'7, đối tượng này thiếu các tính năng sau
Bất kỳ hình thức IO mạng nào;
Không có máy móc do_handshake_on_connect. Bạn phải luôn gọi thủ công
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
41 để bắt đầu bắt tayKhông có cách xử lý của DSP_ragged_eofs. Tất cả các điều kiện cuối tệp vi phạm giao thức đều được báo cáo qua ngoại lệ
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
181Cuộc gọi phương thức
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
173 không trả về bất kỳ thứ gì, không giống như đối với ổ cắm SSL nơi nó trả về ổ cắm bên dướiCuộc gọi lại server_name_callback được chuyển đến
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
617 sẽ nhận phiên bả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'
46 thay vì phiên bả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'
7 làm tham số đầu tiên của nó
Một số lưu ý liên quan đến việc 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'46
Tất cả IO trê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'
46 là không chặn . Điều này có nghĩa là ví dụctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
667 sẽ tăngctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
665 nếu nó cần nhiều dữ liệu hơn BIO sắp tới có sẵn.Không có lệnh gọi cấp mô-đun
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
155 như đối 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'
9. 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'
46 luôn được tạo thông qua 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
Đã thay đổi trong phiên bản 3. 7. ______446 phiên bản phải được tạo bằng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3155. Trong các phiên bản trước, có thể tạo phiên bản trực tiếp. Điều này chưa bao giờ được ghi lại hoặc hỗ trợ chính thức.
SSLObject giao tiếp với thế giới bên ngoài bằng cách sử dụng bộ đệm bộ nhớ. Lớp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3179 cung cấp bộ nhớ đệm có thể được sử dụng cho mục đích này. Nó bao bọc một đối tượng BIO bộ nhớ OpenSSL [IO cơ bản]lớp ssl. Sinh học bộ nhớ ¶
Bộ đệm bộ nhớ có thể được sử dụng để truyền dữ liệu giữa Python và phiên bản giao thức SSL
đang chờ xử lý ¶Trả về số byte hiện có trong bộ nhớ đệm
eof ¶Một boolean cho biết liệu BIO bộ nhớ có hiện tại ở vị trí cuối tệp hay không
đọc[n=- 1]¶Đọc tối đa n byte từ bộ nhớ đệm. Nếu n không được chỉ định hoặc âm, tất cả các byte được trả về
viết[buf] ¶Viết các byte từ buf vào bộ nhớ BIO. Đối số buf phải là một đối tượng hỗ trợ giao thức bộ đệm
Giá trị trả về là số byte được ghi, giá trị này luôn bằng độ dài của buf
write_eof[] ¶Viết một điểm đánh dấu EOF vào bộ nhớ BIO. Sau khi phương thức này được gọi, việc gọi
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3672 là bất hợp pháp. Thuộc tính
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3198 sẽ trở thành true sau khi tất cả dữ liệu hiện có trong bộ đệm đã được đọc
Phiên SSL¶
Mới trong phiên bản 3. 6
lớp ssl. Phiên SSL ¶Đối tượng phiên được sử dụng 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'39id ¶ thời gian ¶ thời gian chờ¶ticket_lifetime_hint¶has_ticket¶
Cân nhắc về Bảo mật¶
Giá trị mặc định tốt nhất¶
Đối với việc sử dụng của khách hàng, nếu bạn không có bất kỳ yêu cầu đặc biệt nào đối với chính sách bảo mật của mình, bạn nên sử dụ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'8 để tạo bối cảnh SSL của mình. Nó sẽ tải các chứng chỉ CA đáng tin cậy của hệ thống, cho phép xác thực chứng chỉ và kiểm tra tên máy chủ, đồng thời cố gắng chọn các cài đặt mật mã và giao thức an toàn hợp lý
Ví dụ: đây là cách bạn sẽ sử dụng lớp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3701 để tạo kết nối an toàn, đáng tin cậy đến máy chủ SMTP
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv33
Nếu cần chứng chỉ ứng dụng khách cho kết nối, thì có thể thêm chứng chỉ đó bằng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3550
Ngược lại, nếu bạn tạo bối cảnh SSL bằng cách tự gọi hàm tạo
>>> 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ó sẽ không có xác thực chứng chỉ cũng như kiểm tra tên máy chủ được bật theo mặc định. Nếu bạn làm như vậy, vui lòng đọc các đoạn bên dưới để đạt được mức bảo mật tốt
Cài đặt thủ công¶
Xác minh chứng chỉ¶
Khi gọi trực tiếp hàm tạo
>>> 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, giá trị mặc định là
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv300. Vì nó không xác thực máy ngang hàng khác, nên nó có thể không an toàn, đặc biệt là ở chế độ máy khách, nơi mà hầu hết thời gian bạn muốn đảm bảo tính xác thực của máy chủ mà bạn đang nói chuyện. Do đó, khi ở chế độ máy khách, bạn nên sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372. Tuy nhiên, bản thân nó không đủ; . Đối với nhiều giao thức và ứng dụng, dịch vụ có thể được xác định bằng tên máy chủ; . Kiểm tra thông thường này được thực hiện tự động khi bật
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3709
Đã thay đổi trong phiên bản 3. 7. Kết hợp tên máy chủ hiện được thực hiện bởi OpenSSL. Python không còn sử dụng
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3680.
Ở chế độ máy chủ, nếu bạn muốn xác thực ứng dụng khách của mình bằng lớp SSL [thay vì sử dụng cơ chế xác thực cấp cao hơn], bạn cũng sẽ phải chỉ định
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv372 và kiểm tra tương tự chứng chỉ ứng dụng khách
Phiên bản giao thức¶
SSL phiên bản 2 và 3 được coi là không an toàn và do đó nguy hiểm khi sử dụng. Nếu bạn muốn khả năng tương thích tối đa giữa máy khách và máy chủ, bạn nên sử dụng
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 làm phiên bản giao thức. SSLv2 và SSLv3 bị tắt theo mặc định
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv34
Bối cảnh SSL được tạo ở trên sẽ chỉ cho phép 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ủ. Theo mặc định,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv316 ngụ ý xác thực chứng chỉ và kiểm tra tên máy chủ. Bạn phải tải chứng chỉ vào ngữ cảnh
Lựa chọn mật mã¶
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. Bắt đầu từ Python 3. 2. 3, mô-đun ssl tắt một số mật mã yếu nhất định theo mặc định, nhưng bạn có thể muốn hạn chế hơn nữa lựa chọn mật mã. Đảm bảo đọc tài liệu của OpenSSL về định dạng danh sách mật mã. Nếu bạn muốn kiểm tra xem mật mã nào được kích hoạt bởi một danh sách mật mã nhất định, hãy sử dụng lệnh
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3716 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3717 trên hệ thống của bạn
Đa xử lý¶
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. Các ứng dụng phải thay đổi trạng thái PRNG của quy trình gốc nếu chúng sử dụng bất kỳ tính năng SSL nào với
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3720. Bất kỳ cuộc gọi thành công nào 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'92,
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3722 hoặc
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3723 là đủ
TLS 1. 3¶
Mới trong phiên bản 3. 7
TLS 1. 3 hoạt động hơi khác so với phiên bản trước của TLS/SSL. Một số TLS mới 1. 3 tính năng chưa khả dụng
TLS 1. 3 sử dụng một bộ mật mã khác nhau. Tất cả các bộ mật mã AES-GCM và ChaCha20 đều được bật theo mặc định. Phương pháp
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
552 không thể bật hoặc tắt bất kỳ TLS 1 nào. Chưa có 3 mật mã, nhưngctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
716 trả về chúngVé phiên không còn được gửi như một phần của quá trình bắt tay ban đầu và được xử lý theo cách khác.
ctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
726 vàctx = ssl.create_default_context[Purpose.CLIENT_AUTH] ctx.options &= ~ssl.OP_NO_SSLv3
40 không tương thích với TLS 1. 3Chứng chỉ phía máy khách cũng không còn được xác minh trong quá trình bắt tay ban đầu. Máy chủ có thể yêu cầu chứng chỉ bất cứ lúc nào. Máy khách xử lý các yêu cầu chứng chỉ trong khi họ gửi hoặc nhận dữ liệu ứng dụng từ máy chủ
TLS 1. 3 tính năng như dữ liệu ban đầu, yêu cầu chứng chỉ máy khách TLS bị trì hoãn, cấu hình thuật toán chữ ký và tạo lại khóa chưa được hỗ trợ
Pip tìm chứng chỉ ở đâu?
Mới trong phiên bản 1. 3. Theo mặc định, pip sẽ thực hiện xác minh chứng chỉ SSL cho các kết nối mạng mà nó thực hiện qua HTTPS. Chúng phục vụ để ngăn chặn các cuộc tấn công trung gian đối với các gói tải xuống. Điều này không sử dụng kho chứng chỉ hệ thống mà thay vào đó, sử dụng kho chứng chỉ CA đi kèm từ certifi .Python có sử dụng kho lưu trữ chứng chỉ hệ thống không?
Thư viện python-certifi-win32 sử dụng kho chứng chỉ Windows để kiểm tra tính hợp lệ của chứng chỉ.Trình duyệt xác minh CA như thế nào?
Để xác minh chứng chỉ, trình duyệt sẽ lấy một chuỗi chứng chỉ, mỗi chứng chỉ đã ký vào chứng chỉ tiếp theo theo trình tự, kết nối gốc của CA đang ký với chứng chỉ của máy chủ. This sequence of certificates is called a certification path.CA xác minh chứng chỉ như thế nào?
CA xác minh xem thông tin trên chứng chỉ có chính xác hay không và sau đó ký vào đó bằng khóa riêng [của CA] . Sau đó, nó sẽ trả lại chứng chỉ máy chủ đã ký cho bạn. Bạn nhập chứng chỉ máy chủ đã ký vào máy chủ của mình.