~ubuntu-branches/ubuntu/utopic/php-net-dns2/utopic-proposed

« back to all changes in this revision

Viewing changes to Net_DNS2-1.3.2/Net/DNS2/RR/NSEC3.php

  • Committer: Package Import Robot
  • Author(s): Mathieu Parent
  • Date: 2014-02-21 16:59:58 UTC
  • Revision ID: package-import@ubuntu.com-20140221165958-tyc92gchfhho9660
Tags: upstream-1.3.2
ImportĀ upstreamĀ versionĀ 1.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: NSEC3.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
 * NSEC3 Resource Record - RFC5155 section 3.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
 *  |  Hash Length  |             Next Hashed Owner Name            /
 
62
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
63
 *  /                         Type Bit Maps                         /
 
64
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
65
 * 
 
66
 * @category Networking
 
67
 * @package  Net_DNS2
 
68
 * @author   Mike Pultz <mike@mikepultz.com>
 
69
 * @license  http://www.opensource.org/licenses/bsd-license.php  BSD License
 
70
 * @link     http://pear.php.net/package/Net_DNS2
 
71
 * @see      Net_DNS2_RR
 
72
 *
 
73
 */
 
74
class Net_DNS2_RR_NSEC3 extends Net_DNS2_RR
 
75
{
 
76
    /*
 
77
     * Algorithm to use
 
78
     */
 
79
    public $algorithm;
 
80
 
 
81
    /*
 
82
     * flags
 
83
     */
 
84
    public $flags;
 
85
 
 
86
    /*
 
87
     *  defines the number of additional times the hash is performed.
 
88
     */
 
89
    public $iterations;
 
90
 
 
91
    /*
 
92
     * the length of the salt- not displayed
 
93
     */
 
94
    public $salt_length;
 
95
 
 
96
    /*
 
97
     * the salt
 
98
     */
 
99
    public $salt;
 
100
 
 
101
    /*
 
102
     * the length of the hash value
 
103
     */
 
104
    public $hash_length;
 
105
 
 
106
    /*
 
107
     * the hashed value of the owner name
 
108
     */
 
109
    public $hashed_owner_name;
 
110
 
 
111
    /*
 
112
     * array of RR type names
 
113
     */
 
114
    public $type_bit_maps = array();
 
115
 
 
116
    /**
 
117
     * method to return the rdata portion of the packet as a string
 
118
     *
 
119
     * @return  string
 
120
     * @access  protected
 
121
     *
 
122
     */
 
123
    protected function rrToString()
 
124
    {
 
125
        $out = $this->algorithm . ' ' . $this->flags . ' ' . $this->iterations . ' ';
 
126
 
 
127
        //
 
128
        // per RFC5155, the salt_length value isn't displayed, and if the salt
 
129
        // is empty, the salt is displayed as '-'
 
130
        //
 
131
        if ($this->salt_length > 0) {
 
132
  
 
133
            $out .= $this->salt;
 
134
        } else {     
 
135
 
 
136
            $out .= '-';
 
137
        }
 
138
 
 
139
        //
 
140
        // per RFC5255 the hash length isn't shown
 
141
        //
 
142
        $out .= ' ' . $this->hashed_owner_name;
 
143
 
 
144
        //
 
145
        // show the RR's
 
146
        //
 
147
        foreach ($this->type_bit_maps as $rr) {
 
148
    
 
149
            $out .= ' ' . strtoupper($rr);
 
150
        }
 
151
 
 
152
        return $out;
 
153
    }
 
154
 
 
155
    /**
 
156
     * parses the rdata portion from a standard DNS config line
 
157
     *
 
158
     * @param array $rdata a string split line of values for the rdata
 
159
     *
 
160
     * @return boolean
 
161
     * @access protected
 
162
     *
 
163
     */
 
164
    protected function rrFromString(array $rdata)
 
165
    {
 
166
        $this->algorithm    = array_shift($rdata);
 
167
        $this->flags        = array_shift($rdata);
 
168
        $this->iterations   = array_shift($rdata);
 
169
     
 
170
        //
 
171
        // an empty salt is represented as '-' per RFC5155 section 3.3
 
172
        //
 
173
        $salt = array_shift($rdata);
 
174
        if ($salt == '-') {
 
175
 
 
176
            $this->salt_length = 0;
 
177
            $this->salt = '';
 
178
        } else {
 
179
    
 
180
            $this->salt_length = strlen(pack('H*', $salt));
 
181
            $this->salt = strtoupper($salt);
 
182
        }
 
183
 
 
184
        $this->hashed_owner_name = array_shift($rdata);
 
185
        $this->hash_length = strlen(base64_decode($this->hashed_owner_name));
 
186
 
 
187
        $this->type_bit_maps = $rdata;
 
188
 
 
189
        return true;
 
190
    }
 
191
 
 
192
    /**
 
193
     * parses the rdata of the Net_DNS2_Packet object
 
194
     *
 
195
     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
 
196
     *
 
197
     * @return boolean
 
198
     * @access protected
 
199
     *
 
200
     */
 
201
    protected function rrSet(Net_DNS2_Packet &$packet)
 
202
    {
 
203
        if ($this->rdlength > 0) {
 
204
        
 
205
            //
 
206
            // unpack the first values
 
207
            //
 
208
            $x = unpack('Calgorithm/Cflags/niterations/Csalt_length', $this->rdata);
 
209
        
 
210
            $this->algorithm    = $x['algorithm'];
 
211
            $this->flags        = $x['flags'];
 
212
            $this->iterations   = $x['iterations'];
 
213
            $this->salt_length  = $x['salt_length'];
 
214
 
 
215
            $offset = 5;
 
216
 
 
217
            if ($this->salt_length > 0) {
 
218
 
 
219
                $x = unpack('H*', substr($this->rdata, $offset, $this->salt_length));
 
220
                $this->salt = strtoupper($x[1]);
 
221
                $offset += $this->salt_length;
 
222
            }
 
223
 
 
224
            //
 
225
            // unpack the hash length
 
226
            //
 
227
            $x = unpack('@' . $offset . '/Chash_length', $this->rdata);
 
228
            $offset++;
 
229
 
 
230
            //
 
231
            // copy out the hash
 
232
            //
 
233
            $this->hash_length  = $x['hash_length'];
 
234
            if ($this->hash_length > 0) {
 
235
 
 
236
                $this->hashed_owner_name = base64_encode(
 
237
                    substr($this->rdata, $offset, $this->hash_length)
 
238
                );
 
239
                $offset += $this->hash_length;
 
240
            }
 
241
 
 
242
            //
 
243
            // parse out the RR bitmap
 
244
            //
 
245
            $this->type_bit_maps = Net_DNS2_BitMap::bitMapToArray(
 
246
                substr($this->rdata, $offset)
 
247
            );
 
248
 
 
249
            return true;
 
250
        }
 
251
     
 
252
        return false;
 
253
    }
 
254
 
 
255
    /**
 
256
     * returns the rdata portion of the DNS packet
 
257
     *
 
258
     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
 
259
     *                                 compressed names
 
260
     *
 
261
     * @return mixed                   either returns a binary packed
 
262
     *                                 string or null on failure
 
263
     * @access protected
 
264
     *
 
265
     */
 
266
    protected function rrGet(Net_DNS2_Packet &$packet)
 
267
    {
 
268
        //
 
269
        // pull the salt and build the length
 
270
        //
 
271
        $salt = pack('H*', $this->salt);
 
272
        $this->salt_length = strlen($salt);
 
273
            
 
274
        //
 
275
        // pack the algorithm, flags, iterations and salt length
 
276
        //
 
277
        $data = pack(
 
278
            'CCnC',
 
279
            $this->algorithm, $this->flags, $this->iterations, $this->salt_length
 
280
        );
 
281
        $data .= $salt;
 
282
 
 
283
        //
 
284
        // add the hash length and hash
 
285
        //
 
286
        $data .= chr($this->hash_length);
 
287
        if ($this->hash_length > 0) {
 
288
 
 
289
            $data .= base64_decode($this->hashed_owner_name);
 
290
        }
 
291
 
 
292
        //
 
293
        // conver the array of RR names to a type bitmap
 
294
        //
 
295
        $data .= Net_DNS2_BitMap::arrayToBitMap($this->type_bit_maps);
 
296
 
 
297
        $packet->offset += strlen($data);
 
298
     
 
299
        return $data;
 
300
    }
 
301
}
 
302
 
 
303
/*
 
304
 * Local variables:
 
305
 * tab-width: 4
 
306
 * c-basic-offset: 4
 
307
 * c-hanging-comment-ender-p: nil
 
308
 * End:
 
309
 */
 
310
?>