1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace ASN1\Type\Primitive; |
6
|
|
|
|
7
|
|
|
use ASN1\Element; |
8
|
|
|
use ASN1\Component\Identifier; |
9
|
|
|
use ASN1\Component\Length; |
10
|
|
|
use ASN1\Feature\ElementBase; |
11
|
|
|
use ASN1\Type\PrimitiveType; |
12
|
|
|
use ASN1\Type\UniversalClass; |
13
|
|
|
use ASN1\Util\BigInt; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implements <i>INTEGER</i> type. |
17
|
|
|
*/ |
18
|
|
|
class Integer extends Element |
19
|
|
|
{ |
20
|
|
|
use UniversalClass; |
21
|
|
|
use PrimitiveType; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The number. |
25
|
|
|
* |
26
|
|
|
* @var BigInt |
27
|
|
|
*/ |
28
|
|
|
private $_number; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param int|string $number Base 10 integer |
34
|
|
|
*/ |
35
|
58 |
|
public function __construct($number) |
36
|
|
|
{ |
37
|
58 |
|
$this->_typeTag = self::TYPE_INTEGER; |
38
|
58 |
|
if (!self::_validateNumber($number)) { |
39
|
2 |
|
$var = is_scalar($number) ? strval($number) : gettype($number); |
40
|
2 |
|
throw new \InvalidArgumentException("'$var' is not a valid number."); |
41
|
|
|
} |
42
|
56 |
|
$this->_number = new BigInt($number); |
43
|
56 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the number as a base 10. |
47
|
|
|
* |
48
|
|
|
* @return string Integer as a string |
49
|
|
|
*/ |
50
|
24 |
|
public function number(): string |
51
|
|
|
{ |
52
|
24 |
|
return $this->_number->base10(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the number as an integer type. |
57
|
|
|
* |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
2 |
|
public function intNumber(): int |
61
|
|
|
{ |
62
|
2 |
|
return $this->_number->intVal(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
27 |
|
protected function _encodedContentDER(): string |
70
|
|
|
{ |
71
|
27 |
|
$num = $this->_number->gmpObj(); |
72
|
27 |
|
switch (gmp_sign($num)) { |
73
|
|
|
// positive |
74
|
27 |
|
case 1: |
75
|
10 |
|
return self::_encodePositiveInteger($num); |
76
|
|
|
// negative |
77
|
17 |
|
case -1: |
78
|
15 |
|
return self::_encodeNegativeInteger($num); |
79
|
|
|
} |
80
|
|
|
// zero |
81
|
2 |
|
return "\0"; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Encode positive integer to DER content. |
86
|
|
|
* |
87
|
|
|
* @param \GMP|resource $num |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
10 |
|
private static function _encodePositiveInteger(\GMP $num): string |
91
|
|
|
{ |
92
|
10 |
|
$bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
93
|
|
|
// if first bit is 1, prepend full zero byte |
94
|
|
|
// to represent positive two's complement |
95
|
10 |
|
if (ord($bin[0]) & 0x80) { |
96
|
3 |
|
$bin = chr(0x00) . $bin; |
97
|
|
|
} |
98
|
10 |
|
return $bin; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Encode negative integer to DER content. |
103
|
|
|
* |
104
|
|
|
* @param \GMP|resource $num |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
15 |
|
private static function _encodeNegativeInteger(\GMP $num): string |
108
|
|
|
{ |
109
|
15 |
|
$num = gmp_abs($num); |
110
|
|
|
// compute number of bytes required |
111
|
15 |
|
$width = 1; |
112
|
15 |
|
if ($num > 128) { |
113
|
11 |
|
$tmp = $num; |
114
|
|
|
do { |
115
|
11 |
|
$width++; |
116
|
11 |
|
$tmp >>= 8; |
117
|
11 |
|
} while ($tmp > 128); |
118
|
|
|
} |
119
|
|
|
// compute two's complement 2^n - x |
120
|
15 |
|
$num = gmp_pow("2", 8 * $width) - $num; |
121
|
15 |
|
$bin = gmp_export($num, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); |
122
|
|
|
// if first bit is 0, prepend full inverted byte |
123
|
|
|
// to represent negative two's complement |
124
|
15 |
|
if (!(ord($bin[0]) & 0x80)) { |
125
|
1 |
|
$bin = chr(0xff) . $bin; |
126
|
|
|
} |
127
|
15 |
|
return $bin; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* @return self |
134
|
|
|
*/ |
135
|
29 |
|
protected static function _decodeFromDER(Identifier $identifier, string $data, |
136
|
|
|
int &$offset): ElementBase |
137
|
|
|
{ |
138
|
29 |
|
$idx = $offset; |
139
|
29 |
|
$length = Length::expectFromDER($data, $idx)->intLength(); |
140
|
28 |
|
$bytes = substr($data, $idx, $length); |
141
|
28 |
|
$idx += $length; |
142
|
28 |
|
$neg = ord($bytes[0]) & 0x80; |
143
|
|
|
// negative, apply inversion of two's complement |
144
|
28 |
|
if ($neg) { |
145
|
15 |
|
$len = strlen($bytes); |
146
|
15 |
|
for ($i = 0; $i < $len; $i++) { |
147
|
15 |
|
$bytes[$i] = ~$bytes[$i]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
28 |
|
$num = gmp_init(bin2hex($bytes), 16); |
151
|
|
|
// negative, apply addition of two's complement |
152
|
|
|
// and produce negative result |
153
|
28 |
|
if ($neg) { |
154
|
15 |
|
$num = gmp_neg($num + 1); |
155
|
|
|
} |
156
|
28 |
|
$offset = $idx; |
157
|
|
|
// late static binding since enumerated extends integer type |
158
|
28 |
|
return new static(gmp_strval($num, 10)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Test that number is valid for this context. |
163
|
|
|
* |
164
|
|
|
* @param mixed $num |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
58 |
|
private static function _validateNumber($num): bool |
168
|
|
|
{ |
169
|
58 |
|
if (is_int($num)) { |
170
|
24 |
|
return true; |
171
|
|
|
} |
172
|
34 |
|
if (is_string($num) && preg_match('/-?\d+/', $num)) { |
173
|
32 |
|
return true; |
174
|
|
|
} |
175
|
2 |
|
return false; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|