GeneralName::equals()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.9332
c 0
b 0
f 0
nc 3
cc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace X509\GeneralName;
6
7
use ASN1\Element;
8
use ASN1\Type\TaggedType;
9
use ASN1\Type\UnspecifiedType;
10
11
/**
12
 * Implements <i>GeneralName</i> CHOICE with implicit tagging.
13
 *
14
 * @link https://tools.ietf.org/html/rfc5280#section-4.2.1.6
15
 */
16
abstract class GeneralName
17
{
18
    // GeneralName CHOICE tags
19
    const TAG_OTHER_NAME = 0;
20
    const TAG_RFC822_NAME = 1;
21
    const TAG_DNS_NAME = 2;
22
    const TAG_X400_ADDRESS = 3;
23
    const TAG_DIRECTORY_NAME = 4;
24
    const TAG_EDI_PARTY_NAME = 5;
25
    const TAG_URI = 6;
26
    const TAG_IP_ADDRESS = 7;
27
    const TAG_REGISTERED_ID = 8;
28
    
29
    /**
30
     * Chosen tag.
31
     *
32
     * @var int $_tag
33
     */
34
    protected $_tag;
35
    
36
    /**
37
     * Get string value of the type.
38
     *
39
     * @return string
40
     */
41
    abstract public function string(): string;
42
    
43
    /**
44
     * Get ASN.1 value in GeneralName CHOICE context.
45
     *
46
     * @return TaggedType
47
     */
48
    abstract protected function _choiceASN1(): TaggedType;
49
    
50
    /**
51
     * Initialize concrete object from the chosen ASN.1 element.
52
     *
53
     * @param UnspecifiedType $el
54
     * @return self
55
     */
56 1
    public static function fromChosenASN1(UnspecifiedType $el)
57
    {
58 1
        throw new \BadMethodCallException(
59 1
            __FUNCTION__ . " must be implemented in the derived class.");
60
    }
61
    
62
    /**
63
     * Initialize from ASN.1.
64
     *
65
     * @param TaggedType $el
66
     * @throws \UnexpectedValueException
67
     * @return self
68
     */
69 67
    public static function fromASN1(TaggedType $el): self
70
    {
71 67
        switch ($el->tag()) {
72
            // otherName
73 67
            case self::TAG_OTHER_NAME:
74 9
                return OtherName::fromChosenASN1(
75 9
                    $el->asImplicit(Element::TYPE_SEQUENCE));
76
            // rfc822Name
77 66
            case self::TAG_RFC822_NAME:
78 9
                return RFC822Name::fromChosenASN1(
79 9
                    $el->asImplicit(Element::TYPE_IA5_STRING));
80
            // dNSName
81 65
            case self::TAG_DNS_NAME:
82 13
                return DNSName::fromChosenASN1(
83 13
                    $el->asImplicit(Element::TYPE_IA5_STRING));
84
            // x400Address
85 63
            case self::TAG_X400_ADDRESS:
86 2
                return X400Address::fromChosenASN1(
87 2
                    $el->asImplicit(Element::TYPE_SEQUENCE));
88
            // directoryName
89 61
            case self::TAG_DIRECTORY_NAME:
90
                // because Name is a CHOICE, albeit having only one option,
91
                // explicit tagging must be used
92
                // (see X.680 07/2002 30.6.c)
93 37
                return DirectoryName::fromChosenASN1($el->asExplicit());
94
            // ediPartyName
95 43
            case self::TAG_EDI_PARTY_NAME:
96 2
                return EDIPartyName::fromChosenASN1(
97 2
                    $el->asImplicit(Element::TYPE_SEQUENCE));
98
            // uniformResourceIdentifier
99 41
            case self::TAG_URI:
100 34
                return UniformResourceIdentifier::fromChosenASN1(
101 34
                    $el->asImplicit(Element::TYPE_IA5_STRING));
102
            // iPAddress
103 15
            case self::TAG_IP_ADDRESS:
104 13
                return IPAddress::fromChosenASN1(
105 13
                    $el->asImplicit(Element::TYPE_OCTET_STRING));
106
            // registeredID
107 10
            case self::TAG_REGISTERED_ID:
108 9
                return RegisteredID::fromChosenASN1(
109 9
                    $el->asImplicit(Element::TYPE_OBJECT_IDENTIFIER));
110
        }
111 1
        throw new \UnexpectedValueException(
112 1
            "GeneralName type " . $el->tag() . " not supported.");
113
    }
114
    
115
    /**
116
     * Get type tag.
117
     *
118
     * @return int
119
     */
120 58
    public function tag(): int
121
    {
122 58
        return $this->_tag;
123
    }
124
    
125
    /**
126
     * Generate ASN.1 element.
127
     *
128
     * @return Element
129
     */
130 89
    public function toASN1(): Element
131
    {
132 89
        return $this->_choiceASN1();
133
    }
134
    
135
    /**
136
     * Check whether GeneralName is equal to other.
137
     *
138
     * @param GeneralName $other GeneralName to compare to
139
     * @return boolean True if names are equal
140
     */
141 3
    public function equals(GeneralName $other): bool
142
    {
143 3
        if ($this->_tag != $other->_tag) {
144 1
            return false;
145
        }
146 2
        if ($this->_choiceASN1()->toDER() != $other->_choiceASN1()->toDER()) {
147 1
            return false;
148
        }
149 1
        return true;
150
    }
151
    
152
    /**
153
     * Get general name as a string.
154
     *
155
     * @return string
156
     */
157 8
    public function __toString()
158
    {
159 8
        return $this->string();
160
    }
161
}
162