
    U-fF                     ^   d Z ddlZddlZ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 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlZddlmZ ddlmZ ddlmZ  ej        e          Zej        Z G d d          Z G d d          Zddeddfde de de!de!de!dee"e!f         deee                   dej#        fdZ$	 	 	 d7d e d!eeee"         ee"         f                  d"e%d#eeeej&        ej'        f                           de f
d$Z(d%eej#        ej)        f         dee"         fd&Z*d'eej#        ej)        f         dee"         fd(Z+d'eej#        ej)        f         dee"         fd)Z,d'eej#        ej)        f         dee"         fd*Z-	 	 	 	 	 d8d-ej.        d!eee"                  d.ee!         d/e!d0e%d1eeej/                          d2eeeej&        ej'        f                           dej#        fd3Z0ej1        fd4eeej2                 eej#                 f         d5e!de fd6Z3dS )9zCrypto utilities.    N)Any)Callable)List)Mapping)Optional)Sequence)Set)Tuple)Union)crypto)SSL)errorsc                       e Zd Zdeeeej        ej        f         f         fdZ	de
j        deeej        ej        f                  fdZdS )_DefaultCertSelectioncertsc                     || _         d S N)r   )selfr   s     _/home/cdr/domains/dharristours.com/map/certbot/lib/python3.11/site-packages/acme/crypto_util.py__init__z_DefaultCertSelection.__init__&   s    


    
connectionreturnc                 h    |                                 }|r| j                            |d           S d S r   )get_servernamer   get)r   r   server_names      r   __call__z_DefaultCertSelection.__call__)   s6     //11 	5:>>+t444tr   N)__name__
__module____qualname__r   bytesr
   r   PKeyX509r   r   
Connectionr   r    r   r   r   r   %   s        geU6;3K-L&LM    3> huV[RXR]E]?^6_      r   r   c                   t   e Zd ZdZdeddfdej        deeee	e
j        e
j        f         f                  dedeeej        ee         gef                  deeej        gee	e
j        e
j        f                  f                  ddfd	Zd
edefdZdej        ddfdZ G d d          Zde	eef         fdZdS )	SSLSocketa  SSL wrapper for sockets.

    :ivar socket sock: Original wrapped socket.
    :ivar dict certs: Mapping from domain names (`bytes`) to
        `OpenSSL.crypto.X509`.
    :ivar method: See `OpenSSL.SSL.Context` for allowed values.
    :ivar alpn_selection: Hook to select negotiated ALPN protocol for
        connection.
    :ivar cert_selection: Hook to select certificate for connection. If given,
        `certs` parameter would be ignored, and therefore must be empty.

    Nsockr   methodalpn_selectioncert_selectionr   c                     || _         || _        || _        |s|st          d          |r|rt          d          |}|t	          |r|ni           }|| _        d S )Nz*Neither cert_selection or certs specified.z(Both cert_selection and certs specified.)r)   r+   r*   
ValueErrorr   r,   )r   r)   r   r*   r+   r,   actual_cert_selections          r   r   zSSLSocket.__init__=   s     	, 	Ke 	KIJJJ 	Ie 	IGHHH Tb 	 !($95:P%%b$Q$Q!3r   namec                 ,    t          | j        |          S r   )getattrr)   r   r0   s     r   __getattr__zSSLSocket.__getattr__T   s    ty$'''r   r   c                 
   |                      |          }|/t                              d|                                           dS |\  }}t	          j        | j                  }|                    t          j                   |                    t          j	                   |
                    |           |                    |           | j        |                    | j                   |                    |           dS )a  SNI certificate callback.

        This method will set a new OpenSSL context object for this
        connection when an incoming connection provides an SNI name
        (in order to serve the appropriate certificate, if any).

        :param connection: The TLS connection object on which the SNI
            extension was received.
        :type connection: :class:`OpenSSL.Connection`

        Nz=Certificate selection for server name %s failed, dropping SSL)r,   loggerdebugr   r   Contextr*   set_optionsOP_NO_SSLv2OP_NO_SSLv3use_privatekeyuse_certificater+   set_alpn_select_callbackset_context)r   r   pairkeycertnew_contexts         r   _pick_certificate_cbzSSLSocket._pick_certificate_cbW   s     "":..<LLX#22446 6 6F	Tk$+..000000""3'''##D)))*001DEEE{+++++r   c                   L    e Zd ZdZdej        ddfdZdedefdZ	dede
fd	ZdS )
SSLSocket.FakeConnectionzFake OpenSSL.SSL.Connection.r   r   Nc                     || _         d S r   )_wrapped)r   r   s     r   r   z!SSLSocket.FakeConnection.__init__w   s    &DMMMr   r0   c                 ,    t          | j        |          S r   )r2   rH   r3   s     r   r4   z$SSLSocket.FakeConnection.__getattr__z   s    4=$///r   unused_argsc                     	 | j                                         S # t          j        $ r}t	          j        |          d }~ww xY wr   )rH   shutdownr   Errorsocketerror)r   rJ   rO   s      r   rL   z!SSLSocket.FakeConnection.shutdown}   sN    *}--///9 * * *
 l5)))*s    A>A)r   r    r!   __doc__r   r%   r   strr   r4   boolrL   r&   r   r   FakeConnectionrF   r   s        **	's~ 	'$ 	' 	' 	' 	'	0C 	0C 	0 	0 	0 	0		* 		* 		* 		* 		* 		* 		* 		*r   rS   c                    | j                                         \  }}	 t          j        | j                  }|                    t          j                   |                    t          j                   |                    | j	                   | j
        |                    | j
                   |                     t          j        ||                    }|                                 t                              d|           	 |                                 n+# t          j        $ r}t'          j        |          d }~ww xY w||fS #  |                                  xY w)NzPerforming handshake with %s)r)   acceptr   r8   r*   r9   r:   r;   set_tlsext_servername_callbackrD   r+   r>   rS   r%   set_accept_stater6   r7   do_handshakerM   rN   rO   close)r   r)   addrcontextssl_sockrO   s         r   rU   zSSLSocket.accept   sN   Y%%''
d	k$+..G0000002243LMMM".001DEEE**3>'4+H+HIIH%%''' LL7>>>*%%''''9 * * * l5)))*
 T>!	 JJLLLs0   C)E D E E,E  EE E#)r   r    r!   rP   _DEFAULT_SSL_METHODrN   r   r   r"   r
   r   r#   r$   intr   r   r%   r   r   rQ   r   r4   rD   rS   rU   r&   r   r   r(   r(   0   s         UY2\` UY4 4V] 4 fk6;6N0O)O!PQ44 "*(CNDK3PRW3W*X!Y4 "*(CN3C3;E&+BH+CN =O 4P4P +Q "R	4 4 4 4 4.( ( ( ( ( (,s~ ,$ , , , ,6* * * * * * * *,nc12      r   r(   i  i,  ) r   r0   hostporttimeoutr*   source_addressalpn_protocolsr   c           
      `   t          j        |          }|                    |           d|i}	 t                              d||t          |          r"d                    |d         |d                   nd           ||f}	t          j        |	fi |}
n+# t          j	        $ r}t          j        |          d}~ww xY wt          j        |
          5 }t          j        ||          }|                                 |                    |            ||                    |           	 |                                 |                                 n+# t           j        $ r}t          j        |          d}~ww xY w	 ddd           n# 1 swxY w Y   |                                }|sJ |S )a	  Probe SNI server for SSL certificate.

    :param bytes name: Byte string to send as the server name in the
        client hello message.
    :param bytes host: Host to connect to.
    :param int port: Port to connect to.
    :param int timeout: Timeout in seconds.
    :param method: See `OpenSSL.SSL.Context` for allowed values.
    :param tuple source_address: Enables multi-path probing (selection
        of source interface). See `socket.creation_connection` for more
        info. Available only in Python 2.7+.
    :param alpn_protocols: Protocols to request using ALPN.
    :type alpn_protocols: `Sequence` of `bytes`

    :raises acme.errors.Error: In case of any problems.

    :returns: SSL certificate presented by the server.
    :rtype: OpenSSL.crypto.X509

    rc   z!Attempting to connect to %s:%d%s.z from {0}:{1}r      r_   N)r   r8   set_timeoutr6   r7   anyformatrN   create_connectionrO   r   rM   
contextlibclosingr%   set_connect_stateset_tlsext_host_nameset_alpn_protosrX   rL   get_peer_certificate)r0   r`   ra   rb   r*   rc   rd   r[   socket_kwargssocket_tupler)   rO   client
client_sslrB   s                  r   	probe_sniru      s    . k&!!G   %~6M"/t ^$$-O""q!q!   +-	
 	
 	
 ,0,'FFFF< " " "l5!!!" 
	D	!	! 
&V^GV44
$$&&&''---%&&~666	&##%%%!!!!y 	& 	& 	&,u%%%	& "
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& 
& **,,DKKKKsO   A$B B<#B77B<AF*(EFE;"E66E;;FFFFprivate_key_pemdomainsmust_stapleipaddrsc                    t          j        t           j        |           }t          j                    }g }|g }|g }t	          |          t	          |          z   dk    rt          d          |D ]}|                    d|z              |D ]}|                    d|j        z               d                    |          	                    d          }	t          j
        dd	|	
          g}
|r*|
                    t          j
        dd	d
                     |                    |
           |                    |           |                    d           |                    |d           t          j        t           j        |          S )a  Generate a CSR containing domains or IPs as subjectAltNames.

    :param buffer private_key_pem: Private key, in PEM PKCS#8 format.
    :param list domains: List of DNS names to include in subjectAltNames of CSR.
    :param bool must_staple: Whether to include the TLS Feature extension (aka
        OCSP Must Staple: https://tools.ietf.org/html/rfc7633).
    :param list ipaddrs: List of IPaddress(type ipaddress.IPv4Address or ipaddress.IPv6Address)
    names to include in subbjectAltNames of CSR.
    params ordered this way for backward competablity when called by positional argument.
    :returns: buffer PEM-encoded Certificate Signing Request.
    Nr   zAAt least one of domains or ipaddrs parameter need to be not emptyDNS:IP:, ascii   subjectAltNameFcriticalvalues   1.3.6.1.5.5.7.1.24s   DER:30:03:02:01:05sha256)r   load_privatekeyFILETYPE_PEMX509Reqlenr.   appendexplodedjoinencodeX509Extensionadd_extensions
set_pubkeyset_versionsigndump_certificate_request)rv   rw   rx   ry   private_keycsrsanlistaddressips
san_string
extensionss              r   make_csrr      s    (_. .K
.

CG
7||CLL A%%\]]] ) )v'(((( - -us|+,,,,7##**733J 		
 	
 	
J  *&.!') ) ) 	* 	* 	* z"""NN;OOAHH[(###*S" " "r   loaded_cert_or_reqc                     |                                  j        t          |           }|S gfd|D             z   S )Nc                      g | ]
}|k    |S r&   r&   ).0dcommon_names     r   
<listcomp>z4_pyopenssl_cert_or_req_all_names.<locals>.<listcomp>   s#    @@@!qK/?/?A/?/?/?r   )get_subjectCN_pyopenssl_cert_or_req_san)r   sansr   s     @r    _pyopenssl_cert_or_req_all_namesr     sT     %00225K%&899D=@@@@t@@@@@r   cert_or_reqc                 P    ddz   t          |           }fd|D             S )a  Get Subject Alternative Names from certificate or CSR using pyOpenSSL.

    .. todo:: Implement directly in PyOpenSSL!

    .. note:: Although this is `acme` internal API, it is used by
        `letsencrypt`.

    :param cert_or_req: Certificate or CSR.
    :type cert_or_req: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`.

    :returns: A list of Subject Alternative Names that is DNS.
    :rtype: `list` of `str`

    :DNSc                 p    g | ]2}|                               |                              d          3S )rf   )
startswithsplit)r   partpart_separatorprefixs     r   r   z._pyopenssl_cert_or_req_san.<locals>.<listcomp>:  sJ     ? ? ?doof&=&=?DJJ~&&q) ? ? ?r   _pyopenssl_extract_san_list_raw)r   
sans_partsr   r   s     @@r   r   r   #  sR    $ N^#F0==J? ? ? ? ?"? ? ? ?r   c                 L    d}d|z   t          |           }fd|D             S )ae  Get Subject Alternative Names IPs from certificate or CSR using pyOpenSSL.

    :param cert_or_req: Certificate or CSR.
    :type cert_or_req: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`.

    :returns: A list of Subject Alternative Names that are IP Addresses.
    :rtype: `list` of `str`. note that this returns as string, not IPaddress object

    r   z
IP Addressc                 h    g | ].}|                               |t                    d          /S r   )r   r   )r   r   r   s     r   r   z1_pyopenssl_cert_or_req_san_ip.<locals>.<listcomp>O  s9    QQQ49P9PQDVQQQr   r   )r   r   r   r   s      @r   _pyopenssl_cert_or_req_san_ipr   >  s;     NN*F0==JQQQQ:QQQQr   c                    t          | t          j                  r3t          j        t          j        |                               d          }n2t          j        t          j        |                               d          }t          j        d|          }d}|g n'|	                    d          
                    |          }|S )a  Get raw SAN string from cert or csr, parse it as UTF-8 and return.

    :param cert_or_req: Certificate or CSR.
    :type cert_or_req: `OpenSSL.crypto.X509` or `OpenSSL.crypto.X509Req`.

    :returns: raw san strings, parsed byte as utf-8
    :rtype: `list` of `str`

    zutf-8z5X509v3 Subject Alternative Name:(?: critical)?\s*(.*)r}   Nrf   )
isinstancer   r$   dump_certificateFILETYPE_TEXTdecoder   researchgroupr   )r   textraw_sanparts_separatorr   s        r   r   r   R  s     +v{++ b&v';[IIPPQXYY.v/C[QQXXY`aa iPRVWWGO GMM!,<,<,B,B?,S,SJr   :	 TrA   
not_beforevalidity	force_sanr   r   c           	      n   |s|s
J d            t          j                    }|                    t          t	          j        t          j        d                    d                     |                    d           |g }|g }|g }|	                    t          j
        ddd                     t          |          dk    r|d         |                                _        |                    |                                           g }|D ]}	|	                    d	|	z              |D ]}
|	                    d
|
j        z               d                    |                              d          }|s&t          |          dk    st          |          dk    r*|	                    t          j
        dd|                     |                    |           |                    |dn|           |                    |           |                    |            |                    | d           |S )at  Generate new self-signed certificate.

    :type domains: `list` of `str`
    :param OpenSSL.crypto.PKey key:
    :param bool force_san:
    :param extensions: List of additional extensions to include in the cert.
    :type extensions: `list` of `OpenSSL.crypto.X509Extension`
    :type ips: `list` of (`ipaddress.IPv4Address` or `ipaddress.IPv6Address`)

    If more than one domain is provided, all of the domains are put into
    ``subjectAltName`` X.509 extension and first domain is set as the
    subject CN. If only one domain is provided no ``subjectAltName``
    extension is used, unless `force_san` is ``True``.

    z7Must provide one or more hostnames or IPs for the cert.      Ns   basicConstraintsTs   CA:TRUE, pathlen:0r   r{   r|   r}   r~   rf   r   Fr   r   )r   r$   set_serial_numberr^   binasciihexlifyosurandomr   r   r   r   r   r   
set_issuerr   r   r   r   gmtime_adj_notBeforegmtime_adj_notAfterr   r   )rA   rw   r   r   r   r   r   rB   r   r   ipr   s               r   gen_ss_certr   q  sK   * TcTTTTTT;==D3x/
2??DDEEEQ

{'<	> 	>  
 7||a '
OOD$$&&'''G ) )v'(((( , ,ur{*++++7##**733J CLL1$$C1&.
 
 
 	 	 	 	
###:#5aa:FFFX&&&OOCIIc8Kr   chainfiletypec                     dt           t          j        t          j        f         dt
          ffdd                    fd| D                       S )zDump certificate chain into a bundle.

    :param list chain: List of `OpenSSL.crypto.X509` (or wrapped in
        :class:`josepy.util.ComparableX509`).

    :returns: certificate chain bundle
    :rtype: bytes

    rB   r   c                     t          | t          j                  r:t          | j        t          j                  rt          j        d          | j        } t	          j        |           S )NzUnexpected CSR provided.)	r   joseComparableX509wrappedr   r   r   rM   r   )rB   r   s    r   
_dump_certz(dump_pyopenssl_chain.<locals>._dump_cert  sY    dD/00 	 $,77 ?l#=>>><D&x666r   r   c              3   .   K   | ]} |          V  d S r   r&   )r   rB   r   s     r   	<genexpr>z'dump_pyopenssl_chain.<locals>.<genexpr>  s-      77JJt$$777777r   )r   r   r   r   r$   r"   r   )r   r   r   s    `@r   dump_pyopenssl_chainr     sh    7t2FK?@ 7U 7 7 7 7 7 7 887777777777r   )NFN)NNr   TNN)4rP   r   rk   	ipaddressloggingr   r   rN   typingr   r   r   r   r   r   r	   r
   r   josepyr   OpenSSLr   r   acmer   	getLoggerr   r6   SSLv23_METHODr]   r   r(   r"   r^   rQ   r$   ru   rR   IPv4AddressIPv6Addressr   r   r   r   r   r   r#   r   r   r   r   r   r&   r   r   <module>r      s               				 				                                                                             		8	$	$ '        u u u u u u u up 58/SZ:>6 6E 6 6c 6# 66AFsCx6&x76CI;6 6 6 6r VZ!&\`4" 4"e 4"huSXtCy=P7Q.R 4"4"tE)*?AV*V$WXY4" 4" 4" 4" 4"nAv{FN?Z9[ A*.s)A A A A?E&+v~2M,N ?SWX[S\ ? ? ? ?6RuV[&.5P/Q RVZ[^V_ R R R R(v{FN7R1S X\]`Xa    > BF,0FJCG[_	? ?V[ ?8DI+> ?$SM???C? %T&*>%?@? d5)>	@U)U#VWX	?
 [? ? ? ?F *0)<8 8d4+>&?fkAR&R S 8#&8AF8 8 8 8 8 8r   