@@ -16,151 +16,151 @@ |
||
16 | 16 | */ |
17 | 17 | class Integer extends Element |
18 | 18 | { |
19 | - use UniversalClass; |
|
20 | - use PrimitiveType; |
|
19 | + use UniversalClass; |
|
20 | + use PrimitiveType; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Number as a base 10. |
|
24 | - * |
|
25 | - * @var string |
|
26 | - */ |
|
27 | - private $_number; |
|
22 | + /** |
|
23 | + * Number as a base 10. |
|
24 | + * |
|
25 | + * @var string |
|
26 | + */ |
|
27 | + private $_number; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @param int|string $number Base 10 integer |
|
33 | - */ |
|
34 | - public function __construct($number) |
|
35 | - { |
|
36 | - $this->_typeTag = self::TYPE_INTEGER; |
|
37 | - if (!self::_validateNumber($number)) { |
|
38 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
39 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
40 | - } |
|
41 | - $this->_number = strval($number); |
|
42 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @param int|string $number Base 10 integer |
|
33 | + */ |
|
34 | + public function __construct($number) |
|
35 | + { |
|
36 | + $this->_typeTag = self::TYPE_INTEGER; |
|
37 | + if (!self::_validateNumber($number)) { |
|
38 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
39 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
40 | + } |
|
41 | + $this->_number = strval($number); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get the number as a base 10. |
|
46 | - * |
|
47 | - * @return string Integer as a string |
|
48 | - */ |
|
49 | - public function number(): string |
|
50 | - { |
|
51 | - return $this->_number; |
|
52 | - } |
|
44 | + /** |
|
45 | + * Get the number as a base 10. |
|
46 | + * |
|
47 | + * @return string Integer as a string |
|
48 | + */ |
|
49 | + public function number(): string |
|
50 | + { |
|
51 | + return $this->_number; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * |
|
56 | - * {@inheritdoc} |
|
57 | - */ |
|
58 | - protected function _encodedContentDER(): string |
|
59 | - { |
|
60 | - $num = gmp_init($this->_number, 10); |
|
61 | - switch (gmp_sign($num)) { |
|
62 | - // positive |
|
63 | - case 1: |
|
64 | - return self::_encodePositiveInteger($num); |
|
65 | - // negative |
|
66 | - case -1: |
|
67 | - return self::_encodeNegativeInteger($num); |
|
68 | - } |
|
69 | - // zero |
|
70 | - return "\0"; |
|
71 | - } |
|
54 | + /** |
|
55 | + * |
|
56 | + * {@inheritdoc} |
|
57 | + */ |
|
58 | + protected function _encodedContentDER(): string |
|
59 | + { |
|
60 | + $num = gmp_init($this->_number, 10); |
|
61 | + switch (gmp_sign($num)) { |
|
62 | + // positive |
|
63 | + case 1: |
|
64 | + return self::_encodePositiveInteger($num); |
|
65 | + // negative |
|
66 | + case -1: |
|
67 | + return self::_encodeNegativeInteger($num); |
|
68 | + } |
|
69 | + // zero |
|
70 | + return "\0"; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Encode positive integer to DER content. |
|
75 | - * |
|
76 | - * @param \GMP|resource $num |
|
77 | - * @return string |
|
78 | - */ |
|
79 | - private static function _encodePositiveInteger(\GMP $num): string |
|
80 | - { |
|
81 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
82 | - // if first bit is 1, prepend full zero byte |
|
83 | - // to represent positive two's complement |
|
84 | - if (ord($bin[0]) & 0x80) { |
|
85 | - $bin = chr(0x00) . $bin; |
|
86 | - } |
|
87 | - return $bin; |
|
88 | - } |
|
73 | + /** |
|
74 | + * Encode positive integer to DER content. |
|
75 | + * |
|
76 | + * @param \GMP|resource $num |
|
77 | + * @return string |
|
78 | + */ |
|
79 | + private static function _encodePositiveInteger(\GMP $num): string |
|
80 | + { |
|
81 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
82 | + // if first bit is 1, prepend full zero byte |
|
83 | + // to represent positive two's complement |
|
84 | + if (ord($bin[0]) & 0x80) { |
|
85 | + $bin = chr(0x00) . $bin; |
|
86 | + } |
|
87 | + return $bin; |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Encode negative integer to DER content. |
|
92 | - * |
|
93 | - * @param \GMP|resource $num |
|
94 | - * @return string |
|
95 | - */ |
|
96 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
97 | - { |
|
98 | - $num = gmp_abs($num); |
|
99 | - // compute number of bytes required |
|
100 | - $width = 1; |
|
101 | - if ($num > 128) { |
|
102 | - $tmp = $num; |
|
103 | - do { |
|
104 | - $width++; |
|
105 | - $tmp >>= 8; |
|
106 | - } while ($tmp > 128); |
|
107 | - } |
|
108 | - // compute two's complement 2^n - x |
|
109 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
110 | - $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
111 | - // if first bit is 0, prepend full inverted byte |
|
112 | - // to represent negative two's complement |
|
113 | - if (!(ord($bin[0]) & 0x80)) { |
|
114 | - $bin = chr(0xff) . $bin; |
|
115 | - } |
|
116 | - return $bin; |
|
117 | - } |
|
90 | + /** |
|
91 | + * Encode negative integer to DER content. |
|
92 | + * |
|
93 | + * @param \GMP|resource $num |
|
94 | + * @return string |
|
95 | + */ |
|
96 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
97 | + { |
|
98 | + $num = gmp_abs($num); |
|
99 | + // compute number of bytes required |
|
100 | + $width = 1; |
|
101 | + if ($num > 128) { |
|
102 | + $tmp = $num; |
|
103 | + do { |
|
104 | + $width++; |
|
105 | + $tmp >>= 8; |
|
106 | + } while ($tmp > 128); |
|
107 | + } |
|
108 | + // compute two's complement 2^n - x |
|
109 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
110 | + $bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
|
111 | + // if first bit is 0, prepend full inverted byte |
|
112 | + // to represent negative two's complement |
|
113 | + if (!(ord($bin[0]) & 0x80)) { |
|
114 | + $bin = chr(0xff) . $bin; |
|
115 | + } |
|
116 | + return $bin; |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * |
|
121 | - * {@inheritdoc} |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
125 | - int &$offset): ElementBase |
|
126 | - { |
|
127 | - $idx = $offset; |
|
128 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
129 | - $bytes = substr($data, $idx, $length); |
|
130 | - $idx += $length; |
|
131 | - $neg = ord($bytes[0]) & 0x80; |
|
132 | - // negative, apply inversion of two's complement |
|
133 | - if ($neg) { |
|
134 | - $len = strlen($bytes); |
|
135 | - for ($i = 0; $i < $len; $i++) { |
|
136 | - $bytes[$i] = ~$bytes[$i]; |
|
137 | - } |
|
138 | - } |
|
139 | - $num = gmp_init(bin2hex($bytes), 16); |
|
140 | - // negative, apply addition of two's complement |
|
141 | - // and produce negative result |
|
142 | - if ($neg) { |
|
143 | - $num = gmp_neg($num + 1); |
|
144 | - } |
|
145 | - $offset = $idx; |
|
146 | - // late static binding since enumerated extends integer type |
|
147 | - return new static(gmp_strval($num, 10)); |
|
148 | - } |
|
119 | + /** |
|
120 | + * |
|
121 | + * {@inheritdoc} |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
125 | + int &$offset): ElementBase |
|
126 | + { |
|
127 | + $idx = $offset; |
|
128 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
129 | + $bytes = substr($data, $idx, $length); |
|
130 | + $idx += $length; |
|
131 | + $neg = ord($bytes[0]) & 0x80; |
|
132 | + // negative, apply inversion of two's complement |
|
133 | + if ($neg) { |
|
134 | + $len = strlen($bytes); |
|
135 | + for ($i = 0; $i < $len; $i++) { |
|
136 | + $bytes[$i] = ~$bytes[$i]; |
|
137 | + } |
|
138 | + } |
|
139 | + $num = gmp_init(bin2hex($bytes), 16); |
|
140 | + // negative, apply addition of two's complement |
|
141 | + // and produce negative result |
|
142 | + if ($neg) { |
|
143 | + $num = gmp_neg($num + 1); |
|
144 | + } |
|
145 | + $offset = $idx; |
|
146 | + // late static binding since enumerated extends integer type |
|
147 | + return new static(gmp_strval($num, 10)); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Test that number is valid for this context. |
|
152 | - * |
|
153 | - * @param mixed $num |
|
154 | - * @return boolean |
|
155 | - */ |
|
156 | - private static function _validateNumber($num): bool |
|
157 | - { |
|
158 | - if (is_int($num)) { |
|
159 | - return true; |
|
160 | - } |
|
161 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
162 | - return true; |
|
163 | - } |
|
164 | - return false; |
|
165 | - } |
|
150 | + /** |
|
151 | + * Test that number is valid for this context. |
|
152 | + * |
|
153 | + * @param mixed $num |
|
154 | + * @return boolean |
|
155 | + */ |
|
156 | + private static function _validateNumber($num): bool |
|
157 | + { |
|
158 | + if (is_int($num)) { |
|
159 | + return true; |
|
160 | + } |
|
161 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
162 | + return true; |
|
163 | + } |
|
164 | + return false; |
|
165 | + } |
|
166 | 166 | } |
@@ -122,7 +122,7 @@ |
||
122 | 122 | * @return self |
123 | 123 | */ |
124 | 124 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
125 | - int &$offset): ElementBase |
|
125 | + int & $offset): ElementBase |
|
126 | 126 | { |
127 | 127 | $idx = $offset; |
128 | 128 | $length = Length::expectFromDER($data, $idx)->intLength(); |
@@ -20,105 +20,105 @@ |
||
20 | 20 | * May be encoded back to complete DER encoding. |
21 | 21 | */ |
22 | 22 | class DERTaggedType extends TaggedType implements |
23 | - ExplicitTagging, |
|
24 | - ImplicitTagging |
|
23 | + ExplicitTagging, |
|
24 | + 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 data. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_offset; |
|
40 | + /** |
|
41 | + * Offset to data. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_offset; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Constructor. |
|
49 | - * |
|
50 | - * @param Identifier $identifier |
|
51 | - * @param string $data |
|
52 | - * @param int $offset Offset to next byte after identifier |
|
53 | - */ |
|
54 | - public function __construct(Identifier $identifier, string $data, int $offset) |
|
55 | - { |
|
56 | - $this->_identifier = $identifier; |
|
57 | - $this->_data = $data; |
|
58 | - $this->_offset = $offset; |
|
59 | - $this->_typeTag = intval($identifier->tag()); |
|
60 | - } |
|
47 | + /** |
|
48 | + * Constructor. |
|
49 | + * |
|
50 | + * @param Identifier $identifier |
|
51 | + * @param string $data |
|
52 | + * @param int $offset Offset to next byte after identifier |
|
53 | + */ |
|
54 | + public function __construct(Identifier $identifier, string $data, int $offset) |
|
55 | + { |
|
56 | + $this->_identifier = $identifier; |
|
57 | + $this->_data = $data; |
|
58 | + $this->_offset = $offset; |
|
59 | + $this->_typeTag = intval($identifier->tag()); |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * |
|
64 | - * @see \ASN1\Element::typeClass() |
|
65 | - * @return int |
|
66 | - */ |
|
67 | - public function typeClass(): int |
|
68 | - { |
|
69 | - return $this->_identifier->typeClass(); |
|
70 | - } |
|
62 | + /** |
|
63 | + * |
|
64 | + * @see \ASN1\Element::typeClass() |
|
65 | + * @return int |
|
66 | + */ |
|
67 | + public function typeClass(): int |
|
68 | + { |
|
69 | + return $this->_identifier->typeClass(); |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * |
|
74 | - * @see \ASN1\Element::isConstructed() |
|
75 | - * @return bool |
|
76 | - */ |
|
77 | - public function isConstructed(): bool |
|
78 | - { |
|
79 | - return $this->_identifier->isConstructed(); |
|
80 | - } |
|
72 | + /** |
|
73 | + * |
|
74 | + * @see \ASN1\Element::isConstructed() |
|
75 | + * @return bool |
|
76 | + */ |
|
77 | + public function isConstructed(): bool |
|
78 | + { |
|
79 | + return $this->_identifier->isConstructed(); |
|
80 | + } |
|
81 | 81 | |
82 | - /** |
|
83 | - * |
|
84 | - * @see \ASN1\Element::_encodedContentDER() |
|
85 | - * @return string |
|
86 | - */ |
|
87 | - protected function _encodedContentDER(): string |
|
88 | - { |
|
89 | - $idx = $this->_offset; |
|
90 | - $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | - return substr($this->_data, $idx, $length); |
|
92 | - } |
|
82 | + /** |
|
83 | + * |
|
84 | + * @see \ASN1\Element::_encodedContentDER() |
|
85 | + * @return string |
|
86 | + */ |
|
87 | + protected function _encodedContentDER(): string |
|
88 | + { |
|
89 | + $idx = $this->_offset; |
|
90 | + $length = Length::expectFromDER($this->_data, $idx)->intLength(); |
|
91 | + return substr($this->_data, $idx, $length); |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * |
|
96 | - * {@inheritdoc} |
|
97 | - * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | - * @return UnspecifiedType |
|
99 | - */ |
|
100 | - public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | - { |
|
102 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | - $cls = self::_determineImplClass($identifier); |
|
104 | - $idx = $this->_offset; |
|
105 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
106 | - return new UnspecifiedType($element); |
|
107 | - } |
|
94 | + /** |
|
95 | + * |
|
96 | + * {@inheritdoc} |
|
97 | + * @see \ASN1\Type\Tagged\ImplicitTagging::implicit() |
|
98 | + * @return UnspecifiedType |
|
99 | + */ |
|
100 | + public function implicit($tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
101 | + { |
|
102 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
103 | + $cls = self::_determineImplClass($identifier); |
|
104 | + $idx = $this->_offset; |
|
105 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
106 | + return new UnspecifiedType($element); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * |
|
111 | - * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
112 | - * @return UnspecifiedType |
|
113 | - */ |
|
114 | - public function explicit($expectedTag = null): UnspecifiedType |
|
115 | - { |
|
116 | - $idx = $this->_offset; |
|
117 | - Length::expectFromDER($this->_data, $idx); |
|
118 | - $element = Element::fromDER($this->_data, $idx); |
|
119 | - if (isset($expectedTag)) { |
|
120 | - $element->expectType($expectedTag); |
|
121 | - } |
|
122 | - return new UnspecifiedType($element); |
|
123 | - } |
|
109 | + /** |
|
110 | + * |
|
111 | + * @see \ASN1\Type\Tagged\ExplicitTagging::explicit() |
|
112 | + * @return UnspecifiedType |
|
113 | + */ |
|
114 | + public function explicit($expectedTag = null): UnspecifiedType |
|
115 | + { |
|
116 | + $idx = $this->_offset; |
|
117 | + Length::expectFromDER($this->_data, $idx); |
|
118 | + $element = Element::fromDER($this->_data, $idx); |
|
119 | + if (isset($expectedTag)) { |
|
120 | + $element->expectType($expectedTag); |
|
121 | + } |
|
122 | + return new UnspecifiedType($element); |
|
123 | + } |
|
124 | 124 | } |