Skip to content

Commit

Permalink
- fixed a bug related to canonicalization of names in certain cases i…
Browse files Browse the repository at this point in the history
…ncluding TSIG
  • Loading branch information
mikepultz committed Oct 1, 2023
1 parent ecc4d91 commit 7d304fa
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 40 deletions.
15 changes: 11 additions & 4 deletions Net/DNS2/BitMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ public static function arrayToBitMap(array $data)
//
// get the type id for the RR
//
$type = @Net_DNS2_Lookups::$rr_types_by_name[$rr];
if (isset($type)) {
$type = null;

if (isset(Net_DNS2_Lookups::$rr_types_by_name[$rr]) == true) {

$type = Net_DNS2_Lookups::$rr_types_by_name[$rr];

//
// skip meta types or qtypes
Expand All @@ -138,8 +141,12 @@ public static function arrayToBitMap(array $data)
// if it's not found, then it must be defined as TYPE<id>, per
// RFC3845 section 2.2, if it's not, we ignore it.
//
list($name, $type) = explode('TYPE', $rr);
if (!isset($type)) {
list($name, $index) = explode('TYPE', $rr);

if ( (strlen($index) > 0) && (is_numeric($index) == true) ) {

$type = $index;
} else {

continue;
}
Expand Down
48 changes: 40 additions & 8 deletions Net/DNS2/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ public static function pack($name)
return (is_null($name) == true) ? null : pack('Ca*', strlen($name), $name);
}

/**
* returns the canonical wire-format representation of the domain name
*
* @param string $name a name to be packed
*
* @return string
* @access public
*
*/
public static function canonical($name)
{
$names = explode('.', $name);
$compname = '';

while (!empty($names)) {

$first = array_shift($names);
$length = strlen($first);

$compname .= pack('Ca*', $length, $first);
}

$compname .= "\0";

return $compname;
}

/**
* parses a domain string into a single string
*
Expand All @@ -49,26 +76,31 @@ public static function pack($name)
*/
public static function unpack($rdata, &$offset)
{
if ($offset > strlen($rdata))
{
return null;
}

$name = '';

if (strlen($rdata) < ($offset + 1))
$len = ord($rdata[$offset]);
if ($len == 0)
{
return null;
}

$offset++;

$xlen = ord($rdata[$offset]);
++$offset;

if (($xlen + $offset) > strlen($rdata)) {
if ( ($len + $offset) > strlen($rdata)) {

$name = substr($rdata, $offset);
$offset = strlen($rdata);
} else {

$name = substr($rdata, $offset, $xlen);
$offset += $xlen;
$name = substr($rdata, $offset, $len);
}

$offset += strlen($name);

return $name;
}
}
35 changes: 13 additions & 22 deletions Net/DNS2/RR.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,31 +578,22 @@ public static function fromString($line)
if (isset($class_name)) {

$o = new $class_name;
if (!is_null($o)) {

//
// set the parsed values
//
$o->name = $name;
$o->class = $class;
$o->ttl = $ttl;

//
// parse the rdata
//
if ($o->rrFromString($values) === false) {

throw new Net_DNS2_Exception(
'failed to parse rdata for config: ' . $line,
Net_DNS2_Lookups::E_PARSE_ERROR
);
}

} else {
//
// set the parsed values
//
$o->name = $name;
$o->class = $class;
$o->ttl = $ttl;

//
// parse the rdata
//
if ($o->rrFromString($values) === false) {

throw new Net_DNS2_Exception(
'failed to create new RR record for type: ' . $type,
Net_DNS2_Lookups::E_RR_INVALID
'failed to parse rdata for config: ' . $line,
Net_DNS2_Lookups::E_PARSE_ERROR
);
}

Expand Down
12 changes: 7 additions & 5 deletions Net/DNS2/RR/TSIG.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ protected function rrSet(Net_DNS2_Packet &$packet)
//
// expand the algorithm
//
$offset = 0;
$this->algorithm = Net_DNS2_Names::unpack($this->rdata, $offset);
$newoffset = $packet->offset;
$this->algorithm = Net_DNS2_Packet::expand($packet, $newoffset);
$offset = $newoffset - $packet->offset;

//
// unpack time, fudge and mac_size
Expand Down Expand Up @@ -305,7 +306,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)
//
// add the name without compressing
//
$sig_data .= Net_DNS2_Names::pack($this->name);
$sig_data .= Net_DNS2_Names::canonical($this->name);

//
// add the class and TTL
Expand All @@ -317,7 +318,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)
//
// add the algorithm name without compression
//
$sig_data .= Net_DNS2_Names::pack(strtolower($this->algorithm));
$sig_data .= Net_DNS2_Names::canonical(strtolower($this->algorithm));

//
// add the rest of the values
Expand All @@ -342,7 +343,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)
//
// compress the algorithm
//
$data = Net_DNS2_Names::pack(strtolower($this->algorithm));
$data = $packet->compress(strtolower($this->algorithm), $offset);

//
// pack the time, fudge and mac size
Expand All @@ -362,6 +363,7 @@ protected function rrGet(Net_DNS2_Packet &$packet)

return null;
}

} else {

$this->other_length = 0;
Expand Down
5 changes: 5 additions & 0 deletions tests/Tests_Net_DNS2_ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ public function testCompression()

$response_authority[$id]->rdlength = '';
$response_authority[$id]->rdata = '';

$a = print_r($request_authority[$id], true);
$b = print_r($object, true);

$this->assertSame($a, $b);
}

//
Expand Down
1 change: 0 additions & 1 deletion tests/Tests_Net_DNS2_ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public function testInternalResolver()
],
'PX' => 'px.netdns2.com. 86400 IN PX 10 ab.net2.it. o-ab.prmd-net2.admdb.c-it.',
'RP' => 'rp.netdns2.com. 86400 IN RP louie\.trantor.umd.edu. lam1.people.test.com.',
'RRSIG' => 'rrsig.netdns2.com. 86400 IN RRSIG DNSKEY 7 1 86400 20100827211706 20100822211706 57970 gov. KoWPhMtLHp8sWYZSgsMiYJKB9P71CQmh9CnxJCs5GutKfo7Jpw+nNnDLiNnsd6U1JSkf99rYRWCyOTAPC47xkHr+2Uh7n6HDJznfdCzRa/v9uwEcbXIxCZ7KfzNJewW3EvYAxDIrW6sY/4MAsjS5XM/O9LaWzw6pf7TX5obBbLI+zRECbPNTdY+RF6Fl9K0GVaEZJNYi2PRXnATwvwca2CNRWxeMT/dF5STUram3cWjH0Pkm19Gc1jbdzlZVDbUudDauWoHcc0mfH7PV1sMpe80NqK7yQ24AzAkXSiknO13itHsCe4LECUu0/OtnhHg2swwXaVTf5hqHYpzi3bQenw==',
'RT' => 'rt.netdns2.com. 86400 IN RT 2 relay.prime.com.',
'SIG' => 'sig.netdns2.com. 86400 IN SIG DNSKEY 7 1 86400 20100827211706 20100822211706 57970 gov. KoWPhMtLHp8sWYZSgsMiYJKB9P71CQmh9CnxJCs5GutKfo7Jpw+nNnDLiNnsd6U1JSkf99rYRWCyOTAPC47xkHr+2Uh7n6HDJznfdCzRa/v9uwEcbXIxCZ7KfzNJewW3EvYAxDIrW6sY/4MAsjS5XM/O9LaWzw6pf7TX5obBbLI+zRECbPNTdY+RF6Fl9K0GVaEZJNYi2PRXnATwvwca2CNRWxeMT/dF5STUram3cWjH0Pkm19Gc1jbdzlZVDbUudDauWoHcc0mfH7PV1sMpe80NqK7yQ24AzAkXSiknO13itHsCe4LECUu0/OtnhHg2swwXaVTf5hqHYpzi3bQenw==',
'SMIMEA' => [
Expand Down

0 comments on commit 7d304fa

Please sign in to comment.