@@ -9,13 +9,13 @@ |
||
9 | 9 | */ |
10 | 10 | trait PrimitiveType |
11 | 11 | { |
12 | - /** |
|
13 | - * @see \Sop\ASN1\Feature\ElementBase::isConstructed() |
|
14 | - * |
|
15 | - * @return bool |
|
16 | - */ |
|
17 | - public function isConstructed(): bool |
|
18 | - { |
|
19 | - return false; |
|
20 | - } |
|
12 | + /** |
|
13 | + * @see \Sop\ASN1\Feature\ElementBase::isConstructed() |
|
14 | + * |
|
15 | + * @return bool |
|
16 | + */ |
|
17 | + public function isConstructed(): bool |
|
18 | + { |
|
19 | + return false; |
|
20 | + } |
|
21 | 21 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type; |
6 | 6 |
@@ -14,79 +14,79 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class TaggedType extends Element |
16 | 16 | { |
17 | - /** |
|
18 | - * Check whether element supports explicit tagging. |
|
19 | - * |
|
20 | - * @param null|int $expectedTag Optional outer tag expectation |
|
21 | - * |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * |
|
24 | - * @return ExplicitTagging |
|
25 | - */ |
|
26 | - public function expectExplicit(?int $expectedTag = null): ExplicitTagging |
|
27 | - { |
|
28 | - $el = $this; |
|
29 | - if (!$el instanceof ExplicitTagging) { |
|
30 | - throw new \UnexpectedValueException( |
|
31 | - "Element doesn't implement explicit tagging."); |
|
32 | - } |
|
33 | - if (isset($expectedTag)) { |
|
34 | - $el->expectTagged($expectedTag); |
|
35 | - } |
|
36 | - return $el; |
|
37 | - } |
|
17 | + /** |
|
18 | + * Check whether element supports explicit tagging. |
|
19 | + * |
|
20 | + * @param null|int $expectedTag Optional outer tag expectation |
|
21 | + * |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * |
|
24 | + * @return ExplicitTagging |
|
25 | + */ |
|
26 | + public function expectExplicit(?int $expectedTag = null): ExplicitTagging |
|
27 | + { |
|
28 | + $el = $this; |
|
29 | + if (!$el instanceof ExplicitTagging) { |
|
30 | + throw new \UnexpectedValueException( |
|
31 | + "Element doesn't implement explicit tagging."); |
|
32 | + } |
|
33 | + if (isset($expectedTag)) { |
|
34 | + $el->expectTagged($expectedTag); |
|
35 | + } |
|
36 | + return $el; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Get the wrapped inner element employing explicit tagging. |
|
41 | - * |
|
42 | - * @param null|int $expectedTag Optional outer tag expectation |
|
43 | - * |
|
44 | - * @throws \UnexpectedValueException If expectation fails |
|
45 | - * |
|
46 | - * @return UnspecifiedType |
|
47 | - */ |
|
48 | - public function asExplicit(?int $expectedTag = null): UnspecifiedType |
|
49 | - { |
|
50 | - return $this->expectExplicit($expectedTag)->explicit(); |
|
51 | - } |
|
39 | + /** |
|
40 | + * Get the wrapped inner element employing explicit tagging. |
|
41 | + * |
|
42 | + * @param null|int $expectedTag Optional outer tag expectation |
|
43 | + * |
|
44 | + * @throws \UnexpectedValueException If expectation fails |
|
45 | + * |
|
46 | + * @return UnspecifiedType |
|
47 | + */ |
|
48 | + public function asExplicit(?int $expectedTag = null): UnspecifiedType |
|
49 | + { |
|
50 | + return $this->expectExplicit($expectedTag)->explicit(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Check whether element supports implicit tagging. |
|
55 | - * |
|
56 | - * @param null|int $expectedTag Optional outer tag expectation |
|
57 | - * |
|
58 | - * @throws \UnexpectedValueException If expectation fails |
|
59 | - * |
|
60 | - * @return ImplicitTagging |
|
61 | - */ |
|
62 | - public function expectImplicit(?int $expectedTag = null): ImplicitTagging |
|
63 | - { |
|
64 | - $el = $this; |
|
65 | - if (!$el instanceof ImplicitTagging) { |
|
66 | - throw new \UnexpectedValueException( |
|
67 | - "Element doesn't implement implicit tagging."); |
|
68 | - } |
|
69 | - if (isset($expectedTag)) { |
|
70 | - $el->expectTagged($expectedTag); |
|
71 | - } |
|
72 | - return $el; |
|
73 | - } |
|
53 | + /** |
|
54 | + * Check whether element supports implicit tagging. |
|
55 | + * |
|
56 | + * @param null|int $expectedTag Optional outer tag expectation |
|
57 | + * |
|
58 | + * @throws \UnexpectedValueException If expectation fails |
|
59 | + * |
|
60 | + * @return ImplicitTagging |
|
61 | + */ |
|
62 | + public function expectImplicit(?int $expectedTag = null): ImplicitTagging |
|
63 | + { |
|
64 | + $el = $this; |
|
65 | + if (!$el instanceof ImplicitTagging) { |
|
66 | + throw new \UnexpectedValueException( |
|
67 | + "Element doesn't implement implicit tagging."); |
|
68 | + } |
|
69 | + if (isset($expectedTag)) { |
|
70 | + $el->expectTagged($expectedTag); |
|
71 | + } |
|
72 | + return $el; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Get the wrapped inner element employing implicit tagging. |
|
77 | - * |
|
78 | - * @param int $tag Type tag of the inner element |
|
79 | - * @param null|int $expectedTag Optional outer tag expectation |
|
80 | - * @param int $expectedClass Optional inner type class expectation |
|
81 | - * |
|
82 | - * @throws \UnexpectedValueException If expectation fails |
|
83 | - * |
|
84 | - * @return UnspecifiedType |
|
85 | - */ |
|
86 | - public function asImplicit(int $tag, ?int $expectedTag = null, |
|
87 | - int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
88 | - { |
|
89 | - return $this->expectImplicit($expectedTag)->implicit($tag, |
|
90 | - $expectedClass); |
|
91 | - } |
|
75 | + /** |
|
76 | + * Get the wrapped inner element employing implicit tagging. |
|
77 | + * |
|
78 | + * @param int $tag Type tag of the inner element |
|
79 | + * @param null|int $expectedTag Optional outer tag expectation |
|
80 | + * @param int $expectedClass Optional inner type class expectation |
|
81 | + * |
|
82 | + * @throws \UnexpectedValueException If expectation fails |
|
83 | + * |
|
84 | + * @return UnspecifiedType |
|
85 | + */ |
|
86 | + public function asImplicit(int $tag, ?int $expectedTag = null, |
|
87 | + int $expectedClass = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
88 | + { |
|
89 | + return $this->expectImplicit($expectedTag)->implicit($tag, |
|
90 | + $expectedClass); |
|
91 | + } |
|
92 | 92 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Util; |
6 | 6 |
@@ -9,107 +9,107 @@ |
||
9 | 9 | */ |
10 | 10 | class BigInt |
11 | 11 | { |
12 | - /** |
|
13 | - * Number as a base10 integer string. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - private $_num; |
|
12 | + /** |
|
13 | + * Number as a base10 integer string. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + private $_num; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Number as an integer type. |
|
21 | - * |
|
22 | - * @internal Lazily initialized |
|
23 | - * |
|
24 | - * @var null|int |
|
25 | - */ |
|
26 | - private $_intNum; |
|
19 | + /** |
|
20 | + * Number as an integer type. |
|
21 | + * |
|
22 | + * @internal Lazily initialized |
|
23 | + * |
|
24 | + * @var null|int |
|
25 | + */ |
|
26 | + private $_intNum; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param int|string $num |
|
32 | - */ |
|
33 | - public function __construct($num) |
|
34 | - { |
|
35 | - $this->_num = strval($num); |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param int|string $num |
|
32 | + */ |
|
33 | + public function __construct($num) |
|
34 | + { |
|
35 | + $this->_num = strval($num); |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return string |
|
40 | - */ |
|
41 | - public function __toString() |
|
42 | - { |
|
43 | - return $this->base10(); |
|
44 | - } |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + */ |
|
41 | + public function __toString() |
|
42 | + { |
|
43 | + return $this->base10(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Get the number as a base10 integer string. |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public function base10(): string |
|
52 | - { |
|
53 | - return $this->_num; |
|
54 | - } |
|
46 | + /** |
|
47 | + * Get the number as a base10 integer string. |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public function base10(): string |
|
52 | + { |
|
53 | + return $this->_num; |
|
54 | + } |
|
55 | 55 | |
56 | - /** |
|
57 | - * Get the number as an integer. |
|
58 | - * |
|
59 | - * @throws \RuntimeException If number overflows integer size |
|
60 | - * |
|
61 | - * @return int |
|
62 | - */ |
|
63 | - public function intVal(): int |
|
64 | - { |
|
65 | - if (!isset($this->_intNum)) { |
|
66 | - $num = gmp_init($this->_num, 10); |
|
67 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
68 | - throw new \RuntimeException('Integer overflow.'); |
|
69 | - } |
|
70 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
71 | - throw new \RuntimeException('Integer underflow.'); |
|
72 | - } |
|
73 | - $this->_intNum = gmp_intval($num); |
|
74 | - } |
|
75 | - return $this->_intNum; |
|
76 | - } |
|
56 | + /** |
|
57 | + * Get the number as an integer. |
|
58 | + * |
|
59 | + * @throws \RuntimeException If number overflows integer size |
|
60 | + * |
|
61 | + * @return int |
|
62 | + */ |
|
63 | + public function intVal(): int |
|
64 | + { |
|
65 | + if (!isset($this->_intNum)) { |
|
66 | + $num = gmp_init($this->_num, 10); |
|
67 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
68 | + throw new \RuntimeException('Integer overflow.'); |
|
69 | + } |
|
70 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
71 | + throw new \RuntimeException('Integer underflow.'); |
|
72 | + } |
|
73 | + $this->_intNum = gmp_intval($num); |
|
74 | + } |
|
75 | + return $this->_intNum; |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Get the number as a GMP object. |
|
80 | - * |
|
81 | - * @return \GMP |
|
82 | - */ |
|
83 | - public function gmpObj(): \GMP |
|
84 | - { |
|
85 | - return gmp_init($this->_num, 10); |
|
86 | - } |
|
78 | + /** |
|
79 | + * Get the number as a GMP object. |
|
80 | + * |
|
81 | + * @return \GMP |
|
82 | + */ |
|
83 | + public function gmpObj(): \GMP |
|
84 | + { |
|
85 | + return gmp_init($this->_num, 10); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get the maximum integer value. |
|
90 | - * |
|
91 | - * @return \GMP |
|
92 | - */ |
|
93 | - private function _intMaxGmp(): \GMP |
|
94 | - { |
|
95 | - static $gmp; |
|
96 | - if (!isset($gmp)) { |
|
97 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
98 | - } |
|
99 | - return $gmp; |
|
100 | - } |
|
88 | + /** |
|
89 | + * Get the maximum integer value. |
|
90 | + * |
|
91 | + * @return \GMP |
|
92 | + */ |
|
93 | + private function _intMaxGmp(): \GMP |
|
94 | + { |
|
95 | + static $gmp; |
|
96 | + if (!isset($gmp)) { |
|
97 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
98 | + } |
|
99 | + return $gmp; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Get the minimum integer value. |
|
104 | - * |
|
105 | - * @return \GMP |
|
106 | - */ |
|
107 | - private function _intMinGmp(): \GMP |
|
108 | - { |
|
109 | - static $gmp; |
|
110 | - if (!isset($gmp)) { |
|
111 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
112 | - } |
|
113 | - return $gmp; |
|
114 | - } |
|
102 | + /** |
|
103 | + * Get the minimum integer value. |
|
104 | + * |
|
105 | + * @return \GMP |
|
106 | + */ |
|
107 | + private function _intMinGmp(): \GMP |
|
108 | + { |
|
109 | + static $gmp; |
|
110 | + if (!isset($gmp)) { |
|
111 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
112 | + } |
|
113 | + return $gmp; |
|
114 | + } |
|
115 | 115 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Component; |
6 | 6 |
@@ -13,301 +13,301 @@ |
||
13 | 13 | */ |
14 | 14 | class Identifier implements Encodable |
15 | 15 | { |
16 | - // Type class enumerations |
|
17 | - const CLASS_UNIVERSAL = 0b00; |
|
18 | - const CLASS_APPLICATION = 0b01; |
|
19 | - const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | - const CLASS_PRIVATE = 0b11; |
|
16 | + // Type class enumerations |
|
17 | + const CLASS_UNIVERSAL = 0b00; |
|
18 | + const CLASS_APPLICATION = 0b01; |
|
19 | + const CLASS_CONTEXT_SPECIFIC = 0b10; |
|
20 | + const CLASS_PRIVATE = 0b11; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Mapping from type class to human readable name. |
|
24 | - * |
|
25 | - * @internal |
|
26 | - * |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - const MAP_CLASS_TO_NAME = [ |
|
30 | - self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | - self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | - self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | - self::CLASS_PRIVATE => 'PRIVATE', |
|
34 | - ]; |
|
22 | + /** |
|
23 | + * Mapping from type class to human readable name. |
|
24 | + * |
|
25 | + * @internal |
|
26 | + * |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + const MAP_CLASS_TO_NAME = [ |
|
30 | + self::CLASS_UNIVERSAL => 'UNIVERSAL', |
|
31 | + self::CLASS_APPLICATION => 'APPLICATION', |
|
32 | + self::CLASS_CONTEXT_SPECIFIC => 'CONTEXT SPECIFIC', |
|
33 | + self::CLASS_PRIVATE => 'PRIVATE', |
|
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 BigInt |
|
58 | - */ |
|
59 | - private $_tag; |
|
54 | + /** |
|
55 | + * Content type tag. |
|
56 | + * |
|
57 | + * @var BigInt |
|
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 = new BigInt($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 = new BigInt($tag); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Decode identifier component from DER data. |
|
77 | - * |
|
78 | - * @param string $data DER encoded data |
|
79 | - * @param null|int $offset Reference to the variable that contains offset |
|
80 | - * into the data where to start parsing. |
|
81 | - * Variable is updated to the offset next to the |
|
82 | - * parsed identifier. If null, start from offset 0. |
|
83 | - * |
|
84 | - * @throws DecodeException If decoding fails |
|
85 | - * |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | - { |
|
90 | - $idx = $offset ?? 0; |
|
91 | - $datalen = strlen($data); |
|
92 | - if ($idx >= $datalen) { |
|
93 | - throw new DecodeException('Invalid offset.'); |
|
94 | - } |
|
95 | - $byte = ord($data[$idx++]); |
|
96 | - // bits 8 and 7 (class) |
|
97 | - // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | - $class = (0b11000000 & $byte) >> 6; |
|
99 | - // bit 6 (0 = primitive / 1 = constructed) |
|
100 | - $pc = (0b00100000 & $byte) >> 5; |
|
101 | - // bits 5 to 1 (tag number) |
|
102 | - $tag = (0b00011111 & $byte); |
|
103 | - // long-form identifier |
|
104 | - if (0x1f === $tag) { |
|
105 | - $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | - } |
|
107 | - if (isset($offset)) { |
|
108 | - $offset = $idx; |
|
109 | - } |
|
110 | - return new self($class, $pc, $tag); |
|
111 | - } |
|
75 | + /** |
|
76 | + * Decode identifier component from DER data. |
|
77 | + * |
|
78 | + * @param string $data DER encoded data |
|
79 | + * @param null|int $offset Reference to the variable that contains offset |
|
80 | + * into the data where to start parsing. |
|
81 | + * Variable is updated to the offset next to the |
|
82 | + * parsed identifier. If null, start from offset 0. |
|
83 | + * |
|
84 | + * @throws DecodeException If decoding fails |
|
85 | + * |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public static function fromDER(string $data, int &$offset = null): Identifier |
|
89 | + { |
|
90 | + $idx = $offset ?? 0; |
|
91 | + $datalen = strlen($data); |
|
92 | + if ($idx >= $datalen) { |
|
93 | + throw new DecodeException('Invalid offset.'); |
|
94 | + } |
|
95 | + $byte = ord($data[$idx++]); |
|
96 | + // bits 8 and 7 (class) |
|
97 | + // 0 = universal, 1 = application, 2 = context-specific, 3 = private |
|
98 | + $class = (0b11000000 & $byte) >> 6; |
|
99 | + // bit 6 (0 = primitive / 1 = constructed) |
|
100 | + $pc = (0b00100000 & $byte) >> 5; |
|
101 | + // bits 5 to 1 (tag number) |
|
102 | + $tag = (0b00011111 & $byte); |
|
103 | + // long-form identifier |
|
104 | + if (0x1f === $tag) { |
|
105 | + $tag = self::_decodeLongFormTag($data, $idx); |
|
106 | + } |
|
107 | + if (isset($offset)) { |
|
108 | + $offset = $idx; |
|
109 | + } |
|
110 | + return new self($class, $pc, $tag); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * {@inheritdoc} |
|
115 | - */ |
|
116 | - public function toDER(): string |
|
117 | - { |
|
118 | - $bytes = []; |
|
119 | - $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | - $tag = $this->_tag->gmpObj(); |
|
121 | - if ($tag < 0x1f) { |
|
122 | - $bytes[] = $byte | $tag; |
|
123 | - } |
|
124 | - // long-form identifier |
|
125 | - else { |
|
126 | - $bytes[] = $byte | 0x1f; |
|
127 | - $octets = []; |
|
128 | - for (; $tag > 0; $tag >>= 7) { |
|
129 | - array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | - } |
|
131 | - // last octet has bit 8 set to zero |
|
132 | - $octets[0] &= 0x7f; |
|
133 | - foreach (array_reverse($octets) as $octet) { |
|
134 | - $bytes[] = $octet; |
|
135 | - } |
|
136 | - } |
|
137 | - return pack('C*', ...$bytes); |
|
138 | - } |
|
113 | + /** |
|
114 | + * {@inheritdoc} |
|
115 | + */ |
|
116 | + public function toDER(): string |
|
117 | + { |
|
118 | + $bytes = []; |
|
119 | + $byte = $this->_class << 6 | $this->_pc << 5; |
|
120 | + $tag = $this->_tag->gmpObj(); |
|
121 | + if ($tag < 0x1f) { |
|
122 | + $bytes[] = $byte | $tag; |
|
123 | + } |
|
124 | + // long-form identifier |
|
125 | + else { |
|
126 | + $bytes[] = $byte | 0x1f; |
|
127 | + $octets = []; |
|
128 | + for (; $tag > 0; $tag >>= 7) { |
|
129 | + array_push($octets, gmp_intval(0x80 | ($tag & 0x7f))); |
|
130 | + } |
|
131 | + // last octet has bit 8 set to zero |
|
132 | + $octets[0] &= 0x7f; |
|
133 | + foreach (array_reverse($octets) as $octet) { |
|
134 | + $bytes[] = $octet; |
|
135 | + } |
|
136 | + } |
|
137 | + return pack('C*', ...$bytes); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Get class of the type. |
|
142 | - * |
|
143 | - * @return int |
|
144 | - */ |
|
145 | - public function typeClass(): int |
|
146 | - { |
|
147 | - return $this->_class; |
|
148 | - } |
|
140 | + /** |
|
141 | + * Get class of the type. |
|
142 | + * |
|
143 | + * @return int |
|
144 | + */ |
|
145 | + public function typeClass(): int |
|
146 | + { |
|
147 | + return $this->_class; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Get P/C. |
|
152 | - * |
|
153 | - * @return int |
|
154 | - */ |
|
155 | - public function pc(): int |
|
156 | - { |
|
157 | - return $this->_pc; |
|
158 | - } |
|
150 | + /** |
|
151 | + * Get P/C. |
|
152 | + * |
|
153 | + * @return int |
|
154 | + */ |
|
155 | + public function pc(): int |
|
156 | + { |
|
157 | + return $this->_pc; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Get the tag number. |
|
162 | - * |
|
163 | - * @return string Base 10 integer string |
|
164 | - */ |
|
165 | - public function tag(): string |
|
166 | - { |
|
167 | - return $this->_tag->base10(); |
|
168 | - } |
|
160 | + /** |
|
161 | + * Get the tag number. |
|
162 | + * |
|
163 | + * @return string Base 10 integer string |
|
164 | + */ |
|
165 | + public function tag(): string |
|
166 | + { |
|
167 | + return $this->_tag->base10(); |
|
168 | + } |
|
169 | 169 | |
170 | - /** |
|
171 | - * Get the tag as an integer. |
|
172 | - * |
|
173 | - * @return int |
|
174 | - */ |
|
175 | - public function intTag(): int |
|
176 | - { |
|
177 | - return $this->_tag->intVal(); |
|
178 | - } |
|
170 | + /** |
|
171 | + * Get the tag as an integer. |
|
172 | + * |
|
173 | + * @return int |
|
174 | + */ |
|
175 | + public function intTag(): int |
|
176 | + { |
|
177 | + return $this->_tag->intVal(); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * Check whether type is of an universal class. |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function isUniversal(): bool |
|
186 | - { |
|
187 | - return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | - } |
|
180 | + /** |
|
181 | + * Check whether type is of an universal class. |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function isUniversal(): bool |
|
186 | + { |
|
187 | + return self::CLASS_UNIVERSAL === $this->_class; |
|
188 | + } |
|
189 | 189 | |
190 | - /** |
|
191 | - * Check whether type is of an application class. |
|
192 | - * |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function isApplication(): bool |
|
196 | - { |
|
197 | - return self::CLASS_APPLICATION === $this->_class; |
|
198 | - } |
|
190 | + /** |
|
191 | + * Check whether type is of an application class. |
|
192 | + * |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function isApplication(): bool |
|
196 | + { |
|
197 | + return self::CLASS_APPLICATION === $this->_class; |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Check whether type is of a context specific class. |
|
202 | - * |
|
203 | - * @return bool |
|
204 | - */ |
|
205 | - public function isContextSpecific(): bool |
|
206 | - { |
|
207 | - return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | - } |
|
200 | + /** |
|
201 | + * Check whether type is of a context specific class. |
|
202 | + * |
|
203 | + * @return bool |
|
204 | + */ |
|
205 | + public function isContextSpecific(): bool |
|
206 | + { |
|
207 | + return self::CLASS_CONTEXT_SPECIFIC === $this->_class; |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * Check whether type is of a private class. |
|
212 | - * |
|
213 | - * @return bool |
|
214 | - */ |
|
215 | - public function isPrivate(): bool |
|
216 | - { |
|
217 | - return self::CLASS_PRIVATE === $this->_class; |
|
218 | - } |
|
210 | + /** |
|
211 | + * Check whether type is of a private class. |
|
212 | + * |
|
213 | + * @return bool |
|
214 | + */ |
|
215 | + public function isPrivate(): bool |
|
216 | + { |
|
217 | + return self::CLASS_PRIVATE === $this->_class; |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Check whether content is primitive type. |
|
222 | - * |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function isPrimitive(): bool |
|
226 | - { |
|
227 | - return self::PRIMITIVE === $this->_pc; |
|
228 | - } |
|
220 | + /** |
|
221 | + * Check whether content is primitive type. |
|
222 | + * |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function isPrimitive(): bool |
|
226 | + { |
|
227 | + return self::PRIMITIVE === $this->_pc; |
|
228 | + } |
|
229 | 229 | |
230 | - /** |
|
231 | - * Check hether content is constructed type. |
|
232 | - * |
|
233 | - * @return bool |
|
234 | - */ |
|
235 | - public function isConstructed(): bool |
|
236 | - { |
|
237 | - return self::CONSTRUCTED === $this->_pc; |
|
238 | - } |
|
230 | + /** |
|
231 | + * Check hether content is constructed type. |
|
232 | + * |
|
233 | + * @return bool |
|
234 | + */ |
|
235 | + public function isConstructed(): bool |
|
236 | + { |
|
237 | + return self::CONSTRUCTED === $this->_pc; |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Get self with given type class. |
|
242 | - * |
|
243 | - * @param int $class One of <code>CLASS_*</code> enumerations |
|
244 | - * |
|
245 | - * @return self |
|
246 | - */ |
|
247 | - public function withClass(int $class): Identifier |
|
248 | - { |
|
249 | - $obj = clone $this; |
|
250 | - $obj->_class = 0b11 & $class; |
|
251 | - return $obj; |
|
252 | - } |
|
240 | + /** |
|
241 | + * Get self with given type class. |
|
242 | + * |
|
243 | + * @param int $class One of <code>CLASS_*</code> enumerations |
|
244 | + * |
|
245 | + * @return self |
|
246 | + */ |
|
247 | + public function withClass(int $class): Identifier |
|
248 | + { |
|
249 | + $obj = clone $this; |
|
250 | + $obj->_class = 0b11 & $class; |
|
251 | + return $obj; |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Get self with given type tag. |
|
256 | - * |
|
257 | - * @param int|string $tag Tag number |
|
258 | - * |
|
259 | - * @return self |
|
260 | - */ |
|
261 | - public function withTag($tag): Identifier |
|
262 | - { |
|
263 | - $obj = clone $this; |
|
264 | - $obj->_tag = new BigInt($tag); |
|
265 | - return $obj; |
|
266 | - } |
|
254 | + /** |
|
255 | + * Get self with given type tag. |
|
256 | + * |
|
257 | + * @param int|string $tag Tag number |
|
258 | + * |
|
259 | + * @return self |
|
260 | + */ |
|
261 | + public function withTag($tag): Identifier |
|
262 | + { |
|
263 | + $obj = clone $this; |
|
264 | + $obj->_tag = new BigInt($tag); |
|
265 | + return $obj; |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Get human readable name of the type class. |
|
270 | - * |
|
271 | - * @param int $class |
|
272 | - * |
|
273 | - * @return string |
|
274 | - */ |
|
275 | - public static function classToName(int $class): string |
|
276 | - { |
|
277 | - if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | - return "CLASS {$class}"; |
|
279 | - } |
|
280 | - return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | - } |
|
268 | + /** |
|
269 | + * Get human readable name of the type class. |
|
270 | + * |
|
271 | + * @param int $class |
|
272 | + * |
|
273 | + * @return string |
|
274 | + */ |
|
275 | + public static function classToName(int $class): string |
|
276 | + { |
|
277 | + if (!array_key_exists($class, self::MAP_CLASS_TO_NAME)) { |
|
278 | + return "CLASS {$class}"; |
|
279 | + } |
|
280 | + return self::MAP_CLASS_TO_NAME[$class]; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * Parse long form tag. |
|
285 | - * |
|
286 | - * @param string $data DER data |
|
287 | - * @param int $offset Reference to the variable containing offset to data |
|
288 | - * |
|
289 | - * @throws DecodeException If decoding fails |
|
290 | - * |
|
291 | - * @return string Tag number |
|
292 | - */ |
|
293 | - private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | - { |
|
295 | - $datalen = strlen($data); |
|
296 | - $tag = gmp_init(0, 10); |
|
297 | - while (true) { |
|
298 | - if ($offset >= $datalen) { |
|
299 | - throw new DecodeException( |
|
300 | - 'Unexpected end of data while decoding' . |
|
301 | - ' long form identifier.'); |
|
302 | - } |
|
303 | - $byte = ord($data[$offset++]); |
|
304 | - $tag <<= 7; |
|
305 | - $tag |= 0x7f & $byte; |
|
306 | - // last byte has bit 8 set to zero |
|
307 | - if (!(0x80 & $byte)) { |
|
308 | - break; |
|
309 | - } |
|
310 | - } |
|
311 | - return gmp_strval($tag, 10); |
|
312 | - } |
|
283 | + /** |
|
284 | + * Parse long form tag. |
|
285 | + * |
|
286 | + * @param string $data DER data |
|
287 | + * @param int $offset Reference to the variable containing offset to data |
|
288 | + * |
|
289 | + * @throws DecodeException If decoding fails |
|
290 | + * |
|
291 | + * @return string Tag number |
|
292 | + */ |
|
293 | + private static function _decodeLongFormTag(string $data, int &$offset): string |
|
294 | + { |
|
295 | + $datalen = strlen($data); |
|
296 | + $tag = gmp_init(0, 10); |
|
297 | + while (true) { |
|
298 | + if ($offset >= $datalen) { |
|
299 | + throw new DecodeException( |
|
300 | + 'Unexpected end of data while decoding' . |
|
301 | + ' long form identifier.'); |
|
302 | + } |
|
303 | + $byte = ord($data[$offset++]); |
|
304 | + $tag <<= 7; |
|
305 | + $tag |= 0x7f & $byte; |
|
306 | + // last byte has bit 8 set to zero |
|
307 | + if (!(0x80 & $byte)) { |
|
308 | + break; |
|
309 | + } |
|
310 | + } |
|
311 | + return gmp_strval($tag, 10); |
|
312 | + } |
|
313 | 313 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Exception; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -16,43 +16,43 @@ |
||
16 | 16 | */ |
17 | 17 | class ExplicitlyTaggedType extends TaggedTypeWrap implements ExplicitTagging |
18 | 18 | { |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param int $tag Tag number |
|
23 | - * @param Element $element Wrapped element |
|
24 | - * @param int $class Type class |
|
25 | - */ |
|
26 | - public function __construct(int $tag, Element $element, |
|
27 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | - { |
|
29 | - $this->_typeTag = $tag; |
|
30 | - $this->_element = $element; |
|
31 | - $this->_class = $class; |
|
32 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param int $tag Tag number |
|
23 | + * @param Element $element Wrapped element |
|
24 | + * @param int $class Type class |
|
25 | + */ |
|
26 | + public function __construct(int $tag, Element $element, |
|
27 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | + { |
|
29 | + $this->_typeTag = $tag; |
|
30 | + $this->_element = $element; |
|
31 | + $this->_class = $class; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function isConstructed(): bool |
|
38 | - { |
|
39 | - return true; |
|
40 | - } |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function isConstructed(): bool |
|
38 | + { |
|
39 | + return true; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - public function explicit(): UnspecifiedType |
|
46 | - { |
|
47 | - return $this->_element->asUnspecified(); |
|
48 | - } |
|
42 | + /** |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + public function explicit(): UnspecifiedType |
|
46 | + { |
|
47 | + return $this->_element->asUnspecified(); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * {@inheritdoc} |
|
52 | - */ |
|
53 | - protected function _encodedContentDER(): string |
|
54 | - { |
|
55 | - // get the full encoding of the wrapped element |
|
56 | - return $this->_element->toDER(); |
|
57 | - } |
|
50 | + /** |
|
51 | + * {@inheritdoc} |
|
52 | + */ |
|
53 | + protected function _encodedContentDER(): string |
|
54 | + { |
|
55 | + // get the full encoding of the wrapped element |
|
56 | + return $this->_element->toDER(); |
|
57 | + } |
|
58 | 58 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -17,52 +17,52 @@ |
||
17 | 17 | */ |
18 | 18 | class ImplicitlyTaggedType extends TaggedTypeWrap implements ImplicitTagging |
19 | 19 | { |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param int $tag Tag number |
|
24 | - * @param Element $element Wrapped element |
|
25 | - * @param int $class Type class |
|
26 | - */ |
|
27 | - public function __construct(int $tag, Element $element, |
|
28 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | - { |
|
30 | - $this->_typeTag = $tag; |
|
31 | - $this->_element = $element; |
|
32 | - $this->_class = $class; |
|
33 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param int $tag Tag number |
|
24 | + * @param Element $element Wrapped element |
|
25 | + * @param int $class Type class |
|
26 | + */ |
|
27 | + public function __construct(int $tag, Element $element, |
|
28 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | + { |
|
30 | + $this->_typeTag = $tag; |
|
31 | + $this->_element = $element; |
|
32 | + $this->_class = $class; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - public function isConstructed(): bool |
|
39 | - { |
|
40 | - // depends on the underlying type |
|
41 | - return $this->_element->isConstructed(); |
|
42 | - } |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + public function isConstructed(): bool |
|
39 | + { |
|
40 | + // depends on the underlying type |
|
41 | + return $this->_element->isConstructed(); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * {@inheritdoc} |
|
46 | - */ |
|
47 | - public function implicit( |
|
48 | - int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
49 | - { |
|
50 | - $this->_element->expectType($tag); |
|
51 | - if ($this->_element->typeClass() !== $class) { |
|
52 | - throw new \UnexpectedValueException( |
|
53 | - sprintf('Type class %s expected, got %s.', |
|
54 | - Identifier::classToName($class), |
|
55 | - Identifier::classToName($this->_element->typeClass()))); |
|
56 | - } |
|
57 | - return $this->_element->asUnspecified(); |
|
58 | - } |
|
44 | + /** |
|
45 | + * {@inheritdoc} |
|
46 | + */ |
|
47 | + public function implicit( |
|
48 | + int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
49 | + { |
|
50 | + $this->_element->expectType($tag); |
|
51 | + if ($this->_element->typeClass() !== $class) { |
|
52 | + throw new \UnexpectedValueException( |
|
53 | + sprintf('Type class %s expected, got %s.', |
|
54 | + Identifier::classToName($class), |
|
55 | + Identifier::classToName($this->_element->typeClass()))); |
|
56 | + } |
|
57 | + return $this->_element->asUnspecified(); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected function _encodedContentDER(): string |
|
64 | - { |
|
65 | - // get only the content of the wrapped element. |
|
66 | - return $this->_element->_encodedContentDER(); |
|
67 | - } |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected function _encodedContentDER(): string |
|
64 | + { |
|
65 | + // get only the content of the wrapped element. |
|
66 | + return $this->_element->_encodedContentDER(); |
|
67 | + } |
|
68 | 68 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Tagged; |
6 | 6 |
@@ -23,135 +23,135 @@ |
||
23 | 23 | */ |
24 | 24 | class DERTaggedType extends TaggedType implements ExplicitTagging, ImplicitTagging |
25 | 25 | { |
26 | - /** |
|
27 | - * Identifier. |
|
28 | - * |
|
29 | - * @var Identifier |
|
30 | - */ |
|
31 | - private $_identifier; |
|
26 | + /** |
|
27 | + * Identifier. |
|
28 | + * |
|
29 | + * @var Identifier |
|
30 | + */ |
|
31 | + private $_identifier; |
|
32 | 32 | |
33 | - /** |
|
34 | - * DER data. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $_data; |
|
33 | + /** |
|
34 | + * DER data. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $_data; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Offset to next byte after identifier. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_offset; |
|
40 | + /** |
|
41 | + * Offset to next byte after identifier. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_offset; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Offset to content. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_valueOffset; |
|
47 | + /** |
|
48 | + * Offset to content. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_valueOffset; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Length of the content. |
|
56 | - * |
|
57 | - * @var int |
|
58 | - */ |
|
59 | - private $_valueLength; |
|
54 | + /** |
|
55 | + * Length of the content. |
|
56 | + * |
|
57 | + * @var int |
|
58 | + */ |
|
59 | + private $_valueLength; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param Identifier $identifier Pre-parsed identifier |
|
65 | - * @param string $data DER data |
|
66 | - * @param int $offset Offset to next byte after identifier |
|
67 | - * @param int $value_offset Offset to content |
|
68 | - * @param int $value_length Content length |
|
69 | - */ |
|
70 | - public function __construct(Identifier $identifier, string $data, |
|
71 | - int $offset, int $value_offset, int $value_length, |
|
72 | - bool $indefinite_length) |
|
73 | - { |
|
74 | - $this->_identifier = $identifier; |
|
75 | - $this->_data = $data; |
|
76 | - $this->_offset = $offset; |
|
77 | - $this->_valueOffset = $value_offset; |
|
78 | - $this->_valueLength = $value_length; |
|
79 | - $this->_indefiniteLength = $indefinite_length; |
|
80 | - $this->_typeTag = $identifier->intTag(); |
|
81 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param Identifier $identifier Pre-parsed identifier |
|
65 | + * @param string $data DER data |
|
66 | + * @param int $offset Offset to next byte after identifier |
|
67 | + * @param int $value_offset Offset to content |
|
68 | + * @param int $value_length Content length |
|
69 | + */ |
|
70 | + public function __construct(Identifier $identifier, string $data, |
|
71 | + int $offset, int $value_offset, int $value_length, |
|
72 | + bool $indefinite_length) |
|
73 | + { |
|
74 | + $this->_identifier = $identifier; |
|
75 | + $this->_data = $data; |
|
76 | + $this->_offset = $offset; |
|
77 | + $this->_valueOffset = $value_offset; |
|
78 | + $this->_valueLength = $value_length; |
|
79 | + $this->_indefiniteLength = $indefinite_length; |
|
80 | + $this->_typeTag = $identifier->intTag(); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * {@inheritdoc} |
|
85 | - */ |
|
86 | - public function typeClass(): int |
|
87 | - { |
|
88 | - return $this->_identifier->typeClass(); |
|
89 | - } |
|
83 | + /** |
|
84 | + * {@inheritdoc} |
|
85 | + */ |
|
86 | + public function typeClass(): int |
|
87 | + { |
|
88 | + return $this->_identifier->typeClass(); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * {@inheritdoc} |
|
93 | - */ |
|
94 | - public function isConstructed(): bool |
|
95 | - { |
|
96 | - return $this->_identifier->isConstructed(); |
|
97 | - } |
|
91 | + /** |
|
92 | + * {@inheritdoc} |
|
93 | + */ |
|
94 | + public function isConstructed(): bool |
|
95 | + { |
|
96 | + return $this->_identifier->isConstructed(); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * {@inheritdoc} |
|
101 | - */ |
|
102 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
103 | - { |
|
104 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
105 | - $cls = self::_determineImplClass($identifier); |
|
106 | - $idx = $this->_offset; |
|
107 | - /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
108 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
109 | - return $element->asUnspecified(); |
|
110 | - } |
|
99 | + /** |
|
100 | + * {@inheritdoc} |
|
101 | + */ |
|
102 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
103 | + { |
|
104 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
105 | + $cls = self::_determineImplClass($identifier); |
|
106 | + $idx = $this->_offset; |
|
107 | + /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
108 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
109 | + return $element->asUnspecified(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * {@inheritdoc} |
|
114 | - */ |
|
115 | - public function explicit(): UnspecifiedType |
|
116 | - { |
|
117 | - $idx = $this->_valueOffset; |
|
118 | - return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
119 | - } |
|
112 | + /** |
|
113 | + * {@inheritdoc} |
|
114 | + */ |
|
115 | + public function explicit(): UnspecifiedType |
|
116 | + { |
|
117 | + $idx = $this->_valueOffset; |
|
118 | + return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * {@inheritdoc} |
|
123 | - */ |
|
124 | - protected static function _decodeFromDER(Identifier $identifier, |
|
125 | - string $data, int &$offset): ElementBase |
|
126 | - { |
|
127 | - $idx = $offset; |
|
128 | - $length = Length::expectFromDER($data, $idx); |
|
129 | - // offset to inner value |
|
130 | - $value_offset = $idx; |
|
131 | - if ($length->isIndefinite()) { |
|
132 | - if ($identifier->isPrimitive()) { |
|
133 | - throw new DecodeException( |
|
134 | - 'Primitive type with indefinite length is not supported.'); |
|
135 | - } |
|
136 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
137 | - // EOC consists of two octets. |
|
138 | - $value_length = $idx - $value_offset - 2; |
|
139 | - } else { |
|
140 | - $value_length = $length->intLength(); |
|
141 | - $idx += $value_length; |
|
142 | - } |
|
143 | - // late static binding since ApplicationType and PrivateType extend this class |
|
144 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
145 | - $value_length, $length->isIndefinite()); |
|
146 | - $offset = $idx; |
|
147 | - return $type; |
|
148 | - } |
|
121 | + /** |
|
122 | + * {@inheritdoc} |
|
123 | + */ |
|
124 | + protected static function _decodeFromDER(Identifier $identifier, |
|
125 | + string $data, int &$offset): ElementBase |
|
126 | + { |
|
127 | + $idx = $offset; |
|
128 | + $length = Length::expectFromDER($data, $idx); |
|
129 | + // offset to inner value |
|
130 | + $value_offset = $idx; |
|
131 | + if ($length->isIndefinite()) { |
|
132 | + if ($identifier->isPrimitive()) { |
|
133 | + throw new DecodeException( |
|
134 | + 'Primitive type with indefinite length is not supported.'); |
|
135 | + } |
|
136 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
137 | + // EOC consists of two octets. |
|
138 | + $value_length = $idx - $value_offset - 2; |
|
139 | + } else { |
|
140 | + $value_length = $length->intLength(); |
|
141 | + $idx += $value_length; |
|
142 | + } |
|
143 | + // late static binding since ApplicationType and PrivateType extend this class |
|
144 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
145 | + $value_length, $length->isIndefinite()); |
|
146 | + $offset = $idx; |
|
147 | + return $type; |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * {@inheritdoc} |
|
152 | - */ |
|
153 | - protected function _encodedContentDER(): string |
|
154 | - { |
|
155 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
156 | - } |
|
150 | + /** |
|
151 | + * {@inheritdoc} |
|
152 | + */ |
|
153 | + protected function _encodedContentDER(): string |
|
154 | + { |
|
155 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
156 | + } |
|
157 | 157 | } |