PageRenderTime 36ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bundled-libs/Net/DNS2/RR/TLSA.php

http://github.com/s9y/Serendipity
PHP | 194 lines | 45 code | 20 blank | 129 comment | 2 complexity | 501b17ab5d0c356d02773c62c69748ea MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  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) 2012, 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 2012 Mike Pultz <mike@mikepultz.com>
  44. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  45. * @version SVN: $Id$
  46. * @link http://pear.php.net/package/Net_DNS2
  47. * @since File available since Release 1.2.5
  48. *
  49. */
  50. /**
  51. * TLSA Resource Record - RFC 6698
  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. * | Cert. Usage | Selector | Matching Type | /
  56. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /
  57. * / /
  58. * / Certificate Association Data /
  59. * / /
  60. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  61. *
  62. * @category Networking
  63. * @package Net_DNS2
  64. * @author Mike Pultz <mike@mikepultz.com>
  65. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  66. * @link http://pear.php.net/package/Net_DNS2
  67. * @see Net_DNS2_RR
  68. *
  69. */
  70. class Net_DNS2_RR_TLSA extends Net_DNS2_RR
  71. {
  72. /*
  73. * The Certificate Usage Field
  74. */
  75. public $cert_usage;
  76. /*
  77. * The Selector Field
  78. */
  79. public $selector;
  80. /*
  81. * The Matching Type Field
  82. */
  83. public $matching_type;
  84. /*
  85. * The Certificate Association Data Field
  86. */
  87. public $certificate;
  88. /**
  89. * method to return the rdata portion of the packet as a string
  90. *
  91. * @return string
  92. * @access protected
  93. *
  94. */
  95. protected function rrToString()
  96. {
  97. return $this->cert_usage . ' ' . $this->selector . ' ' .
  98. $this->matching_type . ' ' . base64_encode($this->certificate);
  99. }
  100. /**
  101. * parses the rdata portion from a standard DNS config line
  102. *
  103. * @param array $rdata a string split line of values for the rdata
  104. *
  105. * @return boolean
  106. * @access protected
  107. *
  108. */
  109. protected function rrFromString(array $rdata)
  110. {
  111. $this->cert_usage = array_shift($rdata);
  112. $this->selector = array_shift($rdata);
  113. $this->matching_type = array_shift($rdata);
  114. $this->certificate = base64_decode(implode('', $rdata));
  115. return true;
  116. }
  117. /**
  118. * parses the rdata of the Net_DNS2_Packet object
  119. *
  120. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
  121. *
  122. * @return boolean
  123. * @access protected
  124. *
  125. */
  126. protected function rrSet(Net_DNS2_Packet &$packet)
  127. {
  128. if ($this->rdlength > 0) {
  129. //
  130. // unpack the format, keytag and algorithm
  131. //
  132. $x = unpack('Cusage/Cselector/Ctype', $this->rdata);
  133. $this->cert_usage = $x['usage'];
  134. $this->selector = $x['selector'];
  135. $this->matching_type = $x['type'];
  136. //
  137. // copy the certificate
  138. //
  139. $this->certificate = substr($this->rdata, 3, $this->rdlength - 3);
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * returns the rdata portion of the DNS packet
  146. *
  147. * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
  148. * compressed names
  149. *
  150. * @return mixed either returns a binary packed
  151. * string or null on failure
  152. * @access protected
  153. *
  154. */
  155. protected function rrGet(Net_DNS2_Packet &$packet)
  156. {
  157. if (strlen($this->certificate) > 0) {
  158. $data = pack(
  159. 'CCC', $this->cert_usage, $this->selector, $this->matching_type
  160. ) . $this->certificate;
  161. $packet->offset += strlen($data);
  162. return $data;
  163. }
  164. return null;
  165. }
  166. }
  167. /*
  168. * Local variables:
  169. * tab-width: 4
  170. * c-basic-offset: 4
  171. * c-hanging-comment-ender-p: nil
  172. * End:
  173. */
  174. ?>