| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\CertificationPath\PathValidation; |
| 6 |
|
| 7 |
use DateTimeImmutable; |
| 8 |
use LogicException; |
| 9 |
use Psr\Clock\ClockInterface; |
| 10 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 11 |
use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
final class PathValidationConfig |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private array $policySet; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private ?Certificate $trustAnchor = null; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private bool $policyMappingInhibit; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private bool $explicitPolicy; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private bool $anyPolicyInhibit; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private function __construct( |
| 58 |
private DateTimeImmutable $dateTime, |
| 59 |
private int $maxLength |
| 60 |
) { |
| 61 |
$this->policySet = [PolicyInformation::OID_ANY_POLICY]; |
| 62 |
$this->policyMappingInhibit = false; |
| 63 |
$this->explicitPolicy = false; |
| 64 |
$this->anyPolicyInhibit = false; |
| 65 |
} |
| 66 |
|
| 67 |
public static function create(DateTimeImmutable $dateTime, int $maxLength): self |
| 68 |
{ |
| 69 |
return new self($dateTime, $maxLength); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function defaultConfig(?ClockInterface $clock = null): self |
| 76 |
{ |
| 77 |
$dateTime = $clock !== null ? $clock->now() : new DateTimeImmutable(); |
| 78 |
return self::create($dateTime, 3); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function withMaxLength(int $length): self |
| 85 |
{ |
| 86 |
$obj = clone $this; |
| 87 |
$obj->maxLength = $length; |
| 88 |
return $obj; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function withDateTime(DateTimeImmutable $dt): self |
| 95 |
{ |
| 96 |
$obj = clone $this; |
| 97 |
$obj->dateTime = $dt; |
| 98 |
return $obj; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function withTrustAnchor(Certificate $ca): self |
| 105 |
{ |
| 106 |
$obj = clone $this; |
| 107 |
$obj->trustAnchor = $ca; |
| 108 |
return $obj; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function withPolicyMappingInhibit(bool $flag): self |
| 115 |
{ |
| 116 |
$obj = clone $this; |
| 117 |
$obj->policyMappingInhibit = $flag; |
| 118 |
return $obj; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function withExplicitPolicy(bool $flag): self |
| 125 |
{ |
| 126 |
$obj = clone $this; |
| 127 |
$obj->explicitPolicy = $flag; |
| 128 |
return $obj; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function withAnyPolicyInhibit(bool $flag): self |
| 135 |
{ |
| 136 |
$obj = clone $this; |
| 137 |
$obj->anyPolicyInhibit = $flag; |
| 138 |
return $obj; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function withPolicySet(string ...$policies): self |
| 147 |
{ |
| 148 |
$obj = clone $this; |
| 149 |
$obj->policySet = $policies; |
| 150 |
return $obj; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function maxLength(): int |
| 157 |
{ |
| 158 |
return $this->maxLength; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function dateTime(): DateTimeImmutable |
| 165 |
{ |
| 166 |
return $this->dateTime; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function policySet(): array |
| 175 |
{ |
| 176 |
return $this->policySet; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function hasTrustAnchor(): bool |
| 183 |
{ |
| 184 |
return isset($this->trustAnchor); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function trustAnchor(): Certificate |
| 191 |
{ |
| 192 |
if (! $this->hasTrustAnchor()) { |
| 193 |
throw new LogicException('No trust anchor.'); |
| 194 |
} |
| 195 |
return $this->trustAnchor; |
| 196 |
} |
| 197 |
|
| 198 |
public function policyMappingInhibit(): bool |
| 199 |
{ |
| 200 |
return $this->policyMappingInhibit; |
| 201 |
} |
| 202 |
|
| 203 |
public function explicitPolicy(): bool |
| 204 |
{ |
| 205 |
return $this->explicitPolicy; |
| 206 |
} |
| 207 |
|
| 208 |
public function anyPolicyInhibit(): bool |
| 209 |
{ |
| 210 |
return $this->anyPolicyInhibit; |
| 211 |
} |
| 212 |
} |