3 y^vc@spdZddlZddlZddlZddlZddlZddlmZddlm Z m Z m ZddlZddlmZmZmZddlmZmZmZddlmZmZmZmZmZmZddlmZmZdd lm Z m!Z!m"Z"m#Z#ydd lm$Z$Wne%k rYnXdd lm&Z&m'Z'm(Z(m)Z)m*Z*dd lm+Z+dd lm,Z,e j-de.ddedej-de.ddede j-de.ddede j-de.ddedej-de.ddede j-de.ddede/j0Z1e/_1dde/j2j3DZ4e5e/ddZ6ej7dkrdd lm8Z8m9Z9dd!l:m:Z:m;Z;mZ>m?Z?ddl@Z@ddlAZAddlBZBeCZDejEr>d#gZFngZFe+ZGGd$d%d%eHZIdRd'd(ZJd)d*ZKd+d,ZLed-d.ZMd/d0ZNGd1d2d2ed2d3ZOGd4d5d5eOe ZPGd6d7d7eZQePjRfdddd8d9d:ZSe0fdd;ePjRdddddd<d=d>ZTeSZUeTZVGd?d@d@ZWGdAdBdBe:ZXddd;eYe0ddCdCdf dDdEZZdFdGZ[dHZ\dIZ]dJdKZ^dLdMZ_e0dfdNdOZ`dPdQZadS)Sa This module provides some more Pythonic support for SSL. Object types: SSLSocket -- subtype of socket.socket which does SSL over the socket Exceptions: SSLError -- exception raised for I/O errors Functions: cert_time_to_seconds -- convert time string used for certificate notBefore and notAfter functions to integer seconds past the Epoch (the time values returned from time.time()) fetch_server_certificate (HOST, PORT) -- fetch the certificate provided by the server running on HOST at port PORT. No validation of the certificate is performed. Integer constants: SSL_ERROR_ZERO_RETURN SSL_ERROR_WANT_READ SSL_ERROR_WANT_WRITE SSL_ERROR_WANT_X509_LOOKUP SSL_ERROR_SYSCALL SSL_ERROR_SSL SSL_ERROR_WANT_CONNECT SSL_ERROR_EOF SSL_ERROR_INVALID_ERROR_CODE The following group define certificate requirements that one side is allowing/requiring from the other side: CERT_NONE - no certificates from the other side are required (or will be looked at if provided) CERT_OPTIONAL - certificates are not required, but if provided will be validated, and if validation fails, the connection will also fail CERT_REQUIRED - certificates are required, and will be validated, and if validation fails, the connection will also fail The following constants identify various SSL protocol variants: PROTOCOL_SSLv2 PROTOCOL_SSLv3 PROTOCOL_SSLv23 PROTOCOL_TLS PROTOCOL_TLS_CLIENT PROTOCOL_TLS_SERVER PROTOCOL_TLSv1 PROTOCOL_TLSv1_1 PROTOCOL_TLSv1_2 The following constants identify various SSL alert message descriptions as per http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6 ALERT_DESCRIPTION_CLOSE_NOTIFY ALERT_DESCRIPTION_UNEXPECTED_MESSAGE ALERT_DESCRIPTION_BAD_RECORD_MAC ALERT_DESCRIPTION_RECORD_OVERFLOW ALERT_DESCRIPTION_DECOMPRESSION_FAILURE ALERT_DESCRIPTION_HANDSHAKE_FAILURE ALERT_DESCRIPTION_BAD_CERTIFICATE ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE ALERT_DESCRIPTION_CERTIFICATE_REVOKED ALERT_DESCRIPTION_CERTIFICATE_EXPIRED ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN ALERT_DESCRIPTION_ILLEGAL_PARAMETER ALERT_DESCRIPTION_UNKNOWN_CA ALERT_DESCRIPTION_ACCESS_DENIED ALERT_DESCRIPTION_DECODE_ERROR ALERT_DESCRIPTION_DECRYPT_ERROR ALERT_DESCRIPTION_PROTOCOL_VERSION ALERT_DESCRIPTION_INSUFFICIENT_SECURITY ALERT_DESCRIPTION_INTERNAL_ERROR ALERT_DESCRIPTION_USER_CANCELLED ALERT_DESCRIPTION_NO_RENEGOTIATION ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE ALERT_DESCRIPTION_UNRECOGNIZED_NAME ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY N) namedtuple)EnumIntEnumIntFlag)OPENSSL_VERSION_NUMBEROPENSSL_VERSION_INFOOPENSSL_VERSION) _SSLContext MemoryBIO SSLSession)SSLErrorSSLZeroReturnErrorSSLWantReadErrorSSLWantWriteErrorSSLSyscallError SSLEOFError)txt2objnid2obj) RAND_statusRAND_add RAND_bytesRAND_pseudo_bytes)RAND_egd)HAS_SNIHAS_ECDHHAS_NPNHAS_ALPN HAS_TLSv1_3)_DEFAULT_CIPHERS)_OPENSSL_API_VERSION _SSLMethodcCs|jdo|dkS)NZ PROTOCOL_PROTOCOL_SSLv23 startswithnamer%/usr/lib64/python3.6/ssl.py}r')ZsourceOptionscC |jdS)NZOP_r!r#r%r%r&r'r(ZAlertDescriptioncCr*)NZALERT_DESCRIPTION_r!r#r%r%r&r'r(ZSSLErrorNumbercCr*)NZ SSL_ERROR_r!r#r%r%r&r'r( VerifyFlagscCr*)NZVERIFY_r!r#r%r%r&r'r( VerifyModecCr*)NZCERT_r!r#r%r%r&r'r(cCsi|]\}}||qSr%r%)Z.0r$valuer%r%r& sr.ZPROTOCOL_SSLv2win32)enum_certificates enum_crls)socketAF_INET SOCK_STREAMcreate_connection) SOL_SOCKETSO_TYPE tls-uniquec@s eZdZdS)CertificateErrorN)__name__ __module__ __qualname__r%r%r%r&r9sr9c Csg}|s dS|jd^}}|jd}||kr|jdsx|jdr|jtj|n|jtj|j ddx|D]}|jtj|qWtj d d j |d tj }|j |S) zhMatching according to RFC 6125, section 6.4.3 http://tools.ietf.org/html/rfc6125#section-6.4.3 FZ.Z*z,too many wildcards in certificate DNS name: z[^.]+zxn--z\*z[^.]*z\Az\.z\Z)Zsplitcountr9reprZlowerappendr"reZescapeZreplaceZcompilejoinZ IGNORECASEZmatch) ZdnhostnameZ max_wildcardsZpatsZleftmostZ remainderZ wildcardsZfragZpatr%r%r&_dnsname_matchs&   rDcCstj|j}||kS)zExact matching of IP addresses. RFC 6125 explicitly doesn't define an algorithm for this (section 1.7.2 - "Out of Scope"). ) ipaddress ip_addressZrstrip)Zipnamehost_ipZipr%r%r&_ipaddress_matchsrHcCsP|s tdytj|}Wntk r2d}YnXg}|jdf}xb|D]Z\}}|dkr||dkrpt||rpdS|j|qJ|dkrJ|dk rt||rdS|j|qJW|sxF|jdfD]6}x0|D](\}}|dkrt||rdS|j|qWqWt|dkr td |d j t t |fn,t|dkrDtd ||d fntd dS)a)Verify that *cert* (in decoded format as returned by SSLSocket.getpeercert()) matches the *hostname*. RFC 2818 and RFC 6125 rules are followed, but IP addresses are not accepted for *hostname*. CertificateError is raised on failure. On success, the function returns nothing. ztempty or no certificate, match_hostname needs a SSL socket or SSL context with either CERT_OPTIONAL or CERT_REQUIREDNZsubjectAltNameZDNSz IP AddressZsubjectZ commonNamer=z&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rrz=no appropriate commonName or subjectAltName fields were found) ValueErrorrErFgetrDr@rHlenr9rBZmapr?)certrCrGZdnsnamesZsanZkeyr-Zsubr%r%r&match_hostnames>    rMDefaultVerifyPathszQcafile capath openssl_cafile_env openssl_cafile openssl_capath_env openssl_capathcCsdtj}tjj|d|d}tjj|d|d}ttjj|rF|ndtjj|rX|ndf|S)z/Return paths to default cafile and capath. rr=N) _sslget_default_verify_pathsosZenvironrJrNZpathZisfileZisdir)Zpartscafilecapathr%r%r&rR-s rRcsDeZdZdZfZfddZefddZefddZZ S) _ASN1Objectz#ASN.1 object identifier lookup ctj|ft|ddS)NFr#super__new___txt2obj)clsoid __class__r%r&rZ@z_ASN1Object.__new__cstj|ft|S)z3Create _ASN1Object from OpenSSL numeric ID )rYrZ_nid2obj)r\Znidr^r%r&fromnidCz_ASN1Object.fromnidcrW)z=Create _ASN1Object from short name, long name or OID Tr#rX)r\r$r^r%r&fromnameIrcz_ASN1Object.fromname) r:r;r<__doc__ __slots__rZZ classmethodrbrd __classcell__r%r%r^r&rV;s  rVznid shortname longname oidc@seZdZdZdZdZdS)PurposezDSSLContext purpose flags with X509v3 Extended Key Usage objects z1.3.6.1.5.5.7.3.1z1.3.6.1.5.5.7.3.2N)r:r;r<re SERVER_AUTHZ CLIENT_AUTHr%r%r%r&rhPsrhcseZdZdZd"Zd#ZefddZefdd Zd$d dZ d%ddZ ddZ ddZ ddZ ejfddZefddZejfddZefddZejfddZefdd Zejfd!d ZZS)& SSLContextz|An SSLContext holds various SSL-related configuration options and data, such as certificates and possibly a private key.protocol __weakref__CAROOTcOstj||}|SN)rrZ)r\rkargskwargsselfr%r%r&rZ^s zSSLContext.__new__cCs ||_dSro)rk)rrrkr%r%r&__init__br`zSSLContext.__init__FTNc Cst|||||||dS)N)sock server_sidedo_handshake_on_connectsuppress_ragged_eofsserver_hostname_context_session SSLSocket)rrrtrurvrwrxsessionr%r%r& wrap_socketes zSSLContext.wrap_socketcCs|j||||d}t||dS)N)rurx)r})Z _wrap_bio SSLObject)rrZincomingZoutgoingrurxr}sslobjr%r%r&wrap_bioos zSSLContext.wrap_biocCdt}xN|D]F}t|d}t|dks2t|dkr:td|jt||j|q W|j|dS)Nasciirz(NPN protocols must be 1 to 255 in length) bytearraybytesrKr r@extendZ_set_npn_protocols)rr npn_protocolsprotosrkbr%r%r&set_npn_protocolsu  zSSLContext.set_npn_protocolscCr)Nrrrz)ALPN protocols must be 1 to 255 in length)rrrKr r@rZ_set_alpn_protocols)rrZalpn_protocolsrrkrr%r%r&set_alpn_protocolsrzSSLContext.set_alpn_protocolsc Cszt}y@x:t|D].\}}}|dkr|dks6|j|kr|j|qWWntk rdtjdYnX|rv|j|d|S)NZx509_asnTz-unable to enumerate Windows certificate store)cadata)rr0r]rZPermissionErrorwarningsZwarnload_verify_locations)rr storenamepurposeZcertsrLZencodingZtrustr%r%r&_load_windows_store_certss z$SSLContext._load_windows_store_certscCsDt|tst|tjdkr8x|jD]}|j||q$W|jdS)Nr/) isinstancerV TypeErrorsysplatform_windows_cert_storesrZset_default_verify_paths)rrrrr%r%r&load_default_certss    zSSLContext.load_default_certsc ttjSro)r)rYoptionsrrr^r%r&rzSSLContext.optionsctttjj||dSro)rYrjr__set__rrr-r^r%r&rrcrro)r+rY verify_flagsrr^r%r&rrzSSLContext.verify_flagscrro)rYrjrrrr^r%r&rrc s*tj}yt|Stk r$|SXdSro)rY verify_moder,rIrr^r%r&rs zSSLContext.verify_modecrro)rYrjrrrr^r%r&rr)rkrl)rmrn)FTTNN)FNN)r:r;r<rerfr PROTOCOL_TLSrZrsr~rrrrrhrirpropertyrsetterrrrgr%r%r^r&rjWs*      rj)rTrUrcCsdt|tst|tt}|tjkr0t|_d|_ |s<|s<|rL|j |||n|jt kr`|j ||S)zCreate a SSLContext object with default settings. NOTE: The protocol and settings may change anytime without prior deprecation. The values represent a fair balance between maximum compatibility and security. T) rrVrrjrrhri CERT_REQUIREDrcheck_hostnamer CERT_NONEr)rrTrUrcontextr%r%r&create_default_contexts     rF) cert_reqsrrcertfilekeyfilerTrUrc Cst|tst|t|} |dk r(|| _|| _|r@| r@td|sH|rT| j|||s`|s`|rp| j|||n| jt kr| j || S)a/Create a SSLContext object for Python stdlib modules All Python stdlib modules shall use this function to create SSLContext objects in order to keep common settings in one place. The configuration is less restrict than create_default_context()'s to increase backward compatibility. Ncertfile must be specified) rrVrrjrrrIload_cert_chainrrr) rkrrrrrrTrUrrr%r%r&_create_unverified_contexts      rc@seZdZdZd0ddZeddZejddZedd Zejd d Zed d Z ed dZ eddZ d1ddZ ddZ d2ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd3d*d+Zd,d-Zd.d/ZdS)4raThis class implements an interface on top of a low-level SSL object as implemented by OpenSSL. This object captures the state of an SSL connection but does not provide any network IO itself. IO needs to be performed through separate "BIO" objects which are OpenSSL's IO abstraction layer. This class does not have a public constructor. Instances are returned by ``SSLContext.wrap_bio``. This class is typically used by framework authors that want to implement asynchronous IO for SSL through memory buffers. When compared to ``SSLSocket``, this object lacks the following features: * Any form of network IO, including methods such as ``recv`` and ``send``. * The ``do_handshake_on_connect`` and ``suppress_ragged_eofs`` machinery. NcCs&||_|p ||j_|dk r"||j_dSro)_sslobjownerr})rrrrr}r%r%r&rss zSSLObject.__init__cC|jjS)z(The SSLContext that is currently in use.rrrr%r%r&r"zSSLObject.contextcC ||j_dSrorrrZctxr%r%r&r'rcCr)!The SSLSession for client socket.rr}rr%r%r&r}+rzSSLObject.sessioncCrrorrrr}r%r%r&r}0rcCr).Was the client session reused during handshakersession_reusedrr%r%r&r4rzSSLObject.session_reusedcCr)z%Whether this is a server-side socket.)rrurr%r%r&ru9rzSSLObject.server_sidecCr)z]The currently set server hostname (for SNI), or ``None`` if no server hostame is set.)rrxrr%r%r&rx>rczSSLObject.server_hostnamecCs(|dk r|jj||}n |jj|}|S)zRead up to 'len' bytes from the SSL object and return them. If 'buffer' is provided, read into this buffer and return the number of bytes read. N)rread)rrrKbuffervr%r%r&rDs zSSLObject.readcC |jj|S)zWrite 'data' to the SSL object and return the number of bytes written. The 'data' argument must support the buffer interface. )rwriterrdatar%r%r&rPszSSLObject.writeFcCr)zReturns a formatted version of the data in the certificate provided by the other end of the SSL channel. Return None if no certificate was provided, {} if a certificate was provided, but not validated. )rZpeer_certificaterrZ binary_formr%r%r& getpeercertXszSSLObject.getpeercertcCtjr|jjSdS)zReturn the currently selected NPN protocol as a string, or ``None`` if a next protocol was not negotiated or if NPN is not supported by one of the peers.N)rQrrselected_npn_protocolrr%r%r&razSSLObject.selected_npn_protocolcCr)zReturn the currently selected ALPN protocol as a string, or ``None`` if a next protocol was not negotiated or if ALPN is not supported by one of the peers.N)rQrrselected_alpn_protocolrr%r%r&rhrz SSLObject.selected_alpn_protocolcC |jjS)z_Return the currently selected cipher as a 3-tuple ``(name, ssl_version, secret_bits)``.)rcipherrr%r%r&rorzSSLObject.ciphercCr)zReturn a list of ciphers shared by the client during the handshake or None if this is not a valid server connection. )rshared_ciphersrr%r%r&rtrczSSLObject.shared_cipherscCr)zReturn the current compression algorithm in use, or ``None`` if compression was not negotiated or not supported by one of the peers.)r compressionrr%r%r&rzrzSSLObject.compressioncCr)z8Return the number of bytes that can be read immediately.)rpendingrr%r%r&rrzSSLObject.pendingcCs4|jj|jjr0|js tdt|j|jdS)zStart the SSL/TLS handshake.z-check_hostname needs server_hostname argumentN)r do_handshakerrrxrIrMrrr%r%r&rs  zSSLObject.do_handshakecCr)z!Start the SSL shutdown handshake.)rshutdownrr%r%r&unwraprzSSLObject.unwrapr8cCs0|tkrtd|dkr&tdj||jjS)zGet channel binding data for current connection. Raise ValueError if the requested `cb_type` is not supported. Return bytes of the data or None if the data is not available (e.g. before the handshake).z Unsupported channel binding typer8z({0} channel binding type not implemented)CHANNEL_BINDING_TYPESrINotImplementedErrorZformatrZ tls_unique_cbrrZcb_typer%r%r&get_channel_bindingszSSLObject.get_channel_bindingcCr)zZReturn a string identifying the protocol version used by the current SSL channel. rversionrr%r%r&rrzSSLObject.versioncCrro)rverify_client_post_handshakerr%r%r&rr`z&SSLObject.verify_client_post_handshake)NNrNFr8)r:r;r<rersrrrr}rrurxrrrrrrrrrrrrrrr%r%r%r&r s.          rcseZdZdZddddeeddeeddddddddfddZe dd Z e j d d Z e d d Z e j d d Z e ddZ ddZdVddZddZdWddZddZdXddZddZdd Zd!d"Zd#d$Zd%d&ZdYd'd(ZdZd)d*Zd+d,Zd[d-d.Zd\fd/d0 Zd]d1d2Zd^d3d4Zd_d5d6Z d`d7d8Z!d9d:Z"d;d<Z#d=d>Z$d?d@Z%dAdBZ&dCdDZ'dEdFZ(dadGdHZ)dIdJZ*dKdLZ+dMdNZ,dOdPZ-dbdRdSZ.dTdUZ/Z0S)cr|zThis class implements a subtype of socket.socket that wraps the underlying OS socket in an SSL context when necessary, and provides read and write methods over that channel.NFTrcCsj|r ||_n|r| rtd|r0| r0td|r>| r>|}t||_||j_|r`|jj||rr|jj|||r|jj||r|jj|||_||_ ||_ ||_ ||_ ||_ |jtttkrtd|r|rtd|dk rtd|jjo| rtd||_||_||_||_| |_|dk rdtj||j|j|j|jd|j|j |j!n,| dk r~tj|| dntj|| | | d y |j"Wn8t#k r}z|j$t$j%krd }WYdd}~XnXd }d |_&d|_'||_(|rfyN|jj)|||}t*|||jd |_'|r>|j }|d kr6td|j+Wn$t#tfk rd|j,YnXdS)Nz5certfile must be specified for server-side operationsrz!only stream sockets are supportedz4server_hostname can only be specified in client modez,session can only be specified in client modez'check_hostname requires server_hostname)familytypeprotofileno)r)rrrFTrr}zHdo_handshake_on_connect should not be specified for non-blocking sockets)-ryrIrjrrrrZ set_ciphersrrr ssl_versionca_certsciphersZ getsockoptr6r7r4rrrzrurxrvrwr2rsrrrr settimeout gettimeoutZdetach getpeernameOSErrorerrnoZENOTCONNZ_closedr _connected _wrap_socketrrZclose)rrrtrrrurrrrvrrrrrwrrrxryrzZeZ connectedrtimeoutr%r%r&rss                 zSSLSocket.__init__cCs|jSro)ryrr%r%r&r rzSSLSocket.contextcCs||_||j_dSro)ryrrrr%r%r&rscC|jdk r|jjSdS)rNrrr%r%r&r} zSSLSocket.sessioncCs||_|jdk r||j_dSro)rzrr}rr%r%r&r}s cCr)rNrrr%r%r&r rzSSLSocket.session_reusedcCstd|jjdS)NzCan't dup() %s instances)rr_r:rr%r%r&dup&z SSLSocket.dupcCsdSror%)rrZmsgr%r%r& _checkClosed*rzSSLSocket._checkClosedcCs|js|jdSro)rrrr%r%r&_check_connected.szSSLSocket._check_connectedrcCst|j|jstdy|jj||Stk rn}z.|jdtkr\|jr\|dk rVdSdSnWYdd}~XnXdS)zORead up to LEN bytes and return them. Return zero-length string on EOF.z'Read on closed or unwrapped SSL socket.rNr()rrrIrr rpZ SSL_ERROR_EOFrw)rrrKrZxr%r%r&r6szSSLSocket.readcCs"|j|jstd|jj|S)zhWrite DATA to the underlying SSL channel. Returns number of bytes of DATA actually transmitted.z(Write on closed or unwrapped SSL socket.)rrrIrrr%r%r&rHszSSLSocket.writecCs|j|j|jj|S)zReturns a formatted version of the data in the certificate provided by the other end of the SSL channel. Return None if no certificate was provided, {} if a certificate was provided, but not validated.)rrrrrr%r%r&rQszSSLSocket.getpeercertcC*|j|j stj rdS|jjSdSro)rrrQrrrr%r%r&r[zSSLSocket.selected_npn_protocolcCrro)rrrQrrrr%r%r&rbrz SSLSocket.selected_alpn_protocolcC |j|jsdS|jjSdSro)rrrrr%r%r&rizSSLSocket.ciphercCs|j|jsdS|jjSro)rrrrr%r%r&rpszSSLSocket.shared_cipherscCrro)rrrrr%r%r&rvrzSSLSocket.compressioncCsB|j|jr0|dkr$td|j|jj|Stj|||SdS)Nrz3non-zero flags not allowed in calls to send() on %s)rrrIr_rr2send)rrrflagsr%r%r&r}s  zSSLSocket.sendcCsH|j|jrtd|jn&|dkr4tj|||Stj||||SdS)Nz%sendto not allowed on instances of %s)rrrIr_r2sendto)rrrZ flags_or_addraddrr%r%r&rs zSSLSocket.sendtocOtd|jdS)Nz&sendmsg not allowed on instances of %srr_rrrprqr%r%r&sendmsgszSSLSocket.sendmsgcCs|j|jr|dkr$td|jd}t|L}|jd6}t|}x&||krl|j||d}||7}qHWWdQRXWdQRXntj |||SdS)Nrz6non-zero flags not allowed in calls to sendall() on %sZB) rrrIr_Z memoryviewZcastrKrr2sendall)rrrrr>ZviewZ byte_viewZamountrr%r%r&rs  "zSSLSocket.sendallcs,|jdkrtj|||S|j|||SdS)zSend a file, possibly by using os.sendfile() if this is a clear-text socket. Return the total number of bytes sent. N)rrYsendfileZ_sendfile_use_send)rrZfileZoffsetr>r^r%r&rs zSSLSocket.sendfilecCs@|j|jr.|dkr$td|j|j|Stj|||SdS)Nrz3non-zero flags not allowed in calls to recv() on %s)rrrIr_rr2recvrrZbuflenrr%r%r&rs  zSSLSocket.recvcCsf|j|r|dkrt|}n |dkr*d}|jrR|dkrFtd|j|j||Stj||||SdS)Nrrz8non-zero flags not allowed in calls to recv_into() on %s)rrKrrIr_rr2 recv_intorrrZnbytesrr%r%r&rs    zSSLSocket.recv_intocCs0|j|jrtd|jntj|||SdS)Nz'recvfrom not allowed on instances of %s)rrrIr_r2recvfromrr%r%r&r  zSSLSocket.recvfromcCs2|j|jrtd|jntj||||SdS)Nz,recvfrom_into not allowed on instances of %s)rrrIr_r2 recvfrom_intorr%r%r&r rzSSLSocket.recvfrom_intocOr)Nz&recvmsg not allowed on instances of %srrr%r%r&recvmsgrzSSLSocket.recvmsgcOr)Nz+recvmsg_into not allowed on instances of %srrr%r%r& recvmsg_intorzSSLSocket.recvmsg_intocCs |j|jr|jjSdSdSNr)rrrrr%r%r&rs zSSLSocket.pendingcCs|jd|_tj||dSro)rrr2r)rrZhowr%r%r&rszSSLSocket.shutdowncCs.|jr|jj}d|_|Stdt|dSNzNo SSL wrapper around )rrrIstr)rrZsr%r%r&rs  zSSLSocket.unwrapcCs$|jr|jjStdt|dSr )rrrIrrr%r%r&rs z&SSLSocket.verify_client_post_handshakecCsd|_tj|dSro)rr2 _real_closerr%r%r&rszSSLSocket._real_closec CsF|j|j}z$|dkr(|r(|jd|jjWd|j|XdS)zPerform a TLS/SSL handshake.rN)rrrrr)rrZblockrr%r%r&rs  zSSLSocket.do_handshakec Cs|jrtd|jrtd|jj|d|j}t|||jd|_y>|rTt j ||}nd}t j |||s|d|_|j r||j |Sttfk rd|_YnXdS)Nz!can't connect in server-side modez/attempt to connect already-connected SSLSocket!FrT)rurIrrrrxrrzrr2 connect_exconnectrvrr)rrrrrZrcr%r%r& _real_connect s(  zSSLSocket._real_connectcCs|j|ddS)QConnects to remote ADDR, and then wraps the connection in an SSL channel.FNrrrrr%r%r&r&rzSSLSocket.connectcCs |j|dS)rTrrr%r%r&r+rzSSLSocket.connect_excCs.tj|\}}|jj||j|jdd}||fS)zAccepts a new connection from a remote client, and returns a tuple containing that new connection wrapped with a server-side SSL channel, and the address of the remote client.T)rvrwru)r2acceptrr~rvrw)rrZnewsockrr%r%r&r0s zSSLSocket.acceptr8cCs|jdkrdS|jj|S)zGet channel binding data for current connection. Raise ValueError if the requested `cb_type` is not supported. Return bytes of the data or None if the data is not available (e.g. before the handshake). N)rrrr%r%r&r< zSSLSocket.get_channel_bindingcCs|jdkrdS|jjS)z Return a string identifying the protocol version used by the current SSL channel, or None if there is no established channel. Nrrr%r%r&rErzSSLSocket.versionrorrrror)rNrrr rr rr)1r:r;r<rerrr3r4rsrrrr}rrrrrrrrrrrrrrrrrrrrr r r rrrrrrrrrrrrrgr%r%r^r&r|s`Z             r|Tc Cst|||||||||| d S)N rtrrrurrrrvrwrr{rr%r%r&r~Os r~c Csddlm}ddlm}d}d}y|j|ddjd}Wn$tk rbtd||fYn0X||dd|}||d|f|ddSdS)aReturn the time in seconds since the Epoch, given the timestring representing the "notBefore" or "notAfter" date from a certificate in ``"%b %d %H:%M:%S %Y %Z"`` strptime format (C locale). "notBefore" or "notAfter" dates must use UTC (RFC 5280). Month is one of: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec UTC should be specified as GMT (see ASN1_TIME_print()) r)strptime)timegmJanFebMarAprMayJunJulAugSepOctNovDecz %d %H:%M:%S %Y GMTNrPr=z*time data %r does not match format "%%b%s"rOi) rrrr r!r"r#r$r%r&r'r()ZtimerZcalendarrZindexZtitlerI)Z cert_timerrZmonthsZ time_formatZ month_numberZttr%r%r&cert_time_to_seconds^s  r)z-----BEGIN CERTIFICATE-----z-----END CERTIFICATE-----cCs2ttj|dd}tdtj|ddtdS)z[Takes a certificate in binary DER format and returns the PEM version of it as a string.ASCIIstrictZ i@)rbase64Zstandard_b64encode PEM_HEADERtextwrapZfill PEM_FOOTER)Zder_cert_bytesZfr%r%r&DER_cert_to_PEM_certsr0cCs\|jtstdt|jjts0tdt|jtttt }tj|j ddS)zhTakes a certificate in ASCII PEM format and returns the DER-encoded version of it as a byte sequencez(Invalid PEM encoding; must start with %sz&Invalid PEM encoding; must end with %sr*r+) r"r-rIZstripZendswithr/rKr,Z decodebytesZencode)Zpem_cert_stringZdr%r%r&PEM_cert_to_DER_certs r1c Csd|\}}|dk rt}nt}t|||d}t|&}|j|}|jd} WdQRXWdQRXt| S)zRetrieve the certificate from the server at the specified address, and return it as a PEM-encoded string. If 'ca_certs' is specified, validate the server cert against it. If 'ssl_version' is specified, use it in the connection attempt.N)rrTT)rr_create_stdlib_contextr5r~rr0) rrrZhostZportrrrtZsslsockZdercertr%r%r&get_server_certificates  r3cCs tj|dS)Nz )_PROTOCOL_NAMESrJ)Z protocol_coder%r%r&get_protocol_namer`r5)r=)brerEr.rArrSZ collectionsrZenumrZ_EnumrZ_IntEnumrZ_IntFlagrQrrrrr r r r r rrrrr[rrarrrrrZ ImportErrorrrrrrrrZ_convertr:rrr Z __members__Zitemsr4ZgetattrZ_SSLv2_IF_EXISTSrr0r1r2r3r4r5r6r7r,rrrZ socket_errorZHAS_TLS_UNIQUErZ_RESTRICTED_SERVER_CIPHERSrIr9rDrHrMrNrRrVrhrjrirrZ_create_default_https_contextr2rr|rr~r)r-r/r0r1r3r5r%r%r%r&Z[s        1 4g%-