Skip to content

Commit

Permalink
Merge pull request #1061 from nfephp-org/testes
Browse files Browse the repository at this point in the history
Incluindo manual para criação de certificados auto assinados de teste
  • Loading branch information
robmachado authored Jun 7, 2024
2 parents 03bb7f2 + b2fa2fd commit d97f8ed
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
90 changes: 90 additions & 0 deletions docs/GerarCertificadoAutoAssinadoParaTestes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Gerar Certificado auto assinado

## Para um CNPJ
1 - Gerar chave privada:
```
openssl genpkey -algorithm RSA -out private.key -pkeyopt rsa_keygen_bits:2048
```

2 - Crie o arquivo de configuração openssl.cnf:

```
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = v3_ca
req_extensions = v3_req
[ req_distinguished_name ]
C = BR
ST = São Paulo
L = São Paulo
O = Sua Empresa
OU = Unidade de TI
CN = 05.730.928/0001-45
emailAddress = email@suaempresa.com.br
[ v3_ca ]
subjectAltName = @alt_names
basicConstraints = CA:TRUE
keyUsage = keyCertSign, cRLSign
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
2.16.76.1.3.3 = ASN1:UTF8String:05730928000145
[ alt_names ]
email = email@suaempresa.com.br
```

3 - Gere o certificado autoassinado:

```bash
openssl req -new -x509 -days 3650 -key private.key -out certificate.crt -config openssl.cnf -extensions v3_req
```

4 - Converta para o formato PFX:

```bash
openssl pkcs12 -export -out certificate.pfx -inkey private.key -in certificate.crt -certfile certificate.crt -passout pass:minhasenha
```


## Para um CPF

Mesmos passos anteriores, exeto o arquivo openssl.cnf será esse abaixo.
Note que o oid 2.16.76.1.3.1 é composto por uma data de nascimento seguido do CPF.
2.16.76.1.3.1 = ASN1:UTF8String:ddddddddccccccccccc
Onde d é data de nascimento e c é o cpf somente números.

```
[ req ]
distinguished_name = req_distinguished_name
x509_extensions = v3_ca
req_extensions = v3_req
[ req_distinguished_name ]
C = BR
ST = São Paulo
L = São Paulo
O = SpedNfe
OU = Unidade de TI
CN = 904.839.260-86
emailAddress = email@suaempresa.com.br
[ v3_ca ]
subjectAltName = @alt_names
basicConstraints = CA:TRUE
keyUsage = keyCertSign, cRLSign
[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
2.16.76.1.3.1 = ASN1:UTF8String:1210198506157250000116
[ alt_names ]
email = email@suaempresa.com.br
```
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
Expand Down
25 changes: 25 additions & 0 deletions tests/CertificateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace NFePHP\NFe\Tests;

use NFePHP\Common\Certificate;
use PHPUnit\Framework\TestCase;

class CertificateTest extends TestCase
{
public function test_certificado_pj(): void
{
$conteudo = file_get_contents(TESTS_FIXTURES . '/certs/cert_cnpj_06157250000116_senha_minhasenha.pfx');
$certificado = Certificate::readPfx($conteudo, 'minhasenha');
$this->assertSame('06157250000116', $certificado->getCnpj());
$this->assertSame('05/06/2034', $certificado->getValidTo()->format('d/m/Y'));
}

public function test_certificado_pf(): void
{
$conteudo = file_get_contents(TESTS_FIXTURES . '/certs/cert_cpf_90483926086_minhasenha.pfx');
$certificado = Certificate::readPfx($conteudo, 'minhasenha');
$this->assertSame('90483926086', $certificado->getCpf());
$this->assertSame('03/06/2034', $certificado->getValidTo()->format('d/m/Y'));
}
}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

include dirname(__DIR__) . '/vendor/autoload.php';
include __DIR__ . '/constantes.php';
3 changes: 3 additions & 0 deletions tests/constantes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

define('TESTS_FIXTURES', __DIR__ . '/fixtures');
Binary file not shown.
Binary file not shown.

0 comments on commit d97f8ed

Please sign in to comment.