SubjectAlternativeNames.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\SAN;
  11. use FG\ASN1\Exception\ParserException;
  12. use FG\ASN1\ASNObject;
  13. use FG\ASN1\OID;
  14. use FG\ASN1\Parsable;
  15. use FG\ASN1\Identifier;
  16. use FG\ASN1\Universal\Sequence;
  17. /**
  18. * See section 8.3.2.1 of ITU-T X.509.
  19. */
  20. class SubjectAlternativeNames extends ASNObject implements Parsable
  21. {
  22. private $alternativeNamesSequence;
  23. public function __construct()
  24. {
  25. $this->alternativeNamesSequence = new Sequence();
  26. }
  27. protected function calculateContentLength()
  28. {
  29. return $this->alternativeNamesSequence->getObjectLength();
  30. }
  31. public function getType()
  32. {
  33. return Identifier::OCTETSTRING;
  34. }
  35. public function addDomainName(DNSName $domainName)
  36. {
  37. $this->alternativeNamesSequence->addChild($domainName);
  38. }
  39. public function addIP(IPAddress $ip)
  40. {
  41. $this->alternativeNamesSequence->addChild($ip);
  42. }
  43. public function getContent()
  44. {
  45. return $this->alternativeNamesSequence->getContent();
  46. }
  47. protected function getEncodedValue()
  48. {
  49. return $this->alternativeNamesSequence->getBinary();
  50. }
  51. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  52. {
  53. self::parseIdentifier($binaryData[$offsetIndex], Identifier::OCTETSTRING, $offsetIndex++);
  54. $contentLength = self::parseContentLength($binaryData, $offsetIndex);
  55. if ($contentLength < 2) {
  56. throw new ParserException('Can not parse Subject Alternative Names: The Sequence within the octet string after the Object identifier '.OID::CERT_EXT_SUBJECT_ALT_NAME." is too short ({$contentLength} octets)", $offsetIndex);
  57. }
  58. $offsetOfSequence = $offsetIndex;
  59. $sequence = Sequence::fromBinary($binaryData, $offsetIndex);
  60. $offsetOfSequence += $sequence->getNumberOfLengthOctets() + 1;
  61. if ($sequence->getObjectLength() != $contentLength) {
  62. throw new ParserException('Can not parse Subject Alternative Names: The Sequence length does not match the length of the surrounding octet string', $offsetIndex);
  63. }
  64. $parsedObject = new self();
  65. /** @var \FG\ASN1\ASNObject $object */
  66. foreach ($sequence as $object) {
  67. if ($object->getType() == DNSName::IDENTIFIER) {
  68. $domainName = DNSName::fromBinary($binaryData, $offsetOfSequence);
  69. $parsedObject->addDomainName($domainName);
  70. } elseif ($object->getType() == IPAddress::IDENTIFIER) {
  71. $ip = IPAddress::fromBinary($binaryData, $offsetOfSequence);
  72. $parsedObject->addIP($ip);
  73. } else {
  74. throw new ParserException('Could not parse Subject Alternative Name: Only DNSName and IP SANs are currently supported', $offsetIndex);
  75. }
  76. }
  77. $parsedObject->getBinary(); // Determine the number of content octets and object sizes once (just to let the equality unit tests pass :/ )
  78. return $parsedObject;
  79. }
  80. }