CertificateSubject.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the PHPASN1 library.
  4. *
  5. * Copyright © Friedrich Große <friedrich.grosse@gmail.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 FG\X509;
  11. use FG\ASN1\Composite\RelativeDistinguishedName;
  12. use FG\ASN1\Identifier;
  13. use FG\ASN1\OID;
  14. use FG\ASN1\Parsable;
  15. use FG\ASN1\Composite\RDNString;
  16. use FG\ASN1\Universal\Sequence;
  17. class CertificateSubject extends Sequence implements Parsable
  18. {
  19. private $commonName;
  20. private $email;
  21. private $organization;
  22. private $locality;
  23. private $state;
  24. private $country;
  25. private $organizationalUnit;
  26. /**
  27. * @param string $commonName
  28. * @param string $email
  29. * @param string $organization
  30. * @param string $locality
  31. * @param string $state
  32. * @param string $country
  33. * @param string $organizationalUnit
  34. */
  35. public function __construct($commonName, $email, $organization, $locality, $state, $country, $organizationalUnit)
  36. {
  37. parent::__construct(
  38. new RDNString(OID::COUNTRY_NAME, $country),
  39. new RDNString(OID::STATE_OR_PROVINCE_NAME, $state),
  40. new RDNString(OID::LOCALITY_NAME, $locality),
  41. new RDNString(OID::ORGANIZATION_NAME, $organization),
  42. new RDNString(OID::OU_NAME, $organizationalUnit),
  43. new RDNString(OID::COMMON_NAME, $commonName),
  44. new RDNString(OID::PKCS9_EMAIL, $email)
  45. );
  46. $this->commonName = $commonName;
  47. $this->email = $email;
  48. $this->organization = $organization;
  49. $this->locality = $locality;
  50. $this->state = $state;
  51. $this->country = $country;
  52. $this->organizationalUnit = $organizationalUnit;
  53. }
  54. public function getCommonName()
  55. {
  56. return $this->commonName;
  57. }
  58. public function getEmail()
  59. {
  60. return $this->email;
  61. }
  62. public function getOrganization()
  63. {
  64. return $this->organization;
  65. }
  66. public function getLocality()
  67. {
  68. return $this->locality;
  69. }
  70. public function getState()
  71. {
  72. return $this->state;
  73. }
  74. public function getCountry()
  75. {
  76. return $this->country;
  77. }
  78. public function getOrganizationalUnit()
  79. {
  80. return $this->organizationalUnit;
  81. }
  82. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  83. {
  84. self::parseIdentifier($binaryData[$offsetIndex], Identifier::SEQUENCE, $offsetIndex++);
  85. $contentLength = self::parseContentLength($binaryData, $offsetIndex);
  86. $names = [];
  87. $octetsToRead = $contentLength;
  88. while ($octetsToRead > 0) {
  89. $relativeDistinguishedName = RelativeDistinguishedName::fromBinary($binaryData, $offsetIndex);
  90. $octetsToRead -= $relativeDistinguishedName->getObjectLength();
  91. $names[] = $relativeDistinguishedName;
  92. }
  93. }
  94. }