@@ -19,207 +19,207 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class AAControlsExtension extends Extension |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * Path length contraint. |
|
| 24 | - * |
|
| 25 | - * @var null|int |
|
| 26 | - */ |
|
| 27 | - protected $_pathLenConstraint; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Permitted attributes. |
|
| 31 | - * |
|
| 32 | - * Array of OID's. |
|
| 33 | - * |
|
| 34 | - * @var null|string[] |
|
| 35 | - */ |
|
| 36 | - protected $_permittedAttrs; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Excluded attributes. |
|
| 40 | - * |
|
| 41 | - * Array of OID's. |
|
| 42 | - * |
|
| 43 | - * @var null|string[] |
|
| 44 | - */ |
|
| 45 | - protected $_excludedAttrs; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Whether to permit unspecified attributes. |
|
| 49 | - * |
|
| 50 | - * @var bool |
|
| 51 | - */ |
|
| 52 | - protected $_permitUnSpecified; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Constructor. |
|
| 56 | - * |
|
| 57 | - * @param bool $critical |
|
| 58 | - * @param null|int $path_len |
|
| 59 | - * @param null|string[] $permitted |
|
| 60 | - * @param null|string[] $excluded |
|
| 61 | - * @param bool $permit_unspecified |
|
| 62 | - */ |
|
| 63 | - public function __construct(bool $critical, ?int $path_len = null, |
|
| 64 | - ?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true) |
|
| 65 | - { |
|
| 66 | - parent::__construct(self::OID_AA_CONTROLS, $critical); |
|
| 67 | - $this->_pathLenConstraint = $path_len; |
|
| 68 | - $this->_permittedAttrs = $permitted; |
|
| 69 | - $this->_excludedAttrs = $excluded; |
|
| 70 | - $this->_permitUnSpecified = $permit_unspecified; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Check whether path length constraint is present. |
|
| 75 | - * |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - public function hasPathLen(): bool |
|
| 79 | - { |
|
| 80 | - return isset($this->_pathLenConstraint); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Get path length constraint. |
|
| 85 | - * |
|
| 86 | - * @throws \LogicException If not set |
|
| 87 | - * |
|
| 88 | - * @return int |
|
| 89 | - */ |
|
| 90 | - public function pathLen(): int |
|
| 91 | - { |
|
| 92 | - if (!$this->hasPathLen()) { |
|
| 93 | - throw new \LogicException('pathLen not set.'); |
|
| 94 | - } |
|
| 95 | - return $this->_pathLenConstraint; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Check whether permitted attributes are present. |
|
| 100 | - * |
|
| 101 | - * @return bool |
|
| 102 | - */ |
|
| 103 | - public function hasPermittedAttrs(): bool |
|
| 104 | - { |
|
| 105 | - return isset($this->_permittedAttrs); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get OID's of permitted attributes. |
|
| 110 | - * |
|
| 111 | - * @throws \LogicException If not set |
|
| 112 | - * |
|
| 113 | - * @return string[] |
|
| 114 | - */ |
|
| 115 | - public function permittedAttrs(): array |
|
| 116 | - { |
|
| 117 | - if (!$this->hasPermittedAttrs()) { |
|
| 118 | - throw new \LogicException('permittedAttrs not set.'); |
|
| 119 | - } |
|
| 120 | - return $this->_permittedAttrs; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Check whether excluded attributes are present. |
|
| 125 | - * |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - public function hasExcludedAttrs(): bool |
|
| 129 | - { |
|
| 130 | - return isset($this->_excludedAttrs); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Get OID's of excluded attributes. |
|
| 135 | - * |
|
| 136 | - * @throws \LogicException If not set |
|
| 137 | - * |
|
| 138 | - * @return string[] |
|
| 139 | - */ |
|
| 140 | - public function excludedAttrs(): array |
|
| 141 | - { |
|
| 142 | - if (!$this->hasExcludedAttrs()) { |
|
| 143 | - throw new \LogicException('excludedAttrs not set.'); |
|
| 144 | - } |
|
| 145 | - return $this->_excludedAttrs; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Whether to permit attributes that are not explicitly specified in |
|
| 150 | - * neither permitted nor excluded list. |
|
| 151 | - * |
|
| 152 | - * @return bool |
|
| 153 | - */ |
|
| 154 | - public function permitUnspecified(): bool |
|
| 155 | - { |
|
| 156 | - return $this->_permitUnSpecified; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * {@inheritdoc} |
|
| 161 | - */ |
|
| 162 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
| 163 | - { |
|
| 164 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 165 | - $path_len = null; |
|
| 166 | - $permitted = null; |
|
| 167 | - $excluded = null; |
|
| 168 | - $permit_unspecified = true; |
|
| 169 | - $idx = 0; |
|
| 170 | - if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
| 171 | - $path_len = $seq->at($idx++)->asInteger()->intNumber(); |
|
| 172 | - } |
|
| 173 | - if ($seq->hasTagged(0)) { |
|
| 174 | - $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 175 | - ->asSequence(); |
|
| 176 | - $permitted = array_map( |
|
| 177 | - function (UnspecifiedType $el) { |
|
| 178 | - return $el->asObjectIdentifier()->oid(); |
|
| 179 | - }, $attr_seq->elements()); |
|
| 180 | - ++$idx; |
|
| 181 | - } |
|
| 182 | - if ($seq->hasTagged(1)) { |
|
| 183 | - $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 184 | - ->asSequence(); |
|
| 185 | - $excluded = array_map( |
|
| 186 | - function (UnspecifiedType $el) { |
|
| 187 | - return $el->asObjectIdentifier()->oid(); |
|
| 188 | - }, $attr_seq->elements()); |
|
| 189 | - ++$idx; |
|
| 190 | - } |
|
| 191 | - if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
| 192 | - $permit_unspecified = $seq->at($idx++)->asBoolean()->value(); |
|
| 193 | - } |
|
| 194 | - return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * {@inheritdoc} |
|
| 199 | - */ |
|
| 200 | - protected function _valueASN1(): Element |
|
| 201 | - { |
|
| 202 | - $elements = []; |
|
| 203 | - if (isset($this->_pathLenConstraint)) { |
|
| 204 | - $elements[] = new Integer($this->_pathLenConstraint); |
|
| 205 | - } |
|
| 206 | - if (isset($this->_permittedAttrs)) { |
|
| 207 | - $oids = array_map( |
|
| 208 | - function ($oid) { |
|
| 209 | - return new ObjectIdentifier($oid); |
|
| 210 | - }, $this->_permittedAttrs); |
|
| 211 | - $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
|
| 212 | - } |
|
| 213 | - if (isset($this->_excludedAttrs)) { |
|
| 214 | - $oids = array_map( |
|
| 215 | - function ($oid) { |
|
| 216 | - return new ObjectIdentifier($oid); |
|
| 217 | - }, $this->_excludedAttrs); |
|
| 218 | - $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
|
| 219 | - } |
|
| 220 | - if (true !== $this->_permitUnSpecified) { |
|
| 221 | - $elements[] = new Boolean(false); |
|
| 222 | - } |
|
| 223 | - return new Sequence(...$elements); |
|
| 224 | - } |
|
| 22 | + /** |
|
| 23 | + * Path length contraint. |
|
| 24 | + * |
|
| 25 | + * @var null|int |
|
| 26 | + */ |
|
| 27 | + protected $_pathLenConstraint; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Permitted attributes. |
|
| 31 | + * |
|
| 32 | + * Array of OID's. |
|
| 33 | + * |
|
| 34 | + * @var null|string[] |
|
| 35 | + */ |
|
| 36 | + protected $_permittedAttrs; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Excluded attributes. |
|
| 40 | + * |
|
| 41 | + * Array of OID's. |
|
| 42 | + * |
|
| 43 | + * @var null|string[] |
|
| 44 | + */ |
|
| 45 | + protected $_excludedAttrs; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Whether to permit unspecified attributes. |
|
| 49 | + * |
|
| 50 | + * @var bool |
|
| 51 | + */ |
|
| 52 | + protected $_permitUnSpecified; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Constructor. |
|
| 56 | + * |
|
| 57 | + * @param bool $critical |
|
| 58 | + * @param null|int $path_len |
|
| 59 | + * @param null|string[] $permitted |
|
| 60 | + * @param null|string[] $excluded |
|
| 61 | + * @param bool $permit_unspecified |
|
| 62 | + */ |
|
| 63 | + public function __construct(bool $critical, ?int $path_len = null, |
|
| 64 | + ?array $permitted = null, ?array $excluded = null, bool $permit_unspecified = true) |
|
| 65 | + { |
|
| 66 | + parent::__construct(self::OID_AA_CONTROLS, $critical); |
|
| 67 | + $this->_pathLenConstraint = $path_len; |
|
| 68 | + $this->_permittedAttrs = $permitted; |
|
| 69 | + $this->_excludedAttrs = $excluded; |
|
| 70 | + $this->_permitUnSpecified = $permit_unspecified; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Check whether path length constraint is present. |
|
| 75 | + * |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + public function hasPathLen(): bool |
|
| 79 | + { |
|
| 80 | + return isset($this->_pathLenConstraint); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Get path length constraint. |
|
| 85 | + * |
|
| 86 | + * @throws \LogicException If not set |
|
| 87 | + * |
|
| 88 | + * @return int |
|
| 89 | + */ |
|
| 90 | + public function pathLen(): int |
|
| 91 | + { |
|
| 92 | + if (!$this->hasPathLen()) { |
|
| 93 | + throw new \LogicException('pathLen not set.'); |
|
| 94 | + } |
|
| 95 | + return $this->_pathLenConstraint; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Check whether permitted attributes are present. |
|
| 100 | + * |
|
| 101 | + * @return bool |
|
| 102 | + */ |
|
| 103 | + public function hasPermittedAttrs(): bool |
|
| 104 | + { |
|
| 105 | + return isset($this->_permittedAttrs); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get OID's of permitted attributes. |
|
| 110 | + * |
|
| 111 | + * @throws \LogicException If not set |
|
| 112 | + * |
|
| 113 | + * @return string[] |
|
| 114 | + */ |
|
| 115 | + public function permittedAttrs(): array |
|
| 116 | + { |
|
| 117 | + if (!$this->hasPermittedAttrs()) { |
|
| 118 | + throw new \LogicException('permittedAttrs not set.'); |
|
| 119 | + } |
|
| 120 | + return $this->_permittedAttrs; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Check whether excluded attributes are present. |
|
| 125 | + * |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + public function hasExcludedAttrs(): bool |
|
| 129 | + { |
|
| 130 | + return isset($this->_excludedAttrs); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Get OID's of excluded attributes. |
|
| 135 | + * |
|
| 136 | + * @throws \LogicException If not set |
|
| 137 | + * |
|
| 138 | + * @return string[] |
|
| 139 | + */ |
|
| 140 | + public function excludedAttrs(): array |
|
| 141 | + { |
|
| 142 | + if (!$this->hasExcludedAttrs()) { |
|
| 143 | + throw new \LogicException('excludedAttrs not set.'); |
|
| 144 | + } |
|
| 145 | + return $this->_excludedAttrs; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Whether to permit attributes that are not explicitly specified in |
|
| 150 | + * neither permitted nor excluded list. |
|
| 151 | + * |
|
| 152 | + * @return bool |
|
| 153 | + */ |
|
| 154 | + public function permitUnspecified(): bool |
|
| 155 | + { |
|
| 156 | + return $this->_permitUnSpecified; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * {@inheritdoc} |
|
| 161 | + */ |
|
| 162 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
| 163 | + { |
|
| 164 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 165 | + $path_len = null; |
|
| 166 | + $permitted = null; |
|
| 167 | + $excluded = null; |
|
| 168 | + $permit_unspecified = true; |
|
| 169 | + $idx = 0; |
|
| 170 | + if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
| 171 | + $path_len = $seq->at($idx++)->asInteger()->intNumber(); |
|
| 172 | + } |
|
| 173 | + if ($seq->hasTagged(0)) { |
|
| 174 | + $attr_seq = $seq->getTagged(0)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 175 | + ->asSequence(); |
|
| 176 | + $permitted = array_map( |
|
| 177 | + function (UnspecifiedType $el) { |
|
| 178 | + return $el->asObjectIdentifier()->oid(); |
|
| 179 | + }, $attr_seq->elements()); |
|
| 180 | + ++$idx; |
|
| 181 | + } |
|
| 182 | + if ($seq->hasTagged(1)) { |
|
| 183 | + $attr_seq = $seq->getTagged(1)->asImplicit(Element::TYPE_SEQUENCE) |
|
| 184 | + ->asSequence(); |
|
| 185 | + $excluded = array_map( |
|
| 186 | + function (UnspecifiedType $el) { |
|
| 187 | + return $el->asObjectIdentifier()->oid(); |
|
| 188 | + }, $attr_seq->elements()); |
|
| 189 | + ++$idx; |
|
| 190 | + } |
|
| 191 | + if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
| 192 | + $permit_unspecified = $seq->at($idx++)->asBoolean()->value(); |
|
| 193 | + } |
|
| 194 | + return new self($critical, $path_len, $permitted, $excluded, $permit_unspecified); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * {@inheritdoc} |
|
| 199 | + */ |
|
| 200 | + protected function _valueASN1(): Element |
|
| 201 | + { |
|
| 202 | + $elements = []; |
|
| 203 | + if (isset($this->_pathLenConstraint)) { |
|
| 204 | + $elements[] = new Integer($this->_pathLenConstraint); |
|
| 205 | + } |
|
| 206 | + if (isset($this->_permittedAttrs)) { |
|
| 207 | + $oids = array_map( |
|
| 208 | + function ($oid) { |
|
| 209 | + return new ObjectIdentifier($oid); |
|
| 210 | + }, $this->_permittedAttrs); |
|
| 211 | + $elements[] = new ImplicitlyTaggedType(0, new Sequence(...$oids)); |
|
| 212 | + } |
|
| 213 | + if (isset($this->_excludedAttrs)) { |
|
| 214 | + $oids = array_map( |
|
| 215 | + function ($oid) { |
|
| 216 | + return new ObjectIdentifier($oid); |
|
| 217 | + }, $this->_excludedAttrs); |
|
| 218 | + $elements[] = new ImplicitlyTaggedType(1, new Sequence(...$oids)); |
|
| 219 | + } |
|
| 220 | + if (true !== $this->_permitUnSpecified) { |
|
| 221 | + $elements[] = new Boolean(false); |
|
| 222 | + } |
|
| 223 | + return new Sequence(...$elements); |
|
| 224 | + } |
|
| 225 | 225 | } |
@@ -17,99 +17,99 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class BasicConstraintsExtension extends Extension |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Whether certificate is a CA. |
|
| 22 | - * |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - protected $_ca; |
|
| 20 | + /** |
|
| 21 | + * Whether certificate is a CA. |
|
| 22 | + * |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + protected $_ca; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Maximum certification path length. |
|
| 29 | - * |
|
| 30 | - * @var null|int |
|
| 31 | - */ |
|
| 32 | - protected $_pathLen; |
|
| 27 | + /** |
|
| 28 | + * Maximum certification path length. |
|
| 29 | + * |
|
| 30 | + * @var null|int |
|
| 31 | + */ |
|
| 32 | + protected $_pathLen; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Constructor. |
|
| 36 | - * |
|
| 37 | - * @param bool $critical |
|
| 38 | - * @param bool $ca |
|
| 39 | - * @param null|int $path_len |
|
| 40 | - */ |
|
| 41 | - public function __construct(bool $critical, bool $ca, ?int $path_len = null) |
|
| 42 | - { |
|
| 43 | - parent::__construct(self::OID_BASIC_CONSTRAINTS, $critical); |
|
| 44 | - $this->_ca = $ca; |
|
| 45 | - $this->_pathLen = $path_len; |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * Constructor. |
|
| 36 | + * |
|
| 37 | + * @param bool $critical |
|
| 38 | + * @param bool $ca |
|
| 39 | + * @param null|int $path_len |
|
| 40 | + */ |
|
| 41 | + public function __construct(bool $critical, bool $ca, ?int $path_len = null) |
|
| 42 | + { |
|
| 43 | + parent::__construct(self::OID_BASIC_CONSTRAINTS, $critical); |
|
| 44 | + $this->_ca = $ca; |
|
| 45 | + $this->_pathLen = $path_len; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Whether certificate is a CA. |
|
| 50 | - * |
|
| 51 | - * @return bool |
|
| 52 | - */ |
|
| 53 | - public function isCA(): bool |
|
| 54 | - { |
|
| 55 | - return $this->_ca; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Whether certificate is a CA. |
|
| 50 | + * |
|
| 51 | + * @return bool |
|
| 52 | + */ |
|
| 53 | + public function isCA(): bool |
|
| 54 | + { |
|
| 55 | + return $this->_ca; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Whether path length is present. |
|
| 60 | - * |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - public function hasPathLen(): bool |
|
| 64 | - { |
|
| 65 | - return isset($this->_pathLen); |
|
| 66 | - } |
|
| 58 | + /** |
|
| 59 | + * Whether path length is present. |
|
| 60 | + * |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + public function hasPathLen(): bool |
|
| 64 | + { |
|
| 65 | + return isset($this->_pathLen); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Get path length. |
|
| 70 | - * |
|
| 71 | - * @throws \LogicException If not set |
|
| 72 | - * |
|
| 73 | - * @return int |
|
| 74 | - */ |
|
| 75 | - public function pathLen(): int |
|
| 76 | - { |
|
| 77 | - if (!$this->hasPathLen()) { |
|
| 78 | - throw new \LogicException('pathLenConstraint not set.'); |
|
| 79 | - } |
|
| 80 | - return $this->_pathLen; |
|
| 81 | - } |
|
| 68 | + /** |
|
| 69 | + * Get path length. |
|
| 70 | + * |
|
| 71 | + * @throws \LogicException If not set |
|
| 72 | + * |
|
| 73 | + * @return int |
|
| 74 | + */ |
|
| 75 | + public function pathLen(): int |
|
| 76 | + { |
|
| 77 | + if (!$this->hasPathLen()) { |
|
| 78 | + throw new \LogicException('pathLenConstraint not set.'); |
|
| 79 | + } |
|
| 80 | + return $this->_pathLen; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - protected static function _fromDER(string $data, bool $critical): Extension |
|
| 87 | - { |
|
| 88 | - $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 89 | - $ca = false; |
|
| 90 | - $path_len = null; |
|
| 91 | - $idx = 0; |
|
| 92 | - if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
| 93 | - $ca = $seq->at($idx++)->asBoolean()->value(); |
|
| 94 | - } |
|
| 95 | - if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
| 96 | - $path_len = $seq->at($idx)->asInteger()->intNumber(); |
|
| 97 | - } |
|
| 98 | - return new self($critical, $ca, $path_len); |
|
| 99 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + protected static function _fromDER(string $data, bool $critical): Extension |
|
| 87 | + { |
|
| 88 | + $seq = UnspecifiedType::fromDER($data)->asSequence(); |
|
| 89 | + $ca = false; |
|
| 90 | + $path_len = null; |
|
| 91 | + $idx = 0; |
|
| 92 | + if ($seq->has($idx, Element::TYPE_BOOLEAN)) { |
|
| 93 | + $ca = $seq->at($idx++)->asBoolean()->value(); |
|
| 94 | + } |
|
| 95 | + if ($seq->has($idx, Element::TYPE_INTEGER)) { |
|
| 96 | + $path_len = $seq->at($idx)->asInteger()->intNumber(); |
|
| 97 | + } |
|
| 98 | + return new self($critical, $ca, $path_len); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * {@inheritdoc} |
|
| 103 | - */ |
|
| 104 | - protected function _valueASN1(): Element |
|
| 105 | - { |
|
| 106 | - $elements = []; |
|
| 107 | - if ($this->_ca) { |
|
| 108 | - $elements[] = new Boolean(true); |
|
| 109 | - } |
|
| 110 | - if (isset($this->_pathLen)) { |
|
| 111 | - $elements[] = new Integer($this->_pathLen); |
|
| 112 | - } |
|
| 113 | - return new Sequence(...$elements); |
|
| 114 | - } |
|
| 101 | + /** |
|
| 102 | + * {@inheritdoc} |
|
| 103 | + */ |
|
| 104 | + protected function _valueASN1(): Element |
|
| 105 | + { |
|
| 106 | + $elements = []; |
|
| 107 | + if ($this->_ca) { |
|
| 108 | + $elements[] = new Boolean(true); |
|
| 109 | + } |
|
| 110 | + if (isset($this->_pathLen)) { |
|
| 111 | + $elements[] = new Integer($this->_pathLen); |
|
| 112 | + } |
|
| 113 | + return new Sequence(...$elements); |
|
| 114 | + } |
|
| 115 | 115 | } |
@@ -12,215 +12,215 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class CertificateBundle implements \Countable, \IteratorAggregate |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Certificates. |
|
| 17 | - * |
|
| 18 | - * @var Certificate[] |
|
| 19 | - */ |
|
| 20 | - protected $_certs; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Mapping from public key id to array of certificates. |
|
| 24 | - * |
|
| 25 | - * @var null|(Certificate[])[] |
|
| 26 | - */ |
|
| 27 | - private $_keyIdMap; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Constructor. |
|
| 31 | - * |
|
| 32 | - * @param Certificate ...$certs Certificate objects |
|
| 33 | - */ |
|
| 34 | - public function __construct(Certificate ...$certs) |
|
| 35 | - { |
|
| 36 | - $this->_certs = $certs; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Reset internal cached variables on clone. |
|
| 41 | - */ |
|
| 42 | - public function __clone() |
|
| 43 | - { |
|
| 44 | - $this->_keyIdMap = null; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Initialize from PEMs. |
|
| 49 | - * |
|
| 50 | - * @param PEM ...$pems PEM objects |
|
| 51 | - * |
|
| 52 | - * @return self |
|
| 53 | - */ |
|
| 54 | - public static function fromPEMs(PEM ...$pems): self |
|
| 55 | - { |
|
| 56 | - $certs = array_map( |
|
| 57 | - function ($pem) { |
|
| 58 | - return Certificate::fromPEM($pem); |
|
| 59 | - }, $pems); |
|
| 60 | - return new self(...$certs); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Initialize from PEM bundle. |
|
| 65 | - * |
|
| 66 | - * @param PEMBundle $pem_bundle |
|
| 67 | - * |
|
| 68 | - * @return self |
|
| 69 | - */ |
|
| 70 | - public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
| 71 | - { |
|
| 72 | - return self::fromPEMs(...$pem_bundle->all()); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get self with certificates added. |
|
| 77 | - * |
|
| 78 | - * @param Certificate ...$cert |
|
| 79 | - * |
|
| 80 | - * @return self |
|
| 81 | - */ |
|
| 82 | - public function withCertificates(Certificate ...$cert): self |
|
| 83 | - { |
|
| 84 | - $obj = clone $this; |
|
| 85 | - $obj->_certs = array_merge($obj->_certs, $cert); |
|
| 86 | - return $obj; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Get self with certificates from PEMBundle added. |
|
| 91 | - * |
|
| 92 | - * @param PEMBundle $pem_bundle |
|
| 93 | - * |
|
| 94 | - * @return self |
|
| 95 | - */ |
|
| 96 | - public function withPEMBundle(PEMBundle $pem_bundle): self |
|
| 97 | - { |
|
| 98 | - $certs = $this->_certs; |
|
| 99 | - foreach ($pem_bundle as $pem) { |
|
| 100 | - $certs[] = Certificate::fromPEM($pem); |
|
| 101 | - } |
|
| 102 | - return new self(...$certs); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * Get self with single certificate from PEM added. |
|
| 107 | - * |
|
| 108 | - * @param PEM $pem |
|
| 109 | - * |
|
| 110 | - * @return self |
|
| 111 | - */ |
|
| 112 | - public function withPEM(PEM $pem): self |
|
| 113 | - { |
|
| 114 | - $certs = $this->_certs; |
|
| 115 | - $certs[] = Certificate::fromPEM($pem); |
|
| 116 | - return new self(...$certs); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Check whether bundle contains a given certificate. |
|
| 121 | - * |
|
| 122 | - * @param Certificate $cert |
|
| 123 | - * |
|
| 124 | - * @return bool |
|
| 125 | - */ |
|
| 126 | - public function contains(Certificate $cert): bool |
|
| 127 | - { |
|
| 128 | - $id = self::_getCertKeyId($cert); |
|
| 129 | - $map = $this->_getKeyIdMap(); |
|
| 130 | - if (!isset($map[$id])) { |
|
| 131 | - return false; |
|
| 132 | - } |
|
| 133 | - foreach ($map[$id] as $c) { |
|
| 134 | - /** @var Certificate $c */ |
|
| 135 | - if ($cert->equals($c)) { |
|
| 136 | - return true; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get all certificates that have given subject key identifier. |
|
| 144 | - * |
|
| 145 | - * @param string $id |
|
| 146 | - * |
|
| 147 | - * @return Certificate[] |
|
| 148 | - */ |
|
| 149 | - public function allBySubjectKeyIdentifier(string $id): array |
|
| 150 | - { |
|
| 151 | - $map = $this->_getKeyIdMap(); |
|
| 152 | - if (!isset($map[$id])) { |
|
| 153 | - return []; |
|
| 154 | - } |
|
| 155 | - return $map[$id]; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get all certificates in a bundle. |
|
| 160 | - * |
|
| 161 | - * @return Certificate[] |
|
| 162 | - */ |
|
| 163 | - public function all(): array |
|
| 164 | - { |
|
| 165 | - return $this->_certs; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @see \Countable::count() |
|
| 170 | - * |
|
| 171 | - * @return int |
|
| 172 | - */ |
|
| 173 | - public function count(): int |
|
| 174 | - { |
|
| 175 | - return count($this->_certs); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get iterator for certificates. |
|
| 180 | - * |
|
| 181 | - * @see \IteratorAggregate::getIterator() |
|
| 182 | - * |
|
| 183 | - * @return \ArrayIterator |
|
| 184 | - */ |
|
| 185 | - public function getIterator(): \ArrayIterator |
|
| 186 | - { |
|
| 187 | - return new \ArrayIterator($this->_certs); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Get certificate mapping by public key id. |
|
| 192 | - * |
|
| 193 | - * @return (Certificate[])[] |
|
| 194 | - */ |
|
| 195 | - private function _getKeyIdMap(): array |
|
| 196 | - { |
|
| 197 | - // lazily build mapping |
|
| 198 | - if (!isset($this->_keyIdMap)) { |
|
| 199 | - $this->_keyIdMap = []; |
|
| 200 | - foreach ($this->_certs as $cert) { |
|
| 201 | - $id = self::_getCertKeyId($cert); |
|
| 202 | - if (!isset($this->_keyIdMap[$id])) { |
|
| 203 | - $this->_keyIdMap[$id] = []; |
|
| 204 | - } |
|
| 205 | - array_push($this->_keyIdMap[$id], $cert); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - return $this->_keyIdMap; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * Get public key id for the certificate. |
|
| 213 | - * |
|
| 214 | - * @param Certificate $cert |
|
| 215 | - * |
|
| 216 | - * @return string |
|
| 217 | - */ |
|
| 218 | - private static function _getCertKeyId(Certificate $cert): string |
|
| 219 | - { |
|
| 220 | - $exts = $cert->tbsCertificate()->extensions(); |
|
| 221 | - if ($exts->hasSubjectKeyIdentifier()) { |
|
| 222 | - return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
| 223 | - } |
|
| 224 | - return $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 225 | - } |
|
| 15 | + /** |
|
| 16 | + * Certificates. |
|
| 17 | + * |
|
| 18 | + * @var Certificate[] |
|
| 19 | + */ |
|
| 20 | + protected $_certs; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Mapping from public key id to array of certificates. |
|
| 24 | + * |
|
| 25 | + * @var null|(Certificate[])[] |
|
| 26 | + */ |
|
| 27 | + private $_keyIdMap; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Constructor. |
|
| 31 | + * |
|
| 32 | + * @param Certificate ...$certs Certificate objects |
|
| 33 | + */ |
|
| 34 | + public function __construct(Certificate ...$certs) |
|
| 35 | + { |
|
| 36 | + $this->_certs = $certs; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Reset internal cached variables on clone. |
|
| 41 | + */ |
|
| 42 | + public function __clone() |
|
| 43 | + { |
|
| 44 | + $this->_keyIdMap = null; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Initialize from PEMs. |
|
| 49 | + * |
|
| 50 | + * @param PEM ...$pems PEM objects |
|
| 51 | + * |
|
| 52 | + * @return self |
|
| 53 | + */ |
|
| 54 | + public static function fromPEMs(PEM ...$pems): self |
|
| 55 | + { |
|
| 56 | + $certs = array_map( |
|
| 57 | + function ($pem) { |
|
| 58 | + return Certificate::fromPEM($pem); |
|
| 59 | + }, $pems); |
|
| 60 | + return new self(...$certs); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Initialize from PEM bundle. |
|
| 65 | + * |
|
| 66 | + * @param PEMBundle $pem_bundle |
|
| 67 | + * |
|
| 68 | + * @return self |
|
| 69 | + */ |
|
| 70 | + public static function fromPEMBundle(PEMBundle $pem_bundle): self |
|
| 71 | + { |
|
| 72 | + return self::fromPEMs(...$pem_bundle->all()); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get self with certificates added. |
|
| 77 | + * |
|
| 78 | + * @param Certificate ...$cert |
|
| 79 | + * |
|
| 80 | + * @return self |
|
| 81 | + */ |
|
| 82 | + public function withCertificates(Certificate ...$cert): self |
|
| 83 | + { |
|
| 84 | + $obj = clone $this; |
|
| 85 | + $obj->_certs = array_merge($obj->_certs, $cert); |
|
| 86 | + return $obj; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Get self with certificates from PEMBundle added. |
|
| 91 | + * |
|
| 92 | + * @param PEMBundle $pem_bundle |
|
| 93 | + * |
|
| 94 | + * @return self |
|
| 95 | + */ |
|
| 96 | + public function withPEMBundle(PEMBundle $pem_bundle): self |
|
| 97 | + { |
|
| 98 | + $certs = $this->_certs; |
|
| 99 | + foreach ($pem_bundle as $pem) { |
|
| 100 | + $certs[] = Certificate::fromPEM($pem); |
|
| 101 | + } |
|
| 102 | + return new self(...$certs); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * Get self with single certificate from PEM added. |
|
| 107 | + * |
|
| 108 | + * @param PEM $pem |
|
| 109 | + * |
|
| 110 | + * @return self |
|
| 111 | + */ |
|
| 112 | + public function withPEM(PEM $pem): self |
|
| 113 | + { |
|
| 114 | + $certs = $this->_certs; |
|
| 115 | + $certs[] = Certificate::fromPEM($pem); |
|
| 116 | + return new self(...$certs); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Check whether bundle contains a given certificate. |
|
| 121 | + * |
|
| 122 | + * @param Certificate $cert |
|
| 123 | + * |
|
| 124 | + * @return bool |
|
| 125 | + */ |
|
| 126 | + public function contains(Certificate $cert): bool |
|
| 127 | + { |
|
| 128 | + $id = self::_getCertKeyId($cert); |
|
| 129 | + $map = $this->_getKeyIdMap(); |
|
| 130 | + if (!isset($map[$id])) { |
|
| 131 | + return false; |
|
| 132 | + } |
|
| 133 | + foreach ($map[$id] as $c) { |
|
| 134 | + /** @var Certificate $c */ |
|
| 135 | + if ($cert->equals($c)) { |
|
| 136 | + return true; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get all certificates that have given subject key identifier. |
|
| 144 | + * |
|
| 145 | + * @param string $id |
|
| 146 | + * |
|
| 147 | + * @return Certificate[] |
|
| 148 | + */ |
|
| 149 | + public function allBySubjectKeyIdentifier(string $id): array |
|
| 150 | + { |
|
| 151 | + $map = $this->_getKeyIdMap(); |
|
| 152 | + if (!isset($map[$id])) { |
|
| 153 | + return []; |
|
| 154 | + } |
|
| 155 | + return $map[$id]; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get all certificates in a bundle. |
|
| 160 | + * |
|
| 161 | + * @return Certificate[] |
|
| 162 | + */ |
|
| 163 | + public function all(): array |
|
| 164 | + { |
|
| 165 | + return $this->_certs; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @see \Countable::count() |
|
| 170 | + * |
|
| 171 | + * @return int |
|
| 172 | + */ |
|
| 173 | + public function count(): int |
|
| 174 | + { |
|
| 175 | + return count($this->_certs); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get iterator for certificates. |
|
| 180 | + * |
|
| 181 | + * @see \IteratorAggregate::getIterator() |
|
| 182 | + * |
|
| 183 | + * @return \ArrayIterator |
|
| 184 | + */ |
|
| 185 | + public function getIterator(): \ArrayIterator |
|
| 186 | + { |
|
| 187 | + return new \ArrayIterator($this->_certs); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Get certificate mapping by public key id. |
|
| 192 | + * |
|
| 193 | + * @return (Certificate[])[] |
|
| 194 | + */ |
|
| 195 | + private function _getKeyIdMap(): array |
|
| 196 | + { |
|
| 197 | + // lazily build mapping |
|
| 198 | + if (!isset($this->_keyIdMap)) { |
|
| 199 | + $this->_keyIdMap = []; |
|
| 200 | + foreach ($this->_certs as $cert) { |
|
| 201 | + $id = self::_getCertKeyId($cert); |
|
| 202 | + if (!isset($this->_keyIdMap[$id])) { |
|
| 203 | + $this->_keyIdMap[$id] = []; |
|
| 204 | + } |
|
| 205 | + array_push($this->_keyIdMap[$id], $cert); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + return $this->_keyIdMap; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * Get public key id for the certificate. |
|
| 213 | + * |
|
| 214 | + * @param Certificate $cert |
|
| 215 | + * |
|
| 216 | + * @return string |
|
| 217 | + */ |
|
| 218 | + private static function _getCertKeyId(Certificate $cert): string |
|
| 219 | + { |
|
| 220 | + $exts = $cert->tbsCertificate()->extensions(); |
|
| 221 | + if ($exts->hasSubjectKeyIdentifier()) { |
|
| 222 | + return $exts->subjectKeyIdentifier()->keyIdentifier(); |
|
| 223 | + } |
|
| 224 | + return $cert->tbsCertificate()->subjectPublicKeyInfo()->keyIdentifier(); |
|
| 225 | + } |
|
| 226 | 226 | } |
@@ -13,136 +13,136 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class CertificateChain implements \Countable, \IteratorAggregate |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * List of certificates in a chain. |
|
| 18 | - * |
|
| 19 | - * @var Certificate[] |
|
| 20 | - */ |
|
| 21 | - protected $_certs; |
|
| 16 | + /** |
|
| 17 | + * List of certificates in a chain. |
|
| 18 | + * |
|
| 19 | + * @var Certificate[] |
|
| 20 | + */ |
|
| 21 | + protected $_certs; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constructor. |
|
| 25 | - * |
|
| 26 | - * @param Certificate ...$certs List of certificates, end-entity first |
|
| 27 | - */ |
|
| 28 | - public function __construct(Certificate ...$certs) |
|
| 29 | - { |
|
| 30 | - $this->_certs = $certs; |
|
| 31 | - } |
|
| 23 | + /** |
|
| 24 | + * Constructor. |
|
| 25 | + * |
|
| 26 | + * @param Certificate ...$certs List of certificates, end-entity first |
|
| 27 | + */ |
|
| 28 | + public function __construct(Certificate ...$certs) |
|
| 29 | + { |
|
| 30 | + $this->_certs = $certs; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Initialize from a list of PEMs. |
|
| 35 | - * |
|
| 36 | - * @param PEM ...$pems |
|
| 37 | - * |
|
| 38 | - * @return self |
|
| 39 | - */ |
|
| 40 | - public static function fromPEMs(PEM ...$pems): self |
|
| 41 | - { |
|
| 42 | - $certs = array_map( |
|
| 43 | - function (PEM $pem) { |
|
| 44 | - return Certificate::fromPEM($pem); |
|
| 45 | - }, $pems); |
|
| 46 | - return new self(...$certs); |
|
| 47 | - } |
|
| 33 | + /** |
|
| 34 | + * Initialize from a list of PEMs. |
|
| 35 | + * |
|
| 36 | + * @param PEM ...$pems |
|
| 37 | + * |
|
| 38 | + * @return self |
|
| 39 | + */ |
|
| 40 | + public static function fromPEMs(PEM ...$pems): self |
|
| 41 | + { |
|
| 42 | + $certs = array_map( |
|
| 43 | + function (PEM $pem) { |
|
| 44 | + return Certificate::fromPEM($pem); |
|
| 45 | + }, $pems); |
|
| 46 | + return new self(...$certs); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Initialize from a string containing multiple PEM blocks. |
|
| 51 | - * |
|
| 52 | - * @param string $str |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromPEMString(string $str): self |
|
| 57 | - { |
|
| 58 | - $pems = PEMBundle::fromString($str)->all(); |
|
| 59 | - return self::fromPEMs(...$pems); |
|
| 60 | - } |
|
| 49 | + /** |
|
| 50 | + * Initialize from a string containing multiple PEM blocks. |
|
| 51 | + * |
|
| 52 | + * @param string $str |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromPEMString(string $str): self |
|
| 57 | + { |
|
| 58 | + $pems = PEMBundle::fromString($str)->all(); |
|
| 59 | + return self::fromPEMs(...$pems); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Get all certificates in a chain ordered from the end-entity certificate |
|
| 64 | - * to the trust anchor. |
|
| 65 | - * |
|
| 66 | - * @return Certificate[] |
|
| 67 | - */ |
|
| 68 | - public function certificates(): array |
|
| 69 | - { |
|
| 70 | - return $this->_certs; |
|
| 71 | - } |
|
| 62 | + /** |
|
| 63 | + * Get all certificates in a chain ordered from the end-entity certificate |
|
| 64 | + * to the trust anchor. |
|
| 65 | + * |
|
| 66 | + * @return Certificate[] |
|
| 67 | + */ |
|
| 68 | + public function certificates(): array |
|
| 69 | + { |
|
| 70 | + return $this->_certs; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Get the end-entity certificate. |
|
| 75 | - * |
|
| 76 | - * @throws \LogicException |
|
| 77 | - * |
|
| 78 | - * @return Certificate |
|
| 79 | - */ |
|
| 80 | - public function endEntityCertificate(): Certificate |
|
| 81 | - { |
|
| 82 | - if (!count($this->_certs)) { |
|
| 83 | - throw new \LogicException('No certificates.'); |
|
| 84 | - } |
|
| 85 | - return $this->_certs[0]; |
|
| 86 | - } |
|
| 73 | + /** |
|
| 74 | + * Get the end-entity certificate. |
|
| 75 | + * |
|
| 76 | + * @throws \LogicException |
|
| 77 | + * |
|
| 78 | + * @return Certificate |
|
| 79 | + */ |
|
| 80 | + public function endEntityCertificate(): Certificate |
|
| 81 | + { |
|
| 82 | + if (!count($this->_certs)) { |
|
| 83 | + throw new \LogicException('No certificates.'); |
|
| 84 | + } |
|
| 85 | + return $this->_certs[0]; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Get the trust anchor certificate. |
|
| 90 | - * |
|
| 91 | - * @throws \LogicException |
|
| 92 | - * |
|
| 93 | - * @return Certificate |
|
| 94 | - */ |
|
| 95 | - public function trustAnchorCertificate(): Certificate |
|
| 96 | - { |
|
| 97 | - if (!count($this->_certs)) { |
|
| 98 | - throw new \LogicException('No certificates.'); |
|
| 99 | - } |
|
| 100 | - return $this->_certs[count($this->_certs) - 1]; |
|
| 101 | - } |
|
| 88 | + /** |
|
| 89 | + * Get the trust anchor certificate. |
|
| 90 | + * |
|
| 91 | + * @throws \LogicException |
|
| 92 | + * |
|
| 93 | + * @return Certificate |
|
| 94 | + */ |
|
| 95 | + public function trustAnchorCertificate(): Certificate |
|
| 96 | + { |
|
| 97 | + if (!count($this->_certs)) { |
|
| 98 | + throw new \LogicException('No certificates.'); |
|
| 99 | + } |
|
| 100 | + return $this->_certs[count($this->_certs) - 1]; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Convert certificate chain to certification path. |
|
| 105 | - * |
|
| 106 | - * @return CertificationPath |
|
| 107 | - */ |
|
| 108 | - public function certificationPath(): CertificationPath |
|
| 109 | - { |
|
| 110 | - return CertificationPath::fromCertificateChain($this); |
|
| 111 | - } |
|
| 103 | + /** |
|
| 104 | + * Convert certificate chain to certification path. |
|
| 105 | + * |
|
| 106 | + * @return CertificationPath |
|
| 107 | + */ |
|
| 108 | + public function certificationPath(): CertificationPath |
|
| 109 | + { |
|
| 110 | + return CertificationPath::fromCertificateChain($this); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Convert certificate chain to string of PEM blocks. |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - public function toPEMString(): string |
|
| 119 | - { |
|
| 120 | - return implode("\n", |
|
| 121 | - array_map( |
|
| 122 | - function (Certificate $cert) { |
|
| 123 | - return $cert->toPEM()->string(); |
|
| 124 | - }, $this->_certs)); |
|
| 125 | - } |
|
| 113 | + /** |
|
| 114 | + * Convert certificate chain to string of PEM blocks. |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + public function toPEMString(): string |
|
| 119 | + { |
|
| 120 | + return implode("\n", |
|
| 121 | + array_map( |
|
| 122 | + function (Certificate $cert) { |
|
| 123 | + return $cert->toPEM()->string(); |
|
| 124 | + }, $this->_certs)); |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * @see \Countable::count() |
|
| 129 | - * |
|
| 130 | - * @return int |
|
| 131 | - */ |
|
| 132 | - public function count(): int |
|
| 133 | - { |
|
| 134 | - return count($this->_certs); |
|
| 135 | - } |
|
| 127 | + /** |
|
| 128 | + * @see \Countable::count() |
|
| 129 | + * |
|
| 130 | + * @return int |
|
| 131 | + */ |
|
| 132 | + public function count(): int |
|
| 133 | + { |
|
| 134 | + return count($this->_certs); |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Get iterator for certificates. |
|
| 139 | - * |
|
| 140 | - * @see \IteratorAggregate::getIterator() |
|
| 141 | - * |
|
| 142 | - * @return \ArrayIterator |
|
| 143 | - */ |
|
| 144 | - public function getIterator(): \ArrayIterator |
|
| 145 | - { |
|
| 146 | - return new \ArrayIterator($this->_certs); |
|
| 147 | - } |
|
| 137 | + /** |
|
| 138 | + * Get iterator for certificates. |
|
| 139 | + * |
|
| 140 | + * @see \IteratorAggregate::getIterator() |
|
| 141 | + * |
|
| 142 | + * @return \ArrayIterator |
|
| 143 | + */ |
|
| 144 | + public function getIterator(): \ArrayIterator |
|
| 145 | + { |
|
| 146 | + return new \ArrayIterator($this->_certs); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -16,139 +16,139 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class CertificationPathBuilder |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Trust anchors. |
|
| 21 | - * |
|
| 22 | - * @var CertificateBundle |
|
| 23 | - */ |
|
| 24 | - protected $_trustList; |
|
| 19 | + /** |
|
| 20 | + * Trust anchors. |
|
| 21 | + * |
|
| 22 | + * @var CertificateBundle |
|
| 23 | + */ |
|
| 24 | + protected $_trustList; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | - */ |
|
| 31 | - public function __construct(CertificateBundle $trust_list) |
|
| 32 | - { |
|
| 33 | - $this->_trustList = $trust_list; |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param CertificateBundle $trust_list List of trust anchors |
|
| 30 | + */ |
|
| 31 | + public function __construct(CertificateBundle $trust_list) |
|
| 32 | + { |
|
| 33 | + $this->_trustList = $trust_list; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Get all certification paths to given target certificate from |
|
| 38 | - * any trust anchor. |
|
| 39 | - * |
|
| 40 | - * @param Certificate $target Target certificate |
|
| 41 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 42 | - * |
|
| 43 | - * @return CertificationPath[] |
|
| 44 | - */ |
|
| 45 | - public function allPathsToTarget(Certificate $target, |
|
| 46 | - ?CertificateBundle $intermediate = null): array |
|
| 47 | - { |
|
| 48 | - $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | - // map paths to CertificationPath objects |
|
| 50 | - return array_map( |
|
| 51 | - function ($certs) { |
|
| 52 | - return new CertificationPath(...$certs); |
|
| 53 | - }, $paths); |
|
| 54 | - } |
|
| 36 | + /** |
|
| 37 | + * Get all certification paths to given target certificate from |
|
| 38 | + * any trust anchor. |
|
| 39 | + * |
|
| 40 | + * @param Certificate $target Target certificate |
|
| 41 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 42 | + * |
|
| 43 | + * @return CertificationPath[] |
|
| 44 | + */ |
|
| 45 | + public function allPathsToTarget(Certificate $target, |
|
| 46 | + ?CertificateBundle $intermediate = null): array |
|
| 47 | + { |
|
| 48 | + $paths = $this->_resolvePathsToTarget($target, $intermediate); |
|
| 49 | + // map paths to CertificationPath objects |
|
| 50 | + return array_map( |
|
| 51 | + function ($certs) { |
|
| 52 | + return new CertificationPath(...$certs); |
|
| 53 | + }, $paths); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Get shortest path to given target certificate from any trust anchor. |
|
| 58 | - * |
|
| 59 | - * @param Certificate $target Target certificate |
|
| 60 | - * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 61 | - * |
|
| 62 | - * @throws PathBuildingException |
|
| 63 | - * |
|
| 64 | - * @return CertificationPath |
|
| 65 | - */ |
|
| 66 | - public function shortestPathToTarget(Certificate $target, |
|
| 67 | - ?CertificateBundle $intermediate = null): CertificationPath |
|
| 68 | - { |
|
| 69 | - $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 70 | - if (!count($paths)) { |
|
| 71 | - throw new PathBuildingException('No certification paths.'); |
|
| 72 | - } |
|
| 73 | - usort($paths, |
|
| 74 | - function ($a, $b) { |
|
| 75 | - return count($a) < count($b) ? -1 : 1; |
|
| 76 | - }); |
|
| 77 | - return reset($paths); |
|
| 78 | - } |
|
| 56 | + /** |
|
| 57 | + * Get shortest path to given target certificate from any trust anchor. |
|
| 58 | + * |
|
| 59 | + * @param Certificate $target Target certificate |
|
| 60 | + * @param null|CertificateBundle $intermediate Optional intermediate certificates |
|
| 61 | + * |
|
| 62 | + * @throws PathBuildingException |
|
| 63 | + * |
|
| 64 | + * @return CertificationPath |
|
| 65 | + */ |
|
| 66 | + public function shortestPathToTarget(Certificate $target, |
|
| 67 | + ?CertificateBundle $intermediate = null): CertificationPath |
|
| 68 | + { |
|
| 69 | + $paths = $this->allPathsToTarget($target, $intermediate); |
|
| 70 | + if (!count($paths)) { |
|
| 71 | + throw new PathBuildingException('No certification paths.'); |
|
| 72 | + } |
|
| 73 | + usort($paths, |
|
| 74 | + function ($a, $b) { |
|
| 75 | + return count($a) < count($b) ? -1 : 1; |
|
| 76 | + }); |
|
| 77 | + return reset($paths); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Find all issuers of the target certificate from a given bundle. |
|
| 82 | - * |
|
| 83 | - * @param Certificate $target Target certificate |
|
| 84 | - * @param CertificateBundle $bundle Certificates to search |
|
| 85 | - * |
|
| 86 | - * @return Certificate[] |
|
| 87 | - */ |
|
| 88 | - protected function _findIssuers(Certificate $target, |
|
| 89 | - CertificateBundle $bundle): array |
|
| 90 | - { |
|
| 91 | - $issuers = []; |
|
| 92 | - $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 93 | - $extensions = $target->tbsCertificate()->extensions(); |
|
| 94 | - // find by authority key identifier |
|
| 95 | - if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 96 | - $ext = $extensions->authorityKeyIdentifier(); |
|
| 97 | - if ($ext->hasKeyIdentifier()) { |
|
| 98 | - foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 99 | - $ext->keyIdentifier()) as $issuer) { |
|
| 100 | - // check that issuer name matches |
|
| 101 | - if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
| 102 | - $issuers[] = $issuer; |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - return $issuers; |
|
| 108 | - } |
|
| 80 | + /** |
|
| 81 | + * Find all issuers of the target certificate from a given bundle. |
|
| 82 | + * |
|
| 83 | + * @param Certificate $target Target certificate |
|
| 84 | + * @param CertificateBundle $bundle Certificates to search |
|
| 85 | + * |
|
| 86 | + * @return Certificate[] |
|
| 87 | + */ |
|
| 88 | + protected function _findIssuers(Certificate $target, |
|
| 89 | + CertificateBundle $bundle): array |
|
| 90 | + { |
|
| 91 | + $issuers = []; |
|
| 92 | + $issuer_name = $target->tbsCertificate()->issuer(); |
|
| 93 | + $extensions = $target->tbsCertificate()->extensions(); |
|
| 94 | + // find by authority key identifier |
|
| 95 | + if ($extensions->hasAuthorityKeyIdentifier()) { |
|
| 96 | + $ext = $extensions->authorityKeyIdentifier(); |
|
| 97 | + if ($ext->hasKeyIdentifier()) { |
|
| 98 | + foreach ($bundle->allBySubjectKeyIdentifier( |
|
| 99 | + $ext->keyIdentifier()) as $issuer) { |
|
| 100 | + // check that issuer name matches |
|
| 101 | + if ($issuer->tbsCertificate()->subject()->equals($issuer_name)) { |
|
| 102 | + $issuers[] = $issuer; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + return $issuers; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Resolve all possible certification paths from any trust anchor to |
|
| 112 | - * the target certificate, using optional intermediate certificates. |
|
| 113 | - * |
|
| 114 | - * Helper method for allPathsToTarget to be called recursively. |
|
| 115 | - * |
|
| 116 | - * @todo Implement loop detection |
|
| 117 | - * |
|
| 118 | - * @param Certificate $target |
|
| 119 | - * @param null|CertificateBundle $intermediate |
|
| 120 | - * |
|
| 121 | - * @return array[] Array of arrays containing path certificates |
|
| 122 | - */ |
|
| 123 | - private function _resolvePathsToTarget(Certificate $target, |
|
| 124 | - ?CertificateBundle $intermediate = null): array |
|
| 125 | - { |
|
| 126 | - // array of possible paths |
|
| 127 | - $paths = []; |
|
| 128 | - // signed by certificate in the trust list |
|
| 129 | - foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 130 | - // if target is self-signed, path consists of only |
|
| 131 | - // the target certificate |
|
| 132 | - if ($target->equals($issuer)) { |
|
| 133 | - $paths[] = [$target]; |
|
| 134 | - } else { |
|
| 135 | - $paths[] = [$issuer, $target]; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - if (isset($intermediate)) { |
|
| 139 | - // signed by intermediate certificate |
|
| 140 | - foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 141 | - // intermediate certificate must not be self-signed |
|
| 142 | - if ($issuer->isSelfIssued()) { |
|
| 143 | - continue; |
|
| 144 | - } |
|
| 145 | - // resolve paths to issuer |
|
| 146 | - $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 147 | - foreach ($subpaths as $path) { |
|
| 148 | - $paths[] = array_merge($path, [$target]); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - return $paths; |
|
| 153 | - } |
|
| 110 | + /** |
|
| 111 | + * Resolve all possible certification paths from any trust anchor to |
|
| 112 | + * the target certificate, using optional intermediate certificates. |
|
| 113 | + * |
|
| 114 | + * Helper method for allPathsToTarget to be called recursively. |
|
| 115 | + * |
|
| 116 | + * @todo Implement loop detection |
|
| 117 | + * |
|
| 118 | + * @param Certificate $target |
|
| 119 | + * @param null|CertificateBundle $intermediate |
|
| 120 | + * |
|
| 121 | + * @return array[] Array of arrays containing path certificates |
|
| 122 | + */ |
|
| 123 | + private function _resolvePathsToTarget(Certificate $target, |
|
| 124 | + ?CertificateBundle $intermediate = null): array |
|
| 125 | + { |
|
| 126 | + // array of possible paths |
|
| 127 | + $paths = []; |
|
| 128 | + // signed by certificate in the trust list |
|
| 129 | + foreach ($this->_findIssuers($target, $this->_trustList) as $issuer) { |
|
| 130 | + // if target is self-signed, path consists of only |
|
| 131 | + // the target certificate |
|
| 132 | + if ($target->equals($issuer)) { |
|
| 133 | + $paths[] = [$target]; |
|
| 134 | + } else { |
|
| 135 | + $paths[] = [$issuer, $target]; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + if (isset($intermediate)) { |
|
| 139 | + // signed by intermediate certificate |
|
| 140 | + foreach ($this->_findIssuers($target, $intermediate) as $issuer) { |
|
| 141 | + // intermediate certificate must not be self-signed |
|
| 142 | + if ($issuer->isSelfIssued()) { |
|
| 143 | + continue; |
|
| 144 | + } |
|
| 145 | + // resolve paths to issuer |
|
| 146 | + $subpaths = $this->_resolvePathsToTarget($issuer, $intermediate); |
|
| 147 | + foreach ($subpaths as $path) { |
|
| 148 | + $paths[] = array_merge($path, [$target]); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + return $paths; |
|
| 153 | + } |
|
| 154 | 154 | } |
@@ -20,500 +20,500 @@ |
||
| 20 | 20 | */ |
| 21 | 21 | class ValidatorState |
| 22 | 22 | { |
| 23 | - /** |
|
| 24 | - * Length of the certification path (n). |
|
| 25 | - * |
|
| 26 | - * @var int |
|
| 27 | - */ |
|
| 28 | - protected $_pathLength; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Current index in the certification path in the range of 1..n (i). |
|
| 32 | - * |
|
| 33 | - * @var int |
|
| 34 | - */ |
|
| 35 | - protected $_index; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Valid policy tree (valid_policy_tree). |
|
| 39 | - * |
|
| 40 | - * A tree of certificate policies with their optional qualifiers. |
|
| 41 | - * Each of the leaves of the tree represents a valid policy at this stage in |
|
| 42 | - * the certification path validation. |
|
| 43 | - * Once the tree is set to NULL, policy processing ceases. |
|
| 44 | - * |
|
| 45 | - * @var null|PolicyTree |
|
| 46 | - */ |
|
| 47 | - protected $_validPolicyTree; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Permitted subtrees (permitted_subtrees). |
|
| 51 | - * |
|
| 52 | - * A set of root names for each name type defining a set of subtrees within |
|
| 53 | - * which all subject names in subsequent certificates in the certification |
|
| 54 | - * path must fall. |
|
| 55 | - * |
|
| 56 | - * @var mixed |
|
| 57 | - */ |
|
| 58 | - protected $_permittedSubtrees; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Excluded subtrees (excluded_subtrees). |
|
| 62 | - * |
|
| 63 | - * A set of root names for each name type defining a set of subtrees within |
|
| 64 | - * which no subject name in subsequent certificates in the certification |
|
| 65 | - * path may fall. |
|
| 66 | - * |
|
| 67 | - * @var mixed |
|
| 68 | - */ |
|
| 69 | - protected $_excludedSubtrees; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Explicit policy (explicit_policy). |
|
| 73 | - * |
|
| 74 | - * An integer that indicates if a non-NULL valid_policy_tree is required. |
|
| 75 | - * |
|
| 76 | - * @var int |
|
| 77 | - */ |
|
| 78 | - protected $_explicitPolicy; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Inhibit anyPolicy (inhibit_anyPolicy). |
|
| 82 | - * |
|
| 83 | - * An integer that indicates whether the anyPolicy policy identifier is |
|
| 84 | - * considered a match. |
|
| 85 | - * |
|
| 86 | - * @var int |
|
| 87 | - */ |
|
| 88 | - protected $_inhibitAnyPolicy; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Policy mapping (policy_mapping). |
|
| 92 | - * |
|
| 93 | - * An integer that indicates if policy mapping is permitted. |
|
| 94 | - * |
|
| 95 | - * @var int |
|
| 96 | - */ |
|
| 97 | - protected $_policyMapping; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Working public key algorithm (working_public_key_algorithm). |
|
| 101 | - * |
|
| 102 | - * The digital signature algorithm used to verify the signature of a |
|
| 103 | - * certificate. |
|
| 104 | - * |
|
| 105 | - * @var AlgorithmIdentifierType |
|
| 106 | - */ |
|
| 107 | - protected $_workingPublicKeyAlgorithm; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Working public key (working_public_key). |
|
| 111 | - * |
|
| 112 | - * The public key used to verify the signature of a certificate. |
|
| 113 | - * |
|
| 114 | - * @var PublicKeyInfo |
|
| 115 | - */ |
|
| 116 | - protected $_workingPublicKey; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Working public key parameters (working_public_key_parameters). |
|
| 120 | - * |
|
| 121 | - * Parameters associated with the current public key that may be required to |
|
| 122 | - * verify a signature. |
|
| 123 | - * |
|
| 124 | - * @var null|Element |
|
| 125 | - */ |
|
| 126 | - protected $_workingPublicKeyParameters; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Working issuer name (working_issuer_name). |
|
| 130 | - * |
|
| 131 | - * The issuer distinguished name expected in the next certificate in the |
|
| 132 | - * chain. |
|
| 133 | - * |
|
| 134 | - * @var Name |
|
| 135 | - */ |
|
| 136 | - protected $_workingIssuerName; |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Maximum certification path length (max_path_length). |
|
| 140 | - * |
|
| 141 | - * @var int |
|
| 142 | - */ |
|
| 143 | - protected $_maxPathLength; |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Constructor. |
|
| 147 | - */ |
|
| 148 | - protected function __construct() |
|
| 149 | - { |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Initialize variables according to RFC 5280 6.1.2. |
|
| 154 | - * |
|
| 155 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
| 156 | - * |
|
| 157 | - * @param PathValidationConfig $config |
|
| 158 | - * @param Certificate $trust_anchor Trust anchor certificate |
|
| 159 | - * @param int $n Number of certificates |
|
| 160 | - * in the certification path |
|
| 161 | - * |
|
| 162 | - * @return self |
|
| 163 | - */ |
|
| 164 | - public static function initialize(PathValidationConfig $config, |
|
| 165 | - Certificate $trust_anchor, int $n): self |
|
| 166 | - { |
|
| 167 | - $state = new self(); |
|
| 168 | - $state->_pathLength = $n; |
|
| 169 | - $state->_index = 1; |
|
| 170 | - $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
| 171 | - $state->_permittedSubtrees = null; |
|
| 172 | - $state->_excludedSubtrees = null; |
|
| 173 | - $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
| 174 | - $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
| 175 | - $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
| 176 | - $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
| 177 | - $tbsCert = $trust_anchor->tbsCertificate(); |
|
| 178 | - $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
| 179 | - $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
| 180 | - $state->_workingPublicKey->algorithmIdentifier()); |
|
| 181 | - $state->_workingIssuerName = $tbsCert->issuer(); |
|
| 182 | - $state->_maxPathLength = $config->maxLength(); |
|
| 183 | - return $state; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Get self with current certification path index set. |
|
| 188 | - * |
|
| 189 | - * @param int $index |
|
| 190 | - * |
|
| 191 | - * @return self |
|
| 192 | - */ |
|
| 193 | - public function withIndex(int $index): self |
|
| 194 | - { |
|
| 195 | - $state = clone $this; |
|
| 196 | - $state->_index = $index; |
|
| 197 | - return $state; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Get self with valid_policy_tree. |
|
| 202 | - * |
|
| 203 | - * @param PolicyTree $policy_tree |
|
| 204 | - * |
|
| 205 | - * @return self |
|
| 206 | - */ |
|
| 207 | - public function withValidPolicyTree(PolicyTree $policy_tree): self |
|
| 208 | - { |
|
| 209 | - $state = clone $this; |
|
| 210 | - $state->_validPolicyTree = $policy_tree; |
|
| 211 | - return $state; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Get self with valid_policy_tree set to null. |
|
| 216 | - * |
|
| 217 | - * @return self |
|
| 218 | - */ |
|
| 219 | - public function withoutValidPolicyTree(): self |
|
| 220 | - { |
|
| 221 | - $state = clone $this; |
|
| 222 | - $state->_validPolicyTree = null; |
|
| 223 | - return $state; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Get self with explicit_policy. |
|
| 228 | - * |
|
| 229 | - * @param int $num |
|
| 230 | - * |
|
| 231 | - * @return self |
|
| 232 | - */ |
|
| 233 | - public function withExplicitPolicy(int $num): self |
|
| 234 | - { |
|
| 235 | - $state = clone $this; |
|
| 236 | - $state->_explicitPolicy = $num; |
|
| 237 | - return $state; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Get self with inhibit_anyPolicy. |
|
| 242 | - * |
|
| 243 | - * @param int $num |
|
| 244 | - * |
|
| 245 | - * @return self |
|
| 246 | - */ |
|
| 247 | - public function withInhibitAnyPolicy(int $num): self |
|
| 248 | - { |
|
| 249 | - $state = clone $this; |
|
| 250 | - $state->_inhibitAnyPolicy = $num; |
|
| 251 | - return $state; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Get self with policy_mapping. |
|
| 256 | - * |
|
| 257 | - * @param int $num |
|
| 258 | - * |
|
| 259 | - * @return self |
|
| 260 | - */ |
|
| 261 | - public function withPolicyMapping(int $num): self |
|
| 262 | - { |
|
| 263 | - $state = clone $this; |
|
| 264 | - $state->_policyMapping = $num; |
|
| 265 | - return $state; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get self with working_public_key_algorithm. |
|
| 270 | - * |
|
| 271 | - * @param AlgorithmIdentifierType $algo |
|
| 272 | - * |
|
| 273 | - * @return self |
|
| 274 | - */ |
|
| 275 | - public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self |
|
| 276 | - { |
|
| 277 | - $state = clone $this; |
|
| 278 | - $state->_workingPublicKeyAlgorithm = $algo; |
|
| 279 | - return $state; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * Get self with working_public_key. |
|
| 284 | - * |
|
| 285 | - * @param PublicKeyInfo $pubkey_info |
|
| 286 | - * |
|
| 287 | - * @return self |
|
| 288 | - */ |
|
| 289 | - public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self |
|
| 290 | - { |
|
| 291 | - $state = clone $this; |
|
| 292 | - $state->_workingPublicKey = $pubkey_info; |
|
| 293 | - return $state; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Get self with working_public_key_parameters. |
|
| 298 | - * |
|
| 299 | - * @param null|Element $params |
|
| 300 | - * |
|
| 301 | - * @return self |
|
| 302 | - */ |
|
| 303 | - public function withWorkingPublicKeyParameters(?Element $params = null): self |
|
| 304 | - { |
|
| 305 | - $state = clone $this; |
|
| 306 | - $state->_workingPublicKeyParameters = $params; |
|
| 307 | - return $state; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get self with working_issuer_name. |
|
| 312 | - * |
|
| 313 | - * @param Name $issuer |
|
| 314 | - * |
|
| 315 | - * @return self |
|
| 316 | - */ |
|
| 317 | - public function withWorkingIssuerName(Name $issuer): self |
|
| 318 | - { |
|
| 319 | - $state = clone $this; |
|
| 320 | - $state->_workingIssuerName = $issuer; |
|
| 321 | - return $state; |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * Get self with max_path_length. |
|
| 326 | - * |
|
| 327 | - * @param int $length |
|
| 328 | - * |
|
| 329 | - * @return self |
|
| 330 | - */ |
|
| 331 | - public function withMaxPathLength(int $length): self |
|
| 332 | - { |
|
| 333 | - $state = clone $this; |
|
| 334 | - $state->_maxPathLength = $length; |
|
| 335 | - return $state; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Get the certification path length (n). |
|
| 340 | - * |
|
| 341 | - * @return int |
|
| 342 | - */ |
|
| 343 | - public function pathLength(): int |
|
| 344 | - { |
|
| 345 | - return $this->_pathLength; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - /** |
|
| 349 | - * Get the current index in certification path in the range of 1..n. |
|
| 350 | - * |
|
| 351 | - * @return int |
|
| 352 | - */ |
|
| 353 | - public function index(): int |
|
| 354 | - { |
|
| 355 | - return $this->_index; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * Check whether valid_policy_tree is present. |
|
| 360 | - * |
|
| 361 | - * @return bool |
|
| 362 | - */ |
|
| 363 | - public function hasValidPolicyTree(): bool |
|
| 364 | - { |
|
| 365 | - return isset($this->_validPolicyTree); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Get valid_policy_tree. |
|
| 370 | - * |
|
| 371 | - * @throws \LogicException If not set |
|
| 372 | - * |
|
| 373 | - * @return PolicyTree |
|
| 374 | - */ |
|
| 375 | - public function validPolicyTree(): PolicyTree |
|
| 376 | - { |
|
| 377 | - if (!$this->hasValidPolicyTree()) { |
|
| 378 | - throw new \LogicException('valid_policy_tree not set.'); |
|
| 379 | - } |
|
| 380 | - return $this->_validPolicyTree; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * Get permitted_subtrees. |
|
| 385 | - * |
|
| 386 | - * @return mixed |
|
| 387 | - */ |
|
| 388 | - public function permittedSubtrees() |
|
| 389 | - { |
|
| 390 | - return $this->_permittedSubtrees; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Get excluded_subtrees. |
|
| 395 | - * |
|
| 396 | - * @return mixed |
|
| 397 | - */ |
|
| 398 | - public function excludedSubtrees() |
|
| 399 | - { |
|
| 400 | - return $this->_excludedSubtrees; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * Get explicit_policy. |
|
| 405 | - * |
|
| 406 | - * @return int |
|
| 407 | - */ |
|
| 408 | - public function explicitPolicy(): int |
|
| 409 | - { |
|
| 410 | - return $this->_explicitPolicy; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Get inhibit_anyPolicy. |
|
| 415 | - * |
|
| 416 | - * @return int |
|
| 417 | - */ |
|
| 418 | - public function inhibitAnyPolicy(): int |
|
| 419 | - { |
|
| 420 | - return $this->_inhibitAnyPolicy; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get policy_mapping. |
|
| 425 | - * |
|
| 426 | - * @return int |
|
| 427 | - */ |
|
| 428 | - public function policyMapping(): int |
|
| 429 | - { |
|
| 430 | - return $this->_policyMapping; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * Get working_public_key_algorithm. |
|
| 435 | - * |
|
| 436 | - * @return AlgorithmIdentifierType |
|
| 437 | - */ |
|
| 438 | - public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
| 439 | - { |
|
| 440 | - return $this->_workingPublicKeyAlgorithm; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Get working_public_key. |
|
| 445 | - * |
|
| 446 | - * @return PublicKeyInfo |
|
| 447 | - */ |
|
| 448 | - public function workingPublicKey(): PublicKeyInfo |
|
| 449 | - { |
|
| 450 | - return $this->_workingPublicKey; |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Get working_public_key_parameters. |
|
| 455 | - * |
|
| 456 | - * @return null|Element |
|
| 457 | - */ |
|
| 458 | - public function workingPublicKeyParameters(): ?Element |
|
| 459 | - { |
|
| 460 | - return $this->_workingPublicKeyParameters; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Get working_issuer_name. |
|
| 465 | - * |
|
| 466 | - * @return Name |
|
| 467 | - */ |
|
| 468 | - public function workingIssuerName(): Name |
|
| 469 | - { |
|
| 470 | - return $this->_workingIssuerName; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * Get maximum certification path length. |
|
| 475 | - * |
|
| 476 | - * @return int |
|
| 477 | - */ |
|
| 478 | - public function maxPathLength(): int |
|
| 479 | - { |
|
| 480 | - return $this->_maxPathLength; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Check whether processing the final certificate of the certification path. |
|
| 485 | - * |
|
| 486 | - * @return bool |
|
| 487 | - */ |
|
| 488 | - public function isFinal(): bool |
|
| 489 | - { |
|
| 490 | - return $this->_index === $this->_pathLength; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Get the path validation result. |
|
| 495 | - * |
|
| 496 | - * @param Certificate[] $certificates Certificates in a certification path |
|
| 497 | - * |
|
| 498 | - * @return PathValidationResult |
|
| 499 | - */ |
|
| 500 | - public function getResult(array $certificates): PathValidationResult |
|
| 501 | - { |
|
| 502 | - return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
| 503 | - $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
| 504 | - $this->_workingPublicKeyParameters); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Get ASN.1 parameters from algorithm identifier. |
|
| 509 | - * |
|
| 510 | - * @param AlgorithmIdentifierType $algo |
|
| 511 | - * |
|
| 512 | - * @return null|Element ASN.1 element or null if parameters are omitted |
|
| 513 | - */ |
|
| 514 | - public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element |
|
| 515 | - { |
|
| 516 | - $seq = $algo->toASN1(); |
|
| 517 | - return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
| 518 | - } |
|
| 23 | + /** |
|
| 24 | + * Length of the certification path (n). |
|
| 25 | + * |
|
| 26 | + * @var int |
|
| 27 | + */ |
|
| 28 | + protected $_pathLength; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Current index in the certification path in the range of 1..n (i). |
|
| 32 | + * |
|
| 33 | + * @var int |
|
| 34 | + */ |
|
| 35 | + protected $_index; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Valid policy tree (valid_policy_tree). |
|
| 39 | + * |
|
| 40 | + * A tree of certificate policies with their optional qualifiers. |
|
| 41 | + * Each of the leaves of the tree represents a valid policy at this stage in |
|
| 42 | + * the certification path validation. |
|
| 43 | + * Once the tree is set to NULL, policy processing ceases. |
|
| 44 | + * |
|
| 45 | + * @var null|PolicyTree |
|
| 46 | + */ |
|
| 47 | + protected $_validPolicyTree; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Permitted subtrees (permitted_subtrees). |
|
| 51 | + * |
|
| 52 | + * A set of root names for each name type defining a set of subtrees within |
|
| 53 | + * which all subject names in subsequent certificates in the certification |
|
| 54 | + * path must fall. |
|
| 55 | + * |
|
| 56 | + * @var mixed |
|
| 57 | + */ |
|
| 58 | + protected $_permittedSubtrees; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Excluded subtrees (excluded_subtrees). |
|
| 62 | + * |
|
| 63 | + * A set of root names for each name type defining a set of subtrees within |
|
| 64 | + * which no subject name in subsequent certificates in the certification |
|
| 65 | + * path may fall. |
|
| 66 | + * |
|
| 67 | + * @var mixed |
|
| 68 | + */ |
|
| 69 | + protected $_excludedSubtrees; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Explicit policy (explicit_policy). |
|
| 73 | + * |
|
| 74 | + * An integer that indicates if a non-NULL valid_policy_tree is required. |
|
| 75 | + * |
|
| 76 | + * @var int |
|
| 77 | + */ |
|
| 78 | + protected $_explicitPolicy; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Inhibit anyPolicy (inhibit_anyPolicy). |
|
| 82 | + * |
|
| 83 | + * An integer that indicates whether the anyPolicy policy identifier is |
|
| 84 | + * considered a match. |
|
| 85 | + * |
|
| 86 | + * @var int |
|
| 87 | + */ |
|
| 88 | + protected $_inhibitAnyPolicy; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Policy mapping (policy_mapping). |
|
| 92 | + * |
|
| 93 | + * An integer that indicates if policy mapping is permitted. |
|
| 94 | + * |
|
| 95 | + * @var int |
|
| 96 | + */ |
|
| 97 | + protected $_policyMapping; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Working public key algorithm (working_public_key_algorithm). |
|
| 101 | + * |
|
| 102 | + * The digital signature algorithm used to verify the signature of a |
|
| 103 | + * certificate. |
|
| 104 | + * |
|
| 105 | + * @var AlgorithmIdentifierType |
|
| 106 | + */ |
|
| 107 | + protected $_workingPublicKeyAlgorithm; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Working public key (working_public_key). |
|
| 111 | + * |
|
| 112 | + * The public key used to verify the signature of a certificate. |
|
| 113 | + * |
|
| 114 | + * @var PublicKeyInfo |
|
| 115 | + */ |
|
| 116 | + protected $_workingPublicKey; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Working public key parameters (working_public_key_parameters). |
|
| 120 | + * |
|
| 121 | + * Parameters associated with the current public key that may be required to |
|
| 122 | + * verify a signature. |
|
| 123 | + * |
|
| 124 | + * @var null|Element |
|
| 125 | + */ |
|
| 126 | + protected $_workingPublicKeyParameters; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Working issuer name (working_issuer_name). |
|
| 130 | + * |
|
| 131 | + * The issuer distinguished name expected in the next certificate in the |
|
| 132 | + * chain. |
|
| 133 | + * |
|
| 134 | + * @var Name |
|
| 135 | + */ |
|
| 136 | + protected $_workingIssuerName; |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Maximum certification path length (max_path_length). |
|
| 140 | + * |
|
| 141 | + * @var int |
|
| 142 | + */ |
|
| 143 | + protected $_maxPathLength; |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Constructor. |
|
| 147 | + */ |
|
| 148 | + protected function __construct() |
|
| 149 | + { |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Initialize variables according to RFC 5280 6.1.2. |
|
| 154 | + * |
|
| 155 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.2 |
|
| 156 | + * |
|
| 157 | + * @param PathValidationConfig $config |
|
| 158 | + * @param Certificate $trust_anchor Trust anchor certificate |
|
| 159 | + * @param int $n Number of certificates |
|
| 160 | + * in the certification path |
|
| 161 | + * |
|
| 162 | + * @return self |
|
| 163 | + */ |
|
| 164 | + public static function initialize(PathValidationConfig $config, |
|
| 165 | + Certificate $trust_anchor, int $n): self |
|
| 166 | + { |
|
| 167 | + $state = new self(); |
|
| 168 | + $state->_pathLength = $n; |
|
| 169 | + $state->_index = 1; |
|
| 170 | + $state->_validPolicyTree = new PolicyTree(PolicyNode::anyPolicyNode()); |
|
| 171 | + $state->_permittedSubtrees = null; |
|
| 172 | + $state->_excludedSubtrees = null; |
|
| 173 | + $state->_explicitPolicy = $config->explicitPolicy() ? 0 : $n + 1; |
|
| 174 | + $state->_inhibitAnyPolicy = $config->anyPolicyInhibit() ? 0 : $n + 1; |
|
| 175 | + $state->_policyMapping = $config->policyMappingInhibit() ? 0 : $n + 1; |
|
| 176 | + $state->_workingPublicKeyAlgorithm = $trust_anchor->signatureAlgorithm(); |
|
| 177 | + $tbsCert = $trust_anchor->tbsCertificate(); |
|
| 178 | + $state->_workingPublicKey = $tbsCert->subjectPublicKeyInfo(); |
|
| 179 | + $state->_workingPublicKeyParameters = self::getAlgorithmParameters( |
|
| 180 | + $state->_workingPublicKey->algorithmIdentifier()); |
|
| 181 | + $state->_workingIssuerName = $tbsCert->issuer(); |
|
| 182 | + $state->_maxPathLength = $config->maxLength(); |
|
| 183 | + return $state; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Get self with current certification path index set. |
|
| 188 | + * |
|
| 189 | + * @param int $index |
|
| 190 | + * |
|
| 191 | + * @return self |
|
| 192 | + */ |
|
| 193 | + public function withIndex(int $index): self |
|
| 194 | + { |
|
| 195 | + $state = clone $this; |
|
| 196 | + $state->_index = $index; |
|
| 197 | + return $state; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Get self with valid_policy_tree. |
|
| 202 | + * |
|
| 203 | + * @param PolicyTree $policy_tree |
|
| 204 | + * |
|
| 205 | + * @return self |
|
| 206 | + */ |
|
| 207 | + public function withValidPolicyTree(PolicyTree $policy_tree): self |
|
| 208 | + { |
|
| 209 | + $state = clone $this; |
|
| 210 | + $state->_validPolicyTree = $policy_tree; |
|
| 211 | + return $state; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Get self with valid_policy_tree set to null. |
|
| 216 | + * |
|
| 217 | + * @return self |
|
| 218 | + */ |
|
| 219 | + public function withoutValidPolicyTree(): self |
|
| 220 | + { |
|
| 221 | + $state = clone $this; |
|
| 222 | + $state->_validPolicyTree = null; |
|
| 223 | + return $state; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Get self with explicit_policy. |
|
| 228 | + * |
|
| 229 | + * @param int $num |
|
| 230 | + * |
|
| 231 | + * @return self |
|
| 232 | + */ |
|
| 233 | + public function withExplicitPolicy(int $num): self |
|
| 234 | + { |
|
| 235 | + $state = clone $this; |
|
| 236 | + $state->_explicitPolicy = $num; |
|
| 237 | + return $state; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Get self with inhibit_anyPolicy. |
|
| 242 | + * |
|
| 243 | + * @param int $num |
|
| 244 | + * |
|
| 245 | + * @return self |
|
| 246 | + */ |
|
| 247 | + public function withInhibitAnyPolicy(int $num): self |
|
| 248 | + { |
|
| 249 | + $state = clone $this; |
|
| 250 | + $state->_inhibitAnyPolicy = $num; |
|
| 251 | + return $state; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Get self with policy_mapping. |
|
| 256 | + * |
|
| 257 | + * @param int $num |
|
| 258 | + * |
|
| 259 | + * @return self |
|
| 260 | + */ |
|
| 261 | + public function withPolicyMapping(int $num): self |
|
| 262 | + { |
|
| 263 | + $state = clone $this; |
|
| 264 | + $state->_policyMapping = $num; |
|
| 265 | + return $state; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get self with working_public_key_algorithm. |
|
| 270 | + * |
|
| 271 | + * @param AlgorithmIdentifierType $algo |
|
| 272 | + * |
|
| 273 | + * @return self |
|
| 274 | + */ |
|
| 275 | + public function withWorkingPublicKeyAlgorithm(AlgorithmIdentifierType $algo): self |
|
| 276 | + { |
|
| 277 | + $state = clone $this; |
|
| 278 | + $state->_workingPublicKeyAlgorithm = $algo; |
|
| 279 | + return $state; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * Get self with working_public_key. |
|
| 284 | + * |
|
| 285 | + * @param PublicKeyInfo $pubkey_info |
|
| 286 | + * |
|
| 287 | + * @return self |
|
| 288 | + */ |
|
| 289 | + public function withWorkingPublicKey(PublicKeyInfo $pubkey_info): self |
|
| 290 | + { |
|
| 291 | + $state = clone $this; |
|
| 292 | + $state->_workingPublicKey = $pubkey_info; |
|
| 293 | + return $state; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Get self with working_public_key_parameters. |
|
| 298 | + * |
|
| 299 | + * @param null|Element $params |
|
| 300 | + * |
|
| 301 | + * @return self |
|
| 302 | + */ |
|
| 303 | + public function withWorkingPublicKeyParameters(?Element $params = null): self |
|
| 304 | + { |
|
| 305 | + $state = clone $this; |
|
| 306 | + $state->_workingPublicKeyParameters = $params; |
|
| 307 | + return $state; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get self with working_issuer_name. |
|
| 312 | + * |
|
| 313 | + * @param Name $issuer |
|
| 314 | + * |
|
| 315 | + * @return self |
|
| 316 | + */ |
|
| 317 | + public function withWorkingIssuerName(Name $issuer): self |
|
| 318 | + { |
|
| 319 | + $state = clone $this; |
|
| 320 | + $state->_workingIssuerName = $issuer; |
|
| 321 | + return $state; |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * Get self with max_path_length. |
|
| 326 | + * |
|
| 327 | + * @param int $length |
|
| 328 | + * |
|
| 329 | + * @return self |
|
| 330 | + */ |
|
| 331 | + public function withMaxPathLength(int $length): self |
|
| 332 | + { |
|
| 333 | + $state = clone $this; |
|
| 334 | + $state->_maxPathLength = $length; |
|
| 335 | + return $state; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Get the certification path length (n). |
|
| 340 | + * |
|
| 341 | + * @return int |
|
| 342 | + */ |
|
| 343 | + public function pathLength(): int |
|
| 344 | + { |
|
| 345 | + return $this->_pathLength; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + /** |
|
| 349 | + * Get the current index in certification path in the range of 1..n. |
|
| 350 | + * |
|
| 351 | + * @return int |
|
| 352 | + */ |
|
| 353 | + public function index(): int |
|
| 354 | + { |
|
| 355 | + return $this->_index; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * Check whether valid_policy_tree is present. |
|
| 360 | + * |
|
| 361 | + * @return bool |
|
| 362 | + */ |
|
| 363 | + public function hasValidPolicyTree(): bool |
|
| 364 | + { |
|
| 365 | + return isset($this->_validPolicyTree); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Get valid_policy_tree. |
|
| 370 | + * |
|
| 371 | + * @throws \LogicException If not set |
|
| 372 | + * |
|
| 373 | + * @return PolicyTree |
|
| 374 | + */ |
|
| 375 | + public function validPolicyTree(): PolicyTree |
|
| 376 | + { |
|
| 377 | + if (!$this->hasValidPolicyTree()) { |
|
| 378 | + throw new \LogicException('valid_policy_tree not set.'); |
|
| 379 | + } |
|
| 380 | + return $this->_validPolicyTree; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * Get permitted_subtrees. |
|
| 385 | + * |
|
| 386 | + * @return mixed |
|
| 387 | + */ |
|
| 388 | + public function permittedSubtrees() |
|
| 389 | + { |
|
| 390 | + return $this->_permittedSubtrees; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Get excluded_subtrees. |
|
| 395 | + * |
|
| 396 | + * @return mixed |
|
| 397 | + */ |
|
| 398 | + public function excludedSubtrees() |
|
| 399 | + { |
|
| 400 | + return $this->_excludedSubtrees; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * Get explicit_policy. |
|
| 405 | + * |
|
| 406 | + * @return int |
|
| 407 | + */ |
|
| 408 | + public function explicitPolicy(): int |
|
| 409 | + { |
|
| 410 | + return $this->_explicitPolicy; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Get inhibit_anyPolicy. |
|
| 415 | + * |
|
| 416 | + * @return int |
|
| 417 | + */ |
|
| 418 | + public function inhibitAnyPolicy(): int |
|
| 419 | + { |
|
| 420 | + return $this->_inhibitAnyPolicy; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get policy_mapping. |
|
| 425 | + * |
|
| 426 | + * @return int |
|
| 427 | + */ |
|
| 428 | + public function policyMapping(): int |
|
| 429 | + { |
|
| 430 | + return $this->_policyMapping; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * Get working_public_key_algorithm. |
|
| 435 | + * |
|
| 436 | + * @return AlgorithmIdentifierType |
|
| 437 | + */ |
|
| 438 | + public function workingPublicKeyAlgorithm(): AlgorithmIdentifierType |
|
| 439 | + { |
|
| 440 | + return $this->_workingPublicKeyAlgorithm; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Get working_public_key. |
|
| 445 | + * |
|
| 446 | + * @return PublicKeyInfo |
|
| 447 | + */ |
|
| 448 | + public function workingPublicKey(): PublicKeyInfo |
|
| 449 | + { |
|
| 450 | + return $this->_workingPublicKey; |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Get working_public_key_parameters. |
|
| 455 | + * |
|
| 456 | + * @return null|Element |
|
| 457 | + */ |
|
| 458 | + public function workingPublicKeyParameters(): ?Element |
|
| 459 | + { |
|
| 460 | + return $this->_workingPublicKeyParameters; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Get working_issuer_name. |
|
| 465 | + * |
|
| 466 | + * @return Name |
|
| 467 | + */ |
|
| 468 | + public function workingIssuerName(): Name |
|
| 469 | + { |
|
| 470 | + return $this->_workingIssuerName; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * Get maximum certification path length. |
|
| 475 | + * |
|
| 476 | + * @return int |
|
| 477 | + */ |
|
| 478 | + public function maxPathLength(): int |
|
| 479 | + { |
|
| 480 | + return $this->_maxPathLength; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Check whether processing the final certificate of the certification path. |
|
| 485 | + * |
|
| 486 | + * @return bool |
|
| 487 | + */ |
|
| 488 | + public function isFinal(): bool |
|
| 489 | + { |
|
| 490 | + return $this->_index === $this->_pathLength; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Get the path validation result. |
|
| 495 | + * |
|
| 496 | + * @param Certificate[] $certificates Certificates in a certification path |
|
| 497 | + * |
|
| 498 | + * @return PathValidationResult |
|
| 499 | + */ |
|
| 500 | + public function getResult(array $certificates): PathValidationResult |
|
| 501 | + { |
|
| 502 | + return new PathValidationResult($certificates, $this->_validPolicyTree, |
|
| 503 | + $this->_workingPublicKey, $this->_workingPublicKeyAlgorithm, |
|
| 504 | + $this->_workingPublicKeyParameters); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Get ASN.1 parameters from algorithm identifier. |
|
| 509 | + * |
|
| 510 | + * @param AlgorithmIdentifierType $algo |
|
| 511 | + * |
|
| 512 | + * @return null|Element ASN.1 element or null if parameters are omitted |
|
| 513 | + */ |
|
| 514 | + public static function getAlgorithmParameters(AlgorithmIdentifierType $algo): ?Element |
|
| 515 | + { |
|
| 516 | + $seq = $algo->toASN1(); |
|
| 517 | + return $seq->has(1) ? $seq->at(1)->asElement() : null; |
|
| 518 | + } |
|
| 519 | 519 | } |
@@ -18,81 +18,81 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class PathValidationResult |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * Certificates in a certification path. |
|
| 23 | - * |
|
| 24 | - * @var Certificate[] |
|
| 25 | - */ |
|
| 26 | - protected $_certificates; |
|
| 21 | + /** |
|
| 22 | + * Certificates in a certification path. |
|
| 23 | + * |
|
| 24 | + * @var Certificate[] |
|
| 25 | + */ |
|
| 26 | + protected $_certificates; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Valid policy tree. |
|
| 30 | - * |
|
| 31 | - * @var null|PolicyTree |
|
| 32 | - */ |
|
| 33 | - protected $_policyTree; |
|
| 28 | + /** |
|
| 29 | + * Valid policy tree. |
|
| 30 | + * |
|
| 31 | + * @var null|PolicyTree |
|
| 32 | + */ |
|
| 33 | + protected $_policyTree; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * End-entity certificate's public key. |
|
| 37 | - * |
|
| 38 | - * @var PublicKeyInfo |
|
| 39 | - */ |
|
| 40 | - protected $_publicKeyInfo; |
|
| 35 | + /** |
|
| 36 | + * End-entity certificate's public key. |
|
| 37 | + * |
|
| 38 | + * @var PublicKeyInfo |
|
| 39 | + */ |
|
| 40 | + protected $_publicKeyInfo; |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Public key algorithm. |
|
| 44 | - * |
|
| 45 | - * @var AlgorithmIdentifierType |
|
| 46 | - */ |
|
| 47 | - protected $_publicKeyAlgo; |
|
| 42 | + /** |
|
| 43 | + * Public key algorithm. |
|
| 44 | + * |
|
| 45 | + * @var AlgorithmIdentifierType |
|
| 46 | + */ |
|
| 47 | + protected $_publicKeyAlgo; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Public key parameters. |
|
| 51 | - * |
|
| 52 | - * @var null|Element |
|
| 53 | - */ |
|
| 54 | - protected $_publicKeyParameters; |
|
| 49 | + /** |
|
| 50 | + * Public key parameters. |
|
| 51 | + * |
|
| 52 | + * @var null|Element |
|
| 53 | + */ |
|
| 54 | + protected $_publicKeyParameters; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Constructor. |
|
| 58 | - * |
|
| 59 | - * @param Certificate[] $certificates Certificates in a certification path |
|
| 60 | - * @param null|PolicyTree $policy_tree Valid policy tree |
|
| 61 | - * @param PublicKeyInfo $pubkey_info Public key of the end-entity certificate |
|
| 62 | - * @param AlgorithmIdentifierType $algo Public key algorithm of the end-entity certificate |
|
| 63 | - * @param null|Element $params Algorithm parameters |
|
| 64 | - */ |
|
| 65 | - public function __construct(array $certificates, ?PolicyTree $policy_tree, |
|
| 66 | - PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
| 67 | - ?Element $params = null) |
|
| 68 | - { |
|
| 69 | - $this->_certificates = array_values($certificates); |
|
| 70 | - $this->_policyTree = $policy_tree; |
|
| 71 | - $this->_publicKeyInfo = $pubkey_info; |
|
| 72 | - $this->_publicKeyAlgo = $algo; |
|
| 73 | - $this->_publicKeyParameters = $params; |
|
| 74 | - } |
|
| 56 | + /** |
|
| 57 | + * Constructor. |
|
| 58 | + * |
|
| 59 | + * @param Certificate[] $certificates Certificates in a certification path |
|
| 60 | + * @param null|PolicyTree $policy_tree Valid policy tree |
|
| 61 | + * @param PublicKeyInfo $pubkey_info Public key of the end-entity certificate |
|
| 62 | + * @param AlgorithmIdentifierType $algo Public key algorithm of the end-entity certificate |
|
| 63 | + * @param null|Element $params Algorithm parameters |
|
| 64 | + */ |
|
| 65 | + public function __construct(array $certificates, ?PolicyTree $policy_tree, |
|
| 66 | + PublicKeyInfo $pubkey_info, AlgorithmIdentifierType $algo, |
|
| 67 | + ?Element $params = null) |
|
| 68 | + { |
|
| 69 | + $this->_certificates = array_values($certificates); |
|
| 70 | + $this->_policyTree = $policy_tree; |
|
| 71 | + $this->_publicKeyInfo = $pubkey_info; |
|
| 72 | + $this->_publicKeyAlgo = $algo; |
|
| 73 | + $this->_publicKeyParameters = $params; |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - /** |
|
| 77 | - * Get end-entity certificate. |
|
| 78 | - * |
|
| 79 | - * @return Certificate |
|
| 80 | - */ |
|
| 81 | - public function certificate(): Certificate |
|
| 82 | - { |
|
| 83 | - return $this->_certificates[count($this->_certificates) - 1]; |
|
| 84 | - } |
|
| 76 | + /** |
|
| 77 | + * Get end-entity certificate. |
|
| 78 | + * |
|
| 79 | + * @return Certificate |
|
| 80 | + */ |
|
| 81 | + public function certificate(): Certificate |
|
| 82 | + { |
|
| 83 | + return $this->_certificates[count($this->_certificates) - 1]; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Get certificate policies of the end-entity certificate. |
|
| 88 | - * |
|
| 89 | - * @return PolicyInformation[] |
|
| 90 | - */ |
|
| 91 | - public function policies(): array |
|
| 92 | - { |
|
| 93 | - if (!$this->_policyTree) { |
|
| 94 | - return []; |
|
| 95 | - } |
|
| 96 | - return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
| 97 | - } |
|
| 86 | + /** |
|
| 87 | + * Get certificate policies of the end-entity certificate. |
|
| 88 | + * |
|
| 89 | + * @return PolicyInformation[] |
|
| 90 | + */ |
|
| 91 | + public function policies(): array |
|
| 92 | + { |
|
| 93 | + if (!$this->_policyTree) { |
|
| 94 | + return []; |
|
| 95 | + } |
|
| 96 | + return $this->_policyTree->policiesAtDepth(count($this->_certificates)); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -14,282 +14,282 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class PathValidationConfig |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Maximum allowed certification path length. |
|
| 19 | - * |
|
| 20 | - * @var int |
|
| 21 | - */ |
|
| 22 | - protected $_maxLength; |
|
| 17 | + /** |
|
| 18 | + * Maximum allowed certification path length. |
|
| 19 | + * |
|
| 20 | + * @var int |
|
| 21 | + */ |
|
| 22 | + protected $_maxLength; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Reference time. |
|
| 26 | - * |
|
| 27 | - * @var \DateTimeImmutable |
|
| 28 | - */ |
|
| 29 | - protected $_dateTime; |
|
| 24 | + /** |
|
| 25 | + * Reference time. |
|
| 26 | + * |
|
| 27 | + * @var \DateTimeImmutable |
|
| 28 | + */ |
|
| 29 | + protected $_dateTime; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * List of acceptable policy identifiers. |
|
| 33 | - * |
|
| 34 | - * @var string[] |
|
| 35 | - */ |
|
| 36 | - protected $_policySet; |
|
| 31 | + /** |
|
| 32 | + * List of acceptable policy identifiers. |
|
| 33 | + * |
|
| 34 | + * @var string[] |
|
| 35 | + */ |
|
| 36 | + protected $_policySet; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Trust anchor certificate. |
|
| 40 | - * |
|
| 41 | - * If not set, path validation uses the first certificate of the path. |
|
| 42 | - * |
|
| 43 | - * @var null|Certificate |
|
| 44 | - */ |
|
| 45 | - protected $_trustAnchor; |
|
| 38 | + /** |
|
| 39 | + * Trust anchor certificate. |
|
| 40 | + * |
|
| 41 | + * If not set, path validation uses the first certificate of the path. |
|
| 42 | + * |
|
| 43 | + * @var null|Certificate |
|
| 44 | + */ |
|
| 45 | + protected $_trustAnchor; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Whether policy mapping in inhibited. |
|
| 49 | - * |
|
| 50 | - * Setting this to true disallows policy mapping. |
|
| 51 | - * |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $_policyMappingInhibit; |
|
| 47 | + /** |
|
| 48 | + * Whether policy mapping in inhibited. |
|
| 49 | + * |
|
| 50 | + * Setting this to true disallows policy mapping. |
|
| 51 | + * |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $_policyMappingInhibit; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * Whether the path must be valid for at least one policy in the |
|
| 58 | - * initial policy set. |
|
| 59 | - * |
|
| 60 | - * @var bool |
|
| 61 | - */ |
|
| 62 | - protected $_explicitPolicy; |
|
| 56 | + /** |
|
| 57 | + * Whether the path must be valid for at least one policy in the |
|
| 58 | + * initial policy set. |
|
| 59 | + * |
|
| 60 | + * @var bool |
|
| 61 | + */ |
|
| 62 | + protected $_explicitPolicy; |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Whether anyPolicy OID processing should be inhibited. |
|
| 66 | - * |
|
| 67 | - * Setting this to true disallows the usage of anyPolicy. |
|
| 68 | - * |
|
| 69 | - * @var bool |
|
| 70 | - */ |
|
| 71 | - protected $_anyPolicyInhibit; |
|
| 64 | + /** |
|
| 65 | + * Whether anyPolicy OID processing should be inhibited. |
|
| 66 | + * |
|
| 67 | + * Setting this to true disallows the usage of anyPolicy. |
|
| 68 | + * |
|
| 69 | + * @var bool |
|
| 70 | + */ |
|
| 71 | + protected $_anyPolicyInhibit; |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * @todo Implement |
|
| 75 | - * |
|
| 76 | - * @var mixed |
|
| 77 | - */ |
|
| 78 | - protected $_permittedSubtrees; |
|
| 73 | + /** |
|
| 74 | + * @todo Implement |
|
| 75 | + * |
|
| 76 | + * @var mixed |
|
| 77 | + */ |
|
| 78 | + protected $_permittedSubtrees; |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @todo Implement |
|
| 82 | - * |
|
| 83 | - * @var mixed |
|
| 84 | - */ |
|
| 85 | - protected $_excludedSubtrees; |
|
| 80 | + /** |
|
| 81 | + * @todo Implement |
|
| 82 | + * |
|
| 83 | + * @var mixed |
|
| 84 | + */ |
|
| 85 | + protected $_excludedSubtrees; |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * Constructor. |
|
| 89 | - * |
|
| 90 | - * @param \DateTimeImmutable $dt Reference date and time |
|
| 91 | - * @param int $max_length Maximum certification path length |
|
| 92 | - */ |
|
| 93 | - public function __construct(\DateTimeImmutable $dt, int $max_length) |
|
| 94 | - { |
|
| 95 | - $this->_dateTime = $dt; |
|
| 96 | - $this->_maxLength = $max_length; |
|
| 97 | - $this->_policySet = [PolicyInformation::OID_ANY_POLICY]; |
|
| 98 | - $this->_policyMappingInhibit = false; |
|
| 99 | - $this->_explicitPolicy = false; |
|
| 100 | - $this->_anyPolicyInhibit = false; |
|
| 101 | - } |
|
| 87 | + /** |
|
| 88 | + * Constructor. |
|
| 89 | + * |
|
| 90 | + * @param \DateTimeImmutable $dt Reference date and time |
|
| 91 | + * @param int $max_length Maximum certification path length |
|
| 92 | + */ |
|
| 93 | + public function __construct(\DateTimeImmutable $dt, int $max_length) |
|
| 94 | + { |
|
| 95 | + $this->_dateTime = $dt; |
|
| 96 | + $this->_maxLength = $max_length; |
|
| 97 | + $this->_policySet = [PolicyInformation::OID_ANY_POLICY]; |
|
| 98 | + $this->_policyMappingInhibit = false; |
|
| 99 | + $this->_explicitPolicy = false; |
|
| 100 | + $this->_anyPolicyInhibit = false; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Get default configuration. |
|
| 105 | - * |
|
| 106 | - * @return self |
|
| 107 | - */ |
|
| 108 | - public static function defaultConfig(): self |
|
| 109 | - { |
|
| 110 | - return new self(new \DateTimeImmutable(), 3); |
|
| 111 | - } |
|
| 103 | + /** |
|
| 104 | + * Get default configuration. |
|
| 105 | + * |
|
| 106 | + * @return self |
|
| 107 | + */ |
|
| 108 | + public static function defaultConfig(): self |
|
| 109 | + { |
|
| 110 | + return new self(new \DateTimeImmutable(), 3); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * Get self with maximum path length. |
|
| 115 | - * |
|
| 116 | - * @param int $length |
|
| 117 | - * |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public function withMaxLength(int $length): self |
|
| 121 | - { |
|
| 122 | - $obj = clone $this; |
|
| 123 | - $obj->_maxLength = $length; |
|
| 124 | - return $obj; |
|
| 125 | - } |
|
| 113 | + /** |
|
| 114 | + * Get self with maximum path length. |
|
| 115 | + * |
|
| 116 | + * @param int $length |
|
| 117 | + * |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public function withMaxLength(int $length): self |
|
| 121 | + { |
|
| 122 | + $obj = clone $this; |
|
| 123 | + $obj->_maxLength = $length; |
|
| 124 | + return $obj; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Get self with reference date and time. |
|
| 129 | - * |
|
| 130 | - * @param \DateTimeImmutable $dt |
|
| 131 | - * |
|
| 132 | - * @return self |
|
| 133 | - */ |
|
| 134 | - public function withDateTime(\DateTimeImmutable $dt): self |
|
| 135 | - { |
|
| 136 | - $obj = clone $this; |
|
| 137 | - $obj->_dateTime = $dt; |
|
| 138 | - return $obj; |
|
| 139 | - } |
|
| 127 | + /** |
|
| 128 | + * Get self with reference date and time. |
|
| 129 | + * |
|
| 130 | + * @param \DateTimeImmutable $dt |
|
| 131 | + * |
|
| 132 | + * @return self |
|
| 133 | + */ |
|
| 134 | + public function withDateTime(\DateTimeImmutable $dt): self |
|
| 135 | + { |
|
| 136 | + $obj = clone $this; |
|
| 137 | + $obj->_dateTime = $dt; |
|
| 138 | + return $obj; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - /** |
|
| 142 | - * Get self with trust anchor certificate. |
|
| 143 | - * |
|
| 144 | - * @param Certificate $ca |
|
| 145 | - * |
|
| 146 | - * @return self |
|
| 147 | - */ |
|
| 148 | - public function withTrustAnchor(Certificate $ca): self |
|
| 149 | - { |
|
| 150 | - $obj = clone $this; |
|
| 151 | - $obj->_trustAnchor = $ca; |
|
| 152 | - return $obj; |
|
| 153 | - } |
|
| 141 | + /** |
|
| 142 | + * Get self with trust anchor certificate. |
|
| 143 | + * |
|
| 144 | + * @param Certificate $ca |
|
| 145 | + * |
|
| 146 | + * @return self |
|
| 147 | + */ |
|
| 148 | + public function withTrustAnchor(Certificate $ca): self |
|
| 149 | + { |
|
| 150 | + $obj = clone $this; |
|
| 151 | + $obj->_trustAnchor = $ca; |
|
| 152 | + return $obj; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - /** |
|
| 156 | - * Get self with initial-policy-mapping-inhibit set. |
|
| 157 | - * |
|
| 158 | - * @param bool $flag |
|
| 159 | - * |
|
| 160 | - * @return self |
|
| 161 | - */ |
|
| 162 | - public function withPolicyMappingInhibit(bool $flag): self |
|
| 163 | - { |
|
| 164 | - $obj = clone $this; |
|
| 165 | - $obj->_policyMappingInhibit = $flag; |
|
| 166 | - return $obj; |
|
| 167 | - } |
|
| 155 | + /** |
|
| 156 | + * Get self with initial-policy-mapping-inhibit set. |
|
| 157 | + * |
|
| 158 | + * @param bool $flag |
|
| 159 | + * |
|
| 160 | + * @return self |
|
| 161 | + */ |
|
| 162 | + public function withPolicyMappingInhibit(bool $flag): self |
|
| 163 | + { |
|
| 164 | + $obj = clone $this; |
|
| 165 | + $obj->_policyMappingInhibit = $flag; |
|
| 166 | + return $obj; |
|
| 167 | + } |
|
| 168 | 168 | |
| 169 | - /** |
|
| 170 | - * Get self with initial-explicit-policy set. |
|
| 171 | - * |
|
| 172 | - * @param bool $flag |
|
| 173 | - * |
|
| 174 | - * @return self |
|
| 175 | - */ |
|
| 176 | - public function withExplicitPolicy(bool $flag): self |
|
| 177 | - { |
|
| 178 | - $obj = clone $this; |
|
| 179 | - $obj->_explicitPolicy = $flag; |
|
| 180 | - return $obj; |
|
| 181 | - } |
|
| 169 | + /** |
|
| 170 | + * Get self with initial-explicit-policy set. |
|
| 171 | + * |
|
| 172 | + * @param bool $flag |
|
| 173 | + * |
|
| 174 | + * @return self |
|
| 175 | + */ |
|
| 176 | + public function withExplicitPolicy(bool $flag): self |
|
| 177 | + { |
|
| 178 | + $obj = clone $this; |
|
| 179 | + $obj->_explicitPolicy = $flag; |
|
| 180 | + return $obj; |
|
| 181 | + } |
|
| 182 | 182 | |
| 183 | - /** |
|
| 184 | - * Get self with initial-any-policy-inhibit set. |
|
| 185 | - * |
|
| 186 | - * @param bool $flag |
|
| 187 | - * |
|
| 188 | - * @return self |
|
| 189 | - */ |
|
| 190 | - public function withAnyPolicyInhibit(bool $flag): self |
|
| 191 | - { |
|
| 192 | - $obj = clone $this; |
|
| 193 | - $obj->_anyPolicyInhibit = $flag; |
|
| 194 | - return $obj; |
|
| 195 | - } |
|
| 183 | + /** |
|
| 184 | + * Get self with initial-any-policy-inhibit set. |
|
| 185 | + * |
|
| 186 | + * @param bool $flag |
|
| 187 | + * |
|
| 188 | + * @return self |
|
| 189 | + */ |
|
| 190 | + public function withAnyPolicyInhibit(bool $flag): self |
|
| 191 | + { |
|
| 192 | + $obj = clone $this; |
|
| 193 | + $obj->_anyPolicyInhibit = $flag; |
|
| 194 | + return $obj; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - /** |
|
| 198 | - * Get self with user-initial-policy-set set to policy OIDs. |
|
| 199 | - * |
|
| 200 | - * @param string ...$policies List of policy OIDs |
|
| 201 | - * |
|
| 202 | - * @return self |
|
| 203 | - */ |
|
| 204 | - public function withPolicySet(string ...$policies): self |
|
| 205 | - { |
|
| 206 | - $obj = clone $this; |
|
| 207 | - $obj->_policySet = $policies; |
|
| 208 | - return $obj; |
|
| 209 | - } |
|
| 197 | + /** |
|
| 198 | + * Get self with user-initial-policy-set set to policy OIDs. |
|
| 199 | + * |
|
| 200 | + * @param string ...$policies List of policy OIDs |
|
| 201 | + * |
|
| 202 | + * @return self |
|
| 203 | + */ |
|
| 204 | + public function withPolicySet(string ...$policies): self |
|
| 205 | + { |
|
| 206 | + $obj = clone $this; |
|
| 207 | + $obj->_policySet = $policies; |
|
| 208 | + return $obj; |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | - /** |
|
| 212 | - * Get maximum certification path length. |
|
| 213 | - * |
|
| 214 | - * @return int |
|
| 215 | - */ |
|
| 216 | - public function maxLength(): int |
|
| 217 | - { |
|
| 218 | - return $this->_maxLength; |
|
| 219 | - } |
|
| 211 | + /** |
|
| 212 | + * Get maximum certification path length. |
|
| 213 | + * |
|
| 214 | + * @return int |
|
| 215 | + */ |
|
| 216 | + public function maxLength(): int |
|
| 217 | + { |
|
| 218 | + return $this->_maxLength; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Get reference date and time. |
|
| 223 | - * |
|
| 224 | - * @return \DateTimeImmutable |
|
| 225 | - */ |
|
| 226 | - public function dateTime(): \DateTimeImmutable |
|
| 227 | - { |
|
| 228 | - return $this->_dateTime; |
|
| 229 | - } |
|
| 221 | + /** |
|
| 222 | + * Get reference date and time. |
|
| 223 | + * |
|
| 224 | + * @return \DateTimeImmutable |
|
| 225 | + */ |
|
| 226 | + public function dateTime(): \DateTimeImmutable |
|
| 227 | + { |
|
| 228 | + return $this->_dateTime; |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Get user-initial-policy-set. |
|
| 233 | - * |
|
| 234 | - * @return string[] Array of OID's |
|
| 235 | - */ |
|
| 236 | - public function policySet(): array |
|
| 237 | - { |
|
| 238 | - return $this->_policySet; |
|
| 239 | - } |
|
| 231 | + /** |
|
| 232 | + * Get user-initial-policy-set. |
|
| 233 | + * |
|
| 234 | + * @return string[] Array of OID's |
|
| 235 | + */ |
|
| 236 | + public function policySet(): array |
|
| 237 | + { |
|
| 238 | + return $this->_policySet; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * Check whether trust anchor certificate is set. |
|
| 243 | - * |
|
| 244 | - * @return bool |
|
| 245 | - */ |
|
| 246 | - public function hasTrustAnchor(): bool |
|
| 247 | - { |
|
| 248 | - return isset($this->_trustAnchor); |
|
| 249 | - } |
|
| 241 | + /** |
|
| 242 | + * Check whether trust anchor certificate is set. |
|
| 243 | + * |
|
| 244 | + * @return bool |
|
| 245 | + */ |
|
| 246 | + public function hasTrustAnchor(): bool |
|
| 247 | + { |
|
| 248 | + return isset($this->_trustAnchor); |
|
| 249 | + } |
|
| 250 | 250 | |
| 251 | - /** |
|
| 252 | - * Get trust anchor certificate. |
|
| 253 | - * |
|
| 254 | - * @throws \LogicException If not set |
|
| 255 | - * |
|
| 256 | - * @return Certificate |
|
| 257 | - */ |
|
| 258 | - public function trustAnchor(): Certificate |
|
| 259 | - { |
|
| 260 | - if (!$this->hasTrustAnchor()) { |
|
| 261 | - throw new \LogicException('No trust anchor.'); |
|
| 262 | - } |
|
| 263 | - return $this->_trustAnchor; |
|
| 264 | - } |
|
| 251 | + /** |
|
| 252 | + * Get trust anchor certificate. |
|
| 253 | + * |
|
| 254 | + * @throws \LogicException If not set |
|
| 255 | + * |
|
| 256 | + * @return Certificate |
|
| 257 | + */ |
|
| 258 | + public function trustAnchor(): Certificate |
|
| 259 | + { |
|
| 260 | + if (!$this->hasTrustAnchor()) { |
|
| 261 | + throw new \LogicException('No trust anchor.'); |
|
| 262 | + } |
|
| 263 | + return $this->_trustAnchor; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * Get initial-policy-mapping-inhibit. |
|
| 268 | - * |
|
| 269 | - * @return bool |
|
| 270 | - */ |
|
| 271 | - public function policyMappingInhibit(): bool |
|
| 272 | - { |
|
| 273 | - return $this->_policyMappingInhibit; |
|
| 274 | - } |
|
| 266 | + /** |
|
| 267 | + * Get initial-policy-mapping-inhibit. |
|
| 268 | + * |
|
| 269 | + * @return bool |
|
| 270 | + */ |
|
| 271 | + public function policyMappingInhibit(): bool |
|
| 272 | + { |
|
| 273 | + return $this->_policyMappingInhibit; |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * Get initial-explicit-policy. |
|
| 278 | - * |
|
| 279 | - * @return bool |
|
| 280 | - */ |
|
| 281 | - public function explicitPolicy(): bool |
|
| 282 | - { |
|
| 283 | - return $this->_explicitPolicy; |
|
| 284 | - } |
|
| 276 | + /** |
|
| 277 | + * Get initial-explicit-policy. |
|
| 278 | + * |
|
| 279 | + * @return bool |
|
| 280 | + */ |
|
| 281 | + public function explicitPolicy(): bool |
|
| 282 | + { |
|
| 283 | + return $this->_explicitPolicy; |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - /** |
|
| 287 | - * Get initial-any-policy-inhibit. |
|
| 288 | - * |
|
| 289 | - * @return bool |
|
| 290 | - */ |
|
| 291 | - public function anyPolicyInhibit(): bool |
|
| 292 | - { |
|
| 293 | - return $this->_anyPolicyInhibit; |
|
| 294 | - } |
|
| 286 | + /** |
|
| 287 | + * Get initial-any-policy-inhibit. |
|
| 288 | + * |
|
| 289 | + * @return bool |
|
| 290 | + */ |
|
| 291 | + public function anyPolicyInhibit(): bool |
|
| 292 | + { |
|
| 293 | + return $this->_anyPolicyInhibit; |
|
| 294 | + } |
|
| 295 | 295 | } |
@@ -17,599 +17,599 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class PathValidator |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * Crypto engine. |
|
| 22 | - * |
|
| 23 | - * @var Crypto |
|
| 24 | - */ |
|
| 25 | - protected $_crypto; |
|
| 20 | + /** |
|
| 21 | + * Crypto engine. |
|
| 22 | + * |
|
| 23 | + * @var Crypto |
|
| 24 | + */ |
|
| 25 | + protected $_crypto; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Path validation configuration. |
|
| 29 | - * |
|
| 30 | - * @var PathValidationConfig |
|
| 31 | - */ |
|
| 32 | - protected $_config; |
|
| 27 | + /** |
|
| 28 | + * Path validation configuration. |
|
| 29 | + * |
|
| 30 | + * @var PathValidationConfig |
|
| 31 | + */ |
|
| 32 | + protected $_config; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Certification path. |
|
| 36 | - * |
|
| 37 | - * @var Certificate[] |
|
| 38 | - */ |
|
| 39 | - protected $_certificates; |
|
| 34 | + /** |
|
| 35 | + * Certification path. |
|
| 36 | + * |
|
| 37 | + * @var Certificate[] |
|
| 38 | + */ |
|
| 39 | + protected $_certificates; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Certification path trust anchor. |
|
| 43 | - * |
|
| 44 | - * @var Certificate |
|
| 45 | - */ |
|
| 46 | - protected $_trustAnchor; |
|
| 41 | + /** |
|
| 42 | + * Certification path trust anchor. |
|
| 43 | + * |
|
| 44 | + * @var Certificate |
|
| 45 | + */ |
|
| 46 | + protected $_trustAnchor; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Constructor. |
|
| 50 | - * |
|
| 51 | - * @param Crypto $crypto Crypto engine |
|
| 52 | - * @param PathValidationConfig $config Validation config |
|
| 53 | - * @param Certificate ...$certificates Certificates from the trust anchor to |
|
| 54 | - * the end-entity certificate |
|
| 55 | - */ |
|
| 56 | - public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
| 57 | - Certificate ...$certificates) |
|
| 58 | - { |
|
| 59 | - if (!count($certificates)) { |
|
| 60 | - throw new \LogicException('No certificates.'); |
|
| 61 | - } |
|
| 62 | - $this->_crypto = $crypto; |
|
| 63 | - $this->_config = $config; |
|
| 64 | - $this->_certificates = $certificates; |
|
| 65 | - // if trust anchor is explicitly given in configuration |
|
| 66 | - if ($config->hasTrustAnchor()) { |
|
| 67 | - $this->_trustAnchor = $config->trustAnchor(); |
|
| 68 | - } else { |
|
| 69 | - $this->_trustAnchor = $certificates[0]; |
|
| 70 | - } |
|
| 71 | - } |
|
| 48 | + /** |
|
| 49 | + * Constructor. |
|
| 50 | + * |
|
| 51 | + * @param Crypto $crypto Crypto engine |
|
| 52 | + * @param PathValidationConfig $config Validation config |
|
| 53 | + * @param Certificate ...$certificates Certificates from the trust anchor to |
|
| 54 | + * the end-entity certificate |
|
| 55 | + */ |
|
| 56 | + public function __construct(Crypto $crypto, PathValidationConfig $config, |
|
| 57 | + Certificate ...$certificates) |
|
| 58 | + { |
|
| 59 | + if (!count($certificates)) { |
|
| 60 | + throw new \LogicException('No certificates.'); |
|
| 61 | + } |
|
| 62 | + $this->_crypto = $crypto; |
|
| 63 | + $this->_config = $config; |
|
| 64 | + $this->_certificates = $certificates; |
|
| 65 | + // if trust anchor is explicitly given in configuration |
|
| 66 | + if ($config->hasTrustAnchor()) { |
|
| 67 | + $this->_trustAnchor = $config->trustAnchor(); |
|
| 68 | + } else { |
|
| 69 | + $this->_trustAnchor = $certificates[0]; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * Validate certification path. |
|
| 75 | - * |
|
| 76 | - * @throws PathValidationException |
|
| 77 | - * |
|
| 78 | - * @return PathValidationResult |
|
| 79 | - */ |
|
| 80 | - public function validate(): PathValidationResult |
|
| 81 | - { |
|
| 82 | - $n = count($this->_certificates); |
|
| 83 | - $state = ValidatorState::initialize( |
|
| 84 | - $this->_config, $this->_trustAnchor, $n); |
|
| 85 | - for ($i = 0; $i < $n; ++$i) { |
|
| 86 | - $state = $state->withIndex($i + 1); |
|
| 87 | - $cert = $this->_certificates[$i]; |
|
| 88 | - // process certificate (section 6.1.3.) |
|
| 89 | - $state = $this->_processCertificate($state, $cert); |
|
| 90 | - if (!$state->isFinal()) { |
|
| 91 | - // prepare next certificate (section 6.1.4.) |
|
| 92 | - $state = $this->_prepareNext($state, $cert); |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - if (!isset($cert)) { |
|
| 96 | - throw new \LogicException('No certificates.'); |
|
| 97 | - } |
|
| 98 | - // wrap-up (section 6.1.5.) |
|
| 99 | - $state = $this->_wrapUp($state, $cert); |
|
| 100 | - // return outputs |
|
| 101 | - return $state->getResult($this->_certificates); |
|
| 102 | - } |
|
| 73 | + /** |
|
| 74 | + * Validate certification path. |
|
| 75 | + * |
|
| 76 | + * @throws PathValidationException |
|
| 77 | + * |
|
| 78 | + * @return PathValidationResult |
|
| 79 | + */ |
|
| 80 | + public function validate(): PathValidationResult |
|
| 81 | + { |
|
| 82 | + $n = count($this->_certificates); |
|
| 83 | + $state = ValidatorState::initialize( |
|
| 84 | + $this->_config, $this->_trustAnchor, $n); |
|
| 85 | + for ($i = 0; $i < $n; ++$i) { |
|
| 86 | + $state = $state->withIndex($i + 1); |
|
| 87 | + $cert = $this->_certificates[$i]; |
|
| 88 | + // process certificate (section 6.1.3.) |
|
| 89 | + $state = $this->_processCertificate($state, $cert); |
|
| 90 | + if (!$state->isFinal()) { |
|
| 91 | + // prepare next certificate (section 6.1.4.) |
|
| 92 | + $state = $this->_prepareNext($state, $cert); |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + if (!isset($cert)) { |
|
| 96 | + throw new \LogicException('No certificates.'); |
|
| 97 | + } |
|
| 98 | + // wrap-up (section 6.1.5.) |
|
| 99 | + $state = $this->_wrapUp($state, $cert); |
|
| 100 | + // return outputs |
|
| 101 | + return $state->getResult($this->_certificates); |
|
| 102 | + } |
|
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
| 106 | - * |
|
| 107 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
| 108 | - * |
|
| 109 | - * @param ValidatorState $state |
|
| 110 | - * @param Certificate $cert |
|
| 111 | - * |
|
| 112 | - * @throws PathValidationException |
|
| 113 | - * |
|
| 114 | - * @return ValidatorState |
|
| 115 | - */ |
|
| 116 | - private function _processCertificate(ValidatorState $state, |
|
| 117 | - Certificate $cert): ValidatorState |
|
| 118 | - { |
|
| 119 | - // (a.1) verify signature |
|
| 120 | - $this->_verifySignature($state, $cert); |
|
| 121 | - // (a.2) check validity period |
|
| 122 | - $this->_checkValidity($cert); |
|
| 123 | - // (a.3) check that certificate is not revoked |
|
| 124 | - $this->_checkRevocation($cert); |
|
| 125 | - // (a.4) check issuer |
|
| 126 | - $this->_checkIssuer($state, $cert); |
|
| 127 | - // (b)(c) if certificate is self-issued and it is not |
|
| 128 | - // the final certificate in the path, skip this step |
|
| 129 | - if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
| 130 | - // (b) check permitted subtrees |
|
| 131 | - $this->_checkPermittedSubtrees($state, $cert); |
|
| 132 | - // (c) check excluded subtrees |
|
| 133 | - $this->_checkExcludedSubtrees($state, $cert); |
|
| 134 | - } |
|
| 135 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 136 | - if ($extensions->hasCertificatePolicies()) { |
|
| 137 | - // (d) process policy information |
|
| 138 | - if ($state->hasValidPolicyTree()) { |
|
| 139 | - $state = $state->validPolicyTree()->processPolicies($state, $cert); |
|
| 140 | - } |
|
| 141 | - } else { |
|
| 142 | - // (e) certificate policies extension not present, |
|
| 143 | - // set the valid_policy_tree to NULL |
|
| 144 | - $state = $state->withoutValidPolicyTree(); |
|
| 145 | - } |
|
| 146 | - // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
| 147 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 148 | - throw new PathValidationException('No valid policies.'); |
|
| 149 | - } |
|
| 150 | - return $state; |
|
| 151 | - } |
|
| 104 | + /** |
|
| 105 | + * Apply basic certificate processing according to RFC 5280 section 6.1.3. |
|
| 106 | + * |
|
| 107 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.3 |
|
| 108 | + * |
|
| 109 | + * @param ValidatorState $state |
|
| 110 | + * @param Certificate $cert |
|
| 111 | + * |
|
| 112 | + * @throws PathValidationException |
|
| 113 | + * |
|
| 114 | + * @return ValidatorState |
|
| 115 | + */ |
|
| 116 | + private function _processCertificate(ValidatorState $state, |
|
| 117 | + Certificate $cert): ValidatorState |
|
| 118 | + { |
|
| 119 | + // (a.1) verify signature |
|
| 120 | + $this->_verifySignature($state, $cert); |
|
| 121 | + // (a.2) check validity period |
|
| 122 | + $this->_checkValidity($cert); |
|
| 123 | + // (a.3) check that certificate is not revoked |
|
| 124 | + $this->_checkRevocation($cert); |
|
| 125 | + // (a.4) check issuer |
|
| 126 | + $this->_checkIssuer($state, $cert); |
|
| 127 | + // (b)(c) if certificate is self-issued and it is not |
|
| 128 | + // the final certificate in the path, skip this step |
|
| 129 | + if (!($cert->isSelfIssued() && !$state->isFinal())) { |
|
| 130 | + // (b) check permitted subtrees |
|
| 131 | + $this->_checkPermittedSubtrees($state, $cert); |
|
| 132 | + // (c) check excluded subtrees |
|
| 133 | + $this->_checkExcludedSubtrees($state, $cert); |
|
| 134 | + } |
|
| 135 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 136 | + if ($extensions->hasCertificatePolicies()) { |
|
| 137 | + // (d) process policy information |
|
| 138 | + if ($state->hasValidPolicyTree()) { |
|
| 139 | + $state = $state->validPolicyTree()->processPolicies($state, $cert); |
|
| 140 | + } |
|
| 141 | + } else { |
|
| 142 | + // (e) certificate policies extension not present, |
|
| 143 | + // set the valid_policy_tree to NULL |
|
| 144 | + $state = $state->withoutValidPolicyTree(); |
|
| 145 | + } |
|
| 146 | + // (f) check that explicit_policy > 0 or valid_policy_tree is set |
|
| 147 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 148 | + throw new PathValidationException('No valid policies.'); |
|
| 149 | + } |
|
| 150 | + return $state; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - /** |
|
| 154 | - * Apply preparation for the certificate i+1 according to rfc5280 section |
|
| 155 | - * 6.1.4. |
|
| 156 | - * |
|
| 157 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
| 158 | - * |
|
| 159 | - * @param ValidatorState $state |
|
| 160 | - * @param Certificate $cert |
|
| 161 | - * |
|
| 162 | - * @return ValidatorState |
|
| 163 | - */ |
|
| 164 | - private function _prepareNext(ValidatorState $state, |
|
| 165 | - Certificate $cert): ValidatorState |
|
| 166 | - { |
|
| 167 | - // (a)(b) if policy mappings extension is present |
|
| 168 | - $state = $this->_preparePolicyMappings($state, $cert); |
|
| 169 | - // (c) assign working_issuer_name |
|
| 170 | - $state = $state->withWorkingIssuerName( |
|
| 171 | - $cert->tbsCertificate()->subject()); |
|
| 172 | - // (d)(e)(f) |
|
| 173 | - $state = $this->_setPublicKeyState($state, $cert); |
|
| 174 | - // (g) if name constraints extension is present |
|
| 175 | - $state = $this->_prepareNameConstraints($state, $cert); |
|
| 176 | - // (h) if certificate is not self-issued |
|
| 177 | - if (!$cert->isSelfIssued()) { |
|
| 178 | - $state = $this->_prepareNonSelfIssued($state); |
|
| 179 | - } |
|
| 180 | - // (i) if policy constraints extension is present |
|
| 181 | - $state = $this->_preparePolicyConstraints($state, $cert); |
|
| 182 | - // (j) if inhibit any policy extension is present |
|
| 183 | - $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
| 184 | - // (k) check basic constraints |
|
| 185 | - $this->_processBasicContraints($cert); |
|
| 186 | - // (l) verify max_path_length |
|
| 187 | - $state = $this->_verifyMaxPathLength($state, $cert); |
|
| 188 | - // (m) check pathLenContraint |
|
| 189 | - $state = $this->_processPathLengthContraint($state, $cert); |
|
| 190 | - // (n) check key usage |
|
| 191 | - $this->_checkKeyUsage($cert); |
|
| 192 | - // (o) process relevant extensions |
|
| 193 | - return $this->_processExtensions($state, $cert); |
|
| 194 | - } |
|
| 153 | + /** |
|
| 154 | + * Apply preparation for the certificate i+1 according to rfc5280 section |
|
| 155 | + * 6.1.4. |
|
| 156 | + * |
|
| 157 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.4 |
|
| 158 | + * |
|
| 159 | + * @param ValidatorState $state |
|
| 160 | + * @param Certificate $cert |
|
| 161 | + * |
|
| 162 | + * @return ValidatorState |
|
| 163 | + */ |
|
| 164 | + private function _prepareNext(ValidatorState $state, |
|
| 165 | + Certificate $cert): ValidatorState |
|
| 166 | + { |
|
| 167 | + // (a)(b) if policy mappings extension is present |
|
| 168 | + $state = $this->_preparePolicyMappings($state, $cert); |
|
| 169 | + // (c) assign working_issuer_name |
|
| 170 | + $state = $state->withWorkingIssuerName( |
|
| 171 | + $cert->tbsCertificate()->subject()); |
|
| 172 | + // (d)(e)(f) |
|
| 173 | + $state = $this->_setPublicKeyState($state, $cert); |
|
| 174 | + // (g) if name constraints extension is present |
|
| 175 | + $state = $this->_prepareNameConstraints($state, $cert); |
|
| 176 | + // (h) if certificate is not self-issued |
|
| 177 | + if (!$cert->isSelfIssued()) { |
|
| 178 | + $state = $this->_prepareNonSelfIssued($state); |
|
| 179 | + } |
|
| 180 | + // (i) if policy constraints extension is present |
|
| 181 | + $state = $this->_preparePolicyConstraints($state, $cert); |
|
| 182 | + // (j) if inhibit any policy extension is present |
|
| 183 | + $state = $this->_prepareInhibitAnyPolicy($state, $cert); |
|
| 184 | + // (k) check basic constraints |
|
| 185 | + $this->_processBasicContraints($cert); |
|
| 186 | + // (l) verify max_path_length |
|
| 187 | + $state = $this->_verifyMaxPathLength($state, $cert); |
|
| 188 | + // (m) check pathLenContraint |
|
| 189 | + $state = $this->_processPathLengthContraint($state, $cert); |
|
| 190 | + // (n) check key usage |
|
| 191 | + $this->_checkKeyUsage($cert); |
|
| 192 | + // (o) process relevant extensions |
|
| 193 | + return $this->_processExtensions($state, $cert); |
|
| 194 | + } |
|
| 195 | 195 | |
| 196 | - /** |
|
| 197 | - * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
| 198 | - * |
|
| 199 | - * @see https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
| 200 | - * |
|
| 201 | - * @param ValidatorState $state |
|
| 202 | - * @param Certificate $cert |
|
| 203 | - * |
|
| 204 | - * @throws PathValidationException |
|
| 205 | - * |
|
| 206 | - * @return ValidatorState |
|
| 207 | - */ |
|
| 208 | - private function _wrapUp(ValidatorState $state, |
|
| 209 | - Certificate $cert): ValidatorState |
|
| 210 | - { |
|
| 211 | - $tbs_cert = $cert->tbsCertificate(); |
|
| 212 | - $extensions = $tbs_cert->extensions(); |
|
| 213 | - // (a) |
|
| 214 | - if ($state->explicitPolicy() > 0) { |
|
| 215 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 216 | - } |
|
| 217 | - // (b) |
|
| 218 | - if ($extensions->hasPolicyConstraints()) { |
|
| 219 | - $ext = $extensions->policyConstraints(); |
|
| 220 | - if ($ext->hasRequireExplicitPolicy() && |
|
| 221 | - 0 === $ext->requireExplicitPolicy()) { |
|
| 222 | - $state = $state->withExplicitPolicy(0); |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - // (c)(d)(e) |
|
| 226 | - $state = $this->_setPublicKeyState($state, $cert); |
|
| 227 | - // (f) process relevant extensions |
|
| 228 | - $state = $this->_processExtensions($state, $cert); |
|
| 229 | - // (g) intersection of valid_policy_tree and the initial-policy-set |
|
| 230 | - $state = $this->_calculatePolicyIntersection($state); |
|
| 231 | - // check that explicit_policy > 0 or valid_policy_tree is set |
|
| 232 | - if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 233 | - throw new PathValidationException('No valid policies.'); |
|
| 234 | - } |
|
| 235 | - // path validation succeeded |
|
| 236 | - return $state; |
|
| 237 | - } |
|
| 196 | + /** |
|
| 197 | + * Apply wrap-up procedure according to RFC 5280 section 6.1.5. |
|
| 198 | + * |
|
| 199 | + * @see https://tools.ietf.org/html/rfc5280#section-6.1.5 |
|
| 200 | + * |
|
| 201 | + * @param ValidatorState $state |
|
| 202 | + * @param Certificate $cert |
|
| 203 | + * |
|
| 204 | + * @throws PathValidationException |
|
| 205 | + * |
|
| 206 | + * @return ValidatorState |
|
| 207 | + */ |
|
| 208 | + private function _wrapUp(ValidatorState $state, |
|
| 209 | + Certificate $cert): ValidatorState |
|
| 210 | + { |
|
| 211 | + $tbs_cert = $cert->tbsCertificate(); |
|
| 212 | + $extensions = $tbs_cert->extensions(); |
|
| 213 | + // (a) |
|
| 214 | + if ($state->explicitPolicy() > 0) { |
|
| 215 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 216 | + } |
|
| 217 | + // (b) |
|
| 218 | + if ($extensions->hasPolicyConstraints()) { |
|
| 219 | + $ext = $extensions->policyConstraints(); |
|
| 220 | + if ($ext->hasRequireExplicitPolicy() && |
|
| 221 | + 0 === $ext->requireExplicitPolicy()) { |
|
| 222 | + $state = $state->withExplicitPolicy(0); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + // (c)(d)(e) |
|
| 226 | + $state = $this->_setPublicKeyState($state, $cert); |
|
| 227 | + // (f) process relevant extensions |
|
| 228 | + $state = $this->_processExtensions($state, $cert); |
|
| 229 | + // (g) intersection of valid_policy_tree and the initial-policy-set |
|
| 230 | + $state = $this->_calculatePolicyIntersection($state); |
|
| 231 | + // check that explicit_policy > 0 or valid_policy_tree is set |
|
| 232 | + if (!($state->explicitPolicy() > 0 || $state->hasValidPolicyTree())) { |
|
| 233 | + throw new PathValidationException('No valid policies.'); |
|
| 234 | + } |
|
| 235 | + // path validation succeeded |
|
| 236 | + return $state; |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Update working_public_key, working_public_key_parameters and |
|
| 241 | - * working_public_key_algorithm state variables from certificate. |
|
| 242 | - * |
|
| 243 | - * @param ValidatorState $state |
|
| 244 | - * @param Certificate $cert |
|
| 245 | - * |
|
| 246 | - * @return ValidatorState |
|
| 247 | - */ |
|
| 248 | - private function _setPublicKeyState(ValidatorState $state, |
|
| 249 | - Certificate $cert): ValidatorState |
|
| 250 | - { |
|
| 251 | - $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 252 | - // assign working_public_key |
|
| 253 | - $state = $state->withWorkingPublicKey($pk_info); |
|
| 254 | - // assign working_public_key_parameters |
|
| 255 | - $params = ValidatorState::getAlgorithmParameters( |
|
| 256 | - $pk_info->algorithmIdentifier()); |
|
| 257 | - if (null !== $params) { |
|
| 258 | - $state = $state->withWorkingPublicKeyParameters($params); |
|
| 259 | - } else { |
|
| 260 | - // if algorithms differ, set parameters to null |
|
| 261 | - if ($pk_info->algorithmIdentifier()->oid() !== |
|
| 262 | - $state->workingPublicKeyAlgorithm()->oid()) { |
|
| 263 | - $state = $state->withWorkingPublicKeyParameters(null); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - // assign working_public_key_algorithm |
|
| 267 | - return $state->withWorkingPublicKeyAlgorithm( |
|
| 268 | - $pk_info->algorithmIdentifier()); |
|
| 269 | - } |
|
| 239 | + /** |
|
| 240 | + * Update working_public_key, working_public_key_parameters and |
|
| 241 | + * working_public_key_algorithm state variables from certificate. |
|
| 242 | + * |
|
| 243 | + * @param ValidatorState $state |
|
| 244 | + * @param Certificate $cert |
|
| 245 | + * |
|
| 246 | + * @return ValidatorState |
|
| 247 | + */ |
|
| 248 | + private function _setPublicKeyState(ValidatorState $state, |
|
| 249 | + Certificate $cert): ValidatorState |
|
| 250 | + { |
|
| 251 | + $pk_info = $cert->tbsCertificate()->subjectPublicKeyInfo(); |
|
| 252 | + // assign working_public_key |
|
| 253 | + $state = $state->withWorkingPublicKey($pk_info); |
|
| 254 | + // assign working_public_key_parameters |
|
| 255 | + $params = ValidatorState::getAlgorithmParameters( |
|
| 256 | + $pk_info->algorithmIdentifier()); |
|
| 257 | + if (null !== $params) { |
|
| 258 | + $state = $state->withWorkingPublicKeyParameters($params); |
|
| 259 | + } else { |
|
| 260 | + // if algorithms differ, set parameters to null |
|
| 261 | + if ($pk_info->algorithmIdentifier()->oid() !== |
|
| 262 | + $state->workingPublicKeyAlgorithm()->oid()) { |
|
| 263 | + $state = $state->withWorkingPublicKeyParameters(null); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + // assign working_public_key_algorithm |
|
| 267 | + return $state->withWorkingPublicKeyAlgorithm( |
|
| 268 | + $pk_info->algorithmIdentifier()); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - /** |
|
| 272 | - * Verify certificate signature. |
|
| 273 | - * |
|
| 274 | - * @param ValidatorState $state |
|
| 275 | - * @param Certificate $cert |
|
| 276 | - * |
|
| 277 | - * @throws PathValidationException |
|
| 278 | - */ |
|
| 279 | - private function _verifySignature(ValidatorState $state, |
|
| 280 | - Certificate $cert): void |
|
| 281 | - { |
|
| 282 | - try { |
|
| 283 | - $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
| 284 | - } catch (\RuntimeException $e) { |
|
| 285 | - throw new PathValidationException( |
|
| 286 | - 'Failed to verify signature: ' . $e->getMessage(), 0, $e); |
|
| 287 | - } |
|
| 288 | - if (!$valid) { |
|
| 289 | - throw new PathValidationException( |
|
| 290 | - "Certificate signature doesn't match."); |
|
| 291 | - } |
|
| 292 | - } |
|
| 271 | + /** |
|
| 272 | + * Verify certificate signature. |
|
| 273 | + * |
|
| 274 | + * @param ValidatorState $state |
|
| 275 | + * @param Certificate $cert |
|
| 276 | + * |
|
| 277 | + * @throws PathValidationException |
|
| 278 | + */ |
|
| 279 | + private function _verifySignature(ValidatorState $state, |
|
| 280 | + Certificate $cert): void |
|
| 281 | + { |
|
| 282 | + try { |
|
| 283 | + $valid = $cert->verify($state->workingPublicKey(), $this->_crypto); |
|
| 284 | + } catch (\RuntimeException $e) { |
|
| 285 | + throw new PathValidationException( |
|
| 286 | + 'Failed to verify signature: ' . $e->getMessage(), 0, $e); |
|
| 287 | + } |
|
| 288 | + if (!$valid) { |
|
| 289 | + throw new PathValidationException( |
|
| 290 | + "Certificate signature doesn't match."); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - /** |
|
| 295 | - * Check certificate validity. |
|
| 296 | - * |
|
| 297 | - * @param Certificate $cert |
|
| 298 | - * |
|
| 299 | - * @throws PathValidationException |
|
| 300 | - */ |
|
| 301 | - private function _checkValidity(Certificate $cert): void |
|
| 302 | - { |
|
| 303 | - $refdt = $this->_config->dateTime(); |
|
| 304 | - $validity = $cert->tbsCertificate()->validity(); |
|
| 305 | - if ($validity->notBefore()->dateTime()->diff($refdt)->invert) { |
|
| 306 | - throw new PathValidationException( |
|
| 307 | - 'Certificate validity period has not started.'); |
|
| 308 | - } |
|
| 309 | - if ($refdt->diff($validity->notAfter()->dateTime())->invert) { |
|
| 310 | - throw new PathValidationException('Certificate has expired.'); |
|
| 311 | - } |
|
| 312 | - } |
|
| 294 | + /** |
|
| 295 | + * Check certificate validity. |
|
| 296 | + * |
|
| 297 | + * @param Certificate $cert |
|
| 298 | + * |
|
| 299 | + * @throws PathValidationException |
|
| 300 | + */ |
|
| 301 | + private function _checkValidity(Certificate $cert): void |
|
| 302 | + { |
|
| 303 | + $refdt = $this->_config->dateTime(); |
|
| 304 | + $validity = $cert->tbsCertificate()->validity(); |
|
| 305 | + if ($validity->notBefore()->dateTime()->diff($refdt)->invert) { |
|
| 306 | + throw new PathValidationException( |
|
| 307 | + 'Certificate validity period has not started.'); |
|
| 308 | + } |
|
| 309 | + if ($refdt->diff($validity->notAfter()->dateTime())->invert) { |
|
| 310 | + throw new PathValidationException('Certificate has expired.'); |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * Check certificate revocation. |
|
| 316 | - * |
|
| 317 | - * @param Certificate $cert |
|
| 318 | - */ |
|
| 319 | - private function _checkRevocation(Certificate $cert) |
|
| 320 | - { |
|
| 321 | - // @todo Implement CRL handling |
|
| 322 | - } |
|
| 314 | + /** |
|
| 315 | + * Check certificate revocation. |
|
| 316 | + * |
|
| 317 | + * @param Certificate $cert |
|
| 318 | + */ |
|
| 319 | + private function _checkRevocation(Certificate $cert) |
|
| 320 | + { |
|
| 321 | + // @todo Implement CRL handling |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - /** |
|
| 325 | - * Check certificate issuer. |
|
| 326 | - * |
|
| 327 | - * @param ValidatorState $state |
|
| 328 | - * @param Certificate $cert |
|
| 329 | - * |
|
| 330 | - * @throws PathValidationException |
|
| 331 | - */ |
|
| 332 | - private function _checkIssuer(ValidatorState $state, Certificate $cert): void |
|
| 333 | - { |
|
| 334 | - if (!$cert->tbsCertificate()->issuer()->equals($state->workingIssuerName())) { |
|
| 335 | - throw new PathValidationException('Certification issuer mismatch.'); |
|
| 336 | - } |
|
| 337 | - } |
|
| 324 | + /** |
|
| 325 | + * Check certificate issuer. |
|
| 326 | + * |
|
| 327 | + * @param ValidatorState $state |
|
| 328 | + * @param Certificate $cert |
|
| 329 | + * |
|
| 330 | + * @throws PathValidationException |
|
| 331 | + */ |
|
| 332 | + private function _checkIssuer(ValidatorState $state, Certificate $cert): void |
|
| 333 | + { |
|
| 334 | + if (!$cert->tbsCertificate()->issuer()->equals($state->workingIssuerName())) { |
|
| 335 | + throw new PathValidationException('Certification issuer mismatch.'); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * @param ValidatorState $state |
|
| 341 | - * @param Certificate $cert |
|
| 342 | - */ |
|
| 343 | - private function _checkPermittedSubtrees(ValidatorState $state, Certificate $cert) |
|
| 344 | - { |
|
| 345 | - // @todo Implement |
|
| 346 | - $state->permittedSubtrees(); |
|
| 347 | - } |
|
| 339 | + /** |
|
| 340 | + * @param ValidatorState $state |
|
| 341 | + * @param Certificate $cert |
|
| 342 | + */ |
|
| 343 | + private function _checkPermittedSubtrees(ValidatorState $state, Certificate $cert) |
|
| 344 | + { |
|
| 345 | + // @todo Implement |
|
| 346 | + $state->permittedSubtrees(); |
|
| 347 | + } |
|
| 348 | 348 | |
| 349 | - /** |
|
| 350 | - * @param ValidatorState $state |
|
| 351 | - * @param Certificate $cert |
|
| 352 | - */ |
|
| 353 | - private function _checkExcludedSubtrees(ValidatorState $state, Certificate $cert) |
|
| 354 | - { |
|
| 355 | - // @todo Implement |
|
| 356 | - $state->excludedSubtrees(); |
|
| 357 | - } |
|
| 349 | + /** |
|
| 350 | + * @param ValidatorState $state |
|
| 351 | + * @param Certificate $cert |
|
| 352 | + */ |
|
| 353 | + private function _checkExcludedSubtrees(ValidatorState $state, Certificate $cert) |
|
| 354 | + { |
|
| 355 | + // @todo Implement |
|
| 356 | + $state->excludedSubtrees(); |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - /** |
|
| 360 | - * Apply policy mappings handling for the preparation step. |
|
| 361 | - * |
|
| 362 | - * @param ValidatorState $state |
|
| 363 | - * @param Certificate $cert |
|
| 364 | - * |
|
| 365 | - * @throws PathValidationException |
|
| 366 | - * |
|
| 367 | - * @return ValidatorState |
|
| 368 | - */ |
|
| 369 | - private function _preparePolicyMappings(ValidatorState $state, |
|
| 370 | - Certificate $cert): ValidatorState |
|
| 371 | - { |
|
| 372 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 373 | - if ($extensions->hasPolicyMappings()) { |
|
| 374 | - // (a) verify that anyPolicy mapping is not used |
|
| 375 | - if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
| 376 | - throw new PathValidationException('anyPolicy mapping found.'); |
|
| 377 | - } |
|
| 378 | - // (b) process policy mappings |
|
| 379 | - if ($state->hasValidPolicyTree()) { |
|
| 380 | - $state = $state->validPolicyTree()->processMappings($state, $cert); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - return $state; |
|
| 384 | - } |
|
| 359 | + /** |
|
| 360 | + * Apply policy mappings handling for the preparation step. |
|
| 361 | + * |
|
| 362 | + * @param ValidatorState $state |
|
| 363 | + * @param Certificate $cert |
|
| 364 | + * |
|
| 365 | + * @throws PathValidationException |
|
| 366 | + * |
|
| 367 | + * @return ValidatorState |
|
| 368 | + */ |
|
| 369 | + private function _preparePolicyMappings(ValidatorState $state, |
|
| 370 | + Certificate $cert): ValidatorState |
|
| 371 | + { |
|
| 372 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 373 | + if ($extensions->hasPolicyMappings()) { |
|
| 374 | + // (a) verify that anyPolicy mapping is not used |
|
| 375 | + if ($extensions->policyMappings()->hasAnyPolicyMapping()) { |
|
| 376 | + throw new PathValidationException('anyPolicy mapping found.'); |
|
| 377 | + } |
|
| 378 | + // (b) process policy mappings |
|
| 379 | + if ($state->hasValidPolicyTree()) { |
|
| 380 | + $state = $state->validPolicyTree()->processMappings($state, $cert); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + return $state; |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | - /** |
|
| 387 | - * Apply name constraints handling for the preparation step. |
|
| 388 | - * |
|
| 389 | - * @param ValidatorState $state |
|
| 390 | - * @param Certificate $cert |
|
| 391 | - * |
|
| 392 | - * @return ValidatorState |
|
| 393 | - */ |
|
| 394 | - private function _prepareNameConstraints(ValidatorState $state, |
|
| 395 | - Certificate $cert): ValidatorState |
|
| 396 | - { |
|
| 397 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 398 | - if ($extensions->hasNameConstraints()) { |
|
| 399 | - $state = $this->_processNameConstraints($state, $cert); |
|
| 400 | - } |
|
| 401 | - return $state; |
|
| 402 | - } |
|
| 386 | + /** |
|
| 387 | + * Apply name constraints handling for the preparation step. |
|
| 388 | + * |
|
| 389 | + * @param ValidatorState $state |
|
| 390 | + * @param Certificate $cert |
|
| 391 | + * |
|
| 392 | + * @return ValidatorState |
|
| 393 | + */ |
|
| 394 | + private function _prepareNameConstraints(ValidatorState $state, |
|
| 395 | + Certificate $cert): ValidatorState |
|
| 396 | + { |
|
| 397 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 398 | + if ($extensions->hasNameConstraints()) { |
|
| 399 | + $state = $this->_processNameConstraints($state, $cert); |
|
| 400 | + } |
|
| 401 | + return $state; |
|
| 402 | + } |
|
| 403 | 403 | |
| 404 | - /** |
|
| 405 | - * Apply preparation for a non-self-signed certificate. |
|
| 406 | - * |
|
| 407 | - * @param ValidatorState $state |
|
| 408 | - * |
|
| 409 | - * @return ValidatorState |
|
| 410 | - */ |
|
| 411 | - private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
| 412 | - { |
|
| 413 | - // (h.1) |
|
| 414 | - if ($state->explicitPolicy() > 0) { |
|
| 415 | - $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 416 | - } |
|
| 417 | - // (h.2) |
|
| 418 | - if ($state->policyMapping() > 0) { |
|
| 419 | - $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
| 420 | - } |
|
| 421 | - // (h.3) |
|
| 422 | - if ($state->inhibitAnyPolicy() > 0) { |
|
| 423 | - $state = $state->withInhibitAnyPolicy($state->inhibitAnyPolicy() - 1); |
|
| 424 | - } |
|
| 425 | - return $state; |
|
| 426 | - } |
|
| 404 | + /** |
|
| 405 | + * Apply preparation for a non-self-signed certificate. |
|
| 406 | + * |
|
| 407 | + * @param ValidatorState $state |
|
| 408 | + * |
|
| 409 | + * @return ValidatorState |
|
| 410 | + */ |
|
| 411 | + private function _prepareNonSelfIssued(ValidatorState $state): ValidatorState |
|
| 412 | + { |
|
| 413 | + // (h.1) |
|
| 414 | + if ($state->explicitPolicy() > 0) { |
|
| 415 | + $state = $state->withExplicitPolicy($state->explicitPolicy() - 1); |
|
| 416 | + } |
|
| 417 | + // (h.2) |
|
| 418 | + if ($state->policyMapping() > 0) { |
|
| 419 | + $state = $state->withPolicyMapping($state->policyMapping() - 1); |
|
| 420 | + } |
|
| 421 | + // (h.3) |
|
| 422 | + if ($state->inhibitAnyPolicy() > 0) { |
|
| 423 | + $state = $state->withInhibitAnyPolicy($state->inhibitAnyPolicy() - 1); |
|
| 424 | + } |
|
| 425 | + return $state; |
|
| 426 | + } |
|
| 427 | 427 | |
| 428 | - /** |
|
| 429 | - * Apply policy constraints handling for the preparation step. |
|
| 430 | - * |
|
| 431 | - * @param ValidatorState $state |
|
| 432 | - * @param Certificate $cert |
|
| 433 | - * |
|
| 434 | - * @return ValidatorState |
|
| 435 | - */ |
|
| 436 | - private function _preparePolicyConstraints(ValidatorState $state, |
|
| 437 | - Certificate $cert): ValidatorState |
|
| 438 | - { |
|
| 439 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 440 | - if (!$extensions->hasPolicyConstraints()) { |
|
| 441 | - return $state; |
|
| 442 | - } |
|
| 443 | - $ext = $extensions->policyConstraints(); |
|
| 444 | - // (i.1) |
|
| 445 | - if ($ext->hasRequireExplicitPolicy() && |
|
| 446 | - $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
| 447 | - $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
| 448 | - } |
|
| 449 | - // (i.2) |
|
| 450 | - if ($ext->hasInhibitPolicyMapping() && |
|
| 451 | - $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
| 452 | - $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
| 453 | - } |
|
| 454 | - return $state; |
|
| 455 | - } |
|
| 428 | + /** |
|
| 429 | + * Apply policy constraints handling for the preparation step. |
|
| 430 | + * |
|
| 431 | + * @param ValidatorState $state |
|
| 432 | + * @param Certificate $cert |
|
| 433 | + * |
|
| 434 | + * @return ValidatorState |
|
| 435 | + */ |
|
| 436 | + private function _preparePolicyConstraints(ValidatorState $state, |
|
| 437 | + Certificate $cert): ValidatorState |
|
| 438 | + { |
|
| 439 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 440 | + if (!$extensions->hasPolicyConstraints()) { |
|
| 441 | + return $state; |
|
| 442 | + } |
|
| 443 | + $ext = $extensions->policyConstraints(); |
|
| 444 | + // (i.1) |
|
| 445 | + if ($ext->hasRequireExplicitPolicy() && |
|
| 446 | + $ext->requireExplicitPolicy() < $state->explicitPolicy()) { |
|
| 447 | + $state = $state->withExplicitPolicy($ext->requireExplicitPolicy()); |
|
| 448 | + } |
|
| 449 | + // (i.2) |
|
| 450 | + if ($ext->hasInhibitPolicyMapping() && |
|
| 451 | + $ext->inhibitPolicyMapping() < $state->policyMapping()) { |
|
| 452 | + $state = $state->withPolicyMapping($ext->inhibitPolicyMapping()); |
|
| 453 | + } |
|
| 454 | + return $state; |
|
| 455 | + } |
|
| 456 | 456 | |
| 457 | - /** |
|
| 458 | - * Apply inhibit any-policy handling for the preparation step. |
|
| 459 | - * |
|
| 460 | - * @param ValidatorState $state |
|
| 461 | - * @param Certificate $cert |
|
| 462 | - * |
|
| 463 | - * @return ValidatorState |
|
| 464 | - */ |
|
| 465 | - private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
| 466 | - Certificate $cert): ValidatorState |
|
| 467 | - { |
|
| 468 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 469 | - if ($extensions->hasInhibitAnyPolicy()) { |
|
| 470 | - $ext = $extensions->inhibitAnyPolicy(); |
|
| 471 | - if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
| 472 | - $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - return $state; |
|
| 476 | - } |
|
| 457 | + /** |
|
| 458 | + * Apply inhibit any-policy handling for the preparation step. |
|
| 459 | + * |
|
| 460 | + * @param ValidatorState $state |
|
| 461 | + * @param Certificate $cert |
|
| 462 | + * |
|
| 463 | + * @return ValidatorState |
|
| 464 | + */ |
|
| 465 | + private function _prepareInhibitAnyPolicy(ValidatorState $state, |
|
| 466 | + Certificate $cert): ValidatorState |
|
| 467 | + { |
|
| 468 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 469 | + if ($extensions->hasInhibitAnyPolicy()) { |
|
| 470 | + $ext = $extensions->inhibitAnyPolicy(); |
|
| 471 | + if ($ext->skipCerts() < $state->inhibitAnyPolicy()) { |
|
| 472 | + $state = $state->withInhibitAnyPolicy($ext->skipCerts()); |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + return $state; |
|
| 476 | + } |
|
| 477 | 477 | |
| 478 | - /** |
|
| 479 | - * Verify maximum certification path length for the preparation step. |
|
| 480 | - * |
|
| 481 | - * @param ValidatorState $state |
|
| 482 | - * @param Certificate $cert |
|
| 483 | - * |
|
| 484 | - * @throws PathValidationException |
|
| 485 | - * |
|
| 486 | - * @return ValidatorState |
|
| 487 | - */ |
|
| 488 | - private function _verifyMaxPathLength(ValidatorState $state, |
|
| 489 | - Certificate $cert): ValidatorState |
|
| 490 | - { |
|
| 491 | - if (!$cert->isSelfIssued()) { |
|
| 492 | - if ($state->maxPathLength() <= 0) { |
|
| 493 | - throw new PathValidationException( |
|
| 494 | - 'Certification path length exceeded.'); |
|
| 495 | - } |
|
| 496 | - $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
| 497 | - } |
|
| 498 | - return $state; |
|
| 499 | - } |
|
| 478 | + /** |
|
| 479 | + * Verify maximum certification path length for the preparation step. |
|
| 480 | + * |
|
| 481 | + * @param ValidatorState $state |
|
| 482 | + * @param Certificate $cert |
|
| 483 | + * |
|
| 484 | + * @throws PathValidationException |
|
| 485 | + * |
|
| 486 | + * @return ValidatorState |
|
| 487 | + */ |
|
| 488 | + private function _verifyMaxPathLength(ValidatorState $state, |
|
| 489 | + Certificate $cert): ValidatorState |
|
| 490 | + { |
|
| 491 | + if (!$cert->isSelfIssued()) { |
|
| 492 | + if ($state->maxPathLength() <= 0) { |
|
| 493 | + throw new PathValidationException( |
|
| 494 | + 'Certification path length exceeded.'); |
|
| 495 | + } |
|
| 496 | + $state = $state->withMaxPathLength($state->maxPathLength() - 1); |
|
| 497 | + } |
|
| 498 | + return $state; |
|
| 499 | + } |
|
| 500 | 500 | |
| 501 | - /** |
|
| 502 | - * Check key usage extension for the preparation step. |
|
| 503 | - * |
|
| 504 | - * @param Certificate $cert |
|
| 505 | - * |
|
| 506 | - * @throws PathValidationException |
|
| 507 | - */ |
|
| 508 | - private function _checkKeyUsage(Certificate $cert): void |
|
| 509 | - { |
|
| 510 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 511 | - if ($extensions->hasKeyUsage()) { |
|
| 512 | - $ext = $extensions->keyUsage(); |
|
| 513 | - if (!$ext->isKeyCertSign()) { |
|
| 514 | - throw new PathValidationException('keyCertSign usage not set.'); |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - } |
|
| 501 | + /** |
|
| 502 | + * Check key usage extension for the preparation step. |
|
| 503 | + * |
|
| 504 | + * @param Certificate $cert |
|
| 505 | + * |
|
| 506 | + * @throws PathValidationException |
|
| 507 | + */ |
|
| 508 | + private function _checkKeyUsage(Certificate $cert): void |
|
| 509 | + { |
|
| 510 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 511 | + if ($extensions->hasKeyUsage()) { |
|
| 512 | + $ext = $extensions->keyUsage(); |
|
| 513 | + if (!$ext->isKeyCertSign()) { |
|
| 514 | + throw new PathValidationException('keyCertSign usage not set.'); |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - /** |
|
| 520 | - * @param ValidatorState $state |
|
| 521 | - * @param Certificate $cert |
|
| 522 | - * |
|
| 523 | - * @return ValidatorState |
|
| 524 | - */ |
|
| 525 | - private function _processNameConstraints(ValidatorState $state, |
|
| 526 | - Certificate $cert): ValidatorState |
|
| 527 | - { |
|
| 528 | - // @todo Implement |
|
| 529 | - return $state; |
|
| 530 | - } |
|
| 519 | + /** |
|
| 520 | + * @param ValidatorState $state |
|
| 521 | + * @param Certificate $cert |
|
| 522 | + * |
|
| 523 | + * @return ValidatorState |
|
| 524 | + */ |
|
| 525 | + private function _processNameConstraints(ValidatorState $state, |
|
| 526 | + Certificate $cert): ValidatorState |
|
| 527 | + { |
|
| 528 | + // @todo Implement |
|
| 529 | + return $state; |
|
| 530 | + } |
|
| 531 | 531 | |
| 532 | - /** |
|
| 533 | - * Process basic constraints extension. |
|
| 534 | - * |
|
| 535 | - * @param Certificate $cert |
|
| 536 | - * |
|
| 537 | - * @throws PathValidationException |
|
| 538 | - */ |
|
| 539 | - private function _processBasicContraints(Certificate $cert): void |
|
| 540 | - { |
|
| 541 | - if (TBSCertificate::VERSION_3 === $cert->tbsCertificate()->version()) { |
|
| 542 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 543 | - if (!$extensions->hasBasicConstraints()) { |
|
| 544 | - throw new PathValidationException( |
|
| 545 | - 'v3 certificate must have basicConstraints extension.'); |
|
| 546 | - } |
|
| 547 | - // verify that cA is set to TRUE |
|
| 548 | - if (!$extensions->basicConstraints()->isCA()) { |
|
| 549 | - throw new PathValidationException( |
|
| 550 | - 'Certificate is not a CA certificate.'); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - } |
|
| 532 | + /** |
|
| 533 | + * Process basic constraints extension. |
|
| 534 | + * |
|
| 535 | + * @param Certificate $cert |
|
| 536 | + * |
|
| 537 | + * @throws PathValidationException |
|
| 538 | + */ |
|
| 539 | + private function _processBasicContraints(Certificate $cert): void |
|
| 540 | + { |
|
| 541 | + if (TBSCertificate::VERSION_3 === $cert->tbsCertificate()->version()) { |
|
| 542 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 543 | + if (!$extensions->hasBasicConstraints()) { |
|
| 544 | + throw new PathValidationException( |
|
| 545 | + 'v3 certificate must have basicConstraints extension.'); |
|
| 546 | + } |
|
| 547 | + // verify that cA is set to TRUE |
|
| 548 | + if (!$extensions->basicConstraints()->isCA()) { |
|
| 549 | + throw new PathValidationException( |
|
| 550 | + 'Certificate is not a CA certificate.'); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + } |
|
| 554 | 554 | |
| 555 | - /** |
|
| 556 | - * Process pathLenConstraint. |
|
| 557 | - * |
|
| 558 | - * @param ValidatorState $state |
|
| 559 | - * @param Certificate $cert |
|
| 560 | - * |
|
| 561 | - * @return ValidatorState |
|
| 562 | - */ |
|
| 563 | - private function _processPathLengthContraint(ValidatorState $state, |
|
| 564 | - Certificate $cert): ValidatorState |
|
| 565 | - { |
|
| 566 | - $extensions = $cert->tbsCertificate()->extensions(); |
|
| 567 | - if ($extensions->hasBasicConstraints()) { |
|
| 568 | - $ext = $extensions->basicConstraints(); |
|
| 569 | - if ($ext->hasPathLen()) { |
|
| 570 | - if ($ext->pathLen() < $state->maxPathLength()) { |
|
| 571 | - $state = $state->withMaxPathLength($ext->pathLen()); |
|
| 572 | - } |
|
| 573 | - } |
|
| 574 | - } |
|
| 575 | - return $state; |
|
| 576 | - } |
|
| 555 | + /** |
|
| 556 | + * Process pathLenConstraint. |
|
| 557 | + * |
|
| 558 | + * @param ValidatorState $state |
|
| 559 | + * @param Certificate $cert |
|
| 560 | + * |
|
| 561 | + * @return ValidatorState |
|
| 562 | + */ |
|
| 563 | + private function _processPathLengthContraint(ValidatorState $state, |
|
| 564 | + Certificate $cert): ValidatorState |
|
| 565 | + { |
|
| 566 | + $extensions = $cert->tbsCertificate()->extensions(); |
|
| 567 | + if ($extensions->hasBasicConstraints()) { |
|
| 568 | + $ext = $extensions->basicConstraints(); |
|
| 569 | + if ($ext->hasPathLen()) { |
|
| 570 | + if ($ext->pathLen() < $state->maxPathLength()) { |
|
| 571 | + $state = $state->withMaxPathLength($ext->pathLen()); |
|
| 572 | + } |
|
| 573 | + } |
|
| 574 | + } |
|
| 575 | + return $state; |
|
| 576 | + } |
|
| 577 | 577 | |
| 578 | - /** |
|
| 579 | - * @param ValidatorState $state |
|
| 580 | - * @param Certificate $cert |
|
| 581 | - * |
|
| 582 | - * @return ValidatorState |
|
| 583 | - */ |
|
| 584 | - private function _processExtensions(ValidatorState $state, |
|
| 585 | - Certificate $cert): ValidatorState |
|
| 586 | - { |
|
| 587 | - // @todo Implement |
|
| 588 | - return $state; |
|
| 589 | - } |
|
| 578 | + /** |
|
| 579 | + * @param ValidatorState $state |
|
| 580 | + * @param Certificate $cert |
|
| 581 | + * |
|
| 582 | + * @return ValidatorState |
|
| 583 | + */ |
|
| 584 | + private function _processExtensions(ValidatorState $state, |
|
| 585 | + Certificate $cert): ValidatorState |
|
| 586 | + { |
|
| 587 | + // @todo Implement |
|
| 588 | + return $state; |
|
| 589 | + } |
|
| 590 | 590 | |
| 591 | - /** |
|
| 592 | - * @param ValidatorState $state |
|
| 593 | - * |
|
| 594 | - * @return ValidatorState |
|
| 595 | - */ |
|
| 596 | - private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
| 597 | - { |
|
| 598 | - // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
| 599 | - if (!$state->hasValidPolicyTree()) { |
|
| 600 | - return $state; |
|
| 601 | - } |
|
| 602 | - // (ii) If the valid_policy_tree is not NULL and |
|
| 603 | - // the user-initial-policy-set is any-policy, the intersection |
|
| 604 | - // is the entire valid_policy_tree |
|
| 605 | - $initial_policies = $this->_config->policySet(); |
|
| 606 | - if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
| 607 | - return $state; |
|
| 608 | - } |
|
| 609 | - // (iii) If the valid_policy_tree is not NULL and the |
|
| 610 | - // user-initial-policy-set is not any-policy, calculate |
|
| 611 | - // the intersection of the valid_policy_tree and the |
|
| 612 | - // user-initial-policy-set as follows |
|
| 613 | - return $state->validPolicyTree()->calculateIntersection($state, $initial_policies); |
|
| 614 | - } |
|
| 591 | + /** |
|
| 592 | + * @param ValidatorState $state |
|
| 593 | + * |
|
| 594 | + * @return ValidatorState |
|
| 595 | + */ |
|
| 596 | + private function _calculatePolicyIntersection(ValidatorState $state): ValidatorState |
|
| 597 | + { |
|
| 598 | + // (i) If the valid_policy_tree is NULL, the intersection is NULL |
|
| 599 | + if (!$state->hasValidPolicyTree()) { |
|
| 600 | + return $state; |
|
| 601 | + } |
|
| 602 | + // (ii) If the valid_policy_tree is not NULL and |
|
| 603 | + // the user-initial-policy-set is any-policy, the intersection |
|
| 604 | + // is the entire valid_policy_tree |
|
| 605 | + $initial_policies = $this->_config->policySet(); |
|
| 606 | + if (in_array(PolicyInformation::OID_ANY_POLICY, $initial_policies)) { |
|
| 607 | + return $state; |
|
| 608 | + } |
|
| 609 | + // (iii) If the valid_policy_tree is not NULL and the |
|
| 610 | + // user-initial-policy-set is not any-policy, calculate |
|
| 611 | + // the intersection of the valid_policy_tree and the |
|
| 612 | + // user-initial-policy-set as follows |
|
| 613 | + return $state->validPolicyTree()->calculateIntersection($state, $initial_policies); |
|
| 614 | + } |
|
| 615 | 615 | } |