@@ -12,25 +12,25 @@ |
||
12 | 12 | */ |
13 | 13 | class VisibleString extends PrimitiveString |
14 | 14 | { |
15 | - use UniversalClass; |
|
15 | + use UniversalClass; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param string $string |
|
21 | - */ |
|
22 | - public function __construct(string $string) |
|
23 | - { |
|
24 | - $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
25 | - parent::__construct($string); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param string $string |
|
21 | + */ |
|
22 | + public function __construct(string $string) |
|
23 | + { |
|
24 | + $this->_typeTag = self::TYPE_VISIBLE_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - protected function _validateString(string $string): bool |
|
33 | - { |
|
34 | - return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
35 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + protected function _validateString(string $string): bool |
|
33 | + { |
|
34 | + return preg_match('/[^\x20-\x7e]/', $string) == 0; |
|
35 | + } |
|
36 | 36 | } |
@@ -12,26 +12,26 @@ |
||
12 | 12 | */ |
13 | 13 | class ObjectDescriptor extends PrimitiveString |
14 | 14 | { |
15 | - use UniversalClass; |
|
15 | + use UniversalClass; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param string $descriptor |
|
21 | - */ |
|
22 | - public function __construct(string $descriptor) |
|
23 | - { |
|
24 | - $this->_string = $descriptor; |
|
25 | - $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
26 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param string $descriptor |
|
21 | + */ |
|
22 | + public function __construct(string $descriptor) |
|
23 | + { |
|
24 | + $this->_string = $descriptor; |
|
25 | + $this->_typeTag = self::TYPE_OBJECT_DESCRIPTOR; |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Get the object descriptor. |
|
30 | - * |
|
31 | - * @return string |
|
32 | - */ |
|
33 | - public function descriptor(): string |
|
34 | - { |
|
35 | - return $this->_string; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Get the object descriptor. |
|
30 | + * |
|
31 | + * @return string |
|
32 | + */ |
|
33 | + public function descriptor(): string |
|
34 | + { |
|
35 | + return $this->_string; |
|
36 | + } |
|
37 | 37 | } |
@@ -12,25 +12,25 @@ |
||
12 | 12 | */ |
13 | 13 | class NumericString extends PrimitiveString |
14 | 14 | { |
15 | - use UniversalClass; |
|
15 | + use UniversalClass; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param string $string |
|
21 | - */ |
|
22 | - public function __construct(string $string) |
|
23 | - { |
|
24 | - $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
25 | - parent::__construct($string); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param string $string |
|
21 | + */ |
|
22 | + public function __construct(string $string) |
|
23 | + { |
|
24 | + $this->_typeTag = self::TYPE_NUMERIC_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * |
|
30 | - * {@inheritdoc} |
|
31 | - */ |
|
32 | - protected function _validateString(string $string): bool |
|
33 | - { |
|
34 | - return preg_match('/[^0-9 ]/', $string) == 0; |
|
35 | - } |
|
28 | + /** |
|
29 | + * |
|
30 | + * {@inheritdoc} |
|
31 | + */ |
|
32 | + protected function _validateString(string $string): bool |
|
33 | + { |
|
34 | + return preg_match('/[^0-9 ]/', $string) == 0; |
|
35 | + } |
|
36 | 36 | } |
@@ -12,20 +12,20 @@ |
||
12 | 12 | */ |
13 | 13 | abstract class ContextSpecificTaggedType extends TaggedType |
14 | 14 | { |
15 | - /** |
|
16 | - * Wrapped element. |
|
17 | - * |
|
18 | - * @var \ASN1\Element $_element |
|
19 | - */ |
|
20 | - protected $_element; |
|
15 | + /** |
|
16 | + * Wrapped element. |
|
17 | + * |
|
18 | + * @var \ASN1\Element $_element |
|
19 | + */ |
|
20 | + protected $_element; |
|
21 | 21 | |
22 | - /** |
|
23 | - * |
|
24 | - * @see \ASN1\Element::typeClass() |
|
25 | - * @return int |
|
26 | - */ |
|
27 | - public function typeClass(): int |
|
28 | - { |
|
29 | - return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | - } |
|
22 | + /** |
|
23 | + * |
|
24 | + * @see \ASN1\Element::typeClass() |
|
25 | + * @return int |
|
26 | + */ |
|
27 | + public function typeClass(): int |
|
28 | + { |
|
29 | + return Identifier::CLASS_CONTEXT_SPECIFIC; |
|
30 | + } |
|
31 | 31 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | interface ExplicitTagging extends ElementBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Get explicitly tagged wrapped element. |
|
17 | - * |
|
18 | - * NOTE! Expectation checking is deprecated and shall be done |
|
19 | - * with UnspecifiedType. |
|
20 | - * |
|
21 | - * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * @return \ASN1\Type\UnspecifiedType |
|
24 | - */ |
|
25 | - public function explicit($expectedTag = null): UnspecifiedType; |
|
15 | + /** |
|
16 | + * Get explicitly tagged wrapped element. |
|
17 | + * |
|
18 | + * NOTE! Expectation checking is deprecated and shall be done |
|
19 | + * with UnspecifiedType. |
|
20 | + * |
|
21 | + * @param int|null $expectedTag Expected tag of the underlying type |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * @return \ASN1\Type\UnspecifiedType |
|
24 | + */ |
|
25 | + public function explicit($expectedTag = null): UnspecifiedType; |
|
26 | 26 | } |
@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | trait PrimitiveType |
11 | 11 | { |
12 | - /** |
|
13 | - * |
|
14 | - * @see \ASN1\Element::isConstructed() |
|
15 | - * @return boolean |
|
16 | - */ |
|
17 | - public function isConstructed(): bool |
|
18 | - { |
|
19 | - return false; |
|
20 | - } |
|
12 | + /** |
|
13 | + * |
|
14 | + * @see \ASN1\Element::isConstructed() |
|
15 | + * @return boolean |
|
16 | + */ |
|
17 | + public function isConstructed(): bool |
|
18 | + { |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | } |
@@ -11,130 +11,130 @@ |
||
11 | 11 | */ |
12 | 12 | class Flags |
13 | 13 | { |
14 | - /** |
|
15 | - * Flag octets. |
|
16 | - * |
|
17 | - * @var string $_flags |
|
18 | - */ |
|
19 | - protected $_flags; |
|
14 | + /** |
|
15 | + * Flag octets. |
|
16 | + * |
|
17 | + * @var string $_flags |
|
18 | + */ |
|
19 | + protected $_flags; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Number of flags. |
|
23 | - * |
|
24 | - * @var int $_width |
|
25 | - */ |
|
26 | - protected $_width; |
|
21 | + /** |
|
22 | + * Number of flags. |
|
23 | + * |
|
24 | + * @var int $_width |
|
25 | + */ |
|
26 | + protected $_width; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param number $flags Flags |
|
32 | - * @param int $width The number of flags. If width is larger than number of |
|
33 | - * bits in $flags, zeroes are prepended to flag field. |
|
34 | - */ |
|
35 | - public function __construct($flags, int $width) |
|
36 | - { |
|
37 | - if (!$width) { |
|
38 | - $this->_flags = ""; |
|
39 | - } else { |
|
40 | - // calculate number of unused bits in last octet |
|
41 | - $last_octet_bits = $width % 8; |
|
42 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | - $num = gmp_init($flags); |
|
44 | - // mask bits outside bitfield width |
|
45 | - $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | - $num &= $mask; |
|
47 | - // shift towards MSB if needed |
|
48 | - $data = gmp_export($num << $unused_bits, 1, |
|
49 | - GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | - $octets = unpack("C*", $data); |
|
51 | - $bits = count($octets) * 8; |
|
52 | - // pad with zeroes |
|
53 | - while ($bits < $width) { |
|
54 | - array_unshift($octets, 0); |
|
55 | - $bits += 8; |
|
56 | - } |
|
57 | - $this->_flags = pack("C*", ...$octets); |
|
58 | - } |
|
59 | - $this->_width = $width; |
|
60 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param number $flags Flags |
|
32 | + * @param int $width The number of flags. If width is larger than number of |
|
33 | + * bits in $flags, zeroes are prepended to flag field. |
|
34 | + */ |
|
35 | + public function __construct($flags, int $width) |
|
36 | + { |
|
37 | + if (!$width) { |
|
38 | + $this->_flags = ""; |
|
39 | + } else { |
|
40 | + // calculate number of unused bits in last octet |
|
41 | + $last_octet_bits = $width % 8; |
|
42 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
43 | + $num = gmp_init($flags); |
|
44 | + // mask bits outside bitfield width |
|
45 | + $mask = gmp_sub(gmp_init(1) << $width, 1); |
|
46 | + $num &= $mask; |
|
47 | + // shift towards MSB if needed |
|
48 | + $data = gmp_export($num << $unused_bits, 1, |
|
49 | + GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
50 | + $octets = unpack("C*", $data); |
|
51 | + $bits = count($octets) * 8; |
|
52 | + // pad with zeroes |
|
53 | + while ($bits < $width) { |
|
54 | + array_unshift($octets, 0); |
|
55 | + $bits += 8; |
|
56 | + } |
|
57 | + $this->_flags = pack("C*", ...$octets); |
|
58 | + } |
|
59 | + $this->_width = $width; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Initialize from BitString. |
|
64 | - * |
|
65 | - * @param BitString $bs |
|
66 | - * @param int $width |
|
67 | - * @return self |
|
68 | - */ |
|
69 | - public static function fromBitString(BitString $bs, int $width): self |
|
70 | - { |
|
71 | - $num_bits = $bs->numBits(); |
|
72 | - $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | - $num >>= $bs->unusedBits(); |
|
74 | - if ($num_bits < $width) { |
|
75 | - $num <<= ($width - $num_bits); |
|
76 | - } |
|
77 | - return new self(gmp_strval($num, 10), $width); |
|
78 | - } |
|
62 | + /** |
|
63 | + * Initialize from BitString. |
|
64 | + * |
|
65 | + * @param BitString $bs |
|
66 | + * @param int $width |
|
67 | + * @return self |
|
68 | + */ |
|
69 | + public static function fromBitString(BitString $bs, int $width): self |
|
70 | + { |
|
71 | + $num_bits = $bs->numBits(); |
|
72 | + $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
73 | + $num >>= $bs->unusedBits(); |
|
74 | + if ($num_bits < $width) { |
|
75 | + $num <<= ($width - $num_bits); |
|
76 | + } |
|
77 | + return new self(gmp_strval($num, 10), $width); |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check whether a bit at given index is set. |
|
82 | - * Index 0 is the leftmost bit. |
|
83 | - * |
|
84 | - * @param int $idx |
|
85 | - * @throws \OutOfBoundsException |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - public function test(int $idx): bool |
|
89 | - { |
|
90 | - if ($idx >= $this->_width) { |
|
91 | - throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | - } |
|
93 | - // octet index |
|
94 | - $oi = (int) floor($idx / 8); |
|
95 | - $byte = $this->_flags[$oi]; |
|
96 | - // bit index |
|
97 | - $bi = $idx % 8; |
|
98 | - // index 0 is the most significant bit in byte |
|
99 | - $mask = 0x01 << (7 - $bi); |
|
100 | - return (ord($byte) & $mask) > 0; |
|
101 | - } |
|
80 | + /** |
|
81 | + * Check whether a bit at given index is set. |
|
82 | + * Index 0 is the leftmost bit. |
|
83 | + * |
|
84 | + * @param int $idx |
|
85 | + * @throws \OutOfBoundsException |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + public function test(int $idx): bool |
|
89 | + { |
|
90 | + if ($idx >= $this->_width) { |
|
91 | + throw new \OutOfBoundsException("Index is out of bounds."); |
|
92 | + } |
|
93 | + // octet index |
|
94 | + $oi = (int) floor($idx / 8); |
|
95 | + $byte = $this->_flags[$oi]; |
|
96 | + // bit index |
|
97 | + $bi = $idx % 8; |
|
98 | + // index 0 is the most significant bit in byte |
|
99 | + $mask = 0x01 << (7 - $bi); |
|
100 | + return (ord($byte) & $mask) > 0; |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * Get flags as an octet string. |
|
105 | - * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | - * |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function string(): string |
|
110 | - { |
|
111 | - return $this->_flags; |
|
112 | - } |
|
103 | + /** |
|
104 | + * Get flags as an octet string. |
|
105 | + * Zeroes are appended to the last octet if width is not divisible by 8. |
|
106 | + * |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function string(): string |
|
110 | + { |
|
111 | + return $this->_flags; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Get flags as a base 10 integer. |
|
116 | - * |
|
117 | - * @return string Integer as a string |
|
118 | - */ |
|
119 | - public function number(): string |
|
120 | - { |
|
121 | - $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | - $last_octet_bits = $this->_width % 8; |
|
123 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | - $num >>= $unused_bits; |
|
125 | - return gmp_strval($num, 10); |
|
126 | - } |
|
114 | + /** |
|
115 | + * Get flags as a base 10 integer. |
|
116 | + * |
|
117 | + * @return string Integer as a string |
|
118 | + */ |
|
119 | + public function number(): string |
|
120 | + { |
|
121 | + $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
122 | + $last_octet_bits = $this->_width % 8; |
|
123 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
124 | + $num >>= $unused_bits; |
|
125 | + return gmp_strval($num, 10); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Get flags as a BitString. |
|
130 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
131 | - * |
|
132 | - * @return BitString |
|
133 | - */ |
|
134 | - public function bitString(): BitString |
|
135 | - { |
|
136 | - $last_octet_bits = $this->_width % 8; |
|
137 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
138 | - return new BitString($this->_flags, $unused_bits); |
|
139 | - } |
|
128 | + /** |
|
129 | + * Get flags as a BitString. |
|
130 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
131 | + * |
|
132 | + * @return BitString |
|
133 | + */ |
|
134 | + public function bitString(): BitString |
|
135 | + { |
|
136 | + $last_octet_bits = $this->_width % 8; |
|
137 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
138 | + return new BitString($this->_flags, $unused_bits); |
|
139 | + } |
|
140 | 140 | } |
@@ -12,79 +12,79 @@ |
||
12 | 12 | */ |
13 | 13 | interface ElementBase extends Encodable |
14 | 14 | { |
15 | - /** |
|
16 | - * Get the class of the ASN.1 type. |
|
17 | - * |
|
18 | - * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | - * |
|
20 | - * @return int |
|
21 | - */ |
|
22 | - public function typeClass(): int; |
|
15 | + /** |
|
16 | + * Get the class of the ASN.1 type. |
|
17 | + * |
|
18 | + * One of <code>Identifier::CLASS_*</code> constants. |
|
19 | + * |
|
20 | + * @return int |
|
21 | + */ |
|
22 | + public function typeClass(): int; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Check whether the element is constructed. |
|
26 | - * |
|
27 | - * Otherwise it's primitive. |
|
28 | - * |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public function isConstructed(): bool; |
|
24 | + /** |
|
25 | + * Check whether the element is constructed. |
|
26 | + * |
|
27 | + * Otherwise it's primitive. |
|
28 | + * |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public function isConstructed(): bool; |
|
32 | 32 | |
33 | - /** |
|
34 | - * Get the tag of the element. |
|
35 | - * |
|
36 | - * Interpretation of the tag depends on the context. For example it may |
|
37 | - * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | - * tagged type. |
|
39 | - * |
|
40 | - * @return int |
|
41 | - */ |
|
42 | - public function tag(): int; |
|
33 | + /** |
|
34 | + * Get the tag of the element. |
|
35 | + * |
|
36 | + * Interpretation of the tag depends on the context. For example it may |
|
37 | + * represent a universal type tag or a tag of an implicitly or explicitly |
|
38 | + * tagged type. |
|
39 | + * |
|
40 | + * @return int |
|
41 | + */ |
|
42 | + public function tag(): int; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Check whether the element is a type of a given tag. |
|
46 | - * |
|
47 | - * @param int $tag Type tag |
|
48 | - * @return boolean |
|
49 | - */ |
|
50 | - public function isType($tag): bool; |
|
44 | + /** |
|
45 | + * Check whether the element is a type of a given tag. |
|
46 | + * |
|
47 | + * @param int $tag Type tag |
|
48 | + * @return boolean |
|
49 | + */ |
|
50 | + public function isType($tag): bool; |
|
51 | 51 | |
52 | - /** |
|
53 | - * Check whether the element is a type of a given tag. |
|
54 | - * |
|
55 | - * Throws an exception if expectation fails. |
|
56 | - * |
|
57 | - * @param int $tag Type tag |
|
58 | - * @throws \UnexpectedValueException If the element type differs from the |
|
59 | - * expected |
|
60 | - * @return ElementBase |
|
61 | - */ |
|
62 | - public function expectType($tag): ElementBase; |
|
52 | + /** |
|
53 | + * Check whether the element is a type of a given tag. |
|
54 | + * |
|
55 | + * Throws an exception if expectation fails. |
|
56 | + * |
|
57 | + * @param int $tag Type tag |
|
58 | + * @throws \UnexpectedValueException If the element type differs from the |
|
59 | + * expected |
|
60 | + * @return ElementBase |
|
61 | + */ |
|
62 | + public function expectType($tag): ElementBase; |
|
63 | 63 | |
64 | - /** |
|
65 | - * Check whether the element is tagged (context specific). |
|
66 | - * |
|
67 | - * @return bool |
|
68 | - */ |
|
69 | - public function isTagged(): bool; |
|
64 | + /** |
|
65 | + * Check whether the element is tagged (context specific). |
|
66 | + * |
|
67 | + * @return bool |
|
68 | + */ |
|
69 | + public function isTagged(): bool; |
|
70 | 70 | |
71 | - /** |
|
72 | - * Check whether the element is tagged (context specific) and optionally has |
|
73 | - * a given tag. |
|
74 | - * |
|
75 | - * Throws an exception if the element is not tagged or tag differs from |
|
76 | - * the expected. |
|
77 | - * |
|
78 | - * @param int|null $tag Optional type tag |
|
79 | - * @throws \UnexpectedValueException If expectation fails |
|
80 | - * @return \ASN1\Type\TaggedType |
|
81 | - */ |
|
82 | - public function expectTagged($tag = null): TaggedType; |
|
71 | + /** |
|
72 | + * Check whether the element is tagged (context specific) and optionally has |
|
73 | + * a given tag. |
|
74 | + * |
|
75 | + * Throws an exception if the element is not tagged or tag differs from |
|
76 | + * the expected. |
|
77 | + * |
|
78 | + * @param int|null $tag Optional type tag |
|
79 | + * @throws \UnexpectedValueException If expectation fails |
|
80 | + * @return \ASN1\Type\TaggedType |
|
81 | + */ |
|
82 | + public function expectTagged($tag = null): TaggedType; |
|
83 | 83 | |
84 | - /** |
|
85 | - * Get the object as an abstract Element instance. |
|
86 | - * |
|
87 | - * @return \ASN1\Element |
|
88 | - */ |
|
89 | - public function asElement(): Element; |
|
84 | + /** |
|
85 | + * Get the object as an abstract Element instance. |
|
86 | + * |
|
87 | + * @return \ASN1\Element |
|
88 | + */ |
|
89 | + public function asElement(): Element; |
|
90 | 90 | } |
@@ -12,285 +12,285 @@ |
||
12 | 12 | */ |
13 | 13 | class Identifier implements Encodable |
14 | 14 | { |
15 | - // Type class enumerations |
|
16 | - const CLASS_UNIVERSAL = 0b00; |
|
17 | - const CLASS_APPLICATION = 0b01; |
|
18 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | - const CLASS_PRIVATE = 0b11; |
|
15 | + // Type class enumerations |
|
16 | + const CLASS_UNIVERSAL = 0b00; |
|
17 | + const CLASS_APPLICATION = 0b01; |
|
18 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
19 | + const CLASS_PRIVATE = 0b11; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Mapping from type class to human readable name. |
|
23 | - * |
|
24 | - * @internal |
|
25 | - * |
|
26 | - * @var array |
|
27 | - */ |
|
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | - self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | - self::CLASS_APPLICATION => "APPLICATION", |
|
31 | - self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | - self::CLASS_PRIVATE => "PRIVATE", |
|
33 | - /* @formatter:on */ |
|
34 | - ]; |
|
21 | + /** |
|
22 | + * Mapping from type class to human readable name. |
|
23 | + * |
|
24 | + * @internal |
|
25 | + * |
|
26 | + * @var array |
|
27 | + */ |
|
28 | + const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
29 | + self::CLASS_UNIVERSAL => "UNIVERSAL", |
|
30 | + self::CLASS_APPLICATION => "APPLICATION", |
|
31 | + self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
|
32 | + self::CLASS_PRIVATE => "PRIVATE", |
|
33 | + /* @formatter:on */ |
|
34 | + ]; |
|
35 | 35 | |
36 | - // P/C enumerations |
|
37 | - const PRIMITIVE = 0b0; |
|
38 | - const CONSTRUCTED = 0b1; |
|
36 | + // P/C enumerations |
|
37 | + const PRIMITIVE = 0b0; |
|
38 | + const CONSTRUCTED = 0b1; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Type class. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_class; |
|
40 | + /** |
|
41 | + * Type class. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_class; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Primitive or Constructed. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_pc; |
|
47 | + /** |
|
48 | + * Primitive or Constructed. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_pc; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Content type tag. |
|
56 | - * |
|
57 | - * @var int|string |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var int|string |
|
58 | + */ |
|
59 | + private $_tag; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param int $class Type class |
|
65 | - * @param int $pc Primitive / Constructed |
|
66 | - * @param int|string $tag Type tag number |
|
67 | - */ |
|
68 | - public function __construct(int $class, int $pc, $tag) |
|
69 | - { |
|
70 | - $this->_class = 0b11 & $class; |
|
71 | - $this->_pc = 0b1 & $pc; |
|
72 | - $this->_tag = $tag; |
|
73 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param int $class Type class |
|
65 | + * @param int $pc Primitive / Constructed |
|
66 | + * @param int|string $tag Type tag number |
|
67 | + */ |
|
68 | + public function __construct(int $class, int $pc, $tag) |
|
69 | + { |
|
70 | + $this->_class = 0b11 & $class; |
|
71 | + $this->_pc = 0b1 & $pc; |
|
72 | + $this->_tag = $tag; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param int|null $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. Variable is updated to |
|
81 | - * the offset next to the parsed identifier. If null, start from |
|
82 | - * offset 0. |
|
83 | - * @throws DecodeException If decoding fails |
|
84 | - * @return self |
|
85 | - */ |
|
86 | - public static function fromDER(string $data, int &$offset = null): self |
|
87 | - { |
|
88 | - $idx = $offset ? $offset : 0; |
|
89 | - $datalen = strlen($data); |
|
90 | - if ($idx >= $datalen) { |
|
91 | - throw new DecodeException("Invalid offset."); |
|
92 | - } |
|
93 | - $byte = ord($data[$idx++]); |
|
94 | - // bits 8 and 7 (class) |
|
95 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | - $class = (0b11000000 & $byte) >> 6; |
|
97 | - // bit 6 (0 = primitive / 1 = constructed) |
|
98 | - $pc = (0b00100000 & $byte) >> 5; |
|
99 | - // bits 5 to 1 (tag number) |
|
100 | - $tag = (0b00011111 & $byte); |
|
101 | - // long-form identifier |
|
102 | - if (0x1f == $tag) { |
|
103 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | - } |
|
105 | - if (isset($offset)) { |
|
106 | - $offset = $idx; |
|
107 | - } |
|
108 | - return new self($class, $pc, $tag); |
|
109 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param int|null $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. Variable is updated to |
|
81 | + * the offset next to the parsed identifier. If null, start from |
|
82 | + * offset 0. |
|
83 | + * @throws DecodeException If decoding fails |
|
84 | + * @return self |
|
85 | + */ |
|
86 | + public static function fromDER(string $data, int &$offset = null): self |
|
87 | + { |
|
88 | + $idx = $offset ? $offset : 0; |
|
89 | + $datalen = strlen($data); |
|
90 | + if ($idx >= $datalen) { |
|
91 | + throw new DecodeException("Invalid offset."); |
|
92 | + } |
|
93 | + $byte = ord($data[$idx++]); |
|
94 | + // bits 8 and 7 (class) |
|
95 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
96 | + $class = (0b11000000 & $byte) >> 6; |
|
97 | + // bit 6 (0 = primitive / 1 = constructed) |
|
98 | + $pc = (0b00100000 & $byte) >> 5; |
|
99 | + // bits 5 to 1 (tag number) |
|
100 | + $tag = (0b00011111 & $byte); |
|
101 | + // long-form identifier |
|
102 | + if (0x1f == $tag) { |
|
103 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
104 | + } |
|
105 | + if (isset($offset)) { |
|
106 | + $offset = $idx; |
|
107 | + } |
|
108 | + return new self($class, $pc, $tag); |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * Parse long form tag. |
|
113 | - * |
|
114 | - * @param string $data DER data |
|
115 | - * @param int $offset Reference to the variable containing offset to data |
|
116 | - * @throws DecodeException If decoding fails |
|
117 | - * @return string Tag number |
|
118 | - */ |
|
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | - { |
|
121 | - $datalen = strlen($data); |
|
122 | - $tag = gmp_init(0, 10); |
|
123 | - while (true) { |
|
124 | - if ($offset >= $datalen) { |
|
125 | - throw new DecodeException( |
|
126 | - "Unexpected end of data while decoding" . |
|
127 | - " long form identifier."); |
|
128 | - } |
|
129 | - $byte = ord($data[$offset++]); |
|
130 | - $tag <<= 7; |
|
131 | - $tag |= 0x7f & $byte; |
|
132 | - // last byte has bit 8 set to zero |
|
133 | - if (!(0x80 & $byte)) { |
|
134 | - break; |
|
135 | - } |
|
136 | - } |
|
137 | - return gmp_strval($tag, 10); |
|
138 | - } |
|
111 | + /** |
|
112 | + * Parse long form tag. |
|
113 | + * |
|
114 | + * @param string $data DER data |
|
115 | + * @param int $offset Reference to the variable containing offset to data |
|
116 | + * @throws DecodeException If decoding fails |
|
117 | + * @return string Tag number |
|
118 | + */ |
|
119 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
120 | + { |
|
121 | + $datalen = strlen($data); |
|
122 | + $tag = gmp_init(0, 10); |
|
123 | + while (true) { |
|
124 | + if ($offset >= $datalen) { |
|
125 | + throw new DecodeException( |
|
126 | + "Unexpected end of data while decoding" . |
|
127 | + " long form identifier."); |
|
128 | + } |
|
129 | + $byte = ord($data[$offset++]); |
|
130 | + $tag <<= 7; |
|
131 | + $tag |= 0x7f & $byte; |
|
132 | + // last byte has bit 8 set to zero |
|
133 | + if (!(0x80 & $byte)) { |
|
134 | + break; |
|
135 | + } |
|
136 | + } |
|
137 | + return gmp_strval($tag, 10); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * |
|
142 | - * @see Encodable::toDER() |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function toDER(): string |
|
146 | - { |
|
147 | - $bytes = []; |
|
148 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | - $tag = gmp_init($this->_tag, 10); |
|
150 | - if ($tag < 0x1f) { |
|
151 | - $bytes[] = $byte | $tag; |
|
152 | - } else { // long-form identifier |
|
153 | - $bytes[] = $byte | 0x1f; |
|
154 | - $octets = []; |
|
155 | - for (; $tag > 0; $tag >>= 7) { |
|
156 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | - } |
|
158 | - // last octet has bit 8 set to zero |
|
159 | - $octets[0] &= 0x7f; |
|
160 | - foreach (array_reverse($octets) as $octet) { |
|
161 | - $bytes[] = $octet; |
|
162 | - } |
|
163 | - } |
|
164 | - return pack("C*", ...$bytes); |
|
165 | - } |
|
140 | + /** |
|
141 | + * |
|
142 | + * @see Encodable::toDER() |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function toDER(): string |
|
146 | + { |
|
147 | + $bytes = []; |
|
148 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
149 | + $tag = gmp_init($this->_tag, 10); |
|
150 | + if ($tag < 0x1f) { |
|
151 | + $bytes[] = $byte | $tag; |
|
152 | + } else { // long-form identifier |
|
153 | + $bytes[] = $byte | 0x1f; |
|
154 | + $octets = []; |
|
155 | + for (; $tag > 0; $tag >>= 7) { |
|
156 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
157 | + } |
|
158 | + // last octet has bit 8 set to zero |
|
159 | + $octets[0] &= 0x7f; |
|
160 | + foreach (array_reverse($octets) as $octet) { |
|
161 | + $bytes[] = $octet; |
|
162 | + } |
|
163 | + } |
|
164 | + return pack("C*", ...$bytes); |
|
165 | + } |
|
166 | 166 | |
167 | - /** |
|
168 | - * Get class of the type. |
|
169 | - * |
|
170 | - * @return int |
|
171 | - */ |
|
172 | - public function typeClass(): int |
|
173 | - { |
|
174 | - return $this->_class; |
|
175 | - } |
|
167 | + /** |
|
168 | + * Get class of the type. |
|
169 | + * |
|
170 | + * @return int |
|
171 | + */ |
|
172 | + public function typeClass(): int |
|
173 | + { |
|
174 | + return $this->_class; |
|
175 | + } |
|
176 | 176 | |
177 | - /** |
|
178 | - * Get P/C. |
|
179 | - * |
|
180 | - * @return int |
|
181 | - */ |
|
182 | - public function pc(): int |
|
183 | - { |
|
184 | - return $this->_pc; |
|
185 | - } |
|
177 | + /** |
|
178 | + * Get P/C. |
|
179 | + * |
|
180 | + * @return int |
|
181 | + */ |
|
182 | + public function pc(): int |
|
183 | + { |
|
184 | + return $this->_pc; |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Get the tag number. |
|
189 | - * |
|
190 | - * @return int|string |
|
191 | - */ |
|
192 | - public function tag() |
|
193 | - { |
|
194 | - return $this->_tag; |
|
195 | - } |
|
187 | + /** |
|
188 | + * Get the tag number. |
|
189 | + * |
|
190 | + * @return int|string |
|
191 | + */ |
|
192 | + public function tag() |
|
193 | + { |
|
194 | + return $this->_tag; |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Check whether type is of an universal class. |
|
199 | - * |
|
200 | - * @return boolean |
|
201 | - */ |
|
202 | - public function isUniversal(): bool |
|
203 | - { |
|
204 | - return self::CLASS_UNIVERSAL == $this->_class; |
|
205 | - } |
|
197 | + /** |
|
198 | + * Check whether type is of an universal class. |
|
199 | + * |
|
200 | + * @return boolean |
|
201 | + */ |
|
202 | + public function isUniversal(): bool |
|
203 | + { |
|
204 | + return self::CLASS_UNIVERSAL == $this->_class; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Check whether type is of an application class. |
|
209 | - * |
|
210 | - * @return boolean |
|
211 | - */ |
|
212 | - public function isApplication(): bool |
|
213 | - { |
|
214 | - return self::CLASS_APPLICATION == $this->_class; |
|
215 | - } |
|
207 | + /** |
|
208 | + * Check whether type is of an application class. |
|
209 | + * |
|
210 | + * @return boolean |
|
211 | + */ |
|
212 | + public function isApplication(): bool |
|
213 | + { |
|
214 | + return self::CLASS_APPLICATION == $this->_class; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Check whether type is of a context specific class. |
|
219 | - * |
|
220 | - * @return boolean |
|
221 | - */ |
|
222 | - public function isContextSpecific(): bool |
|
223 | - { |
|
224 | - return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
225 | - } |
|
217 | + /** |
|
218 | + * Check whether type is of a context specific class. |
|
219 | + * |
|
220 | + * @return boolean |
|
221 | + */ |
|
222 | + public function isContextSpecific(): bool |
|
223 | + { |
|
224 | + return self::CLASS_CONTEXT_SPECIFIC == $this->_class; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * Check whether type is of a private class. |
|
229 | - * |
|
230 | - * @return boolean |
|
231 | - */ |
|
232 | - public function isPrivate(): bool |
|
233 | - { |
|
234 | - return self::CLASS_PRIVATE == $this->_class; |
|
235 | - } |
|
227 | + /** |
|
228 | + * Check whether type is of a private class. |
|
229 | + * |
|
230 | + * @return boolean |
|
231 | + */ |
|
232 | + public function isPrivate(): bool |
|
233 | + { |
|
234 | + return self::CLASS_PRIVATE == $this->_class; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Check whether content is primitive type. |
|
239 | - * |
|
240 | - * @return boolean |
|
241 | - */ |
|
242 | - public function isPrimitive(): bool |
|
243 | - { |
|
244 | - return self::PRIMITIVE == $this->_pc; |
|
245 | - } |
|
237 | + /** |
|
238 | + * Check whether content is primitive type. |
|
239 | + * |
|
240 | + * @return boolean |
|
241 | + */ |
|
242 | + public function isPrimitive(): bool |
|
243 | + { |
|
244 | + return self::PRIMITIVE == $this->_pc; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * Check hether content is constructed type. |
|
249 | - * |
|
250 | - * @return boolean |
|
251 | - */ |
|
252 | - public function isConstructed(): bool |
|
253 | - { |
|
254 | - return self::CONSTRUCTED == $this->_pc; |
|
255 | - } |
|
247 | + /** |
|
248 | + * Check hether content is constructed type. |
|
249 | + * |
|
250 | + * @return boolean |
|
251 | + */ |
|
252 | + public function isConstructed(): bool |
|
253 | + { |
|
254 | + return self::CONSTRUCTED == $this->_pc; |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Get self with given type class. |
|
259 | - * |
|
260 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
261 | - * @return self |
|
262 | - */ |
|
263 | - public function withClass(int $class): self |
|
264 | - { |
|
265 | - $obj = clone $this; |
|
266 | - $obj->_class = $class; |
|
267 | - return $obj; |
|
268 | - } |
|
257 | + /** |
|
258 | + * Get self with given type class. |
|
259 | + * |
|
260 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
261 | + * @return self |
|
262 | + */ |
|
263 | + public function withClass(int $class): self |
|
264 | + { |
|
265 | + $obj = clone $this; |
|
266 | + $obj->_class = $class; |
|
267 | + return $obj; |
|
268 | + } |
|
269 | 269 | |
270 | - /** |
|
271 | - * Get self with given type tag. |
|
272 | - * |
|
273 | - * @param int|string $tag Tag number |
|
274 | - * @return self |
|
275 | - */ |
|
276 | - public function withTag(int $tag): self |
|
277 | - { |
|
278 | - $obj = clone $this; |
|
279 | - $obj->_tag = $tag; |
|
280 | - return $obj; |
|
281 | - } |
|
270 | + /** |
|
271 | + * Get self with given type tag. |
|
272 | + * |
|
273 | + * @param int|string $tag Tag number |
|
274 | + * @return self |
|
275 | + */ |
|
276 | + public function withTag(int $tag): self |
|
277 | + { |
|
278 | + $obj = clone $this; |
|
279 | + $obj->_tag = $tag; |
|
280 | + return $obj; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Get human readable name of the type class. |
|
285 | - * |
|
286 | - * @param int $class |
|
287 | - * @return string |
|
288 | - */ |
|
289 | - public static function classToName(int $class): string |
|
290 | - { |
|
291 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
292 | - return "CLASS $class"; |
|
293 | - } |
|
294 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
295 | - } |
|
283 | + /** |
|
284 | + * Get human readable name of the type class. |
|
285 | + * |
|
286 | + * @param int $class |
|
287 | + * @return string |
|
288 | + */ |
|
289 | + public static function classToName(int $class): string |
|
290 | + { |
|
291 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
292 | + return "CLASS $class"; |
|
293 | + } |
|
294 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
295 | + } |
|
296 | 296 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | * |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - const MAP_CLASS_TO_NAME = [ /* @formatter:off */ |
|
28 | + const MAP_CLASS_TO_NAME = [/* @formatter:off */ |
|
29 | 29 | self::CLASS_UNIVERSAL => "UNIVERSAL", |
30 | 30 | self::CLASS_APPLICATION => "APPLICATION", |
31 | 31 | self::CLASS_CONTEXT_SPECIFIC => "CONTEXT SPECIFIC", |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | * @throws DecodeException If decoding fails |
84 | 84 | * @return self |
85 | 85 | */ |
86 | - public static function fromDER(string $data, int &$offset = null): self |
|
86 | + public static function fromDER(string $data, int & $offset = null): self |
|
87 | 87 | { |
88 | 88 | $idx = $offset ? $offset : 0; |
89 | 89 | $datalen = strlen($data); |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * @throws DecodeException If decoding fails |
117 | 117 | * @return string Tag number |
118 | 118 | */ |
119 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
119 | + private static function _decodeLongFormTag(string $data, int & $offset): string |
|
120 | 120 | { |
121 | 121 | $datalen = strlen($data); |
122 | 122 | $tag = gmp_init(0, 10); |