PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Net/DNS2/RR/NSEC3.php

http://netdns2.googlecode.com/
PHP | 308 lines | 91 code | 43 blank | 174 comment | 9 complexity | 82d6ccc46627968704471aa265c2c2a7 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * DNS Library for handling lookups and updates.
  5. *
  6. * PHP Version 5
  7. *
  8. * Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. *
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. *
  23. * * Neither the name of Mike Pultz nor the names of his contributors
  24. * may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * @category Networking
  41. * @package Net_DNS2
  42. * @author Mike Pultz <mike@mikepultz.com>
  43. * @copyright 2010 Mike Pultz <mike@mikepultz.com>
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id: NSEC3.php 125 2011-12-03 00:19:49Z mike.pultz $
  46. * @link http://pear.php.net/package/Net_DNS2
  47. * @since File available since Release 0.6.0
  48. *
  49. */
  50. /**
  51. * NSEC3 Resource Record - RFC5155 section 3.2
  52. *
  53. * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  54. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  55. * | Hash Alg. | Flags | Iterations |
  56. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  57. * | Salt Length | Salt /
  58. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. * | Hash Length | Next Hashed Owner Name /
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. * / Type Bit Maps /
  62. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  63. *
  64. * @category Networking
  65. * @package Net_DNS2
  66. * @author Mike Pultz <mike@mikepultz.com>
  67. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  68. * @link http://pear.php.net/package/Net_DNS2
  69. * @see Net_DNS2_RR
  70. *
  71. */
  72. class Net_DNS2_RR_NSEC3 extends Net_DNS2_RR
  73. {
  74. /*
  75. * Algorithm to use
  76. */
  77. public $algorithm;
  78. /*
  79. * flags
  80. */
  81. public $flags;
  82. /*
  83. * defines the number of additional times the hash is performed.
  84. */
  85. public $iterations;
  86. /*
  87. * the length of the salt- not displayed
  88. */
  89. public $salt_length;
  90. /*
  91. * the salt
  92. */
  93. public $salt;
  94. /*
  95. * the length of the hash value
  96. */
  97. public $hash_length;
  98. /*
  99. * the hashed value of the owner name
  100. */
  101. public $hashed_owner_name;
  102. /*
  103. * array of RR type names
  104. */
  105. public $type_bit_maps = array();
  106. /**
  107. * method to return the rdata portion of the packet as a string
  108. *
  109. * @return string
  110. * @access protected
  111. *
  112. */
  113. protected function rrToString()
  114. {
  115. $out = $this->algorithm . ' ' . $this->flags . ' ' . $this->iterations . ' ';
  116. //
  117. // per RFC5155, the salt_length value isn't displayed, and if the salt
  118. // is empty, the salt is displayed as '-'
  119. //
  120. if ($this->salt_length > 0) {
  121. $out .= $this->salt;
  122. } else {
  123. $out .= '-';
  124. }
  125. //
  126. // per RFC5255 the hash length isn't shown
  127. //
  128. $out .= ' ' . $this->hashed_owner_name;
  129. //
  130. // show the RR's
  131. //
  132. foreach ($this->type_bit_maps as $rr) {
  133. $out .= ' ' . strtoupper($rr);
  134. }
  135. return $out;
  136. }
  137. /**
  138. * parses the rdata portion from a standard DNS config line
  139. *
  140. * @param array $rdata a string split line of values for the rdata
  141. *
  142. * @return boolean
  143. * @access protected
  144. *
  145. */
  146. protected function rrFromString(array $rdata)
  147. {
  148. $this->algorithm = array_shift($rdata);
  149. $this->flags = array_shift($rdata);
  150. $this->iterations = array_shift($rdata);
  151. //
  152. // an empty salt is represented as '-' per RFC5155 section 3.3
  153. //
  154. $salt = array_shift($rdata);
  155. if ($salt == '-') {
  156. $this->salt_length = 0;
  157. $this->salt = '';
  158. } else {
  159. $this->salt_length = strlen(pack('H*', $salt));
  160. $this->salt = strtoupper($salt);
  161. }
  162. $this->hashed_owner_name = array_shift($rdata);
  163. $this->hash_length = strlen(base64_decode($this->hashed_owner_name));
  164. $this->type_bit_maps = $rdata;
  165. return true;
  166. }
  167. /**
  168. * parses the rdata of the Net_DNS2_Packet object
  169. *
  170. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  171. *
  172. * @return boolean
  173. * @access protected
  174. *
  175. */
  176. protected function rrSet(Net_DNS2_Packet &$packet)
  177. {
  178. if ($this->rdlength > 0) {
  179. //
  180. // unpack the first values
  181. //
  182. $x = unpack('Calgorithm/Cflags/niterations/Csalt_length', $this->rdata);
  183. $this->algorithm = $x['algorithm'];
  184. $this->flags = $x['flags'];
  185. $this->iterations = $x['iterations'];
  186. $this->salt_length = $x['salt_length'];
  187. $offset = 5;
  188. if ($this->salt_length > 0) {
  189. $x = unpack('H*', substr($this->rdata, $offset, $this->salt_length));
  190. $this->salt = strtoupper($x[1]);
  191. $offset += $this->salt_length;
  192. }
  193. //
  194. // unpack the hash length
  195. //
  196. $x = unpack('@' . $offset . '/Chash_length', $this->rdata);
  197. $offset++;
  198. //
  199. // copy out the hash
  200. //
  201. $this->hash_length = $x['hash_length'];
  202. if ($this->hash_length > 0) {
  203. $this->hashed_owner_name = base64_encode(
  204. substr($this->rdata, $offset, $this->hash_length)
  205. );
  206. $offset += $this->hash_length;
  207. }
  208. //
  209. // parse out the RR bitmap
  210. //
  211. $this->type_bit_maps = Net_DNS2_BitMap::bitMapToArray(
  212. substr($this->rdata, $offset)
  213. );
  214. return true;
  215. }
  216. return false;
  217. }
  218. /**
  219. * returns the rdata portion of the DNS packet
  220. *
  221. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
  222. * compressed names
  223. *
  224. * @return mixed either returns a binary packed
  225. * string or null on failure
  226. * @access protected
  227. *
  228. */
  229. protected function rrGet(Net_DNS2_Packet &$packet)
  230. {
  231. //
  232. // pull the salt and build the length
  233. //
  234. $salt = pack('H*', $this->salt);
  235. $this->salt_length = strlen($salt);
  236. //
  237. // pack the algorithm, flags, iterations and salt length
  238. //
  239. $data = pack(
  240. 'CCnC',
  241. $this->algorithm, $this->flags, $this->iterations, $this->salt_length
  242. );
  243. $data .= $salt;
  244. //
  245. // add the hash length and hash
  246. //
  247. $data .= chr($this->hash_length);
  248. if ($this->hash_length > 0) {
  249. $data .= base64_decode($this->hashed_owner_name);
  250. }
  251. //
  252. // conver the array of RR names to a type bitmap
  253. //
  254. $data .= Net_DNS2_BitMap::arrayToBitMap($this->type_bit_maps);
  255. return $data;
  256. }
  257. }
  258. /*
  259. * Local variables:
  260. * tab-width: 4
  261. * c-basic-offset: 4
  262. * c-hanging-comment-ender-p: nil
  263. * End:
  264. */
  265. ?>