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