Skip to content

Commit

Permalink
Improve error handling and update dependencies
Browse files Browse the repository at this point in the history
Enhanced the error handling in KeyConverter and RSAKey files to include OpenSSL messages in exceptions. Updated the version of 'infection/infection' and 'qossmic/deptrac' in composer.json. Also, made modifications to 'deptrac.yaml' and renamed 'values' to 'expectedValues' in 'JWKFactoryTest.php'.
  • Loading branch information
Spomky committed Jun 1, 2024
1 parent 9627e5d commit 62e59a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"ext-sodium": "*",
"ekino/phpstan-banned-code": "^1.0",
"ergebnis/phpunit-slow-test-detector": "^2.14",
"infection/infection": "^0.28",
"infection/infection": "^0.28|^0.29",
"matthiasnoback/symfony-config-test": "^5.0",
"paragonie/sodium_compat": "^1.20 || ^2.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
Expand All @@ -80,7 +80,7 @@
"phpstan/phpstan-strict-rules": "^1.4",
"phpstan/phpstan-symfony": "^1.3",
"phpunit/phpunit": "^10.5.10|^11.0",
"qossmic/deptrac-shim": "^1.0",
"qossmic/deptrac": "^2.0",
"rector/rector": "^1.0",
"roave/security-advisories": "dev-latest",
"spomky-labs/aes-key-wrap": "^7.0",
Expand Down
23 changes: 12 additions & 11 deletions deptrac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ parameters:
layers:
- name: 'Library'
collectors:
- { type: className, regex: '^Jose\\Component\\' }
- { type: classLike, value: '^Jose\\Component\\' }
- name: 'Experimental'
collectors:
- { type: className, regex: '^Jose\\Experimental\\' }
- { type: classLike, value: '^Jose\\Experimental\\' }
- name: 'Bundle'
collectors:
- { type: className, regex: '^Jose\\Bundle\\' }
- { type: classLike, value: '^Jose\\Bundle\\' }
- name: 'Vendors'
collectors:
- { type: className, regex: '^Symfony\\' }
- { type: className, regex: '^SpomkyLabs\\Pki\\' }
- { type: className, regex: '^ParagonIE\\Sodium\\' }
- { type: className, regex: '^Psr\\EventDispatcher\\' }
- { type: className, regex: '^Psr\\Clock\\' }
- { type: className, regex: '^Brick\\Math\\' }
- { type: className, regex: '^AESKW\\' }
- { type: className, regex: '^ZxcvbnPhp\\' }
- { type: classLike, value: '^Symfony\\' }
- { type: classLike, value: '^SpomkyLabs\\Pki\\' }
- { type: classLike, value: '^ParagonIE\\Sodium\\' }
- { type: classLike, value: '^Psr\\EventDispatcher\\' }
- { type: classLike, value: '^Psr\\Clock\\' }
- { type: classLike, value: '^Brick\\Math\\' }
- { type: classLike, value: '^AESKW\\' }
- { type: classLike, value: '^ZxcvbnPhp\\' }
- { type: classLike, value: '^Psr\\' }
ruleset:
Library:
- 'Vendors'
Expand Down
5 changes: 4 additions & 1 deletion src/Library/KeyManagement/KeyConverter/KeyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,15 @@ private static function loadKeyFromPEM(string $pem, ?string $password = null): a
}

self::sanitizePEM($pem);
while (openssl_error_string()){};
$res = openssl_pkey_get_private($pem);
if ($res === false) {
$res = openssl_pkey_get_public($pem);
}
if ($res === false) {
throw new InvalidArgumentException('Unable to load the key.');
$opensslMessages = [];
while ($m = openssl_error_string()) {$opensslmessages[] = $m;}

Check failure on line 224 in src/Library/KeyManagement/KeyConverter/KeyConverter.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Implicit array creation is not allowed - variable $opensslmessages might not exist.
throw new InvalidArgumentException('Unable to load the key.', ['opensslmessages' => $opensslmessages]);

Check failure on line 225 in src/Library/KeyManagement/KeyConverter/KeyConverter.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #2 $code of class InvalidArgumentException constructor expects int, array<string, array<int, string>> given.

Check failure on line 225 in src/Library/KeyManagement/KeyConverter/KeyConverter.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Variable $opensslmessages might not be defined.
}

$details = openssl_pkey_get_details($res);
Expand Down
9 changes: 7 additions & 2 deletions src/Library/KeyManagement/KeyConverter/RSAKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,22 @@ public static function createFromPEM(string $pem): self
if (! extension_loaded('openssl')) {
throw new RuntimeException('Please install the OpenSSL extension');
}
while (openssl_error_string()){};
$res = openssl_pkey_get_private($pem);
if ($res === false) {
$res = openssl_pkey_get_public($pem);
}
if ($res === false) {
throw new InvalidArgumentException('Unable to load the key.');
$opensslMessages = [];
while ($m = openssl_error_string()) {$opensslmessages[] = $m;}

Check failure on line 78 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Implicit array creation is not allowed - variable $opensslmessages might not exist.
throw new InvalidArgumentException('Unable to load the key.', ['opensslmessages' => $opensslmessages]);

Check failure on line 79 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #2 $code of class InvalidArgumentException constructor expects int, array<string, array<int, string>> given.

Check failure on line 79 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Variable $opensslmessages might not be defined.
}

$details = openssl_pkey_get_details($res);
if (! is_array($details) || ! isset($details['rsa'])) {
throw new InvalidArgumentException('Unable to load the key.');
$opensslMessages = [];
while ($m = openssl_error_string()) {$opensslmessages[] = $m;}

Check failure on line 85 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Implicit array creation is not allowed - variable $opensslmessages might not exist.
throw new InvalidArgumentException('Unable to load the key.', ['opensslmessages' => $opensslmessages]);

Check failure on line 86 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #2 $code of class InvalidArgumentException constructor expects int, array<string, array<int, string>> given.

Check failure on line 86 in src/Library/KeyManagement/KeyConverter/RSAKey.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Variable $opensslmessages might not be defined.
}
$data = $details['rsa'];
if (! is_array($data)) {
Expand Down
18 changes: 9 additions & 9 deletions tests/Component/KeyManagement/JWKFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,55 +247,55 @@ public static function dataKeys(): iterable
{
yield [
'filename' => __DIR__ . '/Keys/ED/public-ed448.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'Ed448',
'x' => 'wwHKDV7s4fBhmFSTzYorlaToGXNcsa7SakZdekT_sexD5ENj5lWP6_KX9_u--w_QSm80rNOodj0A',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/public-ed25519.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'Ed25519',
'x' => 'wrI33AEj15KHHYplueUE5cnJKtbM8oVHFf6wGnw2oOE',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/public-X448.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'X448',
'x' => 'UoPD73NQACC8A-otDUVun4IrMsk775ShMRf4ThDrq4xY2eAI-pOIVujrvBXXd9g8gUNwBT0fmnc',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/public-X25519.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'X25519',
'x' => '3OJLiffmOCQGtil23QGyn0nk9EBKoZx6P-6o-EnsBB4',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/private-ed448.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'Ed448',
'd' => '0GXSbNLOh7NQBlwoF8y2WJmjeP5Puif4_JL4ihFUzRLrb_3r4cH8l_HWJA-2ffY62LEB_ozsehG5',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/private-X448.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'X448',
'd' => 'OHZK0Fp9MAAmk0yZekiAkB8qxpCVAF4dT2x_xmFNDdCTnyDvixaiZ0NSRpAdR59tA6OJmOFfbck',
],
];
yield [
'filename' => __DIR__ . '/Keys/ED/private-ed25519.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'Ed25519',
'd' => 'Pr9AxZivB-zSq95wLrZfYa7DQ3TUPqZTkP_0w33r3rc',
Expand All @@ -304,7 +304,7 @@ public static function dataKeys(): iterable
];
yield [
'filename' => __DIR__ . '/Keys/ED/private-secp384r1-with-public.pem',
'values' => [
'expectedValues' => [
'kty' => 'EC',
'crv' => 'P-384',
'd' => '31taDOPQnlNl2aBC_EaGTqVGjGN_qg6iuLwP6cVTmhKMQ5PTL67wS6mmyKi8GdVP',
Expand All @@ -314,7 +314,7 @@ public static function dataKeys(): iterable
];
yield [
'filename' => __DIR__ . '/Keys/ED/private-X25519.pem',
'values' => [
'expectedValues' => [
'kty' => 'OKP',
'crv' => 'X25519',
'd' => 'mG-fgDwkr58hwIeqCQKZbR8HKeY4yg_AzvU6zyNaVUE',
Expand Down

0 comments on commit 62e59a9

Please sign in to comment.