a
    sd_F                     @   s  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	ZG dd dej
jZG dd dej
jZG dd dej
jZG d	d
 d
ej
jZG dd dej
jZG dd deZG dd deZG dd deZG dd deZejdZejdZejdZejdZejdZejdZeejeejeejeej eej!eej"iZ#eZ$d(ddZ%dd Z&d)d d!Z'd*d"d#Z(d$d% Z)G d&d' d'Z*dS )+zDNS TSIG support.    Nc                   @   s   e Zd ZdZdS )BadTimez8The current time is not within the TSIG's validity time.N__name__
__module____qualname____doc__ r   r   */usr/lib/python3/dist-packages/dns/tsig.pyr      s   r   c                   @   s   e Zd ZdZdS )BadSignaturez#The TSIG signature fails to verify.Nr   r   r   r   r	   r
   #   s   r
   c                   @   s   e Zd ZdZdS )BadKeyz2The TSIG record owner name does not match the key.Nr   r   r   r   r	   r   (   s   r   c                   @   s   e Zd ZdZdS )BadAlgorithmz*The TSIG algorithm does not match the key.Nr   r   r   r   r	   r   -   s   r   c                   @   s   e Zd ZdZdS )	PeerErrorz;Base class for all TSIG errors generated by the remote peerNr   r   r   r   r	   r   2   s   r   c                   @   s   e Zd ZdZdS )
PeerBadKeyz$The peer didn't know the key we usedNr   r   r   r   r	   r   7   s   r   c                   @   s   e Zd ZdZdS )PeerBadSignaturez*The peer didn't like the signature we sentNr   r   r   r   r	   r   <   s   r   c                   @   s   e Zd ZdZdS )PeerBadTimez%The peer didn't like the time we sentNr   r   r   r   r	   r   A   s   r   c                   @   s   e Zd ZdZdS )PeerBadTruncationz=The peer didn't like amount of truncation in the TSIG we sentNr   r   r   r   r	   r   F   s   r   zHMAC-MD5.SIG-ALG.REG.INTz	hmac-sha1zhmac-sha224zhmac-sha256zhmac-sha384zhmac-sha512c                 C   s0  |o| }|r:t |}|r:|tdt| || |td|j || dd  |r||j  |tdtj	j
 |tdd |du r|j}|d? d@ }|d@ }	td	||	|j}
t|j}|dkrtd
|r"||j |
  |td|j||j  n
||
 |S )zReturn a context containing the TSIG rdata for the input parameters
    @rtype: hmac.HMAC object
    @raises ValueError: I{other_data} is too long
    @raises NotImplementedError: I{algorithm} is not supported
    !H   Nz!Ir       i  l    z!HIHz TSIG Other Data is > 65535 bytesz!HH)get_contextupdatestructpacklenoriginal_idnameZto_digestabledns
rdataclassANYtime_signedfudgeother
ValueError	algorithmerror)wirekeyrdatatimerequest_macctxmultifirstZ
upper_timeZ
lower_timeZtime_encodedZ	other_lenr   r   r	   _digest_   s2    



r-   c                 C   s8   |r0t | }|tdt| || |S dS dS )ztIf this is the first message in a multi-message sequence,
    start a new context.
    @rtype: hmac.HMAC object
    r   N)r   r   r   r   r   )r&   macr+   r*   r   r   r	   _maybe_start_digest   s    
r/   Fc           	      C   s^   t | ||||||}| }tjjjtjjtjj|j||j	||j
|j|j	}|t|||fS )ab  Return a (tsig_rdata, mac, ctx) tuple containing the HMAC TSIG rdata
    for the input parameters, the HMAC MAC calculated by applying the
    TSIG signature algorithm, and the TSIG digest context.
    @rtype: (string, hmac.HMAC object)
    @raises ValueError: I{other_data} is too long
    @raises NotImplementedError: I{algorithm} is not supported
    )r-   digestr   Zrdtypesr   ZTSIGr   Z	rdatatyper#   r    r   r$   r!   r/   )	r%   r&   r'   r(   r)   r*   r+   r.   Ztsigr   r   r	   sign   s    	r1   c	                 C   s,  t d| dd \}	|	dkr&tjj|	d8 }	| dd t d|	 | d|  }
|jdkr|jtjjkrpt	nJ|jtjj
krtn6|jtjjkrtn"|jtjjkrtntd|j t|j| |jkrt|j|krt|j|jkrtt|
||d|||}| }t||js tt|||S )a*  Validate the specified TSIG rdata against the other input parameters.

    @raises FormError: The TSIG is badly formed.
    @raises BadTime: There is too much time skew between the client and the
    server.
    @raises BadSignature: The TSIG signature did not validate
    @rtype: hmac.HMAC objectr   
      r      zunknown TSIG error code %dN) r   Zunpackr   	exceptionZ	FormErrorr   r$   ZrcodeZBADSIGr   ZBADKEYr   ZBADTIMEr   ZBADTRUNCr   r   absr   r    r   r   r   r#   r   r-   r0   hmacZcompare_digestr.   r
   r/   )r%   r&   ownerr'   Znowr)   Z
tsig_startr*   r+   ZadcountZnew_wirer.   r   r   r	   validate   s4    
$

r9   c                 C   sH   zt | j }W n( ty6   td| j dd Y n0 tj| j|dS )zReturns an HMAC context foe the specified key.

    @rtype: HMAC context
    @raises NotImplementedError: I{algorithm} is not supported
    zTSIG algorithm  zis not supported)	digestmod)_hashesr#   KeyErrorNotImplementedErrorr7   newsecret)r&   r;   r   r   r	   r      s    r   c                   @   s    e Zd ZefddZdd ZdS )Keyc                 C   sZ   t |trtj|}|| _t |tr4t| }|| _t |trPtj|}|| _	d S N)

isinstancestrr   r   	from_textbase64Zdecodebytesencoder@   r#   )selfr   r@   r#   r   r   r	   __init__   s    


zKey.__init__c                 C   s.   t |to,| j|jko,| j|jko,| j|jkS rB   )rC   rA   r   r@   r#   )rH   r!   r   r   r	   __eq__   s    



z
Key.__eq__N)r   r   r   default_algorithmrI   rJ   r   r   r   r	   rA      s   rA   )NNNN)NNNF)NF)+r   rF   Zhashlibr7   r   Zdns.exceptionr   Zdns.rdataclassZdns.nameZ	dns.rcoder5   ZDNSExceptionr   r
   r   r   r   r   r   r   r   r   rE   ZHMAC_MD5Z	HMAC_SHA1ZHMAC_SHA224ZHMAC_SHA256ZHMAC_SHA384ZHMAC_SHA512Zsha224Zsha256Zsha384Zsha512Zsha1Zmd5r<   rK   r-   r/   r1   r9   r   rA   r   r   r   r	   <module>   sR   	  
$
  
'