ssl --- ãœã±ãããªããžã§ã¯ãçšã® TLS/SSL ã©ãããŒÂ¶
Source code: Lib/ssl.py
This module provides access to Transport Layer Security (often known as "Secure Sockets Layer") encryption and peer authentication facilities for network sockets, both client-side and server-side. This module uses the OpenSSL library.
This is an optional module. If it is missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see ãªãã·ã§ã³ã®ã¢ãžã¥ãŒã«ã®èŠä»¶.
泚é
Some behavior may be platform dependent, since calls are made to the operating system socket APIs. The installed version of OpenSSL may also cause variations in behavior. For example, TLSv1.3 comes with OpenSSL version 1.1.1.
èŠå
ã»ãã¥ãªãã£ã§èæ ®ãã¹ãç¹ ãèªãŸãã«ãã®ã¢ãžã¥ãŒã«ã䜿çšããªãã§ãã ãããSSL ã®ããã©ã«ãèšå®ã¯ã¢ããªã±ãŒã·ã§ã³ã«ååã§ã¯ãªãã®ã§ãèªãŸãªãå Žåã¯ã»ãã¥ãªãã£ã«èª€ã£ãæèãæã£ãŠããŸããããããŸããã
Availability: not WASI.
ãã®ã¢ãžã¥ãŒã«ã¯ WebAssembly ã§ã¯åäœããªãããå©çšäžå¯ã§ãã詳ããã¯ãWebAssembly ãã©ãããã©ãŒã ãèŠãŠãã ããã
ãã®ã»ã¯ã·ã§ã³ã§ã¯ã ssl ã¢ãžã¥ãŒã«ã®ãªããžã§ã¯ããšé¢æ°ã解説ããŸãã TLS, SSL, èšŒææžã«é¢ããããäžè¬çãªæ
å ±ã¯ãæ«å°Ÿã«ãã "See Also" ã®ã»ã¯ã·ã§ã³ãåç
§ããŠãã ããã
This module provides a class, ssl.SSLSocket, which is derived from the
socket.socket type, and provides a socket-like wrapper that also
encrypts and decrypts the data going over the socket with SSL. It supports
additional methods such as getpeercert(), which retrieves the
certificate of the other side of the connection, cipher(), which
retrieves the cipher being used for the secure connection or
get_verified_chain(), get_unverified_chain() which retrieves
certificate chain.
ããæŽç·Žãããã¢ããªã±ãŒã·ã§ã³ã®ããã«ã ssl.SSLContext ã¯ã©ã¹ãèšå®ãšèšŒææžã®ç®¡çã®å©ããšãªãã§ãããããã㯠SSLContext.wrap_socket() ã¡ãœãããéã㊠SSL ãœã±ãããäœæããããšã§åŒãç¶ãããŸãã
ããŒãžã§ã³ 3.5.3 ã§å€æŽ: Updated to support linking with OpenSSL 1.1.0
ããŒãžã§ã³ 3.6 ã§å€æŽ: OpenSSL 0.9.8, 1.0.0, 1.0.1 ã¯å»æ¢ãããŠããããã¯ããµããŒããããŠããŸãããssl ã¢ãžã¥ãŒã«ã¯ãå°æ¥çã« OpenSSL 1.0.2 ãŸã㯠1.1.0 ãå¿ èŠãšããããã«ãªããŸãã
ããŒãžã§ã³ 3.10 ã§å€æŽ: PEP 644 has been implemented. The ssl module requires OpenSSL 1.1.1 or newer.
Use of deprecated constants and functions result in deprecation warnings.
Functions, constants, and exceptions¶
ãœã±ããã®äœæÂ¶
Instances of SSLSocket must be created using the
SSLContext.wrap_socket() method. The helper function
create_default_context() returns a new context with secure default
settings.
Client socket example with default context and IPv4/IPv6 dual stack:
import socket
import ssl
hostname = 'www.python.org'
context = ssl.create_default_context()
with socket.create_connection((hostname, 443)) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
print(ssock.version())
Client socket example with custom context and IPv4:
hostname = 'www.python.org'
# PROTOCOL_TLS_CLIENT requires valid cert chain and hostname
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations('path/to/cabundle.pem')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
with context.wrap_socket(sock, server_hostname=hostname) as ssock:
print(ssock.version())
Server socket example listening on localhost IPv4:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain('/path/to/certchain.pem', '/path/to/private.key')
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind(('127.0.0.1', 8443))
sock.listen(5)
with context.wrap_socket(sock, server_side=True) as ssock:
conn, addr = ssock.accept()
...
ã³ã³ããã¹ãã®äœæÂ¶
ã³ã³ãããšã³ã¹é¢æ°ããå
±éã®ç®çã§äœ¿çšããã SSLContext ãªããžã§ã¯ããäœæããã®ã«åœ¹ç«ã¡ãŸãã
- ssl.create_default_context(purpose=Purpose.SERVER_AUTH, *, cafile=None, capath=None, cadata=None)¶
Return a new
SSLContextobject with default settings for the given purpose. The settings are chosen by thesslmodule, and usually represent a higher security level than when calling theSSLContextconstructor directly.cafile, capath, cadata ã¯èšŒææžã®æ€èšŒã§ä¿¡çšãããªãã·ã§ã³ã® CA èšŒææžã§ã
SSLContext.load_verify_locations()ã®ãã®ãšåãã§ããããã 3 ã€ãã¹ãŠãNoneã§ããã°ããã®é¢æ°ã¯ä»£ããã«ã·ã¹ãã ã®ããã©ã«ãã® CA èšŒææžãä¿¡çšããŠéžæããããšãã§ããŸããThe settings are:
PROTOCOL_TLS_CLIENTorPROTOCOL_TLS_SERVER,OP_NO_SSLv2, andOP_NO_SSLv3with high encryption cipher suites without RC4 and without unauthenticated cipher suites. PassingSERVER_AUTHas purpose setsverify_modetoCERT_REQUIREDand either loads CA certificates (when at least one of cafile, capath or cadata is given) or usesSSLContext.load_default_certs()to load default CA certificates.When
keylog_filenameis supported and the environment variableSSLKEYLOGFILEis set,create_default_context()enables key logging.The default settings for this context include
VERIFY_X509_PARTIAL_CHAINandVERIFY_X509_STRICT. These make the underlying OpenSSL implementation behave more like a conforming implementation of RFC 5280, in exchange for a small amount of incompatibility with older X.509 certificates.泚é
ãããã³ã«ããªãã·ã§ã³ãæå·æ¹åŒãã®ä»ã®èšå®ã¯ãäºåã«éæšå¥šã®ç¶æ ã«ããããšãªãããã£ãšå¶éã®åŒ·ãå€ã«å€æŽãããå ŽåããããŸãããããã®å€ã¯ãäºææ§ãšå®å šæ§ãšã®åŠ¥åœãªãã©ã³ã¹ããšã£ãŠæ±ºããããŸãã
ãããããªãã®ã¢ããªã±ãŒã·ã§ã³ãç¹å®ã®èšå®ãå¿ èŠãšããå Žåã
SSLContextãäœã£ãŠèªåèªèº«ã§èšå®ãé©çšãã¹ãã§ããæ³šé
ããçš®ã®å€ãã¯ã©ã€ã¢ã³ãããµãŒããæ¥ç¶ããããšè©Šã¿ãŠããå Žåã«ããã®é¢æ°ã§äœããã
SSLContextã "Protocol or cipher suite mismatch" ã§å§ãŸããšã©ãŒãèµ·ããã®ãç®æãããããã¯ããã®é¢æ°ãOP_NO_SSLv3ã䜿ã£ãŠé€å€ããŠãã SSL 3.0 ãããµããŒãããŠããªãã®ã§ããããSSL 3.0 㯠å®ç§ã«ã¶ã£å£ããŠãã ããšãåºãç¥ãããŠããŸããããã§ããŸã ãã®é¢æ°ã䜿ã£ãŠããã ã SSL 3.0 æ¥ç¶ãèš±å¯ããããšæããªãã°ãããããã®ããã«åæå¹åã§ããŸã:ctx = ssl.create_default_context(Purpose.CLIENT_AUTH) ctx.options &= ~ssl.OP_NO_SSLv3
泚é
This context enables
VERIFY_X509_STRICTby default, which may reject pre-RFC 5280 or malformed certificates that the underlying OpenSSL implementation otherwise would accept. While disabling this is not recommended, you can do so using:ctx = ssl.create_default_context() ctx.verify_flags &= ~ssl.VERIFY_X509_STRICT
Added in version 3.4.
ããŒãžã§ã³ 3.4.4 ã§å€æŽ: ããã©ã«ãã®æå·èšå®ãã RC4 ãé€ãããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: ããã©ã«ãã®æå·åæååã« ChaCha20/Poly1305 ã远å ãããŸããã
ããã©ã«ãã®æå·åæååãã 3DES ãé€ãããŸããã
ããŒãžã§ã³ 3.8 ã§å€æŽ: Support for key logging to
SSLKEYLOGFILEwas added.ããŒãžã§ã³ 3.10 ã§å€æŽ: The context now uses
PROTOCOL_TLS_CLIENTorPROTOCOL_TLS_SERVERprotocol instead of genericPROTOCOL_TLS.ããŒãžã§ã³ 3.13 ã§å€æŽ: The context now uses
VERIFY_X509_PARTIAL_CHAINandVERIFY_X509_STRICTin its default verify flags.
äŸå€Â¶
- exception ssl.SSLError¶
(çŸåšã®ãšãã OpenSSL ã©ã€ãã©ãªã«ãã£ãŠæäŸãããŠãã)äžå±€ã® SSL å®è£ ããã®ãšã©ãŒãäŒããããã®äŸå€ã§ãããã®ãšã©ãŒã¯ãäœã¬ãã«ãªãããã¯ãŒã¯ã®äžã«èŒã£ãŠãããé«ã¬ãã«ãªæå·åãšèªèšŒã¬ã€ã€ãŒã§ã®åé¡ãéç¥ããŸãããã®ãšã©ãŒã¯
OSErrorã®ãµãã¿ã€ãã§ããSSLErrorã€ã³ã¹ã¿ã³ã¹ã®ãšã©ãŒã³ãŒããšã¡ãã»ãŒãžã¯ OpenSSL ã©ã€ãã©ãªã«ãããã®ã§ããããŒãžã§ã³ 3.3 ã§å€æŽ:
SSLErrorã¯ä»¥åã¯socket.errorã®ãµãã¿ã€ãã§ããã- library¶
ãšã©ãŒãèµ·ãã£ã OpenSSL ãµãã¢ãžã¥ãŒã«ã瀺ãããŒã¢ããã¯æååã§ã
SSL,PEM,X509ãªã©ã§ããåãåŸãå€ã¯ OpenSSL ã®ããŒãžã§ã³ã«äŸåããŸããAdded in version 3.3.
- reason¶
ãšã©ãŒãèµ·ãã£ãåå ã瀺ãããŒã¢ããã¯æååã§ã
CERTIFICATE_VERIFY_FAILEDãªã©ã§ããåãåŸãå€ã¯ OpenSSL ã®ããŒãžã§ã³ã«äŸåããŸããAdded in version 3.3.
- exception ssl.SSLZeroReturnError¶
èªã¿åºããããã¯æžã蟌ã¿ã詊ã¿ãããšããéã« SSL ã³ãã¯ã·ã§ã³ãè¡åããéããããŠããŸã£ãå Žåã«éåºããã
SSLErrorãµãã¯ã©ã¹äŸå€ã§ããããã¯äžå±€ã®è»¢é(read TCP)ãéããããšã¯æå³ããªãããšã«æ³šæããŠãã ãããAdded in version 3.3.
- exception ssl.SSLWantReadError¶
èªã¿åºããããã¯æžã蟌ã¿ã詊ã¿ãããšããéã«ããªã¯ãšã¹ããéè¡ãããåã«äžå±€ã® TCP 転éã§åãåãå¿ èŠãããããŒã¿ãäžè¶³ããå Žåã« non-blocking SSL socket ã«ãã£ãŠéåºããã
SSLErrorãµãã¯ã©ã¹äŸå€ã§ããAdded in version 3.3.
- exception ssl.SSLWantWriteError¶
èªã¿åºããããã¯æžã蟌ã¿ã詊ã¿ãããšããéã«ããªã¯ãšã¹ããéè¡ãããåã«äžå±€ã® TCP 転éãéä¿¡ããå¿ èŠãããããŒã¿ãäžè¶³ããå Žåã« non-blocking SSL socket ã«ãã£ãŠéåºããã
SSLErrorãµãã¯ã©ã¹äŸå€ã§ããAdded in version 3.3.
- exception ssl.SSLSyscallError¶
SSL ãœã±ããäžã§æäœãéè¡ããããšããŠããŠã·ã¹ãã ãšã©ãŒãèµ·ãã£ãå Žåã«éåºããã
SSLErrorãµãã¯ã©ã¹äŸå€ã§ããæ®å¿µãªããå ãšãªã£ã errno çªå·ã調ã¹ãç°¡åãªæ¹æ³ã¯ãããŸãããAdded in version 3.3.
- exception ssl.SSLEOFError¶
SSL ã³ãã¯ã·ã§ã³ãåçªã«æã¡åãããéã«éåºããã
SSLErrorãµãã¯ã©ã¹äŸå€ã§ããäžè¬çã«ããã®ãšã©ãŒãèµ·ãã£ããäžå±€ã®è»¢éãåå©çšããããšè©Šã¿ãã¹ãã§ã¯ãããŸãããAdded in version 3.3.
- exception ssl.SSLCertVerificationError¶
A subclass of
SSLErrorraised when certificate validation has failed.Added in version 3.7.
- verify_code¶
A numeric error number that denotes the verification error.
- verify_message¶
A human readable string of the verification error.
- exception ssl.CertificateError¶
SSLCertVerificationErrorã®å¥åã§ããããŒãžã§ã³ 3.7 ã§å€æŽ: äŸå€ã¯
SSLCertVerificationErrorã®å¥åã«ãªããŸããã
ä¹±æ°çæÂ¶
- ssl.RAND_bytes(num, /)¶
æå·åŠçã«åŒ·åºãªæ¬äŒŒä¹±æ°ã® num ãã€ããè¿ããŸããæ¬äŒŒä¹±æ°çæåšã«ååãªããŒã¿ã§ã·ãŒããäžããããŠããªãå ŽåããçŸåšã® RANDOM ã¡ãœããã«æäœããµããŒããããŠããªãå Žåã¯
SSLErrorãéåºããŸããRAND_status()ã䜿ã£ãŠæ¬äŒŒä¹±æ°çæåšã®ç¶æ ããã§ãã¯ã§ããŸããRAND_add()ã䜿ã£ãŠæ¬äŒŒä¹±æ°çæåšã«ã·ãŒããäžããããšãã§ããŸããã»ãšãã©ãã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯
os.urandom()ãæãŸããã§ããæå·è«çã«åŒ·ãæ¬äŒŒä¹±æ°çæåšã«èŠæ±ãããããšã«ã€ããŠã¯ Wikipedia ã®èšäº Cryptographically secure pseudorandom number generator (CSPRNG) (æ¥æ¬èªç: æå·è«çæ¬äŒŒä¹±æ°çæåš) ãåç §ããŠãã ããã
Added in version 3.3.
- ssl.RAND_status()¶
Return
Trueif the SSL pseudo-random number generator has been seeded with 'enough' randomness, andFalseotherwise. You can usessl.RAND_egd()andssl.RAND_add()to increase the randomness of the pseudo-random number generator.
- ssl.RAND_add(bytes, entropy, /)¶
Mix the given bytes into the SSL pseudo-random number generator. The parameter entropy (a float) is a lower bound on the entropy contained in string (so you can always use
0.0). See RFC 1750 for more information on sources of entropy.ããŒãžã§ã³ 3.5 ã§å€æŽ: æžã蟌ã¿å¯èœãª bytes-like object ã䜿çšã§ããããã«ãªããŸããã
èšŒææžã®åãæ±ã¶
- ssl.cert_time_to_seconds(cert_time)¶
Return the time in seconds since the epoch, given the
cert_timestring representing the "notBefore" or "notAfter" date from a certificate in"%b %d %H:%M:%S %Y %Z"strptime format (C locale).äŸã§ãã :
>>> import ssl >>> import datetime as dt >>> timestamp = ssl.cert_time_to_seconds("Jan 5 09:34:43 2018 GMT") >>> timestamp 1515144883 >>> print(dt.datetime.fromtimestamp(timestamp, dt.UTC)) 2018-01-05 09:34:43+00:00
"notBefore" ã "notAfter" ã®æ¥ä»ã«ã¯ GMT ã䜿ããªããã°ãªããŸãã(RFC 5280)ã
ããŒãžã§ã³ 3.5 ã§å€æŽ: å ¥åæååã«æå®ããã 'GMT' ã¿ã€ã ãŸãŒã³ã UTC ãšããŠè§£éããããã«ãªããŸããã以åã¯ããŒã«ã«ã¿ã€ã ã§è§£éããŠããŸããããŸããæŽæ°ãè¿ãããã«ãªããŸãã(å ¥åã«å«ãŸããç§ã®ç«¯æ°ãå«ãŸãªã)ã
- ssl.get_server_certificate(addr, ssl_version=PROTOCOL_TLS_CLIENT, ca_certs=None[, timeout])¶
Given the address
addrof an SSL-protected server, as a (hostname, port-number) pair, fetches the server's certificate, and returns it as a PEM-encoded string. Ifssl_versionis specified, uses that version of the SSL protocol to attempt to connect to the server. If ca_certs is specified, it should be a file containing a list of root certificates, the same format as used for the cafile parameter inSSLContext.load_verify_locations(). The call will attempt to validate the server certificate against that set of root certificates, and will fail if the validation attempt fails. A timeout can be specified with thetimeoutparameter.ããŒãžã§ã³ 3.3 ã§å€æŽ: ãã®é¢æ°ã¯IPv6äºæã«ãªããŸããã
ããŒãžã§ã³ 3.5 ã§å€æŽ: ssl_version ã®ããã©ã«ãããæè¿ã®ãµãŒããžã®æå€§éã®äºææ§ã®ããã«
PROTOCOL_SSLv3ããPROTOCOL_TLSã«å€æŽãããŸãããããŒãžã§ã³ 3.10 ã§å€æŽ: timeout åŒæ°ã远å ãããŸããã
- ssl.DER_cert_to_PEM_cert(der_cert_bytes)¶
DERãšã³ã³ãŒãããããã€ãåãšããŠäžããããèšŒææžããã PEMãšã³ã³ãŒããããããŒãžã§ã³ã®åãèšŒææžãè¿ããŸãã
- ssl.PEM_cert_to_DER_cert(pem_cert_string)¶
PEM 圢åŒã®ASCIIæååãšããŠäžããããèšŒææžãããåãèšŒææžãDERãšã³ã³ãŒããããã€ãåãè¿ããŸãã
- ssl.get_default_verify_paths()¶
OpenSSL ããã©ã«ãã® cafile, capath ãæããã¹ãååä»ãã¿ãã«ã§è¿ããŸãããã¹ã¯
SSLContext.set_default_verify_paths()ã§äœ¿ããããã®ãšåãã§ããæ»ãå€ã¯ named tupleDefaultVerifyPathsã§ã:cafile- cafile ã®è§£æ±ºæžã¿ãã¹ããŸãã¯ãã¡ã€ã«ãååšããªãå Žåã¯Nonecapath- capath ã®è§£æ±ºæžã¿ãã¹ããŸãã¯ãã£ã¬ã¯ããªãååšããªãå Žåã¯Noneopenssl_cafile_env- cafile ãæã OpenSSL ã®ç°å¢å€æ°openssl_cafile- OpenSSL ã«ããŒãã³ãŒãããã cafile ã®ãã¹openssl_capath_env- capath ãæã OpenSSL ã®ç°å¢å€æ°openssl_capath- OpenSSL ã«ããŒãã³ãŒãããã capath ã®ãã¹
Added in version 3.4.
- ssl.enum_certificates(store_name)¶
Windows ã®ã·ã¹ãã èšŒææžã¹ãã¢ããèšŒææžãæœåºããŸãã store_name ã¯
CA,ROOT,MYã®ãã¡ã©ããäžã€ã§ããããWindows ã¯è¿œå ã®èšŒææžã¹ãã¢ãæäŸããŠãããããããŸããããã®é¢æ°ã¯ã¿ãã« (cert_bytes, encoding_type, trust) ã®ãªã¹ãã§è¿ããŸããencoding_type 㯠cert_bytes ã®ãšã³ã³ãŒãã£ã³ã°ã衚ããŸããX.509 ASN.1 ã«å¯Ÿãã
x509_asnã PKCS#7 ASN.1 ããŒã¿ã«å¯Ÿããpkcs_7_asnã®ããããã§ããtrust ã¯ãèšŒææžã®ç®çããOIDS ãå å®¹ã«æã€ set ãšããŠè¡šããããŸãã¯èšŒææžããã¹ãŠã®ç®çã§ä¿¡é Œã§ãããªãã°Trueã§ãã以äžã¯ããã°ã©ã äŸã§ã:
>>> ssl.enum_certificates("CA") [(b'data...', 'x509_asn', {'1.3.6.1.5.5.7.3.1', '1.3.6.1.5.5.7.3.2'}), (b'data...', 'x509_asn', True)]
Availability: Windows.
Added in version 3.4.
- ssl.enum_crls(store_name)¶
Windows ã®ã·ã¹ãã èšŒææžã¹ãã¢ãã CRLs ãæœåºããŸãã store_name ã¯
CA,ROOT,MYã®ãã¡ã©ããäžã€ã§ããããWindows ã¯è¿œå ã®èšŒææžã¹ãã¢ãæäŸããŠãããããããŸããããã®é¢æ°ã¯ã¿ãã« (cert_bytes, encoding_type, trust) ã®ãªã¹ãã§è¿ããŸããencoding_type 㯠cert_bytes ã®ãšã³ã³ãŒãã£ã³ã°ã衚ããŸããX.509 ASN.1 ã«å¯Ÿãã
x509_asnã PKCS#7 ASN.1 ããŒã¿ã«å¯Ÿããpkcs_7_asnã®ããããã§ããAvailability: Windows.
Added in version 3.4.
宿°Â¶
ãã¹ãŠã®å®æ°ã
enum.IntEnumã³ã¬ã¯ã·ã§ã³ãŸãã¯enum.IntFlagã³ã¬ã¯ã·ã§ã³ã«ãªããŸãããAdded in version 3.6.
- ssl.CERT_NONE¶
Possible value for
SSLContext.verify_mode. Except forPROTOCOL_TLS_CLIENT, it is the default mode. With client-side sockets, just about any cert is accepted. Validation errors, such as untrusted or expired cert, are ignored and do not abort the TLS/SSL handshake.In server mode, no certificate is requested from the client, so the client does not send any for client cert authentication.
ãã®ããã¥ã¡ã³ãã®äžã®æ¹ã®ã ã»ãã¥ãªãã£ã§èæ ®ãã¹ãç¹ ã«é¢ããè°è«ãåç §ããŠãã ããã
- ssl.CERT_OPTIONAL¶
Possible value for
SSLContext.verify_mode. In client mode,CERT_OPTIONALhas the same meaning asCERT_REQUIRED. It is recommended to useCERT_REQUIREDfor client-side sockets instead.In server mode, a client certificate request is sent to the client. The client may either ignore the request or send a certificate in order perform TLS client cert authentication. If the client chooses to send a certificate, it is verified. Any verification error immediately aborts the TLS handshake.
Use of this setting requires a valid set of CA certificates to be passed to
SSLContext.load_verify_locations().
- ssl.CERT_REQUIRED¶
Possible value for
SSLContext.verify_mode. In this mode, certificates are required from the other side of the socket connection; anSSLErrorwill be raised if no certificate is provided, or if its validation fails. This mode is not sufficient to verify a certificate in client mode as it does not match hostnames.check_hostnamemust be enabled as well to verify the authenticity of a cert.PROTOCOL_TLS_CLIENTusesCERT_REQUIREDand enablescheck_hostnameby default.With server socket, this mode provides mandatory TLS client cert authentication. A client certificate request is sent to the client and the client must provide a valid and trusted certificate.
Use of this setting requires a valid set of CA certificates to be passed to
SSLContext.load_verify_locations().
- class ssl.VerifyMode¶
CERT_* 宿°ã®
enum.IntEnumã³ã¬ã¯ã·ã§ã³ã§ããAdded in version 3.6.
- ssl.VERIFY_DEFAULT¶
SSLContext.verify_flagsã«æž¡ããå€ã§ãããã®ã¢ãŒãã§ã¯ãèšŒææžå€±å¹ãªã¹ã(CRLs)ã¯ãã§ãã¯ãããŸãããããã©ã«ãã§ã¯ OpenSSL 㯠CRLs ãå¿ èŠãšãããŸãããæ€èšŒã«ã䜿ããŸãããAdded in version 3.4.
- ssl.VERIFY_CRL_CHECK_LEAF¶
SSLContext.verify_flagsã«æž¡ããå€ã§ãããã®ã¢ãŒãã§ã¯ãæ¥ç¶å ã®èšŒææžã®ã¿ããã§ãã¯ããã仲ä»ã® CA èšŒææžã¯ãã§ãã¯ãããŸãããæ¥ç¶å èšŒææžã®çºè¡è (ãã® CA ã®çŽæ¥ã®ç¥å )ã«ãã£ãŠçœ²åããã劥åœãª CRL ãå¿ èŠã§ããSSLContext.load_verify_locationsã§çžå¿ãã CRL ãããŒãããŠããªããã°ãæ€èšŒã¯å€±æããŸããAdded in version 3.4.
- ssl.VERIFY_CRL_CHECK_CHAIN¶
SSLContext.verify_flagsã«æž¡ããå€ã§ãããã®ã¢ãŒãã§ã¯ãæ¥ç¶å ã®èšŒææžãã§ã€ã³å ã®ãã¹ãŠã®èšŒææžã«ã€ããŠã® CRLs ããã§ãã¯ãããŸããAdded in version 3.4.
- ssl.VERIFY_X509_STRICT¶
SSLContext.verify_flagsã«æž¡ããå€ã§ãå£ãã X.509 èšŒææžã«å¯Ÿããã¯ãŒã¯ã¢ã©ãŠã³ããç¡å¹ã«ããŸããAdded in version 3.4.
- ssl.VERIFY_ALLOW_PROXY_CERTS¶
Possible value for
SSLContext.verify_flagsto enables proxy certificate verification.Added in version 3.10.
- ssl.VERIFY_X509_TRUSTED_FIRST¶
SSLContext.verify_flagsã«æž¡ããå€ã§ããOpenSSL ã«å¯ŸããèšŒææžæ€èšŒã®ããã«ä¿¡é Œãã§ã€ã³ãæ§ç¯ããéãä¿¡é Œã§ããèšŒææžãéžã¶ããã«æç€ºããŸããããã¯ããã©ã«ãã§æå¹ã«ãããŠããŸããAdded in version 3.4.4.
- ssl.VERIFY_X509_PARTIAL_CHAIN¶
Possible value for
SSLContext.verify_flags. 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.Added in version 3.10.
- class ssl.VerifyFlags¶
VERIFY_* 宿°ã®
enum.IntFlagã³ã¬ã¯ã·ã§ã³ã§ããAdded in version 3.6.
- ssl.PROTOCOL_TLS¶
ã¯ã©ã€ã¢ã³ããšãµãŒãã®äž¡æ¹ããµããŒããããããã³ã«ããŒãžã§ã³ã®ãã¡ãæã倧ããªãã®ãéžæããŸããååã«åããŠããã®ãªãã·ã§ã³ã¯ "SSL" ãš "TLS" ãããã³ã«ã®ããããéžæã§ããŸãã
Added in version 3.6.
ããŒãžã§ã³ 3.10 ã§éæšå¥š: TLS clients and servers require different default settings for secure communication. The generic TLS protocol constant is deprecated in favor of
PROTOCOL_TLS_CLIENTandPROTOCOL_TLS_SERVER.
- ssl.PROTOCOL_TLS_CLIENT¶
Auto-negotiate the highest protocol version that both the client and server support, and configure the context client-side connections. The protocol enables
CERT_REQUIREDandcheck_hostnameby default.Added in version 3.6.
- ssl.PROTOCOL_TLS_SERVER¶
Auto-negotiate the highest protocol version that both the client and server support, and configure the context server-side connections.
Added in version 3.6.
- ssl.PROTOCOL_SSLv23¶
PROTOCOL_TLSã®ãšã€ãªã¢ã¹ã§ããããŒãžã§ã³ 3.6 ã§éæšå¥š: 代ããã«
PROTOCOL_TLSã䜿çšããŠãã ããã
- ssl.PROTOCOL_SSLv3¶
ãã£ã³ãã«æå·åãããã³ã«ãšããŠSSLããŒãžã§ã³3ãéžæããŸãã
ãã®ãããã³ã«ã¯ã OpenSSL ã
no-ssl3ãªãã·ã§ã³ãã€ããŠã³ã³ãã€ã«ãããŠããå Žåã«ã¯å©çšã§ããŸãããèŠå
SSL version 3 ã¯éã»ãã¥ã¢ã§ãããã®ãããã³ã«ã¯åŒ·ãéæšå¥šã§ãã
ããŒãžã§ã³ 3.6 ã§éæšå¥š: OpenSSL has deprecated all version specific protocols. Use the default protocol
PROTOCOL_TLS_SERVERorPROTOCOL_TLS_CLIENTwithSSLContext.minimum_versionandSSLContext.maximum_versioninstead.
- ssl.PROTOCOL_TLSv1¶
ãã£ã³ãã«æå·åãããã³ã«ãšããŠTLSããŒãžã§ã³1.0ãéžæããŸãã
ããŒãžã§ã³ 3.6 ã§éæšå¥š: OpenSSL has deprecated all version specific protocols.
- ssl.PROTOCOL_TLSv1_1¶
ãã£ã³ãã«æå·åãããã³ã«ãšããŠTLSããŒãžã§ã³1.1ãéžæããŸãã openssl version 1.0.1+ ã®ã¿ã§å©çšå¯èœã§ãã
Added in version 3.4.
ããŒãžã§ã³ 3.6 ã§éæšå¥š: OpenSSL has deprecated all version specific protocols.
- ssl.PROTOCOL_TLSv1_2¶
ãã£ã³ãã«æå·åãããã³ã«ãšããŠTLSããŒãžã§ã³ 1.2 ãéžæããŸãã openssl version 1.0.1+ ã®ã¿ã§å©çšå¯èœã§ãã
Added in version 3.4.
ããŒãžã§ã³ 3.6 ã§éæšå¥š: OpenSSL has deprecated all version specific protocols.
- ssl.OP_ALL¶
çžæã«ãã SSL å®è£ ã®ããŸããŸãªãã°ãåé¿ããããã®ã¯ãŒã¯ã¢ã©ãŠã³ããæå¹ã«ããŸãããã®ãªãã·ã§ã³ã¯ããã©ã«ãã§æå¹ã§ãããããæå¹ã«ããå Žå OpenSSL çšã®åãæå³ã®ãã©ã°
SSL_OP_ALLãã»ããããå¿ èŠã¯ãããŸãããAdded in version 3.2.
- ssl.OP_NO_SSLv2¶
SSLv2 æ¥ç¶ãè¡ãããªãããã«ããŸãããã®ãªãã·ã§ã³ã¯
PROTOCOL_TLSãšçµã¿åããããŠããå Žåã«ã®ã¿é©çšãããŸãããã¢ããããã³ã«ããŒãžã§ã³ãšã㊠SSLv2 ãéžæããªãããã«ããŸããAdded in version 3.2.
ããŒãžã§ã³ 3.6 ã§éæšå¥š: SSLv2 ã¯éæšå¥šã§ã
- ssl.OP_NO_SSLv3¶
SSLv3 æ¥ç¶ãè¡ãããªãããã«ããŸãããã®ãªãã·ã§ã³ã¯
PROTOCOL_TLSãšçµã¿åããããŠããå Žåã«ã®ã¿é©çšãããŸãããã¢ããããã³ã«ããŒãžã§ã³ãšã㊠SSLv3 ãéžæããªãããã«ããŸããAdded in version 3.2.
ããŒãžã§ã³ 3.6 ã§éæšå¥š: SSLv3 ã¯éæšå¥šã§ã
- ssl.OP_NO_TLSv1¶
TLSv1 æ¥ç¶ãè¡ãããªãããã«ããŸãããã®ãªãã·ã§ã³ã¯
PROTOCOL_TLSãšçµã¿åããããŠããå Žåã«ã®ã¿é©çšãããŸãããã¢ããããã³ã«ããŒãžã§ã³ãšã㊠TLSv1 ãéžæããªãããã«ããŸããAdded in version 3.2.
ããŒãžã§ã³ 3.7 ã§éæšå¥š: The option is deprecated since OpenSSL 1.1.0, use the new
SSLContext.minimum_versionandSSLContext.maximum_versioninstead.
- ssl.OP_NO_TLSv1_1¶
TLSv1.1 æ¥ç¶ãè¡ãããªãããã«ããŸãããã®ãªãã·ã§ã³ã¯
PROTOCOL_TLSãšçµã¿åããããŠããå Žåã«ã®ã¿é©çšãããŸãããã¢ããããã³ã«ããŒãžã§ã³ãšã㊠TLSv1.1 ãéžæããªãããã«ããŸããopenssl ããŒãžã§ã³ 1.0.1 以éã§ã®ã¿å©çšã§ããŸããAdded in version 3.4.
ããŒãžã§ã³ 3.7 ã§éæšå¥š: The option is deprecated since OpenSSL 1.1.0.
- ssl.OP_NO_TLSv1_2¶
TLSv1.2 æ¥ç¶ãè¡ãããªãããã«ããŸãããã®ãªãã·ã§ã³ã¯
PROTOCOL_TLSãšçµã¿åããããŠããå Žåã«ã®ã¿é©çšãããŸãããã¢ããããã³ã«ããŒãžã§ã³ãšã㊠TLSv1.2 ãéžæããªãããã«ããŸããopenssl ããŒãžã§ã³ 1.0.1 以éã§ã®ã¿å©çšã§ããŸããAdded in version 3.4.
ããŒãžã§ã³ 3.7 ã§éæšå¥š: The option is deprecated since OpenSSL 1.1.0.
- ssl.OP_NO_TLSv1_3¶
Prevents a TLSv1.3 connection. This option is only applicable in conjunction with
PROTOCOL_TLS. 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.Added in version 3.6.3.
ããŒãžã§ã³ 3.7 ã§éæšå¥š: The option is deprecated since OpenSSL 1.1.0. It was added to 2.7.15 and 3.6.3 for backwards compatibility with OpenSSL 1.0.2.
- ssl.OP_NO_RENEGOTIATION¶
Disable all renegotiation in TLSv1.2 and earlier. Do not send HelloRequest messages, and ignore renegotiation requests via ClientHello.
ãã®ãªãã·ã§ã³ã¯ OpenSSL 1.1.0h 以éã®ã¿ã§äœ¿çšã§ããŸãã
Added in version 3.7.
- ssl.OP_CIPHER_SERVER_PREFERENCE¶
æå·ã®åªå é äœãšããŠãã¯ã©ã€ã¢ã³ãã®ãã®ã§ã¯ãªããµãŒãã®ãã®ã䜿ããŸãããã®ãªãã·ã§ã³ã¯ã¯ã©ã€ã¢ã³ããœã±ãããš SSLv2 ã®ãµãŒããœã±ããã§ã¯å¹æã¯ãããŸããã
Added in version 3.3.
- ssl.OP_SINGLE_DH_USE¶
Prevents reuse 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.
Added in version 3.3.
- ssl.OP_SINGLE_ECDH_USE¶
Prevents reuse 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.
Added in version 3.3.
- ssl.OP_ENABLE_MIDDLEBOX_COMPAT¶
Send dummy Change Cipher Spec (CCS) messages in TLS 1.3 handshake to make a TLS 1.3 connection look more like a TLS 1.2 connection.
ãã®ãªãã·ã§ã³ã¯ OpenSSL 1.1.1 以éã®ã¿ã§äœ¿çšã§ããŸãã
Added in version 3.8.
- ssl.OP_NO_COMPRESSION¶
SSL ãã£ãã«ã§ã®å§çž®ãç¡å¹ã«ããŸããããã¯ã¢ããªã±ãŒã·ã§ã³ã®ãããã³ã«ãèªèº«ã®å§çž®æ¹æ³ããµããŒãããå Žåã«æçšã§ãã
Added in version 3.3.
- class ssl.Options¶
OP_* 宿°ã®
enum.IntFlagã³ã¬ã¯ã·ã§ã³ã§ãã
- ssl.OP_NO_TICKET¶
ã¯ã©ã€ã¢ã³ããµã€ããã»ãã·ã§ã³ãã±ããããªã¯ãšã¹ãããªãããã«ããŸãã
Added in version 3.6.
- ssl.OP_IGNORE_UNEXPECTED_EOF¶
Ignore unexpected shutdown of TLS connections.
ãã®ãªãã·ã§ã³ã¯ OpenSSL 3.0.0以éã®ã¿ã§äœ¿çšã§ããŸãã
Added in version 3.10.
- ssl.OP_ENABLE_KTLS¶
Enable the use of the kernel TLS. To benefit from the feature, OpenSSL must have been compiled with support for it, and the negotiated cipher suites and extensions must be supported by it (a list of supported ones may vary by platform and kernel version).
Note that with enabled kernel TLS some cryptographic operations are performed by the kernel directly and not via any available OpenSSL Providers. This might be undesirable if, for example, the application requires all cryptographic operations to be performed by the FIPS provider.
ãã®ãªãã·ã§ã³ã¯ OpenSSL 3.0.0以éã®ã¿ã§äœ¿çšã§ããŸãã
Added in version 3.12.
- ssl.OP_LEGACY_SERVER_CONNECT¶
Allow legacy insecure renegotiation between OpenSSL and unpatched servers only.
Added in version 3.12.
- ssl.HAS_ALPN¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ RFC 7301 ã§èšè¿°ãããŠãã Application-Layer Protocol Negotiation TLS æ¡åŒµããµããŒãããŠãããã©ããã
Added in version 3.5.
- ssl.HAS_NEVER_CHECK_COMMON_NAME¶
Whether the OpenSSL library has built-in support not checking subject common name and
SSLContext.hostname_checks_common_nameis writeable.Added in version 3.7.
- ssl.HAS_ECDH¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã®æ¥åæ²ç·ãã£ãã£ãŒã»ãã«ãã³éµå ±æããµããŒãããŠãããã©ãããããã¯ããã£ã¹ããªãã¥ãŒã¿ãæç€ºçã«ç¡å¹ã«ããŠããªãéãã¯ãçã§ããã¯ãã§ãã
Added in version 3.3.
- ssl.HAS_SNI¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ (RFC 6066 ã§èšè¿°ãããŠãã) Server Name Indication æ¡åŒµããµããŒãããŠãããã©ããã
Added in version 3.2.
- ssl.HAS_NPN¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ãApplication Layer Protocol Negotiation ã§èšè¿°ãããŠãã Next Protocol Negotiation ããµããŒãããŠãããã©ããã true ã§ããã°ããµããŒãããããããã³ã«ã
SSLContext.set_npn_protocols()ã¡ãœããã§æç€ºããããšãã§ããŸããAdded in version 3.3.
- ssl.HAS_SSLv2¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ SSL 2.0 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_SSLv3¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ SSL 3.0 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_TLSv1¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ TLS 1.0 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_TLSv1_1¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ TLS 1.1 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_TLSv1_2¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ TLS 1.2 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_TLSv1_3¶
OpenSSL ã©ã€ãã©ãªããçµã¿èŸŒã¿ã§ TLS 1.3 ãããã³ã«ããµããŒãããŠãããã©ããã
Added in version 3.7.
- ssl.HAS_PSK¶
Whether the OpenSSL library has built-in support for TLS-PSK.
Added in version 3.13.
- ssl.HAS_PHA¶
Whether the OpenSSL library has built-in support for TLS-PHA.
Added in version 3.14.
- ssl.CHANNEL_BINDING_TYPES¶
ãµããŒããããŠãã TLS ã®ãã£ãã«ãã€ã³ãã£ã³ã°ã®ã¿ã€ãã®ãªã¹ãããªã¹ãå ã®æååã¯
SSLSocket.get_channel_binding()ã®åŒæ°ã«æž¡ããŸããAdded in version 3.3.
- ssl.OPENSSL_VERSION¶
ã€ã³ã¿ããªã¿ã«ãã£ãŠããŒãããã OpenSSL ã©ã€ãã©ãªã®ããŒãžã§ã³æåå:
>>> ssl.OPENSSL_VERSION 'OpenSSL 1.0.2k 26 Jan 2017'
Added in version 3.2.
- ssl.OPENSSL_VERSION_INFO¶
OpenSSL ã©ã€ãã©ãªã®ããŒãžã§ã³æ å ±ã衚ã5ã€ã®æŽæ°ã®ã¿ãã«:
>>> ssl.OPENSSL_VERSION_INFO (1, 0, 2, 11, 15)
Added in version 3.2.
- ssl.OPENSSL_VERSION_NUMBER¶
1ã€ã®æŽæ°ã®åœ¢åŒã®ã OpenSSL ã©ã€ãã©ãªã®çã®ããŒãžã§ã³çªå·:
>>> ssl.OPENSSL_VERSION_NUMBER 268443839 >>> hex(ssl.OPENSSL_VERSION_NUMBER) '0x100020bf'
Added in version 3.2.
- ssl.ALERT_DESCRIPTION_HANDSHAKE_FAILURE¶
- ssl.ALERT_DESCRIPTION_INTERNAL_ERROR¶
- ALERT_DESCRIPTION_*
RFC 5246 ãã®ä»ããã®ã¢ã©ãŒãã®çš®é¡ã§ãã IANA TLS Alert Registry ã«ã¯ãã®ãªã¹ããšãã®æå³ãå®çŸ©ããã RFC ãžã®ãªãã¡ã¬ã³ã¹ãå«ãŸããŠããŸãã
SSLContext.set_servername_callback()ã§ã®ã³ãŒã«ããã¯é¢æ°ã®æ»ãå€ãšããŠäœ¿ãããŸããAdded in version 3.4.
- class ssl.AlertDescription¶
ALERT_DESCRIPTION_* 宿°ã®
enum.IntEnum ã³ã¬ã¯ã·ã§ã³ã§ããAdded in version 3.6.
- Purpose.SERVER_AUTH¶
create_default_context()ãšSSLContext.load_default_certs()ã«æž¡ããªãã·ã§ã³ã§ãããã®å€ã¯ã³ã³ããã¹ãã web ãµãŒãã®èªèšŒã«äœ¿ãããããšã瀺ããŸã (ã§ãã®ã§ãã¯ã©ã€ã¢ã³ããµã€ãã®ãœã±ãããäœãã®ã«äœ¿ãããšã«ãªãã§ããã)ãAdded in version 3.4.
- Purpose.CLIENT_AUTH¶
create_default_context()ãšSSLContext.load_default_certs()ã«æž¡ããªãã·ã§ã³ã§ãããã®å€ã¯ã³ã³ããã¹ãã web ã¯ã©ã€ã¢ã³ãã®èªèšŒã«äœ¿ãããããšã瀺ããŸã (ã§ãã®ã§ããµãŒããµã€ãã®ãœã±ãããäœãã®ã«äœ¿ãããšã«ãªãã§ããã)ãAdded in version 3.4.
- class ssl.SSLErrorNumber¶
SSL_ERROR_* 宿°ã®
enum.IntEnum ã³ã¬ã¯ã·ã§ã³ã§ããAdded in version 3.6.
- class ssl.TLSVersion¶
enum.IntEnumcollection of SSL and TLS versions forSSLContext.maximum_versionandSSLContext.minimum_version.Added in version 3.7.
- TLSVersion.MINIMUM_SUPPORTED¶
- TLSVersion.MAXIMUM_SUPPORTED¶
The minimum or maximum supported SSL or TLS version. These are magic constants. Their values don't reflect the lowest and highest available TLS/SSL versions.
- TLSVersion.SSLv3¶
- TLSVersion.TLSv1¶
- TLSVersion.TLSv1_1¶
- TLSVersion.TLSv1_2¶
- TLSVersion.TLSv1_3¶
SSL 3.0 to TLS 1.3.
ããŒãžã§ã³ 3.10 ã§éæšå¥š: All
TLSVersionmembers exceptTLSVersion.TLSv1_2andTLSVersion.TLSv1_3are deprecated.
SSL sockets¶
- class ssl.SSLSocket(socket.socket)¶
SSL ãœã±ãã㯠socket ãªããžã§ã¯ã ã®ä»¥äžã®ã¡ãœãããæäŸããŸã:
recv(),recv_into()(but passing a non-zeroflagsargument is not allowed)sendfile()(butos.sendfilewill be used for plain-text sockets only, elsesend()will be used)
SSL(ããã³TLS)ãããã³ã«ã¯ TCP ã®äžã«ç¬èªã®æ çµã¿ãæã£ãŠããã®ã§ãSSLãœã±ããã®æœè±¡åã¯ãããã€ãã®ç¹ã§éåžžã® OSã¬ãã«ã®ãœã±ããã®ä»æ§ããéžè±ããããšããããŸããç¹ã« ãã³ããããã³ã°ãœã±ããã«ã€ããŠã®æ³šé ãåç §ããŠãã ããã
SSLSocketã®ã€ã³ã¹ã¿ã³ã¹ã¯SSLContext.wrap_socket()ã¡ãœããã䜿çšããŠäœæãããªããã°ãªããŸãããããŒãžã§ã³ 3.5 ã§å€æŽ:
sendfile()ã¡ãœããã远å ãããŸãããããŒãžã§ã³ 3.5 ã§å€æŽ:
shutdown()ã¯ããã€ããéåä¿¡ããããã³ã«ãœã±ããã®ã¿ã€ã ã¢ãŠãããªã»ããããŸããããœã±ããã®ã¿ã€ã ã¢ãŠãã¯ãã·ã£ããããŠã³ã®æå€§åèšæéã«ãªããŸãããããŒãžã§ã³ 3.6 ã§éæšå¥š:
SSLSocketã€ã³ã¹ã¿ã³ã¹ãçŽæ¥äœæããããšã¯éæšå¥šã§ãããœã±ãããã©ããããããã«SSLContext.wrap_socket()ã䜿çšããŠãã ãããããŒãžã§ã³ 3.7 ã§å€æŽ:
SSLSocketinstances must be created withwrap_socket(). In earlier versions, it was possible to create instances directly. This was never documented or officially supported.ããŒãžã§ã³ 3.10 ã§å€æŽ: Python now uses
SSL_read_exandSSL_write_exinternally. 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 ãœã±ããã«ã¯ã以äžã«ç€ºã远å ã®ã¡ãœãããšå±æ§ããããŸã:
- SSLSocket.read(len=1024, buffer=None)¶
SSL ãœã±ããããããŒã¿ã® len ãã€ããŸã§ãèªã¿åºããèªã¿åºããçµæã
bytesã€ã³ã¹ã¿ã³ã¹ã§è¿ããŸãã buffer ãæå®ãããšãçµæã¯ä»£ããã« buffer ã«èªã¿èŸŒãŸããèªã¿èŸŒãã ãã€ãæ°ãè¿ããŸãããœã±ããã non-blocking ã§èªã¿åºãããããã¯ãããšã
SSLWantReadErrorãããã¯SSLWantWriteErrorãéåºãããŸããåããŽã·ãšãŒã·ã§ã³ããã€ã§ãå¯èœãªã®ã§ã
read()ã®åŒã³åºãã¯æžãèŸŒã¿æäœãåŒãèµ·ããããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ãœã±ããã®ã¿ã€ã ã¢ãŠãã¯ããã€ããéåä¿¡ããããã³ã«ãªã»ãããããªããªããŸããããœã±ããã®ã¿ã€ã ã¢ãŠãã¯ãæå€§ len ãã€ããèªãã®ã«ãããæå€§åèšæéã«ãªããŸããã
ããŒãžã§ã³ 3.6 ã§éæšå¥š:
read()ã®ä»£ããã«recv()ã䜿çšããŠãã ããã
- SSLSocket.write(data)¶
Write data to the SSL socket and return the number of bytes written. The data argument must be an object supporting the buffer interface.
ãœã±ããã non-blocking ã§æžã蟌ã¿ããããã¯ãããšã
SSLWantReadErrorãããã¯SSLWantWriteErrorãéåºãããŸããåããŽã·ãšãŒã·ã§ã³ããã€ã§ãå¯èœãªã®ã§ã
write()ã®åŒã³åºãã¯èªã¿åºãæäœãåŒãèµ·ããããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: The socket timeout is no longer reset each time bytes are received or sent. The socket timeout is now the maximum total duration to write data.
ããŒãžã§ã³ 3.6 ã§éæšå¥š:
write()ã®ä»£ããã«send()ã䜿çšããŠãã ããã
泚é
read(), write() ã¡ãœããã¯äžäœã¬ãã«ã®ã¡ãœããã§ãããæå·åãããŠããªãã¢ããªã±ãŒã·ã§ã³ã¬ãã«ã®ããŒã¿ãèªã¿æžãããããã埩å·/æå·åããŠæå·åãããæžã蟌ã¿ã¬ãã«ã®ããŒã¿ã«ããŸãããããã®ã¡ãœããã¯ã¢ã¯ãã£ã㪠SSL æ¥ç¶ã€ãŸãããã³ãã·ã§ã€ã¯ãå®äºããŠããŠã SSLSocket.unwrap() ãåŒã°ããŠããªãããšãå¿
èŠãšããŸãã
éåžžã¯ãããã®ã¡ãœããã®ä»£ããã« recv() ã send() ã®ãããªãœã±ãã API ã¡ãœããã䜿ãã¹ãã§ãã
- SSLSocket.do_handshake(block=False)¶
SSL ã»ããã¢ããã®ãã³ãã·ã§ã€ã¯ãå®è¡ããŸãã
If block is true and the timeout obtained by
gettimeout()is zero, the socket is set in blocking mode until the handshake is performed.ããŒãžã§ã³ 3.4 ã§å€æŽ: The handshake method also performs
match_hostname()when thecheck_hostnameattribute of the socket'scontextis true.ããŒãžã§ã³ 3.5 ã§å€æŽ: ãœã±ããã®ã¿ã€ã ã¢ãŠãã¯ããã€ããéåä¿¡ããããã³ã«ãªã»ãããããªããªããŸããããœã±ããã®ã¿ã€ã ã¢ãŠãã¯ããã³ãã·ã§ã€ã¯ã«ãããæå€§åèšæéã«ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: Hostname or IP address is matched by OpenSSL during handshake. The function
match_hostname()is no longer used. In case OpenSSL refuses a hostname or IP address, the handshake is aborted early and a TLS alert message is sent to the peer.
- SSLSocket.getpeercert(binary_form=False)¶
æ¥ç¶å ã«èšŒææžãç¡ãå Žåã
Noneãè¿ããŸããSSL ãã³ãã·ã§ã€ã¯ããŸã è¡ãããŠããªãå Žåã¯ãValueErrorãéåºãããŸããbinary_formãFalseã§æ¥ç¶å ããèšŒææžãååŸããå Žåããã®ã¡ãœããã¯dictã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããŸããèšŒææžãèªèšŒãããŠããªãå ŽåãèŸæžã¯ç©ºã§ããèšŒææžãèªèšŒãããŠããå Žåããã€ãã®ããŒãæã£ãèŸæžãè¿ããsubject(èšŒææžãçºè¡ããã principal),issuer(èšŒææžãçºè¡ãã principal) ãå«ã¿ãŸããèšŒææžã Subject Alternative Name æ¡åŒµ(RFC 3280 ãåç §)ã®ã€ã³ã¹ã¿ã³ã¹ãæ ŒçŽããŠããå ŽåãsubjectAltNameããŒãèŸæžã«å«ãŸããŸããsubject,issuerãã£ãŒã«ãã¯ãèšŒææžã®ããããã®ãã£ãŒã«ãã«ã€ããŠã®ããŒã¿æ§é ã§äžãããã RDN (relative distinguishued name) ã®ã·ãŒã±ã³ã¹ãæ ŒçŽããã¿ãã«ã§ãå RDN 㯠name-value ãã¢ã®ã·ãŒã±ã³ã¹ã§ããçŸå®äžçã§ã®äŸããèŠãããŸã:{'issuer': ((('countryName', 'IL'),), (('organizationName', 'StartCom Ltd.'),), (('organizationalUnitName', 'Secure Digital Certificate Signing'),), (('commonName', 'StartCom Class 2 Primary Intermediate Server CA'),)), 'notAfter': 'Nov 22 08:15:19 2013 GMT', 'notBefore': 'Nov 21 03:09:52 2011 GMT', 'serialNumber': '95F0', 'subject': ((('description', '571208-SLe257oHY9fVQ07Z'),), (('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'San Francisco'),), (('organizationName', 'Electronic Frontier Foundation, Inc.'),), (('commonName', '*.eff.org'),), (('emailAddress', 'hostmaster@eff.org'),)), 'subjectAltName': (('DNS', '*.eff.org'), ('DNS', 'eff.org')), 'version': 3}
binary_formåŒæ°ãTrueã ã£ãå ŽåãèšŒææžãæž¡ãããŠããã°ãã®ã¡ãœããã¯DERãšã³ã³ãŒããããèšŒææžå šäœããã€ãåãšããŠè¿ããæ¥ç¶å ãèšŒææžãæç€ºããªãã£ãå Žåã¯Noneãè¿ããŸããæ¥ç¶å ãèšŒææžãæäŸãããã©ãã㯠SSL ãœã±ããã®åœ¹å²ã«äŸåããŸã:ã¯ã©ã€ã¢ã³ã SSL ãœã±ããã§ã¯ãèªèšŒãèŠæ±ãããŠãããã©ããã«é¢ãããããµãŒãã¯åžžã«èšŒææžãæäŸããŸãã
ãµãŒã SSL ãœã±ããã§ã¯ãã¯ã©ã€ã¢ã³ãã¯ãµãŒãã«ãã£ãŠèªèšŒãèŠæ±ãããŠããå Žåã«ã®ã¿èšŒææžãæäŸããŸãããããã£ãŠã (
CERT_OPTIONALãCERT_REQUIREDã§ã¯ãªã)CERT_NONEã䜿çšããå Žågetpeercert()ã¯Noneãè¿ããŸãã
See also
SSLContext.check_hostname.ããŒãžã§ã³ 3.2 ã§å€æŽ: è¿ãããèŸæžã«
issuer,notBeforeã®ãããªè¿œå ã¢ã€ãã ãå«ãããã«ãªããŸãããããŒãžã§ã³ 3.4 ã§å€æŽ: ãã³ãã·ã§ã€ã¯ãæžãã§ããªããã°
ValueErrorãæããããã«ãªããŸãããè¿ãããèŸæžã«crlDistributionPoints,caIssuers,OCSPURI ã®ãã㪠X509v3 æ¡åŒµã¢ã€ãã ãå«ãããã«ãªããŸãããããŒãžã§ã³ 3.9 ã§å€æŽ: IPv6 address strings no longer have a trailing new line.
- SSLSocket.get_verified_chain()¶
Returns verified certificate chain provided by the other end of the SSL channel as a list of DER-encoded bytes. If certificate verification was disabled method acts the same as
get_unverified_chain().Added in version 3.13.
- SSLSocket.get_unverified_chain()¶
Returns raw certificate chain provided by the other end of the SSL channel as a list of DER-encoded bytes.
Added in version 3.13.
- SSLSocket.cipher()¶
å©çšãããŠããæå·ã®ååããã®æå·ã®å©çšãå®çŸ©ããŠããSSLãããã³ã«ã®ããŒãžã§ã³ãå©çšãããŠããéµã®bité·ã®3ã€ã®å€ãå«ãã¿ãã«ãè¿ããŸããããæ¥ç¶ã確ç«ãããŠããªãå Žåã
Noneãè¿ããŸãã
ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®äž¡æ¹ã§å©çšã§ããæå·æ¹åŒã®ãªã¹ããè¿ããŸããè¿ããããªã¹ãã®åèŠçŽ ã¯ 3ã€ã®å€ãå«ãã¿ãã«ã§ããã®å€ã¯ãããããæå·æ¹åŒã®ååããã®æå·ã®å©çšãå®çŸ©ããŠãã SSL ãããã³ã«ã®ããŒãžã§ã³ãæå·ã§äœ¿çšãããç§å¯éµã®ãããé·ã§ããæ¥ç¶ã確ç«ãããŠããªããããœã±ãããã¯ã©ã€ã¢ã³ããœã±ããã§ããå Žåã
shared_ciphers()ã¯Noneãè¿ããŸããAdded in version 3.5.
- SSLSocket.compression()¶
䜿ãããŠããå§çž®ã¢ã«ãŽãªãºã ãæååã§è¿ããŸããæ¥ç¶ãå§çž®ãããŠããªããã°
Noneãè¿ããŸããäžäœã¬ãã«ã®ãããã³ã«ãèªèº«ã§å§çž®ã¡ã«ããºã ããµããŒãããå ŽåãSSL ã¬ãã«ã§ã®å§çž®ã
OP_NO_COMPRESSIONã䜿ã£ãŠç¡å¹ã«ã§ããŸããAdded in version 3.3.
- SSLSocket.get_channel_binding(cb_type='tls-unique')¶
çŸåšã®æ¥ç¶ã«ããããã£ãã«ãã€ã³ãã£ã³ã°ã®ããŒã¿ãååŸããŸããæªæ¥ç¶ãããã¯ãã³ãã·ã§ã€ã¯ãå®äºããŠããªããã°
Noneãè¿ããŸããcb_type ãã©ã¡ãŒã¿ã«ãããæã¿ã®ãã£ãã«ãã€ã³ãã£ã³ã°ã®ã¿ã€ããéžæã§ããŸãããã£ãã«ãã€ã³ãã£ã³ã°ã®ã¿ã€ãã®åŠ¥åœãªãã®ã¯
CHANNEL_BINDING_TYPESã§ãªã¹ããããŠããŸããçŸåšã®ãšãã㯠RFC 5929 ã§å®çŸ©ãããŠãã 'tls-unique' ã®ã¿ããµããŒããããŠããŸããæªãµããŒãã®ãã£ãã«ãã€ã³ãã£ã³ã°ã®ã¿ã€ããèŠæ±ãããå ŽåãValueErrorãéåºããŸããAdded in version 3.3.
- SSLSocket.selected_alpn_protocol()¶
TLS ãã³ãã·ã§ã€ã¯ã§éžæããããããã³ã«ãè¿ããŸãã
SSLContext.set_alpn_protocols()ãåŒã°ããŠããªãå ŽåãçžæåŽã ALPN ããµããŒãããŠããªãå Žåãã¯ã©ã€ã¢ã³ããææ¡ãããããã³ã«ã®ã©ãããœã±ããããµããŒãããªãå Žåããããã¯ãã³ãã·ã§ã€ã¯ããŸã è¡ãããŠããªãå Žåã«ã¯ãNoneãè¿ãããŸããAdded in version 3.5.
- SSLSocket.selected_npn_protocol()¶
TLS/SSL ãã³ãã·ã§ã€ã¯ã§éžæãããäžäœã¬ãã«ã®ãããã³ã«ãè¿ããŸãã
SSLContext.set_npn_protocols()ãåŒã°ããŠããªãå ŽåãçžæåŽã NPN ããµããŒãããŠããªãå Žåããããã¯ãã³ãã·ã§ã€ã¯ããŸã è¡ãããŠããªãå Žåã«ã¯ãNoneãè¿ãããŸããAdded in version 3.3.
ããŒãžã§ã³ 3.10 ã§éæšå¥š: NPN has been superseded by ALPN
- SSLSocket.unwrap()¶
SSLã·ã£ããããŠã³ãã³ãã·ã§ã€ã¯ãå®è¡ããŸããããã¯äžäœã¬ã€ã€ãŒã®ãœã±ããããTLSã¬ã€ã€ãŒãåãé€ããäžäœã¬ã€ã€ãŒã®ãœã±ãããªããžã§ã¯ããè¿ããŸããããã¯æå·åããããªãã¬ãŒã·ã§ã³ããæå·åãããŠããªãæ¥ç¶ã«ç§»è¡ãããšãã«å©çšãããŸãã以éã®éä¿¡ã«ã¯ããªãªãžãã«ã®ãœã±ããã§ã¯ãªããã®ã¡ãœãããè¿ãããœã±ããã®ã¿ãå©çšããã¹ãã§ãã
- 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
SSLContext.post_handshake_auth.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
SSLErroris raised.泚é
Only available with OpenSSL 1.1.1 and TLS 1.3 enabled. Without TLS 1.3 support, the method raises
NotImplementedError.Added in version 3.8.
- SSLSocket.version()¶
ã³ãã¯ã·ã§ã³ã«ãã£ãŠå®éã«ããŽã·ãšã€ãããã SSL ãããã³ã«ããŒãžã§ã³ãæååã§ããŸãã¯ãã»ãã¥ã¢ãªã³ãã¯ã·ã§ã³ã確ç«ããŠããªããã°
Noneãè¿ããŸãããããæžããŠããæç¹ã§ã¯ã"SSLv2","SSLv3","TLSv1","TLSv1.1","TLSv1.2"ãªã©ãè¿ããŸããææ°ã® OpenSSL ã¯ãã£ãšè²ã ãªå€ãå®çŸ©ããŠãããããããŸãããAdded in version 3.5.
- SSLSocket.pending()¶
æ¥ç¶ã«ãããŠæ¢ã«åŸ©å·æžã¿ã§èªã¿åºãå¯èœã§ä¿çã«ãªã£ãŠãããã€ãåã®æ°ãè¿ããŸãã
- SSLSocket.context¶
The
SSLContextobject this SSL socket is tied to.Added in version 3.2.
- SSLSocket.server_side¶
ãµãŒããµã€ãã®ãœã±ããã«å¯ŸããŠ
Trueãã¯ã©ã€ã¢ã³ããµã€ãã®ãœã±ããã«å¯ŸããŠFalseãšãªãçåœå€ã§ããAdded in version 3.2.
- SSLSocket.server_hostname¶
ãµãŒãã®ãã¹ãå:
stråããŸãã¯ãµãŒããµã€ãã®ãœã±ããã®å Žåãšã³ã³ã¹ãã©ã¯ã¿ã§ hostname ãæå®ãããªãã£ãå Žåã¯NoneAdded in version 3.2.
ããŒãžã§ã³ 3.7 ã§å€æŽ: The attribute is now always ASCII text. When
server_hostnameis an internationalized domain name (IDN), this attribute now stores the A-label form ("xn--pythn-mua.org"), rather than the U-label form ("pythön.org").
- SSLSocket.session¶
ãã® SSL æ¥ç¶ã«å¯Ÿãã
SSLSessionã§ãããã®ã»ãã·ã§ã³ã¯ãTLS ãã³ãã·ã§ã€ã¯ã®å®è¡åŸãã¯ã©ã€ã¢ã³ããµã€ããšãµãŒããµã€ãã®ãœã±ããã§äœ¿çšã§ããŸããã¯ã©ã€ã¢ã³ããœã±ããã§ã¯ããã®ã»ãã·ã§ã³ãdo_handshake()ãåŒã°ããåã«èšå®ããŠãã»ãã·ã§ã³ãåå©çšã§ããŸããAdded in version 3.6.
- SSLSocket.session_reused¶
Added in version 3.6.
SSL contexts¶
Added in version 3.2.
SSL ã³ã³ããã¹ãã¯ãSSL æ§æãªãã·ã§ã³ãèšŒææž(矀)ãç§å¯éµ(矀)ãªã©ã®ãããªãäžåã® SSL æ¥ç¶ãããé·çãããããŸããŸãªããŒã¿ãä¿æããŸããããã¯ãµãŒããµã€ããœã±ããã® SSL ã»ãã·ã§ã³ã®ãã£ãã·ã¥ã管çããåãã¯ã©ã€ã¢ã³ãããã®ç¹°ãè¿ãã®æ¥ç¶æã®é床åäžã«äžåœ¹è²·ããŸãã
- class ssl.SSLContext(protocol=None)¶
Create a new SSL context. You may pass protocol which must be one of the
PROTOCOL_*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 isPROTOCOL_TLS; it provides the most compatibility with other versions.次ã®ããŒãã«ã¯ãã©ã®ã¯ã©ã€ã¢ã³ãã®ããŒãžã§ã³ãã©ã®ãµãŒãã®ããŒãžã§ã³ã«æ¥ç¶ã§ãããã瀺ããŠããŸã:
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
èæ³š
åè
create_default_context()lets thesslmodule choose security settings for a given purpose.ããŒãžã§ã³ 3.6 ã§å€æŽ: The context is created with secure default values. The options
OP_NO_COMPRESSION,OP_CIPHER_SERVER_PREFERENCE,OP_SINGLE_DH_USE,OP_SINGLE_ECDH_USE,OP_NO_SSLv2, andOP_NO_SSLv3(except forPROTOCOL_SSLv3) are set by default. The initial cipher suite list contains onlyHIGHciphers, noNULLciphers and noMD5ciphers.ããŒãžã§ã³ 3.10 ã§éæšå¥š:
SSLContextwithout protocol argument is deprecated. The context class will either requirePROTOCOL_TLS_CLIENTorPROTOCOL_TLS_SERVERprotocol in the future.ããŒãžã§ã³ 3.10 ã§å€æŽ: The default cipher suites now include only secure AES and ChaCha20 ciphers with forward secrecy and security level 2. RSA and DH keys with less than 2048 bits and ECC keys with less than 224 bits are prohibited.
PROTOCOL_TLS,PROTOCOL_TLS_CLIENT, andPROTOCOL_TLS_SERVERuse TLS 1.2 as minimum TLS version.泚é
SSLContextonly supports limited mutation once it has been used by a connection. Adding new certificates to the internal trust store is allowed, but changing ciphers, verification settings, or mTLS certificates may result in surprising behavior.泚é
SSLContextis designed to be shared and used by multiple connections. Thus, it is thread-safe as long as it is not reconfigured after being used by a connection.
SSLContext ãªããžã§ã¯ãã¯ä»¥äžã®ã¡ãœãããšå±æ§ãæã£ãŠããŸã:
- SSLContext.cert_store_stats()¶
ããŒãããã X.509 èšŒææžã®æ°ãCA èšŒææžã§æŽ»æ§ã® X.509 èšŒææžã®æ°ãèšŒææžå€±å¹ãªã¹ãã®æ°ãã«ã€ããŠã®çµ±èšæ å ±ãèŸæžãšããŠååŸããŸãã
äžã€ã® CA ãšä»ã®äžã€ã®èšŒææžãæã£ãã³ã³ããã¹ãã§ã®äŸã§ã:
>>> context.cert_store_stats() {'crl': 0, 'x509_ca': 1, 'x509': 2}
Added in version 3.4.
- SSLContext.load_cert_chain(certfile, keyfile=None, password=None)¶
ç§å¯éµãšå¯Ÿå¿ããèšŒææžãããŒãããŸãã certfile ã¯ãèšŒææžãšãèšŒææžèªèšŒã§å¿ èŠãšãããä»»æã®æ°ã® CA èšŒææžãå«ããPEM ãã©ãŒãããã®åäžãã¡ã€ã«ãžã®ãã¹ã§ãªããã°ãªããŸããã keyfile æååãæå®ããå Žåãç§å¯éµãå«ãŸãããã¡ã€ã«ãæããã®ã§ãªããã°ãªããŸãããæå®ããªãå Žåãç§å¯éµã certfile ããååŸãããŸãã certfile ãžã®èšŒææžã®æ ŒçŽã«ã€ããŠã®è©³çްã¯ã èšŒææž ã®è°è«ãåç §ããŠãã ããã
password åŒæ°ã«ãç§å¯éµã埩å·ããããã®ãã¹ã¯ãŒããè¿ã颿°ãäžããããšãã§ããŸãããã®é¢æ°ã¯ç§å¯éµãæå·åãããŠããŠããªããã€ãã¹ã¯ãŒããå¿ èŠãªå Žåã«ã®ã¿åŒã³åºãããŸãããã®é¢æ°ã¯åŒæ°ãªãã§åŒã³åºãããstring, bytes, ãŸã㯠bytearray ãè¿ããªããã°ãªããŸãããæ»ãå€ã string ã®å Žåã¯éµã埩å·åããã®ã«äœ¿ãåã« UTF-8 ã§ãšã³ã³ãŒããããŸããstring ã®ä»£ããã« bytes ã bytearray ãè¿ããå Žå㯠password åŒæ°ã«çŽæ¥äŸçµŠãããŸããç§å¯éµãæå·åãããŠããªãã£ãããã¹ã¯ãŒããå¿ èŠãšããªãå Žåã¯ãæå®ã¯ç¡èŠãããŸãã
password ãäžããããããããŠãã¹ã¯ãŒããå¿ èŠãªå Žåã«ã¯ãOpenSSL çµã¿èŸŒã¿ã®ãã¹ã¯ãŒãåãåããã¡ã«ããºã ãããŠãŒã¶ã«å¯Ÿè©±çã«ãã¹ã¯ãŒããåãåãããŸãã
ç§å¯éµãèšŒææžã«åèŽããªããã°ã
SSLErrorãéåºãããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: æ°ãããªãã·ã§ã³åŒæ° passwordã
- SSLContext.load_default_certs(purpose=Purpose.SERVER_AUTH)¶
ããã©ã«ãã®å Žæãã "èªèšŒå±" (CA=certification authority) èšŒææžãã¡ã€ã«äžåŒãããŒãããŸããWindows ã§ã¯ãCA èšŒææžã¯ã·ã¹ãã èšæ¶åã®
CAãšROOTããããŒãããŸããå šãŠã®ã·ã¹ãã ã§ã¯ããã®é¢æ°ã¯SSLContext.set_default_verify_paths()ãåŒã³åºããŸããå°æ¥çã«ã¯ãã®ã¡ãœããã¯ãä»ã®å Žæããã CA èšŒææžãããŒããããããããŸãããThe purpose flag specifies what kind of CA certificates are loaded. The default settings
Purpose.SERVER_AUTHloads certificates, that are flagged and trusted for TLS web server authentication (client side sockets).Purpose.CLIENT_AUTHloads CA certificates for client certificate verification on the server side.Added in version 3.4.
- SSLContext.load_verify_locations(cafile=None, capath=None, cadata=None)¶
verify_modeãCERT_NONEã§ãªãå Žåã«æ¥ç¶å ã®èšŒææžãã¡ã€ã«ã®æ£åœæ§æ€èšŒã«äœ¿ããã "èªèšŒå±" (CA=certification authority) èšŒææžãã¡ã€ã«äžåŒãããŒãããŸããå°ãªããšã cafile ã capath ã®ã©ã¡ããã¯æå®ããªããã°ãªããŸããããã®ã¡ãœãã㯠PEM ãŸã㯠DER ãã©ãŒãããã®èšŒææžå€±å¹ãªã¹ã (CRLs=certification revocation lists)ãããŒãã§ããŸããCRLs ã®ããã«äœ¿ãã«ã¯ã
SSLContext.verify_flagsãé©åã«èšå®ããªããã°ãªããŸãããcafile ãæå®ããå Žåã¯ãPEM ãã©ãŒãããã§ CA èšŒææžãçµåããããã¡ã€ã«ãžã®ãã¹ãæå®ããŠãã ããããã®ãã¡ã€ã«å ã§èšŒææžãã©ã®ããã«ç·šæããã°è¯ãã®ãã«ã€ããŠã®è©³ããæ å ±ã«ã€ããŠã¯ã èšŒææž ã®è°è«ãåç §ããŠãã ããã
The capath string, if present, is the path to a directory containing several CA certificates in PEM format, following an OpenSSL specific layout.
cadata ãªããžã§ã¯ããæå®ããå Žåã¯ãPEM ãšã³ã³ãŒãã®èšŒææžäžã€ä»¥äžã® ASCII æååããDER ãšã³ã³ãŒãã®èšŒææžã® bytes-like object ãªããžã§ã¯ãã®ã©ã¡ãããæå®ããŠãã ãããPEM ãšã³ã³ãŒãã®èšŒææžã®åšå²ã®äœåãªè¡ã¯ç¡èŠãããŸãããå°ãªããšãäžã€ã®èšŒææžãå«ãŸããŠããå¿ èŠããããŸãã
ããŒãžã§ã³ 3.4 ã§å€æŽ: æ°ãããªãã·ã§ã³åŒæ° cadata ã
- SSLContext.get_ca_certs(binary_form=False)¶
ããŒãããã "èªèšŒå±" (CA=certification authority) èšŒææžã®ãªã¹ããååŸããŸãã
binary_formåŒæ°ãFalseã§ããå Žåããªã¹ãã®ããããã®ãšã³ããªã¯SSLSocket.getpeercert()ãåºåãããããªèŸæžã«ãªããŸããTrue ã§ããå Žåããã®ã¡ãœããã¯ãDER ãšã³ã³ãŒã圢åŒã®èšŒææžã®ãªã¹ããè¿ããŸããè¿åŽããããªã¹ãã«ã¯ã SSL æ¥ç¶ã«ãã£ãŠèšŒææžããªã¯ãšã¹ãããã³ããŒããããªãéãã capath ããã®èšŒææžã¯å«ãŸããŸãããæ³šé
capath ãã£ã¬ã¯ããªå ã®èšŒææžã¯äžåºŠã§ã䜿ãããªãéãã¯ããŒããããŸããã
Added in version 3.4.
- SSLContext.get_ciphers()¶
æå¹ãªæå·åã®ãªã¹ããååŸããŸãããªã¹ãã¯æå·ååªå 床é ã«äžŠã³ãŸãã
SSLContext.set_ciphers()ãåç §ããŠãã ããã以äžã¯ããã°ã©ã äŸã§ã:
>>> ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) >>> ctx.set_ciphers('ECDHE+AESGCM:!ECDSA') >>> ctx.get_ciphers() [{'aead': True, 'alg_bits': 256, 'auth': 'auth-rsa', 'description': 'ECDHE-RSA-AES256-GCM-SHA384 TLSv1.2 Kx=ECDH Au=RSA ' 'Enc=AESGCM(256) Mac=AEAD', 'digest': None, 'id': 50380848, 'kea': 'kx-ecdhe', 'name': 'ECDHE-RSA-AES256-GCM-SHA384', 'protocol': 'TLSv1.2', 'strength_bits': 256, 'symmetric': 'aes-256-gcm'}, {'aead': True, 'alg_bits': 128, 'auth': 'auth-rsa', 'description': 'ECDHE-RSA-AES128-GCM-SHA256 TLSv1.2 Kx=ECDH Au=RSA ' 'Enc=AESGCM(128) Mac=AEAD', 'digest': None, 'id': 50380847, 'kea': 'kx-ecdhe', 'name': 'ECDHE-RSA-AES128-GCM-SHA256', 'protocol': 'TLSv1.2', 'strength_bits': 128, 'symmetric': 'aes-128-gcm'}]
Added in version 3.6.
- SSLContext.set_default_verify_paths()¶
ããã©ã«ãã® "èªèšŒå±" (CA=certification authority) èšŒææžããOpenSSL ã©ã€ãã©ãªããã«ããããéã«å®çŸ©ããããã¡ã€ã«ã·ã¹ãã äžã®ãã¹ããããŒãããŸããæ®å¿µãªãããã®ã¡ãœãããæåãããã©ãããç¥ãããã®ç°¡åãªæ¹æ³ã¯ãããŸãã: èšŒææžãèŠã€ãããªããŠããšã©ãŒã¯è¿ããŸãããOpenSSL ã©ã€ãã©ãªããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®äžéšãšããŠæäŸãããŠããéã«ã¯ã©ãããé©åã«æ§æã§ããããã§ããã
- 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
SSLErrorwill be raised.泚é
when connected, the
SSLSocket.cipher()method of SSL sockets will give the currently selected cipher.TLS 1.3 cipher suites cannot be disabled with
set_ciphers().
- SSLContext.set_alpn_protocols(alpn_protocols)¶
SSL/TLS ãã³ãã·ã§ã€ã¯æã«ãœã±ãããæç€ºãã¹ããããã³ã«ãæå®ããŸãã
['http/1.1', 'spdy/2']ã®ãããªæšå¥šé ã«äžŠã¹ã ASCII æååã®ãªã¹ãã§ãªããã°ãªããŸããããããã³ã«ã®éžæã¯ RFC 7301 ã«åŸããã³ãã·ã§ã€ã¯äžã«è¡ãããŸãããã³ãã·ã§ã€ã¯ãæ£åžžã«çµäºããåŸãSSLSocket.selected_alpn_protocol()ã¡ãœããã¯åæããããããã³ã«ãè¿ããŸãããã®ã¡ãœããã¯
HAS_ALPNãFalseã®å ŽåNotImplementedErrorãéåºããŸããAdded in version 3.5.
- SSLContext.set_npn_protocols(npn_protocols)¶
SSL/TLS ãã³ãã·ã§ã€ã¯æã«ãœã±ãããæç€ºãã¹ããããã³ã«ãæå®ããŸãã
['http/1.1', 'spdy/2']ã®ãããªæšå¥šé ã«äžŠã¹ãæååã®ãªã¹ãã§ãªããã°ãªããŸããããããã³ã«ã®éžæã¯ Application Layer Protocol Negotiation ã«åŸããã³ãã·ã§ã€ã¯äžã«è¡ãããŸãããã³ãã·ã§ã€ã¯ãæ£åžžã«çµäºããåŸãSSLSocket.selected_alpn_protocol()ã¡ãœããã¯åæããããããã³ã«ãè¿ããŸãããã®ã¡ãœããã¯
HAS_NPNãFalseã®å ŽåNotImplementedErrorãéåºããŸããAdded in version 3.3.
ããŒãžã§ã³ 3.10 ã§éæšå¥š: NPN has been superseded by ALPN
- SSLContext.sni_callback¶
TLS ã¯ã©ã€ã¢ã³ãããµãŒãå衚瀺ãæå®ããéã®ãSSL/TLS ãµãŒãã«ãã£ãŠ TLS Client Hello ãã³ãã·ã§ã€ã¯ã¡ãã»ãŒãžãåãåãããããšã§åŒã³åºãããã³ãŒã«ããã¯é¢æ°ãç»é²ããŸãããµãŒãå衚瀺ã¡ã«ããºã 㯠RFC 6066 ã»ã¯ã·ã§ã³ 3 - Server Name Indication ã§è¿°ã¹ãããŠããŸãã
SSLContextããšã«äžã€ã ãã³ãŒã«ããã¯ãã»ããã§ããŸãã sni_callback ãNoneã«ããã°ã³ãŒã«ããã¯ã¯ç¡å¹ã«ãªããŸãããã®é¢æ°ãç¶ããŠåŒã¶ãšã以åã«ç»é²ãããã³ãŒã«ããã¯ãäžæžãããŸããThe callback function will be called with three arguments; the first being the
ssl.SSLSocket, the second is a string that represents the server name that the client is intending to communicate (orNoneif the TLS Client Hello does not contain a server name) and the third argument is the originalSSLContext. The server name argument is text. For internationalized domain name, the server name is an IDN A-label ("xn--pythn-mua.org").ãã®ã³ãŒã«ããã¯ã®å žåçãªå©ç𿹿³ã¯ã
ssl.SSLSocketã®SSLSocket.context屿§ãããµãŒãåã«åèŽããèšŒææžãã§ã€ã³ãæã€æ°ããSSLContextãªããžã§ã¯ãã«å€æŽããããšã§ããIf the callback assigns a new context to
SSLSocket.context, any further ClientHello message on the same connection (for example after a TLS 1.3 HelloRetryRequest) is dispatched to the new context's sni_callback, if it has one; the original callback is not called again for that connection.Due to the early negotiation phase of the TLS connection, only limited methods and attributes are usable like
SSLSocket.selected_alpn_protocol()andSSLSocket.context. TheSSLSocket.getpeercert(),SSLSocket.get_verified_chain(),SSLSocket.get_unverified_chain()SSLSocket.cipher()andSSLSocket.compression()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.TLS ããŽã·ãšãŒã·ã§ã³ãç¶ç¶ããããªãã°ã sni_callback 颿°ã¯
Noneãè¿ããªããã°ãªããŸãããTLS ã倱æããããšãå¿ èŠãšãããªãã constantALERT_DESCRIPTION_*ãè¿ããŠãã ãããããã«ãªãå€ãè¿ããšãèŽåœãšã©ãŒALERT_DESCRIPTION_INTERNAL_ERRORãåŒãèµ·ãããŸããsni_callback 颿°ãäŸå€ãéåºããå ŽåãTLS æ¥ç¶ã¯ TLS ã®èŽåœçã¢ã©ãŒãã¡ãã»ãŒãž
ALERT_DESCRIPTION_HANDSHAKE_FAILUREãšãšãã«çµäºããŸãããã®ã¡ãœãã㯠OpenSSL ã©ã€ãã©ãªã OPENSSL_NO_TLSEXT ãå®çŸ©ããŠãã«ããããŠããå Žåã
NotImplementedErrorãéåºããŸããAdded in version 3.7.
ããŒãžã§ã³ 3.14.8 ã§å€æŽ: After the callback assigns a new
SSLSocket.context, later ClientHello messages on the connection are dispatched to the new context's sni_callback.
- SSLContext.set_servername_callback(server_name_callback)¶
This is a legacy API retained for backwards compatibility. When possible, you should use
sni_callbackinstead. The given server_name_callback is similar to sni_callback, except that when the server hostname is an IDN-encoded internationalized domain name, the server_name_callback receives a decoded U-label ("pythön.org").If there is a decoding error on the server name, the TLS connection will terminate with an
ALERT_DESCRIPTION_INTERNAL_ERRORfatal TLS alert message to the client.Added in version 3.4.
- SSLContext.load_dh_params(dhfile, /)¶
ãã£ãã£ãŒã»ãã«ãã³(DH)éµäº€æã®ããã®éµçæãã©ã¡ãŒã¿ãããŒãããŸããDH éµäº€æãçšããããšã¯ã(ãµãŒããã¯ã©ã€ã¢ã³ããšãã«)èšç®æ©ãªãœãŒã¹ã«é«ãåŠçè² è·ããããŸããã»ãã¥ãªãã£ãåäžãããŸãã dhfile ãã©ã¡ãŒã¿ã¯ PEM ãã©ãŒãããã® DH ãã©ã¡ãŒã¿ãå«ãã ãã¡ã€ã«ãžã®ãã¹ã§ãªããã°ãªããŸããã
ãã®èšå®ã¯ã¯ã©ã€ã¢ã³ããœã±ããã«ã¯é©çšãããŸãããããã«ã»ãã¥ãªãã£ãæ¹åããã®ã«
OP_SINGLE_DH_USEãªãã·ã§ã³ãå©çšã§ããŸããAdded in version 3.3.
- SSLContext.set_ecdh_curve(curve_name, /)¶
æ¥åæ²ç·ãã£ãã£ãŒã»ãã«ãã³(ECDH)éµäº€æã®æ²ç·åãæå®ããŸããECDH ã¯ããšã® DH ã«èŒã¹ãŠãã»ãŒééããªãåçšåºŠã«å®å šã§ããäžæ¹ã§ãé¡èã«é«éã§ãã curve_name ãã©ã¡ãŒã¿ã¯æ¢ç¥ã®æ¥åæ²ç·ã衚ãæååã§ãªããã°ãªããŸãããäŸãã°
prime256v1ãåºããµããŒããããŠããæ²ç·ã§ãããã®èšå®ã¯ã¯ã©ã€ã¢ã³ããœã±ããã«ã¯é©çšãããŸãããããã«ã»ãã¥ãªãã£ãæ¹åããã®ã«
OP_SINGLE_ECDH_USEãªãã·ã§ã³ãå©çšã§ããŸãããã®ã¡ãœããã¯
HAS_ECDHãFalseã®å Žåã¯å©çšã§ããŸãããAdded in version 3.3.
åè
- SSL/TLS & Perfect Forward Secrecy
Vincent 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
SSLContext.sslsocket_class(defaultSSLSocket). The returned SSL socket is tied to the context, its settings and certificates. sock must be aSOCK_STREAMsocket; other socket types are unsupported.server_sideåŒæ°ã¯çåœå€ã§ããã®ãœã±ããããµãŒããµã€ããšã¯ã©ã€ã¢ã³ããµã€ãã®ã©ã¡ãã®åäœãããã®ããæå®ããŸããã¯ã©ã€ã¢ã³ããµã€ããœã±ããã«ãããŠãã³ã³ããã¹ãã®çæã¯é å»¶ãããŸããã€ãŸããäœã¬ã€ã€ã®ãœã±ããããŸã æ¥ç¶ãããŠããªãå Žåãã³ã³ããã¹ãã®çæã¯ãã®ãœã±ããã®
connect()ã¡ãœãããåŒã°ããåŸã«è¡ãããŸãããµãŒããµã€ããœã±ããã®å Žåããã®ãœã±ããã«æ¥ç¶å ãå± ãªããã°ãã㯠listen çšãœã±ããã ãšå€æãããŸããaccept()ã¡ãœããã§çæãããã¯ã©ã€ã¢ã³ãæ¥ç¶ã«å¯ŸããŠã®ãµãŒããµã€ã SSLã©ããã¯èªåçã«è¡ãããŸããã¡ãœããã¯SSLErrorãéåºããããšããããŸããã¯ã©ã€ã¢ã³ãããã®æ¥ç¶ã§ã¯ã server_hostname ã§æ¥ç¶å ãµãŒãã¹ã®ãã¹ãåãæå®ã§ããŸãããã㯠HTTP ããŒãã£ã«ãã¹ãã«ããªã䌌ãŠãã·ã³ã°ã«ãµãŒãã§è€æ°ã® SSL ããŒã¹ã®ãµãŒãã¹ãå¥ã ã®èšŒææžã§ãã¹ãããŠãããããªãµãŒãã«å¯ŸããŠäœ¿ããŸãã server_side ã True ã®å Žåã« server_hostname ãæå®ãããš
ValueErrorãéåºããŸããdo_handshake_on_connectåŒæ°ã¯ãsocket.connect()ã®åŸã«èªåçã« SSLãã³ãã·ã§ã€ã¯ãè¡ããããããšãã¢ããªã±ãŒã·ã§ã³ãæç€ºçã«SSLSocket.do_handshake()ã¡ãœãããå®è¡ããããæå®ããŸããSSLSocket.do_handshake()ãæç€ºçã«åŒã³ã ãããšã§ããã³ãã·ã§ã€ã¯ã«ãããœã±ããI/Oã®ããããã³ã°åäœãå¶åŸ¡ã§ããŸããsuppress_ragged_eofsåŒæ°ã¯ãSSLSocket.recv()ã¡ãœããããæ¥ç¶å ããäºæããªãEOF ãåãåã£ãæã«éç¥ããæ¹æ³ãæå®ããŸããTrue(ããã©ã«ã) ã®å Žåãäžäœã®ãœã±ããã¬ã€ã€ãŒããäºæãã¬EOFãšã©ãŒãæ¥ãå Žåãéåžžã®EOF (空ã®ãã€ãåãªããžã§ã¯ã)ãè¿ããŸããFalseã®å ŽåãåŒã³åºãå ã«äŸå€ãæããŠéç¥ããŸããsession,
sessionãåç §ããŠãã ãããTo wrap an
SSLSocketin anotherSSLSocket, useSSLContext.wrap_bio().ããŒãžã§ã³ 3.5 ã§å€æŽ: OpenSSL ã SNI ããµããŒãããªããŠã server_hostname ã蚱容ããããã«ãªããŸããã
ããŒãžã§ã³ 3.6 ã§å€æŽ: session åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: The method returns an instance of
SSLContext.sslsocket_classinstead of hard-codedSSLSocket.
- SSLContext.sslsocket_class¶
The return type of
SSLContext.wrap_socket(), defaults toSSLSocket. The attribute can be assigned to on instances ofSSLContextin order to return a custom subclass ofSSLSocket.Added in version 3.7.
- SSLContext.wrap_bio(incoming, outgoing, server_side=False, server_hostname=None, session=None)¶
Wrap the BIO objects incoming and outgoing and return an instance of
SSLContext.sslobject_class(defaultSSLObject). The SSL routines will read input data from the incoming BIO and write data to the outgoing BIO.The server_side, server_hostname and session parameters have the same meaning as in
SSLContext.wrap_socket(), and are validated in the same way: in particular aValueErroris raised whencheck_hostnameis enabled but no server_hostname is given, since there would be no name to match the peer's certificate against.ããŒãžã§ã³ 3.6 ã§å€æŽ: session åŒæ°ã远å ãããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: The method returns an instance of
SSLContext.sslobject_classinstead of hard-codedSSLObject.ããŒãžã§ã³ 3.14.8 ã§å€æŽ: The server_side, server_hostname and session parameters are now validated as
SSLContext.wrap_socket()validates them. Previously a context withcheck_hostnameenabled and no server_hostname was accepted, and verified the certificate chain but never the peer's identity.
- SSLContext.sslobject_class¶
The return type of
SSLContext.wrap_bio(), defaults toSSLObject. The attribute can be overridden on instance of class in order to return a custom subclass ofSSLObject.Added in version 3.7.
- SSLContext.session_stats()¶
Get statistics about the SSL sessions created or managed by this context. 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:
>>> stats = context.session_stats() >>> stats['hits'], stats['misses'] (0, 0)
- SSLContext.check_hostname¶
Whether to match the peer cert's hostname in
SSLSocket.do_handshake(). The context'sverify_modemust be set toCERT_OPTIONALorCERT_REQUIRED, and you must pass server_hostname towrap_socket()in order to match the hostname. Enabling hostname checking automatically setsverify_modefromCERT_NONEtoCERT_REQUIRED. It cannot be set back toCERT_NONEas long as hostname checking is enabled. ThePROTOCOL_TLS_CLIENTprotocol enables hostname checking by default. With other protocols, hostname checking must be enabled explicitly.以äžã¯ããã°ã©ã äŸã§ã:
import socket, ssl context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) context.verify_mode = ssl.CERT_REQUIRED context.check_hostname = True context.load_default_certs() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssl_sock = context.wrap_socket(s, server_hostname='www.verisign.com') ssl_sock.connect(('www.verisign.com', 443))
Added in version 3.4.
ããŒãžã§ã³ 3.7 ã§å€æŽ:
verify_modeis now automatically changed toCERT_REQUIREDwhen hostname checking is enabled andverify_modeisCERT_NONE. Previously the same operation would have failed with aValueError.
- 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. The log file is opened in append-only mode. Writes are synchronized between threads, but not between processes.
Added in version 3.8.
- SSLContext.maximum_version¶
A
TLSVersionenum member representing the highest supported TLS version. The value defaults toTLSVersion.MAXIMUM_SUPPORTED. The attribute is read-only for protocols other thanPROTOCOL_TLS,PROTOCOL_TLS_CLIENT, andPROTOCOL_TLS_SERVER.The attributes
maximum_version,minimum_versionandSSLContext.optionsall affect the supported SSL and TLS versions of the context. The implementation does not prevent invalid combinations. For example a context withOP_NO_TLSv1_2inoptionsandmaximum_versionset toTLSVersion.TLSv1_2will not be able to establish a TLS 1.2 connection.Added in version 3.7.
- SSLContext.minimum_version¶
Like
SSLContext.maximum_versionexcept it is the lowest supported version orTLSVersion.MINIMUM_SUPPORTED.Added in version 3.7.
- SSLContext.num_tickets¶
Control the number of TLS 1.3 session tickets of a
PROTOCOL_TLS_SERVERcontext. The setting has no impact on TLS 1.0 to 1.2 connections.Added in version 3.8.
- SSLContext.options¶
ãã®ã³ã³ããã¹ãã§æå¹ã«ãªã£ãŠãã SSL ãªãã·ã§ã³ãè¡šãæŽæ°ãããã©ã«ãã®å€ã¯
OP_ALLã§ãããOP_NO_SSLv2ã®ãããªä»ã®å€ãããã OR æŒç®ã§æå®ã§ããŸããããŒãžã§ã³ 3.6 ã§å€æŽ:
SSLContext.optionsã¯æ¬¡ã®ããã«Optionsã®ãã©ã°ãè¿ããŸãã>>> ssl.create_default_context().options <Options.OP_ALL|OP_NO_SSLv3|OP_NO_SSLv2|OP_NO_COMPRESSION: 2197947391>
ããŒãžã§ã³ 3.7 ã§éæšå¥š: All
OP_NO_SSL*andOP_NO_TLS*options have been deprecated since Python 3.7. UseSSLContext.minimum_versionandSSLContext.maximum_versioninstead.
- SSLContext.post_handshake_auth¶
Enable TLS 1.3 post-handshake client authentication. 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.
When enabled on client-side sockets, the client signals the server that it supports post-handshake authentication.
When enabled on server-side sockets,
SSLContext.verify_modemust be set toCERT_OPTIONALorCERT_REQUIRED, too. The actual client cert exchange is delayed untilSSLSocket.verify_client_post_handshake()is called and some I/O is performed.Added in version 3.8.
- SSLContext.protocol¶
ã³ã³ããã¹ãã®æ§ç¯æã«éžæããããããã³ã«ããŒãžã§ã³ããã®å±æ§ã¯èªã¿åºãå°çšã§ãã
- SSLContext.hostname_checks_common_name¶
Whether
check_hostnamefalls back to verify the cert's subject common name in the absence of a subject alternative name extension (default: true).Added in version 3.7.
ããŒãžã§ã³ 3.10 ã§å€æŽ: The flag had no effect with OpenSSL before version 1.1.1l. Python 3.8.9, 3.9.3, and 3.10 include workarounds for previous versions.
- SSLContext.security_level¶
An integer representing the security level for the context. This attribute is read-only.
Added in version 3.10.
- SSLContext.verify_flags¶
èšŒææžã®æ€èšŒæäœã®ããã®ãã©ã°ã§ãã
VERIFY_CRL_CHECK_LEAFãªã©ã®ãã©ã°ãããã OR æŒç®ã§ã»ããã§ããŸããããã©ã«ãã§ã¯ OpenSSL ã¯èšŒææžå€±å¹ãªã¹ã (CRLs) ãå¿ èŠãšããŸãããæ€èšŒã«ã䜿ããŸãããAdded in version 3.4.
ããŒãžã§ã³ 3.6 ã§å€æŽ:
SSLContext.verify_flagsã¯æ¬¡ã®ããã«VerifyFlagsã®ãã©ã°ãè¿ããŸãã>>> ssl.create_default_context().verify_flags <VerifyFlags.VERIFY_X509_TRUSTED_FIRST: 32768>
- SSLContext.verify_mode¶
æ¥ç¶å ã®èšŒææžã®æ€èšŒã詊ã¿ããã©ããããŸããæ€èšŒã倱æããå Žåã«ã©ã®ããã«æ¯èãã¹ãããå¶åŸ¡ããŸãããã®å±æ§ã¯
CERT_NONE,CERT_OPTIONAL,CERT_REQUIREDã®ãã¡ã©ããäžã€ã§ãªããã°ãªããŸãããããŒãžã§ã³ 3.6 ã§å€æŽ:
SSLContext.verify_modeã¯æ¬¡ã®ããã«VerifyModeenum (åæ) ãè¿ããŸãã>>> ssl.create_default_context().verify_mode <VerifyMode.CERT_REQUIRED: 2>
- SSLContext.set_psk_client_callback(callback)¶
Enables TLS-PSK (pre-shared key) authentication on a client-side connection.
In general, certificate based authentication should be preferred over this method.
The parameter
callbackis a callable object with the signature:def callback(hint: str | None) -> tuple[str | None, bytes]. Thehintparameter is an optional identity hint sent by the server. The return value is a tuple in the form (client-identity, psk). Client-identity is an optional string which may be used by the server to select a corresponding PSK for the client. The string must be less than or equal to256octets when UTF-8 encoded. PSK is a bytes-like object representing the pre-shared key. Return a zero length PSK to reject the connection.Setting
callbacktoNoneremoves any existing callback.泚é
When using TLS 1.3:
the
hintparameter is alwaysNone.client-identity must be a non-empty string.
䜿çšäŸ:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) context.check_hostname = False context.verify_mode = ssl.CERT_NONE context.maximum_version = ssl.TLSVersion.TLSv1_2 context.set_ciphers('PSK') # A simple lambda: psk = bytes.fromhex('c0ffee') context.set_psk_client_callback(lambda hint: (None, psk)) # A table using the hint from the server: psk_table = { 'ServerId_1': bytes.fromhex('c0ffee'), 'ServerId_2': bytes.fromhex('facade') } def callback(hint): return 'ClientId_1', psk_table.get(hint, b'') context.set_psk_client_callback(callback)
This method will raise
NotImplementedErrorifHAS_PSKisFalse.Added in version 3.13.
- SSLContext.set_psk_server_callback(callback, identity_hint=None)¶
Enables TLS-PSK (pre-shared key) authentication on a server-side connection.
In general, certificate based authentication should be preferred over this method.
The parameter
callbackis a callable object with the signature:def callback(identity: str | None) -> bytes. Theidentityparameter is an optional identity sent by the client which can be used to select a corresponding PSK. The return value is a bytes-like object representing the pre-shared key. Return a zero length PSK to reject the connection.Setting
callbacktoNoneremoves any existing callback.The parameter
identity_hintis an optional identity hint string sent to the client. The string must be less than or equal to256octets when UTF-8 encoded.泚é
When using TLS 1.3 the
identity_hintparameter is not sent to the client.䜿çšäŸ:
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) context.maximum_version = ssl.TLSVersion.TLSv1_2 context.set_ciphers('PSK') # A simple lambda: psk = bytes.fromhex('c0ffee') context.set_psk_server_callback(lambda identity: psk) # A table using the identity of the client: psk_table = { 'ClientId_1': bytes.fromhex('c0ffee'), 'ClientId_2': bytes.fromhex('facade') } def callback(identity): return psk_table.get(identity, b'') context.set_psk_server_callback(callback, 'ServerId_1')
This method will raise
NotImplementedErrorifHAS_PSKisFalse.Added in version 3.13.
èšŒææžÂ¶
èšŒææžã倧ãŸãã«èª¬æãããšãå ¬ééµ/ç§å¯éµã·ã¹ãã ã®äžçš®ã§ãããã®ã·ã¹ãã ã§ã¯ãå principal (ããã¯ãã·ã³ã人ãçµç¹ãªã©ã§ã) ã¯ããŠããŒã¯ãª2ã€ã®æå·éµãå²ãåœãŠãããŸãã1ã€ã¯å ¬éããã å ¬ééµ(public key) ãšåŒã°ããŸããããäžæ¹ã¯ç§å¯ã«ããã ç§å¯éµ(private key) ãšåŒã°ããŸãã 2ã€ã®éµã¯é¢é£ããŠãããçæ¹ã®éµã§æå·åããã¡ãã»ãŒãžã¯ãããçæ¹ã®éµ ã®ã¿ ã§åŸ©å·ã§ããŸãã
èšŒææžã¯2ã€ã® principal ã®æ å ±ãå«ãã§ããŸããèšŒææžã¯ subject åãšãã®å ¬ééµãå«ãã§ããŸãããŸããããäžã€ã® principal ã§ãã çºè¡è (issuer) ããã®ã subject ãæ¬äººã§ããããšãšããã®å ¬ééµãæ£ããããšã®å®£èš(statement)ãå«ãã§ããŸããçºè¡è ããã®å®£èšã¯ããã®çºè¡è ã®ç§å¯éµã§çœ²åãããŠããŸããçºè¡è ã®ç§å¯éµã¯çºè¡è ããç¥ããŸãããã誰ãããã®çºè¡è ã®å ¬ééµãå©çšããŠå®£èšã埩å·ããèšŒææžå ã®å¥ã®æ å ±ãšæ¯èŒããããšã§èªèšŒããããšãã§ããŸããèšŒææžã¯ãŸãããã®èšŒææžãæå¹ã§ããæéã«é¢ããæ å ±ãå«ãã§ããŸãããã®æé㯠"notBefore" ãš "notAfter" ãšåŒã°ãã2ã€ã®ãã£ãŒã«ãã§è¡šçŸãããŠããŸãã
Python ã«ãããŠèšŒææžãå©çšããå Žåãã¯ã©ã€ã¢ã³ãããµãŒããŒãèªåã蚌æããããã«èšŒææžãå©çšããããšãã§ããŸãããããã¯ãŒã¯æ¥ç¶ã®çžæåŽã«èšŒææžã®æç€ºãèŠæ±ããäºãã§ãããã®ã¯ã©ã€ã¢ã³ãããµãŒããŒãèªèšŒãå¿ èŠãšãããªããã®èšŒææžãèªèšŒããããšãã§ããŸããèªèšŒã倱æããå Žåãæ¥ç¶ã¯äŸå€ãçºçãããŸããèªèšŒã¯äžäœå±€ã®OpenSSLãã¬ãŒã ã¯ãŒã¯ãèªåçã«è¡ããŸããã¢ããªã±ãŒã·ã§ã³ã¯èªèšŒæ©æ§ã«ã€ããŠæèããå¿ èŠã¯ãããŸãããããããã¢ããªã±ãŒã·ã§ã³ã¯èªèšŒããã»ã¹ã®ããã«å¹Ÿã€ãã®èšŒææžãæäŸããå¿ èŠããããããããŸããã
Python ã¯èšŒææžãæ ŒçŽãããã¡ã€ã«ãå©çšããŸãããã®ãã¡ã€ã«ã¯ "PEM" (RFC 1422 åç §) ãã©ãŒããããšãããããããŒè¡ãšããã¿ãŒè¡ã®éã«base-64ãšã³ã³ãŒãããã圢ããšã£ãŠããå¿ èŠããããŸãã
-----BEGIN CERTIFICATE-----
... (certificate in base64 PEM encoding) ...
-----END CERTIFICATE-----
èšŒææžãã§ã€ã³Â¶
Pythonãå©çšããèšŒææžãæ ŒçŽãããã¡ã€ã«ã¯ããšãã«ã¯ èšŒææžãã§ã€ã³(certificate chain) ãšåŒã°ããèšŒææžã®ã·ãŒã±ã³ã¹ãæ ŒçŽããŸãããã®ãã§ã€ã³ã®å é ã«ã¯ããŸãã¯ã©ã€ã¢ã³ãããµãŒããŒã§ãã principal ã®èšŒææžã眮ãããã以éã«ã¯ããã®èšŒææžã®çºè¡è (issuer)ã®èšŒææžãªã©ãç¶ããæåŸã«èšŒæå¯Ÿè±¡(subject)ãšçºè¡è ãåã èªå·±çœ²å(self-signed) èšŒææžã§çµãããŸãããã®æåŸã®èšŒææžã¯ ã«ãŒãèšŒææž(root certificate ãšåŒã°ããŸãããããã®èšŒææžãã§ã€ã³ã¯åçŽã«1ã€ã®èšŒææžãã¡ã€ã«ã«çµåããŠãã ãããäŸãã°ã3ã€ã®èšŒææžãããªãèšŒææžãã§ã€ã³ãããå Žåãç§ãã¡ã®ãµãŒããŒã®èšŒææžãããç§ãã¡ã®ãµãŒããŒã«çœ²åããèªèšŒå±ã®èšŒææžããããŠèªèšŒå±ã®èšŒææžãçºè¡ããæ©é¢ã®ã«ãŒãèšŒææžãšç¶ããŸã:
-----BEGIN CERTIFICATE-----
... (certificate for your server)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the certificate for the CA)...
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
... (the root certificate for the CA's issuer)...
-----END CERTIFICATE-----
CA èšŒææžÂ¶
ããçžæããéãããŠããèšŒææžã®èªèšŒããããå Žåãä¿¡é ŒããŠããåçºè¡è
ã®èšŒææžãã§ã€ã³ãå
¥ã£ã "CA certs" ãã¡ã€ã«ãæäŸããå¿
èŠããããŸããç¹°ãè¿ããŸããããã®ãã¡ã€ã«ã¯åçŽã«ãåãã§ã€ã³ãçµåããã ãã®ãã®ã§ããèªèšŒã®ããã«ãPythonã¯ãã®ãã¡ã€ã«ã®äžã®æåã«ããããããã§ã€ã³ãå©çšããŸããSSLContext.load_default_certs() ãåŒã³åºãããšã§ãã©ãããã©ãŒã ã®èšŒææžãã¡ã€ã«ã䜿ãããŸããããã㯠create_default_context() ã«ãã£ãŠèªåçã«è¡ãããŸãã
ç§å¯éµãšèšŒææžã®çµã¿åãã¶
Often the private key is stored in the same file as the certificate; in this
case, only the certfile parameter to SSLContext.load_cert_chain()
needs to be passed. If the private key is stored
with the certificate, it should come before the first certificate in
the certificate chain:
-----BEGIN RSA PRIVATE KEY-----
... (private key in base64 encoding) ...
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
... (certificate in base64 PEM encoding) ...
-----END CERTIFICATE-----
èªå·±çœ²åèšŒææžÂ¶
SSLæå·åæ¥ç¶ãµãŒãã¹ãæäŸãããµãŒããŒã建ãŠãå Žåãé©åãªèšŒææžãååŸããã«ã¯ãèªèšŒå±ããè²·ããªã©ã®å¹Ÿã€ãã®æ¹æ³ããããŸãããŸããèªå·±çœ²åèšŒææžãäœãã±ãŒã¹ããããŸãã OpenSSLã䜿ã£ãŠèªå·±çœ²åèšŒææžãäœãã«ã¯ã次ã®ããã«ããŸãã
% openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout cert.pem
Generating a 1024 bit RSA private key
.......++++++
.............................++++++
writing new private key to 'cert.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:MyState
Locality Name (eg, city) []:Some City
Organization Name (eg, company) [Internet Widgits Pty Ltd]:My Organization, Inc.
Organizational Unit Name (eg, section) []:My Group
Common Name (eg, YOUR name) []:myserver.mygroup.myorganization.com
Email Address []:ops@myserver.mygroup.myorganization.com
%
èªå·±çœ²åèšŒææžã®æ¬ ç¹ã¯ãããèªèº«ãã«ãŒãèšŒææžã§ãããä»ã®äººã¯ãã®èšŒææžãæã£ãŠããªã (ãããŠä¿¡é Œããªã)ããšã§ãã
䜿çšäŸÂ¶
SSLãµããŒãããã¹ããã¶
ã€ã³ã¹ããŒã«ãããŠããPythonãSSLããµããŒãããŠãããã©ããããã¹ãããããã«ããŠãŒã¶ãŒã³ãŒãã¯æ¬¡ã®ã€ãã£ãªã ãå©çšããããšãã§ããŸãã
try:
import ssl
except ImportError:
pass
else:
... # do something that requires SSL support
ã¯ã©ã€ã¢ã³ããµã€ãã®åŠç¶
ãã®äŸã§ã¯ãèªåçã«èšŒææžã®æ€èšŒãè¡ãããšãå«ãæãŸããã»ãã¥ãªãã£èšå®ã§ã¯ã©ã€ã¢ã³ããœã±ããã® SSL ã³ã³ããã¹ããäœããŸã:
>>> context = ssl.create_default_context()
èªåèªèº«ã§ã»ãã¥ãªãã£èšå®ã調æŽãããå Žåãã³ã³ããã¹ããäžããäœãããšã¯ã§ããŸã (ãã ããæ£ãããªãèšå®ãããŠããŸããã¡ãªããšã«æ³šæããŠãã ãã):
>>> context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
>>> context.load_verify_locations("/etc/ssl/certs/ca-bundle.crt")
(ãã®ã¹ããããã¯ãã¹ãŠã® CA èšŒææžã /etc/ssl/certs/ca-bundle.crt ã«ãã³ãã«ãããŠããããšãä»®å®ããŠããŸã; ããéã£ãŠããã°ãšã©ãŒã«ãªããŸãã®ã§ãé©å®ä¿®æ£ããŠãã ãã)
The PROTOCOL_TLS_CLIENT protocol configures the context for cert
validation and hostname verification. verify_mode is
set to CERT_REQUIRED and check_hostname is set
to True. All other protocols create SSL contexts with insecure defaults.
When you use the context to connect to a server, CERT_REQUIRED
and check_hostname validate the server certificate: it
ensures that the server certificate was signed with one of the CA
certificates, checks the signature for correctness, and verifies other
properties like validity and identity of the hostname:
>>> conn = context.wrap_socket(socket.socket(socket.AF_INET),
... server_hostname="www.python.org")
>>> conn.connect(("www.python.org", 443))
ãããŠèšŒææžãæã£ãŠããããšãã§ããŸã:
>>> cert = conn.getpeercert()
èšŒææžããæåŸ
ããŠãããµãŒãã¹ (ã€ãŸãã HTTPS ãã¹ã www.python.org) ã®èº«å
ãç¹å®ããŠããããšãèŠèŠçã«ç¹æ€ããŠã¿ãŸããã:
>>> pprint.pprint(cert)
{'OCSP': ('http://ocsp.digicert.com',),
'caIssuers': ('http://cacerts.digicert.com/DigiCertSHA2ExtendedValidationServerCA.crt',),
'crlDistributionPoints': ('http://crl3.digicert.com/sha2-ev-server-g1.crl',
'http://crl4.digicert.com/sha2-ev-server-g1.crl'),
'issuer': ((('countryName', 'US'),),
(('organizationName', 'DigiCert Inc'),),
(('organizationalUnitName', 'www.digicert.com'),),
(('commonName', 'DigiCert SHA2 Extended Validation Server CA'),)),
'notAfter': 'Sep 9 12:00:00 2016 GMT',
'notBefore': 'Sep 5 00:00:00 2014 GMT',
'serialNumber': '01BB6F00122B177F36CAB49CEA8B6B26',
'subject': ((('businessCategory', 'Private Organization'),),
(('1.3.6.1.4.1.311.60.2.1.3', 'US'),),
(('1.3.6.1.4.1.311.60.2.1.2', 'Delaware'),),
(('serialNumber', '3359300'),),
(('streetAddress', '16 Allen Rd'),),
(('postalCode', '03894-4801'),),
(('countryName', 'US'),),
(('stateOrProvinceName', 'NH'),),
(('localityName', 'Wolfeboro'),),
(('organizationName', 'Python Software Foundation'),),
(('commonName', 'www.python.org'),)),
'subjectAltName': (('DNS', 'www.python.org'),
('DNS', 'python.org'),
('DNS', 'pypi.org'),
('DNS', 'docs.python.org'),
('DNS', 'testpypi.org'),
('DNS', 'bugs.python.org'),
('DNS', 'wiki.python.org'),
('DNS', 'hg.python.org'),
('DNS', 'mail.python.org'),
('DNS', 'packaging.python.org'),
('DNS', 'pythonhosted.org'),
('DNS', 'www.pythonhosted.org'),
('DNS', 'test.pythonhosted.org'),
('DNS', 'us.pycon.org'),
('DNS', 'id.python.org')),
'version': 3}
SSL ãã£ãã«ã¯ä»ã確ç«ãããŠèšŒææžãæ€èšŒãããŠããã®ã§ããµãŒããšã®ãåããç¶ããããšãã§ããŸã:
>>> conn.sendall(b"HEAD / HTTP/1.0\r\nHost: linuxfr.org\r\n\r\n")
>>> pprint.pprint(conn.recv(1024).split(b"\r\n"))
[b'HTTP/1.1 200 OK',
b'Date: Sat, 18 Oct 2014 18:27:20 GMT',
b'Server: nginx',
b'Content-Type: text/html; charset=utf-8',
b'X-Frame-Options: SAMEORIGIN',
b'Content-Length: 45679',
b'Accept-Ranges: bytes',
b'Via: 1.1 varnish',
b'Age: 2188',
b'X-Served-By: cache-lcy1134-LCY',
b'X-Cache: HIT',
b'X-Cache-Hits: 11',
b'Vary: Cookie',
b'Strict-Transport-Security: max-age=63072000; includeSubDomains',
b'Connection: close',
b'',
b'']
ãã®ããã¥ã¡ã³ãã®äžã®æ¹ã®ã ã»ãã¥ãªãã£ã§èæ ®ãã¹ãç¹ ã«é¢ããè°è«ãåç §ããŠãã ããã
ãµãŒããµã€ãã®åŠç¶
ãµãŒããµã€ãã®åŠçã§ã¯ãéåžžããµãŒããŒèšŒææžãšç§å¯éµããããããã¡ã€ã«ã«æ ŒçŽããã圢ã§å¿
èŠã§ããæåã«ç§å¯éµãšèšŒææžãä¿æãããã³ã³ããã¹ããäœæããã¯ã©ã€ã¢ã³ããããªãã®ä¿¡ææ§ããã§ãã¯ã§ããããã«ããŸãããã®ã®ã¡ã«ãœã±ãããéããããŒãã«ãã€ã³ããããã®ãœã±ããã® listen() ãåŒã³ãã¯ã©ã€ã¢ã³ãããã®æ¥ç¶ãåŸ
ã¡ãŸãã
import socket, ssl
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile="mycertfile", keyfile="mykeyfile")
bindsocket = socket.socket()
bindsocket.bind(('myaddr.example.com', 10023))
bindsocket.listen(5)
ã¯ã©ã€ã¢ã³ããæ¥ç¶ããŠããå Žåã accept() ãåŒãã§æ°ãããœã±ãããäœæããæ¥ç¶ã®ããã«ãµãŒããµã€ãã® SSL ãœã±ããããã³ã³ããã¹ãã® SSLContext.wrap_socket() ã¡ãœããã§äœããŸã:
while True:
newsocket, fromaddr = bindsocket.accept()
connstream = context.wrap_socket(newsocket, server_side=True)
try:
deal_with_client(connstream)
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
ãããŠã connstream ããããŒã¿ãèªã¿ãã¯ã©ã€ã¢ã³ããšåæãã(ãããã¯ã¯ã©ã€ã¢ã³ããåæããŠãã)ãŸã§äœãåŠçãããŸãã
def deal_with_client(connstream):
data = connstream.recv(1024)
# empty data means the client is finished with us
while data:
if not do_something(connstream, data):
# we'll assume do_something returns False
# when we're finished with client
break
data = connstream.recv(1024)
# finished with client
ãããŠæ°ããã¯ã©ã€ã¢ã³ãæ¥ç¶ã®ããã« listen ã«æ»ããŸãã (ãã¡ããçŸå®ã®ãµãŒãã¯ãããããåã ã®ã¯ã©ã€ã¢ã³ãæ¥ç¶ããšã«å¥ã®ã¹ã¬ããã§åŠçãããããœã±ããã ãã³ããããã³ã°ã¢ãŒã ã«ããã€ãã³ãã«ãŒãã䜿ãã§ãããã)
ãã³ããããã³ã°ãœã±ããã«ã€ããŠã®æ³šæäºé ¶
SSL ãœã±ããã¯ãã³ããããã³ã°ã¢ãŒãã«ãããŠã¯ãæ®éã®ãœã±ãããšã¯å°ãéã£ãæ¯ãèããããŸããã§ãã®ã§ãã³ããããã³ã°ãœã±ãããšãšãã«äœ¿ãå Žåãããã€ãæ°ãã€ããªããã°ãªããªãäºé ããããŸã:
ã»ãšãã©ã®
SSLSocketã®ã¡ãœãã㯠I/O æäœããããã¯ãããšBlockingIOErrorã§ã¯ãªãSSLWantWriteErrorãSSLWantReadErrorã®ã©ã¡ãããéåºããŸããSSLWantReadErrorã¯äžå±€ã®ãœã±ããã§èªã¿åºããå¿ èŠãªå Žåã«éåºãããSSLWantWriteErrorã¯äžå±€ã®ãœã±ããã§æžã蟌ã¿ãå¿ èŠãªå Žåã«éåºãããŸããSSL ãœã±ããã«å¯Ÿã㊠æžã蟌㿠ã詊ã¿ããšäžå±€ã®ãœã±ããããæåã« èªã¿åºã å¿ èŠãããããããããSSL ãœã±ããã«å¯Ÿã㊠èªã¿åºã ã詊ã¿ããšäžå±€ã®ãœã±ããã«å ã« æžã蟌ã å¿ èŠããããããããªãããšã«æ³šæããŠãã ãããããŒãžã§ã³ 3.5 ã§å€æŽ: 以åã® Python ããŒãžã§ã³ã§ã¯ã
SSLSocket.send()ã¡ãœããã¯SSLWantWriteErrorãŸãã¯SSLWantReadErrorãéåºããã®ã§ã¯ãªãããŒããè¿ããŠããŸãããselect()åŒã³åºã㯠OS ã¬ãã«ã§ã®ãœã±ãããèªã¿åºãå¯èœ(ãŸãã¯æžã蟌ã¿å¯èœ)ã«ãªã£ãããšãæããŠãããŸãããäžäœã® SSL ã¬ã€ã€ãŒã§ã®ååãªããŒã¿ãããããšãæå³ããããã§ã¯ãããŸãããäŸãã°ãSSL ãã¬ãŒã ã®äžéšãå±ããã ããããããŸãããã§ããããSSLSocket.recv()ãšSSLSocket.send()ã®å€±æãåŠçããããšã«åããã»ãã®select()åŒã³åºãåŸã«ãªãã©ã€ããªããã°ãªããŸãããå察ã«ãSSL ã¬ã€ã€ãŒã¯ç¬èªã®æ çµã¿ãæã£ãŠããããã
select()ãæ°ä»ããªãèªã¿åºãå¯èœãªããŒã¿ã SSL ãœã±ãããæã£ãŠããå ŽåããããŸãããããã£ãŠãå ¥æå¯èœãªå¯èœæ§ã®ããããŒã¿ããã¹ãŠåŒãåºãããã«æåã«SSLSocket.recv()ãåŒã³åºããæ¬¡ã«ããã§ããŸã å¿ èŠãªå Žåã«ã ãselect()åŒã³åºãã§ãããã¯ãã¹ãã§ãã(åœç¶ã®ããšãªãããã»ãã®ããªããã£ããäŸãã°
poll()ãselectorsã¢ãžã¥ãŒã«å ã®ãã®ã䜿ãéã«ã䌌ãäœãæžããä»ããŸã)SSL ãã³ãã·ã§ã€ã¯ãã®ãã®ããã³ããããã³ã°ã«ãªããŸã:
SSLSocket.do_handshake()ã¡ãœããã¯æåãããŸã§ãªãã©ã€ããªããã°ãªããŸãããselect()ãçšããŠãœã±ããã®æºåãæŽãã®ãåŸ ã€ããã«ã¯ãããã以äžã®ããã«ããŸã:while True: try: sock.do_handshake() break except ssl.SSLWantReadError: select.select([sock], [], []) except ssl.SSLWantWriteError: select.select([], [sock], [])
åè
The asyncio module supports non-blocking SSL sockets and provides a higher level Streams API.
It polls for events using the selectors module and
handles SSLWantWriteError, SSLWantReadError and
BlockingIOError exceptions. It runs the SSL handshake asynchronously
as well.
Memory BIO support¶
Added in version 3.5.
Python 2.6 ã§ SSL ã¢ãžã¥ãŒã«ãå°å
¥ãããŠä»¥éãSSLSocket ã¯ã©ã¹ã¯ã以äžã®äºãã«é¢é£ãããå¥ã
ã®æ©èœãæäŸããŠããŸããã
SSL ãããã³ã«åŠç
ãããã¯ãŒã¯ IO
ãããã¯ãŒã¯ IO API ã¯ãsocket.socket ãæäŸãããã®ãšåãã§ããSSLSocket ãããã®ã¯ã©ã¹ããç¶æ¿ããŠããŸããããã«ãããSSL ãœã±ããã¯æšæºã®ãœã±ããããã£ãããã®ãŸãŸçœ®ãæãããã®ãšããŠäœ¿çšã§ãããããæ¢åã®ã¢ããªã±ãŒã·ã§ã³ã SSL ã«å¯Ÿå¿ãããã®ãéåžžã«ç°¡åã«ãªããŸãã
SSL ãããã³ã«ã®åŠçãšãããã¯ãŒã¯ IO ãçµã¿åãããå Žåãéåžžã¯åé¡ãªãåäœããŸãããåé¡ãçºçããå ŽåããããŸããäžäŸãæãããšãéåæ IO ãã¬ãŒã ã¯ãŒã¯ãå¥ã®å€éåã¢ãã«ã䜿çšããå Žåããã㯠socket.socket ãšå
éš OpenSSL ãœã±ãã IO ã«ãŒãã£ã³ãæ³å®ããããã¡ã€ã«èšè¿°åäžã® select/pollãã¢ãã«ïŒæºåç¶æ
ããŒã¹ïŒãšã¯ç°ãªããŸããããã¯ããã®ã¢ãã«ãéå¹ççã«ãªã Windows ãªã©ã®ãã©ãããã©ãŒã ã«äž»ã«è©²åœããŸãããã®ãããã¹ã³ãŒããéå®ãã SSLSocket ã®å€çš®ã SSLObject ãæäŸãããŠããŸãã
- class ssl.SSLObject¶
ãããã¯ãŒã¯ IO ã¡ãœãããå«ãŸãªã SSL ãããã³ã«ã€ã³ã¹ã¿ã³ã¹ã衚ããã¹ã³ãŒããéå®ãã
SSLSocketã®å€çš®ã§ããäžè¬çã«ããã®ã¯ã©ã¹ã䜿çšããã®ã¯ãã¡ã¢ãªãããã¡ãéã㊠SSL ã®ããã®éåæ IO ãå®è£ ãããã¬ãŒã ã¯ãŒã¯äœæè ã§ãããã®ã¯ã©ã¹ã¯ãOpenSSL ãå®è£ ããäœæ°Žæº SSL ãªããžã§ã¯ãã®äžã«ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£ ããŸãããã®ãªããžã§ã¯ã㯠SSL æ¥ç¶ã®ç¶æ ããã£ããã£ããŸããããããã¯ãŒã¯ IO èªäœã¯æäŸããŸãããIO ã¯ãOpenSSL ã® IO æœè±¡ã¬ã€ã€ã§ããå¥ã®ãBIOããªããžã§ã¯ããéããŠå®è¡ããå¿ èŠããããŸãã
ãã®ã¯ã©ã¹ã«ã¯å ¬éãããã³ã³ã¹ãã©ã¯ã¿ããããŸããã
SSLObjectã€ã³ã¹ã¿ã³ã¹ã¯ãwrap_bio()ã¡ãœããã䜿çšããŠäœæããªããã°ãªããŸããããã®ã¡ãœããã¯ãSSLObjectã€ã³ã¹ã¿ã³ã¹ãäœæãã2 ã€ã® BIO ã«æçžããŸããincoming BIO ã¯ãPython ãã SSL ãããã³ã«ã€ã³ã¹ã¿ã³ã¹ã«ããŒã¿ãæž¡ãããã«äœ¿çšãããoutgoing BIO ã¯ãããŒã¿ãå察åãã«æž¡ãããã«äœ¿çšãããŸããæ¬¡ã®ã¡ãœããããµããŒããããŠããŸã:
SSLSocketãšæ¯èŒãããšããã®ãªããžã§ã¯ãã§ã¯ä»¥äžã®æ©èœãäžè¶³ããŠããŸããAny form of network IO;
recv()andsend()read and write only to the underlyingMemoryBIObuffers.do_handshake_on_connect æ©æ§ã¯ãããŸãããå¿ ãæåã§
do_handshake()ãåŒãã§ããã³ãã·ã§ã€ã¯ãéå§ããå¿ èŠããããŸããsuppress_ragged_eofs ã¯åŠçãããŸããããããã³ã«ã«éåãããã¡ã€ã«æ«å°Ÿç¶æ ã¯ã
SSLEOFErroräŸå€ãéããŠå ±åãããŸããunwrap()ã¡ãœããã®åŒã³åºãã¯ãäžå±€ã®ãœã±ãããè¿ã SSL ãœã±ãããšã¯ç°ãªããäœãè¿ããŸãããSSLContext.set_servername_callback()ã«æž¡ããã server_name_callback ã³ãŒã«ããã¯ã¯ã1 ã€ç®ã®åŒæ°ãšããŠSSLSocketã€ã³ã¹ã¿ã³ã¹ã§ã¯ãªãSSLObjectã€ã³ã¹ã¿ã³ã¹ãåãåããŸãã
SSLObjectã®äœ¿çšã«é¢ããæ³šæ:SSLObjectäžã®ãã¹ãŠã® IO 㯠non-blocking ã§ããäŸãã°ãread()ã¯å ¥å BIO ãæã€ããŒã¿ãããå€ãã®ããŒã¿ãå¿ èŠãšããå ŽåãSSLWantReadErrorãéåºããŸãã
ããŒãžã§ã³ 3.7 ã§å€æŽ:
SSLObjectinstances must be created withwrap_bio(). In earlier versions, it was possible to create instances directly. This was never documented or officially supported.
SSLObject ã¯ãã¡ã¢ãªãããã¡ã䜿çšããŠå€çãšéä¿¡ããŸããMemoryBIO ã¯ã©ã¹ã¯ã以äžã®ããã« OpenSSL ã¡ã¢ãª BIO (Basic IO) ãªããžã§ã¯ããã©ãããããã®ç®çã«äœ¿çšã§ããã¡ã¢ãªãããã¡ãæäŸããŸãã
- class ssl.MemoryBIO¶
Python ãš SSL ãããã³ã«ã€ã³ã¹ã¿ã³ã¹éã§ããŒã¿ãããåãããããã«äœ¿çšã§ããã¡ã¢ãªãããã¡ã
- pending¶
çŸåšã¡ã¢ãªãããã¡äžã«ãããã€ãæ°ãè¿ããŸãã
- eof¶
ã¡ã¢ãª BIOãçŸåšãã¡ã€ã«ã®æ«å°Ÿã«ãããã衚ãçåœå€ã§ãã
- read(n=-1, /)¶
ã¡ã¢ãªãããã¡ããæå€§ n èªã¿åããŸããn ãæå®ãããŠããªãããè² å€ã®å Žåããã¹ãŠã®ãã€ããè¿ãããŸãã
- write(buf, /)¶
buf ããã¡ã¢ãª BIO ã«ãã€ããæžã蟌ã¿ãŸããbuf åŒæ°ã¯ããããã¡ãããã³ã«ããµããŒããããªããžã§ã¯ãã§ãªããã°ãªããŸããã
æ»ãå€ã¯ãæžã蟌ãŸãããã€ãæ°ã§ãããåžžã« buf ã®é·ããšçãããªããŸãã
SSL ã»ãã·ã§ã³Â¶
Added in version 3.6.
ã»ãã¥ãªãã£ã§èæ ®ãã¹ãç¹Â¶
æåã®ããã©ã«ãå€Â¶
ã¯ã©ã€ã¢ã³ãã§ã®äœ¿çš ã§ã¯ãã»ãã¥ãªãã£ããªã·ãŒã«ããç¹æ®ãªèŠä»¶ããªãéãã¯ã create_default_context() 颿°ã䜿çšã㊠SSL ã³ã³ããã¹ããäœæããããšã匷ããå§ãããŸãããã®é¢æ°ã¯ãã·ã¹ãã ã®ä¿¡é Œæžã¿ CA èšŒææžãããŒãããèšŒææžã®æ€èšŒãšãã¹ãåã®ãã§ãã¯ãæå¹åããååã«ã»ãã¥ã¢ãªãããã³ã«ãšæå·ãéžæããããšããŸãã
äŸãšããŠã smtplib.SMTP ã¯ã©ã¹ã䜿çšã㊠SMTP ãµãŒããŒã«å¯ŸããŠä¿¡é Œã§ããã»ãã¥ã¢ãªæ¥ç¶ãè¡ãæ¹æ³ã以äžã«ç€ºããŸã:
>>> import ssl, smtplib
>>> smtp = smtplib.SMTP("mail.python.org", port=587)
>>> context = ssl.create_default_context()
>>> smtp.starttls(context=context)
(220, b'2.0.0 Ready to start TLS')
æ¥ç¶ã«ã¯ã©ã€ã¢ã³ãã®èšŒææžãå¿
èŠãªå Žåã SSLContext.load_cert_chain() ã«ãã£ãŠè¿œå ã§ããŸãã
察ç
§çã«ãèªåèªèº«ã§ SSLContext ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ãåŒã³åºãããšã«ãã£ãŠ SSL ã³ã³ããã¹ããäœããšãããã©ã«ãã§ã¯èšŒææžæ€èšŒããã¹ãåãã§ãã¯ãæå¹ã«ãªããŸãããèªåã§èšå®ãè¡ãå Žåã¯ãååãªã»ãã¥ãªãã£ã¬ãã«ãéæããããã«ã以äžã®ãã©ã°ã©ãããèªã¿ãã ããã
æåã§ã®èšå®Â¶
èšŒææžã®æ€èšŒÂ¶
When calling the SSLContext constructor directly,
CERT_NONE is the default. Since it does not authenticate the other
peer, it can be insecure, especially in client mode where most of the time you
would like to ensure the authenticity of the server you're talking to.
Therefore, when in client mode, it is highly recommended to use
CERT_REQUIRED. However, it is in itself not sufficient; you also
have to check that the server certificate, which can be obtained by calling
SSLSocket.getpeercert(), matches the desired service. For many
protocols and applications, the service can be identified by the hostname.
This common check is automatically performed when
SSLContext.check_hostname is enabled.
ããŒãžã§ã³ 3.7 ã§å€æŽ: Hostname matchings is now performed by OpenSSL. Python no longer uses
match_hostname().
ãµãŒãã¢ãŒãã«ãããŠã(ããäžäœã®ã¬ãã«ã§ã®èªèšŒã¡ã«ããºã ã§ã¯ãªã) SSL ã¬ã€ã€ãŒã䜿ã£ãŠããªãã®ã¯ã©ã€ã¢ã³ããèªèšŒããããªãã°ã CERT_REQUIRED ãæå®ããŠåãããã«ã¯ã©ã€ã¢ã³ãã®èšŒææžãæ€èšŒãã¹ãã§ãããã
ãããã³ã«ã®ããŒãžã§ã³Â¶
SSL ããŒãžã§ã³ 2 ãš 3 ã¯å®å
šæ§ã«æ¬ ãããšèããããŠããã䜿çšããã®ã¯å±éºã§ããã¯ã©ã€ã¢ã³ããšãµãŒãéã®äºææ§ãæå€§éã«ç¢ºä¿ãããå Žåããããã³ã«ããŒãžã§ã³ãšã㊠PROTOCOL_TLS_CLIENT ãŸã㯠PROTOCOL_TLS_SERVER ã䜿çšããŠãã ããã SSLv2 ãš SSLv3 ã¯ããã©ã«ãã§ç¡å¹ã«ãªã£ãŠããŸãã
>>> client_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
>>> client_context.minimum_version = ssl.TLSVersion.TLSv1_2
>>> client_context.maximum_version = ssl.TLSVersion.TLSv1_3
The SSL client context created above will only allow TLSv1.2 and TLSv1.3 (if
supported by your system) connections to a server. PROTOCOL_TLS_CLIENT
implies certificate validation and hostname checks by default. You have to
load certificates into the context.
æå·ã®éžæÂ¶
If you have advanced security requirements, fine-tuning of the ciphers
enabled when negotiating a SSL session is possible through the
SSLContext.set_ciphers() method. Starting from Python 3.2.3, the
ssl module disables certain weak ciphers by default, but you may want
to further restrict the cipher choice. Be sure to read OpenSSL's documentation
about the cipher list format.
If you want to check which ciphers are enabled by a given cipher list, use
SSLContext.get_ciphers() or the openssl ciphers command on your
system.
ãã«ãããã»ã¹å¶
If using this module as part of a multi-processed application (using,
for example the multiprocessing or concurrent.futures modules),
be aware that OpenSSL's internal random number generator does not properly
handle forked processes. Applications must change the PRNG state of the
parent process if they use any SSL feature with os.fork(). Any
successful call of RAND_add() or RAND_bytes() is
sufficient.
TLS 1.3¶
Added in version 3.7.
The TLS 1.3 protocol behaves slightly differently than previous version of TLS/SSL. Some new TLS 1.3 features are not yet available.
TLS 1.3 uses a disjunct set of cipher suites. All AES-GCM and ChaCha20 cipher suites are enabled by default. The method
SSLContext.set_ciphers()cannot enable or disable any TLS 1.3 ciphers yet, butSSLContext.get_ciphers()returns them.Session tickets are no longer sent as part of the initial handshake and are handled differently.
SSLSocket.sessionandSSLSessionare not compatible with TLS 1.3.Client-side certificates are also no longer verified during the initial handshake. A server can request a certificate at any time. Clients process certificate requests while they send or receive application data from the server.
TLS 1.3 features like early data, deferred TLS client cert request, signature algorithm configuration, and rekeying are not supported yet.
åè
socket.socketã¯ã©ã¹äžäœã¬ã€ã€ãŒã®
socketã¯ã©ã¹ã®ããã¥ã¡ã³ã- SSL/TLS Strong Encryption: An Introduction
Apache HTTPãµãŒãã®ããã¥ã¡ã³ããŒã·ã§ã³ã®ã€ã³ãã
- RFC 1422: Privacy Enhancement for Internet Electronic Mail: Part II: Certificate-Based Key Management
Steve Kent
- RFC 4086: Randomness Requirements for Security
Donald E. Eastlake, Jeffrey I. Schiller, Steve Crocker
- RFC 5280: Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile
David Cooper et al.
- RFC 5246: The Transport Layer Security (TLS) Protocol Version 1.2
Tim Dierks and Eric Rescorla.
- RFC 6066: Transport Layer Security (TLS) Extensions
Donald E. Eastlake
- IANA TLS: Transport Layer Security (TLS) Parameters
IANA
- RFC 7525: Recommendations for Secure Use of Transport Layer Security (TLS) and Datagram Transport Layer Security (DTLS)
IETF
- Mozilla's Server Side TLS recommendations
Mozilla