@@ -20,208 +20,208 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class Name implements \Countable, \IteratorAggregate |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Relative distinguished name components. |
|
| 25 | - * |
|
| 26 | - * @var RDN[] |
|
| 27 | - */ |
|
| 28 | - protected $_rdns; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Constructor. |
|
| 32 | - * |
|
| 33 | - * @param RDN ...$rdns RDN components |
|
| 34 | - */ |
|
| 35 | - public function __construct(RDN ...$rdns) |
|
| 36 | - { |
|
| 37 | - $this->_rdns = $rdns; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function __toString() |
|
| 44 | - { |
|
| 45 | - return $this->toString(); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Initialize from ASN.1. |
|
| 50 | - * |
|
| 51 | - * @param Sequence $seq |
|
| 52 | - * |
|
| 53 | - * @return self |
|
| 54 | - */ |
|
| 55 | - public static function fromASN1(Sequence $seq): self |
|
| 56 | - { |
|
| 57 | - $rdns = array_map( |
|
| 58 | - function (UnspecifiedType $el) { |
|
| 59 | - return RDN::fromASN1($el->asSet()); |
|
| 60 | - }, $seq->elements()); |
|
| 61 | - return new self(...$rdns); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Initialize from distinguished name string. |
|
| 66 | - * |
|
| 67 | - * @see https://tools.ietf.org/html/rfc1779 |
|
| 68 | - * |
|
| 69 | - * @param string $str |
|
| 70 | - * |
|
| 71 | - * @return self |
|
| 72 | - */ |
|
| 73 | - public static function fromString(string $str): self |
|
| 74 | - { |
|
| 75 | - $rdns = []; |
|
| 76 | - foreach (DNParser::parseString($str) as $nameComponent) { |
|
| 77 | - $attribs = []; |
|
| 78 | - foreach ($nameComponent as [$name, $val]) { |
|
| 79 | - $type = AttributeType::fromName($name); |
|
| 80 | - // hexstrings are parsed to ASN.1 elements |
|
| 81 | - if ($val instanceof Element) { |
|
| 82 | - $el = $val; |
|
| 83 | - } else { |
|
| 84 | - $el = AttributeType::asn1StringForType($type->oid(), $val); |
|
| 85 | - } |
|
| 86 | - $value = AttributeValue::fromASN1ByOID($type->oid(), |
|
| 87 | - $el->asUnspecified()); |
|
| 88 | - $attribs[] = new AttributeTypeAndValue($type, $value); |
|
| 89 | - } |
|
| 90 | - $rdns[] = new RDN(...$attribs); |
|
| 91 | - } |
|
| 92 | - return new self(...$rdns); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Generate ASN.1 structure. |
|
| 97 | - * |
|
| 98 | - * @return Sequence |
|
| 99 | - */ |
|
| 100 | - public function toASN1(): Sequence |
|
| 101 | - { |
|
| 102 | - $elements = array_map( |
|
| 103 | - function (RDN $rdn) { |
|
| 104 | - return $rdn->toASN1(); |
|
| 105 | - }, $this->_rdns); |
|
| 106 | - return new Sequence(...$elements); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Get distinguised name string conforming to RFC 2253. |
|
| 111 | - * |
|
| 112 | - * @see https://tools.ietf.org/html/rfc2253#section-2.1 |
|
| 113 | - * |
|
| 114 | - * @return string |
|
| 115 | - */ |
|
| 116 | - public function toString(): string |
|
| 117 | - { |
|
| 118 | - $parts = array_map( |
|
| 119 | - function (RDN $rdn) { |
|
| 120 | - return $rdn->toString(); |
|
| 121 | - }, array_reverse($this->_rdns)); |
|
| 122 | - return implode(',', $parts); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Whether name is semantically equal to other. |
|
| 127 | - * |
|
| 128 | - * Comparison conforms to RFC 4518 string preparation algorithm. |
|
| 129 | - * |
|
| 130 | - * @see https://tools.ietf.org/html/rfc4518 |
|
| 131 | - * |
|
| 132 | - * @param Name $other Object to compare to |
|
| 133 | - * |
|
| 134 | - * @return bool |
|
| 135 | - */ |
|
| 136 | - public function equals(Name $other): bool |
|
| 137 | - { |
|
| 138 | - // if RDN count doesn't match |
|
| 139 | - if (count($this) != count($other)) { |
|
| 140 | - return false; |
|
| 141 | - } |
|
| 142 | - for ($i = count($this) - 1; $i >= 0; --$i) { |
|
| 143 | - $rdn1 = $this->_rdns[$i]; |
|
| 144 | - $rdn2 = $other->_rdns[$i]; |
|
| 145 | - if (!$rdn1->equals($rdn2)) { |
|
| 146 | - return false; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - return true; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Get all RDN objects. |
|
| 154 | - * |
|
| 155 | - * @return RDN[] |
|
| 156 | - */ |
|
| 157 | - public function all(): array |
|
| 158 | - { |
|
| 159 | - return $this->_rdns; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the first AttributeValue of given type. |
|
| 164 | - * |
|
| 165 | - * Relative name components shall be traversed in encoding order, which is |
|
| 166 | - * reversed in regards to the string representation. |
|
| 167 | - * Multi-valued RDN with multiple attributes of the requested type is |
|
| 168 | - * ambiguous and shall throw an exception. |
|
| 169 | - * |
|
| 170 | - * @param string $name Attribute OID or name |
|
| 171 | - * |
|
| 172 | - * @throws \RuntimeException If attribute cannot be resolved |
|
| 173 | - * |
|
| 174 | - * @return AttributeValue |
|
| 175 | - */ |
|
| 176 | - public function firstValueOf(string $name): AttributeValue |
|
| 177 | - { |
|
| 178 | - $oid = AttributeType::attrNameToOID($name); |
|
| 179 | - foreach ($this->_rdns as $rdn) { |
|
| 180 | - $tvs = $rdn->allOf($oid); |
|
| 181 | - if (count($tvs) > 1) { |
|
| 182 | - throw new \RangeException("RDN with multiple {$name} attributes."); |
|
| 183 | - } |
|
| 184 | - if (1 == count($tvs)) { |
|
| 185 | - return $tvs[0]->value(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - throw new \RangeException("Attribute {$name} not found."); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * @see \Countable::count() |
|
| 193 | - * |
|
| 194 | - * @return int |
|
| 195 | - */ |
|
| 196 | - public function count(): int |
|
| 197 | - { |
|
| 198 | - return count($this->_rdns); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Get the number of attributes of given type. |
|
| 203 | - * |
|
| 204 | - * @param string $name Attribute OID or name |
|
| 205 | - * |
|
| 206 | - * @return int |
|
| 207 | - */ |
|
| 208 | - public function countOfType(string $name): int |
|
| 209 | - { |
|
| 210 | - $oid = AttributeType::attrNameToOID($name); |
|
| 211 | - return (int) array_sum( |
|
| 212 | - array_map( |
|
| 213 | - function (RDN $rdn) use ($oid): int { |
|
| 214 | - return count($rdn->allOf($oid)); |
|
| 215 | - }, $this->_rdns)); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @see \IteratorAggregate::getIterator() |
|
| 220 | - * |
|
| 221 | - * @return \ArrayIterator |
|
| 222 | - */ |
|
| 223 | - public function getIterator(): \ArrayIterator |
|
| 224 | - { |
|
| 225 | - return new \ArrayIterator($this->_rdns); |
|
| 226 | - } |
|
| 23 | + /** |
|
| 24 | + * Relative distinguished name components. |
|
| 25 | + * |
|
| 26 | + * @var RDN[] |
|
| 27 | + */ |
|
| 28 | + protected $_rdns; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Constructor. |
|
| 32 | + * |
|
| 33 | + * @param RDN ...$rdns RDN components |
|
| 34 | + */ |
|
| 35 | + public function __construct(RDN ...$rdns) |
|
| 36 | + { |
|
| 37 | + $this->_rdns = $rdns; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function __toString() |
|
| 44 | + { |
|
| 45 | + return $this->toString(); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Initialize from ASN.1. |
|
| 50 | + * |
|
| 51 | + * @param Sequence $seq |
|
| 52 | + * |
|
| 53 | + * @return self |
|
| 54 | + */ |
|
| 55 | + public static function fromASN1(Sequence $seq): self |
|
| 56 | + { |
|
| 57 | + $rdns = array_map( |
|
| 58 | + function (UnspecifiedType $el) { |
|
| 59 | + return RDN::fromASN1($el->asSet()); |
|
| 60 | + }, $seq->elements()); |
|
| 61 | + return new self(...$rdns); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Initialize from distinguished name string. |
|
| 66 | + * |
|
| 67 | + * @see https://tools.ietf.org/html/rfc1779 |
|
| 68 | + * |
|
| 69 | + * @param string $str |
|
| 70 | + * |
|
| 71 | + * @return self |
|
| 72 | + */ |
|
| 73 | + public static function fromString(string $str): self |
|
| 74 | + { |
|
| 75 | + $rdns = []; |
|
| 76 | + foreach (DNParser::parseString($str) as $nameComponent) { |
|
| 77 | + $attribs = []; |
|
| 78 | + foreach ($nameComponent as [$name, $val]) { |
|
| 79 | + $type = AttributeType::fromName($name); |
|
| 80 | + // hexstrings are parsed to ASN.1 elements |
|
| 81 | + if ($val instanceof Element) { |
|
| 82 | + $el = $val; |
|
| 83 | + } else { |
|
| 84 | + $el = AttributeType::asn1StringForType($type->oid(), $val); |
|
| 85 | + } |
|
| 86 | + $value = AttributeValue::fromASN1ByOID($type->oid(), |
|
| 87 | + $el->asUnspecified()); |
|
| 88 | + $attribs[] = new AttributeTypeAndValue($type, $value); |
|
| 89 | + } |
|
| 90 | + $rdns[] = new RDN(...$attribs); |
|
| 91 | + } |
|
| 92 | + return new self(...$rdns); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Generate ASN.1 structure. |
|
| 97 | + * |
|
| 98 | + * @return Sequence |
|
| 99 | + */ |
|
| 100 | + public function toASN1(): Sequence |
|
| 101 | + { |
|
| 102 | + $elements = array_map( |
|
| 103 | + function (RDN $rdn) { |
|
| 104 | + return $rdn->toASN1(); |
|
| 105 | + }, $this->_rdns); |
|
| 106 | + return new Sequence(...$elements); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Get distinguised name string conforming to RFC 2253. |
|
| 111 | + * |
|
| 112 | + * @see https://tools.ietf.org/html/rfc2253#section-2.1 |
|
| 113 | + * |
|
| 114 | + * @return string |
|
| 115 | + */ |
|
| 116 | + public function toString(): string |
|
| 117 | + { |
|
| 118 | + $parts = array_map( |
|
| 119 | + function (RDN $rdn) { |
|
| 120 | + return $rdn->toString(); |
|
| 121 | + }, array_reverse($this->_rdns)); |
|
| 122 | + return implode(',', $parts); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Whether name is semantically equal to other. |
|
| 127 | + * |
|
| 128 | + * Comparison conforms to RFC 4518 string preparation algorithm. |
|
| 129 | + * |
|
| 130 | + * @see https://tools.ietf.org/html/rfc4518 |
|
| 131 | + * |
|
| 132 | + * @param Name $other Object to compare to |
|
| 133 | + * |
|
| 134 | + * @return bool |
|
| 135 | + */ |
|
| 136 | + public function equals(Name $other): bool |
|
| 137 | + { |
|
| 138 | + // if RDN count doesn't match |
|
| 139 | + if (count($this) != count($other)) { |
|
| 140 | + return false; |
|
| 141 | + } |
|
| 142 | + for ($i = count($this) - 1; $i >= 0; --$i) { |
|
| 143 | + $rdn1 = $this->_rdns[$i]; |
|
| 144 | + $rdn2 = $other->_rdns[$i]; |
|
| 145 | + if (!$rdn1->equals($rdn2)) { |
|
| 146 | + return false; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + return true; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Get all RDN objects. |
|
| 154 | + * |
|
| 155 | + * @return RDN[] |
|
| 156 | + */ |
|
| 157 | + public function all(): array |
|
| 158 | + { |
|
| 159 | + return $this->_rdns; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the first AttributeValue of given type. |
|
| 164 | + * |
|
| 165 | + * Relative name components shall be traversed in encoding order, which is |
|
| 166 | + * reversed in regards to the string representation. |
|
| 167 | + * Multi-valued RDN with multiple attributes of the requested type is |
|
| 168 | + * ambiguous and shall throw an exception. |
|
| 169 | + * |
|
| 170 | + * @param string $name Attribute OID or name |
|
| 171 | + * |
|
| 172 | + * @throws \RuntimeException If attribute cannot be resolved |
|
| 173 | + * |
|
| 174 | + * @return AttributeValue |
|
| 175 | + */ |
|
| 176 | + public function firstValueOf(string $name): AttributeValue |
|
| 177 | + { |
|
| 178 | + $oid = AttributeType::attrNameToOID($name); |
|
| 179 | + foreach ($this->_rdns as $rdn) { |
|
| 180 | + $tvs = $rdn->allOf($oid); |
|
| 181 | + if (count($tvs) > 1) { |
|
| 182 | + throw new \RangeException("RDN with multiple {$name} attributes."); |
|
| 183 | + } |
|
| 184 | + if (1 == count($tvs)) { |
|
| 185 | + return $tvs[0]->value(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + throw new \RangeException("Attribute {$name} not found."); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * @see \Countable::count() |
|
| 193 | + * |
|
| 194 | + * @return int |
|
| 195 | + */ |
|
| 196 | + public function count(): int |
|
| 197 | + { |
|
| 198 | + return count($this->_rdns); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Get the number of attributes of given type. |
|
| 203 | + * |
|
| 204 | + * @param string $name Attribute OID or name |
|
| 205 | + * |
|
| 206 | + * @return int |
|
| 207 | + */ |
|
| 208 | + public function countOfType(string $name): int |
|
| 209 | + { |
|
| 210 | + $oid = AttributeType::attrNameToOID($name); |
|
| 211 | + return (int) array_sum( |
|
| 212 | + array_map( |
|
| 213 | + function (RDN $rdn) use ($oid): int { |
|
| 214 | + return count($rdn->allOf($oid)); |
|
| 215 | + }, $this->_rdns)); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @see \IteratorAggregate::getIterator() |
|
| 220 | + * |
|
| 221 | + * @return \ArrayIterator |
|
| 222 | + */ |
|
| 223 | + public function getIterator(): \ArrayIterator |
|
| 224 | + { |
|
| 225 | + return new \ArrayIterator($this->_rdns); |
|
| 226 | + } |
|
| 227 | 227 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Sop\X501\ASN1; |
| 6 | 6 | |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | public static function fromASN1(Sequence $seq): self |
| 56 | 56 | { |
| 57 | 57 | $rdns = array_map( |
| 58 | - function (UnspecifiedType $el) { |
|
| 58 | + function(UnspecifiedType $el) { |
|
| 59 | 59 | return RDN::fromASN1($el->asSet()); |
| 60 | 60 | }, $seq->elements()); |
| 61 | 61 | return new self(...$rdns); |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | public function toASN1(): Sequence |
| 101 | 101 | { |
| 102 | 102 | $elements = array_map( |
| 103 | - function (RDN $rdn) { |
|
| 103 | + function(RDN $rdn) { |
|
| 104 | 104 | return $rdn->toASN1(); |
| 105 | 105 | }, $this->_rdns); |
| 106 | 106 | return new Sequence(...$elements); |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | public function toString(): string |
| 117 | 117 | { |
| 118 | 118 | $parts = array_map( |
| 119 | - function (RDN $rdn) { |
|
| 119 | + function(RDN $rdn) { |
|
| 120 | 120 | return $rdn->toString(); |
| 121 | 121 | }, array_reverse($this->_rdns)); |
| 122 | 122 | return implode(',', $parts); |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $oid = AttributeType::attrNameToOID($name); |
| 211 | 211 | return (int) array_sum( |
| 212 | 212 | array_map( |
| 213 | - function (RDN $rdn) use ($oid): int { |
|
| 213 | + function(RDN $rdn) use ($oid): int { |
|
| 214 | 214 | return count($rdn->allOf($oid)); |
| 215 | 215 | }, $this->_rdns)); |
| 216 | 216 | } |