Skip to content

Commit

Permalink
feature #29821 [VarDumper] add caster for OpenSSL X.509 resources (ni…
Browse files Browse the repository at this point in the history
…colas-grekas)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[VarDumper] add caster for OpenSSL X.509 resources

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

I needed that today, so here we are:

![image](https://user-images.githubusercontent.com/243674/50846515-a0b49e80-136f-11e9-9ebd-5d570c01df26.png)

Commits
-------

5e88dc6 [VarDumper] add caster for OpenSSL X.509 resources
  • Loading branch information
fabpot committed Jan 9, 2019
2 parents 006dacd + 5e88dc6 commit 29b151b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ConstStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
*/
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()
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/ResourceCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,30 @@ public static function castMysqlLink($h, array $a, Stub $stub, $isNested)

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;
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -176,7 +177,7 @@ public function __construct(array $casters = null)
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);
};
Expand Down

0 comments on commit 29b151b

Please sign in to comment.