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) 2012, 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 2012 Mike Pultz <mike@mikepultz.com>
45 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
46 * @version   SVN: $Id: TLSA.php 198 2013-05-26 05:05:22Z mike.pultz $
47 * @link      http://pear.php.net/package/Net_DNS2
48 * @since     File available since Release 1.2.5
49 *
50 */
51
52/**
53 * TLSA Resource Record - RFC 6698
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 *  |  Cert. Usage  |   Selector    | Matching Type |               /
58 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+               /
59 *  /                                                               /
60 *  /                 Certificate Association Data                  /
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 */
72class Net_DNS2_RR_TLSA extends Net_DNS2_RR
73{
74    /*
75     * The Certificate Usage Field
76     */
77    public $cert_usage;
78
79    /*
80     * The Selector Field
81     */
82    public $selector;
83
84    /*
85     * The Matching Type Field
86     */
87    public $matching_type;
88
89    /*
90     * The Certificate Association Data Field
91     */
92    public $certificate;
93
94    /**
95     * method to return the rdata portion of the packet as a string
96     *
97     * @return  string
98     * @access  protected
99     *
100     */
101    protected function rrToString()
102    {
103        return $this->cert_usage . ' ' . $this->selector . ' ' .
104            $this->matching_type . ' ' . base64_encode($this->certificate);
105    }
106
107    /**
108     * parses the rdata portion from a standard DNS config line
109     *
110     * @param array $rdata a string split line of values for the rdata
111     *
112     * @return boolean
113     * @access protected
114     *
115     */
116    protected function rrFromString(array $rdata)
117    {
118        $this->cert_usage       = array_shift($rdata);
119        $this->selector         = array_shift($rdata);
120        $this->matching_type    = array_shift($rdata);
121        $this->certificate      = base64_decode(implode('', $rdata));
122
123        return true;
124    }
125
126    /**
127     * parses the rdata of the Net_DNS2_Packet object
128     *
129     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
130     *
131     * @return boolean
132     * @access protected
133     *
134     */
135    protected function rrSet(Net_DNS2_Packet &$packet)
136    {
137        if ($this->rdlength > 0) {
138
139            //
140            // unpack the format, keytag and algorithm
141            //
142            $x = unpack('Cusage/Cselector/Ctype', $this->rdata);
143
144            $this->cert_usage       = $x['usage'];
145            $this->selector         = $x['selector'];
146            $this->matching_type    = $x['type'];
147
148            //
149            // copy the certificate
150            //
151            $this->certificate  = substr($this->rdata, 3, $this->rdlength - 3);
152
153            return true;
154        }
155
156        return false;
157    }
158
159    /**
160     * returns the rdata portion of the DNS packet
161     *
162     * @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
163     *                                 compressed names
164     *
165     * @return mixed                   either returns a binary packed
166     *                                 string or null on failure
167     * @access protected
168     *
169     */
170    protected function rrGet(Net_DNS2_Packet &$packet)
171    {
172        if (strlen($this->certificate) > 0) {
173
174            $data = pack(
175                'CCC', $this->cert_usage, $this->selector, $this->matching_type
176            ) . $this->certificate;
177
178            $packet->offset += strlen($data);
179
180            return $data;
181        }
182
183        return null;
184    }
185}
186
187/*
188 * Local variables:
189 * tab-width: 4
190 * c-basic-offset: 4
191 * c-hanging-comment-ender-p: nil
192 * End:
193 */
194?>
195