Net_DNS
[ class tree: Net_DNS ] [ index: Net_DNS ] [ all elements ]

Source for file TSIG.php

Documentation is available at TSIG.php

  1. <?php
  2. /**
  3. *  License Information:
  4. *
  5. *  Net_DNS:  A resolver library for PHP
  6. *  Copyright (c) 2002-2003 Eric Kilfoil eric@ypass.net
  7. *  Maintainers:
  8. *  Marco Kaiser <bate@php.net>
  9. *  Florian Anderiasch <fa@php.net>
  10. *
  11. * PHP versions 4 and 5
  12. *
  13. * LICENSE: This source file is subject to version 3.01 of the PHP license
  14. * that is available through the world-wide-web at the following URI:
  15. * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
  16. * the PHP License and are unable to obtain it through the web, please
  17. * send a note to license@php.net so we can mail you a copy immediately.
  18. */
  19.  
  20. define('NET_DNS_DEFAULT_ALGORITHM''hmac-md5.sig-alg.reg.int');
  21. define('NET_DNS_DEFAULT_FUDGE'300);
  22.  
  23. /* Net_DNS_RR_TSIG definition {{{ */
  24. /**
  25.  * A representation of a resource record of type <b>TSIG</b>
  26.  *
  27.  * @package Net_DNS
  28.  */
  29. class Net_DNS_RR_TSIG extends Net_DNS_RR
  30. {
  31.     /* class variable definitions {{{ */
  32.     var $name;
  33.     var $type;
  34.     var $class;
  35.     var $ttl;
  36.     var $rdlength;
  37.     var $rdata;
  38.     var $time_signed;
  39.     var $fudge;
  40.     var $mac_size;
  41.     var $mac;
  42.     var $original_id;
  43.     var $error;
  44.     var $other_len;
  45.     var $other_data;
  46.     var $key;
  47.  
  48.     /* }}} */
  49.     /* class constructor - RR(&$rro, $data, $offset = '') {{{ */
  50.     function Net_DNS_RR_TSIG(&$rro$data$offset '')
  51.     {
  52.         $this->name = $rro->name;
  53.         $this->type = $rro->type;
  54.         $this->class = $rro->class;
  55.         $this->ttl = $rro->ttl;
  56.         $this->rdlength = $rro->rdlength;
  57.         $this->rdata = $rro->rdata;
  58.  
  59.         if ($offset{
  60.             if ($this->rdlength > 0{
  61.                 list($alg$offsetNet_DNS_Packet::dn_expand($data$offset);
  62.                 $this->algorithm $alg;
  63.  
  64.                 $d unpack("\@$offset/nth/Ntl/nfudge/nmac_size"$data);
  65.                 $time_high $d['th'];
  66.                 $time_low $d['tl'];
  67.                 $this->time_signed = $time_low;
  68.                 $this->fudge = $d['fudge'];
  69.                 $this->mac_size = $d['mac_size'];
  70.                 $offset += 10;
  71.  
  72.                 $this->mac = substr($data$offset$this->mac_size);
  73.                 $offset += $this->mac_size;
  74.  
  75.                 $d unpack("@$offset/noid/nerror/nolen"$data);
  76.                 $this->original_id = $d['oid'];
  77.                 $this->error = $d['error'];
  78.                 $this->other_len = $d['olen'];
  79.                 $offset += 6;
  80.  
  81.                 $odata substr($data$offset$this->other_len);
  82.                 $d unpack('nodata_high/Nodata_low'$odata);
  83.                 $this->other_data = $d['odata_low'];
  84.             }
  85.         else {
  86.             if (strlen($data&& preg_match('/^(.*)$/'$data$regs)) {
  87.                 $this->key = $regs[1];
  88.             }
  89.  
  90.             $this->algorithm   NET_DNS_DEFAULT_ALGORITHM;
  91.             $this->time_signed = time();
  92.  
  93.             $this->fudge       = NET_DNS_DEFAULT_FUDGE;
  94.             $this->mac_size    = 0;
  95.             $this->mac         = '';
  96.             $this->original_id = 0;
  97.             $this->error       = 0;
  98.             $this->other_len   = 0;
  99.             $this->other_data  = '';
  100.  
  101.             // RFC 2845 Section 2.3
  102.             $this->class = 'ANY';
  103.         }
  104.     }
  105.  
  106.     /* }}} */
  107.     /* Net_DNS_RR_TSIG::rdatastr() {{{ */
  108.     function rdatastr()
  109.     {
  110.         $error $this->error;
  111.         if ($error{
  112.             $error 'UNDEFINED';
  113.         }
  114.  
  115.         if (strlen($this->algorithm)) {
  116.             $rdatastr $this->algorithm '. ' $this->time_signed . ' ' .
  117.                 $this->fudge . ' ';
  118.             if ($this->mac_size && strlen($this->mac)) {
  119.                 $rdatastr .= ' ' $this->mac_size . ' ' base64_encode($this->mac);
  120.             else {
  121.                 $rdatastr .= ' 0 ';
  122.             }
  123.             $rdatastr .= ' ' $this->original_id . ' ' $error;
  124.             if ($this->other_len && strlen($this->other_data)) {
  125.                 $rdatastr .= ' ' $this->other_data;
  126.             else {
  127.                 $rdatastr .= ' 0 ';
  128.             }
  129.         else {
  130.             $rdatastr '; no data';
  131.         }
  132.  
  133.         return $rdatastr;
  134.     }
  135.  
  136.     /* }}} */
  137.     /* Net_DNS_RR_TSIG::rr_rdata($packet, $offset) {{{ */
  138.     function rr_rdata($packet$offset)
  139.     {
  140.         $rdata '';
  141.         $sigdata '';
  142.  
  143.         if (strlen($this->key)) {
  144.             $key $this->key;
  145.             $key ereg_replace(' '''$key);
  146.             $key base64_decode($key);
  147.  
  148.             $newpacket $packet;
  149.             $newoffset $offset;
  150.             array_pop($newpacket->additional);
  151.             $newpacket->header->arcount--;
  152.             $newpacket->compnames = array();
  153.  
  154.             /*
  155.              * Add the request MAC if present (used to validate responses).
  156.              */
  157.             if (isset($this->request_mac)) {
  158.                 $sigdata .= pack('H*'$this->request_mac);
  159.             }
  160.             $sigdata .= $newpacket->data();
  161.  
  162.             /*
  163.              * Don't compress the record (key) name.
  164.              */
  165.             $tmppacket = new Net_DNS_Packet;
  166.             $sigdata .= $tmppacket->dn_comp(strtolower($this->name)0);
  167.  
  168.             $sigdata .= pack('n'Net_DNS::classesbyname(strtoupper($this->class)));
  169.             $sigdata .= pack('N'$this->ttl);
  170.  
  171.             /*
  172.              * Don't compress the algorithm name.
  173.              */
  174.             $tmppacket->compnames = array();
  175.             $sigdata .= $tmppacket->dn_comp(strtolower($this->algorithm)0);
  176.  
  177.             $sigdata .= pack('nN'0$this->time_signed);
  178.             $sigdata .= pack('n'$this->fudge);
  179.             $sigdata .= pack('nn'$this->error$this->other_len);
  180.  
  181.             if (strlen($this->other_data)) {
  182.                 $sigdata .= pack('nN'0$this->other_data);
  183.             }
  184.  
  185.             //$this->mac = mhash(MHASH_MD5, $sigdata, $key);
  186.             $this->mac = $this->hmac($sigdata$key'md5');
  187.             $this->mac_size = strlen($this->mac);
  188.  
  189.             /*
  190.              * Don't compress the algorithm name.
  191.              */
  192.             unset($tmppacket);
  193.             $tmppacket = new Net_DNS_Packet;
  194.             $rdata .= $tmppacket->dn_comp(strtolower($this->algorithm)0);
  195.  
  196.             $rdata .= pack('nN'0$this->time_signed);
  197.             $rdata .= pack('nn'$this->fudge$this->mac_size);
  198.             $rdata .= $this->mac;
  199.  
  200.             $rdata .= pack('nnn',$packet->header->id,
  201.                     $this->error,
  202.                     $this->other_len);
  203.  
  204.             if ($this->other_data{
  205.                 $rdata .= pack('nN'0$this->other_data);
  206.             }
  207.         }
  208.         return $rdata;
  209.     }
  210.     /* }}} */
  211.     /* Net_DNS_RR_TSIG::error() {{{ */
  212.     function error()
  213.     {
  214.         if ($this->error != 0{
  215.             $rcode Net_DNS::rcodesbyval($error);
  216.         }
  217.         return $rcode;
  218.     }
  219.     /* }}} */
  220.  
  221.     /* Net_DNS_RR_TSIG::hmac() {{{ */
  222.     // Calculate HMAC according to RFC2104
  223.     // http://www.ietf.org/rfc/rfc2104.txt
  224.     // posted by mina86 at tlen dot pl on http://php.net/manual/en/function.md5.php
  225.     function hmac($data$key$hash 'md5'$blocksize = 64{
  226.         if (strlen($key)>$blocksize{
  227.             $key pack('H*'$hash($key));
  228.         }
  229.         $key  str_pad($key$blocksizechr(0));
  230.         $ipad str_repeat(chr(0x36)$blocksize);
  231.         $opad str_repeat(chr(0x5c)$blocksize);
  232.         return $hash(($key^$opadpack('H*'$hash(($key^$ipad$data)));
  233.     }
  234.     /* }}} */
  235. }
  236. /* }}} */
  237. /* VIM settings {{{
  238.  * Local variables:
  239.  * tab-width: 4
  240.  * c-basic-offset: 4
  241.  * soft-stop-width: 4
  242.  * c indent on
  243.  * expandtab on
  244.  * End:
  245.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  246.  * vim<600: sw=4 ts=4
  247.  * }}} */
  248. ?>

Documentation generated on Mon, 11 Mar 2019 15:31:49 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.