
     hE                         d dl mZmZmZmZ d dlZd dlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZmZ g dZd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(dS )     )unicode_literalsdivisionabsolute_importprint_functionN)datetime   )Certificateint_from_bytestimezone)CIPHER_SUITE_MAP)TLSVerificationErrorTLSDisconnectErrorTLSError)detect_client_auth_requestextract_chainget_dh_params_lengthparse_alertparse_handshake_messagesparse_session_infoparse_tls_recordsraise_client_authraise_dh_paramsraise_disconnectionraise_expired_not_yet_validraise_handshakeraise_hostnameraise_no_issuerraise_protocol_errorraise_revokedraise_self_signedraise_verificationraise_weak_signaturec                    g }d}t          |           D ]0\  }}}|dk    rt          |          D ]\  }}|dk    r|} n|r n1|rd}|t          |          k     rjt          |||dz                      }	|dz   }
|
|	z   }|}||
|         }|                    t          j        |                     |t          |          k     j|S )a  
    Extracts the X.509 certificates from the server handshake bytes for use
    when debugging

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        A list of asn1crypto.x509.Certificate objects
    N         )r   r   lenr
   appendr	   load)server_handshake_bytesoutputchain_bytesrecord_type_record_datamessage_typemessage_datapointercert_length
cert_startcert_end
cert_bytess                I/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/oscrypto/_tls.pyr   r   #   s"    FK'89O'P'P  #Q'!!*B;*O*O 	 	&L,w&&* '  	E	  	8K(((((WWq[5H)IJJK 1J!K/HG$Z%89JMM+*:66777 K(((( M    c                 ~    t          |           D ],\  }}}|dk    rt          |          D ]\  }}|dk    r  dS -dS )a)  
    Determines if a CertificateRequest message is sent from the server asking
    the client for a certificate

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        A boolean - if a client certificate request was found
    r$      TF)r   r   )r*   r-   r.   r/   r0   r1   s         r7   r   r   K   sr     (99O'P'P  #Q'!!*B;*O*O 	 	&L,w&&ttt '	 5r8   c                     d}d}t          |           D ]0\  }}}|dk    rt          |          D ]\  }}|dk    r|} n|r n1|rt          |dd                   dz  }|S )a  
    Determines the length of the DH params from the ServerKeyExchange

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        None or an integer of the bit size of the DH parameters
    Nr$      r         )r   r   r
   )r*   r+   dh_params_bytesr-   r.   r/   r0   r1   s           r7   r   r   `   s     FO'89O'P'P  #Q'!!*B;*O*O 	 	&L,w&&". '  	E	  :! 4559Mr8   c                     t          |           D ]R\  }}}|dk    rt          |          dk    r dS t          |dd                   t          |dd                   fc S dS )aV  
    Parses the handshake for protocol alerts

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        None or an 2-element tuple of integers:
         0: 1 (warning) or 2 (fatal)
         1: The alert description (see https://tools.ietf.org/html/rfc5246#section-7.2)
       r=   Nr   r   )r   r'   r
   )r*   r-   r.   r/   s       r7   r   r      s     (99O'P'P T T#Q'!!{q  44{1Q3/00.QqSAQ2R2RSSSS4r8   c                    d}d}d}d}d}d}d}t          |           D ]\  }	}
}|	dk    rt          |          D ]\  }}|dk    rddddd	d
|dd                  }t          |dd                   }|dk    r|dd|z            }d|z   }|||dz            }t          |         }|dz   }|||dz            dk    }|dz   }||d         }t	          |          D ]\  }}|dk    rd} n t          |          D ]\  }	}
}|	dk    rt          |          D ]\  }}|dk    rt          |dd                   }|dk    r|dd|z            }d|z   }t          |||dz                      }|dz   |z   }t          |||dz                      }|3|1|dz   |z   }||d         }t	          |          D ]\  }}|dk    rd} n ||d}n||k    rd}nd}|||||dS )a  
    Parse the TLS handshake from the client to the server to extract information
    including the cipher suite selected, if compression is enabled, the
    session id and if a new or reused session ticket exists.

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :param client_handshake_bytes:
        A byte string of the handshake data sent to the server

    :return:
        A dict with the following keys:
         - "protocol": unicode string
         - "cipher_suite": unicode string
         - "compression": boolean
         - "session_id": "new", "reused" or None
         - "session_ticket: "new", "reused" or None
    NFr$      SSLv3TLSv1zTLSv1.1zTLSv1.2zTLSv1.3)s    s   s   s   s   r   r=   "   #   r       new   reused)protocolcipher_suitecompression
session_idsession_ticket)r   r   r
   r   _parse_hello_extensions)r*   client_handshake_bytesrL   rM   rN   rO   rP   server_session_idclient_session_idr-   r.   r/   r0   r1   session_id_lengthcipher_suite_startcipher_suite_bytescompression_startextensions_length_startextensions_dataextension_typeextension_datacipher_suite_lengthcompression_lengths                           r7   r   r      s8   * HLKJN'89O'P'P    #Q'!!*B;*O*O 	 	&L,w&&$$&&&  1Q3!H !/|BrE/B C C 1$$$0B9J4J1J$K!!#&7!7!-.@ASVWAW.W!X+,>?L 2Q 6&'89JQ9N'NOSZZK&7!&;#*+B+C+CDO2I/2Z2Z  .!R''%*NE ( '89O'P'P  #Q'!!*B;*O*O 	 	&L,w&& .|BrE/B C C 1$$$0B9J4J1J$K!!#&7!7"0>PQcfgQg>g1h"i"i 2Q 69L L!/=NO`cdOd=d0e!f!f !(^-C*;a*?BT*T'"./F/G/G"H6Mo6^6^  2NN%++)1 , $$JJ $555"

%
 $" (  r8   c              #     K   d}t          |           }||k     rq| ||dz            dk    rdS t          | |dz   |dz                      }| ||dz            | |dz   |dz            | |dz   |dz   |z            fV  |d|z   z  }||k     odS dS )a  
    Creates a generator returning tuples of information about each record
    in a byte string of data from a TLS client or server. Stops as soon as it
    find a ChangeCipherSpec message since all data from then on is encrypted.

    :param data:
        A byte string of TLS records

    :return:
        A generator that yields 3-element tuples:
        [0] Byte string of record type
        [1] Byte string of protocol version
        [2] Byte string of record data
    r   r      r&      Nr'   r
   datar2   data_lenlengths       r7   r   r     s        G4yyH
H

!#$//EWq[1%< =>>1$%1Wq[()1Wq[6112
 	
 	
 	

 	1v: H





r8   c              #      K   d}t          |           }||k     rOt          | |dz   |dz                      }| ||dz            | |dz   |dz   |z            fV  |d|z   z  }||k     MdS dS )a`  
    Creates a generator returning tuples of information about each message in
    a byte string of data from a TLS handshake record

    :param data:
        A byte string of a TLS handshake record data

    :return:
        A generator that yields 2-element tuples:
        [0] Byte string of message type
        [1] Byte string of message data
    r   r      Nrb   rc   s       r7   r   r   #  s       G4yyH
H

Wq[1%< =>>1$%1Wq[6112
 	
 	
 	
 	1v: H





r8   c              #   "  K   | dk    rdS t          | dd                   }d}d|z   }|}||k     r^t          | ||dz                      }t          | |dz   |dz                      }|| |dz   |dz   |z            fV  |d|z   z  }||k     \dS dS )a  
    Creates a generator returning tuples of information about each extension
    from a byte string of extension data contained in a ServerHello ores
    ClientHello message

    :param data:
        A byte string of a extension data from a TLS ServerHello or ClientHello
        message

    :return:
        A generator that yields 2-element tuples:
        [0] Byte string of extension type
        [1] Byte string of extension data
    r8   Nr   r=   rh   )r
   )rd   extentions_lengthextensions_startextensions_endr2   r[   extension_lengths          r7   rQ   rQ   <  s        s{{&tAaCy11**NG
N
"
"'WWq[-@(ABB)$w{7Q;/F*GHH1Wq[+;;;<
 	
 	
 	
 	1''' N
"
"
"
"
"
"r8   c                 D   t          j        d|          p|                    d          dk    }|rd|z  }nd|z  }d|z  }d                    | j                  }d                    | j                  }|r|d|z  z  }|r|r|d	z  }|r|d
|z  z  }t          ||           )z
    Raises a TLSVerificationError due to a hostname mismatch

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    z^\d+\.\d+\.\d+\.\d+$:zIP address %szdomain name %sz:Server certificate verification failed - %s does not matchz, z valid domains: %sz orz valid IP addresses: %s)rematchfindjoin	valid_ipsvalid_domainsr   )certificatehostnameis_iphostname_typemessageru   rv   s          r7   r   r   ^  s     H2H==YsASASWYAYE 4'(2(83J]ZG		+/00IIIk788M 8'-77  5 9,y88
w
4
44r8   c                 &    d}t          ||           )z
    Raises a generic TLSVerificationError

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    z&Server certificate verification failedr   rw   r{   s     r7   r!   r!   z  s     7G
w
4
44r8   c                 &    d}t          ||           )z
    Raises a TLSVerificationError when a certificate uses a weak signature
    algorithm

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    zMServer certificate verification failed - weak certificate signature algorithmr}   r~   s     r7   r"   r"     s     ^G
w
4
44r8   c                  $    d} t          |           )zg
    Raises a TLSError indicating client authentication is required

    :raises:
        TLSError
    z5TLS handshake failed - client authentication requiredr   )r{   s    r7   r   r     s     FG
7

r8   c                 &    d}t          ||           )z
    Raises a TLSVerificationError due to the certificate being revoked

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    zEServer certificate verification failed - certificate has been revokedr}   r~   s     r7   r   r     s     VG
w
4
44r8   c                 &    d}t          ||           )z
    Raises a TLSVerificationError due to no issuer certificate found in trust
    roots

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    zgServer certificate verification failed - certificate issuer not found in trusted root certificate storer}   r~   s     r7   r   r     s     xG
w
4
44r8   c                 &    d}t          ||           )z
    Raises a TLSVerificationError due to a self-signed certificate
    roots

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    zCServer certificate verification failed - certificate is self-signedr}   r~   s     r7   r    r      s     TG
w
4
44r8   c                 &    d}t          ||           )z
    Raises a TLSVerificationError due to a certificate lifetime exceeding
    the CAB forum certificate lifetime limit

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    zIServer certificate verification failed - certificate lifetime is too longr}   r~   s     r7   raise_lifetime_too_longr     s     ZG
w
4
44r8   c                 0   | d         d         }|d         j         }|d         j         }t          j        t          j                  }||k    r|                    d          }d|z  }n ||k     r|                    d          }d|z  }t          ||           )z
    Raises a TLSVerificationError due to certificate being expired, or not yet
    being valid

    :param certificate:
        An asn1crypto.x509.Certificate object

    :raises:
        TLSVerificationError
    tbs_certificatevalidity	not_after
not_beforez%Y-%m-%d %H:%M:%SZzGServer certificate verification failed - certificate not valid until %sz?Server certificate verification failed - certificate expired %s)nativer   nowr   utcstrftimer   )rw   r   r   r   r   formatted_beforer{   formatted_afters           r7   r   r     s     ,-j9H%,I,'.J
,x|
$
$CC%../CDD[^nn	S#,,-ABBSVee
w
4
44r8   c                       t          d          )ze
    Raises a TLSDisconnectError due to a disconnection

    :raises:
        TLSDisconnectError
    z$The remote end closed the connection)r    r8   r7   r   r      s     C
D
DDr8   c                 f    t          |           }|rt          d|z            t          d          )z
    Raises a TLSError due to a protocol error

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :raises:
        TLSError
    z.TLS protocol error - server responded using %sz@TLS protocol error - server responded using a different protocol)detect_other_protocolr   )r*   other_protocols     r7   r   r     s>     ++ABBN ZG.XYYY
U
V
VVr8   c                       t          d          )zS
    Raises a TLSError due to a handshake error

    :raises:
        TLSError
    zTLS handshake failedr   r   r8   r7   r   r     s     )
*
**r8   c                       t          d          )z_
    Raises a TLSError due to a TLS version incompatibility

    :raises:
        TLSError
    z-TLS handshake failed - protocol version errorr   r   r8   r7   raise_protocol_versionr   )  s     B
C
CCr8   c                       t          d          )zP
    Raises a TLSError due to weak DH params

    :raises:
        TLSError
    z)TLS handshake failed - weak DH parametersr   r   r8   r7   r   r   4  s     >
?
??r8   c                    | dd         dk    rdS | dd         dk    r$t          j        d| t           j                  rdS d	S | dd         d
k    rdS | dd         dk    rdS | dd         dk    s| dd         dk    rdS dS )a  
    Looks at the server handshake bytes to try and detect a different protocol

    :param server_handshake_bytes:
        A byte string of the handshake data received from the server

    :return:
        None, or a unicode string of "ftp", "http", "imap", "pop3", "smtp"
    r   ra   s   HTTP/HTTPrh   s   220 s
   ^[^
]*ftpFTPSMTPs   220-s   +OK POP3s   * OK	   s	   * PREAUTHIMAPN)rq   rr   I)r*   s    r7   r   r   ?  s     ac"h..vac"g--8O%;RTBB 	56ac"g--uac"g--vac"g--1G!1LP\1\1\v4r8   ))
__future__r   r   r   r   rq   r   _asn1r	   r
   r   _cipher_suitesr   errorsr   r   r   __all__r   r   r   r   r   r   r   rQ   r   r!   r"   r   r   r   r    r   r   r   r   r   r   r   r   r   r8   r7   <module>r      s   R R R R R R R R R R R R 				       8 8 8 8 8 8 8 8 8 8 , , , , , , F F F F F F F F F F  .% % %P  *  >  ,l l l^  >  2( ( (D5 5 585 5 55 5 5 	 	 	5 5 55 5 5 5 5 5 5 5 5 5 5 58E E EW W W&+ + +D D D@ @ @    r8   