Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/CertificationRequest/CertificationRequest.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationRequest;
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\Signature\Signature;
14 use Stringable;
15 use UnexpectedValueException;
16
17 /**
18 * Implements *CertificationRequest* ASN.1 type.
19 *
20 * @see https://tools.ietf.org/html/rfc2986#section-4
21 */
22 final class CertificationRequest implements Stringable
23 {
24 private function __construct(
25 private readonly CertificationRequestInfo $certificationRequestInfo,
26 private readonly SignatureAlgorithmIdentifier $signatureAlgorithm,
27 private readonly Signature $signature
28 ) {
29 }
30
31 /**
32 * Get certification request as a PEM formatted string.
33 */
34 public function __toString(): string
35 {
36 return $this->toPEM()
37 ->string();
38 }
39
40 public static function create(
41 CertificationRequestInfo $_certificationRequestInfo,
42 SignatureAlgorithmIdentifier $_signatureAlgorithm,
43 Signature $_signature
44 ): self {
45 return new self($_certificationRequestInfo, $_signatureAlgorithm, $_signature);
46 }
47
48 /**
49 * Initialize from ASN.1.
50 */
51 public static function fromASN1(Sequence $seq): self
52 {
53 $info = CertificationRequestInfo::fromASN1($seq->at(0)->asSequence());
54 $algo = AlgorithmIdentifier::fromASN1($seq->at(1)->asSequence());
55 if (! $algo instanceof SignatureAlgorithmIdentifier) {
56 throw new UnexpectedValueException('Unsupported signature algorithm ' . $algo->oid() . '.');
57 }
58 $signature = Signature::fromSignatureData($seq->at(2)->asBitString()->string(), $algo);
59 return self::create($info, $algo, $signature);
60 }
61
62 /**
63 * Initialize from DER.
64 */
65 public static function fromDER(string $data): self
66 {
67 return self::fromASN1(UnspecifiedType::fromDER($data)->asSequence());
68 }
69
70 /**
71 * Initialize from PEM.
72 */
73 public static function fromPEM(PEM $pem): self
74 {
75 if ($pem->type() !== PEM::TYPE_CERTIFICATE_REQUEST) {
76 throw new UnexpectedValueException('Invalid PEM type.');
77 }
78 return self::fromDER($pem->data());
79 }
80
81 /**
82 * Get certification request info.
83 */
84 public function certificationRequestInfo(): CertificationRequestInfo
85 {
86 return $this->certificationRequestInfo;
87 }
88
89 /**
90 * Get signature algorithm.
91 */
92 public function signatureAlgorithm(): SignatureAlgorithmIdentifier
93 {
94 return $this->signatureAlgorithm;
95 }
96
97 public function signature(): Signature
98 {
99 return $this->signature;
100 }
101
102 /**
103 * Generate ASN.1 structure.
104 */
105 public function toASN1(): Sequence
106 {
107 return Sequence::create(
108 $this->certificationRequestInfo->toASN1(),
109 $this->signatureAlgorithm->toASN1(),
110 $this->signature->bitString()
111 );
112 }
113
114 /**
115 * Get certification request as a DER.
116 */
117 public function toDER(): string
118 {
119 return $this->toASN1()
120 ->toDER();
121 }
122
123 /**
124 * Get certification request as a PEM.
125 */
126 public function toPEM(): PEM
127 {
128 return PEM::create(PEM::TYPE_CERTIFICATE_REQUEST, $this->toDER());
129 }
130
131 /**
132 * Verify certification request signature.
133 *
134 * @param null|Crypto $crypto Crypto engine, use default if not set
135 *
136 * @return bool True if signature matches
137 */
138 public function verify(?Crypto $crypto = null): bool
139 {
140 $crypto ??= Crypto::getDefault();
141 $data = $this->certificationRequestInfo->toASN1()
142 ->toDER();
143 $pk_info = $this->certificationRequestInfo->subjectPKInfo();
144 return $crypto->verify($data, $this->signature, $pk_info, $this->signatureAlgorithm);
145 }
146 }