Skip to content

Commit

Permalink
Avoid PHP warnings about undefined array keys when sanitizing malform…
Browse files Browse the repository at this point in the history
…ed PEMs (#556)

* Avoid PHP warnings about undefined indexes when sanitizing malformed PEMs
* Add test for loading invalid PEM key

A new test function was added in ECKeysTest.php to handle cases of loading invalid PEM keys. An InvalidArgumentException is expected to be thrown with a specific error message when an invalid private PEM key is loaded.

---------

Co-authored-by: Florent Morselli <florent.morselli@spomky-labs.com>
  • Loading branch information
crocodele and Spomky authored Apr 17, 2024
1 parent 638ec0e commit 69586ce
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion KeyManagement/KeyConverter/KeyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ private static function getCurve(string $oid): string
*/
private static function sanitizePEM(string &$pem): void
{
preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER);
$number = preg_match_all('#(-.*-)#', $pem, $matches, PREG_PATTERN_ORDER);
if ($number !== 2) {
throw new InvalidArgumentException('Unable to load the key');
}

$ciphertext = preg_replace('#-.*-|\r|\n| #', '', $pem);

$pem = $matches[0][0] . PHP_EOL;
Expand Down

0 comments on commit 69586ce

Please sign in to comment.