/usr/share/php/Net/DNS2/RR/TSIG.php is in php-net-dns2 1.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | <?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* DNS Library for handling lookups and updates.
*
* PHP Version 5
*
* Copyright (c) 2010, Mike Pultz <mike@mikepultz.com>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Mike Pultz nor the names of his contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @copyright 2010 Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @version SVN: $Id: TSIG.php 198 2013-05-26 05:05:22Z mike.pultz $
* @link http://pear.php.net/package/Net_DNS2
* @since File available since Release 0.6.0
*
*/
/**
* TSIG Resource Record - RFC 2845
*
* 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* / algorithm /
* / /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | time signed |
* | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | | fudge |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | mac size | /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /
* / mac /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | original id | error |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | other length | /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ /
* / other data /
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*
* @category Networking
* @package Net_DNS2
* @author Mike Pultz <mike@mikepultz.com>
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link http://pear.php.net/package/Net_DNS2
* @see Net_DNS2_RR
*
*/
class Net_DNS2_RR_TSIG extends Net_DNS2_RR
{
/*
* TSIG Algorithm Identifiers
*/
const HMAC_MD5 = 'hmac-md5.sig-alg.reg.int'; // RFC 2845, required
const GSS_TSIG = 'gss-tsig'; // unsupported, optional
const HMAC_SHA1 = 'hmac-sha1'; // RFC 4635, required
const HMAC_SHA224 = 'hmac-sha224'; // RFC 4635, optional
const HMAC_SHA256 = 'hmac-sha256'; // RFC 4635, required
const HMAC_SHA384 = 'hmac-sha384'; // RFC 4635, optional
const HMAC_SHA512 = 'hmac-sha512'; // RFC 4635, optional
/*
* the map of hash values to names
*/
public static $hash_algorithms = array(
self::HMAC_MD5 => 'md5',
self::HMAC_SHA1 => 'sha1',
self::HMAC_SHA224 => 'sha224',
self::HMAC_SHA256 => 'sha256',
self::HMAC_SHA384 => 'sha384',
self::HMAC_SHA512 => 'sha512'
);
/*
* algorithm used; only supports HMAC-MD5
*/
public $algorithm;
/*
* The time it was signed
*/
public $time_signed;
/*
* fudge- allowed offset from the time signed
*/
public $fudge;
/*
* size of the digest
*/
public $mac_size;
/*
* the digest data
*/
public $mac;
/*
* the original id of the request
*/
public $original_id;
/*
* additional error code
*/
public $error;
/*
* length of the "other" data, should only ever be 0 when there is
* no error, or 6 when there is the error RCODE_BADTIME
*/
public $other_length;
/*
* the other data; should only ever be a timestamp when there is the
* error RCODE_BADTIME
*/
public $other_data;
/*
* the key to use for signing - passed in, not included in the rdata
*/
public $key;
/**
* method to return the rdata portion of the packet as a string
*
* @return string
* @access protected
*
*/
protected function rrToString()
{
$out = $this->cleanString($this->algorithm) . '. ' .
$this->time_signed . ' ' .
$this->fudge . ' ' . $this->mac_size . ' ' .
base64_encode($this->mac) . ' ' . $this->original_id . ' ' .
$this->error . ' '. $this->other_length;
if ($this->other_length > 0) {
$out .= ' ' . $this->other_data;
}
return $out;
}
/**
* parses the rdata portion from a standard DNS config line
*
* @param array $rdata a string split line of values for the rdata
*
* @return boolean
* @access protected
*
*/
protected function rrFromString(array $rdata)
{
//
// the only value passed in is the key-
//
// this assumes it's passed in base64 encoded.
//
$this->key = preg_replace('/\s+/', '', array_shift($rdata));
//
// the rest of the data is set to default
//
$this->algorithm = self::HMAC_MD5;
$this->time_signed = time();
$this->fudge = 300;
$this->mac_size = 0;
$this->mac = '';
$this->original_id = 0;
$this->error = 0;
$this->other_length = 0;
$this->other_data = '';
//
// per RFC 2845 section 2.3
//
$this->class = 'ANY';
$this->ttl = 0;
return true;
}
/**
* parses the rdata of the Net_DNS2_Packet object
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet to parse the RR from
*
* @return boolean
* @access protected
*
*/
protected function rrSet(Net_DNS2_Packet &$packet)
{
if ($this->rdlength > 0) {
//
// expand the algorithm
//
$newoffset = $packet->offset;
$this->algorithm = Net_DNS2_Packet::expand($packet, $newoffset);
$offset = $newoffset - $packet->offset;
//
// unpack time, fudge and mac_size
//
$x = unpack(
'@' . $offset . '/ntime_high/Ntime_low/nfudge/nmac_size',
$this->rdata
);
$this->time_signed = Net_DNS2::expandUint32($x['time_low']);
$this->fudge = $x['fudge'];
$this->mac_size = $x['mac_size'];
$offset += 10;
//
// copy out the mac
//
if ($this->mac_size > 0) {
$this->mac = substr($this->rdata, $offset, $this->mac_size);
$offset += $this->mac_size;
}
//
// unpack the original id, error, and other_length values
//
$x = unpack(
'@' . $offset . '/noriginal_id/nerror/nother_length',
$this->rdata
);
$this->original_id = $x['original_id'];
$this->error = $x['error'];
$this->other_length = $x['other_length'];
//
// the only time there is actually any "other data", is when there's
// a BADTIME error code.
//
// The other length should be 6, and the other data field includes the
// servers current time - per RFC 2845 section 4.5.2
//
if ($this->error == Net_DNS2_Lookups::RCODE_BADTIME) {
if ($this->other_length != 6) {
return false;
}
//
// other data is a 48bit timestamp
//
$x = unpack(
'nhigh/nlow',
substr($this->rdata, $offset + 6, $this->other_length)
);
$this->other_data = $x['low'];
}
return true;
}
return false;
}
/**
* returns the rdata portion of the DNS packet
*
* @param Net_DNS2_Packet &$packet a Net_DNS2_Packet packet use for
* compressed names
*
* @return mixed either returns a binary packed
* string or null on failure
* @access protected
*
*/
protected function rrGet(Net_DNS2_Packet &$packet)
{
if (strlen($this->key) > 0) {
//
// create a new packet for the signature-
//
$new_packet = new Net_DNS2_Packet_Request('example.com', 'SOA', 'IN');
//
// copy the packet data over
//
$new_packet->copy($packet);
//
// remove the TSIG object from the additional list
//
array_pop($new_packet->additional);
$new_packet->header->arcount = count($new_packet->additional);
//
// copy out the data
//
$sig_data = $new_packet->get();
//
// add the name without compressing
//
$sig_data .= Net_DNS2_Packet::pack($this->name);
//
// add the class and TTL
//
$sig_data .= pack(
'nN', Net_DNS2_Lookups::$classes_by_name[$this->class], $this->ttl
);
//
// add the algorithm name without compression
//
$sig_data .= Net_DNS2_Packet::pack(strtolower($this->algorithm));
//
// add the rest of the values
//
$sig_data .= pack(
'nNnnn', 0, $this->time_signed, $this->fudge,
$this->error, $this->other_length
);
if ($this->other_length > 0) {
$sig_data .= pack('nN', 0, $this->other_data);
}
//
// sign the data
//
$this->mac = $this->_signHMAC(
$sig_data, base64_decode($this->key), $this->algorithm
);
$this->mac_size = strlen($this->mac);
//
// compress the algorithm
//
$data = Net_DNS2_Packet::pack(strtolower($this->algorithm));
//
// pack the time, fudge and mac size
//
$data .= pack(
'nNnn', 0, $this->time_signed, $this->fudge, $this->mac_size
);
$data .= $this->mac;
//
// check the error and other_length
//
if ($this->error == Net_DNS2_Lookups::RCODE_BADTIME) {
$this->other_length = strlen($this->other_data);
if ($this->other_length != 6) {
return null;
}
} else {
$this->other_length = 0;
$this->other_data = '';
}
//
// pack the id, error and other_length
//
$data .= pack(
'nnn', $packet->header->id, $this->error, $this->other_length
);
if ($this->other_length > 0) {
$data .= pack('nN', 0, $this->other_data);
}
$packet->offset += strlen($data);
return $data;
}
return null;
}
/**
* signs the given data with the given key, and returns the result
*
* @param string $data the data to sign
* @param string $key key to use for signing
* @param string $algorithm the algorithm to use; defaults to MD5
*
* @return string the signed digest
* @throws Net_DNS2_Exception
* @access private
*
*/
private function _signHMAC($data, $key = null, $algorithm = self::HMAC_MD5)
{
//
// use the hash extension; this is included by default in >= 5.1.2 which
// is our dependent version anyway- so it's easy to switch to it.
//
if (extension_loaded('hash')) {
if (!isset(self::$hash_algorithms[$algorithm])) {
throw new Net_DNS2_Exception(
'invalid or unsupported algorithm',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
return hash_hmac(self::$hash_algorithms[$algorithm], $data, $key, true);
}
//
// if the hash extension isn't loaded, and they selected something other
// than MD5, throw an exception
//
if ($algorithm != self::HMAC_MD5) {
throw new Net_DNS2_Exception(
'only HMAC-MD5 supported. please install the php-extension ' .
'"hash" in order to use the sha-family',
Net_DNS2_Lookups::E_PARSE_ERROR
);
}
//
// otherwise, do it ourselves
//
if (is_null($key)) {
return pack('H*', md5($data));
}
$key = str_pad($key, 64, chr(0x00));
if (strlen($key) > 64) {
$key = pack('H*', md5($key));
}
$k_ipad = $key ^ str_repeat(chr(0x36), 64);
$k_opad = $key ^ str_repeat(chr(0x5c), 64);
return $this->_signHMAC(
$k_opad . pack('H*', md5($k_ipad . $data)), null, $algorithm
);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* c-hanging-comment-ender-p: nil
* End:
*/
?>
|