| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate; |
| 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 Stringable; |
| 16 |
use UnexpectedValueException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
final class Certificate implements Stringable |
| 24 |
{ |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
private function __construct( |
| 31 |
private readonly TBSCertificate $tbsCertificate, |
| 32 |
private readonly SignatureAlgorithmIdentifier $signatureAlgorithm, |
| 33 |
private readonly Signature $signatureValue |
| 34 |
) { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function __toString(): string |
| 41 |
{ |
| 42 |
return $this->toPEM() |
| 43 |
->string(); |
| 44 |
} |
| 45 |
|
| 46 |
public static function create( |
| 47 |
TBSCertificate $tbsCertificate, |
| 48 |
SignatureAlgorithmIdentifier $signatureAlgorithm, |
| 49 |
Signature $signatureValue |
| 50 |
): self { |
| 51 |
return new self($tbsCertificate, $signatureAlgorithm, $signatureValue); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static function fromASN1(Sequence $seq): self |
| 58 |
{ |
| 59 |
$tbsCert = TBSCertificate::fromASN1($seq->at(0)->asSequence()); |
| 60 |
$algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence()); |
| 61 |
if (! $algo instanceof SignatureAlgorithmIdentifier) { |
| 62 |
throw new UnexpectedValueException('Unsupported signature algorithm ' . $algo->oid() . '.'); |
| 63 |
} |
| 64 |
$signature = Signature::fromSignatureData($seq->at(2)->asBitString()->string(), $algo); |
| 65 |
return self::create($tbsCert, $algo, $signature); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static function fromDER(string $data): self |
| 72 |
{ |
| 73 |
return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence()); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public static function fromPEM(PEM $pem): self |
| 80 |
{ |
| 81 |
if ($pem->type() !== PEM::TYPE_CERTIFICATE) { |
| 82 |
throw new UnexpectedValueException('Invalid PEM type.'); |
| 83 |
} |
| 84 |
return self::fromDER($pem->data()); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function tbsCertificate(): TBSCertificate |
| 91 |
{ |
| 92 |
return $this->tbsCertificate; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function signatureAlgorithm(): SignatureAlgorithmIdentifier |
| 99 |
{ |
| 100 |
return $this->signatureAlgorithm; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function signatureValue(): Signature |
| 107 |
{ |
| 108 |
return $this->signatureValue; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function isSelfIssued(): bool |
| 115 |
{ |
| 116 |
return $this->tbsCertificate->subject() |
| 117 |
->equals($this->tbsCertificate->issuer()); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function equals(self $cert): bool |
| 126 |
{ |
| 127 |
return $this->_hasEqualSerialNumber($cert) && |
| 128 |
$this->_hasEqualPublicKey($cert) && $this->_hasEqualSubject($cert); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public function toASN1(): Sequence |
| 135 |
{ |
| 136 |
return Sequence::create( |
| 137 |
$this->tbsCertificate->toASN1(), |
| 138 |
$this->signatureAlgorithm->toASN1(), |
| 139 |
$this->signatureValue->bitString() |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function toDER(): string |
| 147 |
{ |
| 148 |
return $this->toASN1() |
| 149 |
->toDER(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function toPEM(): PEM |
| 156 |
{ |
| 157 |
return PEM::create(PEM::TYPE_CERTIFICATE, $this->toDER()); |
| 158 |
} |
| 159 |
|
| 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->tbsCertificate->toASN1() |
| 172 |
->toDER(); |
| 173 |
return $crypto->verify($data, $this->signatureValue, $pubkey_info, $this->signatureAlgorithm); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private function _hasEqualSerialNumber(self $cert): bool |
| 180 |
{ |
| 181 |
$sn1 = $this->tbsCertificate->serialNumber(); |
| 182 |
$sn2 = $cert->tbsCertificate->serialNumber(); |
| 183 |
return $sn1 === $sn2; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private function _hasEqualPublicKey(self $cert): bool |
| 190 |
{ |
| 191 |
$kid1 = $this->tbsCertificate->subjectPublicKeyInfo() |
| 192 |
->keyIdentifier(); |
| 193 |
$kid2 = $cert->tbsCertificate->subjectPublicKeyInfo() |
| 194 |
->keyIdentifier(); |
| 195 |
return $kid1 === $kid2; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private function _hasEqualSubject(self $cert): bool |
| 202 |
{ |
| 203 |
$dn1 = $this->tbsCertificate->subject(); |
| 204 |
$dn2 = $cert->tbsCertificate->subject(); |
| 205 |
return $dn1->equals($dn2); |
| 206 |
} |
| 207 |
} |