| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414 |
- <?php
- /**
- * Pure-PHP ASN.1 Parser
- *
- * PHP version 5
- *
- * ASN.1 provides the semantics for data encoded using various schemes. The most commonly
- * utilized scheme is DER or the "Distinguished Encoding Rules". PEM's are base64 encoded
- * DER blobs.
- *
- * \phpseclib\File\ASN1 decodes and encodes DER formatted messages and places them in a semantic context.
- *
- * Uses the 1988 ASN.1 syntax.
- *
- * @category File
- * @package ASN1
- * @author Jim Wigginton <terrafrost@php.net>
- * @copyright 2012 Jim Wigginton
- * @license http://www.opensource.org/licenses/mit-license.html MIT License
- * @link http://phpseclib.sourceforge.net
- */
- namespace phpseclib\File;
- use ParagonIE\ConstantTime\Base64;
- use phpseclib\File\ASN1\Element;
- use phpseclib\Math\BigInteger;
- /**
- * Pure-PHP ASN.1 Parser
- *
- * @package ASN1
- * @author Jim Wigginton <terrafrost@php.net>
- * @access public
- */
- abstract class ASN1
- {
- /**#@+
- * Tag Classes
- *
- * @access private
- * @link http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=12
- */
- const CLASS_UNIVERSAL = 0;
- const CLASS_APPLICATION = 1;
- const CLASS_CONTEXT_SPECIFIC = 2;
- const CLASS_PRIVATE = 3;
- /**#@-*/
- /**#@+
- * Tag Classes
- *
- * @access private
- * @link http://www.obj-sys.com/asn1tutorial/node124.html
- */
- const TYPE_BOOLEAN = 1;
- const TYPE_INTEGER = 2;
- const TYPE_BIT_STRING = 3;
- const TYPE_OCTET_STRING = 4;
- const TYPE_NULL = 5;
- const TYPE_OBJECT_IDENTIFIER = 6;
- //const TYPE_OBJECT_DESCRIPTOR = 7;
- //const TYPE_INSTANCE_OF = 8; // EXTERNAL
- const TYPE_REAL = 9;
- const TYPE_ENUMERATED = 10;
- //const TYPE_EMBEDDED = 11;
- const TYPE_UTF8_STRING = 12;
- //const TYPE_RELATIVE_OID = 13;
- const TYPE_SEQUENCE = 16; // SEQUENCE OF
- const TYPE_SET = 17; // SET OF
- /**#@-*/
- /**#@+
- * More Tag Classes
- *
- * @access private
- * @link http://www.obj-sys.com/asn1tutorial/node10.html
- */
- const TYPE_NUMERIC_STRING = 18;
- const TYPE_PRINTABLE_STRING = 19;
- const TYPE_TELETEX_STRING = 20; // T61String
- const TYPE_VIDEOTEX_STRING = 21;
- const TYPE_IA5_STRING = 22;
- const TYPE_UTC_TIME = 23;
- const TYPE_GENERALIZED_TIME = 24;
- const TYPE_GRAPHIC_STRING = 25;
- const TYPE_VISIBLE_STRING = 26; // ISO646String
- const TYPE_GENERAL_STRING = 27;
- const TYPE_UNIVERSAL_STRING = 28;
- //const TYPE_CHARACTER_STRING = 29;
- const TYPE_BMP_STRING = 30;
- /**#@-*/
- /**#@+
- * Tag Aliases
- *
- * These tags are kinda place holders for other tags.
- *
- * @access private
- */
- const TYPE_CHOICE = -1;
- const TYPE_ANY = -2;
- /**#@-*/
- /**
- * ASN.1 object identifiers
- *
- * @var array
- * @access private
- * @link http://en.wikipedia.org/wiki/Object_identifier
- */
- private static $oids = [];
- /**
- * ASN.1 object identifier reverse mapping
- *
- * @var array
- * @access private
- */
- private static $reverseOIDs = [];
- /**
- * Default date format
- *
- * @var string
- * @access private
- * @link http://php.net/class.datetime
- */
- private static $format = 'D, d M Y H:i:s O';
- /**
- * Filters
- *
- * If the mapping type is self::TYPE_ANY what do we actually encode it as?
- *
- * @var array
- * @access private
- * @see self::encode_der()
- */
- private static $filters;
- /**
- * Current Location of most recent ASN.1 encode process
- *
- * Useful for debug purposes
- *
- * @var array
- * @access private
- * @see self::encode_der()
- */
- private static $location;
- /**
- * DER Encoded String
- *
- * In case we need to create ASN1\Element object's..
- *
- * @var string
- * @access private
- * @see self::decodeDER()
- */
- private static $encoded;
- /**
- * Type mapping table for the ANY type.
- *
- * Structured or unknown types are mapped to a \phpseclib\File\ASN1\Element.
- * Unambiguous types get the direct mapping (int/real/bool).
- * Others are mapped as a choice, with an extra indexing level.
- *
- * @var array
- * @access public
- */
- const ANY_MAP = [
- self::TYPE_BOOLEAN => true,
- self::TYPE_INTEGER => true,
- self::TYPE_BIT_STRING => 'bitString',
- self::TYPE_OCTET_STRING => 'octetString',
- self::TYPE_NULL => 'null',
- self::TYPE_OBJECT_IDENTIFIER => 'objectIdentifier',
- self::TYPE_REAL => true,
- self::TYPE_ENUMERATED => 'enumerated',
- self::TYPE_UTF8_STRING => 'utf8String',
- self::TYPE_NUMERIC_STRING => 'numericString',
- self::TYPE_PRINTABLE_STRING => 'printableString',
- self::TYPE_TELETEX_STRING => 'teletexString',
- self::TYPE_VIDEOTEX_STRING => 'videotexString',
- self::TYPE_IA5_STRING => 'ia5String',
- self::TYPE_UTC_TIME => 'utcTime',
- self::TYPE_GENERALIZED_TIME => 'generalTime',
- self::TYPE_GRAPHIC_STRING => 'graphicString',
- self::TYPE_VISIBLE_STRING => 'visibleString',
- self::TYPE_GENERAL_STRING => 'generalString',
- self::TYPE_UNIVERSAL_STRING => 'universalString',
- //self::TYPE_CHARACTER_STRING => 'characterString',
- self::TYPE_BMP_STRING => 'bmpString'
- ];
- /**
- * String type to character size mapping table.
- *
- * Non-convertable types are absent from this table.
- * size == 0 indicates variable length encoding.
- *
- * @var array
- * @access public
- */
- const STRING_TYPE_SIZE = [
- self::TYPE_UTF8_STRING => 0,
- self::TYPE_BMP_STRING => 2,
- self::TYPE_UNIVERSAL_STRING => 4,
- self::TYPE_PRINTABLE_STRING => 1,
- self::TYPE_TELETEX_STRING => 1,
- self::TYPE_IA5_STRING => 1,
- self::TYPE_VISIBLE_STRING => 1,
- ];
- /**
- * Parse BER-encoding
- *
- * Serves a similar purpose to openssl's asn1parse
- *
- * @param string $encoded
- * @return array
- * @access public
- */
- public static function decodeBER($encoded)
- {
- if ($encoded instanceof Element) {
- $encoded = $encoded->element;
- }
- self::$encoded = $encoded;
- $decoded = [self::decode_ber($encoded)];
- // encapsulate in an array for BC with the old decodeBER
- return $decoded;
- }
- /**
- * Parse BER-encoding (Helper function)
- *
- * Sometimes we want to get the BER encoding of a particular tag. $start lets us do that without having to reencode.
- * $encoded is passed by reference for the recursive calls done for self::TYPE_BIT_STRING and
- * self::TYPE_OCTET_STRING. In those cases, the indefinite length is used.
- *
- * @param string $encoded
- * @param int $start
- * @param int $encoded_pos
- * @return array
- * @access private
- */
- private static function decode_ber($encoded, $start = 0, $encoded_pos = 0)
- {
- $current = ['start' => $start];
- $type = ord($encoded[$encoded_pos++]);
- $start++;
- $constructed = ($type >> 5) & 1;
- $tag = $type & 0x1F;
- if ($tag == 0x1F) {
- $tag = 0;
- // process septets (since the eighth bit is ignored, it's not an octet)
- do {
- $loop = ord($encoded[0]) >> 7;
- $tag <<= 7;
- $tag |= ord($encoded[$encoded_pos++]) & 0x7F;
- $start++;
- } while ($loop);
- }
- // Length, as discussed in paragraph 8.1.3 of X.690-0207.pdf#page=13
- $length = ord($encoded[$encoded_pos++]);
- $start++;
- if ($length == 0x80) { // indefinite length
- // "[A sender shall] use the indefinite form (see 8.1.3.6) if the encoding is constructed and is not all
- // immediately available." -- paragraph 8.1.3.2.c
- $length = strlen($encoded) - $encoded_pos;
- } elseif ($length & 0x80) { // definite length, long form
- // technically, the long form of the length can be represented by up to 126 octets (bytes), but we'll only
- // support it up to four.
- $length&= 0x7F;
- $temp = substr($encoded, $encoded_pos, $length);
- $encoded_pos += $length;
- // tags of indefinte length don't really have a header length; this length includes the tag
- $current+= ['headerlength' => $length + 2];
- $start+= $length;
- extract(unpack('Nlength', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4)));
- } else {
- $current+= ['headerlength' => 2];
- }
- if ($length > (strlen($encoded) - $encoded_pos)) {
- return false;
- }
- $content = substr($encoded, $encoded_pos, $length);
- $content_pos = 0;
- // at this point $length can be overwritten. it's only accurate for definite length things as is
- /* Class is UNIVERSAL, APPLICATION, PRIVATE, or CONTEXT-SPECIFIC. The UNIVERSAL class is restricted to the ASN.1
- built-in types. It defines an application-independent data type that must be distinguishable from all other
- data types. The other three classes are user defined. The APPLICATION class distinguishes data types that
- have a wide, scattered use within a particular presentation context. PRIVATE distinguishes data types within
- a particular organization or country. CONTEXT-SPECIFIC distinguishes members of a sequence or set, the
- alternatives of a CHOICE, or universally tagged set members. Only the class number appears in braces for this
- data type; the term CONTEXT-SPECIFIC does not appear.
- -- http://www.obj-sys.com/asn1tutorial/node12.html */
- $class = ($type >> 6) & 3;
- switch ($class) {
- case self::CLASS_APPLICATION:
- case self::CLASS_PRIVATE:
- case self::CLASS_CONTEXT_SPECIFIC:
- if (!$constructed) {
- return [
- 'type' => $class,
- 'constant' => $tag,
- 'content' => $content,
- 'length' => $length + $start - $current['start']
- ] + $current;
- }
- $newcontent = [];
- $remainingLength = $length;
- while ($remainingLength > 0) {
- $temp = self::decode_ber($content, $start, $content_pos);
- $length = $temp['length'];
- // end-of-content octets - see paragraph 8.1.5
- if (substr($content, $content_pos + $length, 2) == "\0\0") {
- $length+= 2;
- $start+= $length;
- $newcontent[] = $temp;
- break;
- }
- $start+= $length;
- $remainingLength-= $length;
- $newcontent[] = $temp;
- $content_pos += $length;
- }
- return [
- 'type' => $class,
- 'constant' => $tag,
- // the array encapsulation is for BC with the old format
- 'content' => $newcontent,
- // the only time when $content['headerlength'] isn't defined is when the length is indefinite.
- // the absence of $content['headerlength'] is how we know if something is indefinite or not.
- // technically, it could be defined to be 2 and then another indicator could be used but whatever.
- 'length' => $start - $current['start']
- ] + $current;
- }
- $current+= ['type' => $tag];
- // decode UNIVERSAL tags
- switch ($tag) {
- case self::TYPE_BOOLEAN:
- // "The contents octets shall consist of a single octet." -- paragraph 8.2.1
- //if (strlen($content) != 1) {
- // return false;
- //}
- $current['content'] = (bool) ord($content[$content_pos]);
- break;
- case self::TYPE_INTEGER:
- case self::TYPE_ENUMERATED:
- $current['content'] = new BigInteger(substr($content, $content_pos), -256);
- break;
- case self::TYPE_REAL: // not currently supported
- return false;
- case self::TYPE_BIT_STRING:
- // The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit,
- // the number of unused bits in the final subsequent octet. The number shall be in the range zero to
- // seven.
- if (!$constructed) {
- $current['content'] = substr($content, $content_pos);
- } else {
- $temp = self::decode_ber($content, $start, $content_pos);
- $length-= (strlen($content) - $content_pos);
- $last = count($temp) - 1;
- for ($i = 0; $i < $last; $i++) {
- // all subtags should be bit strings
- //if ($temp[$i]['type'] != self::TYPE_BIT_STRING) {
- // return false;
- //}
- $current['content'].= substr($temp[$i]['content'], 1);
- }
- // all subtags should be bit strings
- //if ($temp[$last]['type'] != self::TYPE_BIT_STRING) {
- // return false;
- //}
- $current['content'] = $temp[$last]['content'][0] . $current['content'] . substr($temp[$i]['content'], 1);
- }
- break;
- case self::TYPE_OCTET_STRING:
- if (!$constructed) {
- $current['content'] = substr($content, $content_pos);
- } else {
- $current['content'] = '';
- $length = 0;
- while (substr($content, $content_pos, 2) != "\0\0") {
- $temp = self::decode_ber($content, $length + $start, $content_pos);
- $content_pos += $temp['length'];
- // all subtags should be octet strings
- //if ($temp['type'] != self::TYPE_OCTET_STRING) {
- // return false;
- //}
- $current['content'].= $temp['content'];
- $length+= $temp['length'];
- }
- if (substr($content, $content_pos, 2) == "\0\0") {
- $length+= 2; // +2 for the EOC
- }
- }
- break;
- case self::TYPE_NULL:
- // "The contents octets shall not contain any octets." -- paragraph 8.8.2
- //if (strlen($content)) {
- // return false;
- //}
- break;
- case self::TYPE_SEQUENCE:
- case self::TYPE_SET:
- $offset = 0;
- $current['content'] = [];
- $content_len = strlen($content);
- while ($content_pos < $content_len) {
- // if indefinite length construction was used and we have an end-of-content string next
- // see paragraphs 8.1.1.3, 8.1.3.2, 8.1.3.6, 8.1.5, and (for an example) 8.6.4.2
- if (!isset($current['headerlength']) && substr($content, $content_pos, 2) == "\0\0") {
- $length = $offset + 2; // +2 for the EOC
- break 2;
- }
- $temp = self::decode_ber($content, $start + $offset, $content_pos);
- $content_pos += $temp['length'];
- $current['content'][] = $temp;
- $offset+= $temp['length'];
- }
- break;
- case self::TYPE_OBJECT_IDENTIFIER:
- $temp = ord($content[$content_pos++]);
- $current['content'] = sprintf('%d.%d', floor($temp / 40), $temp % 40);
- $valuen = 0;
- // process septets
- $content_len = strlen($content);
- while ($content_pos < $content_len) {
- $temp = ord($content[$content_pos++]);
- $valuen <<= 7;
- $valuen |= $temp & 0x7F;
- if (~$temp & 0x80) {
- $current['content'].= ".$valuen";
- $valuen = 0;
- }
- }
- // the eighth bit of the last byte should not be 1
- //if ($temp >> 7) {
- // return false;
- //}
- break;
- /* Each character string type shall be encoded as if it had been declared:
- [UNIVERSAL x] IMPLICIT OCTET STRING
- -- X.690-0207.pdf#page=23 (paragraph 8.21.3)
- Per that, we're not going to do any validation. If there are any illegal characters in the string,
- we don't really care */
- case self::TYPE_NUMERIC_STRING:
- // 0,1,2,3,4,5,6,7,8,9, and space
- case self::TYPE_PRINTABLE_STRING:
- // Upper and lower case letters, digits, space, apostrophe, left/right parenthesis, plus sign, comma,
- // hyphen, full stop, solidus, colon, equal sign, question mark
- case self::TYPE_TELETEX_STRING:
- // The Teletex character set in CCITT's T61, space, and delete
- // see http://en.wikipedia.org/wiki/Teletex#Character_sets
- case self::TYPE_VIDEOTEX_STRING:
- // The Videotex character set in CCITT's T.100 and T.101, space, and delete
- case self::TYPE_VISIBLE_STRING:
- // Printing character sets of international ASCII, and space
- case self::TYPE_IA5_STRING:
- // International Alphabet 5 (International ASCII)
- case self::TYPE_GRAPHIC_STRING:
- // All registered G sets, and space
- case self::TYPE_GENERAL_STRING:
- // All registered C and G sets, space and delete
- case self::TYPE_UTF8_STRING:
- // ????
- case self::TYPE_BMP_STRING:
- $current['content'] = substr($content, $content_pos);
- break;
- case self::TYPE_UTC_TIME:
- case self::TYPE_GENERALIZED_TIME:
- $current['content'] = self::decodeTime(substr($content, $content_pos), $tag);
- default:
- }
- $start+= $length;
- // ie. length is the length of the full TLV encoding - it's not just the length of the value
- return $current + ['length' => $start - $current['start']];
- }
- /**
- * ASN.1 Map
- *
- * Provides an ASN.1 semantic mapping ($mapping) from a parsed BER-encoding to a human readable format.
- *
- * "Special" mappings may be applied on a per tag-name basis via $special.
- *
- * @param array $decoded
- * @param array $mapping
- * @param array $special
- * @return array
- * @access public
- */
- public static function asn1map($decoded, $mapping, $special = [])
- {
- if (isset($mapping['explicit']) && is_array($decoded['content'])) {
- $decoded = $decoded['content'][0];
- }
- switch (true) {
- case $mapping['type'] == self::TYPE_ANY:
- $intype = $decoded['type'];
- // !isset(self::ANY_MAP[$intype]) produces a fatal error on PHP 5.6
- if (isset($decoded['constant']) || !array_key_exists($intype, self::ANY_MAP) || (ord(self::$encoded[$decoded['start']]) & 0x20)) {
- return new Element(substr(self::$encoded, $decoded['start'], $decoded['length']));
- }
- $inmap = self::ANY_MAP[$intype];
- if (is_string($inmap)) {
- return [$inmap => self::asn1map($decoded, ['type' => $intype] + $mapping, $special)];
- }
- break;
- case $mapping['type'] == self::TYPE_CHOICE:
- foreach ($mapping['children'] as $key => $option) {
- switch (true) {
- case isset($option['constant']) && $option['constant'] == $decoded['constant']:
- case !isset($option['constant']) && $option['type'] == $decoded['type']:
- $value = self::asn1map($decoded, $option, $special);
- break;
- case !isset($option['constant']) && $option['type'] == self::TYPE_CHOICE:
- $v = self::asn1map($decoded, $option, $special);
- if (isset($v)) {
- $value = $v;
- }
- }
- if (isset($value)) {
- if (isset($special[$key])) {
- $value = call_user_func($special[$key], $value);
- }
- return [$key => $value];
- }
- }
- return null;
- case isset($mapping['implicit']):
- case isset($mapping['explicit']):
- case $decoded['type'] == $mapping['type']:
- break;
- default:
- // if $decoded['type'] and $mapping['type'] are both strings, but different types of strings,
- // let it through
- switch (true) {
- case $decoded['type'] < 18: // self::TYPE_NUMERIC_STRING == 18
- case $decoded['type'] > 30: // self::TYPE_BMP_STRING == 30
- case $mapping['type'] < 18:
- case $mapping['type'] > 30:
- return null;
- }
- }
- if (isset($mapping['implicit'])) {
- $decoded['type'] = $mapping['type'];
- }
- switch ($decoded['type']) {
- case self::TYPE_SEQUENCE:
- $map = [];
- // ignore the min and max
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $child = $mapping['children'];
- foreach ($decoded['content'] as $content) {
- if (($map[] = self::asn1map($content, $child, $special)) === null) {
- return null;
- }
- }
- return $map;
- }
- $n = count($decoded['content']);
- $i = 0;
- foreach ($mapping['children'] as $key => $child) {
- $maymatch = $i < $n; // Match only existing input.
- if ($maymatch) {
- $temp = $decoded['content'][$i];
- if ($child['type'] != self::TYPE_CHOICE) {
- // Get the mapping and input class & constant.
- $childClass = $tempClass = self::CLASS_UNIVERSAL;
- $constant = null;
- if (isset($temp['constant'])) {
- $tempClass = isset($temp['class']) ? $temp['class'] : self::CLASS_CONTEXT_SPECIFIC;
- }
- if (isset($child['class'])) {
- $childClass = $child['class'];
- $constant = $child['cast'];
- } elseif (isset($child['constant'])) {
- $childClass = self::CLASS_CONTEXT_SPECIFIC;
- $constant = $child['constant'];
- }
- if (isset($constant) && isset($temp['constant'])) {
- // Can only match if constants and class match.
- $maymatch = $constant == $temp['constant'] && $childClass == $tempClass;
- } else {
- // Can only match if no constant expected and type matches or is generic.
- $maymatch = !isset($child['constant']) && array_search($child['type'], [$temp['type'], self::TYPE_ANY, self::TYPE_CHOICE]) !== false;
- }
- }
- }
- if ($maymatch) {
- // Attempt submapping.
- $candidate = self::asn1map($temp, $child, $special);
- $maymatch = $candidate !== null;
- }
- if ($maymatch) {
- // Got the match: use it.
- if (isset($special[$key])) {
- $candidate = call_user_func($special[$key], $candidate);
- }
- $map[$key] = $candidate;
- $i++;
- } elseif (isset($child['default'])) {
- switch ($child['type']) {
- case ASN1::TYPE_INTEGER:
- $map[$key] = new BigInteger($child['default']);
- break;
- //case ASN1::TYPE_BOOLEAN:
- default:
- $map[$key] = $child['type'];
- }
- } elseif (!isset($child['optional'])) {
- return null; // Syntax error.
- }
- }
- // Fail mapping if all input items have not been consumed.
- return $i < $n ? null: $map;
- // the main diff between sets and sequences is the encapsulation of the foreach in another for loop
- case self::TYPE_SET:
- $map = [];
- // ignore the min and max
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $child = $mapping['children'];
- foreach ($decoded['content'] as $content) {
- if (($map[] = self::asn1map($content, $child, $special)) === null) {
- return null;
- }
- }
- return $map;
- }
- for ($i = 0; $i < count($decoded['content']); $i++) {
- $temp = $decoded['content'][$i];
- $tempClass = self::CLASS_UNIVERSAL;
- if (isset($temp['constant'])) {
- $tempClass = isset($temp['class']) ? $temp['class'] : self::CLASS_CONTEXT_SPECIFIC;
- }
- foreach ($mapping['children'] as $key => $child) {
- if (isset($map[$key])) {
- continue;
- }
- $maymatch = true;
- if ($child['type'] != self::TYPE_CHOICE) {
- $childClass = self::CLASS_UNIVERSAL;
- $constant = null;
- if (isset($child['class'])) {
- $childClass = $child['class'];
- $constant = $child['cast'];
- } elseif (isset($child['constant'])) {
- $childClass = self::CLASS_CONTEXT_SPECIFIC;
- $constant = $child['constant'];
- }
- if (isset($constant) && isset($temp['constant'])) {
- // Can only match if constants and class match.
- $maymatch = $constant == $temp['constant'] && $childClass == $tempClass;
- } else {
- // Can only match if no constant expected and type matches or is generic.
- $maymatch = !isset($child['constant']) && array_search($child['type'], [$temp['type'], self::TYPE_ANY, self::TYPE_CHOICE]) !== false;
- }
- }
- if ($maymatch) {
- // Attempt submapping.
- $candidate = self::asn1map($temp, $child, $special);
- $maymatch = $candidate !== null;
- }
- if (!$maymatch) {
- break;
- }
- // Got the match: use it.
- if (isset($special[$key])) {
- $candidate = call_user_func($special[$key], $candidate);
- }
- $map[$key] = $candidate;
- break;
- }
- }
- foreach ($mapping['children'] as $key => $child) {
- if (!isset($map[$key])) {
- if (isset($child['default'])) {
- $map[$key] = $child['default'];
- } elseif (!isset($child['optional'])) {
- return null;
- }
- }
- }
- return $map;
- case self::TYPE_OBJECT_IDENTIFIER:
- return isset(self::$oids[$decoded['content']]) ? self::$oids[$decoded['content']] : $decoded['content'];
- case self::TYPE_UTC_TIME:
- case self::TYPE_GENERALIZED_TIME:
- if (isset($mapping['implicit'])) {
- $decoded['content'] = self::decodeTime($decoded['content'], $decoded['type']);
- }
- return @date(self::$format, $decoded['content']);
- case self::TYPE_BIT_STRING:
- if (isset($mapping['mapping'])) {
- $offset = ord($decoded['content'][0]);
- $size = (strlen($decoded['content']) - 1) * 8 - $offset;
- /*
- From X.680-0207.pdf#page=46 (21.7):
- "When a "NamedBitList" is used in defining a bitstring type ASN.1 encoding rules are free to add (or remove)
- arbitrarily any trailing 0 bits to (or from) values that are being encoded or decoded. Application designers should
- therefore ensure that different semantics are not associated with such values which differ only in the number of trailing
- 0 bits."
- */
- $bits = count($mapping['mapping']) == $size ? [] : array_fill(0, count($mapping['mapping']) - $size, false);
- for ($i = strlen($decoded['content']) - 1; $i > 0; $i--) {
- $current = ord($decoded['content'][$i]);
- for ($j = $offset; $j < 8; $j++) {
- $bits[] = (bool) ($current & (1 << $j));
- }
- $offset = 0;
- }
- $values = [];
- $map = array_reverse($mapping['mapping']);
- foreach ($map as $i => $value) {
- if ($bits[$i]) {
- $values[] = $value;
- }
- }
- return $values;
- }
- case self::TYPE_OCTET_STRING:
- return $decoded['content'];
- case self::TYPE_NULL:
- return '';
- case self::TYPE_BOOLEAN:
- return $decoded['content'];
- case self::TYPE_NUMERIC_STRING:
- case self::TYPE_PRINTABLE_STRING:
- case self::TYPE_TELETEX_STRING:
- case self::TYPE_VIDEOTEX_STRING:
- case self::TYPE_IA5_STRING:
- case self::TYPE_GRAPHIC_STRING:
- case self::TYPE_VISIBLE_STRING:
- case self::TYPE_GENERAL_STRING:
- case self::TYPE_UNIVERSAL_STRING:
- case self::TYPE_UTF8_STRING:
- case self::TYPE_BMP_STRING:
- return $decoded['content'];
- case self::TYPE_INTEGER:
- case self::TYPE_ENUMERATED:
- $temp = $decoded['content'];
- if (isset($mapping['implicit'])) {
- $temp = new BigInteger($decoded['content'], -256);
- }
- if (isset($mapping['mapping'])) {
- $temp = (int) $temp->toString();
- return isset($mapping['mapping'][$temp]) ?
- $mapping['mapping'][$temp] :
- false;
- }
- return $temp;
- }
- }
- /**
- * ASN.1 Encode
- *
- * DER-encodes an ASN.1 semantic mapping ($mapping). Some libraries would probably call this function
- * an ASN.1 compiler.
- *
- * "Special" mappings can be applied via $special.
- *
- * @param string $source
- * @param string $mapping
- * @param int $idx
- * @return string
- * @access public
- */
- public static function encodeDER($source, $mapping, $special = [])
- {
- self::$location = [];
- return self::encode_der($source, $mapping, null, $special);
- }
- /**
- * ASN.1 Encode (Helper function)
- *
- * @param string $source
- * @param string $mapping
- * @param int $idx
- * @return string
- * @throws \RuntimeException if the input has an error in it
- * @access private
- */
- private static function encode_der($source, $mapping, $idx = null, $special = [])
- {
- if ($source instanceof Element) {
- return $source->element;
- }
- // do not encode (implicitly optional) fields with value set to default
- if (isset($mapping['default']) && $source === $mapping['default']) {
- return '';
- }
- if (isset($idx)) {
- if (isset($special[$idx])) {
- $source = call_user_func($special[$idx], $source);
- }
- self::$location[] = $idx;
- }
- $tag = $mapping['type'];
- switch ($tag) {
- case self::TYPE_SET: // Children order is not important, thus process in sequence.
- case self::TYPE_SEQUENCE:
- $tag|= 0x20; // set the constructed bit
- // ignore the min and max
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $value = [];
- $child = $mapping['children'];
- foreach ($source as $content) {
- $temp = self::encode_der($content, $child, null, $special);
- if ($temp === false) {
- return false;
- }
- $value[]= $temp;
- }
- /* "The encodings of the component values of a set-of value shall appear in ascending order, the encodings being compared
- as octet strings with the shorter components being padded at their trailing end with 0-octets.
- NOTE - The padding octets are for comparison purposes only and do not appear in the encodings."
- -- sec 11.6 of http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf */
- if ($mapping['type'] == self::TYPE_SET) {
- sort($value);
- }
- $value = implode($value, '');
- break;
- }
- $value = '';
- foreach ($mapping['children'] as $key => $child) {
- if (!array_key_exists($key, $source)) {
- if (!isset($child['optional'])) {
- return false;
- }
- continue;
- }
- $temp = self::encode_der($source[$key], $child, $key, $special);
- if ($temp === false) {
- return false;
- }
- // An empty child encoding means it has been optimized out.
- // Else we should have at least one tag byte.
- if ($temp === '') {
- continue;
- }
- // if isset($child['constant']) is true then isset($child['optional']) should be true as well
- if (isset($child['constant'])) {
- /*
- From X.680-0207.pdf#page=58 (30.6):
- "The tagging construction specifies explicit tagging if any of the following holds:
- ...
- c) the "Tag Type" alternative is used and the value of "TagDefault" for the module is IMPLICIT TAGS or
- AUTOMATIC TAGS, but the type defined by "Type" is an untagged choice type, an untagged open type, or
- an untagged "DummyReference" (see ITU-T Rec. X.683 | ISO/IEC 8824-4, 8.3)."
- */
- if (isset($child['explicit']) || $child['type'] == self::TYPE_CHOICE) {
- $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
- $temp = $subtag . self::encodeLength(strlen($temp)) . $temp;
- } else {
- $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
- $temp = $subtag . substr($temp, 1);
- }
- }
- $value.= $temp;
- }
- break;
- case self::TYPE_CHOICE:
- $temp = false;
- foreach ($mapping['children'] as $key => $child) {
- if (!isset($source[$key])) {
- continue;
- }
- $temp = self::encode_der($source[$key], $child, $key, $special);
- if ($temp === false) {
- return false;
- }
- // An empty child encoding means it has been optimized out.
- // Else we should have at least one tag byte.
- if ($temp === '') {
- continue;
- }
- $tag = ord($temp[0]);
- // if isset($child['constant']) is true then isset($child['optional']) should be true as well
- if (isset($child['constant'])) {
- if (isset($child['explicit']) || $child['type'] == self::TYPE_CHOICE) {
- $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
- $temp = $subtag . self::encodeLength(strlen($temp)) . $temp;
- } else {
- $subtag = chr((self::CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
- $temp = $subtag . substr($temp, 1);
- }
- }
- }
- if (isset($idx)) {
- array_pop(self::$location);
- }
- if ($temp && isset($mapping['cast'])) {
- $temp[0] = chr(($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast']);
- }
- return $temp;
- case self::TYPE_INTEGER:
- case self::TYPE_ENUMERATED:
- if (!isset($mapping['mapping'])) {
- if (is_numeric($source)) {
- $source = new BigInteger($source);
- }
- $value = $source->toBytes(true);
- } else {
- $value = array_search($source, $mapping['mapping']);
- if ($value === false) {
- return false;
- }
- $value = new BigInteger($value);
- $value = $value->toBytes(true);
- }
- if (!strlen($value)) {
- $value = chr(0);
- }
- break;
- case self::TYPE_UTC_TIME:
- case self::TYPE_GENERALIZED_TIME:
- $format = $mapping['type'] == self::TYPE_UTC_TIME ? 'y' : 'Y';
- $format.= 'mdHis';
- $value = @gmdate($format, strtotime($source)) . 'Z';
- break;
- case self::TYPE_BIT_STRING:
- if (isset($mapping['mapping'])) {
- $bits = array_fill(0, count($mapping['mapping']), 0);
- $size = 0;
- for ($i = 0; $i < count($mapping['mapping']); $i++) {
- if (in_array($mapping['mapping'][$i], $source)) {
- $bits[$i] = 1;
- $size = $i;
- }
- }
- if (isset($mapping['min']) && $mapping['min'] >= 1 && $size < $mapping['min']) {
- $size = $mapping['min'] - 1;
- }
- $offset = 8 - (($size + 1) & 7);
- $offset = $offset !== 8 ? $offset : 0;
- $value = chr($offset);
- for ($i = $size + 1; $i < count($mapping['mapping']); $i++) {
- unset($bits[$i]);
- }
- $bits = implode('', array_pad($bits, $size + $offset + 1, 0));
- $bytes = explode(' ', rtrim(chunk_split($bits, 8, ' ')));
- foreach ($bytes as $byte) {
- $value.= chr(bindec($byte));
- }
- break;
- }
- case self::TYPE_OCTET_STRING:
- /* The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit,
- the number of unused bits in the final subsequent octet. The number shall be in the range zero to seven.
- -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=16 */
- $value = $source;
- break;
- case self::TYPE_OBJECT_IDENTIFIER:
- if (!preg_match('#(?:\d+\.)+#', $source)) {
- $oid = isset(self::$reverseOIDs[$source]) ? self::$reverseOIDs[$source] : false;
- } else {
- $oid = $source;
- }
- if ($oid === false) {
- throw new \RuntimeException('Invalid OID');
- }
- $value = '';
- $parts = explode('.', $oid);
- $value = chr(40 * $parts[0] + $parts[1]);
- for ($i = 2; $i < count($parts); $i++) {
- $temp = '';
- if (!$parts[$i]) {
- $temp = "\0";
- } else {
- while ($parts[$i]) {
- $temp = chr(0x80 | ($parts[$i] & 0x7F)) . $temp;
- $parts[$i] >>= 7;
- }
- $temp[strlen($temp) - 1] = $temp[strlen($temp) - 1] & chr(0x7F);
- }
- $value.= $temp;
- }
- break;
- case self::TYPE_ANY:
- $loc = self::$location;
- if (isset($idx)) {
- array_pop(self::$location);
- }
- switch (true) {
- case !isset($source):
- return self::encode_der(null, ['type' => self::TYPE_NULL] + $mapping, null, $special);
- case is_int($source):
- case $source instanceof BigInteger:
- return self::encode_der($source, ['type' => self::TYPE_INTEGER] + $mapping, null, $special);
- case is_float($source):
- return self::encode_der($source, ['type' => self::TYPE_REAL] + $mapping, null, $special);
- case is_bool($source):
- return self::encode_der($source, ['type' => self::TYPE_BOOLEAN] + $mapping, null, $special);
- case is_array($source) && count($source) == 1:
- $typename = implode('', array_keys($source));
- $outtype = array_search($typename, self::ANY_MAP, true);
- if ($outtype !== false) {
- return self::encode_der($source[$typename], ['type' => $outtype] + $mapping, null, $special);
- }
- }
- $filters = self::$filters;
- foreach ($loc as $part) {
- if (!isset($filters[$part])) {
- $filters = false;
- break;
- }
- $filters = $filters[$part];
- }
- if ($filters === false) {
- throw new \RuntimeException('No filters defined for ' . implode('/', $loc));
- }
- return self::encode_der($source, $filters + $mapping, null, $special);
- case self::TYPE_NULL:
- $value = '';
- break;
- case self::TYPE_NUMERIC_STRING:
- case self::TYPE_TELETEX_STRING:
- case self::TYPE_PRINTABLE_STRING:
- case self::TYPE_UNIVERSAL_STRING:
- case self::TYPE_UTF8_STRING:
- case self::TYPE_BMP_STRING:
- case self::TYPE_IA5_STRING:
- case self::TYPE_VISIBLE_STRING:
- case self::TYPE_VIDEOTEX_STRING:
- case self::TYPE_GRAPHIC_STRING:
- case self::TYPE_GENERAL_STRING:
- $value = $source;
- break;
- case self::TYPE_BOOLEAN:
- $value = $source ? "\xFF" : "\x00";
- break;
- default:
- throw new \RuntimeException('Mapping provides no type definition for ' . implode('/', self::$location));
- }
- if (isset($idx)) {
- array_pop(self::$location);
- }
- if (isset($mapping['cast'])) {
- if (isset($mapping['explicit']) || $mapping['type'] == self::TYPE_CHOICE) {
- $value = chr($tag) . self::encodeLength(strlen($value)) . $value;
- $tag = ($mapping['class'] << 6) | 0x20 | $mapping['cast'];
- } else {
- $tag = ($mapping['class'] << 6) | (ord($temp[0]) & 0x20) | $mapping['cast'];
- }
- }
- return chr($tag) . self::encodeLength(strlen($value)) . $value;
- }
- /**
- * BER-decode the time
- *
- * Called by _decode_ber() and in the case of implicit tags asn1map().
- *
- * @access private
- * @param string $content
- * @param int $tag
- * @return string
- */
- private static function decodeTime($content, $tag)
- {
- /* UTCTime:
- http://tools.ietf.org/html/rfc5280#section-4.1.2.5.1
- http://www.obj-sys.com/asn1tutorial/node15.html
- GeneralizedTime:
- http://tools.ietf.org/html/rfc5280#section-4.1.2.5.2
- http://www.obj-sys.com/asn1tutorial/node14.html */
- $pattern = $tag == self::TYPE_UTC_TIME ?
- '#^(..)(..)(..)(..)(..)(..)?(.*)$#' :
- '#(....)(..)(..)(..)(..)(..).*([Z+-].*)$#';
- preg_match($pattern, $content, $matches);
- list(, $year, $month, $day, $hour, $minute, $second, $timezone) = $matches;
- if ($tag == self::TYPE_UTC_TIME) {
- $year = $year >= 50 ? "19$year" : "20$year";
- }
- if ($timezone == 'Z') {
- $mktime = 'gmmktime';
- $timezone = 0;
- } elseif (preg_match('#([+-])(\d\d)(\d\d)#', $timezone, $matches)) {
- $mktime = 'gmmktime';
- $timezone = 60 * $matches[3] + 3600 * $matches[2];
- if ($matches[1] == '-') {
- $timezone = -$timezone;
- }
- } else {
- $mktime = 'mktime';
- $timezone = 0;
- }
- return @$mktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year) + $timezone;
- }
- /**
- * Set the time format
- *
- * Sets the time / date format for asn1map().
- *
- * @access public
- * @param string $format
- */
- public static function setTimeFormat($format)
- {
- self::$format = $format;
- }
- /**
- * Load OIDs
- *
- * Load the relevant OIDs for a particular ASN.1 semantic mapping.
- * Previously loaded OIDs are retained.
- *
- * @access public
- * @param array $oids
- */
- public static function loadOIDs($oids)
- {
- self::$oids+= $oids;
- self::$reverseOIDs = array_flip(self::$oids);
- }
- /**
- * Set filters
- *
- * See \phpseclib\File\X509, etc, for an example.
- * Previously loaded filters are not retained.
- *
- * @access public
- * @param array $filters
- */
- public static function setFilters($filters)
- {
- self::$filters = $filters;
- }
- /**
- * String type conversion
- *
- * This is a lazy conversion, dealing only with character size.
- * No real conversion table is used.
- *
- * @param string $in
- * @param int $from
- * @param int $to
- * @return string
- * @access public
- */
- public static function convert($in, $from = self::TYPE_UTF8_STRING, $to = self::TYPE_UTF8_STRING)
- {
- // isset(self::STRING_TYPE_SIZE[$from] returns a fatal error on PHP 5.6
- if (!array_key_exists($from, self::STRING_TYPE_SIZE) || !array_key_exists($to, self::STRING_TYPE_SIZE)) {
- return false;
- }
- $insize = self::STRING_TYPE_SIZE[$from];
- $outsize = self::STRING_TYPE_SIZE[$to];
- $inlength = strlen($in);
- $out = '';
- for ($i = 0; $i < $inlength;) {
- if ($inlength - $i < $insize) {
- return false;
- }
- // Get an input character as a 32-bit value.
- $c = ord($in[$i++]);
- switch (true) {
- case $insize == 4:
- $c = ($c << 8) | ord($in[$i++]);
- $c = ($c << 8) | ord($in[$i++]);
- case $insize == 2:
- $c = ($c << 8) | ord($in[$i++]);
- case $insize == 1:
- break;
- case ($c & 0x80) == 0x00:
- break;
- case ($c & 0x40) == 0x00:
- return false;
- default:
- $bit = 6;
- do {
- if ($bit > 25 || $i >= $inlength || (ord($in[$i]) & 0xC0) != 0x80) {
- return false;
- }
- $c = ($c << 6) | (ord($in[$i++]) & 0x3F);
- $bit += 5;
- $mask = 1 << $bit;
- } while ($c & $bit);
- $c &= $mask - 1;
- break;
- }
- // Convert and append the character to output string.
- $v = '';
- switch (true) {
- case $outsize == 4:
- $v .= chr($c & 0xFF);
- $c >>= 8;
- $v .= chr($c & 0xFF);
- $c >>= 8;
- case $outsize == 2:
- $v .= chr($c & 0xFF);
- $c >>= 8;
- case $outsize == 1:
- $v .= chr($c & 0xFF);
- $c >>= 8;
- if ($c) {
- return false;
- }
- break;
- case ($c & 0x80000000) != 0:
- return false;
- case $c >= 0x04000000:
- $v .= chr(0x80 | ($c & 0x3F));
- $c = ($c >> 6) | 0x04000000;
- case $c >= 0x00200000:
- $v .= chr(0x80 | ($c & 0x3F));
- $c = ($c >> 6) | 0x00200000;
- case $c >= 0x00010000:
- $v .= chr(0x80 | ($c & 0x3F));
- $c = ($c >> 6) | 0x00010000;
- case $c >= 0x00000800:
- $v .= chr(0x80 | ($c & 0x3F));
- $c = ($c >> 6) | 0x00000800;
- case $c >= 0x00000080:
- $v .= chr(0x80 | ($c & 0x3F));
- $c = ($c >> 6) | 0x000000C0;
- default:
- $v .= chr($c);
- break;
- }
- $out .= strrev($v);
- }
- return $out;
- }
- /**
- * Extract raw BER from Base64 encoding
- *
- * @access private
- * @param string $str
- * @return string
- */
- public static function extractBER($str)
- {
- /* X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them
- * above and beyond the ceritificate.
- * ie. some may have the following preceding the -----BEGIN CERTIFICATE----- line:
- *
- * Bag Attributes
- * localKeyID: 01 00 00 00
- * subject=/O=organization/OU=org unit/CN=common name
- * issuer=/O=organization/CN=common name
- */
- $temp = preg_replace('#.*?^-+[^-]+-+[\r\n ]*$#ms', '', $str, 1);
- // remove the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- stuff
- $temp = preg_replace('#-+[^-]+-+#', '', $temp);
- // remove new lines
- $temp = str_replace(["\r", "\n", ' '], '', $temp);
- $temp = preg_match('#^[a-zA-Z\d/+]*={0,2}$#', $temp) ? Base64::decode($temp) : false;
- return $temp != false ? $temp : $str;
- }
- /**
- * DER-decode the length
- *
- * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
- * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
- *
- * @access public
- * @param string $string
- * @return int
- */
- public static function decodeLength(&$string)
- {
- $length = ord(Strings::shift($string));
- if ($length & 0x80) { // definite length, long form
- $length&= 0x7F;
- $temp = Strings::shift($string, $length);
- list(, $length) = unpack('N', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4));
- }
- return $length;
- }
- /**
- * DER-encode the length
- *
- * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
- * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
- *
- * @access public
- * @param int $length
- * @return string
- */
- public static function encodeLength($length)
- {
- if ($length <= 0x7F) {
- return chr($length);
- }
- $temp = ltrim(pack('N', $length), chr(0));
- return pack('Ca*', 0x80 | strlen($temp), $temp);
- }
- /**
- * Returns the OID corresponding to a name
- *
- * 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.
- *
- * 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
- */
- static function getOID($name)
- {
- return isset(self::$reverseOIDs[$name]) ? self::$reverseOIDs[$name] : $name;
- }
- }
|