[903] | 1 | <?php |
---|
| 2 | require_once('Verifica_Certificado_conf.php'); |
---|
| 3 | require_once('funcoes_auxiliares.php'); |
---|
| 4 | |
---|
| 5 | class Verifica_Certificado |
---|
| 6 | { |
---|
| 7 | public $msgerro = ''; |
---|
| 8 | public $revogado = false; |
---|
| 9 | public $status = false; |
---|
| 10 | public $certificado = ''; |
---|
| 11 | public $CRL = ''; |
---|
| 12 | public $erros_ssl = array(); |
---|
| 13 | public $arquivos_para_deletar = array(); |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | public function __construct($parametro1,$parametro2) |
---|
| 17 | { |
---|
| 18 | //include('Verifica_Certificado_conf.php'); |
---|
| 19 | # $parametro1 = array com dados do certificado... |
---|
| 20 | # $parametro2 = certificado no formato PEM ... |
---|
| 21 | $this->status = false; |
---|
| 22 | if(!verificaopenssl()) |
---|
| 23 | { |
---|
| 24 | $this->msgerro = 'MSG005i - Modulo openssl nao disponivel no PHP.'; |
---|
| 25 | return false; |
---|
| 26 | } |
---|
| 27 | If(!$parametro2) |
---|
| 28 | { |
---|
| 29 | $this->msgerro = 'MSG005 - Certificado nao informado.'; |
---|
| 30 | return false; |
---|
| 31 | } |
---|
| 32 | $this->certificado = $parametro2; |
---|
| 33 | $aux = explode('/',$parametro1['CRLDISTRIBUTIONPOINTS']); |
---|
| 34 | $this->CRL = $aux[count($aux)-1]; |
---|
| 35 | |
---|
| 36 | if(!$this->cmd_openssl_verify($this->certificado)) |
---|
| 37 | { |
---|
| 38 | $this->msgerro = 'MSG010- Erro verificando Expiracao/CAs do certificado.'; |
---|
| 39 | return false; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | if($this->Testa_se_Certificado_Revogado(trim($GLOBALS['CRLs'].$this->CRL),$parametro1['SERIALNUMBER'])) |
---|
| 43 | { |
---|
| 44 | $this->msgerro = 'MSG011- Ocorreu erro validando o certificado.'; |
---|
| 45 | return false; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | $this->status = true; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | public function __destruct() |
---|
| 52 | { |
---|
| 53 | #Remover arquivos temporarios..... |
---|
| 54 | deleta_arquivos_temporarios(&$this->arquivos_para_deletar); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | private function cmd_openssl_verify($ZXZ) |
---|
| 58 | { |
---|
| 59 | $this->erros_ssl = array(); |
---|
| 60 | |
---|
| 61 | if(!is_file($GLOBALS['CAs'])) |
---|
| 62 | { |
---|
| 63 | # Se arquivo com cas nao existe, assume certificado invalido.. |
---|
| 64 | $this->erros_ssl[]= 'Autoridade certificadora desconhecida.(CA-01)'; //'Arquivo CAs nao localizado.'; |
---|
| 65 | return false; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | $arq = gera_nome_arquivo_temporario(&$this->arquivos_para_deletar); |
---|
| 69 | if(!$arq) |
---|
| 70 | { |
---|
| 71 | #Nao foi possivel gerar path(nome) para arquivo temporario... |
---|
| 72 | $this->erros_ssl[]= 'Autoridade certificadora desconhecida.(CA-02)'; //'Arquivo CAs nao localizado.'; |
---|
| 73 | return false; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | $ret = grava_arquivo($arq,$this->certificado); |
---|
| 77 | |
---|
| 78 | $saida = array(); |
---|
| 79 | |
---|
| 80 | $w = exec('openssl verify -CAfile '.$GLOBALS['CAs'].' '.$arq,$saida); |
---|
| 81 | deleta_arquivos_temporarios(&$this->arquivos_para_deletar); |
---|
| 82 | $this->arquivos_para_deletar = array(); |
---|
| 83 | //echo 'w= '.$w.'<br>'; |
---|
| 84 | //echo '<pre>'; |
---|
| 85 | //print_r($saida); |
---|
| 86 | //echo '</pre>'; |
---|
| 87 | $aux = explode(' ',$w); |
---|
| 88 | if($aux[1] != 'OK') // OK no primeiro item do array significa que o comando executaou OK... mas tem erro no certificado... |
---|
| 89 | { |
---|
| 90 | |
---|
| 91 | foreach($saida as $item) |
---|
| 92 | { |
---|
| 93 | $aux = explode(':',$item); |
---|
| 94 | if(isset($aux[1])) |
---|
| 95 | { |
---|
| 96 | $this->erros_ssl[] = trim($aux[1]); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | return false; |
---|
| 100 | } |
---|
| 101 | return true; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | private function Testa_se_Certificado_Revogado($pcrl,$serial) |
---|
| 105 | { |
---|
| 106 | $this->erros_ssl = array(); |
---|
| 107 | |
---|
| 108 | if(!file_exists($pcrl)) |
---|
| 109 | { |
---|
| 110 | # Se arquivo com crls nao existe, assume certificado revogado.. |
---|
| 111 | $this->erros_ssl[]= 'Nao foi possivel verificar se o certificado esta revogado.(CD-01)'; //'Arquivo CRL nao localizado.'; |
---|
| 112 | return true; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | if(!is_file($pcrl)) |
---|
| 116 | { |
---|
| 117 | # Se nao for um arquivo, assume certificado revogado.. |
---|
| 118 | $this->erros_ssl[]= 'Nao foi possivel verificar se o certificado esta revogado.(CD-01)'; //'Arquivo CRL nao localizado.'; |
---|
| 119 | return true; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | $crl = file_get_contents($pcrl,true); |
---|
| 124 | $cert_data = Crl_parseASN($crl); |
---|
| 125 | |
---|
| 126 | # testa se crl expirada.... |
---|
| 127 | if(gmdate('YmdHis') >= data_hora($cert_data[1][0][1][4][1])) |
---|
| 128 | { |
---|
| 129 | # Se crl expirada, assume certificado revogado.. |
---|
| 130 | $this->erros_ssl[]= 'Nao foi possivel verificar se o certificado esta revogado.(CD-02)'; //'Arquivo CRL expirado.'; |
---|
| 131 | return true; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | if (!is_array($cert_data) || ($cert_data[0] == 'UNKNOWN')) |
---|
| 135 | { |
---|
| 136 | # Se o primeiro parametro aponta uma crl invalida retorna como certificado revogado... |
---|
| 137 | $this->erros_ssl[]='Nao foi possivel verificar se o certificado esta revogado.(CD-03)'; //Arquivo CRL invalido. |
---|
| 138 | return true; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | foreach($cert_data[1][0][1][5][1] as $item) |
---|
| 142 | { |
---|
| 143 | if($serial === $item[1][0][1]) |
---|
| 144 | { |
---|
| 145 | #Certificado esta revogado mesmo !!!!! |
---|
| 146 | $this->erros_ssl[]='Certificado REVOGADO.'; |
---|
| 147 | $this->revogado = true; |
---|
| 148 | return true; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | return false; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | ?> |
---|