diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 4ebafa17..78d0e91d 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -1620,7 +1620,10 @@ class X509 corresponding to the extension type identified by extnID */ $map = $this->_getMapping($id); if (!is_bool($map)) { - $mapped = $asn1->asn1map($decoded[0], $map, array('iPAddress' => array($this, '_decodeIP'))); + $decoder = $id == 'id-ce-nameConstraints' ? + array($this, '_decodeNameConstraintIP') : + array($this, '_decodeIP'); + $mapped = $asn1->asn1map($decoded[0], $map, array('iPAddress' => $decoder)); $value = $mapped === false ? $decoded[0] : $mapped; if ($id == 'id-ce-certificatePolicies') { @@ -2511,18 +2514,38 @@ class X509 return inet_ntop(base64_decode($ip)); } + /** + * Decodes an IP address in a name constraints extension + * + * Takes in a base64 encoded "blob" and returns a human readable IP address / mask + * + * @param string $ip + * @access private + * @return array + */ + function _decodeNameConstraintIP($ip) + { + $ip = base64_decode($ip); + $size = strlen($ip) >> 1; + $mask = substr($ip, $size); + $ip = substr($ip, 0, $size); + return array(inet_ntop($ip), inet_ntop($mask)); + } + /** * Encodes an IP address * * Takes a human readable IP address into a base64-encoded "blob" * - * @param string $ip + * @param string|array $ip * @access private * @return string */ function _encodeIP($ip) { - return base64_encode(inet_pton($ip)); + return is_string($ip) ? + base64_encode(inet_pton($ip)) : + base64_encode(inet_pton($ip[0]) . inet_pton($ip[1])); } /** diff --git a/tests/Unit/File/X509/X509Test.php b/tests/Unit/File/X509/X509Test.php index d655a392..6c0879c2 100644 --- a/tests/Unit/File/X509/X509Test.php +++ b/tests/Unit/File/X509/X509Test.php @@ -755,4 +755,46 @@ A9bhRA0cVk7bAEU2c44CYg== $this->assertFalse($r); } + + /** + * @group github1387 + */ + public function testNameConstraintIP() + { + $x509 = new X509(); + $r = $x509->loadX509('-----BEGIN CERTIFICATE----- +MIIGcDCCBVigAwIBAgIQRUgJC4ec7yFWcqzT3mwbWzANBgkqhkiG9w0BAQwFADB1MQswCQYDVQQG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i5vhnP8M7b974R18WCOpgNQvXDI+2/8ZINeU= +-----END CERTIFICATE-----'); + $r = $x509->saveX509($r); + $r = $x509->loadX509($r); + $this->assertSame($r['tbsCertificate']['extensions'][5]['extnValue']['excludedSubtrees'][1]['base']['iPAddress'], ['0.0.0.0', '0.0.0.0']); + } }