Skip to content

Commit 99bea9a

Browse files
authored
Merge pull request #8 from eclipxe13/master
Version 1.1.2
2 parents 5e1f391 + d239ba9 commit 99bea9a

File tree

13 files changed

+105
-24
lines changed

13 files changed

+105
-24
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
language: php
22

33
# php compatibility
4-
php: ["7.2", "7.3", "7.4"]
4+
php: ["7.2", "7.3", "7.4", "8.0"]
5+
6+
env:
7+
global:
8+
- PHP_CS_FIXER_IGNORE_ENV=yes
59

610
cache:
711
- directories:

docs/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66

77
Nos apegamos a [SEMVER](SEMVER.md), revisa la información para entender mejor el control de versiones.
88

9+
## Version 1.1.2 2020-12-20
10+
11+
- Desde esta versión se soporta PHP 8.0. Se hicieron cambios porque en la nueva versión de PHP la librería
12+
`openssl` ya no devuelve recursos y se deprecaron las funciones de liberación de recursos.
13+
- Se agregó la capacidad de abrir un archivo con el path `c:\archivos\certificado.cer`.
14+
- Se agregó información de cómo poder verificar un certificado usando la API del Gobierno de Colima.
15+
916
## Version 1.1.1 2020-01-22
1017

1118
- Weak Break Compatibility Change: `PemExtractor::__construct($contents)` se podría construir con un parámetro de

docs/TODO.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
# phpcfdi/credentials Tareas pendientes
1+
# phpcfdi/credentials To Do List
22

3-
- [X] Encontrar como diferenciar entre un archivo CSD y un archivo FIEL
4-
R: Se identifica por el campo OU (Organization Unit / Sucursal) del certificado, si está vacío es FIEL,
5-
si tiene contenido es CSD.
3+
## Tareas pendientes
64

75
- [ ] Verificar si un certificado fue realmente emitido por el SAT
86
Ver [VerificacionCertificadosSAT](VerificacionCertificadosSAT.md)
97

108
- [ ] Usar excepciones específicas en lugar de genéricas
9+
10+
## Tareas completadas
11+
12+
- Encontrar como diferenciar entre un archivo CSD y un archivo FIEL
13+
R: Se identifica por el campo OU (Organization Unit / Sucursal) del certificado, si está vacío es FIEL,
14+
si tiene contenido es CSD.

docs/VerificacionCertificadosSAT.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,31 @@ function extract() {
8484
extract "$CA_PROD_SOURCE" "$CA_PROD_DEST"
8585
extract "$CA_TEST_SOURCE" "$CA_TEST_DEST"
8686
```
87+
88+
## Verificación de certificados a través de la página del Gobierno de Colima
89+
90+
El Gobierno de Colima expone una API JSON en <https://apisnet.col.gob.mx/wsSignGob> que sirve para el propósito
91+
de verificar el estado de un certificado.
92+
93+
El principal inconveniente del servicio es que no establece la fecha de revocación.
94+
Por lo que el estado del certificado solo es relativo al momento de la consulta.
95+
96+
Ejemplo de consumo:
97+
98+
```shell
99+
curl -X POST -F 'certificado=@/path/to/certificate.cer' \
100+
https://apisnet.col.gob.mx/wsSignGob/apiV1/Valida/Certificado
101+
```
102+
103+
Ejemplo de respuesta:
104+
105+
```json
106+
{
107+
"RESTService": {
108+
"Message": "Certificado Aceptado ante el SAT"
109+
},
110+
"Response": {
111+
"OCSPStatus": "Revocado"
112+
}
113+
}
114+
```

phpstan.neon.dist

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Certificate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function convertDerToPem(string $contents): string
8080
* @param string $filename must be a local file (without scheme or file:// scheme)
8181
* @return Certificate
8282
*/
83-
public static function openFile(string $filename)
83+
public static function openFile(string $filename): self
8484
{
8585
return new self(static::localFileOpen($filename));
8686
}
@@ -92,7 +92,7 @@ public function pem(): string
9292

9393
public function pemAsOneLine(): string
9494
{
95-
return implode('', preg_grep('/^((?!-).)*$/', explode(PHP_EOL, $this->pem())));
95+
return implode('', preg_grep('/^((?!-).)*$/', explode(PHP_EOL, $this->pem())) ?: []);
9696
}
9797

9898
/**

src/Internal/Key.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Key
1313
private $type;
1414

1515
/** @param array<mixed> $dataArray */
16-
public function __construct($dataArray)
16+
public function __construct(array $dataArray)
1717
{
1818
$this->dataArray = $dataArray;
1919
}

src/Internal/LocalFileOpenTrait.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ private static function localFileOpen(string $filename): string
1515
if ('file://' === substr($filename, 0, 7)) {
1616
$filename = substr($filename, 7);
1717
}
18+
19+
if ('' === $filename) {
20+
throw new UnexpectedValueException('The file to open is empty');
21+
}
22+
1823
$scheme = strval(parse_url($filename, PHP_URL_SCHEME));
19-
if ('' !== $scheme) {
24+
if ('' !== $scheme && strlen($scheme) > 1) {
2025
throw new UnexpectedValueException('Invalid scheme to open file');
2126
}
2227

28+
$path = (realpath($filename) ?: '');
29+
if ('' === $path) {
30+
throw new RuntimeException('Unable to locate the file to open');
31+
}
32+
2333
/** @noinspection PhpUsageOfSilenceOperatorInspection */
24-
$contents = @file_get_contents($filename, false) ?: '';
34+
$contents = @file_get_contents($path, false) ?: '';
2535
if ('' === $contents) {
2636
throw new RuntimeException('File content is empty');
2737
}

src/PrivateKey.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpCfdi\Credentials\Internal\LocalFileOpenTrait;
1010
use RuntimeException;
1111
use UnexpectedValueException;
12+
use const PHP_VERSION_ID;
1213

1314
class PrivateKey extends Key
1415
{
@@ -117,7 +118,7 @@ function ($privateKey) use ($data, $algorithm): string {
117118
* This method id created to wrap and mock openssl_sign
118119
* @param string $data
119120
* @param string|null $signature
120-
* @param resource $privateKey
121+
* @param mixed $privateKey
121122
* @param int $algorithm
122123
* @return bool
123124
* @internal
@@ -149,13 +150,15 @@ function ($privateKey) use ($certificate): bool {
149150
public function callOnPrivateKey(Closure $function)
150151
{
151152
$privateKey = openssl_get_privatekey($this->pem(), $this->passPhrase());
152-
if (! is_resource($privateKey)) {
153+
if (false === $privateKey) {
153154
throw new RuntimeException('Cannot open private key: ' . openssl_error_string());
154155
}
155156
try {
156157
return call_user_func($function, $privateKey);
157158
} finally {
158-
openssl_free_key($privateKey);
159+
if (PHP_VERSION_ID < 80000) {
160+
openssl_free_key($privateKey);
161+
}
159162
}
160163
}
161164

src/PublicKey.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpCfdi\Credentials\Internal\Key;
99
use PhpCfdi\Credentials\Internal\LocalFileOpenTrait;
1010
use RuntimeException;
11+
use const PHP_VERSION_ID;
1112

1213
class PublicKey extends Key
1314
{
@@ -59,13 +60,17 @@ function ($publicKey) use ($data, $signature, $algorithm): bool {
5960
* This method id created to wrap and mock openssl_verify
6061
* @param string $data
6162
* @param string $signature
62-
* @param resource $publicKey
63+
* @param mixed $publicKey
6364
* @param int $algorithm
6465
* @return int
6566
*/
6667
protected function openSslVerify(string $data, string $signature, $publicKey, int $algorithm): int
6768
{
68-
return openssl_verify($data, $signature, $publicKey, $algorithm);
69+
$verify = openssl_verify($data, $signature, $publicKey, $algorithm);
70+
if (false === $verify) {
71+
return -1;
72+
}
73+
return $verify;
6974
}
7075

7176
/**
@@ -88,13 +93,15 @@ public function callOnPublicKey(Closure $function)
8893
private static function callOnPublicKeyWithContents(Closure $function, string $publicKeyContents)
8994
{
9095
$pubKey = openssl_get_publickey($publicKeyContents);
91-
if (! is_resource($pubKey)) {
96+
if (false === $pubKey) {
9297
throw new RuntimeException('Cannot open public key: ' . openssl_error_string());
9398
}
9499
try {
95100
return call_user_func($function, $pubKey);
96101
} finally {
97-
openssl_free_key($pubKey);
102+
if (PHP_VERSION_ID < 80000) {
103+
openssl_free_key($pubKey);
104+
}
98105
}
99106
}
100107
}

0 commit comments

Comments
 (0)