Attributes.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\CSR;
  11. use FG\ASN1\ASNObject;
  12. use FG\X509\CertificateExtensions;
  13. use FG\ASN1\OID;
  14. use FG\ASN1\Parsable;
  15. use FG\ASN1\Construct;
  16. use FG\ASN1\Identifier;
  17. use FG\ASN1\Universal\Set;
  18. use FG\ASN1\Universal\Sequence;
  19. use FG\ASN1\Universal\ObjectIdentifier;
  20. class Attributes extends Construct implements Parsable
  21. {
  22. public function getType()
  23. {
  24. return 0xA0;
  25. }
  26. public function addAttribute($objectIdentifier, Set $attribute)
  27. {
  28. if (is_string($objectIdentifier)) {
  29. $objectIdentifier = new ObjectIdentifier($objectIdentifier);
  30. }
  31. $attributeSequence = new Sequence($objectIdentifier, $attribute);
  32. $attributeSequence->getNumberOfLengthOctets(); // length and number of length octets is calculated
  33. $this->addChild($attributeSequence);
  34. }
  35. public static function fromBinary(&$binaryData, &$offsetIndex = 0)
  36. {
  37. self::parseIdentifier($binaryData[$offsetIndex], 0xA0, $offsetIndex++);
  38. $contentLength = self::parseContentLength($binaryData, $offsetIndex);
  39. $octetsToRead = $contentLength;
  40. $parsedObject = new self();
  41. while ($octetsToRead > 0) {
  42. $initialOffset = $offsetIndex; // used to calculate how much bits have been read
  43. self::parseIdentifier($binaryData[$offsetIndex], Identifier::SEQUENCE, $offsetIndex++);
  44. self::parseContentLength($binaryData, $offsetIndex);
  45. $objectIdentifier = ObjectIdentifier::fromBinary($binaryData, $offsetIndex);
  46. $oidString = $objectIdentifier->getContent();
  47. if ($oidString == OID::PKCS9_EXTENSION_REQUEST) {
  48. $attribute = CertificateExtensions::fromBinary($binaryData, $offsetIndex);
  49. } else {
  50. $attribute = ASNObject::fromBinary($binaryData, $offsetIndex);
  51. }
  52. $parsedObject->addAttribute($objectIdentifier, $attribute);
  53. $octetsToRead -= ($offsetIndex - $initialOffset);
  54. }
  55. $parsedObject->setContentLength($contentLength);
  56. return $parsedObject;
  57. }
  58. }