OpenSSLCaster.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. * @author Alexandre Daubois <alex.daubois@gmail.com>
  15. *
  16. * @internal
  17. */
  18. final class OpenSSLCaster
  19. {
  20. public static function castOpensslX509(\OpenSSLCertificate $h, array $a, Stub $stub, bool $isNested): array
  21. {
  22. $stub->cut = -1;
  23. $info = openssl_x509_parse($h, false);
  24. $pin = openssl_pkey_get_public($h);
  25. $pin = openssl_pkey_get_details($pin)['key'];
  26. $pin = \array_slice(explode("\n", $pin), 1, -2);
  27. $pin = base64_decode(implode('', $pin));
  28. $pin = base64_encode(hash('sha256', $pin, true));
  29. $a += [
  30. Caster::PREFIX_VIRTUAL.'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
  31. Caster::PREFIX_VIRTUAL.'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
  32. Caster::PREFIX_VIRTUAL.'expiry' => new ConstStub(date(\DateTimeInterface::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
  33. Caster::PREFIX_VIRTUAL.'fingerprint' => new EnumStub([
  34. 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
  35. 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
  36. 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
  37. 'pin-sha256' => new ConstStub($pin),
  38. ]),
  39. ];
  40. return $a;
  41. }
  42. public static function castOpensslAsymmetricKey(\OpenSSLAsymmetricKey $key, array $a, Stub $stub, bool $isNested): array
  43. {
  44. foreach (openssl_pkey_get_details($key) as $k => $v) {
  45. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  46. }
  47. unset($a[Caster::PREFIX_VIRTUAL.'rsa']); // binary data
  48. return $a;
  49. }
  50. public static function castOpensslCsr(\OpenSSLCertificateSigningRequest $csr, array $a, Stub $stub, bool $isNested): array
  51. {
  52. foreach (openssl_csr_get_subject($csr, false) as $k => $v) {
  53. $a[Caster::PREFIX_VIRTUAL.$k] = $v;
  54. }
  55. return $a;
  56. }
  57. }