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.                 $packet = new Net_DNS_Packet();
  62.  
  63.                 list($alg$offset$packet->dn_expand($data$offset);
  64.                 $this->algorithm $alg;
  65.  
  66.                 $d unpack("@$offset/nth/Ntl/nfudge/nmac_size"$data);
  67.                 $time_high $d['th'];
  68.                 $time_low $d['tl'];
  69.                 $this->time_signed = $time_low;
  70.                 $this->fudge = $d['fudge'];
  71.                 $this->mac_size = $d['mac_size'];
  72.                 $offset += 10;
  73.  
  74.                 $this->mac = substr($data$offset$this->mac_size);
  75.                 $offset += $this->mac_size;
  76.  
  77.                 $d unpack("@$offset/noid/nerror/nolen"$data);
  78.                 $this->original_id = $d['oid'];
  79.                 $this->error = $d['error'];
  80.                 $this->other_len = $d['olen'];
  81.                 $offset += 6;
  82.  
  83.                 if ($this->other_len{
  84.                     $odata substr($data$offset$this->other_len);
  85.                     $d unpack('nodata_high/Nodata_low'$odata);
  86.                     $this->other_data = $d['odata_low'];
  87.                 }
  88.             }
  89.         elseif (is_array($data)) {
  90.             $this->key = $data['key'];
  91.             $this->algorithm $data['algorithm'];
  92.             $this->time_signed = $data['time_signed'];
  93.             $this->fudge = $data['fudge'];
  94.             $this->mac = $data['mac'];
  95.             $this->original_id = $data['original_id'];
  96.             $this->error = $data['error'];
  97.             $this->other_len = $data['other_len'];
  98.             $this->other_data = $data['other_data'];
  99.         else {
  100.             if (strlen($data&& preg_match('/^(.*)$/'$data$regs)) {
  101.                 $this->key = $regs[1];
  102.             }
  103.  
  104.             $this->algorithm   NET_DNS_DEFAULT_ALGORITHM;
  105.             $this->time_signed = time();
  106.  
  107.             $this->fudge       = NET_DNS_DEFAULT_FUDGE;
  108.             $this->mac_size    = 0;
  109.             $this->mac         = '';
  110.             $this->original_id = 0;
  111.             $this->error       = 0;
  112.             $this->other_len   = 0;
  113.             $this->other_data  = '';
  114.  
  115.             // RFC 2845 Section 2.3
  116.             $this->class = 'ANY';
  117.         }
  118.     }
  119.  
  120.     /* }}} */
  121.     /* Net_DNS_RR_TSIG::rdatastr() {{{ */
  122.     function rdatastr()
  123.     {
  124.         $error $this->error;
  125.         if ($error{
  126.             $error 'UNDEFINED';
  127.         }
  128.  
  129.         if (strlen($this->algorithm)) {
  130.             $rdatastr $this->algorithm '. ' $this->time_signed . ' ' .
  131.                 $this->fudge . ' ';
  132.             if ($this->mac_size && strlen($this->mac)) {
  133.                 $rdatastr .= ' ' $this->mac_size . ' ' base64_encode($this->mac);
  134.             else {
  135.                 $rdatastr .= ' 0 ';
  136.             }
  137.             $rdatastr .= ' ' $this->original_id . ' ' $error;
  138.             if ($this->other_len && strlen($this->other_data)) {
  139.                 $rdatastr .= ' ' $this->other_data;
  140.             else {
  141.                 $rdatastr .= ' 0 ';
  142.             }
  143.         else {
  144.             $rdatastr '; no data';
  145.         }
  146.  
  147.         return $rdatastr;
  148.     }
  149.  
  150.     /* }}} */
  151.     /* Net_DNS_RR_TSIG::rr_rdata($packet, $offset) {{{ */
  152.     function rr_rdata($packet$offset)
  153.     {
  154.         $rdata '';
  155.         $sigdata '';
  156.  
  157.         if (strlen($this->key)) {
  158.             $key $this->key;
  159.             $key preg_replace('/ /'''$key);
  160.             $key base64_decode($key);
  161.  
  162.             $newpacket $packet;
  163.             $newoffset $offset;
  164.             array_pop($newpacket->additional);
  165.             $newpacket->header->arcount--;
  166.             $newpacket->compnames = array();
  167.  
  168.             /*
  169.              * Add the request MAC if present (used to validate responses).
  170.              */
  171.             if (isset($this->request_mac)) {
  172.                 $sigdata .= pack('H*'$this->request_mac);
  173.             }
  174.             $sigdata .= $newpacket->data();
  175.  
  176.             /*
  177.              * Don't compress the record (key) name.
  178.              */
  179.             $tmppacket = new Net_DNS_Packet;
  180.             $sigdata .= $tmppacket->dn_comp(strtolower($this->name)0);
  181.  
  182.             $sigdata .= pack('n'Net_DNS::classesbyname(strtoupper($this->class)));
  183.             $sigdata .= pack('N'$this->ttl);
  184.  
  185.             /*
  186.              * Don't compress the algorithm name.
  187.              */
  188.             $tmppacket->compnames = array();
  189.             $sigdata .= $tmppacket->dn_comp(strtolower($this->algorithm)0);
  190.  
  191.             $sigdata .= pack('nN'0$this->time_signed);
  192.             $sigdata .= pack('n'$this->fudge);
  193.             $sigdata .= pack('nn'$this->error$this->other_len);
  194.  
  195.             if (strlen($this->other_data)) {
  196.                 $sigdata .= pack('nN'0$this->other_data);
  197.             }
  198.  
  199.             $this->mac = $this->hmac_md5($sigdata$key);
  200.             $this->mac_size = strlen($this->mac);
  201.  
  202.             /*
  203.              * Don't compress the algorithm name.
  204.              */
  205.             unset($tmppacket);
  206.             $tmppacket = new Net_DNS_Packet;
  207.             $rdata .= $tmppacket->dn_comp(strtolower($this->algorithm)0);
  208.  
  209.             $rdata .= pack('nN'0$this->time_signed);
  210.             $rdata .= pack('nn'$this->fudge$this->mac_size);
  211.             $rdata .= $this->mac;
  212.  
  213.             $rdata .= pack('nnn',$packet->header->id,
  214.                     $this->error,
  215.                     $this->other_len);
  216.  
  217.             if ($this->other_data{
  218.                 $rdata .= pack('nN'0$this->other_data);
  219.             }
  220.         }
  221.         return $rdata;
  222.     }
  223.     /* }}} */
  224.     /* Net_DNS_RR_TSIG::error() {{{ */
  225.     function error()
  226.     {
  227.         if ($this->error != 0{
  228.             $rcode Net_DNS::rcodesbyval($error);
  229.         }
  230.         return $rcode;
  231.     }
  232.     /* }}} */
  233.  
  234.     /* Net_DNS_RR_TSIG::hmac() {{{ */
  235.     // Calculate HMAC according to RFC2104
  236.     // http://www.ietf.org/rfc/rfc2104.txt
  237.     // posted by mina86 at tlen dot pl on http://php.net/manual/en/function.md5.php
  238.     /**
  239.      * @deprecated
  240.      */
  241.     function hmac($data$key$hash 'md5'$blocksize = 64{
  242.         if ($hash === 'md5'{
  243.             return $this->hmac_md5($data$key);
  244.         }
  245.  
  246.         return false;
  247.     }
  248.  
  249.     /* Net_DNS_RR_TSIG::hmac_md5() {{{ */
  250.     // Calculate HMAC according to RFC2104
  251.     // http://www.ietf.org/rfc/rfc2104.txt
  252.     function hmac_md5($data$key{
  253.         if (strlen($key)>64{
  254.             $key md5($keytrue);
  255.         }
  256.         $key  str_pad($key64chr(0));
  257.         $ipad str_repeat(chr(0x36)64);
  258.         $opad str_repeat(chr(0x5c)64);
  259.         return md5(($key^$opadmd5(($key^$ipad$datatrue)true);
  260.     }
  261.     /* }}} */
  262. }
  263. /* }}} */
  264. /* VIM settings {{{
  265.  * Local variables:
  266.  * tab-width: 4
  267.  * c-basic-offset: 4
  268.  * soft-stop-width: 4
  269.  * c indent on
  270.  * expandtab on
  271.  * End:
  272.  * vim600: sw=4 ts=4 sts=4 cindent fdm=marker et
  273.  * vim<600: sw=4 ts=4
  274.  * }}} */
  275. ?>

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