Ignore:
Timestamp:
04/12/11 09:27:42 (13 years ago)
Author:
rafaelraymundo
Message:

Ticket #1738 - Corrige falha no login com certificado digital.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/security/classes/funcoes_auxiliares.php

    r3563 r3984  
    11<?php 
    22//  Conjunto de funcoes de apoio as Classes de tratamento do certificado digital. 
    3          
     3 
    44                function pega_path($chaves,$linha) 
    55                { 
     
    3333                        return $ret; 
    3434                } 
    35          
     35 
    3636        function ler_certificados_CAS($path,$flag=false) 
    3737            { 
     
    9898                    return  $aux_xml; 
    9999                } 
    100                  
    101                  
    102         function data_hora ($valor)  
     100 
     101 
     102        function data_hora ($valor) 
    103103        { 
    104104                $year  = substr($valor, 0, 2); 
     
    123123                $N = $lista[rand(0,count($lista)-1)].date('U').$lista[rand(0,count($lista)-1)].RAND(12345,9999999999).$lista[rand(0,count($lista)-1)].$lista[rand(0,count($lista)-1)].RAND(12345,9999999999).'.tmp'; 
    124124                $aux = $GLOBALS['dirtemp'].'/'.$N;; 
    125                 array_push($tab_arqs ,$aux);  
    126                 return  $aux;            
     125                array_push($tab_arqs ,$aux); 
     126                return  $aux; 
    127127        } 
    128128 
     
    138138                        return $ret; 
    139139        } 
    140          
    141          
     140 
     141 
    142142        function deleta_arquivos_temporarios($tab_arqs) 
    143143        { 
    144144                foreach($tab_arqs as $arquivo ) 
    145                         {  
     145                        { 
    146146                                if(file_exists($arquivo)) 
    147147                                        { 
    148148                                                unlink($arquivo); 
    149149                                        } 
    150                         }        
    151         } 
    152  
    153  
     150                        } 
     151        } 
     152 
     153        function troca_espaco_por_mais($pem_data) 
     154        { 
     155            $begin = "CERTIFICATE-----"; 
     156            $end   = "-----END"; 
     157            $aux = substr($pem_data, strpos($pem_data, $begin)+strlen($begin)); 
     158            $aux = substr($aux, 0, strpos($aux, $end)); 
     159            $aux = strtr($aux,' ','+'); 
     160            $aux = '-----BEGIN CERTIFICATE-----'.$aux.'-----END CERTIFICATE-----'; 
     161            return $aux; 
     162        } 
    154163        function verificaopenssl() 
    155164        { 
     
    165174        function print_hex($value) 
    166175        { 
    167                 $tab_val = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');  
     176                $tab_val = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); 
    168177                for($A=0;$A<strlen($value);$A++) 
    169178                        { 
     
    184193                # Testa se tamanho menor/igual a 127 bytes.. 
    185194                # Neste caso $len  ja he o tamanho do conteudo 
    186                 if ($len & 0x80)  
     195                if ($len & 0x80) 
    187196                        { 
    188197                                # Testa se tamanho indefinido (nao deve ocorrer em uma codificação DER. 
     
    193202                                                $bytes = 0; 
    194203                                        } 
    195                                 else                                     
    196                                         { 
    197                                                 # he tamanho definido. diz quantos bytes formam o tamanho....  
     204                                else 
     205                                        { 
     206                                                # he tamanho definido. diz quantos bytes formam o tamanho.... 
    198207                                                $bytes = $len & 0x0f; 
    199208                                                $len = 0; 
    200                                                 for ($i = 0; $i < $bytes; $i++)  
     209                                                for ($i = 0; $i < $bytes; $i++) 
    201210                                                        { 
    202211                                                                $len = ($len << 8) | ord($data[$i + 2]); 
     
    205214                        } 
    206215        } 
    207          
     216 
    208217        function xBase128($ab,$q,$flag ) 
    209218        { 
    210                 $abc = $ab;  
     219                $abc = $ab; 
    211220                if( $q > 127 ) 
    212221                        { 
     
    256265                return $value; 
    257266        } 
    258          
    259          
    260          
     267 
     268 
     269 
    261270        function Crl_parseASN($data,$context_especific = false) 
    262         {   
     271        { 
    263272        // Tabela de OIDs . 
    264273                $_oids = array( 
     
    290299                        '1.2.840.10040.4.3' => 'id-dsa-with-sha-1', 
    291300                        '1.3.6.1.5.5.7.3.2' => 'id_kp_clientAuth', 
    292                         '1.3.6.1.5.5.7.3.4' => 'id_kp_securityemail',    
     301                        '1.3.6.1.5.5.7.3.4' => 'id_kp_securityemail', 
    293302                        '1.3.6.1.5.5.7.2.1' => 'id_certificatePolicies', 
    294303                        '2.16.840.1.113730.1.1' => 'netscape-cert-type', 
     
    300309                        '2.16.840.1.113730.1.12' => 'netscape-ssl-server-name', 
    301310                        '2.16.840.1.113730.1.13' => 'netscape-comment', 
    302                         '2.16.76.1.2.1' => 'A1',         
    303                         '2.16.76.1.2.3' => 'A3',         
     311                        '2.16.76.1.2.1' => 'A1', 
     312                        '2.16.76.1.2.3' => 'A3', 
    304313                        '2.16.76.1.2.1.16' => 'Certification Practice Statement pointer', 
    305314                        '2.16.76.1.3.1' => 'Dados do cert parte 1', 
     
    11321141 
    11331142                $result = array(); 
    1134                  
    1135                 while (strlen($data) > 1)  
     1143 
     1144                while (strlen($data) > 1) 
    11361145                { 
    11371146                        $class = ord($data[0]); 
    1138                         switch ($class)  
     1147                        switch ($class) 
    11391148                        { 
    11401149                                case 0x30: 
     
    11461155                                        $data = substr($data, 2 + $bytes + $len); 
    11471156                                        $values = Crl_parseASN($sequence_data); 
    1148                                         if (!is_array($values) || is_string($values[0]))  
     1157                                        if (!is_array($values) || is_string($values[0])) 
    11491158                                        { 
    11501159                                                $values = array($values); 
     
    11691178                                        $result[] = array('boolean (1)' , $boolean_value); 
    11701179                                        break; 
    1171                                  
     1180 
    11721181                                case 0x02: 
    11731182                                        // Integer type 
     
    11881197                                        { 
    11891198                                                $value = 0; 
    1190                                                 if ($len <= 4)  
     1199                                                if ($len <= 4) 
    11911200                                                { 
    11921201                                                        // Method works fine for small integers 
    1193                                                         for ($i = 0; $i < strlen($integer_data); $i++)  
     1202                                                        for ($i = 0; $i < strlen($integer_data); $i++) 
    11941203                                                        { 
    11951204                                                                $value = ($value << 8) | ord($integer_data[$i]); 
    11961205                                                        } 
    1197                                                 }  
    1198                                                 else  
     1206                                                } 
     1207                                                else 
    11991208                                                { 
    1200                                                         // Method works for arbitrary length integers  
    1201                                                         if (extension_loaded('bcmath'))  
     1209                                                        // Method works for arbitrary length integers 
     1210                                                        if (extension_loaded('bcmath')) 
    12021211                                                        { 
    1203                                                                 for ($i = 0; $i < strlen($integer_data); $i++)  
     1212                                                                for ($i = 0; $i < strlen($integer_data); $i++) 
    12041213                                                                { 
    12051214                                                                        $value = bcadd(bcmul($value, 256), ord($integer_data[$i])); 
    12061215                                                                } 
    1207                                                         }  
    1208                                                         else  
     1216                                                        } 
     1217                                                        else 
    12091218                                                        { 
    12101219                                                                $value = -1; 
     
    12271236 
    12281237                                case 0x04: 
    1229                                         // Octetstring type  
     1238                                        // Octetstring type 
    12301239                                        $len = ord($data[1]); 
    12311240                                        $bytes = 0; 
     
    12421251                                        } 
    12431252                                        break; 
    1244                                          
     1253 
    12451254                                case 0x0C: 
    12461255                                        // UTF8 STRING 
     
    12811290                                        $value = 0; 
    12821291                                        $i = 1; 
    1283                                         while ($i < strlen($oid_data))  
     1292                                        while ($i < strlen($oid_data)) 
    12841293                                        { 
    12851294                                                $value = $value << 7; 
    12861295                                                $value = $value | (ord($oid_data[$i]) & 0x7f); 
    12871296 
    1288                                                 if (!(ord($oid_data[$i]) & 0x80))  
     1297                                                if (!(ord($oid_data[$i]) & 0x80)) 
    12891298                                                { 
    12901299                                                        $plain .= '.' . $value; 
     
    12941303                                        } 
    12951304 
    1296                                         if (isset($_oids[$plain]))  
     1305                                        if (isset($_oids[$plain])) 
    12971306                                        { 
    12981307                                                $result[] =  array('oid(' . $len . '): '  . $plain, $_oids[$plain]); 
    1299                                         }  
    1300                                         else  
     1308                                        } 
     1309                                        else 
    13011310                                        { 
    13021311                                                $result[] = array('oid(' . $len . '): '  . $plain, $plain); 
     
    13131322                                        $result[] = array('IA5 String (' . $len . ')'  , $string_data); 
    13141323                                        break; 
    1315                                          
     1324 
    13161325                                case 0x12: 
    13171326                                case 0x14: 
    1318                                 case 0x15:       
    1319                                 case 0x81:                       
     1327                                case 0x15: 
     1328                                case 0x81: 
    13201329                                        // Character string type 
    13211330                                        $len = ord($data[1]); 
     
    13361345                                        $result[] = array('string (' . $len . ')'  , print_hex($string_data)); 
    13371346                                        break; 
    1338                                          
     1347 
    13391348                                case 0x13: 
    13401349                                case 0x86: 
     
    13791388                                        $result[] = array('Context Especific (' . $len . ')' , array(Crl_parseASN($extension_data,true))); 
    13801389                                        break; 
    1381                                          
     1390 
    13821391                                case 0xa3: 
    13831392                                        // Extensions 
     
    14141423 
    14151424   } 
    1416     
    1417    function openssl_to_timestamp ($in)  
     1425 
     1426   function openssl_to_timestamp ($in) 
    14181427   { 
    14191428        $year  = substr($in, 0, 4); /* NOTE: Yes, this returns a two digit year */ 
     
    14231432        $min   = substr($in, 10, 2); 
    14241433        $sec   = substr($in, 12, 2); 
    1425      
     1434 
    14261435        return gmmktime($hour, $min, $sec, $month, $day, $year); 
    14271436        } 
     
    14291438#============================================================================================ 
    14301439# Transforma o certificado do formato PEM para o formato DER ... 
    1431 function pem2der($pem_data)  
     1440function pem2der($pem_data) 
    14321441        { 
    14331442                $begin = "CERTIFICATE-----"; 
    14341443                $end   = "-----END"; 
    1435                 $pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin));     
     1444                $pem_data = substr($pem_data, strpos($pem_data, $begin)+strlen($begin)); 
    14361445                $pem_data = substr($pem_data, 0, strpos($pem_data, $end)); 
    14371446                $der = base64_decode($pem_data); 
     
    14421451function testa_p7m($msg) 
    14431452        { 
    1444                 // oids pesquisadas:  
     1453                // oids pesquisadas: 
    14451454                //                                1.2.840.113549.1.7.2     assinatura digital 
    14461455                //                                 1.2.840.113549.1.7.3     envelopeddata 
     
    14551464                                        return 'signature' ; 
    14561465                        } 
    1457                 $oid_hexa = OIDtoHex('1.2.840.113549.1.7.3');   
     1466                $oid_hexa = OIDtoHex('1.2.840.113549.1.7.3'); 
    14581467                $partes = explode($oid_hexa,$p7m_formato_der);    // Faz o split pela oid... 
    14591468                if(count($partes)>1) 
    14601469                        { 
    14611470                                        return 'cipher' ; 
    1462                         }                        
     1471                        } 
    14631472                return 'normal'; 
    14641473        } 
     
    14921501                                                        { 
    14931502                                                                $xcv = $xcv2; 
    1494                                                                 $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia.....         
     1503                                                                $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia..... 
    14951504                                                                $ret = parse_sequence($data); 
    14961505 
     
    15041513                                                        { 
    15051514                                                                $xcv = $xcv3; 
    1506                                                                 $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia.....         
     1515                                                                $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia..... 
    15071516                                                                $ret = parse_sequence($data); 
    15081517                                                                if($ret[0] != '') 
     
    15151524                                                        { 
    15161525                                                                $xcv = $xcv4; 
    1517                                                                 $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia.....         
     1526                                                                $data = $xcv . $oid_hexa . $partes[$i];           // reconstroi a sequencia..... 
    15181527                                                                $ret = parse_sequence($data); 
    15191528                                                                if($ret[0] != '') 
     
    15211530                                                                                $retr[] = $ret; 
    15221531                                                                                continue; 
    1523                                                                         }                                                                                
    1524                                                         }                                                        
    1525                                         }  
     1532                                                                        } 
     1533                                                        } 
     1534                                        } 
    15261535                        } 
    15271536                return $retr; 
     
    15351544                //  OID's PESSOA FISICA = 2.16.76.1.3.1 ,  2.16.76.1.3.6 ,  2.16.76.1.3.5 ,  2.16.76.1.4.n ... as 2.16.1.4.n não são obrigatórias e ão sao tratadas.. 
    15361545                // 
    1537                 //  OID's PESSOA JURIDICA = 2.16.76.1.3.4 ,  2.16.76.1.3.2 , 2.16.76.1.3.3 , 2.16.76.1.3.7  
     1546                //  OID's PESSOA JURIDICA = 2.16.76.1.3.4 ,  2.16.76.1.3.2 , 2.16.76.1.3.3 , 2.16.76.1.3.7 
    15381547                // 
    1539                 //  OID's EQUIPAMENTO/APLICAÇÃO = 2.16.76.1.3.8 ,  2.16.76.1.3.3 , 2.16.76.1.3.2 , 2.16.76.1.3.4  
    1540                 //       
     1548                //  OID's EQUIPAMENTO/APLICAÇÃO = 2.16.76.1.3.8 ,  2.16.76.1.3.3 , 2.16.76.1.3.2 , 2.16.76.1.3.4 
     1549                // 
    15411550                //  OID  para logon no NT:  1.3.6.1.4.1.311.20.2.3 
    15421551                // 
     
    15521561                                                               '3'=>array('NIS',11), 
    15531562                                                               '4'=>array('RG',15), 
    1554                                                                '5'=>array('ORGAOUF',6)),         
     1563                                                               '5'=>array('ORGAOUF',6)), 
    15551564                                     '2.16.76.1.3.5' => array('1'=>array('TITULO',12), 
    15561565                                                              '2'=>array('ZONA',3), 
    15571566                                                              '3'=>array('SECAO',4), 
    1558                                                               '4'=>array('TITULO_CIDADE_UF',0)),         
    1559                                      '2.16.76.1.3.6' => array('1'=>array('CADINSS',12)),                                                               
    1560                                      '2.16.76.1.3.7' => array('1'=>array('CEI',12)),                                                                    
     1567                                                              '4'=>array('TITULO_CIDADE_UF',0)), 
     1568                                     '2.16.76.1.3.6' => array('1'=>array('CADINSS',12)), 
     1569                                     '2.16.76.1.3.7' => array('1'=>array('CEI',12)), 
    15611570                                     '2.16.76.1.3.8' => array('1'=>array('NOMEEMPRESARIAL',0)), 
    1562                                      '1.3.6.1.4.1.311.20.2.3' => array('1'=>array('NTNOMEPRINCIPAL',0)));                                     
    1563                                       
    1564                 $resultado = array();  
     1571                                     '1.3.6.1.4.1.311.20.2.3' => array('1'=>array('NTNOMEPRINCIPAL',0))); 
     1572 
     1573                $resultado = array(); 
    15651574                $esta_oid = $oids[$oid]; 
    15661575                $p = 0; 
     
    15751584                                        { 
    15761585                                                $tamanho = $esta_oid[$i][1]; 
    1577                                         }  
     1586                                        } 
    15781587                                $resultado[$oid][$esta_oid[$i][0]] = substr($valor,$p,$tamanho); 
    15791588                                // A linha logo abaixo he para manter compatibilidade com versoes anteriores... Sera desativada assim que possivel... 
     
    15831592 
    15841593                return $resultado; 
    1585         }   
    1586  
    1587          
     1594        } 
     1595 
     1596 
    15881597function subjectAltName($xx, $certificado_digital_formato_der) 
    15891598        { 
     
    16211630        } 
    16221631 
    1623          
     1632 
    16241633function CRLDistributionPointsxx($xx, $certificado_digital_formato_der) 
    16251634        { 
     
    16291638                echo '</pre><br/><br/>'; 
    16301639                exit(); 
    1631                  
    1632                  
     1640 
     1641 
    16331642                $i=1; 
    16341643                if(substr($AUX[0][1][0],0,7) == 'boolean') 
     
    16381647                $AUX1 = $AUX[0][$i][1][1][0]; 
    16391648                // Pode existir mais de uma crl, mas vamos considerar apenas a primeira..... 
    1640                 return array('CRLDISTRIBUTIONPOINTS' => $AUX1[1][0][1][0][1][0][1]);             
    1641         }        
     1649                return array('CRLDISTRIBUTIONPOINTS' => $AUX1[1][0][1][0][1][0][1]); 
     1650        } 
    16421651 
    16431652function CRLDistributionPoints($xx, $certificado_digital_formato_der) 
     
    16641673                                          } 
    16651674                        } 
    1666                  
     1675 
    16671676                // Se $ret esta vazio tenta obter crls em outra estrutura(outro layout). 
    16681677                if(count($ret) == 0) 
     
    16801689                                          } 
    16811690                        } 
    1682                          
     1691 
    16831692                return array('CRLDISTRIBUTIONPOINTS' => $ret); 
    1684         }        
    1685  
    1686          
     1693        } 
     1694 
     1695 
    16871696function SERIALNUMBER($cert_data,$KK) 
    16881697        { 
     
    16921701                                $dados['SERIALNUMBER'] = $cert_data[1][0][1][$KK][1]; 
    16931702                        } 
    1694                 return $dados;                   
    1695         } 
    1696          
     1703                return $dados; 
     1704        } 
     1705 
    16971706function SUBJECT($cert_data,$KK) 
    16981707        { 
     
    17051714                        $AUX = explode(':',$dados['SUBJECT']['CN']); 
    17061715                $dados['NOME'] = $AUX[0]; 
    1707                 return $dados;                   
    1708         } 
    1709                  
    1710 function ISSUER($cert_data,$KK)          
    1711         { 
    1712                 $dados = array();        
     1716                return $dados; 
     1717        } 
     1718 
     1719function ISSUER($cert_data,$KK) 
     1720        { 
     1721                $dados = array(); 
    17131722                $dados['EMISSOR_CAMINHO_COMPLETO']  = array(); 
    1714                  
     1723 
    17151724                foreach($cert_data[1][0][1][$KK][1] as $AUX2) 
    17161725                        { 
     
    17181727                        } 
    17191728                $dados['EMISSOR'] = $dados['EMISSOR_CAMINHO_COMPLETO']['CN']; 
    1720                 return $dados;   
    1721         }                
    1722  
    1723          
     1729                return $dados; 
     1730        } 
     1731 
     1732 
    17241733function BEFOREAFTER($cert_data,$KK) 
    17251734        { 
     
    17351744                        { 
    17361745                                $dados['EXPIRADO'] = false; 
    1737                         }        
    1738                 return $dados;           
    1739                 }        
     1746                        } 
     1747                return $dados; 
     1748                } 
    17401749 
    17411750 
     
    17581767                                $dados['AUTHORITYKEYIDENTIFIER'] = "auto-assinado"; 
    17591768                        } 
    1760                 return $dados;                   
    1761         } 
    1762          
     1769                return $dados; 
     1770        } 
     1771 
    17631772function KEYUSAGE($xx, $certificado_digital_formato_der) 
    17641773        { 
    1765                 $KeyUsage= array( 0x80 => 'digitalSignature',  
     1774                $KeyUsage= array( 0x80 => 'digitalSignature', 
    17661775                              0x40 => 'nonRepudiation', 
    17671776                              0x20 => 'keyEncipherment', 
     
    17861795                                                        } 
    17871796                                        } 
    1788                         }        
    1789                 return $dados;                   
     1797                        } 
     1798                return $dados; 
    17901799        } 
    17911800 
     
    18061815                                } 
    18071816                        } 
    1808                 return $dados;                   
    1809         } 
    1810          
     1817                return $dados; 
     1818        } 
     1819 
    18111820function BASICCONSTRAINTS($xx, $certificado_digital_formato_der) 
    18121821        { 
     
    18201829                                        } 
    18211830                        } 
    1822                 return $dados;                   
    1823         } 
    1824          
     1831                return $dados; 
     1832        } 
     1833 
    18251834function recupera_dados_do_ceritificado_digital($certificado_digital_formato_pem) 
    18261835        { 
    18271836                $cert_der =  pem2der($certificado_digital_formato_pem); 
    1828                  
     1837 
    18291838                $funcoes = array(SERIALNUMBER => 1, 
    18301839                                        ISSUER => 3, 
     
    18371846                                        CRLDistributionPoints =>  $cert_der, 
    18381847                                        subjectAltName => $cert_der); 
    1839                             
     1848 
    18401849                $dados=array(); 
    1841                  
     1850 
    18421851                $cert_data = Crl_parseASN( $cert_der); 
    1843                  
     1852 
    18441853                foreach($funcoes as $funcao => $parametro) 
    18451854                        { 
     
    18541863                                        } 
    18551864                        } 
    1856                 return $dados;           
     1865                return $dados; 
    18571866        } 
    18581867 
Note: See TracChangeset for help on using the changeset viewer.