Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 09ab898

Browse filesBrowse files
[VarDumper] Add OpenSslCaster
1 parent b839125 commit 09ab898
Copy full SHA for 09ab898

File tree

Expand file treeCollapse file tree

4 files changed

+177
-28
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+177
-28
lines changed
+92Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Symfony\Component\VarDumper\Cloner\Stub;
15+
16+
/**
17+
* @author Alexandre Daubois <alex.daubois@gmail.com>
18+
*/
19+
class OpenSslCaster
20+
{
21+
public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested): array
22+
{
23+
$stub->cut = -1;
24+
$info = openssl_x509_parse($h, false);
25+
26+
$pin = openssl_pkey_get_public($h);
27+
$pin = openssl_pkey_get_details($pin)['key'];
28+
$pin = \array_slice(explode("\n", $pin), 1, -2);
29+
$pin = base64_decode(implode('', $pin));
30+
$pin = base64_encode(hash('sha256', $pin, true));
31+
32+
$a += [
33+
'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
34+
'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
35+
'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
36+
'fingerprint' => new EnumStub([
37+
'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
38+
'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
39+
'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
40+
'pin-sha256' => new ConstStub($pin),
41+
]),
42+
];
43+
44+
return $a;
45+
}
46+
47+
public static function castOpensslAsymmetricKey($h, array $a, Stub $stub, bool $isNested): array
48+
{
49+
$info = openssl_pkey_get_details($h);
50+
51+
$a += [
52+
'type' => new ConstStub($info['type'], $info['type']),
53+
'bits' => new ConstStub($info['bits'], $info['bits']),
54+
'publicKey' => new EnumStub([
55+
'size' => new ConstStub($info['bits'], $info['bits']),
56+
'md5' => new ConstStub(wordwrap(strtoupper(md5($info['key'])), 2, ':', true)),
57+
'sha1' => new ConstStub(wordwrap(strtoupper(sha1($info['key'])), 2, ':', true)),
58+
'sha256' => new ConstStub(wordwrap(strtoupper(hash('sha256', $info['key'])), 2, ':', true)),
59+
]),
60+
];
61+
62+
return $a;
63+
}
64+
65+
public static function castOpensslCsr($h, array $a, Stub $stub, bool $isNested): array
66+
{
67+
$info = openssl_csr_get_subject($h, false);
68+
$key = openssl_csr_get_public_key($h);
69+
70+
$pin = openssl_pkey_get_details($key)['key'];
71+
72+
$a += [
73+
'subject' => new EnumStub(array_intersect_key($info, [
74+
'organizationName' => true,
75+
'commonName' => true,
76+
'countryName' => true,
77+
'stateOrProvinceName' => true,
78+
'localityName' => true,
79+
'organizationalUnitName' => true,
80+
'emailAddress' => true,
81+
])),
82+
'publicKey' => new EnumStub([
83+
'size' => new ConstStub(openssl_pkey_get_details($key)['bits'], openssl_pkey_get_details($key)['bits']),
84+
'md5' => new ConstStub(wordwrap(strtoupper(md5($pin)), 2, ':', true)),
85+
'sha1' => new ConstStub(wordwrap(strtoupper(sha1($pin)), 2, ':', true)),
86+
'sha256' => new ConstStub(wordwrap(strtoupper(hash('sha256', $pin)), 2, ':', true)),
87+
]),
88+
];
89+
90+
return $a;
91+
}
92+
}

‎src/Symfony/Component/VarDumper/Caster/ResourceCaster.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/ResourceCaster.php
-26Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,4 @@ public static function castGd($gd, array $a, Stub $stub, bool $isNested): array
5757

5858
return $a;
5959
}
60-
61-
public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested): array
62-
{
63-
$stub->cut = -1;
64-
$info = openssl_x509_parse($h, false);
65-
66-
$pin = openssl_pkey_get_public($h);
67-
$pin = openssl_pkey_get_details($pin)['key'];
68-
$pin = \array_slice(explode("\n", $pin), 1, -2);
69-
$pin = base64_decode(implode('', $pin));
70-
$pin = base64_encode(hash('sha256', $pin, true));
71-
72-
$a += [
73-
'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
74-
'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
75-
'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
76-
'fingerprint' => new EnumStub([
77-
'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
78-
'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
79-
'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
80-
'pin-sha256' => new ConstStub($pin),
81-
]),
82-
];
83-
84-
return $a;
85-
}
8660
}

‎src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ abstract class AbstractCloner implements ClonerInterface
192192
':process' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castProcess'],
193193
':stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
194194

195-
'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'],
196-
':OpenSSL X.509' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castOpensslX509'],
195+
'OpenSSLAsymmetricKey' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslAsymmetricKey'],
196+
'OpenSSLCertificateSigningRequest' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslCsr'],
197+
'OpenSSLCertificate' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslX509'],
198+
':OpenSSL X.509' => ['Symfony\Component\VarDumper\Caster\OpenSslCaster', 'castOpensslX509'],
197199

198200
':persistent stream' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStream'],
199201
':stream-context' => ['Symfony\Component\VarDumper\Caster\ResourceCaster', 'castStreamContext'],
+81Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
16+
17+
/**
18+
* @requires extension openssl
19+
*/
20+
class OpenSslCasterTest extends TestCase
21+
{
22+
use VarDumperTestTrait;
23+
24+
public function testAsymmetricKey()
25+
{
26+
$key = openssl_pkey_new([
27+
'private_key_bits' => 1024,
28+
'private_key_type' => OPENSSL_KEYTYPE_RSA,
29+
]);
30+
31+
$this->assertDumpMatchesFormat(
32+
<<<'EODUMP'
33+
OpenSSLAsymmetricKey {
34+
+type: 0
35+
+bits: 1024
36+
+publicKey: {
37+
size: 1024
38+
md5: %A
39+
sha1: %A
40+
sha256: %A
41+
}
42+
}
43+
EODUMP, $key);
44+
}
45+
46+
public function testOpensslCsr()
47+
{
48+
$dn = [
49+
'countryName' => 'FR',
50+
'stateOrProvinceName' => 'Ile-de-France',
51+
'localityName' => 'Paris',
52+
'organizationName' => 'Symfony',
53+
'organizationalUnitName' => 'Security',
54+
'commonName' => 'symfony.com',
55+
'emailAddress' => 'test@symfony.com',
56+
];
57+
$privkey = openssl_pkey_new();
58+
$csr = openssl_csr_new($dn, $privkey);
59+
60+
$this->assertDumpMatchesFormat(
61+
<<<'EODUMP'
62+
OpenSSLCertificateSigningRequest {
63+
+subject: {
64+
countryName: "FR"
65+
stateOrProvinceName: "Ile-de-France"
66+
localityName: "Paris"
67+
organizationName: "Symfony"
68+
organizationalUnitName: "Security"
69+
commonName: "symfony.com"
70+
emailAddress: "test@symfony.com"
71+
}
72+
+publicKey: {
73+
size: 2048
74+
md5: %A
75+
sha1: %A
76+
sha256: %A
77+
}
78+
}
79+
EODUMP, $csr);
80+
}
81+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.