
    cn                     4   d dl Z d dlZd dlZd dlZd dlZddlmZ ddlmZ ddl	m
Z
 ddlmZmZmZmZ ddlmZmZmZ h dZ eed	d
          Z e            Z eed ej        ed                    re                    d            eed ej        ed                    re                    d            eed ej        ed                    re                    d           d Zd ZddZ	 d dZ G d de          Zd!dZd Z d Z!d Z"d Z#dS )"    N   )_)getattr)hex)encodingerrorpycompatutil)hashutilresourceutil
stringutil>      tls1.0   tls1.1   tls1.2HAS_SNIF	HAS_TLSv1PROTOCOL_TLSv1r   HAS_TLSv1_1PROTOCOL_TLSv1_1r   HAS_TLSv1_2PROTOCOL_TLSv1_2r   c           	         t          j        |          }dg ddddddd}d }t          dhz
  sJ d}d}|                     d	||          } |||           d
|z  }|                     d	||          } |||           |                     d	d          }|                     d	d|z  |          }| j        rd}|sd}||d<   ||d<   |                     d	d|z            }	|	D ]}
|
                    d          s5t          j        t          d          ||
fz  t          d                    |

                    dd          \  }}
|
                    dd                                          }
|d                             ||
f           |                     d|          D ]L}
|
                    dd                                          }
|d                             d|
f           d|d<   M|d         rt          j        |d<   d|d<   n | j        rd|d<   t          j        |d<   d|d<   |                     dd          rd|d<   |                     d	d|z            }|d         r'|r%|                     t          d           |z             |d         G|rct%          j        |          }t(          j                            |          s*t          j        t          d!          d"|fz  |fz            ||d#<   n|                     d$d%          }|rkt%          j        |          }t(          j                            |          s7t          j        t          d&          |z  t          d'          d(z  z             n1|d         r)t/          |           }|r|                     d)|z             ||d#<   |s|d         rt          j        |d<   nt          j        |d<   |d         J |S )*zhObtain security settings for a hostname.

    Returns a dict of settings relevant to that hostname.
    TNF)   allowloaddefaultcerts   certfingerprints   cafile   disablecertverification   legacyfingerprint   minimumprotocol
   verifymode   ciphersc           	          | t           vr]t          j        t          d          || fz  t          d          d                    t          t                               z            d S )Ns-   unsupported protocol from hostsecurity.%s: %ss   valid protocols: %s    hint)configprotocolsr   Abortr   joinsorted)protocolkeys     3/usr/lib/python3/dist-packages/mercurial/sslutil.pyvalidateprotocolz'_hostsettings.<locals>.validateprotocolZ   sn    ?**+BCC/"-..))F?33445    +*    r   r   r      hostsecuritys   %s:minimumprotocolr    s
   %s:cipherss   DEFAULT:@SECLEVEL=0s   %s:fingerprints)s   sha1:s   sha256:s   sha512:s   invalid fingerprint for %s: %ss0   must begin with "sha1:", "sha256:", or "sha512:"r#      :r   r-   r   s   hostfingerprints   sha1r   r   r   r      devels   disableloaddefaultcertss   %s:verifycertsfiless   (hostsecurity.%s:verifycertsfile ignored when host fingerprints defined; using host fingerprints for verification)
s'   path specified by %s does not exist: %ss   hostsecurity.%s:verifycertsfiler   s   webs   cacertss   could not find web.cacerts: %ss     (try installing the %s package)s   ca-certificatess   using %s for CA file
)r	   bytesurlsupportedprotocolsconfiginsecureconnections
configlist
startswithr   r&   r   splitreplacelowerappendssl	CERT_NONE
configboolwarnr
   
expandpathospathexists_defaultcacertsdebugCERT_REQUIRED)uihostname	bhostnamesr,   defaultminimumprotocolr*   minimumprotocolciphersfingerprintsfingerprintalgcafiles                r+   _hostsettingsrR   =   s   
 !(++I #' $)# !'	 	A.   ++++&
Cii6LMMO_c***
)
+CiioFFO_c***ii44Gii)BGLLG
 
 -# 	-,G+AAjM ==+i7 L $ : :&&'IJJ 	+344	;7OOJKK   
 ',,T155[!))$44::<< 	

%%sK&89999 }}%8)DD ' '!))$44::<< 	

%%w&<==="&
 		 ,=-&+
"## 
	 ,(,
$%=-&+
"#	}}X9:: ,&+
"#
 YY(=	(IJJF	 
& 

# 
 	
 	
 	
 	 	"_V,,F7>>&)) k@AA:i\I   "AiLL YYvz22F A00w~~f-- +;<<vE=>>ASST  
 +, A(,, AHH6?@@@!AiL  	-Q/0 	-"0Am  #}Am]'''Hr-   c                 b   | t           vrt          d| z            t          j        t          j        z  }| dk    rnZ| dk    r|t          j        z  }nD| dk    r|t          j        t          j        z  z  }n!t          j        t          d                    |t          t          dd          z  }|S )z8Return SSLContext options common to servers and clients.s    protocol value not supported: %sr   r   r      this should not happenOP_NO_COMPRESSIONr   )r%   
ValueErrorr<   OP_NO_SSLv2OP_NO_SSLv3OP_NO_TLSv1OP_NO_TLSv1_1r   r&   r   r   )rL   optionss     r+   commonssloptionsr\      s    o--<NOOO o/G)##	I	%	%3?"	I	%	%3?S%666k!566777 ws/333GNr-   c           
         |s!t          j        t          d                    dt          j        v rw	 ddl}|                    t          j        t          j        d                              	                    d           n%# t          $ r 	                    d           Y nw xY wfD ]j}|rft          j                            |          sGt          j        t          d          |t          j        |          fz  t          d          	          kt          |          }t!          j        t$          d
          rOt%          j        t$          j                  }|d         }	|	dk    r]t+          j                    5  t+          j        ddt0                     t$          j        j        |_        ddd           n# 1 swxY w Y   n|	dk    r]t+          j                    5  t+          j        ddt0                     t$          j        j        |_        ddd           n# 1 swxY w Y   n>|	dk    rt$          j        j        |_        n!t          j        t          d                    |xj        t?          t$          dd          z  c_        nAt%          j        t$          j                   }|xj        tC          |d                   z  c_        d|_"        |d         |_#        |d         r	 |$                    t          j%        |d                              np# t$          j&        $ r^}
t          j        t          d          tO          j(        |
j)        d                   z  t          d          |d         z  	          d}
~
ww xY wfd}|*                    |           |d         	 |+                    |d                    n# t$          j&        $ r}
tY          |
j)                  dk    r|
j)        d         }n|
j)        d         }t          j        t          d          |d         tO          j(        |          fz  t          d          	          d}
~
ww xY wd}n!|d          r|-                                 d}nd}	 |.                    | |!          }n # t$          j&        $ r}
	 |rL|d         t$          j/        k    r6|0                                s"1                    t          d"                     n# t$          j&        $ r Y nw xY wt!          j        |
d#          r|
j2        d$v rI|d         dk    rtf          dhk    r`1                    t          d%          t          j        |          d&4                    tk          tf                              fz             n
1                    t          d'          t          j        |          z             n҉1                    t          d(          |d         t          j        |          fz             1                    t          d)          t          j        |          z             1                    t          d*                     n9|
j2        d+k    r.t          j6        r"1                    t          d,                      d}
~
ww xY w|7                                s!t          j8        t          d-                    |||d.|_9        |S )/a  Add SSL/TLS to a socket.

    This is a glorified wrapper for ``ssl.wrap_socket()``. It makes sane
    choices based on what security options are available.

    In addition to the arguments supported by ``ssl.wrap_socket``, we allow
    the following additional arguments:

    * serverhostname - The expected hostname of the remote server. If the
      server (and client) support SNI, this tells the server which certificate
      to use.
    s#   serverhostname argument is requireds   SSLKEYLOGFILEr   Ns8   sslkeylog enabled by SSLKEYLOGFILE environment variable
s?   sslkeylog module missing, but SSLKEYLOGFILE set in environment
s:   certificate file (%s) does not exist; cannot connect to %ss:   restore missing file or fix references in Mercurial configr#   
TLSVersionr   r   ignore"ssl.TLSVersion.TLSv1 is deprecatedr   $ssl.TLSVersion.TLSv1_1 is deprecatedr   rT   rU   Fr   r    s   could not set ciphers: %ss#   change cipher string (%s) in configc                  X    p}                      t          d          | z  d          S )Ns   passphrase for %s: r-   )getpassr   )fcertfilekeyfilerG   s    r+   passwordzwrapsocket.<locals>.passwordo  s/    #8A::a 677!;SAAAr-   r   rQ   r   s   error loading CA file %s: %ss   file is empty or malformed?Tr   )server_hostnames   (an attempt was made to load CA certificates but none were loaded; see https://mercurial-scm.org/wiki/SecureConnections for how to configure Mercurial to avoid this error)
s   reason)UNSUPPORTED_PROTOCOLTLSV1_ALERT_PROTOCOL_VERSIONs   (could not communicate with %s using security protocols %s; if you are using a modern Mercurial version, consider contacting the operator of this server; see https://mercurial-scm.org/wiki/SecureConnections for more info)
   , s   (could not communicate with %s using TLS 1.0; the likely cause of this is the server no longer supports TLS 1.0 because it has known security vulnerabilities; see https://mercurial-scm.org/wiki/SecureConnections for more info)
s   (could not negotiate a common security protocol (%s+) with %s; the likely cause is Mercurial is configured to be more secure than the server can support)
s   (consider contacting the operator of this server and ask them to support modern TLS protocol versions; or, set hostsecurity.%s:minimumprotocol=tls1.0 to allow use of legacy, less secure protocols when communicating with this server)
sE   (see https://mercurial-scm.org/wiki/SecureConnections for more info)
CERTIFICATE_VERIFY_FAILEDsR   (the full certificate chain may not be available locally; see "hg help debugssl")
s   ssl connection failed)   caloaded   hostname   settings   ui):r   r&   r   r   environ	sslkeylog
set_keylogr	   fsdecode
warnnoi18nImportErrorrA   rB   rC   r2   rR   r
   safehasattrr<   
SSLContextPROTOCOL_TLS_CLIENTwarningscatch_warningsfilterwarningsDeprecationWarningr^   TLSv1minimum_versionTLSv1_1TLSv1_2r[   r   PROTOCOL_SSLv23r\   check_hostnameverify_modeset_cipherssysstrSSLErrorr   forcebytestrargsload_cert_chainload_verify_locationslenload_default_certswrap_socketrF   get_ca_certsr?   reasonr3   r'   r(   	iswindowscipherSecurityError_hgstate)sockrf   re   rG   serverhostnamers   rd   settings
sslcontextrL   erg   msgcaloaded	sslsockets    ```           r+   
wrapsocketr     s     Ek!BCCDDD8+++	  !("23C"DEE   MML     	 	 	MM:    	 x  	 	 	RW^^A&& 	+OPPh'7789+     R00H \**  M^C$;<<
"#56i''(** B B'8&  
 .1^-A
*B B B B B B B B B B B B B B B 	))(** D D':&  
 .1^-C
*D D D D D D D D D D D D D D D 	))),)?J&&+a 9::;;; 	gc+>BBB
 ^C$788
.x8J/KLLL !&J%m4J
 		""8?8J3G#H#HIIII| 	 	 	+.//)!&)445=>>:&'   	 	B 	B 	B 	B 	B 	B 	B 	""8Wh???	&	,,HY4G,HHHH| 		 		 		16{{afQifQi+122I&
(?(D(DEF566   		 	*	+ %%'''s**4*PP		< q q q	]+s/@@@"//11 A $    | 	 	 	D	 Ay)) U	 x   
 ./9<< *i[88!4  !) 1. A A %

62D+E+E F F    !4  '/??@
 
 
 
  GGP  %%78$-n==
 
 
 GGA  #+N;;<
 
 
 GG0     888X=O8>    	cql  ?!!$<"="=>>> #	 I s   AB B)(B)2GG
G(2H&&H*-H*3-L! !N0AN		N:O Q+&B Q&&Q+R, ,[	=AT[T[TF&[[	c                 	   |||fD ]G}|rCt           j                            |          s$t          j        t          d          |z            Ht          j        t          d          r&t          j	        t          j
                  }|xj        t          t          dd          z  c_        |                    dd          }|dk    rdt          vr!t          j        t          d                    t          j                    5  t          j        d	d
t$                     t          j        j        |_        t          j        j        |_        ddd           n# 1 swxY w Y   n\|dk    rdt          vr!t          j        t          d                    t          j                    5  t          j        d	dt$                     t          j        j        |_        t          j        j        |_        ddd           n# 1 swxY w Y   n|dk    rXdt          vr!t          j        t          d                    t          j        j        |_        t          j        j        |_        nZ|r$t          j        t          d          |z            n2t          j        }	t5          d          }
|                    dd          }|dk    r7dt          vr!t          j        t          d                    t          j        }	n|dk    r7dt          vr!t          j        t          d                    t          j        }	nc|dk    r7dt          vr!t          j        t          d                    t          j        }	n&|r$t          j        t          d          |z            t          j	        |	          }|xj        |
z  c_        |xj        t          t          dd          z  c_        |xj        t          t          dd          z  c_        |r|                    d           n]t          j        t          d          rC|xj        t          t          dd          z  c_        |                    t          j                   |rt          j         |_!        nt          j"        |_!        |s|r|#                    ||           |r|$                    |           |%                    | d          S )a  Wrap a socket for use by servers.

    ``certfile`` and ``keyfile`` specify the files containing the certificate's
    public and private keys, respectively. Both keys can be defined in the same
    file via ``certfile`` (the private key must come first in the file).

    ``cafile`` defines the path to certificate authorities.

    ``requireclientcert`` specifies whether to require client certificates.

    Typically ``cafile`` is only defined if ``requireclientcert`` is true.
    s/   referenced certificate file (%s) does not existr^   rU   r   r1   s   serverexactprotocolr   s$   TLS 1.0 not supported by this Pythonr_   r`   Nr   s$   TLS 1.1 not supported by this Pythonra   r   s$   TLS 1.2 not supported by this Pythons)   invalid value for serverexactprotocol: %sOP_SINGLE_DH_USEOP_SINGLE_ECDH_USEzDEFAULT:@SECLEVEL=0s   _RESTRICTED_SERVER_CIPHERSOP_CIPHER_SERVER_PREFERENCE)re   rf   rh   T)server_side)&rA   rB   rC   r   r&   r   r
   rx   r<   ry   PROTOCOL_TLS_SERVERr[   r   r4   r3   r{   r|   r}   r~   r^   r   r   maximum_versionr   r   r   r\   r   r   r   r   _RESTRICTED_SERVER_CIPHERSrF   r   r=   r   r   r   )r   rG   re   rf   rQ   requireclientcertrd   r   exactprotocolr)   r[   s              r+   wrapserversocketr     sf   " (   	RW^^A&& 	+DEEI   \** F&^C$;<<
gc+>BBB 		(,BCCI%% 222k!$K"L"LMMM(** B B'8&  
 .1^-A
*-0^-A
*B B B B B B B B B B B B B B B i'' 222k!$K"L"LMMM(** D D':&  
 .1^-C
*-0^-C
*D D D D D D D D D D D D D D D i'' 222k!$K"L"LMMM),)?J&),)?J&& 	+>??-O  	 &"9-- 		(,BCCI%% 222k!$K"L"LMMM)HHi'' 222k!$K"L"LMMM+HHi'' 222k!$K"L"LMMM+HH 	+>??-O   ^H--
g% '#'91==='#';Q???  ?45555		#<	=	= ?gc+H!LLLs=>>>  /!$!2
!$
 G7 G""Hg"FFF 8(((777!!$D!999s&   AEEE(AG<<H H c                       e Zd ZdZdS )wildcarderrorz2Represents an error parsing wildcards in DNS name.N)__name__
__module____qualname____doc__ r-   r+   r   r     s        <<<<r-   r   c                    g }| sdS t          j        |           } t          j        |          }|                     d          }|d         }|dd         }|                    d          }||k    rt	          t          d          | z            |s*|                                 |                                k    S |dk    r|                    d           n|                    d	          s|                    d	          r(|                    t          j
        |                     n;|                    t          j
        |                              d
d                     |D ])}|                    t          j
        |                     *t          j        dd                    |          z   dz   t          j                  }	|	                    |          duS )zMatch DNS names according RFC 6125 section 6.4.3.

    This code is effectively copied from CPython's ssl._dnsname_match.

    Returns a bool indicating whether the expected hostname matches
    the value in ``dn``.
    F   .r   r   N   *s.   too many wildcards in certificate DNS name: %ss   [^.]+s   xn--s   \*s   [^.]*s   \As   \.s   \Z)r	   r2   r8   countr   r   r:   r;   r7   r   reescaper9   recompiler'   
IGNORECASEmatch)
dnrH   maxwildcardspatspiecesleftmost	remainder	wildcardsfragpats
             r+   _dnsnamematchr     s    D u		2		B **HXXd^^FayHqrr
It$$I<?@@2E
 
 	

  .xxzzX^^----
 4 	H			W	%	% M)<)<W)E)E M
 	J'112222 	J'1199&(KKLLL  / /J'--....
*Vfkk$///&8"-
H
HC99Xd**r-   c                    | st          d          S g }|                     dg           }|D ]m\  }}|dk    rb	 t          ||          r dS n8# t          $ r+}t	          j        |j        d                   cY d}~c S d}~ww xY w|                    |           n|s|                     dg           D ]}|D ]\  }}|dk    r	 |                    d          }n## t          $ r t          d	          cY c c S w xY w	 t          ||          r  dS n:# t          $ r-}t	          j        |j        d                   cY d}~c c S d}~ww xY w|                    |           d
 |D             }t          |          dk    r%t          d          d                    |          z  S t          |          dk    rt          d          |d         z  S t          d          S )zVerify that cert (in socket.getpeercert() format) matches hostname.
    CRLs is not handled.

    Returns error message if any problems are found and None on success.
    s   no certificate receivedsubjectAltNameDNSNr   subject
commonNameasciis    IDN in certificate not supportedc                 6    g | ]}t          j        |          S r   )r	   r2   ).0ds     r+   
<listcomp>z_verifycert.<locals>.<listcomp>  s#    777!!$$777r-   r   s   certificate is for %srl   s4   no commonName or subjectAltName found in certificate)r   getr   r   r   r   r   r;   encodeUnicodeEncodeErrorr   r'   )certrH   dnsnamessanr*   valuer   subs           r+   _verifycertr     s     -+,,,H
((#R
(
(C # #
U%<<: 11 FF  : : :!.qvay9999999999: OOE""" +88Ir** 	+ 	+C! + +
U ,&&F %W 5 5- F F F !DEEEEEEEEEFB(99 #"FFF#( B B B)6qvayAAAAAAAAAAAAB OOE***!+$ 87h777H
8}}q)**UZZ-A-AAA	X!		)**Xa[88HIIIsM   A
BA<4B<BCC87C8<D
EE9EEc                  &   t           j        rt          j                    st           j        sdS t
          j                            t           j                                                  } | 	                    d          p| 	                    d          S )a@  return true if this seems to be a pure Apple Python that
    * is unfrozen and presumably has the whole mercurial module in the file
      system
    * presumably is an Apple Python that uses Apple OpenSSL which has patches
      for using system certificate store CAs in addition to the provided
      cacerts file
    Fs   /usr/bin/pythons,   /system/library/frameworks/python.framework/)
r	   isdarwinr   
mainfrozensysexecutablerA   rB   realpathr:   r7   )exes    r+   _plainapplepythonr     s     "$$ %
 u
'

81
2
2
8
8
:
:C>>,-- 72 2 r-   c                    	 ddl }|                                }t          j                            |          r)|                     d           t          j        |          S n# t          t          f$ r Y nw xY wt                      rut          j                            t          j                            t          j        t                              d          }t          j                            |          r|S dS )a  return path to default CA certificates or None.

    It is assumed this function is called when the returned certificates
    file will actually be used to validate connections. Therefore this
    function may print warnings or debug messages assuming this usage.

    We don't print a message when the Python is able to load default
    CA certs because this scenario is detected at socket connect time.
    r   Ns#   using ca certificates from certifi
s   dummycert.pem)certifiwhererA   rB   rC   rE   r	   fsencoderw   AttributeErrorr   r'   dirname__file__)rG   r   certs	dummycerts       r+   rD   rD   	  s    7>>%   	,HH<===$U+++	, (     GLLGOOH-h7788:J
 
	 7>>)$$ 	4s   AA# #A76A7c           	      P   | j         d         }t          j        |          }| j         d         }| j         d         }	 |                     d          }|                                 }n2# t          $ r% t          j        t          d          |z            w xY w|s$t          j        t          d          |z            |d         r'|                    t          d          |z             d	S t          t          j        |                                                    t          t          j        |                                                    t          t          j        |                                                    d
}d }d ||d                   z  }	|d         r|d         D ]z\  }
}||
                                         |k    rW|                    d||
 ||          fz             |d         r(|                    t          d          |||	fz              d	S {|d         rd} ||d                   }nd}d|
 |||
                   fz  }t          j        t          d          ||fz  t          d          |z            | j         d         s8t          j        t          d          |z  t          d          ||	fz            t%          ||          }|r:t          j        t          d          ||fz  t          d          ||	fz            d	S )zxValidate a socket meets security requirements.

    The passed socket must have been created with ``wrapsocket()``.
    ro   rq   rp   Ts   %s ssl connection errors-   %s certificate error: no certificate receivedr   s   warning: connection security to %s is disabled per current settings; communication is susceptible to eavesdropping and tampering
N)r0      sha256s   sha512c           	      ~     d                      fdt          dt                     d          D                       S )Nr/   c                 *    g | ]}||d z            S )   r   )r   xrJ   s     r+   r   z:validatesocket.<locals>.fmtfingerprint.<locals>.<listcomp>[  s%    DDD1!AAI,DDDr-   r   r   )r'   ranger   )rJ   s   `r+   fmtfingerprintz&validatesocket.<locals>.fmtfingerprintZ  s<    yyDDDDaQ0C0CDDDEEEr-   s	   sha256:%sr   r   s)   %s certificate matched fingerprint %s:%s
r   s   (SHA-1 fingerprint for %s found in legacy [hostfingerprints] section; if you trust this fingerprint, remove the old SHA-1 fingerprint from [hostfingerprints] and add the following entry to the new [hostsecurity] section: %s:fingerprints=%s)
s   hostfingerprintr0   r.   s   %s:%ss0   certificate for %s has unexpected fingerprint %ss   check %s configurationr#   rn   sP   unable to verify security of %s (no loaded CA certificates); refusing to connects   see https://mercurial-scm.org/wiki/SecureConnections for how to configure Mercurial to avoid this error or set hostsecurity.%s:fingerprints=%s to trust this servers   %s certificate error: %ss^   set hostsecurity.%s:certfingerprints=%s config setting or use --insecure to connect insecurely)r   r	   r2   getpeercertr   r   r   r   r?   r   r   sha1digesthashlibsha256sha512r:   rE   r   )r   shosthostrG   r   peercert	peercert2peerfingerprintsr   nicefingerprinthashrO   sectionnicer   s                  r+   validatesocketr   ,  s   
 M+&EU##D	u	B}[)HH##D))$$&&		 H H H!!$>"?"?$"FGGGH  
!>??$F
 
 	
 *+  	# 
 	
 	
 	
 	
 X]8,,335566w~h//668899w~h//668899 F F F #^^4DY4O%P%PPO#$  
!)*=!> 	 	D+%++--<<AT>>+#>#>?@   01 GGM   78
 
 
 # =( () 	M(G!>"27";<<DD%Gt^^4DT4J%K%KLLD!ABBTl,--7
 
 
 	
 =% 
!'  	
 H 
 _%&
 
 
 	
 i
'
'C
 	
!)**dC[8 
 _%&
 
 
 	
	
 	
s   )A' '/B)N)NNNF)r   )$r   rA   r   r<   r{   i18nr   r	   r   noder    r   r   r
   utilsr   r   r   r%   hassnisetr3   rx   addrR   r\   r   r   	Exceptionr   r   r   r   rD   r   r   r-   r+   <module>r     s    				 				 



                                          
i	'	' SUU 
73-T-c3CDDEE &9%%%
73/t/5GHHII &9%%%
73/t/5GHHII &9%%%n n nb  2E E E ER KPx: x: x: x:v= = = = =I = = =1+ 1+ 1+ 1+h0J 0J 0Jf  (     Fp
 p
 p
 p
 p
r-   