V2Form   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 11
dl 0
loc 143
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromV2ASN1() 0 25 4
A hasIssuerName() 0 4 1
A issuerName() 0 7 2
A name() 0 4 1
A toASN1() 0 16 4
A identifiesPKC() 0 10 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\AttributeCertificate;
6
7
use ASN1\Element;
8
use ASN1\Type\TaggedType;
9
use ASN1\Type\Constructed\Sequence;
10
use ASN1\Type\Tagged\ImplicitlyTaggedType;
11
use X501\ASN1\Name;
12
use X509\Certificate\Certificate;
13
use X509\GeneralName\GeneralNames;
14
15
/**
16
 * Implements <i>V2Form</i> ASN.1 type used as a attribute certificate issuer.
17
 *
18
 * @link https://tools.ietf.org/html/rfc5755#section-4.1
19
 */
20
class V2Form extends AttCertIssuer
21
{
22
    /**
23
     * Issuer name.
24
     *
25
     * @var GeneralNames $_issuerName
26
     */
27
    protected $_issuerName;
28
    
29
    /**
30
     * Issuer PKC's issuer and serial.
31
     *
32
     * @var IssuerSerial $_baseCertificateID
33
     */
34
    protected $_baseCertificateID;
35
    
36
    /**
37
     * Linked object.
38
     *
39
     * @var ObjectDigestInfo $_objectDigestInfo
40
     */
41
    protected $_objectDigestInfo;
42
    
43
    /**
44
     * Constructor.
45
     *
46
     * @param GeneralNames|null $names
47
     */
48 13
    public function __construct(GeneralNames $names = null)
49
    {
50 13
        $this->_issuerName = $names;
51 13
        $this->_baseCertificateID = null;
52 13
        $this->_objectDigestInfo = null;
53 13
    }
54
    
55
    /**
56
     * Initialize from ASN.1.
57
     *
58
     * @param Sequence $seq
59
     * @return self
60
     */
61 8
    public static function fromV2ASN1(Sequence $seq): self
62
    {
63 8
        $issuer = null;
64 8
        $cert_id = null;
65 8
        $digest_info = null;
66 8
        if ($seq->has(0, Element::TYPE_SEQUENCE)) {
67 8
            $issuer = GeneralNames::fromASN1($seq->at(0)->asSequence());
68
        }
69 8
        if ($seq->hasTagged(0)) {
70 1
            $cert_id = IssuerSerial::fromASN1(
71 1
                $seq->getTagged(0)
72 1
                    ->asImplicit(Element::TYPE_SEQUENCE)
73 1
                    ->asSequence());
74
        }
75 8
        if ($seq->hasTagged(1)) {
76 1
            $digest_info = ObjectDigestInfo::fromASN1(
77 1
                $seq->getTagged(1)
78 1
                    ->asImplicit(Element::TYPE_SEQUENCE)
79 1
                    ->asSequence());
80
        }
81 8
        $obj = new self($issuer);
82 8
        $obj->_baseCertificateID = $cert_id;
83 8
        $obj->_objectDigestInfo = $digest_info;
84 8
        return $obj;
85
    }
86
    
87
    /**
88
     * Check whether issuer name is set.
89
     *
90
     * @return bool
91
     */
92 3
    public function hasIssuerName(): bool
93
    {
94 3
        return isset($this->_issuerName);
95
    }
96
    
97
    /**
98
     * Get issuer name.
99
     *
100
     * @throws \LogicException
101
     * @return GeneralNames
102
     */
103 3
    public function issuerName(): GeneralNames
104
    {
105 3
        if (!$this->hasIssuerName()) {
106 1
            throw new \LogicException("issuerName not set.");
107
        }
108 2
        return $this->_issuerName;
109
    }
110
    
111
    /**
112
     * Get DN of the issuer.
113
     *
114
     * This is a convenience method conforming to RFC 5755, which states
115
     * that Issuer must contain only one non-empty distinguished name.
116
     *
117
     * @return \X501\ASN1\Name
118
     */
119 1
    public function name(): Name
120
    {
121 1
        return $this->issuerName()->firstDN();
122
    }
123
    
124
    /**
125
     *
126
     * @see \X509\AttributeCertificate\AttCertIssuer::ASN1()
127
     * @return ImplicitlyTaggedType Tagged Sequence
128
     */
129 21
    public function toASN1(): TaggedType
130
    {
131 21
        $elements = array();
132 21
        if (isset($this->_issuerName)) {
133 21
            $elements[] = $this->_issuerName->toASN1();
134
        }
135 21
        if (isset($this->_baseCertificateID)) {
136 1
            $elements[] = new ImplicitlyTaggedType(0,
137 1
                $this->_baseCertificateID->toASN1());
138
        }
139 21
        if (isset($this->_objectDigestInfo)) {
140 1
            $elements[] = new ImplicitlyTaggedType(1,
141 1
                $this->_objectDigestInfo->toASN1());
142
        }
143 21
        return new ImplicitlyTaggedType(0, new Sequence(...$elements));
144
    }
145
    
146
    /**
147
     *
148
     * {@inheritdoc}
149
     * @see \X509\AttributeCertificate\AttCertIssuer::identifiesPKC()
150
     * @return bool
151
     */
152 13
    public function identifiesPKC(Certificate $cert): bool
153
    {
154 13
        $name = $this->_issuerName->firstDN();
155 13
        if (!$cert->tbsCertificate()
156 13
            ->subject()
157 13
            ->equals($name)) {
158 3
            return false;
159
        }
160 10
        return true;
161
    }
162
}
163