Skip to content

Commit 2292125

Browse files
no longer support OPENSSL_PKCS1_PADDING, ref #133
1 parent 2cabc8a commit 2292125

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

src/Crypto/Rsa.php

+6-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use const OPENSSL_ALGO_SHA256;
66
use const OPENSSL_PKCS1_OAEP_PADDING;
7-
use const OPENSSL_PKCS1_PADDING;
87
use const PHP_URL_SCHEME;
98

109
use function array_column;
@@ -237,34 +236,25 @@ private static function parse($thing, string $type = self::KEY_TYPE_PRIVATE)
237236
}
238237

239238
/**
240-
* Check the RSA padding mode either `OPENSSL_PKCS1_PADDING` or `OPENSSL_PKCS1_OAEP_PADDING`.
239+
* Check the padding mode whether or nor supported.
241240
*
242-
* **Warning:**
243-
*
244-
* Decryption failures in the `RSA_PKCS1_PADDING` mode leak information which can potentially be used to mount a Bleichenbacher padding oracle attack.
245-
* This is an inherent weakness in the PKCS #1 v1.5 padding design. Prefer `RSA_PKCS1_OAEP_PADDING`.
246-
*
247-
* @link https://www.openssl.org/docs/man1.1.1/man3/RSA_public_encrypt.html
248-
*
249-
* @param int $padding - The padding mode, only support `OPENSSL_PKCS1_PADDING` or `OPENSSL_PKCS1_OAEP_PADDING`, otherwise thrown `\UnexpectedValueException`.
241+
* @param int $padding - The padding mode, only support `OPENSSL_PKCS1_PADDING`, otherwise thrown `\UnexpectedValueException`.
250242
*
251243
* @throws UnexpectedValueException
252244
*/
253245
private static function paddingModeLimitedCheck(int $padding): void
254246
{
255-
if (!($padding === OPENSSL_PKCS1_OAEP_PADDING || $padding === OPENSSL_PKCS1_PADDING)) {
256-
throw new UnexpectedValueException(sprintf("Doesn't supported padding mode(%d), here only support OPENSSL_PKCS1_OAEP_PADDING or OPENSSL_PKCS1_PADDING.", $padding));
247+
if ($padding !== OPENSSL_PKCS1_OAEP_PADDING) {
248+
throw new UnexpectedValueException(sprintf('Here\'s only support the OPENSSL_PKCS1_OAEP_PADDING(4) mode, yours(%d).', $padding));
257249
}
258250
}
259251

260252
/**
261253
* Encrypts text by the given `$publicKey` in the `$padding`(default is `OPENSSL_PKCS1_OAEP_PADDING`) mode.
262254
*
263-
* Some of APIs were required the `$padding` mode as of `RSAES-PKCS1-v1_5` which is equal to the `OPENSSL_PKCS1_PADDING` constant, exposed it for this case.
264-
*
265255
* @param string $plaintext - Cleartext to encode.
266256
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|resource|string|mixed $publicKey - The public key.
267-
* @param int $padding - One of OPENSSL_PKCS1_PADDING, OPENSSL_PKCS1_OAEP_PADDING, default is `OPENSSL_PKCS1_OAEP_PADDING`.
257+
* @param int $padding - default is `OPENSSL_PKCS1_OAEP_PADDING`.
268258
*
269259
* @return string - The base64-encoded ciphertext.
270260
* @throws UnexpectedValueException
@@ -320,11 +310,9 @@ public static function sign(string $message, $privateKey): string
320310
/**
321311
* Decrypts base64 encoded string with `$privateKey` in the `$padding`(default is `OPENSSL_PKCS1_OAEP_PADDING`) mode.
322312
*
323-
* Some of APIs were required the `$padding` mode as of `RSAES-PKCS1-v1_5` which is equal to the `OPENSSL_PKCS1_PADDING` constant, exposed it for this case.
324-
*
325313
* @param string $ciphertext - Was previously encrypted string using the corresponding public key.
326314
* @param \OpenSSLAsymmetricKey|\OpenSSLCertificate|resource|string|array{string,string}|mixed $privateKey - The private key.
327-
* @param int $padding - One of OPENSSL_PKCS1_PADDING, OPENSSL_PKCS1_OAEP_PADDING, default is `OPENSSL_PKCS1_OAEP_PADDING`.
315+
* @param int $padding - default is `OPENSSL_PKCS1_OAEP_PADDING`.
328316
*
329317
* @return string - The utf-8 plaintext.
330318
* @throws UnexpectedValueException

tests/Crypto/RsaTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private function getMockContents(string $type, string $suffix): string
4949

5050
preg_match(self::EVELOPE, $pkey ?: '', $matches);
5151

52-
return str_replace(["\r", "\n"], '', $matches['base64'] ?: '');
52+
return str_replace(["\r", "\n"], '', $matches['base64'] ?? '');
5353
}
5454

5555
public function testFromPkcs8(): void
@@ -292,7 +292,7 @@ public function crossPaddingPhrasesProvider(): array
292292
random_bytes(32), [$publicKey, OPENSSL_PKCS1_OAEP_PADDING], [$privateKey, OPENSSL_PKCS1_OAEP_PADDING], null
293293
],
294294
'encrypted as OPENSSL_PKCS1_PADDING, and decrpted as OPENSSL_PKCS1_PADDING' => [
295-
random_bytes(32), [$publicKey, OPENSSL_PKCS1_PADDING], [$privateKey, OPENSSL_PKCS1_PADDING], null
295+
random_bytes(32), [$publicKey, OPENSSL_PKCS1_PADDING], [$privateKey, OPENSSL_PKCS1_PADDING], UnexpectedValueException::class
296296
],
297297
];
298298
}

0 commit comments

Comments
 (0)