-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
OpenSslEncryptParameterOutTypeExtension
- Loading branch information
1 parent
b5fc9ec
commit 4a82c99
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\Type\FunctionParameterOutTypeExtension; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function current; | ||
use function in_array; | ||
use function openssl_get_cipher_methods; | ||
use function strtolower; | ||
use function substr; | ||
|
||
final class OpenSslEncryptParameterOutTypeExtension implements FunctionParameterOutTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool | ||
{ | ||
return $functionReflection->getName() === 'openssl_encrypt' && $parameter->getName() === 'tag'; | ||
} | ||
|
||
public function getParameterOutTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
{ | ||
$args = $funcCall->getArgs(); | ||
$cipherArg = $args[1] ?? null; | ||
|
||
if ($cipherArg === null) { | ||
return null; | ||
} | ||
|
||
$cipherType = current($scope->getType($cipherArg->value)->getConstantStrings()); | ||
|
||
if ($cipherType === false) { | ||
return TypeCombinator::addNull(new StringType()); | ||
} | ||
|
||
$cipher = strtolower($cipherType->getValue()); | ||
$mode = substr($cipher, -3); | ||
|
||
if (in_array($mode, ['gcm', 'ccm'], true)) { | ||
return new StringType(); | ||
} | ||
|
||
return new NullType(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace OpenSslEncrypt; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
public function testStringCipher(string $cipher): void | ||
{ | ||
openssl_encrypt('data', $cipher, random_bytes(32), OPENSSL_RAW_DATA, random_bytes(16), $tag); | ||
assertType('string|null', $tag); | ||
} | ||
|
||
public function testUnknownCipher(): void | ||
{ | ||
openssl_encrypt('data', 'aes-256-cde', random_bytes(32), OPENSSL_RAW_DATA, random_bytes(16), $tag); | ||
assertType('null', $tag); | ||
} | ||
|
||
public function testAeadCipher(): void | ||
{ | ||
$cipher = 'aes-256-gcm'; | ||
openssl_encrypt('data', $cipher, random_bytes(32), OPENSSL_RAW_DATA, random_bytes(16), $tag); | ||
assertType('string', $tag); | ||
|
||
$cipher = 'aes-256-ccm'; | ||
openssl_encrypt('data', $cipher, random_bytes(32), OPENSSL_RAW_DATA, random_bytes(16), $tag); | ||
assertType('string', $tag); | ||
} | ||
|
||
public function testNonAeadCipher(): void | ||
{ | ||
$cipher = 'aes-256-cbc'; | ||
openssl_encrypt('data', $cipher, random_bytes(32), OPENSSL_RAW_DATA, random_bytes(16), $tag); | ||
assertType('null', $tag); | ||
} | ||
} |