| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate; |
| 6 |
|
| 7 |
use Brick\Math\BigInteger; |
| 8 |
use function count; |
| 9 |
use LogicException; |
| 10 |
use SpomkyLabs\Pki\ASN1\Element; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ExplicitlyTaggedType; |
| 14 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 15 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 16 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\AlgorithmIdentifier; |
| 17 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier; |
| 18 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PrivateKeyInfo; |
| 19 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo; |
| 20 |
use SpomkyLabs\Pki\X501\ASN1\Name; |
| 21 |
use SpomkyLabs\Pki\X509\Certificate\Extension\AuthorityKeyIdentifierExtension; |
| 22 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Extension; |
| 23 |
use SpomkyLabs\Pki\X509\Certificate\Extension\SubjectKeyIdentifierExtension; |
| 24 |
use SpomkyLabs\Pki\X509\CertificationRequest\CertificationRequest; |
| 25 |
use function strval; |
| 26 |
use UnexpectedValueException; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
final class TBSCertificate |
| 34 |
{ |
| 35 |
|
| 36 |
final public const VERSION_1 = 0; |
| 37 |
|
| 38 |
final public const VERSION_2 = 1; |
| 39 |
|
| 40 |
final public const VERSION_3 = 2; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private ?int $version = null; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private ?string $serialNumber = null; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private ?SignatureAlgorithmIdentifier $signature = null; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private ?UniqueIdentifier $issuerUniqueID = null; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private ?UniqueIdentifier $subjectUniqueID = null; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private Extensions $extensions; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
private function __construct( |
| 79 |
private Name $subject, |
| 80 |
private PublicKeyInfo $subjectPublicKeyInfo, |
| 81 |
private Name $issuer, |
| 82 |
private Validity $validity |
| 83 |
) { |
| 84 |
$this->extensions = Extensions::create(); |
| 85 |
} |
| 86 |
|
| 87 |
public static function create( |
| 88 |
Name $subject, |
| 89 |
PublicKeyInfo $subjectPublicKeyInfo, |
| 90 |
Name $issuer, |
| 91 |
Validity $validity |
| 92 |
): self { |
| 93 |
return new self($subject, $subjectPublicKeyInfo, $issuer, $validity); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static function fromASN1(Sequence $seq): self |
| 100 |
{ |
| 101 |
$idx = 0; |
| 102 |
if ($seq->hasTagged(0)) { |
| 103 |
++$idx; |
| 104 |
$version = $seq->getTagged(0) |
| 105 |
->asExplicit() |
| 106 |
->asInteger() |
| 107 |
->intNumber(); |
| 108 |
} else { |
| 109 |
$version = self::VERSION_1; |
| 110 |
} |
| 111 |
$serial = $seq->at($idx++) |
| 112 |
->asInteger() |
| 113 |
->number(); |
| 114 |
$algo = AlgorithmIdentifier::fromASN1($seq->at($idx++)->asSequence()); |
| 115 |
if (! $algo instanceof SignatureAlgorithmIdentifier) { |
| 116 |
throw new UnexpectedValueException('Unsupported signature algorithm ' . $algo->name() . '.'); |
| 117 |
} |
| 118 |
$issuer = Name::fromASN1($seq->at($idx++)->asSequence()); |
| 119 |
$validity = Validity::fromASN1($seq->at($idx++)->asSequence()); |
| 120 |
$subject = Name::fromASN1($seq->at($idx++)->asSequence()); |
| 121 |
$pki = PublicKeyInfo::fromASN1($seq->at($idx++)->asSequence()); |
| 122 |
$tbs_cert = self::create($subject, $pki, $issuer, $validity) |
| 123 |
->withVersion($version) |
| 124 |
->withSerialNumber($serial) |
| 125 |
->withSignature($algo) |
| 126 |
; |
| 127 |
if ($seq->hasTagged(1)) { |
| 128 |
$tbs_cert = $tbs_cert->withIssuerUniqueID(UniqueIdentifier::fromASN1( |
| 129 |
$seq->getTagged(1) |
| 130 |
->asImplicit(Element::TYPE_BIT_STRING) |
| 131 |
->asBitString() |
| 132 |
)); |
| 133 |
} |
| 134 |
if ($seq->hasTagged(2)) { |
| 135 |
$tbs_cert = $tbs_cert->withSubjectUniqueID(UniqueIdentifier::fromASN1( |
| 136 |
$seq->getTagged(2) |
| 137 |
->asImplicit(Element::TYPE_BIT_STRING) |
| 138 |
->asBitString() |
| 139 |
)); |
| 140 |
} |
| 141 |
if ($seq->hasTagged(3)) { |
| 142 |
$tbs_cert = $tbs_cert->withExtensions(Extensions::fromASN1($seq->getTagged(3)->asExplicit()->asSequence())); |
| 143 |
} |
| 144 |
return $tbs_cert; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public static function fromCSR(CertificationRequest $cr): self |
| 153 |
{ |
| 154 |
$cri = $cr->certificationRequestInfo(); |
| 155 |
$tbs_cert = self::create( |
| 156 |
$cri->subject(), |
| 157 |
$cri->subjectPKInfo(), |
| 158 |
Name::create(), |
| 159 |
Validity::fromStrings(null, null) |
| 160 |
); |
| 161 |
|
| 162 |
if ($cri->hasAttributes()) { |
| 163 |
$attribs = $cri->attributes(); |
| 164 |
if ($attribs->hasExtensionRequest()) { |
| 165 |
$tbs_cert = $tbs_cert->withExtensions($attribs->extensionRequest()->extensions()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
return $tbs_cert->withAdditionalExtensions( |
| 170 |
SubjectKeyIdentifierExtension::create(false, $cri->subjectPKInfo()->keyIdentifier()) |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function withIssuerCertificate(Certificate $cert): self |
| 183 |
{ |
| 184 |
$obj = clone $this; |
| 185 |
|
| 186 |
$obj->issuer = $cert->tbsCertificate() |
| 187 |
->subject(); |
| 188 |
|
| 189 |
$key_id = $cert->tbsCertificate() |
| 190 |
->subjectPublicKeyInfo() |
| 191 |
->keyIdentifier(); |
| 192 |
$obj->extensions = $obj->extensions->withExtensions(AuthorityKeyIdentifierExtension::create(false, $key_id)); |
| 193 |
return $obj; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function withVersion(int $version): self |
| 202 |
{ |
| 203 |
$obj = clone $this; |
| 204 |
$obj->version = $version; |
| 205 |
return $obj; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function withSerialNumber(int|string $serial): self |
| 214 |
{ |
| 215 |
$obj = clone $this; |
| 216 |
$obj->serialNumber = strval($serial); |
| 217 |
return $obj; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function withRandomSerialNumber(int $size): self |
| 226 |
{ |
| 227 |
|
| 228 |
$num = BigInteger::of(random_int(1, 0x7f)); |
| 229 |
for ($i = 1; $i < $size; ++$i) { |
| 230 |
$num = $num->shiftedLeft(8); |
| 231 |
$num = $num->plus(random_int(0, 0xff)); |
| 232 |
} |
| 233 |
return $this->withSerialNumber($num->toBase(10)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
public function withSignature(SignatureAlgorithmIdentifier $algo): self |
| 240 |
{ |
| 241 |
$obj = clone $this; |
| 242 |
$obj->signature = $algo; |
| 243 |
return $obj; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function withIssuer(Name $issuer): self |
| 250 |
{ |
| 251 |
$obj = clone $this; |
| 252 |
$obj->issuer = $issuer; |
| 253 |
return $obj; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function withValidity(Validity $validity): self |
| 260 |
{ |
| 261 |
$obj = clone $this; |
| 262 |
$obj->validity = $validity; |
| 263 |
return $obj; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public function withSubject(Name $subject): self |
| 270 |
{ |
| 271 |
$obj = clone $this; |
| 272 |
$obj->subject = $subject; |
| 273 |
return $obj; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public function withSubjectPublicKeyInfo(PublicKeyInfo $pub_key_info): self |
| 280 |
{ |
| 281 |
$obj = clone $this; |
| 282 |
$obj->subjectPublicKeyInfo = $pub_key_info; |
| 283 |
return $obj; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
public function withIssuerUniqueID(UniqueIdentifier $id): self |
| 290 |
{ |
| 291 |
$obj = clone $this; |
| 292 |
$obj->issuerUniqueID = $id; |
| 293 |
return $obj; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
public function withSubjectUniqueID(UniqueIdentifier $id): self |
| 300 |
{ |
| 301 |
$obj = clone $this; |
| 302 |
$obj->subjectUniqueID = $id; |
| 303 |
return $obj; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public function withExtensions(Extensions $extensions): self |
| 310 |
{ |
| 311 |
$obj = clone $this; |
| 312 |
$obj->extensions = $extensions; |
| 313 |
return $obj; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function withAdditionalExtensions(Extension ...$exts): self |
| 322 |
{ |
| 323 |
$obj = clone $this; |
| 324 |
$obj->extensions = $obj->extensions->withExtensions(...$exts); |
| 325 |
return $obj; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public function hasVersion(): bool |
| 332 |
{ |
| 333 |
return isset($this->version); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function version(): int |
| 340 |
{ |
| 341 |
if (! $this->hasVersion()) { |
| 342 |
throw new LogicException('version not set.'); |
| 343 |
} |
| 344 |
return $this->version; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
public function hasSerialNumber(): bool |
| 351 |
{ |
| 352 |
return isset($this->serialNumber); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
public function serialNumber(): string |
| 361 |
{ |
| 362 |
if (! $this->hasSerialNumber()) { |
| 363 |
throw new LogicException('serialNumber not set.'); |
| 364 |
} |
| 365 |
return $this->serialNumber; |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function hasSignature(): bool |
| 372 |
{ |
| 373 |
return isset($this->signature); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public function signature(): SignatureAlgorithmIdentifier |
| 380 |
{ |
| 381 |
if (! $this->hasSignature()) { |
| 382 |
throw new LogicException('signature not set.'); |
| 383 |
} |
| 384 |
return $this->signature; |
| 385 |
} |
| 386 |
|
| 387 |
public function issuer(): Name |
| 388 |
{ |
| 389 |
return $this->issuer; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function validity(): Validity |
| 396 |
{ |
| 397 |
return $this->validity; |
| 398 |
} |
| 399 |
|
| 400 |
public function subject(): Name |
| 401 |
{ |
| 402 |
return $this->subject; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
public function subjectPublicKeyInfo(): PublicKeyInfo |
| 409 |
{ |
| 410 |
return $this->subjectPublicKeyInfo; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
public function hasIssuerUniqueID(): bool |
| 417 |
{ |
| 418 |
return isset($this->issuerUniqueID); |
| 419 |
} |
| 420 |
|
| 421 |
public function issuerUniqueID(): UniqueIdentifier |
| 422 |
{ |
| 423 |
if (! $this->hasIssuerUniqueID()) { |
| 424 |
throw new LogicException('issuerUniqueID not set.'); |
| 425 |
} |
| 426 |
return $this->issuerUniqueID; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function hasSubjectUniqueID(): bool |
| 433 |
{ |
| 434 |
return isset($this->subjectUniqueID); |
| 435 |
} |
| 436 |
|
| 437 |
public function subjectUniqueID(): UniqueIdentifier |
| 438 |
{ |
| 439 |
if (! $this->hasSubjectUniqueID()) { |
| 440 |
throw new LogicException('subjectUniqueID not set.'); |
| 441 |
} |
| 442 |
return $this->subjectUniqueID; |
| 443 |
} |
| 444 |
|
| 445 |
public function extensions(): Extensions |
| 446 |
{ |
| 447 |
return $this->extensions; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
public function toASN1(): Sequence |
| 454 |
{ |
| 455 |
$elements = []; |
| 456 |
$version = $this->version(); |
| 457 |
|
| 458 |
if ($version !== self::VERSION_1) { |
| 459 |
$elements[] = ExplicitlyTaggedType::create(0, Integer::create($version)); |
| 460 |
} |
| 461 |
$serial = $this->serialNumber(); |
| 462 |
$signature = $this->signature(); |
| 463 |
|
| 464 |
array_push( |
| 465 |
$elements, |
| 466 |
Integer::create($serial), |
| 467 |
$signature->toASN1(), |
| 468 |
$this->issuer->toASN1(), |
| 469 |
$this->validity->toASN1(), |
| 470 |
$this->subject->toASN1(), |
| 471 |
$this->subjectPublicKeyInfo->toASN1() |
| 472 |
); |
| 473 |
if (isset($this->issuerUniqueID)) { |
| 474 |
$elements[] = ImplicitlyTaggedType::create(1, $this->issuerUniqueID->toASN1()); |
| 475 |
} |
| 476 |
if (isset($this->subjectUniqueID)) { |
| 477 |
$elements[] = ImplicitlyTaggedType::create(2, $this->subjectUniqueID->toASN1()); |
| 478 |
} |
| 479 |
if (count($this->extensions) !== 0) { |
| 480 |
$elements[] = ExplicitlyTaggedType::create(3, $this->extensions->toASN1()); |
| 481 |
} |
| 482 |
return Sequence::create(...$elements); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function sign( |
| 493 |
SignatureAlgorithmIdentifier $algo, |
| 494 |
PrivateKeyInfo $privkey_info, |
| 495 |
?Crypto $crypto = null |
| 496 |
): Certificate { |
| 497 |
$crypto ??= Crypto::getDefault(); |
| 498 |
$tbs_cert = clone $this; |
| 499 |
if (! isset($tbs_cert->version)) { |
| 500 |
$tbs_cert->version = $tbs_cert->_determineVersion(); |
| 501 |
} |
| 502 |
if (! isset($tbs_cert->serialNumber)) { |
| 503 |
$tbs_cert->serialNumber = '0'; |
| 504 |
} |
| 505 |
$tbs_cert->signature = $algo; |
| 506 |
$data = $tbs_cert->toASN1() |
| 507 |
->toDER(); |
| 508 |
$signature = $crypto->sign($data, $privkey_info, $algo); |
| 509 |
return Certificate::create($tbs_cert, $algo, $signature); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
private function _determineVersion(): int |
| 516 |
{ |
| 517 |
|
| 518 |
if (count($this->extensions) !== 0) { |
| 519 |
return self::VERSION_3; |
| 520 |
} |
| 521 |
|
| 522 |
if (isset($this->issuerUniqueID) || isset($this->subjectUniqueID)) { |
| 523 |
return self::VERSION_2; |
| 524 |
} |
| 525 |
return self::VERSION_1; |
| 526 |
} |
| 527 |
} |