123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293 |
- <?php
- define('FILE_ASN1_CLASS_UNIVERSAL', 0);
- define('FILE_ASN1_CLASS_APPLICATION', 1);
- define('FILE_ASN1_CLASS_CONTEXT_SPECIFIC', 2);
- define('FILE_ASN1_CLASS_PRIVATE', 3);
- define('FILE_ASN1_TYPE_BOOLEAN', 1);
- define('FILE_ASN1_TYPE_INTEGER', 2);
- define('FILE_ASN1_TYPE_BIT_STRING', 3);
- define('FILE_ASN1_TYPE_OCTET_STRING', 4);
- define('FILE_ASN1_TYPE_NULL', 5);
- define('FILE_ASN1_TYPE_OBJECT_IDENTIFIER',6);
- define('FILE_ASN1_TYPE_REAL', 9);
- define('FILE_ASN1_TYPE_ENUMERATED', 10);
- define('FILE_ASN1_TYPE_UTF8_STRING', 12);
- define('FILE_ASN1_TYPE_SEQUENCE', 16);
- define('FILE_ASN1_TYPE_SET', 17);
- define('FILE_ASN1_TYPE_NUMERIC_STRING', 18);
- define('FILE_ASN1_TYPE_PRINTABLE_STRING',19);
- define('FILE_ASN1_TYPE_TELETEX_STRING', 20);
- define('FILE_ASN1_TYPE_VIDEOTEX_STRING', 21);
- define('FILE_ASN1_TYPE_IA5_STRING', 22);
- define('FILE_ASN1_TYPE_UTC_TIME', 23);
- define('FILE_ASN1_TYPE_GENERALIZED_TIME',24);
- define('FILE_ASN1_TYPE_GRAPHIC_STRING', 25);
- define('FILE_ASN1_TYPE_VISIBLE_STRING', 26);
- define('FILE_ASN1_TYPE_GENERAL_STRING', 27);
- define('FILE_ASN1_TYPE_UNIVERSAL_STRING',28);
- define('FILE_ASN1_TYPE_BMP_STRING', 30);
- define('FILE_ASN1_TYPE_CHOICE', -1);
- define('FILE_ASN1_TYPE_ANY', -2);
- class File_ASN1_Element {
-
- var $element;
-
- function File_ASN1_Element($encoded)
- {
- $this->element = $encoded;
- }
- }
- class File_ASN1 {
-
- var $oids = array();
-
- var $format = 'D, d M y H:i:s O';
-
- var $encoded;
-
- var $filters;
-
- var $ANYmap = array(
- FILE_ASN1_TYPE_BOOLEAN => true,
- FILE_ASN1_TYPE_INTEGER => true,
- FILE_ASN1_TYPE_BIT_STRING => 'bitString',
- FILE_ASN1_TYPE_OCTET_STRING => 'octetString',
- FILE_ASN1_TYPE_NULL => 'null',
- FILE_ASN1_TYPE_OBJECT_IDENTIFIER => 'objectIdentifier',
- FILE_ASN1_TYPE_REAL => true,
- FILE_ASN1_TYPE_ENUMERATED => 'enumerated',
- FILE_ASN1_TYPE_UTF8_STRING => 'utf8String',
- FILE_ASN1_TYPE_NUMERIC_STRING => 'numericString',
- FILE_ASN1_TYPE_PRINTABLE_STRING => 'printableString',
- FILE_ASN1_TYPE_TELETEX_STRING => 'teletexString',
- FILE_ASN1_TYPE_VIDEOTEX_STRING => 'videotexString',
- FILE_ASN1_TYPE_IA5_STRING => 'ia5String',
- FILE_ASN1_TYPE_UTC_TIME => 'utcTime',
- FILE_ASN1_TYPE_GENERALIZED_TIME => 'generalTime',
- FILE_ASN1_TYPE_GRAPHIC_STRING => 'graphicString',
- FILE_ASN1_TYPE_VISIBLE_STRING => 'visibleString',
- FILE_ASN1_TYPE_GENERAL_STRING => 'generalString',
- FILE_ASN1_TYPE_UNIVERSAL_STRING => 'universalString',
-
- FILE_ASN1_TYPE_BMP_STRING => 'bmpString'
- );
-
- var $stringTypeSize = array(
- FILE_ASN1_TYPE_UTF8_STRING => 0,
- FILE_ASN1_TYPE_BMP_STRING => 2,
- FILE_ASN1_TYPE_UNIVERSAL_STRING => 4,
- FILE_ASN1_TYPE_PRINTABLE_STRING => 1,
- FILE_ASN1_TYPE_TELETEX_STRING => 1,
- FILE_ASN1_TYPE_IA5_STRING => 1,
- FILE_ASN1_TYPE_VISIBLE_STRING => 1,
- );
-
- function File_ASN1()
- {
- static $static_init = null;
- if (!$static_init) {
- $static_init = true;
- if (!class_exists('Math_BigInteger')) {
- require_once('Math/BigInteger.php');
- }
- }
- }
-
- function decodeBER($encoded)
- {
- if (is_object($encoded) && strtolower(get_class($encoded)) == 'file_asn1_element') {
- $encoded = $encoded->element;
- }
- $this->encoded = $encoded;
- return $this->_decode_ber($encoded);
- }
-
- function _decode_ber(&$encoded, $start = 0)
- {
- $decoded = array();
- while ( strlen($encoded) ) {
- $current = array('start' => $start);
- $type = ord($this->_string_shift($encoded));
- $start++;
- $constructed = ($type >> 5) & 1;
- $tag = $type & 0x1F;
- if ($tag == 0x1F) {
- $tag = 0;
-
- do {
- $loop = ord($encoded[0]) >> 7;
- $tag <<= 7;
- $tag |= ord($this->_string_shift($encoded)) & 0x7F;
- $start++;
- } while ( $loop );
- }
-
- $length = ord($this->_string_shift($encoded));
- $start++;
- if ( $length == 0x80 ) {
-
-
-
-
-
- $length = strlen($encoded);
- } elseif ( $length & 0x80 ) {
-
-
- $length&= 0x7F;
- $temp = $this->_string_shift($encoded, $length);
-
- $current+= array('headerlength' => $length + 2);
- $start+= $length;
- extract(unpack('Nlength', substr(str_pad($temp, 4, chr(0), STR_PAD_LEFT), -4)));
- } else {
- $current+= array('headerlength' => 2);
- }
-
- if (!$type && !$length) {
- return $decoded;
- }
- $content = $this->_string_shift($encoded, $length);
-
- $class = ($type >> 6) & 3;
- switch ($class) {
- case FILE_ASN1_CLASS_APPLICATION:
- case FILE_ASN1_CLASS_PRIVATE:
- case FILE_ASN1_CLASS_CONTEXT_SPECIFIC:
- $decoded[] = array(
- 'type' => $class,
- 'constant' => $tag,
- 'content' => $constructed ? $this->_decode_ber($content, $start) : $content,
- 'length' => $length + $start - $current['start']
- ) + $current;
- $start+= $length;
- continue 2;
- }
- $current+= array('type' => $tag);
-
- switch ($tag) {
- case FILE_ASN1_TYPE_BOOLEAN:
-
-
-
-
- $current['content'] = (bool) ord($content[0]);
- break;
- case FILE_ASN1_TYPE_INTEGER:
- case FILE_ASN1_TYPE_ENUMERATED:
- $current['content'] = new Math_BigInteger($content, -256);
- break;
- case FILE_ASN1_TYPE_REAL:
- return false;
- case FILE_ASN1_TYPE_BIT_STRING:
-
-
-
- if (!$constructed) {
- $current['content'] = $content;
- } else {
- $temp = $this->_decode_ber($content, $start);
- $length-= strlen($content);
- $last = count($temp) - 1;
- for ($i = 0; $i < $last; $i++) {
-
-
-
-
- $current['content'].= substr($temp[$i]['content'], 1);
- }
-
-
-
-
- $current['content'] = $temp[$last]['content'][0] . $current['content'] . substr($temp[$i]['content'], 1);
- }
- break;
- case FILE_ASN1_TYPE_OCTET_STRING:
- if (!$constructed) {
- $current['content'] = $content;
- } else {
- $temp = $this->_decode_ber($content, $start);
- $length-= strlen($content);
- for ($i = 0, $size = count($temp); $i < $size; $i++) {
-
-
-
-
- $current['content'].= $temp[$i]['content'];
- }
-
- }
- break;
- case FILE_ASN1_TYPE_NULL:
-
-
-
-
- break;
- case FILE_ASN1_TYPE_SEQUENCE:
- case FILE_ASN1_TYPE_SET:
- $current['content'] = $this->_decode_ber($content, $start);
- break;
- case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
- $temp = ord($this->_string_shift($content));
- $current['content'] = sprintf('%d.%d', floor($temp / 40), $temp % 40);
- $valuen = 0;
-
- while (strlen($content)) {
- $temp = ord($this->_string_shift($content));
- $valuen <<= 7;
- $valuen |= $temp & 0x7F;
- if (~$temp & 0x80) {
- $current['content'].= ".$valuen";
- $valuen = 0;
- }
- }
-
-
-
-
- break;
-
- case FILE_ASN1_TYPE_NUMERIC_STRING:
-
- case FILE_ASN1_TYPE_PRINTABLE_STRING:
-
-
- case FILE_ASN1_TYPE_TELETEX_STRING:
-
-
- case FILE_ASN1_TYPE_VIDEOTEX_STRING:
-
- case FILE_ASN1_TYPE_VISIBLE_STRING:
-
- case FILE_ASN1_TYPE_IA5_STRING:
-
- case FILE_ASN1_TYPE_GRAPHIC_STRING:
-
- case FILE_ASN1_TYPE_GENERAL_STRING:
-
- case FILE_ASN1_TYPE_UTF8_STRING:
-
- case FILE_ASN1_TYPE_BMP_STRING:
- $current['content'] = $content;
- break;
- case FILE_ASN1_TYPE_UTC_TIME:
- case FILE_ASN1_TYPE_GENERALIZED_TIME:
- $current['content'] = $this->_decodeTime($content, $tag);
- default:
- }
- $start+= $length;
- $decoded[] = $current + array('length' => $start - $current['start']);
- }
- return $decoded;
- }
-
- function asn1map($decoded, $mapping)
- {
- if (isset($mapping['explicit'])) {
- $decoded = $decoded['content'][0];
- }
- switch (true) {
- case $mapping['type'] == FILE_ASN1_TYPE_ANY:
- $intype = $decoded['type'];
- if (isset($decoded['constant']) || !isset($this->ANYmap[$intype]) || ($this->encoded[$decoded['start']] & 0x20)) {
- return new File_ASN1_Element(substr($this->encoded, $decoded['start'], $decoded['length']));
- }
- $inmap = $this->ANYmap[$intype];
- if (is_string($inmap)) {
- return array($inmap => $this->asn1map($decoded, array('type' => $intype) + $mapping));
- }
- break;
- case $mapping['type'] == FILE_ASN1_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 = $this->asn1map($decoded, $option);
- break;
- case !isset($option['constant']) && $option['type'] == FILE_ASN1_TYPE_CHOICE:
- $v = $this->asn1map($decoded, $option);
- if (isset($v)) {
- $value = $v;
- }
- }
- if (isset($value)) {
- return array($key => $value);
- }
- }
- return NULL;
- case isset($mapping['implicit']):
- case isset($mapping['explicit']):
- case $decoded['type'] == $mapping['type']:
- break;
- default:
- return NULL;
- }
- if (isset($mapping['implicit'])) {
- $decoded['type'] = $mapping['type'];
- }
- switch ($decoded['type']) {
- case FILE_ASN1_TYPE_SEQUENCE:
- $map = array();
-
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $child = $mapping['children'];
- foreach ($decoded['content'] as $content) {
- if (($map[] = $this->asn1map($content, $child)) === NULL) {
- return NULL;
- }
- }
- return $map;
- }
- $n = count($decoded['content']);
- $i = 0;
- foreach ($mapping['children'] as $key => $child) {
- $maymatch = $i < $n;
- if ($maymatch) {
- $temp = $decoded['content'][$i];
- if ($child['type'] != FILE_ASN1_TYPE_CHOICE) {
-
- $childClass = $tempClass = FILE_ASN1_CLASS_UNIVERSAL;
- $constant = NULL;
- if (isset($temp['constant'])) {
- $tempClass = isset($temp['class']) ? $temp['class'] : FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
- }
- if (isset($child['class'])) {
- $childClass = $child['class'];
- $constant = $child['cast'];
- }
- elseif (isset($child['constant'])) {
- $childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
- $constant = $child['constant'];
- }
- if (isset($constant) && isset($temp['constant'])) {
-
- $maymatch = $constant == $temp['constant'] && $childClass == $tempClass;
- } else {
-
- $maymatch = !isset($child['constant']) && array_search($child['type'], array($temp['type'], FILE_ASN1_TYPE_ANY, FILE_ASN1_TYPE_CHOICE)) !== false;
- }
- }
- }
- if ($maymatch) {
-
- $candidate = $this->asn1map($temp, $child);
- $maymatch = $candidate !== NULL;
- }
- if ($maymatch) {
-
- $map[$key] = $candidate;
- $i++;
- } elseif (isset($child['default'])) {
- $map[$key] = $child['default'];
- } elseif (!isset($child['optional'])) {
- return NULL;
- }
- }
-
- return $i < $n? NULL: $map;
-
- case FILE_ASN1_TYPE_SET:
- $map = array();
-
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $child = $mapping['children'];
- foreach ($decoded['content'] as $content) {
- if (($map[] = $this->asn1map($content, $child)) === NULL) {
- return NULL;
- }
- }
- return $map;
- }
- for ($i = 0; $i < count($decoded['content']); $i++) {
- $temp = $decoded['content'][$i];
- $tempClass = FILE_ASN1_CLASS_UNIVERSAL;
- if (isset($temp['constant'])) {
- $tempClass = isset($temp['class']) ? $temp['class'] : FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
- }
- foreach ($mapping['children'] as $key => $child) {
- if (isset($map[$key])) {
- continue;
- }
- $maymatch = true;
- if ($child['type'] != FILE_ASN1_TYPE_CHOICE) {
- $childClass = FILE_ASN1_CLASS_UNIVERSAL;
- $constant = NULL;
- if (isset($child['class'])) {
- $childClass = $child['class'];
- $constant = $child['cast'];
- }
- elseif (isset($child['constant'])) {
- $childClass = FILE_ASN1_CLASS_CONTEXT_SPECIFIC;
- $constant = $child['constant'];
- }
- if (isset($constant) && isset($temp['constant'])) {
-
- $maymatch = $constant == $temp['constant'] && $childClass == $tempClass;
- } else {
-
- $maymatch = !isset($child['constant']) && array_search($child['type'], array($temp['type'], FILE_ASN1_TYPE_ANY, FILE_ASN1_TYPE_CHOICE)) !== false;
- }
- }
- if ($maymatch) {
-
- $candidate = $this->asn1map($temp, $child);
- $maymatch = $candidate !== NULL;
- }
- if (!$maymatch) {
- break;
- }
-
- $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 FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
- return isset($this->oids[$decoded['content']]) ? $this->oids[$decoded['content']] : $decoded['content'];
- case FILE_ASN1_TYPE_UTC_TIME:
- case FILE_ASN1_TYPE_GENERALIZED_TIME:
- if (isset($mapping['implicit'])) {
- $decoded['content'] = $this->_decodeTime($decoded['content'], $decoded['type']);
- }
- return @date($this->format, $decoded['content']);
- case FILE_ASN1_TYPE_BIT_STRING:
- if (isset($mapping['mapping'])) {
- $offset = ord($decoded['content'][0]);
- $size = (strlen($decoded['content']) - 1) * 8 - $offset;
-
- $bits = count($mapping['mapping']) == $size ? array() : 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 = array();
- $map = array_reverse($mapping['mapping']);
- foreach ($map as $i => $value) {
- if ($bits[$i]) {
- $values[] = $value;
- }
- }
- return $values;
- }
- case FILE_ASN1_TYPE_OCTET_STRING:
- return base64_encode($decoded['content']);
- case FILE_ASN1_TYPE_NULL:
- return '';
- case FILE_ASN1_TYPE_BOOLEAN:
- return $decoded['content'];
- case FILE_ASN1_TYPE_NUMERIC_STRING:
- case FILE_ASN1_TYPE_PRINTABLE_STRING:
- case FILE_ASN1_TYPE_TELETEX_STRING:
- case FILE_ASN1_TYPE_VIDEOTEX_STRING:
- case FILE_ASN1_TYPE_IA5_STRING:
- case FILE_ASN1_TYPE_GRAPHIC_STRING:
- case FILE_ASN1_TYPE_VISIBLE_STRING:
- case FILE_ASN1_TYPE_GENERAL_STRING:
- case FILE_ASN1_TYPE_UNIVERSAL_STRING:
- case FILE_ASN1_TYPE_UTF8_STRING:
- case FILE_ASN1_TYPE_BMP_STRING:
- return $decoded['content'];
- case FILE_ASN1_TYPE_INTEGER:
- case FILE_ASN1_TYPE_ENUMERATED:
- $temp = $decoded['content'];
- if (isset($mapping['implicit'])) {
- $temp = new Math_BigInteger($decoded['content'], -256);
- }
- if (isset($mapping['mapping'])) {
- $temp = (int) $temp->toString();
- return isset($mapping['mapping'][$temp]) ?
- $mapping['mapping'][$temp] :
- false;
- }
- return $temp;
- }
- }
-
- function encodeDER($source, $mapping)
- {
- $this->location = array();
- return $this->_encode_der($source, $mapping);
- }
-
- function _encode_der($source, $mapping, $idx = NULL)
- {
- if (is_object($source) && strtolower(get_class($source)) == 'file_asn1_element') {
- return $source->element;
- }
-
- if (isset($mapping['default']) && $source === $mapping['default']) {
- return '';
- }
- if (isset($idx)) {
- $this->location[] = $idx;
- }
- $tag = $mapping['type'];
- switch ($tag) {
- case FILE_ASN1_TYPE_SET:
- case FILE_ASN1_TYPE_SEQUENCE:
- $tag|= 0x20;
- $value = '';
-
- if (isset($mapping['min']) && isset($mapping['max'])) {
- $child = $mapping['children'];
- foreach ($source as $content) {
- $temp = $this->_encode_der($content, $child);
- if ($temp === false) {
- return false;
- }
- $value.= $temp;
- }
- break;
- }
- foreach ($mapping['children'] as $key => $child) {
- if (!isset($source[$key])) {
- if (!isset($child['optional'])) {
- return false;
- }
- continue;
- }
- $temp = $this->_encode_der($source[$key], $child, $key);
- if ($temp === false) {
- return false;
- }
-
-
- if ($temp === '') {
- continue;
- }
-
- if (isset($child['constant'])) {
-
- if (isset($child['explicit']) || $child['type'] == FILE_ASN1_TYPE_CHOICE) {
- $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
- $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp;
- } else {
- $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
- $temp = $subtag . substr($temp, 1);
- }
- }
- $value.= $temp;
- }
- break;
- case FILE_ASN1_TYPE_CHOICE:
- $temp = false;
- foreach ($mapping['children'] as $key => $child) {
- if (!isset($source[$key])) {
- continue;
- }
- $temp = $this->_encode_der($source[$key], $child, $key);
- if ($temp === false) {
- return false;
- }
-
-
- if ($temp === '') {
- continue;
- }
- $tag = ord($temp[0]);
-
- if (isset($child['constant'])) {
- if (isset($child['explicit']) || $child['type'] == FILE_ASN1_TYPE_CHOICE) {
- $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | 0x20 | $child['constant']);
- $temp = $subtag . $this->_encodeLength(strlen($temp)) . $temp;
- } else {
- $subtag = chr((FILE_ASN1_CLASS_CONTEXT_SPECIFIC << 6) | (ord($temp[0]) & 0x20) | $child['constant']);
- $temp = $subtag . substr($temp, 1);
- }
- }
- }
- if (isset($idx)) {
- array_pop($this->location);
- }
- if ($temp && isset($mapping['cast'])) {
- $temp[0] = chr(($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast']);
- }
- return $temp;
- case FILE_ASN1_TYPE_INTEGER:
- case FILE_ASN1_TYPE_ENUMERATED:
- if (!isset($mapping['mapping'])) {
- $value = $source->toBytes(true);
- } else {
- $value = array_search($source, $mapping['mapping']);
- if ($value === false) {
- return false;
- }
- $value = new Math_BigInteger($value);
- $value = $value->toBytes(true);
- if (!strlen($value)) {
- $value = chr(0);
- }
- }
- break;
- case FILE_ASN1_TYPE_UTC_TIME:
- case FILE_ASN1_TYPE_GENERALIZED_TIME:
- $format = $mapping['type'] == FILE_ASN1_TYPE_UTC_TIME ? 'y' : 'Y';
- $format.= 'mdHis';
- $value = @gmdate($format, strtotime($source)) . 'Z';
- break;
- case FILE_ASN1_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;
- }
- }
- $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 FILE_ASN1_TYPE_OCTET_STRING:
-
- $value = base64_decode($source);
- break;
- case FILE_ASN1_TYPE_OBJECT_IDENTIFIER:
- $oid = preg_match('#(?:\d+\.)+#', $source) ? $source : array_search($source, $this->oids);
- if ($oid === false) {
- user_error('Invalid OID');
- return false;
- }
- $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 FILE_ASN1_TYPE_ANY:
- $loc = $this->location;
- if (isset($idx)) {
- array_pop($this->location);
- }
- switch (true) {
- case !isset($source):
- return $this->_encode_der(NULL, array('type' => FILE_ASN1_TYPE_NULL) + $mapping);
- case is_int($source):
- case is_object($source) && strtolower(get_class($source)) == 'math_biginteger':
- return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_INTEGER) + $mapping);
- case is_float($source):
- return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_REAL) + $mapping);
- case is_bool($source):
- return $this->_encode_der($source, array('type' => FILE_ASN1_TYPE_BOOLEAN) + $mapping);
- case is_array($source) && count($source) == 1:
- $typename = implode('', array_keys($source));
- $outtype = array_search($typename, $this->ANYmap, true);
- if ($outtype !== false) {
- return $this->_encode_der($source[$typename], array('type' => $outtype) + $mapping);
- }
- }
- $filters = $this->filters;
- foreach ($loc as $part) {
- if (!isset($filters[$part])) {
- $filters = false;
- break;
- }
- $filters = $filters[$part];
- }
- if ($filters === false) {
- user_error('No filters defined for ' . implode('/', $loc));
- return false;
- }
- return $this->_encode_der($source, $filters + $mapping);
- case FILE_ASN1_TYPE_NULL:
- $value = '';
- break;
- case FILE_ASN1_TYPE_NUMERIC_STRING:
- case FILE_ASN1_TYPE_TELETEX_STRING:
- case FILE_ASN1_TYPE_PRINTABLE_STRING:
- case FILE_ASN1_TYPE_UNIVERSAL_STRING:
- case FILE_ASN1_TYPE_UTF8_STRING:
- case FILE_ASN1_TYPE_BMP_STRING:
- case FILE_ASN1_TYPE_IA5_STRING:
- case FILE_ASN1_TYPE_VISIBLE_STRING:
- case FILE_ASN1_TYPE_VIDEOTEX_STRING:
- case FILE_ASN1_TYPE_GRAPHIC_STRING:
- case FILE_ASN1_TYPE_GENERAL_STRING:
- $value = $source;
- break;
- case FILE_ASN1_TYPE_BOOLEAN:
- $value = $source ? "\xFF" : "\x00";
- break;
- default:
- user_error('Mapping provides no type definition for ' . implode('/', $this->location));
- return false;
- }
- if (isset($idx)) {
- array_pop($this->location);
- }
- if (isset($mapping['cast'])) {
- $tag = ($mapping['class'] << 6) | ($tag & 0x20) | $mapping['cast'];
- }
- return chr($tag) . $this->_encodeLength(strlen($value)) . $value;
- }
-
- function _encodeLength($length)
- {
- if ($length <= 0x7F) {
- return chr($length);
- }
- $temp = ltrim(pack('N', $length), chr(0));
- return pack('Ca*', 0x80 | strlen($temp), $temp);
- }
-
- function _decodeTime($content, $tag)
- {
-
- $pattern = $tag == FILE_ASN1_TYPE_UTC_TIME ?
- '#(..)(..)(..)(..)(..)(..)(.*)#' :
- '#(....)(..)(..)(..)(..)(..).*([Z+-].*)$#';
- preg_match($pattern, $content, $matches);
- list(, $year, $month, $day, $hour, $minute, $second, $timezone) = $matches;
- if ($tag == FILE_ASN1_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($hour, $minute, $second, $month, $day, $year) + $timezone;
- }
-
- function setTimeFormat($format)
- {
- $this->format = $format;
- }
-
- function loadOIDs($oids)
- {
- $this->oids = $oids;
- }
-
- function loadFilters($filters)
- {
- $this->filters = $filters;
- }
-
- function _string_shift(&$string, $index = 1)
- {
- $substr = substr($string, 0, $index);
- $string = substr($string, $index);
- return $substr;
- }
-
- function convert($in, $from = FILE_ASN1_TYPE_UTF8_STRING, $to = FILE_ASN1_TYPE_UTF8_STRING)
- {
- if (!isset($this->stringTypeSize[$from]) || !isset($this->stringTypeSize[$to])) {
- return false;
- }
- $insize = $this->stringTypeSize[$from];
- $outsize = $this->stringTypeSize[$to];
- $inlength = strlen($in);
- $out = '';
- for ($i = 0; $i < $inlength;) {
- if ($inlength - $i < $insize) {
- return false;
- }
-
- $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;
- }
-
- $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;
- }
- }
|