Skip to content

Commit

Permalink
Refactor Certificado extracting public key routine to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Oct 5, 2018
1 parent 8b79005 commit ec9eba0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
recepción de pagos, page 22* it is required that `pagos10:Pago@Monto` must be in an interval.
- Fix samples from `tests/assets/pagos/` since new validation make it fail.
- Rename validation class `MontoGreaterOrEqualThanSumOfDocuments` to `MontoBetweenIntervalSumOfDocuments`
- Refactor `CfdiUtils\Certificado\Certificado` extracting obtain public key routine to an internal method.
- Create tests for trait `CalculateDocumentAmountTrait`.


Expand Down
38 changes: 22 additions & 16 deletions src/CfdiUtils/Certificado/Certificado.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,7 @@ public function __construct(string $filename)
}

// get the public key
$pubkey = false;
$pubData = false;
try {
$pubkey = openssl_get_publickey($contents);
if (is_resource($pubkey)) {
$pubData = openssl_pkey_get_details($pubkey);
}
} finally {
if (is_resource($pubkey)) {
openssl_free_key($pubkey);
}
}
if (false === $pubData) {
$pubData = ['key' => ''];
}
$pubKey = $this->obtainPubKeyFromContents($contents);

// set all the values
$this->rfc = (string) strstr($data['subject']['x500UniqueIdentifier'] . ' ', ' ', true);
Expand All @@ -84,7 +70,7 @@ public function __construct(string $filename)
$this->serial = $serial->asAscii();
$this->validFrom = $data['validFrom_time_t'];
$this->validTo = $data['validTo_time_t'];
$this->pubkey = $pubData['key'];
$this->pubkey = $pubKey;
$this->pemContents = $contents;
$this->filename = $filename;
}
Expand Down Expand Up @@ -206,4 +192,24 @@ protected function changeCerToPem(string $contents): string
. chunk_split(base64_encode($contents), 64, PHP_EOL)
. '-----END CERTIFICATE-----' . PHP_EOL;
}

protected function obtainPubKeyFromContents(string $contents): string
{
try {
$pubkey = openssl_get_publickey($contents);
if (! is_resource($pubkey)) {
return '';
}
$pubData = openssl_pkey_get_details($pubkey);
if (false === $pubData) {
return '';
}
return $pubData['key'] ?? '';
} finally {
// close public key even if the flow is throw an exception
if (isset($pubkey) && is_resource($pubkey)) {
openssl_free_key($pubkey);
}
}
}
}

0 comments on commit ec9eba0

Please sign in to comment.