Skip to content

Commit 679ab72

Browse files
authored
Fix the presence of padding (#497)
Fix the presence of padding
1 parent c417d45 commit 679ab72

File tree

30 files changed

+89
-83
lines changed

30 files changed

+89
-83
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"ext-openssl": "*",
101101
"ext-sodium": "*",
102102
"brick/math": "^0.9|^0.10|^0.11|^0.12",
103-
"paragonie/constant_time_encoding": "^2.4",
103+
"paragonie/constant_time_encoding": "^2.6",
104104
"psr/clock": "^1.0",
105105
"psr/event-dispatcher": "^1.0",
106106
"psr/http-client": "^1.0",

phpstan-baseline.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ parameters:
17461746
path: src/Component/Encryption/Serializer/JSONFlattenedSerializer.php
17471747

17481748
-
1749-
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decode\\(\\) expects string, mixed given\\.$#"
1749+
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decodeNoPadding\\(\\) expects string, mixed given\\.$#"
17501750
count: 3
17511751
path: src/Component/Encryption/Serializer/JSONFlattenedSerializer.php
17521752

@@ -1776,7 +1776,7 @@ parameters:
17761776
path: src/Component/Encryption/Serializer/JSONGeneralSerializer.php
17771777

17781778
-
1779-
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decode\\(\\) expects string, mixed given\\.$#"
1779+
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decodeNoPadding\\(\\) expects string, mixed given\\.$#"
17801780
count: 3
17811781
path: src/Component/Encryption/Serializer/JSONGeneralSerializer.php
17821782

@@ -2091,7 +2091,7 @@ parameters:
20912091
path: src/Component/Signature/Serializer/CompactSerializer.php
20922092

20932093
-
2094-
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decode\\(\\) expects string, mixed given\\.$#"
2094+
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decodeNoPadding\\(\\) expects string, mixed given\\.$#"
20952095
count: 1
20962096
path: src/Component/Signature/Serializer/JSONFlattenedSerializer.php
20972097

@@ -2121,7 +2121,7 @@ parameters:
21212121
path: src/Component/Signature/Serializer/JSONGeneralSerializer.php
21222122

21232123
-
2124-
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decode\\(\\) expects string, mixed given\\.$#"
2124+
message: "#^Parameter \\#1 \\$encodedString of static method ParagonIE\\\\ConstantTime\\\\Base64\\:\\:decodeNoPadding\\(\\) expects string, mixed given\\.$#"
21252125
count: 1
21262126
path: src/Component/Signature/Serializer/JSONGeneralSerializer.php
21272127

src/Component/Core/Util/ECKey.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private static function p256PrivateKey(JWK $jwk): string
203203
if (! is_string($d)) {
204204
throw new InvalidArgumentException('Unable to get the private key');
205205
}
206-
$d = unpack('H*', str_pad(Base64UrlSafe::decode($d), 32, "\0", STR_PAD_LEFT));
206+
$d = unpack('H*', str_pad(Base64UrlSafe::decodeNoPadding($d), 32, "\0", STR_PAD_LEFT));
207207
if (! is_array($d) || ! isset($d[1])) {
208208
throw new InvalidArgumentException('Unable to get the private key');
209209
}
@@ -229,7 +229,7 @@ private static function p256KPrivateKey(JWK $jwk): string
229229
if (! is_string($d)) {
230230
throw new InvalidArgumentException('Unable to get the private key');
231231
}
232-
$d = unpack('H*', str_pad(Base64UrlSafe::decode($d), 32, "\0", STR_PAD_LEFT));
232+
$d = unpack('H*', str_pad(Base64UrlSafe::decodeNoPadding($d), 32, "\0", STR_PAD_LEFT));
233233
if (! is_array($d) || ! isset($d[1])) {
234234
throw new InvalidArgumentException('Unable to get the private key');
235235
}
@@ -255,7 +255,7 @@ private static function p384PrivateKey(JWK $jwk): string
255255
if (! is_string($d)) {
256256
throw new InvalidArgumentException('Unable to get the private key');
257257
}
258-
$d = unpack('H*', str_pad(Base64UrlSafe::decode($d), 48, "\0", STR_PAD_LEFT));
258+
$d = unpack('H*', str_pad(Base64UrlSafe::decodeNoPadding($d), 48, "\0", STR_PAD_LEFT));
259259
if (! is_array($d) || ! isset($d[1])) {
260260
throw new InvalidArgumentException('Unable to get the private key');
261261
}
@@ -281,7 +281,7 @@ private static function p521PrivateKey(JWK $jwk): string
281281
if (! is_string($d)) {
282282
throw new InvalidArgumentException('Unable to get the private key');
283283
}
284-
$d = unpack('H*', str_pad(Base64UrlSafe::decode($d), 66, "\0", STR_PAD_LEFT));
284+
$d = unpack('H*', str_pad(Base64UrlSafe::decodeNoPadding($d), 66, "\0", STR_PAD_LEFT));
285285
if (! is_array($d) || ! isset($d[1])) {
286286
throw new InvalidArgumentException('Unable to get the private key');
287287
}
@@ -317,8 +317,8 @@ private static function getKey(JWK $jwk): string
317317
if (! is_string($y)) {
318318
throw new InvalidArgumentException('Unable to get the public key');
319319
}
320-
$binX = ltrim(Base64UrlSafe::decode($x), "\0");
321-
$binY = ltrim(Base64UrlSafe::decode($y), "\0");
320+
$binX = ltrim(Base64UrlSafe::decodeNoPadding($x), "\0");
321+
$binY = ltrim(Base64UrlSafe::decodeNoPadding($y), "\0");
322322

323323
return "\04"
324324
. str_pad($binX, $length, "\0", STR_PAD_LEFT)

src/Component/Core/Util/RSAKey.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,12 @@ private function populateBigIntegers(): void
229229

230230
private function convertBase64StringToBigInteger(string $value): BigInteger
231231
{
232-
return BigInteger::createFromBinaryString(Base64UrlSafe::decode($value));
232+
return BigInteger::createFromBinaryString(Base64UrlSafe::decodeNoPadding($value));
233233
}
234234

235235
private function fromBase64ToInteger(string $value): string
236236
{
237-
$unpacked = unpack('H*', Base64UrlSafe::decode($value));
237+
$unpacked = unpack('H*', Base64UrlSafe::decodeNoPadding($value));
238238
if (! is_array($unpacked) || count($unpacked) === 0) {
239239
throw new InvalidArgumentException('Unable to get the private key');
240240
}

src/Component/Core/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"ext-json": "*",
4343
"ext-mbstring": "*",
4444
"brick/math": "^0.9|^0.10|^0.11|^0.12",
45-
"paragonie/constant_time_encoding": "^2.4",
45+
"paragonie/constant_time_encoding": "^2.6",
4646
"spomky-labs/pki-framework": "^1.0"
4747
},
4848
"conflict": {

src/Component/Encryption/JWEBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ private function determineCEK(array &$additionalHeader): string
481481
throw new RuntimeException('Invalid key.');
482482
}
483483

484-
return Base64UrlSafe::decode($k);
484+
return Base64UrlSafe::decodeNoPadding($k);
485485

486486
default :
487487
throw new InvalidArgumentException(sprintf(

src/Component/Encryption/Serializer/CompactSerializer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,16 @@ public function unserialize(string $input): JWE
5858

5959
try {
6060
$encodedSharedProtectedHeader = $parts[0];
61-
$sharedProtectedHeader = JsonConverter::decode(Base64UrlSafe::decode($encodedSharedProtectedHeader));
61+
$sharedProtectedHeader = JsonConverter::decode(
62+
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
63+
);
6264
if (! is_array($sharedProtectedHeader)) {
6365
throw new InvalidArgumentException('Unsupported input.');
6466
}
65-
$encryptedKey = $parts[1] === '' ? null : Base64UrlSafe::decode($parts[1]);
66-
$iv = Base64UrlSafe::decode($parts[2]);
67-
$ciphertext = Base64UrlSafe::decode($parts[3]);
68-
$tag = Base64UrlSafe::decode($parts[4]);
67+
$encryptedKey = $parts[1] === '' ? null : Base64UrlSafe::decodeNoPadding($parts[1]);
68+
$iv = Base64UrlSafe::decodeNoPadding($parts[2]);
69+
$ciphertext = Base64UrlSafe::decodeNoPadding($parts[3]);
70+
$tag = Base64UrlSafe::decodeNoPadding($parts[4]);
6971

7072
return new JWE(
7173
$ciphertext,

src/Component/Encryption/Serializer/JSONFlattenedSerializer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ public function unserialize(string $input): JWE
6565
}
6666
$this->checkData($data);
6767

68-
$ciphertext = Base64UrlSafe::decode($data['ciphertext']);
69-
$iv = Base64UrlSafe::decode($data['iv']);
70-
$tag = Base64UrlSafe::decode($data['tag']);
71-
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decode($data['aad']) : null;
68+
$ciphertext = Base64UrlSafe::decodeNoPadding($data['ciphertext']);
69+
$iv = Base64UrlSafe::decodeNoPadding($data['iv']);
70+
$tag = Base64UrlSafe::decodeNoPadding($data['tag']);
71+
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decodeNoPadding($data['aad']) : null;
7272
[$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader] = $this->processHeaders($data);
73-
$encryptedKey = array_key_exists('encrypted_key', $data) ? Base64UrlSafe::decode($data['encrypted_key']) : null;
73+
$encryptedKey = array_key_exists('encrypted_key', $data) ? Base64UrlSafe::decodeNoPadding(
74+
$data['encrypted_key']
75+
) : null;
7476
$header = array_key_exists('header', $data) ? $data['header'] : [];
7577

7678
return new JWE(
@@ -96,7 +98,7 @@ private function processHeaders(array $data): array
9698
{
9799
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
98100
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(
99-
Base64UrlSafe::decode($encodedSharedProtectedHeader)
101+
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
100102
) : [];
101103
$sharedHeader = $data['unprotected'] ?? [];
102104

src/Component/Encryption/Serializer/JSONGeneralSerializer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ public function unserialize(string $input): JWE
7171
}
7272
$this->checkData($data);
7373

74-
$ciphertext = Base64UrlSafe::decode($data['ciphertext']);
75-
$iv = Base64UrlSafe::decode($data['iv']);
76-
$tag = Base64UrlSafe::decode($data['tag']);
77-
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decode($data['aad']) : null;
74+
$ciphertext = Base64UrlSafe::decodeNoPadding($data['ciphertext']);
75+
$iv = Base64UrlSafe::decodeNoPadding($data['iv']);
76+
$tag = Base64UrlSafe::decodeNoPadding($data['tag']);
77+
$aad = array_key_exists('aad', $data) ? Base64UrlSafe::decodeNoPadding($data['aad']) : null;
7878
[$encodedSharedProtectedHeader, $sharedProtectedHeader, $sharedHeader] = $this->processHeaders($data);
7979
$recipients = [];
8080
foreach ($data['recipients'] as $recipient) {
@@ -103,7 +103,7 @@ private function checkData(?array $data): void
103103

104104
private function processRecipient(array $recipient): array
105105
{
106-
$encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64UrlSafe::decode(
106+
$encryptedKey = array_key_exists('encrypted_key', $recipient) ? Base64UrlSafe::decodeNoPadding(
107107
$recipient['encrypted_key']
108108
) : null;
109109
$header = array_key_exists('header', $recipient) ? $recipient['header'] : [];
@@ -115,7 +115,7 @@ private function processHeaders(array $data): array
115115
{
116116
$encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
117117
$sharedProtectedHeader = $encodedSharedProtectedHeader ? JsonConverter::decode(
118-
Base64UrlSafe::decode($encodedSharedProtectedHeader)
118+
Base64UrlSafe::decodeNoPadding($encodedSharedProtectedHeader)
119119
) : [];
120120
$sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
121121

src/Component/KeyManagement/Analyzer/ESKeyAnalyzer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function analyze(JWK $jwk, MessageBag $bag): void
4040

4141
return;
4242
}
43-
$x = Base64UrlSafe::decode($x);
43+
$x = Base64UrlSafe::decodeNoPadding($x);
4444
$xLength = 8 * mb_strlen($x, '8bit');
4545
$y = $jwk->get('y');
4646
if (! is_string($y)) {
4747
$bag->add(Message::high('Invalid key. The components "y" shall be a string.'));
4848

4949
return;
5050
}
51-
$y = Base64UrlSafe::decode($y);
51+
$y = Base64UrlSafe::decodeNoPadding($y);
5252
$yLength = 8 * mb_strlen($y, '8bit');
5353
if ($yLength !== $xLength || $yLength !== $this->getKeySize()) {
5454
$bag->add(

0 commit comments

Comments
 (0)