3
[_ð]…  ã               @   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m	Z	m
Z
m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jdƒZejjdƒZejjdƒZejjdƒZejjdƒZej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Z%dZ&dZ'dZ(ddde$fdd „Z)ddde$fd!d"„Z*d)d#d$„Z+d%d&„ Z,d'd(„ Z-dS )*zDNS TSIG support.é    Né   )ÚlongÚstring_typesÚ	text_typec               @   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   ú-/tmp/pip-build-yz_zf6az/dnspython/dns/tsig.pyr      s   r   c               @   s   e Zd ZdZdS )ÚBadSignaturez#The TSIG signature fails to verify.N)r   r   r	   r
   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 peerN)r   r   r	   r
   r   r   r   r   r   '   s   r   c               @   s   e Zd ZdZdS )Ú
PeerBadKeyz$The peer didn't know the key we usedN)r   r   r	   r
   r   r   r   r   r   ,   s   r   c               @   s   e Zd ZdZdS )ÚPeerBadSignaturez*The peer didn't like the signature we sentN)r   r   r	   r
   r   r   r   r   r   1   s   r   c               @   s   e Zd ZdZdS )ÚPeerBadTimez%The peer didn't like the time we sentN)r   r   r	   r
   r   r   r   r   r   6   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 sentN)r   r   r	   r
   r   r   r   r   r   ;   s   r   zHMAC-MD5.SIG-ALG.REG.INTz	hmac-sha1zhmac-sha224zhmac-sha256zhmac-sha384zhmac-sha512é   é   é   é   FTc             C   sÄ  t |tƒr|jƒ }t|ƒ\}}|r\tj||d�}	t|ƒ}|dkr\|	jtj	d|ƒƒ |	j|ƒ tj	d|ƒ}|	j|ƒ |	j| dd… ƒ |r¾|	j|j
ƒ ƒ |	jtj	dtjjƒƒ |	jtj	ddƒƒ |tdƒ }|d? tdƒ@ }|td	ƒ@ }tj	d
|||ƒ}|| }t|ƒ}|dk�rtdƒ‚tj	d||ƒ| }|�rF|	j|ƒ |	j|ƒ n
|	j|ƒ |	jƒ }tj	dt|ƒƒ}|| | | | }|
�r¶tj||d�}	t|ƒ}|	jtj	d|ƒƒ |	j|ƒ nd}	|||	fS )aj  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, string, hmac.HMAC object)
    @raises ValueError: I{other_data} is too long
    @raises NotImplementedError: I{algorithm} is not supported
    )Ú	digestmodr   z!Hé   Nz!Ié    iÿÿ  l   ÿÿ z!HIHz TSIG Other Data is > 65535 bytesz!HH)Ú
isinstancer   ÚencodeÚget_algorithmÚhmacÚnewÚlenÚupdateÚstructÚpackÚto_digestableÚdnsZ
rdataclassÚANYr   Ú
ValueErrorÚdigest)ÚwireÚkeynameÚsecretÚtimeÚfudgeÚoriginal_idÚerrorÚ
other_dataÚrequest_macÚctxÚmultiÚfirstÚ	algorithmZalgorithm_namer   ÚmlÚidZ	long_timeÚ
upper_timeÚ
lower_timeZtime_macZpre_macÚolZpost_macÚmacZmpackÚ
tsig_rdatar   r   r   ÚsignY   sN    





r<   c             C   s    t | |||||||||	|
||ƒS )N)r<   )r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r   r   r   Úhmac_md5�   s    r=   c             C   sÖ  t jd| dd… ƒ\}|dkr&tjj‚|d8 }| dd… t jd|ƒ | d|…  }|}tjj| |ƒ\}}|| }t jd| ||d … ƒ\}}}}|tdƒ d> |tdƒ  }|d7 }| ||| … }||7 }t jd| ||d	 … ƒ\}}}|d	7 }| ||| … }||7 }||| k�r tjj‚|dk�rv|t	k�r:t
‚n<|tk�rJt‚n,|tk�rZt‚n|tk�rjt‚ntd
| ƒ‚|| }|| }||k �sš||k�ržt‚t|||||||||||	|
|ƒ\}}}||k�rÒ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 objectz!Hé
   é   r   r   z!HIHHr   z!HHHé   zunknown TSIG error code %d)r!   Úunpackr$   Ú	exceptionÚ	FormErrorr"   ÚnameÚ	from_wirer   ÚBADSIGr   ÚBADKEYr   ÚBADTIMEr   ÚBADTRUNCr   r   r   r<   r   )r(   r)   r*   Únowr0   Z
tsig_startr;   Ú
tsig_rdlenr1   r2   r3   ZadcountZnew_wireÚcurrentÚanameÚusedr7   r8   r,   Úmac_sizer+   r:   r-   r.   Z
other_sizer/   Útime_lowZ	time_highZjunkZour_macr   r   r   Úvalidate–   sN    
$ 





rQ   c             C   sT   t | tƒrtjj| ƒ} y| jƒ t|  fS  tk
rN   tdt	| ƒ d ƒ‚Y nX dS )zÊReturns the wire format string and the hash module to use for the
    specified TSIG algorithm

    @rtype: (string, hash constructor)
    @raises NotImplementedError: I{algorithm} is not supported
    zTSIG algorithm z is not supportedN)
r   r   r$   rD   Ú	from_textr#   Ú_hashesÚKeyErrorÚNotImplementedErrorÚstr)r4   r   r   r   r   Ì   s    
r   c             C   sz   |}t jj| |ƒ\}}|| }tjd| ||d … ƒ\}}}}	|d7 }| |||	 … }
||	7 }||| krrt jj‚||
fS )zlReturn the tsig algorithm for the specified tsig_rdata
    @raises FormError: The TSIG is badly formed.
    z!HIHHr>   )r$   rD   rE   r!   rA   rB   rC   )r(   r;   rK   rL   rM   rN   r7   r8   r,   rO   r:   r   r   r   Úget_algorithm_and_macÞ   s     rW   )NFT).r
   Úhashlibr   r!   Zdns.exceptionr$   Zdns.rdataclassZdns.nameÚ_compatr   r   r   rB   ZDNSExceptionr   r   r   r   r   r   r   rD   rR   ZHMAC_MD5Z	HMAC_SHA1ZHMAC_SHA224ZHMAC_SHA256ZHMAC_SHA384ZHMAC_SHA512Úsha224Úsha256Úsha384Úsha512Úsha1Úmd5rS   Zdefault_algorithmrF   rG   rH   rI   r<   r=   rQ   r   rW   r   r   r   r   Ú<module>   sL   
5
5