From 5e88dc63e2e746ced52c37578d452d8d47fa0092 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 8 Jan 2019 17:39:20 +0100 Subject: [PATCH] [VarDumper] add caster for OpenSSL X.509 resources --- .../Component/VarDumper/Caster/ConstStub.php | 4 +-- .../VarDumper/Caster/ResourceCaster.php | 26 +++++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 3 ++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/ConstStub.php b/src/Symfony/Component/VarDumper/Caster/ConstStub.php index 250a250a9f..15868b0934 100644 --- a/src/Symfony/Component/VarDumper/Caster/ConstStub.php +++ b/src/Symfony/Component/VarDumper/Caster/ConstStub.php @@ -20,10 +20,10 @@ use Symfony\Component\VarDumper\Cloner\Stub; */ class ConstStub extends Stub { - public function __construct(string $name, $value) + public function __construct(string $name, $value = null) { $this->class = $name; - $this->value = $value; + $this->value = 1 < \func_num_args() ? $value : $name; } public function __toString() diff --git a/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php b/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php index 3cdb27c308..85de5b5967 100644 --- a/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ResourceCaster.php @@ -69,4 +69,30 @@ class ResourceCaster return $a; } + + public static function castOpensslX509($h, array $a, Stub $stub, $isNested) + { + $stub->cut = -1; + $info = openssl_x509_parse($h, false); + + $pin = openssl_pkey_get_public($h); + $pin = openssl_pkey_get_details($pin)['key']; + $pin = \array_slice(explode("\n", $pin), 1, -2); + $pin = base64_decode(implode('', $pin)); + $pin = base64_encode(hash('sha256', $pin, true)); + + $a += array( + 'subject' => new EnumStub(array_intersect_key($info['subject'], array('organizationName' => true, 'commonName' => true))), + 'issuer' => new EnumStub(array_intersect_key($info['issuer'], array('organizationName' => true, 'commonName' => true))), + 'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']), + 'fingerprint' => new EnumStub(array( + 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)), + 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)), + 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)), + 'pin-sha256' => new ConstStub($pin), + )), + ); + + return $a; + } } diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index 88691609e1..f55b2a1c67 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -136,6 +136,7 @@ abstract class AbstractCloner implements ClonerInterface ':pgsql result' => array('Symfony\Component\VarDumper\Caster\PgSqlCaster', 'castResult'), ':process' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'), ':stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'), + ':OpenSSL X.509' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'), ':persistent stream' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'), ':stream-context' => array('Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'), ':xml' => array('Symfony\Component\VarDumper\Caster\XmlResourceCaster', 'castXml'), @@ -176,7 +177,7 @@ abstract class AbstractCloner implements ClonerInterface public function addCasters(array $casters) { foreach ($casters as $type => $callback) { - $closure = &$this->casters[strtolower($type)][]; + $closure = &$this->casters['' === $type || ':' === $type[0] ? $type : strtolower($type)][]; $closure = $callback instanceof \Closure ? $callback : static function (...$args) use ($callback, &$closure) { return ($closure = \Closure::fromCallable($callback))(...$args); };