
                            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ej                  j                        Z G d dej                  j                        Z G d	 d
ej                  j                        Z G d de      Z G d de      Z G d de      Z G 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jB                  eejD                  iZ#eZ$dZ%dZ&dZ'dZ(ddde$fdZ)ddde$fd Z*	 d$d!Z+d" Z,d# Z-y)%zDNS TSIG support.    N   )longstring_types	text_typec                       e Zd ZdZy)BadTimez8The current time is not within the TSIG's validity time.N__name__
__module____qualname____doc__     lib/third_party/dns/tsig.pyr   r      s    Br   r   c                       e Zd ZdZy)BadSignaturez#The TSIG signature fails to verify.Nr	   r   r   r   r   r   "   s    -r   r   c                       e Zd ZdZy)	PeerErrorz;Base class for all TSIG errors generated by the remote peerNr	   r   r   r   r   r   '   s    Er   r   c                       e Zd ZdZy)
PeerBadKeyz$The peer didn't know the key we usedNr	   r   r   r   r   r   ,   s    .r   r   c                       e Zd ZdZy)PeerBadSignaturez*The peer didn't like the signature we sentNr	   r   r   r   r   r   1   s    4r   r   c                       e Zd ZdZy)PeerBadTimez%The peer didn't like the time we sentNr	   r   r   r   r   r   6   s    /r   r   c                       e Zd ZdZy)PeerBadTruncationz=The peer didn't like amount of truncation in the TSIG we sentNr	   r   r   r   r   r   ;   s    Gr   r   zHMAC-MD5.SIG-ALG.REG.INTz	hmac-sha1zhmac-sha224zhmac-sha256zhmac-sha384zhmac-sha512            FTc                 @   t        |t              r|j                         }t        |      \  }}|r]t	        j
                  ||      }	t        |      }|dkD  r6|	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      z   }|dz	  t        d      z  }|t        d	      z  }t        j                  d
|||      }||z   }t        |      }|dkD  rt        d      t        j                  d||      |z   }|r#|	j                  |       |	j                  |       n|	j                  |       |	j!                         }t        j                  dt        |            }||z   |z   |z   |z   }|
rYt	        j
                  ||      }	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   !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dns
rdataclassANYr   
ValueErrordigest)wirekeynamesecrettimefudgeoriginal_iderror
other_datarequest_macctxmultifirst	algorithmalgorithm_namer"   mlid	long_time
upper_time
lower_timetime_macpre_macolpost_macmacmpack
tsig_rdatas                              r   signrO   Y   s    *i(&&(
"/	":^Yhhv36JJv{{4,-JJ{#	T;	'BJJrNJJtABx

7((*+

6;;tS^^%7%789

6;;tQ'(tAwIr/T&\1JT*--J{{6:z5AHx'G	ZB	Ez;<<{{5%,z9H

7

8

8
**,CKKc#h'E53&+h6Jhhv3X

6;;tR()

3S!!r   c                 0    t        | |||||||||	|
||      S )N)rO   )r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   s                r   hmac_md5rQ      s.     gvtUKKeUIG Gr   c                    t        j                  d| dd       \  }|dk(  rt        j                  j                  |dz  }| dd t        j
                  d|      z   | d| z   }|}t        j                  j                  | |      \  }}||z   }t        j                  d| ||dz          \  }}}}|t        d      z   dz  |t        d      z   z   }|dz  }| |||z    }||z  }t        j                  d| ||d	z          \  }}}|d	z  }| |||z    }||z  }|||z   k7  rt        j                  j                  |dk7  rJ|t        k(  rt        |t        k(  rt        |t        k(  rt        |t        k(  rt         t#        d
|z        ||z
  }||z   }||k  s||kD  rt$        t'        |||||||||||	|
|      \  }}}||k7  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 objectr#   
      r   r   !HIHHr%   z!HHH   zunknown TSIG error code %d)r-   unpackr0   	exception	FormErrorr.   name	from_wirer   BADSIGr   BADKEYr   BADTIMEr   BADTRUNCr   r   r   rO   r   )r5   r6   r7   nowr=   
tsig_startrN   
tsig_rdlenr>   r?   r@   adcountnew_wirecurrentanameusedrF   rG   r9   mac_sizer8   rL   r:   r;   
other_sizer<   time_low	time_highjunkour_macs                                 r   validatern      s    tT"R[1JW!|mm%%%qLGAbzFKKg66b9LLHGHH&&tW5MUDnGgtGGbL9: .ZUH$q'!b(Z$q'-ABDrMG
ww)
*CxGfd77Q;78 %[%qLGgg
23JzG*z))mm%%%zF?""f_gh##85@AAe|HuI
X~y'64 +UJ +S%GT7C #~Jr   c                     t        | t              rt        j                  j	                  |       } 	 | j                         t        |    fS # t        $ r t        dt        |       z   dz         w xY w)zReturns 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 supported)
r&   r   r0   rZ   	from_textr/   _hashesKeyErrorNotImplementedErrorstr)rA   s    r   r(   r(      sp     )\*HH&&y1	7'')79+=>> 7!"3c)n"D"5#6 7 	77s   A
 
$A.c                    |}t         j                  j                  | |      \  }}||z   }t        j                  d| ||dz          \  }}}}	|dz  }| |||	z    }
||	z  }|||z   kD  rt         j
                  j                  ||
fS )zlReturn the tsig algorithm for the specified tsig_rdata
    @raises FormError: The TSIG is badly formed.
    rU   rS   )r0   rZ   r[   r-   rW   rX   rY   )r5   rN   rb   re   rf   rg   rF   rG   r9   rh   rL   s              r   get_algorithm_and_macrv      s     GHH&&tW5MUDnGgtGGbL9: .ZUHrMG
ww)
*CxGj((mm%%%3<r   )NFT).r   hashlibr)   r-   dns.exceptionr0   dns.rdataclassdns.name_compatr   r   r   rX   DNSExceptionr   r   r   r   r   r   r   rZ   rp   HMAC_MD5	HMAC_SHA1HMAC_SHA224HMAC_SHA256HMAC_SHA384HMAC_SHA512sha224sha256sha384sha512sha1md5rq   default_algorithmr\   r]   r^   r_   rO   rQ   rn   r(   rv   r   r   r   <module>r      s  $        2 2Ccmm(( C
.3==-- .
F** F
/ /
5y 5
0) 0
H	 H 8889HH{+	hh  /hh  /hh  /hh  / w||gkk  		
 '+%t$3"n +/e4(G 7;3l7$r   