Skip to content

Commit 3a7aed4

Browse files
committed
implement small encryption key tests
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent 9f343f5 commit 3a7aed4

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

lib/Controller/ApiController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public function deleteUser(string $userId): DataResponse {
9494
public function getJwks(): JSONResponse {
9595
try {
9696
$jwks = $this->jwkService->getJwks();
97-
// return new JSONResponse(['keys' => $jwks]);
98-
return new JSONResponse($this->jwkService->debug());
97+
return new JSONResponse(['keys' => $jwks]);
98+
// return new JSONResponse($this->jwkService->debug());
9999
} catch (\Exception|\Throwable $e) {
100100
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
101101
}

lib/Service/JwkService.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ class JwkService {
2323
public const PEM_SIG_KEY_SETTINGS_KEY = 'pemSignatureKey';
2424
public const PEM_SIG_KEY_EXPIRES_AT_SETTINGS_KEY = 'pemSignatureKeyExpiresAt';
2525
public const PEM_SIG_KEY_EXPIRES_IN_SECONDS = 60 * 60;
26+
public const PEM_SIG_KEY_ALGORITHM = 'ES384';
27+
public const PEM_SIG_KEY_CURVE = 'P-384';
2628

2729
public const PEM_ENC_KEY_SETTINGS_KEY = 'pemEncryptionKey';
2830
public const PEM_ENC_KEY_EXPIRES_AT_SETTINGS_KEY = 'pemEncryptionKeyExpiresAt';
2931
public const PEM_ENC_KEY_EXPIRES_IN_SECONDS = 60 * 60;
32+
public const PEM_ENC_KEY_ALGORITHM = 'ECDH-ES+A192KW';
33+
public const PEM_ENC_KEY_CURVE = 'P-384';
3034

3135
public function __construct(
3236
private IAppConfig $appConfig,
@@ -127,10 +131,10 @@ public function getJwkFromSslKey(array $sslKeyDetails, bool $isEncryptionKey = f
127131
'kty' => 'EC',
128132
'use' => $isEncryptionKey ? 'enc' : 'sig',
129133
'kid' => ($isEncryptionKey ? 'enc' : 'sig') . '_key_' . $pemPrivateKeyExpiresAt,
130-
'crv' => 'P-384',
134+
'crv' => $isEncryptionKey ? self::PEM_ENC_KEY_CURVE : self::PEM_SIG_KEY_CURVE,
131135
'x' => \rtrim(\strtr(\base64_encode($sslKeyDetails['ec']['x']), '+/', '-_'), '='),
132136
'y' => \rtrim(\strtr(\base64_encode($sslKeyDetails['ec']['y']), '+/', '-_'), '='),
133-
'alg' => $isEncryptionKey ? 'ECDH-ES+A192KW' : 'ES384',
137+
'alg' => $isEncryptionKey ? self::PEM_ENC_KEY_ALGORITHM : self::PEM_SIG_KEY_ALGORITHM,
134138
];
135139
return $jwk;
136140
}
@@ -185,7 +189,7 @@ public function generateClientAssertion(Provider $provider, string $discoveryIss
185189
$payload['code'] = $code;
186190
}
187191

188-
return $this->createJwt($payload, $sslPrivateKey, 'sig_key_' . $pemPrivateKeyExpiresAt, 'ES384');
192+
return $this->createJwt($payload, $sslPrivateKey, 'sig_key_' . $pemPrivateKeyExpiresAt, self::PEM_SIG_KEY_ALGORITHM);
189193
}
190194

191195
public function debug(): array {
@@ -196,11 +200,11 @@ public function debug(): array {
196200

197201
$payload = ['lll' => 'aaa'];
198202
$pemPrivateKeyExpiresAt = $this->appConfig->getAppValueInt(self::PEM_SIG_KEY_EXPIRES_AT_SETTINGS_KEY, lazy: true);
199-
$signedJwtToken = $this->createJwt($payload, $sslPrivateKey, 'sig_key_' . $pemPrivateKeyExpiresAt, 'ES384');
203+
$signedJwtToken = $this->createJwt($payload, $sslPrivateKey, 'sig_key_' . $pemPrivateKeyExpiresAt, self::PEM_SIG_KEY_ALGORITHM);
200204

201205
// check content of JWT
202206
$rawJwks = ['keys' => [$this->getJwkFromSslKey($pubKey)]];
203-
$jwks = JWK::parseKeySet($rawJwks, 'ES384');
207+
$jwks = JWK::parseKeySet($rawJwks, self::PEM_SIG_KEY_ALGORITHM);
204208
$jwtPayload = JWT::decode($signedJwtToken, $jwks);
205209
$jwtPayloadArray = json_decode(json_encode($jwtPayload), true);
206210

tests/unit/Service/JwkServiceTest.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ public function testSignatureKeyAndJwt() {
4141
$initialPayload = ['nice' => 'example'];
4242
$pemPrivateKeyExpiresAt = $this->appConfig->getAppValueInt(JwkService::PEM_SIG_KEY_EXPIRES_AT_SETTINGS_KEY, lazy: true);
4343
$jwkId = 'sig_key_' . $pemPrivateKeyExpiresAt;
44-
$signedJwtToken = $this->jwkService->createJwt($initialPayload, $sslPrivateKey, $jwkId, 'ES384');
44+
$signedJwtToken = $this->jwkService->createJwt($initialPayload, $sslPrivateKey, $jwkId, JwkService::PEM_SIG_KEY_ALGORITHM);
4545

4646
// check JWK
4747
$jwk = $this->jwkService->getJwkFromSslKey($pubKey);
4848
$this->assertEquals('EC', $jwk['kty']);
4949
$this->assertEquals('sig', $jwk['use']);
5050
$this->assertEquals($jwkId, $jwk['kid']);
51-
$this->assertEquals('P-384', $jwk['crv']);
52-
$this->assertEquals('ES384', $jwk['alg']);
51+
$this->assertEquals(JwkService::PEM_SIG_KEY_CURVE, $jwk['crv']);
52+
$this->assertEquals(JwkService::PEM_SIG_KEY_ALGORITHM, $jwk['alg']);
5353

5454
// check content of JWT
5555
$rawJwks = ['keys' => [$jwk]];
56-
$jwks = JWK::parseKeySet($rawJwks, 'ES384');
56+
$jwks = JWK::parseKeySet($rawJwks, JwkService::PEM_SIG_KEY_ALGORITHM);
5757
$jwtPayload = JWT::decode($signedJwtToken, $jwks);
5858
$jwtPayloadArray = json_decode(json_encode($jwtPayload), true);
5959
$this->assertEquals($initialPayload, $jwtPayloadArray);
@@ -62,7 +62,23 @@ public function testSignatureKeyAndJwt() {
6262
$jwtParts = explode('.', $signedJwtToken, 3);
6363
$jwtHeader = json_decode(JWT::urlsafeB64Decode($jwtParts[0]), true);
6464
$this->assertEquals('JWT', $jwtHeader['typ']);
65-
$this->assertEquals('ES384', $jwtHeader['alg']);
65+
$this->assertEquals(JwkService::PEM_SIG_KEY_ALGORITHM, $jwtHeader['alg']);
6666
$this->assertEquals($jwkId, $jwtHeader['kid']);
6767
}
68+
69+
public function testEncryptionKey() {
70+
$myPemEncryptionKey = $this->jwkService->getMyEncryptionKey();
71+
$sslEncryptionKey = openssl_pkey_get_private($myPemEncryptionKey);
72+
$sslEncryptionKeyDetails = openssl_pkey_get_details($sslEncryptionKey);
73+
$encJwk = $this->jwkService->getJwkFromSslKey($sslEncryptionKeyDetails, isEncryptionKey: true);
74+
75+
$pemPrivateKeyExpiresAt = $this->appConfig->getAppValueInt(JwkService::PEM_ENC_KEY_EXPIRES_AT_SETTINGS_KEY, lazy: true);
76+
$encJwkId = 'enc_key_' . $pemPrivateKeyExpiresAt;
77+
78+
$this->assertEquals('EC', $encJwk['kty']);
79+
$this->assertEquals('enc', $encJwk['use']);
80+
$this->assertEquals($encJwkId, $encJwk['kid']);
81+
$this->assertEquals(JwkService::PEM_ENC_KEY_CURVE, $encJwk['crv']);
82+
$this->assertEquals(JwkService::PEM_ENC_KEY_ALGORITHM, $encJwk['alg']);
83+
}
6884
}

0 commit comments

Comments
 (0)