PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Net/DNS2/RR/IPSECKEY.php

http://netdns2.googlecode.com/
PHP | 384 lines | 161 code | 51 blank | 172 comment | 8 complexity | dfed1396c5ff9d914bed8fa766a621f7 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: IPSECKEY.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. * IPSECKEY Resource Record - RFC4025 section 2.1
  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. * | precedence | gateway type | algorithm | gateway |
  56. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-------------+ +
  57. * ~ gateway ~
  58. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  59. * | /
  60. * / public key /
  61. * / /
  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_IPSECKEY extends Net_DNS2_RR
  73. {
  74. const GATEWAY_TYPE_NONE = 0;
  75. const GATEWAY_TYPE_IPV4 = 1;
  76. const GATEWAY_TYPE_IPV6 = 2;
  77. const GATEWAY_TYPE_DOMAIN = 3;
  78. const ALGORITHM_NONE = 0;
  79. const ALGORITHM_DSA = 1;
  80. const ALGORITHM_RSA = 2;
  81. /*
  82. * Precedence (used the same was as a preference field)
  83. */
  84. public $precedence;
  85. /*
  86. * Gateway type - specifies the format of the gataway information
  87. * This can be either:
  88. *
  89. * 0 No Gateway
  90. * 1 IPv4 address
  91. * 2 IPV6 address
  92. * 3 wire-encoded domain name (not compressed)
  93. *
  94. */
  95. public $gateway_type;
  96. /*
  97. * The algorithm used
  98. *
  99. * This can be:
  100. *
  101. * 0 No key is present
  102. * 1 DSA key is present
  103. * 2 RSA key is present
  104. *
  105. */
  106. public $algorithm;
  107. /*
  108. * The gatway information
  109. */
  110. public $gateway;
  111. /*
  112. * the public key
  113. */
  114. public $key;
  115. /**
  116. * method to return the rdata portion of the packet as a string
  117. *
  118. * @return string
  119. * @access protected
  120. *
  121. */
  122. protected function rrToString()
  123. {
  124. $out = $this->precedence . ' ' . $this->gateway_type . ' ' .
  125. $this->algorithm . ' ';
  126. switch($this->gateway_type) {
  127. case self::GATEWAY_TYPE_NONE:
  128. $out .= '. ';
  129. break;
  130. case self::GATEWAY_TYPE_IPV4:
  131. case self::GATEWAY_TYPE_IPV6:
  132. $out .= $this->gateway . ' ';
  133. break;
  134. case self::GATEWAY_TYPE_DOMAIN:
  135. $out .= $this->gateway . '. ';
  136. break;
  137. }
  138. $out .= $this->key;
  139. return $out;
  140. }
  141. /**
  142. * parses the rdata portion from a standard DNS config line
  143. *
  144. * @param array $rdata a string split line of values for the rdata
  145. *
  146. * @return boolean
  147. * @access protected
  148. *
  149. */
  150. protected function rrFromString(array $rdata)
  151. {
  152. //
  153. // load the data
  154. //
  155. $precedence = array_shift($rdata);
  156. $gateway_type = array_shift($rdata);
  157. $algorithm = array_shift($rdata);
  158. $gateway = strtolower(trim(array_shift($rdata)));
  159. $key = array_shift($rdata);
  160. //
  161. // validate it
  162. //
  163. switch($gateway_type) {
  164. case self::GATEWAY_TYPE_NONE:
  165. $gateway = '';
  166. break;
  167. case self::GATEWAY_TYPE_IPV4:
  168. if (Net_DNS2::isIPv4($gateway) == false) {
  169. return false;
  170. }
  171. break;
  172. case self::GATEWAY_TYPE_IPV6:
  173. if (Net_DNS2::isIPv6($gateway) == false) {
  174. return false;
  175. }
  176. break;
  177. case self::GATEWAY_TYPE_DOMAIN:
  178. ; // do nothing
  179. break;
  180. default:
  181. return false;
  182. }
  183. //
  184. // check the algorithm and key
  185. //
  186. switch($algorithm) {
  187. case self::ALGORITHM_NONE:
  188. $key = '';
  189. break;
  190. case self::ALGORITHM_DSA:
  191. case self::ALGORITHM_RSA:
  192. ; // do nothing
  193. break;
  194. default:
  195. return false;
  196. }
  197. //
  198. // store the values
  199. //
  200. $this->precedence = $precedence;
  201. $this->gateway_type = $gateway_type;
  202. $this->algorithm = $algorithm;
  203. $this->gateway = $gateway;
  204. $this->key = $key;
  205. return true;
  206. }
  207. /**
  208. * parses the rdata of the Net_DNS2_Packet object
  209. *
  210. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  211. *
  212. * @return boolean
  213. * @access protected
  214. *
  215. */
  216. protected function rrSet(Net_DNS2_Packet &$packet)
  217. {
  218. if ($this->rdlength > 0) {
  219. //
  220. // parse off the precedence, gateway type and algorithm
  221. //
  222. $x = unpack('Cprecedence/Cgateway_type/Calgorithm', $this->rdata);
  223. $this->precedence = $x['precedence'];
  224. $this->gateway_type = $x['gateway_type'];
  225. $this->algorithm = $x['algorithm'];
  226. $offset = 3;
  227. //
  228. // extract the gatway based on the type
  229. //
  230. switch($this->gateway_type) {
  231. case self::GATEWAY_TYPE_NONE:
  232. $this->gateway = '';
  233. break;
  234. case self::GATEWAY_TYPE_IPV4:
  235. $this->gateway = inet_ntop(substr($this->rdata, $offset, 4));
  236. $offset += 4;
  237. break;
  238. case self::GATEWAY_TYPE_IPV6:
  239. $ip = unpack('n8', substr($this->rdata, $offset, 16));
  240. if (count($ip) == 8) {
  241. $this->gateway = vsprintf('%x:%x:%x:%x:%x:%x:%x:%x', $ip);
  242. $offset += 16;
  243. } else {
  244. return false;
  245. }
  246. break;
  247. case self::GATEWAY_TYPE_DOMAIN:
  248. $doffset = $offset + $packet->offset;
  249. $this->gateway = Net_DNS2_Packet::expand($packet, $doffset);
  250. $offset = ($doffset - $packet->offset);
  251. break;
  252. default:
  253. return false;
  254. }
  255. //
  256. // extract the key
  257. //
  258. switch($this->algorithm) {
  259. case self::ALGORITHM_NONE:
  260. $this->key = '';
  261. break;
  262. case self::ALGORITHM_DSA:
  263. case self::ALGORITHM_RSA:
  264. $this->key = base64_encode(substr($this->rdata, $offset));
  265. break;
  266. default:
  267. return false;
  268. }
  269. return true;
  270. }
  271. return false;
  272. }
  273. /**
  274. * returns the rdata portion of the DNS packet
  275. *
  276. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
  277. * compressed names
  278. *
  279. * @return mixed either returns a binary packed
  280. * string or null on failure
  281. * @access protected
  282. *
  283. */
  284. protected function rrGet(Net_DNS2_Packet &$packet)
  285. {
  286. //
  287. // pack the precedence, gateway type and algorithm
  288. //
  289. $data = pack(
  290. 'CCC', $this->precedence, $this->gateway_type, $this->algorithm
  291. );
  292. //
  293. // add the gateway based on the type
  294. //
  295. switch($this->gateway_type) {
  296. case self::GATEWAY_TYPE_NONE:
  297. ; // add nothing
  298. break;
  299. case self::GATEWAY_TYPE_IPV4:
  300. case self::GATEWAY_TYPE_IPV6:
  301. $data .= inet_pton($this->gateway);
  302. break;
  303. case self::GATEWAY_TYPE_DOMAIN:
  304. $data .= chr(strlen($this->gateway)) . $this->gateway;
  305. break;
  306. default:
  307. return null;
  308. }
  309. //
  310. // add the key if there's one specified
  311. //
  312. switch($this->algorithm) {
  313. case self::ALGORITHM_NONE:
  314. ; // add nothing
  315. break;
  316. case self::ALGORITHM_DSA:
  317. case self::ALGORITHM_RSA:
  318. $data .= base64_decode($this->key);
  319. break;
  320. default:
  321. return null;
  322. }
  323. return $data;
  324. }
  325. }
  326. /*
  327. * Local variables:
  328. * tab-width: 4
  329. * c-basic-offset: 4
  330. * c-hanging-comment-ender-p: nil
  331. * End:
  332. */
  333. ?>