| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\AttributeCertificate\Validation; |
| 6 |
|
| 7 |
use function count; |
| 8 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 9 |
use SpomkyLabs\Pki\X509\AttributeCertificate\AttributeCertificate; |
| 10 |
use SpomkyLabs\Pki\X509\AttributeCertificate\Validation\Exception\ACValidationException; |
| 11 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 12 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Extension; |
| 13 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Target\Targets; |
| 14 |
use SpomkyLabs\Pki\X509\Certificate\Extension\TargetInformationExtension; |
| 15 |
use SpomkyLabs\Pki\X509\CertificationPath\Exception\PathValidationException; |
| 16 |
use SpomkyLabs\Pki\X509\CertificationPath\PathValidation\PathValidationConfig; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class ACValidator |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
private readonly Crypto $crypto; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private function __construct( |
| 36 |
private readonly AttributeCertificate $ac, |
| 37 |
private readonly ACValidationConfig $config, |
| 38 |
?Crypto $crypto |
| 39 |
) { |
| 40 |
$this->crypto = $crypto ?? Crypto::getDefault(); |
| 41 |
} |
| 42 |
|
| 43 |
public static function create( |
| 44 |
AttributeCertificate $ac, |
| 45 |
ACValidationConfig $config, |
| 46 |
?Crypto $crypto = null |
| 47 |
): self { |
| 48 |
return new self($ac, $config, $crypto); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function validate(): AttributeCertificate |
| 57 |
{ |
| 58 |
$this->validateHolder(); |
| 59 |
$issuer = $this->verifyIssuer(); |
| 60 |
$this->validateIssuerProfile($issuer); |
| 61 |
$this->validateTime(); |
| 62 |
$this->validateTargeting(); |
| 63 |
return $this->ac; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private function validateHolder(): Certificate |
| 72 |
{ |
| 73 |
$path = $this->config->holderPath(); |
| 74 |
$config = PathValidationConfig::defaultConfig() |
| 75 |
->withMaxLength(count($path)) |
| 76 |
->withDateTime($this->config->evaluationTime()); |
| 77 |
try { |
| 78 |
$holder = $path->validate($config, $this->crypto) |
| 79 |
->certificate(); |
| 80 |
} catch (PathValidationException $e) { |
| 81 |
throw new ACValidationException("Failed to validate holder PKC's certification path.", 0, $e); |
| 82 |
} |
| 83 |
if (! $this->ac->isHeldBy($holder)) { |
| 84 |
throw new ACValidationException("Name mismatch of AC's holder PKC."); |
| 85 |
} |
| 86 |
return $holder; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private function verifyIssuer(): Certificate |
| 95 |
{ |
| 96 |
$path = $this->config->issuerPath(); |
| 97 |
$config = PathValidationConfig::defaultConfig() |
| 98 |
->withMaxLength(count($path)) |
| 99 |
->withDateTime($this->config->evaluationTime()); |
| 100 |
try { |
| 101 |
$issuer = $path->validate($config, $this->crypto) |
| 102 |
->certificate(); |
| 103 |
} catch (PathValidationException $e) { |
| 104 |
throw new ACValidationException("Failed to validate issuer PKC's certification path.", 0, $e); |
| 105 |
} |
| 106 |
if (! $this->ac->isIssuedBy($issuer)) { |
| 107 |
throw new ACValidationException("Name mismatch of AC's issuer PKC."); |
| 108 |
} |
| 109 |
$pubkey_info = $issuer->tbsCertificate() |
| 110 |
->subjectPublicKeyInfo(); |
| 111 |
if (! $this->ac->verify($pubkey_info, $this->crypto)) { |
| 112 |
throw new ACValidationException('Failed to verify signature.'); |
| 113 |
} |
| 114 |
return $issuer; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
private function validateIssuerProfile(Certificate $cert): void |
| 123 |
{ |
| 124 |
$exts = $cert->tbsCertificate() |
| 125 |
->extensions(); |
| 126 |
if ($exts->hasKeyUsage() && ! $exts->keyUsage()->isDigitalSignature()) { |
| 127 |
throw new ACValidationException( |
| 128 |
"Issuer PKC's Key Usage extension doesn't permit" . |
| 129 |
' verification of digital signatures.' |
| 130 |
); |
| 131 |
} |
| 132 |
if ($exts->hasBasicConstraints() && $exts->basicConstraints()->isCA()) { |
| 133 |
throw new ACValidationException('Issuer PKC must not be a CA.'); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function validateTime(): void |
| 141 |
{ |
| 142 |
$t = $this->config->evaluationTime(); |
| 143 |
$validity = $this->ac->acinfo() |
| 144 |
->validityPeriod(); |
| 145 |
if ($validity->notBeforeTime()->diff($t)->invert === 1) { |
| 146 |
throw new ACValidationException('Validity period has not started.'); |
| 147 |
} |
| 148 |
if ($t->diff($validity->notAfterTime())->invert === 1) { |
| 149 |
throw new ACValidationException('Attribute certificate has expired.'); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private function validateTargeting(): void |
| 157 |
{ |
| 158 |
$exts = $this->ac->acinfo() |
| 159 |
->extensions(); |
| 160 |
|
| 161 |
if (! $exts->has(Extension::OID_TARGET_INFORMATION)) { |
| 162 |
return; |
| 163 |
} |
| 164 |
$ext = $exts->get(Extension::OID_TARGET_INFORMATION); |
| 165 |
if ($ext instanceof TargetInformationExtension && |
| 166 |
! $this->_hasMatchingTarget($ext->targets())) { |
| 167 |
throw new ACValidationException("Attribute certificate doesn't have a matching target."); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private function _hasMatchingTarget(Targets $targets): bool |
| 177 |
{ |
| 178 |
foreach ($this->config->targets() as $target) { |
| 179 |
if ($targets->hasTarget($target)) { |
| 180 |
return true; |
| 181 |
} |
| 182 |
} |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |