U e5dH @sddlZddlZddlmZddlmZmZm Z ddl Z ddl m Z m Z mZddl mZmZmZddl mZmZmZmZmZmZmZddl mZmZddl mZmZmZm Z zdd l m!Z!Wne"k rYnXdd l m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,dd l m-Z-m.Z.ej/d e0d de de j/de0dde dej/de0dde dej/de0dde de j/de0dde dej/de0dde de1j2Z3e1_3dde1j45DZ6e7e1ddZ8GdddeZ9Gdd d eZ:Gd!d"d"eZ;Gd#d$d$eZZ>m?Z?dd'l@m@Z@mAZAmBZBmCZCdd(l@mDZDmEZEddl@ZFddlGZGddlHZHddlIZIeJZKd)gZLeMe d*ZNe-ZOeZPd+d,ZQd-d.ZRd/d0ZSd1d2ZTed3d4ZUd5d6ZVGd7d8d8ed8d9ZWGd:d;d;eWeZXGdd?d@Z[e2fe\dAeXjZddddddBdCdDZ]e[Z^e]Z_GdEdFdFZ`dGdHZaGdIdJdJe@ZbebeY_ce`eY_ddddAe\e2ddKdKdf dLdMZedNdOZfdPZgdQZhdRdSZidTdUZje2dfdVdWZkdXdYZldS)ZN) namedtuple)EnumIntEnumIntFlag)OPENSSL_VERSION_NUMBEROPENSSL_VERSION_INFOOPENSSL_VERSION) _SSLContext MemoryBIO SSLSession)SSLErrorSSLZeroReturnErrorSSLWantReadErrorSSLWantWriteErrorSSLSyscallError SSLEOFErrorSSLCertVerificationError)txt2objnid2obj) RAND_statusRAND_add RAND_bytesRAND_pseudo_bytes)RAND_egd) HAS_SNIHAS_ECDHHAS_NPNHAS_ALPN HAS_SSLv2 HAS_SSLv3 HAS_TLSv1 HAS_TLSv1_1 HAS_TLSv1_2 HAS_TLSv1_3)_DEFAULT_CIPHERS_OPENSSL_API_VERSION _SSLMethodcCs|do|dkS)NZ PROTOCOL_PROTOCOL_SSLv23 startswithnamer,/usr/lib64/python3.8/ssl.py|r.)sourceOptionscCs |dS)NZOP_r(r*r,r,r-r.r/ZAlertDescriptioncCs |dS)NZALERT_DESCRIPTION_r(r*r,r,r-r.r/ZSSLErrorNumbercCs |dS)NZ SSL_ERROR_r(r*r,r,r-r.r/ VerifyFlagscCs |dS)NZVERIFY_r(r*r,r,r-r.r/ VerifyModecCs |dS)NZCERT_r(r*r,r,r-r.r/cCsi|]\}}||qSr,r,).0r+valuer,r,r- sr6ZPROTOCOL_SSLv2c@s6eZdZejZejZejZ ej Z ej Z ejZejZdS) TLSVersionN)__name__ __module__ __qualname___sslZPROTO_MINIMUM_SUPPORTEDZMINIMUM_SUPPORTEDZ PROTO_SSLv3SSLv3Z PROTO_TLSv1ZTLSv1Z PROTO_TLSv1_1ZTLSv1_1Z PROTO_TLSv1_2ZTLSv1_2Z PROTO_TLSv1_3ZTLSv1_3ZPROTO_MAXIMUM_SUPPORTEDZMAXIMUM_SUPPORTEDr,r,r,r-r7sr7c@s$eZdZdZdZdZdZdZdZdS)_TLSContentTypeN) r8r9r:CHANGE_CIPHER_SPECALERTZ HANDSHAKEZAPPLICATION_DATAHEADERZINNER_CONTENT_TYPEr,r,r,r-r=s r=c@seZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"d!Z#d"Z$d#S)$ _TLSAlertTyper r>r?r@()*+,-./0123<FGPVZdmnopqrstxN)%r8r9r:Z CLOSE_NOTIFYZUNEXPECTED_MESSAGEZBAD_RECORD_MACZDECRYPTION_FAILEDZRECORD_OVERFLOWZDECOMPRESSION_FAILUREZHANDSHAKE_FAILUREZNO_CERTIFICATEZBAD_CERTIFICATEZUNSUPPORTED_CERTIFICATEZCERTIFICATE_REVOKEDZCERTIFICATE_EXPIREDZCERTIFICATE_UNKNOWNZILLEGAL_PARAMETERZ UNKNOWN_CAZ ACCESS_DENIEDZ DECODE_ERRORZ DECRYPT_ERRORZEXPORT_RESTRICTIONZPROTOCOL_VERSIONZINSUFFICIENT_SECURITYZINTERNAL_ERRORZINAPPROPRIATE_FALLBACKZ USER_CANCELEDZNO_RENEGOTIATIONZMISSING_EXTENSIONZUNSUPPORTED_EXTENSIONZCERTIFICATE_UNOBTAINABLEZUNRECOGNIZED_NAMEZBAD_CERTIFICATE_STATUS_RESPONSEZBAD_CERTIFICATE_HASH_VALUEZUNKNOWN_PSK_IDENTITYZCERTIFICATE_REQUIREDZNO_APPLICATION_PROTOCOLr,r,r,r-rGsDrGc@sdeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdS)_TLSMessageTyper r>r?r@rACrCN)r8r9r:Z HELLO_REQUESTZ CLIENT_HELLOZ SERVER_HELLOZHELLO_VERIFY_REQUESTZNEWSESSION_TICKETZEND_OF_EARLY_DATAZHELLO_RETRY_REQUESTZENCRYPTED_EXTENSIONSZ CERTIFICATEZSERVER_KEY_EXCHANGEZCERTIFICATE_REQUESTZ SERVER_DONEZCERTIFICATE_VERIFYZCLIENT_KEY_EXCHANGEZFINISHEDZCERTIFICATE_URLZCERTIFICATE_STATUSZSUPPLEMENTAL_DATAZ KEY_UPDATEZ NEXT_PROTOZ MESSAGE_HASHrDr,r,r,r-rfs,rfwin32)enum_certificates enum_crls)socketAF_INET SOCK_STREAMcreate_connection) SOL_SOCKETSO_TYPE tls-uniqueHOSTFLAG_NEVER_CHECK_SUBJECTcCs|sdS|d}|s&||kS|dkrsole wildcard without additional labels are not support: {!r}.ztd ||d fntd dS)Nztempty or no certificate, match_hostname needs a SSL socket or SSL context with either CERT_OPTIONAL or CERT_REQUIREDZsubjectAltNamer,ZDNSz IP AddressZsubjectZ commonNamergz&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rrz=no appropriate commonName or subjectAltName fields were found) rrgetrappendrlenrjoinmaprepr)certrrZdnsnamesZsankeyr5subr,r,r-match_hostnamessB          rDefaultVerifyPathszQcafile capath openssl_cafile_env openssl_cafile openssl_capath_env openssl_capathcCsdt}tj|d|d}tj|d|d}ttj|rF|ndtj|rX|ndf|S)Nrrgrhri) r;get_default_verify_pathsosenvironrrpathisfileisdir)partscafilecapathr,r,r-rsrcs@eZdZdZfddZefddZefddZZS) _ASN1Objectr,cstj|ft|ddS)NFr*super__new___txt2obj)clsoid __class__r,r-rsz_ASN1Object.__new__cstj|ft|Sr)rr_nid2obj)rZnidrr,r-fromnidsz_ASN1Object.fromnidcstj|ft|ddS)NTr*r)rr+rr,r-fromnamesz_ASN1Object.fromname) r8r9r: __slots__r classmethodrr __classcell__r,r,rr-rs  rznid shortname longname oidc@seZdZdZdZdS)Purposez1.3.6.1.5.5.7.3.1z1.3.6.1.5.5.7.3.2N)r8r9r: SERVER_AUTHZ CLIENT_AUTHr,r,r,r-rsrcseZdZdZdZdZefddZddZd1d d Z d2d d Z d dZ ddZ ddZ ddZejfddZeedrefddZejfddZefddZejfddZefddZejfd dZeed!red"d#Zejd$d#Zn ed%d#Zefd&d'Zejfd(d'Zefd)d*Zefd+d,Zejfd-d,Zefd.d/Zejfd0d/ZZS)3 SSLContext)ZCAZROOTNcOst||}|Sr)r r)rprotocolargskwargsselfr,r,r-rs zSSLContext.__new__cCs4|dkr dSt|tr&|ddS|dSdS)NZidnaascii) isinstancestrencodedecode)rrr,r,r-_encode_hostnames  zSSLContext._encode_hostnameFTc Cs|jj|||||||dS)N)sock server_sidedo_handshake_on_connectsuppress_ragged_eofsserver_hostnamecontextsession)sslsocket_class_create)rrrrrrrr,r,r- wrap_socketszSSLContext.wrap_socketcCs|jj|||||||dS)N)rrrr)sslobject_classrr)rincomingoutgoingrrrr,r,r-wrap_bioszSSLContext.wrap_biocCs`t}|D]F}t|d}t|dks0t|dkr8td|t|||q ||dS)Nrrz(NPN protocols must be 1 to 255 in length) bytearraybytesrr rextendZ_set_npn_protocols)rZ npn_protocolsprotosrbr,r,r-set_npn_protocolss  zSSLContext.set_npn_protocolscs8dkrd_n$ts tdfdd}|_dS)Nznot a callable objectcs|}|||Sr)r)sslobjZ servernameZsslctxrserver_name_callbackr,r-shim_cbs z3SSLContext.set_servername_callback..shim_cb)Z sni_callbackcallable TypeError)rrrr,rr-set_servername_callbacks z"SSLContext.set_servername_callbackcCs`t}|D]F}t|d}t|dks0t|dkr8td|t|||q ||dS)Nrrrz)ALPN protocols must be 1 to 255 in length)rrrr rrZ_set_alpn_protocols)rZalpn_protocolsrrrr,r,r-set_alpn_protocols s  zSSLContext.set_alpn_protocolscCsvt}z.inner)rrrrhasattrrr)rrrrrr-rs  cs ttjSr)r&rrrrr,r-rszSSLContext.protocolcs ttjSr)r2r verify_flagsrrr,r-rszSSLContext.verify_flagscstttj||dSr)rrrrrrr,r-rscs0tj}z t|WStk r*|YSXdSr)r verify_moder3rrrr,r-rs  zSSLContext.verify_modecstttj||dSr)rrrrrrr,r-rs)FTTNN)FNN) r8r9r:rrr PROTOCOL_TLSrrrrrrrrrrrrr propertyrsetterrrr;rrrrrrr,r,rr-rsl           &%r)rrrcCst|tst|tt}|tjkr0t|_d|_ |s<|s<|rL| |||n|jt kr`| |t |drtjd}|rtjjs||_|S)NTkeylog_filename SSLKEYLOGFILE)rrrrrrr CERT_REQUIREDrcheck_hostnamer CERT_NONErrrrrrflagsignore_environmentr)rrrrr keylogfiler,r,r-create_default_contexts        rF) cert_reqsr rcertfilekeyfilerrrc Cst|tst|t|} |s$d| _|dk r2|| _|rd?Z"fd@dAZ#edBdCZ$edDdEZ%fdFdGZ&edcdHdIZ'fdJdKZ(dLdMZ)dNdOZ*fdPdQZ+edddSdTZ,edUdVZ-Z.S)e SSLSocketcOst|jjddS)NzX does not have a public constructor. Instances are returned by SSLContext.wrap_socket().rrr,r,r-rs zSSLSocket.__init__FTNc s|tttkrtd|r8|r(td|dk r8td|jrJ|sJtdt|j|j |j | d}|j |f|} t t| jf|| |||| _|| _d| _d| _|| _||| _|| _|| _z | Wn6tk r} z| jtjkrd} W5d} ~ XYnXd} | | _ | rzH| jj!| || j| | jd| _|rj| } | d krbtd | "Wn$ttfk r| #YnX| S) Nz!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)familytypeprotofilenoFTrrzHdo_handshake_on_connect should not be specified for non-blocking sockets)$Z getsockoptr~rr|NotImplementedErrorrr dictr=r>r?r@rrr<r settimeout gettimeoutdetach_context_sessionZ_closedrrrrrr getpeernamererrnoZENOTCONN _connected _wrap_socketr1close) rrrrrrrrrreZ connectedtimeoutrr,r-rsj      zSSLSocket._createcCs|jSr)rHrr,r,r-rszSSLSocket.contextcCs||_||j_dSr)rHrrrr,r,r-rscCs|jdk r|jjSdSrrrr,r,r-r s zSSLSocket.sessioncCs||_|jdk r||j_dSr)rIrrr r,r,r-r&s cCs|jdk r|jjSdSrr!rr,r,r-r",s zSSLSocket.session_reusedcCstd|jjdS)NzCan't dup() %s instances)rCrr8rr,r,r-dup2sz SSLSocket.dupcCsdSrr,)rmsgr,r,r- _checkClosed6szSSLSocket._checkClosedcCs|js|dSr)rLrJrr,r,r-_check_connected:szSSLSocket._check_connectedr#c Cs||jdkrtdz*|dk r4|j||WS|j|WSWnVtk r}z8|jdtkr|jr|dk r|WYdSWY dSnW5d}~XYnXdS)Nz'Read on closed or unwrapped SSL socket.rr/)rSrrr$r rZ SSL_ERROR_EOFr)rrr%xr,r,r-r$Bs   zSSLSocket.readcCs&||jdkrtd|j|S)Nz(Write on closed or unwrapped SSL socket.)rSrrr'r(r,r,r-r'Ws zSSLSocket.writecCs|||j|Sr)rSrTrr)r*r,r,r-r)`szSSLSocket.getpeercertcCs*||jdkstjsdS|jSdSr)rSrr;rr+rr,r,r-r+fszSSLSocket.selected_npn_protocolcCs*||jdkstjsdS|jSdSr)rSrr;rr,rr,r,r-r,nsz SSLSocket.selected_alpn_protocolcCs$||jdkrdS|jSdSr)rSrr-rr,r,r-r-vs zSSLSocket.ciphercCs$||jdkrdS|jSdSr)rSrr.rr,r,r-r.~s zSSLSocket.shared_cipherscCs$||jdkrdS|jSdSr)rSrr/rr,r,r-r/s zSSLSocket.compressionrcsF||jdk r4|dkr(td|j|j|St||SdS)Nrz3non-zero flags not allowed in calls to send() on %s)rSrrrr'rsend)rrr rr,r-rVs  zSSLSocket.sendcsL||jdk r"td|jn&|dkr8t||St|||SdS)Nz%sendto not allowed on instances of %s)rSrrrrsendto)rrZ flags_or_addrrrr,r-rWs zSSLSocket.sendtocOstd|jdS)Nz&sendmsg not allowed on instances of %srCrrr,r,r-sendmsgszSSLSocket.sendmsgc s||jdk r|dkr(td|jd}t|H}|d2}t|}||krn|||d}||7}qJW5QRXW5QRXnt ||SdS)Nrz6non-zero flags not allowed in calls to sendall() on %sB) rSrrr memoryviewcastrrVrsendall)rrr rZviewZ byte_viewZamountr&rr,r-r]s  zSSLSocket.sendallcs,|jdk r||||St|||SdSr)rZ_sendfile_use_sendrsendfile)rfileoffsetrrr,r-r^s zSSLSocket.sendfilecsD||jdk r2|dkr(td|j||St||SdS)Nrz3non-zero flags not allowed in calls to recv() on %s)rSrrrr$rrecvrZbuflenr rr,r-ras  zSSLSocket.recvcsj||r|dkrt|}n |dkr*d}|jdk rV|dkrJtd|j|||St|||SdS)Nr#rz8non-zero flags not allowed in calls to recv_into() on %s)rSrrrrr$r recv_intorr%nbytesr rr,r-rcs    zSSLSocket.recv_intocs4||jdk r"td|jnt||SdS)Nz'recvfrom not allowed on instances of %s)rSrrrrrecvfromrbrr,r-rfs  zSSLSocket.recvfromcs6||jdk r"td|jnt|||SdS)Nz,recvfrom_into not allowed on instances of %s)rSrrrr recvfrom_intordrr,r-rgs  zSSLSocket.recvfrom_intocOstd|jdS)Nz&recvmsg not allowed on instances of %srXrr,r,r-recvmsgszSSLSocket.recvmsgcOstd|jdS)Nz+recvmsg_into not allowed on instances of %srXrr,r,r- recvmsg_intoszSSLSocket.recvmsg_intocCs$||jdk r|jSdSdS)Nr)rSrr0rr,r,r-r0s  zSSLSocket.pendingcs|d|_t|dSr)rSrrr2)rZhowrr,r-r2szSSLSocket.shutdowncCs.|jr|j}d|_|Stdt|dSNzNo SSL wrapper around )rr2rr)rsr,r,r-r3s  zSSLSocket.unwrapcCs$|jr|jStdt|dSrj)rr7rrrr,r,r-r7 s z&SSLSocket.verify_client_post_handshakecsd|_tdSr)rr _real_closerrr,r-rlszSSLSocket._real_closec CsF||}z$|dkr(|r(|d|jW5||XdS)NrB)rTrFrErr1)rblockrPr,r,r-r1s  zSSLSocket.do_handshakec s|jrtd|js|jdk r&td|jj|d|j||jd|_z@|rVt |}nd}t ||s~d|_|j r~| |WSt tfk rd|_YnXdS)Nz!can't connect in server-side modez/attempt to connect already-connected SSLSocket!FrAT)rrrLrrrMrrIr connect_exconnectrr1r)rrrnZrcrr,r- _real_connect!s0 zSSLSocket._real_connectcCs||ddS)NFrprrr,r,r-ro;szSSLSocket.connectcCs ||dSrrqrrr,r,r-rn@szSSLSocket.connect_excs.t\}}|jj||j|jdd}||fS)NT)rrr)racceptrrrr)rZnewsockrrr,r-rsEszSSLSocket.acceptrcCs4|jdk r|j|S|tkr,td|dSdS)Nz({0} channel binding type not implemented)rr4CHANNEL_BINDING_TYPESrrr5r,r,r-r4Qs  zSSLSocket.get_channel_bindingcCs|jdk r|jSdSdSrr6rr,r,r-r\s  zSSLSocket.version)FTTNNN)N)r#N)F)r)N)r)rN)r#r)Nr)r#r)Nr)F)r)/r8r9r:rrrrr;rrrr"rQrSrTr$r'r)r+r,r-r.r/rVrWrYr]r^rarcrfrgrhrir0r2r3r7rlr1rprornrsr4rrr,r,rr-r<s>                          r<Tc Csl|r|std|r |s tdt|} || _|r<| ||rL| ||| rZ| | | j||||dS)Nz5certfile must be specified for server-side operationsr)rrrr)rrrrrZ set_ciphersr) rrrrr ssl_versionca_certsrrZciphersrr,r,r-ris$   rcCsddlm}ddlm}d}d}z||ddd}Wn$tk rbtd||fYn0X||dd|}||d|f|d d SdS) Nr)strptime)timegm) ZJanZFebZMarZAprZMayZJunZJulZAugZSepZOctZNovZDecz %d %H:%M:%S %Y GMTrirgz*time data %r does not match format "%%b%s"rhrl)timerwZcalendarrxindextitler)Z cert_timerwrxZmonthsZ time_formatZ month_numberZttr,r,r-cert_time_to_secondss   r|z-----BEGIN CERTIFICATE-----z-----END CERTIFICATE-----csRtt|ddtg}|fddtdtdD7}|tdd|S)NASCIIstrictcsg|]}||dqS)@r,)r4ifr,r- sz(DER_cert_to_PEM_cert..rr ) rbase64Zstandard_b64encode PEM_HEADERrangerr PEM_FOOTERr)Zder_cert_bytesZssr,rr-DER_cert_to_PEM_certs "rcCs\|tstdt|ts0tdt|tttt }t| ddS)Nz(Invalid PEM encoding; must start with %sz&Invalid PEM encoding; must end with %sr}r~) r)rrstripendswithrrrZ decodebytesr)Zpem_cert_stringdr,r,r-PEM_cert_to_DER_certs rc Csd|\}}|dk rt}nt}t|||d}t|&}||}|d} W5QRXW5QRXt| S)N)rrT)r r _create_stdlib_contextr}rr)r) rrurvZhostZportrrrZsslsockZdercertr,r,r-get_server_certificates  rcCs t|dS)Nz )_PROTOCOL_NAMESr)Z protocol_coder,r,r-get_protocol_namesr)mrr collectionsrenumrZ_EnumrZ_IntEnumrZ_IntFlagr;rrrr r r r r rrrrrrrrrrrrrr ImportErrorrrrrrrr r!r"r#r$r% _convert_r8r&rr' __members__itemsrr8Z_SSLv2_IF_EXISTSr7r=rGrfrrxryrzr{r|r}r~rrrrKrrZ socket_errorrtrZHAS_NEVER_CHECK_COMMON_NAMEZ_RESTRICTED_SERVER_CIPHERSrrrrrrrrrrrrr rZ_create_default_https_contextrrr;r<rrrr|rrrrrrr,r,r,r-]s $0   )  1# 9z # /