| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\AttributeCertificate; |
| 6 |
|
| 7 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 8 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 9 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 10 |
use SpomkyLabs\Pki\CryptoEncoding\PEM; |
| 11 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
| 12 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier; |
| 13 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo; |
| 14 |
use SpomkyLabs\Pki\CryptoTypes\Signature\Signature; |
| 15 |
use SpomkyLabs\Pki\X509\Certificate\Certificate; |
| 16 |
use Stringable; |
| 17 |
use UnexpectedValueException; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class AttributeCertificate implements Stringable |
| 25 |
{ |
| 26 |
private function __construct( |
| 27 |
private readonly AttributeCertificateInfo $acInfo, |
| 28 |
private readonly SignatureAlgorithmIdentifier $signatureAlgorithm, |
| 29 |
private readonly Signature $signatureValue |
| 30 |
) { |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function __toString(): string |
| 37 |
{ |
| 38 |
return $this->toPEM() |
| 39 |
->string(); |
| 40 |
} |
| 41 |
|
| 42 |
public static function create( |
| 43 |
AttributeCertificateInfo $acInfo, |
| 44 |
SignatureAlgorithmIdentifier $signatureAlgorithm, |
| 45 |
Signature $signatureValue |
| 46 |
): self { |
| 47 |
return new self($acInfo, $signatureAlgorithm, $signatureValue); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static function fromASN1(Sequence $seq): self |
| 54 |
{ |
| 55 |
$acinfo = AttributeCertificateInfo::fromASN1($seq->at(0)->asSequence()); |
| 56 |
$algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
| 57 |
if (! $algo instanceof SignatureAlgorithmIdentifier) { |
| 58 |
throw new UnexpectedValueException('Unsupported signature algorithm ' . $algo->oid() . '.'); |
| 59 |
} |
| 60 |
$signature = Signature::fromSignatureData($seq->at(2)->asBitString()->string(), $algo); |
| 61 |
return self::create($acinfo, $algo, $signature); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static function fromDER(string $data): self |
| 68 |
{ |
| 69 |
return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public static function fromPEM(PEM $pem): self |
| 76 |
{ |
| 77 |
if ($pem->type() !== PEM::TYPE_ATTRIBUTE_CERTIFICATE) { |
| 78 |
throw new UnexpectedValueException('Invalid PEM type.'); |
| 79 |
} |
| 80 |
return self::fromDER($pem->data()); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function acinfo(): AttributeCertificateInfo |
| 87 |
{ |
| 88 |
return $this->acInfo; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
| 95 |
{ |
| 96 |
return $this->signatureAlgorithm; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function signatureValue(): Signature |
| 103 |
{ |
| 104 |
return $this->signatureValue; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function toASN1(): Sequence |
| 111 |
{ |
| 112 |
return Sequence::create( |
| 113 |
$this->acInfo->toASN1(), |
| 114 |
$this->signatureAlgorithm->toASN1(), |
| 115 |
$this->signatureValue->bitString() |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public function toDER(): string |
| 123 |
{ |
| 124 |
return $this->toASN1() |
| 125 |
->toDER(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function toPEM(): PEM |
| 132 |
{ |
| 133 |
return PEM::create(PEM::TYPE_ATTRIBUTE_CERTIFICATE, $this->toDER()); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function isHeldBy(Certificate $cert): bool |
| 142 |
{ |
| 143 |
if (! $this->acInfo->holder()->identifiesPKC($cert)) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function isIssuedBy(Certificate $cert): bool |
| 155 |
{ |
| 156 |
if (! $this->acInfo->issuer()->identifiesPKC($cert)) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function verify(PublicKeyInfo $pubkey_info, ?Crypto $crypto = null): bool |
| 169 |
{ |
| 170 |
$crypto ??= Crypto::getDefault(); |
| 171 |
$data = $this->acInfo->toASN1() |
| 172 |
->toDER(); |
| 173 |
return $crypto->verify($data, $this->signatureValue, $pubkey_info, $this->signatureAlgorithm); |
| 174 |
} |
| 175 |
} |