
    6h                    $   S SK Jr  S SKrS SKrS SKrS SKrS SKrSSKJr  SSK	J
r
  SSKJrJrJrJr  SSKJrJrJr  1 Skr\" \S	S
5      r\" 5       r\" \S\" \S5      5      (       a  \R5                  S5        \" \S\" \S5      5      (       a  \R5                  S5        \" \S\" \S5      5      (       a  \R5                  S5        \" \SS
5      (       a  \R5                  S5        S rS S jr S!S jr " S S\5      rS"S jr S r!S r"S r#S r$g)#    )annotationsN   )_)hex)encodingerrorpycompatutil)hashutilresourceutil
stringutil>      tls1.0   tls1.1   tls1.2   tls1.3HAS_SNIF	HAS_TLSv1PROTOCOL_TLSv1r   HAS_TLSv1_1PROTOCOL_TLSv1_1r   HAS_TLSv1_2PROTOCOL_TLSv1_2r   HAS_TLSv1_3r   c           	        [         R                  " U5      nS/ SSSSSSS.nS n[        SS1-
  (       d   eSnS	nU R                  S
Xe5      nU" Xv5        SU-  nU R                  S
Xg5      nU" Xv5        U R                  S
S5      nU R                  S
SU-  U5      nU R                  (       a  SnU(       d  SnXsS	'   XS'   U R                  S
SU-  5      n	U	 H  n
U
R                  S5      (       d+  [        R                  " [        S5      X*4-  [        S5      S9eU
R                  SS5      u  pU
R                  SS5      R                  5       n
US   R                  X45        M     U R                  SU5       H>  n
U
R                  SS5      R                  5       n
US   R                  SU
45        SUS'   M@     US   (       a  [        R                  US'   SUS'   O.U R                  (       a  SUS'   [        R                  US'   SUS'   U R!                  SS5      (       a  SUS'   U R                  S
S U-  5      nUS   (       a$  U(       a  U R#                  [        S!5      U-  5        US   Gc_  U(       ag  [$        R&                  " U5      n[(        R*                  R-                  U5      (       d(  [        R                  " [        S"5      S#U4-  U4-  5      eXS$'   OU R                  S%S&5      nU(       al  [$        R&                  " U5      n[(        R*                  R-                  U5      (       d1  [        R                  " [        S'5      U-  [        S(5      S)-  -   5      eO0US   (       a&  [/        U 5      nU(       a  U R1                  S*U-  5        XS$'   U(       d
  US   (       a  [        R2                  US'   O[        R                  US'   US   c   eU$ )+z`Obtain security settings for a hostname.

Returns a dict of settings relevant to that hostname.
TNF)   allowloaddefaultcerts   certfingerprints   cafile   disablecertverification   legacyfingerprint   minimumprotocol
   verifymode   ciphersc           	         U [         ;  aJ  [        R                  " [        S5      X4-  [        S5      SR	                  [        [         5      5      -  S9eg )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validateprotocol'_hostsettings.<locals>.validateprotocol]   sR    ?*++BC/"-.))F?345  +    r   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   r0   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-   _hostsettingsrU   @   s   
 !!(+I #' $)# !'	A. I 6666&
CiiMO_*
)
+CiiFO_*ii4Gii)BGLG
 
#,G+jM ==+i7L $&&'IJJ++34	7OOJK 
 ',,T15!))$4::< 	

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

%%w&<="&
 E 		==-&+
"# 
		(,
$%==-&+
"#	}}X9::&+
"#
 YY(=	(IJF	&
#
 	
 	__V,F77>>&))kk@A:i\I  "iL YYvz2F0ww~~f--++;<vE=>ASST  .
 +,(,HH6?@!iL Q/0"00Am  #}}Am]'''Hr0   c           
       ^^^ U(       d  [         R                  " [        S5      5      eS[        R                  ;   aL   SSKnUR                  [        R                  " [        R                  S   5      5        TR                  S5        TT4 Hp  nU(       d  M  [        R                  R                  U5      (       a  M2  [         R                  " [        S5      U[        R                  " U5      4-  [        S5      S	9e   [        TU5      n[         R"                  " [         R$                  5      nUS
   n	U	S:X  aZ  [&        R(                  " 5          [&        R*                  " SS[,        5        [         R.                  R0                  Ul        SSS5        OU	S:X  aZ  [&        R(                  " 5          [&        R*                  " SS[,        5        [         R.                  R4                  Ul        SSS5        OkU	S:X  a   [         R.                  R6                  Ul        OEU	S:X  a   [         R.                  R8                  Ul        O[         R                  " [        S5      5      eU=R:                  [=        [         SS5      -  sl        SUl        US   Ul         US   (       a)   URC                  [        RD                  " US   5      5        Tb  UUU4S jnURO                  TTU5        US   b   URQ                  US   S9  SnOUS    (       a  URU                  5         SnOSn URW                  XS!9nURk                  5       (       d  [         Rl                  " [        S-5      5      eUUUTS..Ul7        U$ ! [         a    TR                  S5         GN	f = f! , (       d  f       GND= f! , (       d  f       GNV= f! [         RF                   aV  n
[         R                  " [        S5      [H        RJ                  " U
RL                  S   5      -  [        S5      US   -  S	9eSn
A
ff = f! [         RF                   a  n
[S        U
RL                  5      S:X  a  U
RL                  S   nOU
RL                  S   n[         R                  " [        S5      US   [H        RJ                  " U5      4-  [        S5      S	9eSn
A
ff = f! [         RF                   Ga  n
 U(       aF  US   [         RX                  :X  a/  UR[                  5       (       d  TR]                  [        S"5      5        O! [         RF                   a     Of = f[_        U
S#5      (       Gaj  U
R`                  S$;   Ga  US
   S:X  a  [b        S1:w  aP  TR]                  [        S%5      [        R                  " U5      S&Re                  [g        [b        5      5      4-  5        e TR]                  [        S'5      [        R                  " U5      -  5         e TR]                  [        S(5      US
   [        R                  " U5      4-  5        TR]                  [        S)5      [        R                  " U5      -  5        TR]                  [        S*5      5         e U
R`                  S+:X  a/  [        Rh                  (       a  TR]                  [        S,5      5        e Sn
A
ff = f)/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%   r    r   ignore"ssl.TLSVersion.TLSv1 is deprecatedr   $ssl.TLSVersion.TLSv1_1 is deprecatedr   r   s   this should not happenOP_NO_COMPRESSIONFr!   r"   s   could not set ciphers: %ss#   change cipher string (%s) in configc                 V   > T=(       d    Tn TR                  [        S5      U -  S5      $ )Ns   passphrase for %s: r0   )getpassr   )fcertfilekeyfilerJ   s    r-   passwordwrapsocket.<locals>.passwordS  s)    #8A::a 67!;SAAr0   r   rT   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)
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)8r   r(   r   r   environ	sslkeylog
set_keylogr	   fsdecode
warnnoi18nImportErrorrD   rE   rF   r5   rU   r?   
SSLContextPROTOCOL_TLS_CLIENTwarningscatch_warningsfilterwarningsDeprecationWarning
TLSVersionTLSv1minimum_versionTLSv1_1TLSv1_2TLSv1_3optionsgetattrcheck_hostnameverify_modeset_cipherssysstrSSLErrorr   forcebytestrargsload_cert_chainload_verify_locationslenload_default_certswrap_socketrI   get_ca_certsrB   hasattrrd   r6   r)   r*   	iswindowscipherSecurityError_hgstate)sockr_   r^   rJ   serverhostnamern   r]   settings
sslcontextrO   er`   msgcaloaded	sslsockets    ```           r-   
wrapsocketr      s    kk!BCDD8+++	  !!("2"23C"DE MML x 1RWW^^A&&++OPh''789+  ! R0H  7 78J12O)#$$&##4"
 *-)=)=J& '& 
I	%$$&##6"
 *-)?)?J& '& 
I	%%(^^%;%;
"	I	%%(^^%;%;
"kk!5677 '#':A>> !&J%m4J
	""8??8J3G#HI 	B 	""8Wh?	&	,,HY4G,H 	*	+%%'r**4*P	l !!!$<"=>> #	I I  	MM:	< '& '&0 || 	++./))!&&)45=>:&' 	& || 		166{affQiffQi++12I&
(?(?(DEF56 		( << p	]+s/@/@@"//11$ || 		 1h xx  
 ./9< *i[8!4 !) 1 1. A %

62D+E FF 	i !4 '//?@
h 	I GGP %%78$--n=
 GGA #++N;<
 GG0 	 88X=O=O> 	aps   AM5 <N2<N)"(N; /P( $R? 5NN
N&)
N8;P%AP  P%(R<<A;R77R<?Z?AT#"Z:#T:7Z:9T::F Z::Z?c                   X#U4 HR  nU(       d  M  [         R                  R                  U5      (       a  M2  [        R                  " [        S5      U-  5      e   [        R                  " [        R                  5      nU=R                  [        [        SS5      -  sl	        UR                  SS5      nUS:X  a  S[        ;  a  [        R                  " [        S5      5      e[        R                  " 5          [        R                  " SS	[         5        [        R"                  R$                  Ul        [        R"                  R$                  Ul        S
S
S
5        GOUS:X  a  S[        ;  a  [        R                  " [        S5      5      e[        R                  " 5          [        R                  " SS[         5        [        R"                  R*                  Ul        [        R"                  R*                  Ul        S
S
S
5        GOUS:X  ah  S[        ;  a  [        R                  " [        S5      5      e[        R"                  R,                  Ul        [        R"                  R,                  Ul        OUS:X  ah  S[        ;  a  [        R                  " [        S5      5      e[        R"                  R.                  Ul        [        R"                  R.                  Ul        O)U(       a"  [        R                  " [        S5      U-  5      eU=R                  [        [        SS5      -  sl	        U=R                  [        [        SS5      -  sl	        U(       a  UR1                  S5        OX[3        [        S5      (       aC  U=R                  [        [        SS5      -  sl	        UR1                  [        R4                  5        U(       a  [        R6                  Ul        O[        R:                  Ul        U(       d  U(       a  UR=                  X#S9  U(       a  UR?                  US9  URA                  U SS9$ ! , (       d  f       GN== f! , (       d  f       GNO= f)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 existrZ   r   r4   s   server-insecure-exact-protocolr   s$   TLS 1.0 not supported by this PythonrW   rX   Nr   s$   TLS 1.1 not supported by this PythonrY   r   s$   TLS 1.2 not supported by this Pythonr   s$   TLS 1.3 not supported by this Pythons4   invalid value for server-insecure-exact-protocol: %sOP_SINGLE_DH_USEOP_SINGLE_ECDH_USEzDEFAULT:@SECLEVEL=0_RESTRICTED_SERVER_CIPHERSOP_CIPHER_SERVER_PREFERENCE)r^   r_   rb   T)server_side)!rD   rE   rF   r   r(   r   r?   rs   PROTOCOL_TLS_SERVERr   r   r7   r6   ru   rv   rw   rx   ry   rz   r{   maximum_versionr|   r}   r~   r   r   r   rI   r   r@   r   r   r   )	r   rJ   r^   r_   rT   requireclientcertr]   r   exactprotocols	            r-   wrapserversocketr     s3   " (1RWW^^A&&++DEI  )  7 78J'#':A>> IIh(IJM	!..++a GHII$$&##4"
 *-)=)=J&),)=)=J& '& 
)	#..++a GHII$$&##6"
 *-)?)?J&),)?)?J& '& 
)	#..++a GHII%(^^%;%;
"%(^^%;%;
"	)	#..++a GHII%(^^%;%;
"%(^^%;%;
"	kkEF
 	
 '#'91=='#';Q?? 45	2	3	3gc+H!LLs==> !$!2!2
!$
7""H"F(((7!!$D!99y '& '&s   9AP:"AQ:
Q	
Qc                      \ rS rSrSrSrg)wildcarderroriQ  z2Represents an error parsing wildcards in DNS name. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r   r0   r-   r   r   Q  s    <r0   r   c                   / nU (       d  g[         R                  " U 5      n [         R                  " U5      nU R                  S5      nUS   nUSS nUR                  S5      nXr:  a  [	        [        S5      U -  5      eU(       d!  U R                  5       UR                  5       :H  $ US:X  a  UR                  S5        OUR                  S	5      (       d  UR                  S	5      (       a&  UR                  [        R                  " U5      5        O5UR                  [        R                  " U5      R                  S
S5      5        U H(  nUR                  [        R                  " U5      5        M*     [        R                  " SSR                  U5      -   S-   [        R                  5      n	U	R!                  U5      SL$ )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	   r5   r;   countr   r   r=   r>   r:   r   reescaper<   recompiler)   
IGNORECASEmatch)
dnrK   maxwildcardspatspiecesleftmost	remainder	wildcardsfragpats
             r-   _dnsnamematchr   U  st    D			2	B  *HXXd^FayHqr
It$I?@2E
 	

 xxzX^^---
 4 	H			W	%	%)<)<W)E)E
 	J''12 	J''199&(KL J''-.  **Vfkk$//&8"--
HC99Xd**r0   c                   U (       d  [        S5      $ / nU R                  S/ 5      nU H2  u  pEUS:X  d  M   [        XQ5      (       a    g UR                  U5        M4     U(       dd  U R                  S/ 5       HN  nU HE  u  pEUS:X  d  M   UR                  S5      n [        XQ5      (       a      g UR                  U5        MG     MP     U Vs/ s H  n[        R                  " U5      PM     nn[        U5      S
:  a  [        S5      SR                  U5      -  $ [        U5      S
:X  a  [        S5      US   -  $ [        S5      $ ! [         a/  n[        R
                  " UR                  S   5      s SnAs  $ SnAff = f! [         a    [        S	5      s s  s  $ f = f! [         a1  n[        R
                  " UR                  S   5      s SnAs  s  $ SnAff = fs  snf )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 supportedr   s   certificate is for %srg   s4   no commonName or subjectAltName found in certificate)r   getr   r   r   r   r   r>   encodeUnicodeEncodeErrorr	   r5   r   r)   )	certrK   dnsnamessanr,   valuer   subds	            r-   _verifycertr     s    +,,H
((#R
(C
%<: 11 2
 OOE"  88Ir*C!
 ,&F %W 5B(99" :
 OOE*! " +& /77h!!!$hH7
8}q)*UZZ-AAA	X!	)*Xa[88HIIA ! :!..qvvay99: . F !DEEF ) B)66qvvayAAAB
 8sS   EFF" G 
F "E;3F ;F FF"
G,"GGGc                 T   [         R                  (       a/  [        R                  " 5       (       d  [         R                  (       d  g[
        R                  R                  [         R                  5      R                  5       n U R                  S5      =(       d    U R                  S5      $ )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sysexecutablerD   rE   realpathr=   r:   )exes    r-   _plainapplepythonr     sp     ""$$%%
''

811
2
8
8
:C>>,- 72 r0   c                    SSK nUR                  5       n[        R                  R	                  U5      (       a'  U R                  S5        [        R                  " U5      $  [        5       (       a{  [        R                  R                  [        R                  R                  [        R                  " [        5      5      S5      n[        R                  R	                  U5      (       a  U$ g! [        [        4 a     Nf = f)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whererD   rE   rF   rH   r	   fsencoderr   AttributeErrorr   r)   dirname__file__)rJ   r   certs	dummycerts       r-   rG   rG     s    77>>%  HH<=$$U++ ! GGLLGGOOH--h78:J
	 77>>)$$ ( s   AC- -D ?D c           	        U R                   S   n[        R                  " U5      nU R                   S   nU R                   S   n U R                  S5      nU R                  5       nU(       d"  [
        R                  " [        S5      U-  5      eUS   (       a  UR                  [        S5      U-  5        g	[        [        R                  " U5      R                  5       5      [        [        R                  " U5      R                  5       5      [        [        R                  " U5      R                  5       5      S
.nS nSU" US   5      -  n	US   (       a  US    Hb  u  pXz   R!                  5       U:X  d  M  UR#                  SX*U" U5      4-  5        US   (       a  UR                  [        S5      X"U	4-  5          g	   US   (       a  SnU" US   5      nOSnSW
U" Xz   5      4-  n[
        R                  " [        S5      X-4-  [        S5      U-  S9eU R                   S   (       d.  [
        R                  " [        S5      U-  [        S5      X)4-  S9e[%        Xa5      nU(       a/  [
        R                  " [        S5      X.4-  [        S5      X)4-  S9eg	! [         a#    [
        R                  " [        S5      U-  5      ef = f)zpValidate a socket meets security requirements.

The passed socket must have been created with ``wrapsocket()``.
rj   rl   rk   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)r3      sha256s   sha512c           	         SR                  [        S[        U 5      S5       Vs/ s H	  oXS-    PM     sn5      $ s  snf )Nr2   r      )r)   ranger   )rM   xs     r-   fmtfingerprint&validatesocket.<locals>.fmtfingerprint!  s8    yyaQ0CD0C1AA,0CDEEDs   ;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   hostfingerprintr3   r1   s   %s:%ss0   certificate for %s has unexpected fingerprint %ss   check %s configurationr%   ri   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	   r5   getpeercertr   r   r   r   rB   r   r   sha1digesthashlibsha256sha512r=   rH   r   )r   shosthostrJ   r   peercert	peercert2peerfingerprintsr   nicefingerprinthashrR   sectionnicer   s                  r-   validatesocketr     s   
 MM+&EU#D	u	B}}[)HH##D)$$&	 !!>?$F
 	
 *+ 	#
 	
 	
 X]]8,3356w~~h/6689w~~h/6689F #^4DY4O%PPO#$!)*=!>D%++-<A>+#>?@ 01GGM  78
 % "?* ()(G!"27";<D%Gt^4D4J%KLLD!!ABl,-7
 	
 ==%!!' 	
 H
 %&
 	
 i
'C
!!)*d[8
 %&
 	
 u  H!!!$>"?$"FGGHs   !J -K)N)NNNF)r   )%
__future__r   r   rD   r   r?   ru   i18nr   noder    r   r   r	   r
   utilsr   r   r   r'   r   hassnisetr6   r   addrU   r   r   	Exceptionr   r   r   r   rG   r   r   r0   r-   <module>r     s&   #  	 	 
      
i	' U 
3WS*:;<<9%
3ws,>?@@9%
3ws,>?@@9%
3u%%9%nb~D KP\:~=I =1+h0Jf( Fp
r0   