Skip to content

Commit 40821d6

Browse files
Merge pull request #51 from minvws/improve-insecure-ssl-key
Add generateInsecureOpenSSLKey function for testing
2 parents 20f88dc + 76bb10b commit 40821d6

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

tests/Feature/Http/Controllers/LoginControllerResponseTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
use function MinVWS\OpenIDConnectLaravel\Tests\{
2121
generateJwt,
22-
generateOpenSSLKey,
22+
generateInsecureOpenSSLKey,
2323
};
2424

2525
class LoginControllerResponseTest extends TestCase
@@ -530,7 +530,7 @@ public function testTokenSignedWithPrivateKey(): void
530530
Config::set('oidc.client_id', 'test-client-id');
531531

532532
// Set client private key
533-
[$key, $keyResource] = generateOpenSSLKey();
533+
[$key, $keyResource] = generateInsecureOpenSSLKey();
534534
Config::set('oidc.client_authentication.signing_private_key_path', stream_get_meta_data($keyResource)['uri']);
535535

536536
// Set the current state, which is usually generated and saved in the session before login,

tests/Feature/JweDecryptInterfaceBindingTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use OpenSSLCertificate;
1111

1212
use function MinVWS\OpenIDConnectLaravel\Tests\{
13-
generateOpenSSLKey,
13+
generateInsecureOpenSSLKey,
1414
generateX509Certificate,
1515
buildJweString,
1616
buildExamplePayload
@@ -27,7 +27,7 @@ class JweDecryptInterfaceBindingTest extends TestCase
2727

2828
public function setUp(): void
2929
{
30-
[$key, $keyResource] = generateOpenSSLKey();
30+
[$key, $keyResource] = generateInsecureOpenSSLKey();
3131
$this->decryptionKeyResource = $keyResource;
3232
$this->recipient = generateX509Certificate($key);
3333

tests/TestFunctions.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,30 @@ function buildExamplePayload(): string
6060

6161
/**
6262
* Generate OpenSSL Key and return the tempfile resource
63+
*
64+
* Warning: This function generates a key with 512 bits, which is considered insecure.
65+
* This is only for testing purposes.
66+
*
6367
* @return array{OpenSSLAsymmetricKey, resource}
6468
*/
65-
function generateOpenSSLKey(): array
69+
function generateInsecureOpenSSLKey(): array
70+
{
71+
return generateOpenSSLKey(bits: 512);
72+
}
73+
74+
/**
75+
* Generate OpenSSL Key and return the tempfile resource
76+
* @return array{OpenSSLAsymmetricKey, resource}
77+
*/
78+
function generateOpenSSLKey(int $bits = 2048): array
6679
{
6780
$file = tmpfile();
6881
if (!is_resource($file)) {
6982
throw new RuntimeException('Could not create temporary file');
7083
}
7184

7285
$key = openssl_pkey_new([
73-
'private_key_bits' => 512,
86+
'private_key_bits' => $bits,
7487
'private_key_type' => OPENSSL_KEYTYPE_RSA,
7588
]);
7689
if (!$key instanceof OpenSSLAsymmetricKey) {

tests/Unit/Services/JWE/JweDecryptServiceTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PHPUnit\Framework\TestCase;
1818

1919
use function MinVWS\OpenIDConnectLaravel\Tests\{
20-
generateOpenSSLKey,
20+
generateInsecureOpenSSLKey,
2121
generateX509Certificate,
2222
getJwkFromResource,
2323
buildJweString,
@@ -37,7 +37,7 @@ protected function setUp(): void
3737
{
3838
parent::setUp();
3939

40-
[$key, $keyResource] = generateOpenSSLKey();
40+
[$key, $keyResource] = generateInsecureOpenSSLKey();
4141
$this->decryptionKeyResource = $keyResource;
4242

4343
$this->decryptionKeySet = new JWKSet([
@@ -92,7 +92,7 @@ public function testJweDecryptionThrowsExceptionWhenKeyIsNotCorrect(): void
9292
$this->expectExceptionMessage('Failed to decrypt JWE');
9393

9494
// Create different key
95-
[$key, $keyResource] = generateOpenSSLKey();
95+
[$key, $keyResource] = generateInsecureOpenSSLKey();
9696
$jwk = getJwkFromResource($keyResource);
9797
$decryptionKeySet = new JWKSet([$jwk]);
9898

@@ -149,10 +149,10 @@ public function testJweDecryptionThrowsExceptionWhenPayloadIsNull(): void
149149
*/
150150
public function testJweDecryptionWithMultipleKeysInKeySet(): void
151151
{
152-
[$firstRecipientKey, $firstRecipientKeyResource] = generateOpenSSLKey();
152+
[$firstRecipientKey, $firstRecipientKeyResource] = generateInsecureOpenSSLKey();
153153
$firstRecipient = generateX509Certificate($firstRecipientKey);
154154

155-
[$secondRecipientKey, $secondRecipientKeyResource] = generateOpenSSLKey();
155+
[$secondRecipientKey, $secondRecipientKeyResource] = generateInsecureOpenSSLKey();
156156
$secondRecipient = generateX509Certificate($secondRecipientKey);
157157

158158
$payload = buildExamplePayload();

tests/Unit/Services/JWS/PrivateKeyJWTBuilderTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use PHPUnit\Framework\TestCase;
2121

2222
use function MinVWS\OpenIDConnectLaravel\Tests\{
23-
generateOpenSSLKey,
23+
generateInsecureOpenSSLKey,
2424
getJwkFromResource,
2525
};
2626

@@ -38,7 +38,7 @@ protected function setUp(): void
3838
{
3939
parent::setUp();
4040

41-
[$privateKey, $privateKeyResource] = generateOpenSSLKey();
41+
[$privateKey, $privateKeyResource] = generateInsecureOpenSSLKey();
4242

4343
$this->privateKey = $privateKey;
4444
$this->privateKeyResource = $privateKeyResource;

0 commit comments

Comments
 (0)