Skip to content

Commit e8c6730

Browse files
authored
ECDH-SS support (#366)
* ECDH-SS support
1 parent 272f38e commit e8c6730

File tree

15 files changed

+881
-353
lines changed

15 files changed

+881
-353
lines changed

.github/dependabot.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
version: 2
22
updates:
3-
- package-ecosystem: composer
4-
directory: "/"
5-
schedule:
6-
interval: daily
7-
time: "11:00"
8-
open-pull-requests-limit: 10
3+
- package-ecosystem: "composer"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
day: "friday"
8+
versioning-strategy: "increase"
9+
open-pull-requests-limit: 20
10+
allow:
11+
- dependency-type: all
12+
labels: ["Dependencies"]
13+
14+
- package-ecosystem: "github-actions"
15+
directory: "/"
16+
schedule:
17+
interval: "monthly"
18+
open-pull-requests-limit: 20
19+
labels: ["Dependencies"]

src/Bundle/JoseFramework/DataCollector/AlgorithmCollector.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Jose\Bundle\JoseFramework\DataCollector;
66

77
use function array_key_exists;
8-
use function function_exists;
98
use Jose\Component\Core\Algorithm;
109
use Jose\Component\Core\AlgorithmManagerFactory;
1110
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
@@ -93,7 +92,7 @@ private function getAlgorithmType(
9392

9493
private function getAlgorithmMessages(): array
9594
{
96-
$messages = [
95+
return [
9796
'none' => [
9897
'severity' => 'severity-low',
9998
'message' => 'This algorithm is not secured. Please use with caution.',
@@ -195,27 +194,5 @@ private function getAlgorithmMessages(): array
195194
'message' => 'This algorithm is not secured (known attacks). See <a target="_blank" href="https://tools.ietf.org/html/draft-irtf-cfrg-webcrypto-algorithms-00#section-5">https://tools.ietf.org/html/draft-irtf-cfrg-webcrypto-algorithms-00#section-5</a>.',
196195
],
197196
];
198-
if (! function_exists('openssl_pkey_derive')) {
199-
$messages += [
200-
'ECDH-ES' => [
201-
'severity' => 'severity-medium',
202-
'message' => 'This algorithm is very slow when used with curves P-256, P-384, P-521 with php 7.2 and below.',
203-
],
204-
'ECDH-ES+A128KW' => [
205-
'severity' => 'severity-medium',
206-
'message' => 'This algorithm is very slow when used with curves P-256, P-384, P-521 with php 7.2 and below.',
207-
],
208-
'ECDH-ES+A192KW' => [
209-
'severity' => 'severity-medium',
210-
'message' => 'This algorithm is very slow when used with curves P-256, P-384, P-521 with php 7.2 and below.',
211-
],
212-
'ECDH-ES+A256KW' => [
213-
'severity' => 'severity-medium',
214-
'message' => 'This algorithm is very slow when used with curves P-256, P-384, P-521 with php 7.2 and below.',
215-
],
216-
];
217-
}
218-
219-
return $messages;
220197
}
221198
}

src/Bundle/JoseFramework/Resources/config/Algorithms/encryption_ecdhes.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHESA128KW;
77
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHESA192KW;
88
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHESA256KW;
9+
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHSS;
10+
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHSSA128KW;
11+
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHSSA192KW;
12+
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHSSA256KW;
913

1014
/*
1115
* The MIT License (MIT)
@@ -44,4 +48,24 @@
4448
->tag('jose.algorithm', [
4549
'alias' => 'ECDH-ES+A256KW',
4650
]);
51+
52+
$container->set(ECDHSS::class)
53+
->tag('jose.algorithm', [
54+
'alias' => 'ECDH-SS',
55+
]);
56+
57+
$container->set(ECDHSSA128KW::class)
58+
->tag('jose.algorithm', [
59+
'alias' => 'ECDH-SS+A128KW',
60+
]);
61+
62+
$container->set(ECDHSSA192KW::class)
63+
->tag('jose.algorithm', [
64+
'alias' => 'ECDH-SS+A192KW',
65+
]);
66+
67+
$container->set(ECDHSSA256KW::class)
68+
->tag('jose.algorithm', [
69+
'alias' => 'ECDH-SS+A256KW',
70+
]);
4771
};

src/Component/Encryption/JWEBuilder.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
class JWEBuilder
2929
{
30+
protected ?JWK $senderKey = null;
31+
3032
protected ?string $payload = null;
3133

3234
protected ?string $aad = null;
@@ -55,6 +57,7 @@ public function __construct(
5557
*/
5658
public function create(): self
5759
{
60+
$this->senderKey = null;
5861
$this->payload = null;
5962
$this->aad = null;
6063
$this->recipients = [];
@@ -188,6 +191,31 @@ public function addRecipient(JWK $recipientKey, array $recipientHeader = []): se
188191
return $clone;
189192
}
190193

194+
//TODO: Verify if the key is compatible with the key encrytion algorithm like is done to the ECDH-ES
195+
/**
196+
* Set the sender JWK to be used instead of the internal generated JWK
197+
*/
198+
public function withSenderKey(JWK $senderKey): self
199+
{
200+
$clone = clone $this;
201+
$completeHeader = array_merge($clone->sharedHeader, $clone->sharedProtectedHeader);
202+
$keyEncryptionAlgorithm = $clone->getKeyEncryptionAlgorithm($completeHeader);
203+
if ($clone->keyManagementMode === null) {
204+
$clone->keyManagementMode = $keyEncryptionAlgorithm->getKeyManagementMode();
205+
} else {
206+
if (! $clone->areKeyManagementModesCompatible(
207+
$clone->keyManagementMode,
208+
$keyEncryptionAlgorithm->getKeyManagementMode()
209+
)) {
210+
throw new InvalidArgumentException('Foreign key management mode forbidden.');
211+
}
212+
}
213+
$clone->checkKey($keyEncryptionAlgorithm, $senderKey);
214+
$clone->senderKey = $senderKey;
215+
216+
return $clone;
217+
}
218+
191219
/**
192220
* Builds the JWE.
193221
*/
@@ -255,7 +283,7 @@ private function processRecipient(array $recipient, string $cek, array &$additio
255283
$keyEncryptionAlgorithm,
256284
$additionalHeader,
257285
$recipient['key'],
258-
$recipient['sender_key'] ?? null
286+
$recipient['sender_key'] ?? $this->senderKey ?? null
259287
);
260288
$recipientHeader = $recipient['header'];
261289
if ((is_countable($additionalHeader) ? count($additionalHeader) : 0) !== 0 && count($this->recipients) !== 1) {

0 commit comments

Comments
 (0)