Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\AttributeCertificate;
6
7 use LogicException;
8 use SpomkyLabs\Pki\ASN1\Element;
9 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
10 use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitlyTaggedType;
11 use SpomkyLabs\Pki\X501\ASN1\Name;
12 use SpomkyLabs\Pki\X509\Certificate\Certificate;
13 use SpomkyLabs\Pki\X509\GeneralName\GeneralNames;
14
15 /**
16 * Implements *V2Form* ASN.1 type used as a attribute certificate issuer.
17 *
18 * @see https://tools.ietf.org/html/rfc5755#section-4.1
19 */
20 final class V2Form extends AttCertIssuer
21 {
22 private function __construct(
23 private readonly ?GeneralNames $issuerName,
24 private readonly ?IssuerSerial $baseCertificateID,
25 private readonly ?ObjectDigestInfo $objectDigestInfo
26 ) {
27 }
28
29 public static function create(
30 ?GeneralNames $issuerName = null,
31 ?IssuerSerial $baseCertificateID = null,
32 ?ObjectDigestInfo $objectDigestInfo = null
33 ): self {
34 return new self($issuerName, $baseCertificateID, $objectDigestInfo);
35 }
36
37 /**
38 * Initialize from ASN.1.
39 */
40 public static function fromV2ASN1(Sequence $seq): self
41 {
42 $issuer = null;
43 $cert_id = null;
44 $digest_info = null;
45 if ($seq->has(0, Element::TYPE_SEQUENCE)) {
46 $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence());
47 }
48 if ($seq->hasTagged(0)) {
49 $cert_id = IssuerSerial::fromASN1(
50 $seq->getTagged(0)
51 ->asImplicit(Element::TYPE_SEQUENCE)
52 ->asSequence()
53 );
54 }
55 if ($seq->hasTagged(1)) {
56 $digest_info = ObjectDigestInfo::fromASN1(
57 $seq->getTagged(1)
58 ->asImplicit(Element::TYPE_SEQUENCE)
59 ->asSequence()
60 );
61 }
62 return self::create($issuer, $cert_id, $digest_info);
63 }
64
65 /**
66 * Check whether issuer name is set.
67 */
68 public function hasIssuerName(): bool
69 {
70 return isset($this->issuerName);
71 }
72
73 /**
74 * Get issuer name.
75 */
76 public function issuerName(): GeneralNames
77 {
78 if (! $this->hasIssuerName()) {
79 throw new LogicException('issuerName not set.');
80 }
81 return $this->issuerName;
82 }
83
84 /**
85 * Get DN of the issuer.
86 *
87 * This is a convenience method conforming to RFC 5755, which states that Issuer must contain only one non-empty
88 * distinguished name.
89 */
90 public function name(): Name
91 {
92 return $this->issuerName()
93 ->firstDN();
94 }
95
96 public function toASN1(): Element
97 {
98 $elements = [];
99 if (isset($this->issuerName)) {
100 $elements[] = $this->issuerName->toASN1();
101 }
102 if (isset($this->baseCertificateID)) {
103 $elements[] = ImplicitlyTaggedType::create(0, $this->baseCertificateID->toASN1());
104 }
105 if (isset($this->objectDigestInfo)) {
106 $elements[] = ImplicitlyTaggedType::create(1, $this->objectDigestInfo->toASN1());
107 }
108 return ImplicitlyTaggedType::create(0, Sequence::create(...$elements));
109 }
110
111 public function identifiesPKC(Certificate $cert): bool
112 {
113 $name = $this->issuerName?->firstDN();
114 return ! ($name === null || ! $cert->tbsCertificate()->subject()->equals($name));
115 }
116 }