From d7d9d782b0a90cd12a9da9128d5c4e8b2faa3af8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sat, 29 Aug 2015 21:58:19 -0500 Subject: [PATCH 1/2] X509: add getOID() method --- phpseclib/File/X509.php | 23 +++++++++++++++++++++++ tests/Unit/File/X509/X509Test.php | 8 ++++++++ 2 files changed, 31 insertions(+) diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 0869deb6..2cf34938 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -4614,4 +4614,27 @@ class File_X509 $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? base64_decode($temp) : false; return $temp != false ? $temp : $str; } + + /** + * Returns the OID corresponding to a name + * + * What's returned in the associative array is either a name or an OID if no OID to name mapping is available. + * The problem with this is that what may be an unmapped OID in one version of phpseclib may not be unmapped + * in the next version, so apps that are looking at this OID may not be able to work from version to version. + * + * This method will return the OID if a name is passed to it and if no mapping is avialable it'll assume that + * what's being passed to it already is an OID and return that instead. A few examples. + * + * getOID('2.16.840.1.101.3.4.2.1') == '2.16.840.1.101.3.4.2.1' + * getOID('id-sha256') == '2.16.840.1.101.3.4.2.1' + * getOID('zzz') == 'zzz' + * + * @access public + * @return String + */ + function getOID($name) + { + $idx = array_search($name, $this->oids); + return $idx === false ? $name : $idx; + } } diff --git a/tests/Unit/File/X509/X509Test.php b/tests/Unit/File/X509/X509Test.php index 5a166b28..5f57263d 100644 --- a/tests/Unit/File/X509/X509Test.php +++ b/tests/Unit/File/X509/X509Test.php @@ -214,4 +214,12 @@ aBtsWpliLSex/HHhtRW9AkBGcq67zKmEpJ9kXcYLEjJii3flFS+Ct/rNm+Hhm1l7 } return $value; } + + public function testGetOID() + { + $x509 = new File_X509(); + $this->assertEquals($x509->getOID('2.16.840.1.101.3.4.2.1'), '2.16.840.1.101.3.4.2.1'); + $this->assertEquals($x509->getOID('id-sha256'), '2.16.840.1.101.3.4.2.1'); + $this->assertEquals($x509->getOID('zzz'), 'zzz'); + } } From abbee318cc24fb338cad81499c2a68603c36bd5d Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 31 Aug 2015 07:38:14 -0500 Subject: [PATCH 2/2] X509: updates to getOID() as suggested by bantu --- phpseclib/File/X509.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 2cf34938..c189a8be 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -4618,9 +4618,10 @@ class File_X509 /** * Returns the OID corresponding to a name * - * What's returned in the associative array is either a name or an OID if no OID to name mapping is available. - * The problem with this is that what may be an unmapped OID in one version of phpseclib may not be unmapped - * in the next version, so apps that are looking at this OID may not be able to work from version to version. + * What's returned in the associative array returned by loadX509() (or load*()) is either a name or an OID if + * no OID to name mapping is available. The problem with this is that what may be an unmapped OID in one version + * of phpseclib may not be unmapped in the next version, so apps that are looking at this OID may not be able + * to work from version to version. * * This method will return the OID if a name is passed to it and if no mapping is avialable it'll assume that * what's being passed to it already is an OID and return that instead. A few examples. @@ -4634,7 +4635,10 @@ class File_X509 */ function getOID($name) { - $idx = array_search($name, $this->oids); - return $idx === false ? $name : $idx; + static $reverseMap; + if (!isset($reverseMap)) { + $reverseMap = array_flip($this->oids); + } + return isset($reverseMap[$name]) ? $reverseMap[$name] : $name; } }