| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\CertificationRequest; |
| 6 |
|
| 7 |
use LogicException; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 10 |
use SpomkyLabs\Pki\ASN1\Type\Primitive\Integer; |
| 11 |
use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType; |
| 12 |
use SpomkyLabs\Pki\CryptoBridge\Crypto; |
| 13 |
use SpomkyLabs\Pki\CryptoTypes\AlgorithmIdentifier\Feature\SignatureAlgorithmIdentifier; |
| 14 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PrivateKeyInfo; |
| 15 |
use SpomkyLabs\Pki\CryptoTypes\Asymmetric\PublicKeyInfo; |
| 16 |
use SpomkyLabs\Pki\X501\ASN1\Attribute; |
| 17 |
use SpomkyLabs\Pki\X501\ASN1\Name; |
| 18 |
use SpomkyLabs\Pki\X509\Certificate\Extensions; |
| 19 |
use SpomkyLabs\Pki\X509\CertificationRequest\Attribute\ExtensionRequestValue; |
| 20 |
use UnexpectedValueException; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
final class CertificationRequestInfo |
| 28 |
{ |
| 29 |
final public const VERSION_1 = 0; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private readonly int $version; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private ?Attributes $attributes = null; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private function __construct( |
| 46 |
private Name $subject, |
| 47 |
private readonly PublicKeyInfo $subjectPKInfo |
| 48 |
) { |
| 49 |
$this->version = self::VERSION_1; |
| 50 |
} |
| 51 |
|
| 52 |
public static function create(Name $subject, PublicKeyInfo $subjectPKInfo): self |
| 53 |
{ |
| 54 |
return new self($subject, $subjectPKInfo); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static function fromASN1(Sequence $seq): self |
| 61 |
{ |
| 62 |
$version = $seq->at(0) |
| 63 |
->asInteger() |
| 64 |
->intNumber(); |
| 65 |
if ($version !== self::VERSION_1) { |
| 66 |
throw new UnexpectedValueException("Version {$version} not supported."); |
| 67 |
} |
| 68 |
$subject = Name::fromASN1($seq->at(1)->asSequence()); |
| 69 |
$pkinfo = PublicKeyInfo::fromASN1($seq->at(2)->asSequence()); |
| 70 |
$obj = self::create($subject, $pkinfo); |
| 71 |
if ($seq->hasTagged(0)) { |
| 72 |
$obj = $obj->withAttributes( |
| 73 |
Attributes::fromASN1($seq->getTagged(0)->asImplicit(Element::TYPE_SET)->asSet()) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
return $obj; |
| 78 |
} |
| 79 |
|
| 80 |
public function version(): int |
| 81 |
{ |
| 82 |
return $this->version; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function withSubject(Name $subject): self |
| 89 |
{ |
| 90 |
$obj = clone $this; |
| 91 |
$obj->subject = $subject; |
| 92 |
return $obj; |
| 93 |
} |
| 94 |
|
| 95 |
public function subject(): Name |
| 96 |
{ |
| 97 |
return $this->subject; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function subjectPKInfo(): PublicKeyInfo |
| 104 |
{ |
| 105 |
return $this->subjectPKInfo; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function hasAttributes(): bool |
| 112 |
{ |
| 113 |
return isset($this->attributes); |
| 114 |
} |
| 115 |
|
| 116 |
public function attributes(): Attributes |
| 117 |
{ |
| 118 |
if (! $this->hasAttributes()) { |
| 119 |
throw new LogicException('No attributes.'); |
| 120 |
} |
| 121 |
return $this->attributes; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function withAttributes(Attributes $attribs): self |
| 128 |
{ |
| 129 |
$obj = clone $this; |
| 130 |
$obj->attributes = $attribs; |
| 131 |
return $obj; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function withExtensionRequest(Extensions $extensions): self |
| 140 |
{ |
| 141 |
$obj = clone $this; |
| 142 |
if (! isset($obj->attributes)) { |
| 143 |
$obj->attributes = Attributes::create(); |
| 144 |
} |
| 145 |
$obj->attributes = $obj->attributes->withUnique( |
| 146 |
Attribute::fromAttributeValues(ExtensionRequestValue::create($extensions)) |
| 147 |
); |
| 148 |
return $obj; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public function toASN1(): Sequence |
| 155 |
{ |
| 156 |
$elements = [Integer::create($this->version), $this->subject->toASN1(), $this->subjectPKInfo->toASN1()]; |
| 157 |
if (isset($this->attributes)) { |
| 158 |
$elements[] = ImplicitlyTaggedType::create(0, $this->attributes->toASN1()); |
| 159 |
} |
| 160 |
return Sequence::create(...$elements); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function sign( |
| 171 |
SignatureAlgorithmIdentifier $algo, |
| 172 |
PrivateKeyInfo $privkey_info, |
| 173 |
?Crypto $crypto = null |
| 174 |
): CertificationRequest { |
| 175 |
$crypto ??= Crypto::getDefault(); |
| 176 |
$data = $this->toASN1() |
| 177 |
->toDER(); |
| 178 |
$signature = $crypto->sign($data, $privkey_info, $algo); |
| 179 |
return CertificationRequest::create($this, $algo, $signature); |
| 180 |
} |
| 181 |
} |