Skip to content

Commit

Permalink
Agora a validade do certificado é verificada antes do uso do certificado
Browse files Browse the repository at this point in the history
Criado variável de ambiente para saber o modo de execução da biblioteca
  • Loading branch information
mazinsw committed Feb 1, 2018
1 parent 61245a6 commit b8adf7a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
40 changes: 40 additions & 0 deletions api/NFe/Common/Configuracao.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Configuracao
private $chave_privada;
private $arquivo_chave_publica;
private $arquivo_chave_privada;
private $expiracao;
private $token;
private $csc;
private $token_ibpt;
Expand Down Expand Up @@ -108,6 +109,7 @@ public function getChavePublica()
public function setChavePublica($chave_publica)
{
$this->chave_publica = $chave_publica;
$this->carregaChavePublica();
return $this;
}

Expand Down Expand Up @@ -160,6 +162,20 @@ public function setArquivoChavePrivada($arquivo_chave_privada)
return $this;
}

/**
* Data de expiração do certificado em timestamp
*/
public function getExpiracao()
{
return $this->expiracao;
}

private function setExpiracao($expiracao)
{
$this->expiracao = $expiracao;
return $this;
}

/**
* Token do CSC
*/
Expand Down Expand Up @@ -284,6 +300,7 @@ public function toArray($recursive = false)
$configuracao['evento'] = $this->getEvento();
$configuracao['arquivo_chave_publica'] = $this->getArquivoChavePublica();
$configuracao['arquivo_chave_privada'] = $this->getArquivoChavePrivada();
$configuracao['expiracao'] = $this->getExpiracao();
$configuracao['token'] = $this->getToken();
$configuracao['csc'] = $this->getCSC();
$configuracao['token_ibpt'] = $this->getTokenIBPT();
Expand Down Expand Up @@ -362,4 +379,27 @@ public function fromArray($configuracao = array())
}
return $this;
}

private function carregaChavePublica()
{
if (is_null($this->getChavePublica())) {
$this->setExpiracao(null);
} else {
$cert = openssl_x509_read($this->getChavePublica());
$cert_data = openssl_x509_parse($cert);
$this->setExpiracao($cert_data['validTo_time_t']);
}
}

public function verificaValidadeCertificado()
{
if (getenv('APP_ENV') == 'testing') {
return;
}
if (is_null($this->getExpiracao())) {
throw new \Exception('A data de expiração do certificado não foi informada', 401);
} else if ($this->getExpiracao() < time()) {
throw new \Exception('O certificado digital expirou', 500);
}
}
}
1 change: 1 addition & 0 deletions api/NFe/Core/Nota.php
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,7 @@ public function assinar($dom = null)
$dom = $xml->ownerDocument;
}
$config = SEFAZ::getInstance()->getConfiguracao();
$config->verificaValidadeCertificado();

$adapter = new XmlseclibsAdapter();
$adapter->setPrivateKey($config->getChavePrivada());
Expand Down
1 change: 1 addition & 0 deletions api/NFe/Task/Envio.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ public function envia()
if ($config->isOffline()) {
throw new \NFe\Exception\NetworkException('Operação offline, sem conexão com a internet', 7);
}
$config->verificaValidadeCertificado();
$soap = new CurlSoap();
$soap->setConnectTimeout(intval($config->getTempoLimite()));
$soap->setTimeout(ceil($config->getTempoLimite() * 1.5));
Expand Down
1 change: 1 addition & 0 deletions api/NFe/Task/Evento.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ public function assinar($dom = null)
$dom = $xml->ownerDocument;
}
$config = SEFAZ::getInstance()->getConfiguracao();
$config->verificaValidadeCertificado();

$adapter = new XmlseclibsAdapter();
$adapter->setPrivateKey($config->getChavePrivada());
Expand Down
1 change: 1 addition & 0 deletions api/NFe/Task/Inutilizacao.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ public function assinar($dom = null)
$dom = $xml->ownerDocument;
}
$config = SEFAZ::getInstance()->getConfiguracao();
$config->verificaValidadeCertificado();

$adapter = new XmlseclibsAdapter();
$adapter->setPrivateKey($config->getChavePrivada());
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</filter>
<php>
<ini name="date.timezone" value="UTC"/>
<env name="APP_ENV" value="testing"/>
</php>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
Expand Down
8 changes: 8 additions & 0 deletions tests/NFe/Common/ConfiguracaoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ public function testObjetos()
$emitente = $this->config->getEmitente();
$this->assertNotNull($emitente);
}

public function testDataExpiracao()
{
$this->config
->setArquivoChavePublica(dirname(dirname(__DIR__)) . '/resources/certs/public.pem')
->setArquivoChavePrivada(dirname(dirname(__DIR__)) . '/resources/certs/private.pem');
$this->assertEquals('2010-10-02', date('Y-m-d', $this->config->getExpiracao()));
}
}

0 comments on commit b8adf7a

Please sign in to comment.