@@ -17,64 +17,64 @@ |
||
17 | 17 | */ |
18 | 18 | class Boolean extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Value. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_bool; |
|
23 | + /** |
|
24 | + * Value. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_bool; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param bool $bool |
|
34 | - */ |
|
35 | - public function __construct(bool $bool) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | - $this->_bool = $bool; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param bool $bool |
|
34 | + */ |
|
35 | + public function __construct(bool $bool) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | + $this->_bool = $bool; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get the value. |
|
43 | - * |
|
44 | - * @return bool |
|
45 | - */ |
|
46 | - public function value(): bool |
|
47 | - { |
|
48 | - return $this->_bool; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get the value. |
|
43 | + * |
|
44 | + * @return bool |
|
45 | + */ |
|
46 | + public function value(): bool |
|
47 | + { |
|
48 | + return $this->_bool; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - return $this->_bool ? chr(0xff) : chr(0); |
|
58 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + return $this->_bool ? chr(0xff) : chr(0); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * |
|
62 | - * {@inheritdoc} |
|
63 | - * @return self |
|
64 | - */ |
|
65 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
66 | - int &$offset): ElementBase |
|
67 | - { |
|
68 | - $idx = $offset; |
|
69 | - Length::expectFromDER($data, $idx, 1); |
|
70 | - $byte = ord($data[$idx++]); |
|
71 | - if ($byte !== 0) { |
|
72 | - if ($byte != 0xff) { |
|
73 | - throw new DecodeException( |
|
74 | - "DER encoded boolean true must have all bits set to 1."); |
|
75 | - } |
|
76 | - } |
|
77 | - $offset = $idx; |
|
78 | - return new self($byte !== 0); |
|
79 | - } |
|
60 | + /** |
|
61 | + * |
|
62 | + * {@inheritdoc} |
|
63 | + * @return self |
|
64 | + */ |
|
65 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
66 | + int &$offset): ElementBase |
|
67 | + { |
|
68 | + $idx = $offset; |
|
69 | + Length::expectFromDER($data, $idx, 1); |
|
70 | + $byte = ord($data[$idx++]); |
|
71 | + if ($byte !== 0) { |
|
72 | + if ($byte != 0xff) { |
|
73 | + throw new DecodeException( |
|
74 | + "DER encoded boolean true must have all bits set to 1."); |
|
75 | + } |
|
76 | + } |
|
77 | + $offset = $idx; |
|
78 | + return new self($byte !== 0); |
|
79 | + } |
|
80 | 80 | } |
@@ -63,7 +63,7 @@ |
||
63 | 63 | * @return self |
64 | 64 | */ |
65 | 65 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
66 | - int &$offset): ElementBase |
|
66 | + int & $offset): ElementBase |
|
67 | 67 | { |
68 | 68 | $idx = $offset; |
69 | 69 | Length::expectFromDER($data, $idx, 1); |
@@ -11,48 +11,48 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class StringType extends Element |
13 | 13 | { |
14 | - /** |
|
15 | - * String value. |
|
16 | - * |
|
17 | - * @var string $_string |
|
18 | - */ |
|
19 | - protected $_string; |
|
14 | + /** |
|
15 | + * String value. |
|
16 | + * |
|
17 | + * @var string $_string |
|
18 | + */ |
|
19 | + protected $_string; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Constructor. |
|
23 | - * |
|
24 | - * @param string $string |
|
25 | - * @throws \InvalidArgumentException |
|
26 | - */ |
|
27 | - public function __construct(string $string) |
|
28 | - { |
|
29 | - if (!$this->_validateString($string)) { |
|
30 | - throw new \InvalidArgumentException( |
|
31 | - sprintf("Not a valid %s string.", |
|
32 | - self::tagToName($this->_typeTag))); |
|
33 | - } |
|
34 | - $this->_string = $string; |
|
35 | - } |
|
21 | + /** |
|
22 | + * Constructor. |
|
23 | + * |
|
24 | + * @param string $string |
|
25 | + * @throws \InvalidArgumentException |
|
26 | + */ |
|
27 | + public function __construct(string $string) |
|
28 | + { |
|
29 | + if (!$this->_validateString($string)) { |
|
30 | + throw new \InvalidArgumentException( |
|
31 | + sprintf("Not a valid %s string.", |
|
32 | + self::tagToName($this->_typeTag))); |
|
33 | + } |
|
34 | + $this->_string = $string; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * Get the string value. |
|
39 | - * |
|
40 | - * @return string |
|
41 | - */ |
|
42 | - public function string(): string |
|
43 | - { |
|
44 | - return $this->_string; |
|
45 | - } |
|
37 | + /** |
|
38 | + * Get the string value. |
|
39 | + * |
|
40 | + * @return string |
|
41 | + */ |
|
42 | + public function string(): string |
|
43 | + { |
|
44 | + return $this->_string; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * Check whether string is valid for the concrete type. |
|
49 | - * |
|
50 | - * @param string $string |
|
51 | - * @return bool |
|
52 | - */ |
|
53 | - protected function _validateString(string $string): bool |
|
54 | - { |
|
55 | - // Override in derived classes |
|
56 | - return true; |
|
57 | - } |
|
47 | + /** |
|
48 | + * Check whether string is valid for the concrete type. |
|
49 | + * |
|
50 | + * @param string $string |
|
51 | + * @return bool |
|
52 | + */ |
|
53 | + protected function _validateString(string $string): bool |
|
54 | + { |
|
55 | + // Override in derived classes |
|
56 | + return true; |
|
57 | + } |
|
58 | 58 | } |
@@ -13,38 +13,38 @@ |
||
13 | 13 | */ |
14 | 14 | class RelativeOID extends ObjectIdentifier |
15 | 15 | { |
16 | - /** |
|
17 | - * Constructor. |
|
18 | - * |
|
19 | - * @param string $oid OID in dotted format |
|
20 | - */ |
|
21 | - public function __construct(string $oid) |
|
22 | - { |
|
23 | - $this->_oid = $oid; |
|
24 | - $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | - } |
|
16 | + /** |
|
17 | + * Constructor. |
|
18 | + * |
|
19 | + * @param string $oid OID in dotted format |
|
20 | + */ |
|
21 | + public function __construct(string $oid) |
|
22 | + { |
|
23 | + $this->_oid = $oid; |
|
24 | + $this->_typeTag = self::TYPE_RELATIVE_OID; |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _encodedContentDER(): string |
|
32 | - { |
|
33 | - return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | - } |
|
27 | + /** |
|
28 | + * |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _encodedContentDER(): string |
|
32 | + { |
|
33 | + return self::_encodeSubIDs(...self::_explodeDottedOID($this->_oid)); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * |
|
38 | - * {@inheritdoc} |
|
39 | - * @return self |
|
40 | - */ |
|
41 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | - int &$offset): ElementBase |
|
43 | - { |
|
44 | - $idx = $offset; |
|
45 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
46 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | - $offset = $idx + $len; |
|
48 | - return new self(self::_implodeSubIDs(...$subids)); |
|
49 | - } |
|
36 | + /** |
|
37 | + * |
|
38 | + * {@inheritdoc} |
|
39 | + * @return self |
|
40 | + */ |
|
41 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
42 | + int &$offset): ElementBase |
|
43 | + { |
|
44 | + $idx = $offset; |
|
45 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
46 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
47 | + $offset = $idx + $len; |
|
48 | + return new self(self::_implodeSubIDs(...$subids)); |
|
49 | + } |
|
50 | 50 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | * @return self |
40 | 40 | */ |
41 | 41 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
42 | - int &$offset): ElementBase |
|
42 | + int & $offset): ElementBase |
|
43 | 43 | { |
44 | 44 | $idx = $offset; |
45 | 45 | $len = Length::expectFromDER($data, $idx)->intLength(); |
@@ -69,7 +69,7 @@ discard block |
||
69 | 69 | * @return self |
70 | 70 | */ |
71 | 71 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
72 | - int &$offset): ElementBase |
|
72 | + int & $offset): ElementBase |
|
73 | 73 | { |
74 | 74 | $idx = $offset; |
75 | 75 | $len = Length::expectFromDER($data, $idx)->intLength(); |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | { |
110 | 110 | return implode(".", |
111 | 111 | array_map( |
112 | - function ($num) { |
|
112 | + function($num) { |
|
113 | 113 | return gmp_strval($num, 10); |
114 | 114 | }, $subids)); |
115 | 115 | } |
@@ -17,160 +17,160 @@ |
||
17 | 17 | */ |
18 | 18 | class ObjectIdentifier extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Object identifier in dotted format. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $_oid; |
|
23 | + /** |
|
24 | + * Object identifier in dotted format. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $_oid; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $oid OID in dotted format |
|
34 | - */ |
|
35 | - public function __construct(string $oid) |
|
36 | - { |
|
37 | - $this->_oid = $oid; |
|
38 | - $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $oid OID in dotted format |
|
34 | + */ |
|
35 | + public function __construct(string $oid) |
|
36 | + { |
|
37 | + $this->_oid = $oid; |
|
38 | + $this->_typeTag = self::TYPE_OBJECT_IDENTIFIER; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get OID in dotted format. |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function oid(): string |
|
47 | - { |
|
48 | - return $this->_oid; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get OID in dotted format. |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function oid(): string |
|
47 | + { |
|
48 | + return $this->_oid; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $subids = self::_explodeDottedOID($this->_oid); |
|
58 | - // encode first two subids to one according to spec section 8.19.4 |
|
59 | - if (count($subids) >= 2) { |
|
60 | - $num = ($subids[0] * 40) + $subids[1]; |
|
61 | - array_splice($subids, 0, 2, array($num)); |
|
62 | - } |
|
63 | - return self::_encodeSubIDs(...$subids); |
|
64 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $subids = self::_explodeDottedOID($this->_oid); |
|
58 | + // encode first two subids to one according to spec section 8.19.4 |
|
59 | + if (count($subids) >= 2) { |
|
60 | + $num = ($subids[0] * 40) + $subids[1]; |
|
61 | + array_splice($subids, 0, 2, array($num)); |
|
62 | + } |
|
63 | + return self::_encodeSubIDs(...$subids); |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * |
|
68 | - * {@inheritdoc} |
|
69 | - * @return self |
|
70 | - */ |
|
71 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | - int &$offset): ElementBase |
|
73 | - { |
|
74 | - $idx = $offset; |
|
75 | - $len = Length::expectFromDER($data, $idx)->intLength(); |
|
76 | - $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | - $idx += $len; |
|
78 | - // decode first subidentifier according to spec section 8.19.4 |
|
79 | - if (isset($subids[0])) { |
|
80 | - list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | - array_splice($subids, 0, 1, array($x, $y)); |
|
82 | - } |
|
83 | - $offset = $idx; |
|
84 | - return new self(self::_implodeSubIDs(...$subids)); |
|
85 | - } |
|
66 | + /** |
|
67 | + * |
|
68 | + * {@inheritdoc} |
|
69 | + * @return self |
|
70 | + */ |
|
71 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
72 | + int &$offset): ElementBase |
|
73 | + { |
|
74 | + $idx = $offset; |
|
75 | + $len = Length::expectFromDER($data, $idx)->intLength(); |
|
76 | + $subids = self::_decodeSubIDs(substr($data, $idx, $len)); |
|
77 | + $idx += $len; |
|
78 | + // decode first subidentifier according to spec section 8.19.4 |
|
79 | + if (isset($subids[0])) { |
|
80 | + list($x, $y) = gmp_div_qr($subids[0], "40"); |
|
81 | + array_splice($subids, 0, 1, array($x, $y)); |
|
82 | + } |
|
83 | + $offset = $idx; |
|
84 | + return new self(self::_implodeSubIDs(...$subids)); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Explode dotted OID to an array of sub ID's. |
|
89 | - * |
|
90 | - * @param string $oid OID in dotted format |
|
91 | - * @return \GMP[] Array of GMP numbers |
|
92 | - */ |
|
93 | - protected static function _explodeDottedOID(string $oid): array |
|
94 | - { |
|
95 | - $subids = []; |
|
96 | - foreach (explode(".", $oid) as $subid) { |
|
97 | - $subids[] = gmp_init($subid, 10); |
|
98 | - } |
|
99 | - return $subids; |
|
100 | - } |
|
87 | + /** |
|
88 | + * Explode dotted OID to an array of sub ID's. |
|
89 | + * |
|
90 | + * @param string $oid OID in dotted format |
|
91 | + * @return \GMP[] Array of GMP numbers |
|
92 | + */ |
|
93 | + protected static function _explodeDottedOID(string $oid): array |
|
94 | + { |
|
95 | + $subids = []; |
|
96 | + foreach (explode(".", $oid) as $subid) { |
|
97 | + $subids[] = gmp_init($subid, 10); |
|
98 | + } |
|
99 | + return $subids; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Implode an array of sub IDs to dotted OID format. |
|
104 | - * |
|
105 | - * @param \GMP[] $subids |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | - { |
|
110 | - return implode(".", |
|
111 | - array_map( |
|
112 | - function ($num) { |
|
113 | - return gmp_strval($num, 10); |
|
114 | - }, $subids)); |
|
115 | - } |
|
102 | + /** |
|
103 | + * Implode an array of sub IDs to dotted OID format. |
|
104 | + * |
|
105 | + * @param \GMP[] $subids |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + protected static function _implodeSubIDs(\GMP ...$subids): string |
|
109 | + { |
|
110 | + return implode(".", |
|
111 | + array_map( |
|
112 | + function ($num) { |
|
113 | + return gmp_strval($num, 10); |
|
114 | + }, $subids)); |
|
115 | + } |
|
116 | 116 | |
117 | - /** |
|
118 | - * Encode sub ID's to DER. |
|
119 | - * |
|
120 | - * @param \GMP[] $subids |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | - { |
|
125 | - $data = ""; |
|
126 | - foreach ($subids as $subid) { |
|
127 | - // if number fits to one base 128 byte |
|
128 | - if ($subid < 128) { |
|
129 | - $data .= chr(intval($subid)); |
|
130 | - } else { // encode to multiple bytes |
|
131 | - $bytes = []; |
|
132 | - do { |
|
133 | - array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | - $subid >>= 7; |
|
135 | - } while ($subid > 0); |
|
136 | - // all bytes except last must have bit 8 set to one |
|
137 | - foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | - $data .= chr(0x80 | $byte); |
|
139 | - } |
|
140 | - $data .= chr(reset($bytes)); |
|
141 | - } |
|
142 | - } |
|
143 | - return $data; |
|
144 | - } |
|
117 | + /** |
|
118 | + * Encode sub ID's to DER. |
|
119 | + * |
|
120 | + * @param \GMP[] $subids |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + protected static function _encodeSubIDs(\GMP ...$subids): string |
|
124 | + { |
|
125 | + $data = ""; |
|
126 | + foreach ($subids as $subid) { |
|
127 | + // if number fits to one base 128 byte |
|
128 | + if ($subid < 128) { |
|
129 | + $data .= chr(intval($subid)); |
|
130 | + } else { // encode to multiple bytes |
|
131 | + $bytes = []; |
|
132 | + do { |
|
133 | + array_unshift($bytes, 0x7f & gmp_intval($subid)); |
|
134 | + $subid >>= 7; |
|
135 | + } while ($subid > 0); |
|
136 | + // all bytes except last must have bit 8 set to one |
|
137 | + foreach (array_splice($bytes, 0, -1) as $byte) { |
|
138 | + $data .= chr(0x80 | $byte); |
|
139 | + } |
|
140 | + $data .= chr(reset($bytes)); |
|
141 | + } |
|
142 | + } |
|
143 | + return $data; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Decode sub ID's from DER data. |
|
148 | - * |
|
149 | - * @param string $data |
|
150 | - * @throws DecodeException |
|
151 | - * @return \GMP[] Array of GMP numbers |
|
152 | - */ |
|
153 | - protected static function _decodeSubIDs(string $data): array |
|
154 | - { |
|
155 | - $subids = []; |
|
156 | - $idx = 0; |
|
157 | - $end = strlen($data); |
|
158 | - while ($idx < $end) { |
|
159 | - $num = gmp_init("0", 10); |
|
160 | - while (true) { |
|
161 | - if ($idx >= $end) { |
|
162 | - throw new DecodeException("Unexpected end of data."); |
|
163 | - } |
|
164 | - $byte = ord($data[$idx++]); |
|
165 | - $num |= $byte & 0x7f; |
|
166 | - // bit 8 of the last octet is zero |
|
167 | - if (!($byte & 0x80)) { |
|
168 | - break; |
|
169 | - } |
|
170 | - $num <<= 7; |
|
171 | - } |
|
172 | - $subids[] = $num; |
|
173 | - } |
|
174 | - return $subids; |
|
175 | - } |
|
146 | + /** |
|
147 | + * Decode sub ID's from DER data. |
|
148 | + * |
|
149 | + * @param string $data |
|
150 | + * @throws DecodeException |
|
151 | + * @return \GMP[] Array of GMP numbers |
|
152 | + */ |
|
153 | + protected static function _decodeSubIDs(string $data): array |
|
154 | + { |
|
155 | + $subids = []; |
|
156 | + $idx = 0; |
|
157 | + $end = strlen($data); |
|
158 | + while ($idx < $end) { |
|
159 | + $num = gmp_init("0", 10); |
|
160 | + while (true) { |
|
161 | + if ($idx >= $end) { |
|
162 | + throw new DecodeException("Unexpected end of data."); |
|
163 | + } |
|
164 | + $byte = ord($data[$idx++]); |
|
165 | + $num |= $byte & 0x7f; |
|
166 | + // bit 8 of the last octet is zero |
|
167 | + if (!($byte & 0x80)) { |
|
168 | + break; |
|
169 | + } |
|
170 | + $num <<= 7; |
|
171 | + } |
|
172 | + $subids[] = $num; |
|
173 | + } |
|
174 | + return $subids; |
|
175 | + } |
|
176 | 176 | } |
@@ -17,112 +17,112 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = /* @formatter:off */ '#^' . |
|
31 | - '(\d\d\d\d)' . /* YYYY */ |
|
32 | - '(\d\d)' . /* MM */ |
|
33 | - '(\d\d)' . /* DD */ |
|
34 | - '(\d\d)' . /* hh */ |
|
35 | - '(\d\d)' . /* mm */ |
|
36 | - '(\d\d)' . /* ss */ |
|
37 | - '(?:\.(\d+))?' . /* frac */ |
|
38 | - 'Z' . /* TZ */ |
|
39 | - '$#' /* @formatter:on */; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = /* @formatter:off */ '#^' . |
|
31 | + '(\d\d\d\d)' . /* YYYY */ |
|
32 | + '(\d\d)' . /* MM */ |
|
33 | + '(\d\d)' . /* DD */ |
|
34 | + '(\d\d)' . /* hh */ |
|
35 | + '(\d\d)' . /* mm */ |
|
36 | + '(\d\d)' . /* ss */ |
|
37 | + '(?:\.(\d+))?' . /* frac */ |
|
38 | + 'Z' . /* TZ */ |
|
39 | + '$#' /* @formatter:on */; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var string|null |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var string|null |
|
45 | + */ |
|
46 | + private $_formatted; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param \DateTimeImmutable $dt |
|
52 | - */ |
|
53 | - public function __construct(\DateTimeImmutable $dt) |
|
54 | - { |
|
55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | - parent::__construct($dt); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param \DateTimeImmutable $dt |
|
52 | + */ |
|
53 | + public function __construct(\DateTimeImmutable $dt) |
|
54 | + { |
|
55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | + parent::__construct($dt); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Clear cached variables on clone. |
|
61 | - */ |
|
62 | - public function __clone() |
|
63 | - { |
|
64 | - $this->_formatted = null; |
|
65 | - } |
|
59 | + /** |
|
60 | + * Clear cached variables on clone. |
|
61 | + */ |
|
62 | + public function __clone() |
|
63 | + { |
|
64 | + $this->_formatted = null; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * |
|
69 | - * {@inheritdoc} |
|
70 | - */ |
|
71 | - protected function _encodedContentDER(): string |
|
72 | - { |
|
73 | - if (!isset($this->_formatted)) { |
|
74 | - $dt = $this->_dateTime->setTimezone( |
|
75 | - self::_createTimeZone(self::TZ_UTC)); |
|
76 | - $this->_formatted = $dt->format("YmdHis"); |
|
77 | - // if fractions were used |
|
78 | - $frac = $dt->format("u"); |
|
79 | - if ($frac != 0) { |
|
80 | - $frac = rtrim($frac, "0"); |
|
81 | - $this->_formatted .= ".$frac"; |
|
82 | - } |
|
83 | - // timezone |
|
84 | - $this->_formatted .= "Z"; |
|
85 | - } |
|
86 | - return $this->_formatted; |
|
87 | - } |
|
67 | + /** |
|
68 | + * |
|
69 | + * {@inheritdoc} |
|
70 | + */ |
|
71 | + protected function _encodedContentDER(): string |
|
72 | + { |
|
73 | + if (!isset($this->_formatted)) { |
|
74 | + $dt = $this->_dateTime->setTimezone( |
|
75 | + self::_createTimeZone(self::TZ_UTC)); |
|
76 | + $this->_formatted = $dt->format("YmdHis"); |
|
77 | + // if fractions were used |
|
78 | + $frac = $dt->format("u"); |
|
79 | + if ($frac != 0) { |
|
80 | + $frac = rtrim($frac, "0"); |
|
81 | + $this->_formatted .= ".$frac"; |
|
82 | + } |
|
83 | + // timezone |
|
84 | + $this->_formatted .= "Z"; |
|
85 | + } |
|
86 | + return $this->_formatted; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * |
|
91 | - * {@inheritdoc} |
|
92 | - * @return self |
|
93 | - */ |
|
94 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | - int &$offset): ElementBase |
|
96 | - { |
|
97 | - $idx = $offset; |
|
98 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
99 | - $str = substr($data, $idx, $length); |
|
100 | - $idx += $length; |
|
101 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | - throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | - } |
|
104 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | - if (isset($match[7])) { |
|
106 | - $frac = $match[7]; |
|
107 | - // DER restricts trailing zeroes in fractional seconds component |
|
108 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | - throw new DecodeException( |
|
110 | - "Fractional seconds must omit trailing zeroes."); |
|
111 | - } |
|
112 | - $frac = (int) $frac; |
|
113 | - } else { |
|
114 | - $frac = 0; |
|
115 | - } |
|
116 | - $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | - self::TZ_UTC; |
|
118 | - $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | - self::_createTimeZone(self::TZ_UTC)); |
|
120 | - if (!$dt) { |
|
121 | - throw new DecodeException( |
|
122 | - "Failed to decode GeneralizedTime: " . |
|
123 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | - } |
|
125 | - $offset = $idx; |
|
126 | - return new self($dt); |
|
127 | - } |
|
89 | + /** |
|
90 | + * |
|
91 | + * {@inheritdoc} |
|
92 | + * @return self |
|
93 | + */ |
|
94 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
95 | + int &$offset): ElementBase |
|
96 | + { |
|
97 | + $idx = $offset; |
|
98 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
99 | + $str = substr($data, $idx, $length); |
|
100 | + $idx += $length; |
|
101 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
102 | + throw new DecodeException("Invalid GeneralizedTime format."); |
|
103 | + } |
|
104 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
105 | + if (isset($match[7])) { |
|
106 | + $frac = $match[7]; |
|
107 | + // DER restricts trailing zeroes in fractional seconds component |
|
108 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
109 | + throw new DecodeException( |
|
110 | + "Fractional seconds must omit trailing zeroes."); |
|
111 | + } |
|
112 | + $frac = (int) $frac; |
|
113 | + } else { |
|
114 | + $frac = 0; |
|
115 | + } |
|
116 | + $time = $year . $month . $day . $hour . $minute . $second . "." . $frac . |
|
117 | + self::TZ_UTC; |
|
118 | + $dt = \DateTimeImmutable::createFromFormat("!YmdHis.uT", $time, |
|
119 | + self::_createTimeZone(self::TZ_UTC)); |
|
120 | + if (!$dt) { |
|
121 | + throw new DecodeException( |
|
122 | + "Failed to decode GeneralizedTime: " . |
|
123 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
124 | + } |
|
125 | + $offset = $idx; |
|
126 | + return new self($dt); |
|
127 | + } |
|
128 | 128 | } |
@@ -92,7 +92,7 @@ |
||
92 | 92 | * @return self |
93 | 93 | */ |
94 | 94 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
95 | - int &$offset): ElementBase |
|
95 | + int & $offset): ElementBase |
|
96 | 96 | { |
97 | 97 | $idx = $offset; |
98 | 98 | $length = Length::expectFromDER($data, $idx)->intLength(); |
@@ -17,72 +17,72 @@ |
||
17 | 17 | */ |
18 | 18 | class UTCTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = /* @formatter:off */ '#^' . |
|
31 | - '(\d\d)' . /* YY */ |
|
32 | - '(\d\d)' . /* MM */ |
|
33 | - '(\d\d)' . /* DD */ |
|
34 | - '(\d\d)' . /* hh */ |
|
35 | - '(\d\d)' . /* mm */ |
|
36 | - '(\d\d)' . /* ss */ |
|
37 | - 'Z' . /* TZ */ |
|
38 | - '$#' /* @formatter:on */; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = /* @formatter:off */ '#^' . |
|
31 | + '(\d\d)' . /* YY */ |
|
32 | + '(\d\d)' . /* MM */ |
|
33 | + '(\d\d)' . /* DD */ |
|
34 | + '(\d\d)' . /* hh */ |
|
35 | + '(\d\d)' . /* mm */ |
|
36 | + '(\d\d)' . /* ss */ |
|
37 | + 'Z' . /* TZ */ |
|
38 | + '$#' /* @formatter:on */; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Constructor. |
|
42 | - * |
|
43 | - * @param \DateTimeImmutable $dt |
|
44 | - */ |
|
45 | - public function __construct(\DateTimeImmutable $dt) |
|
46 | - { |
|
47 | - $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | - parent::__construct($dt); |
|
49 | - } |
|
40 | + /** |
|
41 | + * Constructor. |
|
42 | + * |
|
43 | + * @param \DateTimeImmutable $dt |
|
44 | + */ |
|
45 | + public function __construct(\DateTimeImmutable $dt) |
|
46 | + { |
|
47 | + $this->_typeTag = self::TYPE_UTC_TIME; |
|
48 | + parent::__construct($dt); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * |
|
53 | - * {@inheritdoc} |
|
54 | - */ |
|
55 | - protected function _encodedContentDER(): string |
|
56 | - { |
|
57 | - $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | - return $dt->format("ymdHis\Z"); |
|
59 | - } |
|
51 | + /** |
|
52 | + * |
|
53 | + * {@inheritdoc} |
|
54 | + */ |
|
55 | + protected function _encodedContentDER(): string |
|
56 | + { |
|
57 | + $dt = $this->_dateTime->setTimezone(self::_createTimeZone(self::TZ_UTC)); |
|
58 | + return $dt->format("ymdHis\Z"); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * |
|
63 | - * {@inheritdoc} |
|
64 | - * @return self |
|
65 | - */ |
|
66 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | - int &$offset): ElementBase |
|
68 | - { |
|
69 | - $idx = $offset; |
|
70 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
71 | - $str = substr($data, $idx, $length); |
|
72 | - $idx += $length; |
|
73 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | - throw new DecodeException("Invalid UTCTime format."); |
|
75 | - } |
|
76 | - list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | - $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | - $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | - self::_createTimeZone(self::TZ_UTC)); |
|
80 | - if (!$dt) { |
|
81 | - throw new DecodeException( |
|
82 | - "Failed to decode UTCTime: " . |
|
83 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | - } |
|
85 | - $offset = $idx; |
|
86 | - return new self($dt); |
|
87 | - } |
|
61 | + /** |
|
62 | + * |
|
63 | + * {@inheritdoc} |
|
64 | + * @return self |
|
65 | + */ |
|
66 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
67 | + int &$offset): ElementBase |
|
68 | + { |
|
69 | + $idx = $offset; |
|
70 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
71 | + $str = substr($data, $idx, $length); |
|
72 | + $idx += $length; |
|
73 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
74 | + throw new DecodeException("Invalid UTCTime format."); |
|
75 | + } |
|
76 | + list(, $year, $month, $day, $hour, $minute, $second) = $match; |
|
77 | + $time = $year . $month . $day . $hour . $minute . $second . self::TZ_UTC; |
|
78 | + $dt = \DateTimeImmutable::createFromFormat("!ymdHisT", $time, |
|
79 | + self::_createTimeZone(self::TZ_UTC)); |
|
80 | + if (!$dt) { |
|
81 | + throw new DecodeException( |
|
82 | + "Failed to decode UTCTime: " . |
|
83 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
84 | + } |
|
85 | + $offset = $idx; |
|
86 | + return new self($dt); |
|
87 | + } |
|
88 | 88 | } |
@@ -92,7 +92,7 @@ |
||
92 | 92 | * @return self |
93 | 93 | */ |
94 | 94 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
95 | - int &$offset): ElementBase |
|
95 | + int & $offset): ElementBase |
|
96 | 96 | { |
97 | 97 | $idx = $offset; |
98 | 98 | $length = Length::expectFromDER($data, $idx)->intLength(); |
@@ -17,161 +17,161 @@ |
||
17 | 17 | */ |
18 | 18 | class Integer extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * The number. |
|
25 | - * |
|
26 | - * @var BigInt |
|
27 | - */ |
|
28 | - private $_number; |
|
23 | + /** |
|
24 | + * The number. |
|
25 | + * |
|
26 | + * @var BigInt |
|
27 | + */ |
|
28 | + private $_number; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param int|string $number Base 10 integer |
|
34 | - */ |
|
35 | - public function __construct($number) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_INTEGER; |
|
38 | - if (!self::_validateNumber($number)) { |
|
39 | - $var = is_scalar($number) ? strval($number) : gettype($number); |
|
40 | - throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
41 | - } |
|
42 | - $this->_number = new BigInt($number); |
|
43 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param int|string $number Base 10 integer |
|
34 | + */ |
|
35 | + public function __construct($number) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_INTEGER; |
|
38 | + if (!self::_validateNumber($number)) { |
|
39 | + $var = is_scalar($number) ? strval($number) : gettype($number); |
|
40 | + throw new \InvalidArgumentException("'$var' is not a valid number."); |
|
41 | + } |
|
42 | + $this->_number = new BigInt($number); |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Get the number as a base 10. |
|
47 | - * |
|
48 | - * @return string Integer as a string |
|
49 | - */ |
|
50 | - public function number(): string |
|
51 | - { |
|
52 | - return $this->_number->base10(); |
|
53 | - } |
|
45 | + /** |
|
46 | + * Get the number as a base 10. |
|
47 | + * |
|
48 | + * @return string Integer as a string |
|
49 | + */ |
|
50 | + public function number(): string |
|
51 | + { |
|
52 | + return $this->_number->base10(); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Get the number as an integer type. |
|
57 | - * |
|
58 | - * @return int |
|
59 | - */ |
|
60 | - public function intNumber(): int |
|
61 | - { |
|
62 | - return $this->_number->intVal(); |
|
63 | - } |
|
55 | + /** |
|
56 | + * Get the number as an integer type. |
|
57 | + * |
|
58 | + * @return int |
|
59 | + */ |
|
60 | + public function intNumber(): int |
|
61 | + { |
|
62 | + return $this->_number->intVal(); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * |
|
67 | - * {@inheritdoc} |
|
68 | - */ |
|
69 | - protected function _encodedContentDER(): string |
|
70 | - { |
|
71 | - $num = $this->_number->gmpObj(); |
|
72 | - switch (gmp_sign($num)) { |
|
73 | - // positive |
|
74 | - case 1: |
|
75 | - return self::_encodePositiveInteger($num); |
|
76 | - // negative |
|
77 | - case -1: |
|
78 | - return self::_encodeNegativeInteger($num); |
|
79 | - } |
|
80 | - // zero |
|
81 | - return "\0"; |
|
82 | - } |
|
65 | + /** |
|
66 | + * |
|
67 | + * {@inheritdoc} |
|
68 | + */ |
|
69 | + protected function _encodedContentDER(): string |
|
70 | + { |
|
71 | + $num = $this->_number->gmpObj(); |
|
72 | + switch (gmp_sign($num)) { |
|
73 | + // positive |
|
74 | + case 1: |
|
75 | + return self::_encodePositiveInteger($num); |
|
76 | + // negative |
|
77 | + case -1: |
|
78 | + return self::_encodeNegativeInteger($num); |
|
79 | + } |
|
80 | + // zero |
|
81 | + return "\0"; |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Encode positive integer to DER content. |
|
86 | - * |
|
87 | - * @param \GMP|resource $num |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - private static function _encodePositiveInteger(\GMP $num): string |
|
91 | - { |
|
92 | - $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 | - if (ord($bin[0]) & 0x80) { |
|
96 | - $bin = chr(0x00) . $bin; |
|
97 | - } |
|
98 | - return $bin; |
|
99 | - } |
|
84 | + /** |
|
85 | + * Encode positive integer to DER content. |
|
86 | + * |
|
87 | + * @param \GMP|resource $num |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + private static function _encodePositiveInteger(\GMP $num): string |
|
91 | + { |
|
92 | + $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 | + if (ord($bin[0]) & 0x80) { |
|
96 | + $bin = chr(0x00) . $bin; |
|
97 | + } |
|
98 | + return $bin; |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Encode negative integer to DER content. |
|
103 | - * |
|
104 | - * @param \GMP|resource $num |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - private static function _encodeNegativeInteger(\GMP $num): string |
|
108 | - { |
|
109 | - $num = gmp_abs($num); |
|
110 | - // compute number of bytes required |
|
111 | - $width = 1; |
|
112 | - if ($num > 128) { |
|
113 | - $tmp = $num; |
|
114 | - do { |
|
115 | - $width++; |
|
116 | - $tmp >>= 8; |
|
117 | - } while ($tmp > 128); |
|
118 | - } |
|
119 | - // compute two's complement 2^n - x |
|
120 | - $num = gmp_pow("2", 8 * $width) - $num; |
|
121 | - $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 | - if (!(ord($bin[0]) & 0x80)) { |
|
125 | - $bin = chr(0xff) . $bin; |
|
126 | - } |
|
127 | - return $bin; |
|
128 | - } |
|
101 | + /** |
|
102 | + * Encode negative integer to DER content. |
|
103 | + * |
|
104 | + * @param \GMP|resource $num |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + private static function _encodeNegativeInteger(\GMP $num): string |
|
108 | + { |
|
109 | + $num = gmp_abs($num); |
|
110 | + // compute number of bytes required |
|
111 | + $width = 1; |
|
112 | + if ($num > 128) { |
|
113 | + $tmp = $num; |
|
114 | + do { |
|
115 | + $width++; |
|
116 | + $tmp >>= 8; |
|
117 | + } while ($tmp > 128); |
|
118 | + } |
|
119 | + // compute two's complement 2^n - x |
|
120 | + $num = gmp_pow("2", 8 * $width) - $num; |
|
121 | + $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 | + if (!(ord($bin[0]) & 0x80)) { |
|
125 | + $bin = chr(0xff) . $bin; |
|
126 | + } |
|
127 | + return $bin; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * |
|
132 | - * {@inheritdoc} |
|
133 | - * @return self |
|
134 | - */ |
|
135 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
136 | - int &$offset): ElementBase |
|
137 | - { |
|
138 | - $idx = $offset; |
|
139 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
140 | - $bytes = substr($data, $idx, $length); |
|
141 | - $idx += $length; |
|
142 | - $neg = ord($bytes[0]) & 0x80; |
|
143 | - // negative, apply inversion of two's complement |
|
144 | - if ($neg) { |
|
145 | - $len = strlen($bytes); |
|
146 | - for ($i = 0; $i < $len; $i++) { |
|
147 | - $bytes[$i] = ~$bytes[$i]; |
|
148 | - } |
|
149 | - } |
|
150 | - $num = gmp_init(bin2hex($bytes), 16); |
|
151 | - // negative, apply addition of two's complement |
|
152 | - // and produce negative result |
|
153 | - if ($neg) { |
|
154 | - $num = gmp_neg($num + 1); |
|
155 | - } |
|
156 | - $offset = $idx; |
|
157 | - // late static binding since enumerated extends integer type |
|
158 | - return new static(gmp_strval($num, 10)); |
|
159 | - } |
|
130 | + /** |
|
131 | + * |
|
132 | + * {@inheritdoc} |
|
133 | + * @return self |
|
134 | + */ |
|
135 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
136 | + int &$offset): ElementBase |
|
137 | + { |
|
138 | + $idx = $offset; |
|
139 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
140 | + $bytes = substr($data, $idx, $length); |
|
141 | + $idx += $length; |
|
142 | + $neg = ord($bytes[0]) & 0x80; |
|
143 | + // negative, apply inversion of two's complement |
|
144 | + if ($neg) { |
|
145 | + $len = strlen($bytes); |
|
146 | + for ($i = 0; $i < $len; $i++) { |
|
147 | + $bytes[$i] = ~$bytes[$i]; |
|
148 | + } |
|
149 | + } |
|
150 | + $num = gmp_init(bin2hex($bytes), 16); |
|
151 | + // negative, apply addition of two's complement |
|
152 | + // and produce negative result |
|
153 | + if ($neg) { |
|
154 | + $num = gmp_neg($num + 1); |
|
155 | + } |
|
156 | + $offset = $idx; |
|
157 | + // late static binding since enumerated extends integer type |
|
158 | + return new static(gmp_strval($num, 10)); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Test that number is valid for this context. |
|
163 | - * |
|
164 | - * @param mixed $num |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - private static function _validateNumber($num): bool |
|
168 | - { |
|
169 | - if (is_int($num)) { |
|
170 | - return true; |
|
171 | - } |
|
172 | - if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
173 | - return true; |
|
174 | - } |
|
175 | - return false; |
|
176 | - } |
|
161 | + /** |
|
162 | + * Test that number is valid for this context. |
|
163 | + * |
|
164 | + * @param mixed $num |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + private static function _validateNumber($num): bool |
|
168 | + { |
|
169 | + if (is_int($num)) { |
|
170 | + return true; |
|
171 | + } |
|
172 | + if (is_string($num) && preg_match('/-?\d+/', $num)) { |
|
173 | + return true; |
|
174 | + } |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | } |
@@ -71,11 +71,11 @@ |
||
71 | 71 | $num = $this->_number->gmpObj(); |
72 | 72 | switch (gmp_sign($num)) { |
73 | 73 | // positive |
74 | - case 1: |
|
75 | - return self::_encodePositiveInteger($num); |
|
76 | - // negative |
|
77 | - case -1: |
|
78 | - return self::_encodeNegativeInteger($num); |
|
74 | + case 1: |
|
75 | + return self::_encodePositiveInteger($num); |
|
76 | + // negative |
|
77 | + case -1: |
|
78 | + return self::_encodeNegativeInteger($num); |
|
79 | 79 | } |
80 | 80 | // zero |
81 | 81 | return "\0"; |
@@ -92,7 +92,7 @@ |
||
92 | 92 | * @return self |
93 | 93 | */ |
94 | 94 | protected static function _decodeFromDER(Identifier $identifier, string $data, |
95 | - int &$offset): ElementBase |
|
95 | + int & $offset): ElementBase |
|
96 | 96 | { |
97 | 97 | $idx = $offset; |
98 | 98 | $length = Length::expectFromDER($data, $idx)->intLength(); |
@@ -6,106 +6,106 @@ |
||
6 | 6 | |
7 | 7 | class BigInt |
8 | 8 | { |
9 | - /** |
|
10 | - * Number as a base10 integer string. |
|
11 | - * |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - private $_num; |
|
9 | + /** |
|
10 | + * Number as a base10 integer string. |
|
11 | + * |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + private $_num; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Number as an integer type. |
|
18 | - * |
|
19 | - * @internal Lazily initialized |
|
20 | - * @var int|null |
|
21 | - */ |
|
22 | - private $_intNum; |
|
16 | + /** |
|
17 | + * Number as an integer type. |
|
18 | + * |
|
19 | + * @internal Lazily initialized |
|
20 | + * @var int|null |
|
21 | + */ |
|
22 | + private $_intNum; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Constructor. |
|
26 | - * |
|
27 | - * @param string|int $num |
|
28 | - */ |
|
29 | - public function __construct($num) |
|
30 | - { |
|
31 | - $this->_num = strval($num); |
|
32 | - } |
|
24 | + /** |
|
25 | + * Constructor. |
|
26 | + * |
|
27 | + * @param string|int $num |
|
28 | + */ |
|
29 | + public function __construct($num) |
|
30 | + { |
|
31 | + $this->_num = strval($num); |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * Get the number as a base10 integer string. |
|
36 | - * |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public function base10(): string |
|
40 | - { |
|
41 | - return $this->_num; |
|
42 | - } |
|
34 | + /** |
|
35 | + * Get the number as a base10 integer string. |
|
36 | + * |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public function base10(): string |
|
40 | + { |
|
41 | + return $this->_num; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Get the number as an integer. |
|
46 | - * |
|
47 | - * @throws \RuntimeException If number overflows integer size |
|
48 | - * @return int |
|
49 | - */ |
|
50 | - public function intVal(): int |
|
51 | - { |
|
52 | - if (!isset($this->_intNum)) { |
|
53 | - $num = gmp_init($this->_num, 10); |
|
54 | - if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
55 | - throw new \RuntimeException("Integer overflow."); |
|
56 | - } |
|
57 | - if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
58 | - throw new \RuntimeException("Integer underflow."); |
|
59 | - } |
|
60 | - $this->_intNum = gmp_intval($num); |
|
61 | - } |
|
62 | - return $this->_intNum; |
|
63 | - } |
|
44 | + /** |
|
45 | + * Get the number as an integer. |
|
46 | + * |
|
47 | + * @throws \RuntimeException If number overflows integer size |
|
48 | + * @return int |
|
49 | + */ |
|
50 | + public function intVal(): int |
|
51 | + { |
|
52 | + if (!isset($this->_intNum)) { |
|
53 | + $num = gmp_init($this->_num, 10); |
|
54 | + if (gmp_cmp($num, $this->_intMaxGmp()) > 0) { |
|
55 | + throw new \RuntimeException("Integer overflow."); |
|
56 | + } |
|
57 | + if (gmp_cmp($num, $this->_intMinGmp()) < 0) { |
|
58 | + throw new \RuntimeException("Integer underflow."); |
|
59 | + } |
|
60 | + $this->_intNum = gmp_intval($num); |
|
61 | + } |
|
62 | + return $this->_intNum; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the maximum integer value. |
|
67 | - * |
|
68 | - * @return \GMP |
|
69 | - */ |
|
70 | - private function _intMaxGmp(): \GMP |
|
71 | - { |
|
72 | - static $gmp; |
|
73 | - if (!isset($gmp)) { |
|
74 | - $gmp = gmp_init(PHP_INT_MAX, 10); |
|
75 | - } |
|
76 | - return $gmp; |
|
77 | - } |
|
65 | + /** |
|
66 | + * Get the maximum integer value. |
|
67 | + * |
|
68 | + * @return \GMP |
|
69 | + */ |
|
70 | + private function _intMaxGmp(): \GMP |
|
71 | + { |
|
72 | + static $gmp; |
|
73 | + if (!isset($gmp)) { |
|
74 | + $gmp = gmp_init(PHP_INT_MAX, 10); |
|
75 | + } |
|
76 | + return $gmp; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Get the minimum integer value. |
|
81 | - * |
|
82 | - * @return \GMP |
|
83 | - */ |
|
84 | - private function _intMinGmp(): \GMP |
|
85 | - { |
|
86 | - static $gmp; |
|
87 | - if (!isset($gmp)) { |
|
88 | - $gmp = gmp_init(PHP_INT_MIN, 10); |
|
89 | - } |
|
90 | - return $gmp; |
|
91 | - } |
|
79 | + /** |
|
80 | + * Get the minimum integer value. |
|
81 | + * |
|
82 | + * @return \GMP |
|
83 | + */ |
|
84 | + private function _intMinGmp(): \GMP |
|
85 | + { |
|
86 | + static $gmp; |
|
87 | + if (!isset($gmp)) { |
|
88 | + $gmp = gmp_init(PHP_INT_MIN, 10); |
|
89 | + } |
|
90 | + return $gmp; |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get the number as a GMP object. |
|
95 | - * |
|
96 | - * @return \GMP |
|
97 | - */ |
|
98 | - public function gmpObj(): \GMP |
|
99 | - { |
|
100 | - return gmp_init($this->_num, 10); |
|
101 | - } |
|
93 | + /** |
|
94 | + * Get the number as a GMP object. |
|
95 | + * |
|
96 | + * @return \GMP |
|
97 | + */ |
|
98 | + public function gmpObj(): \GMP |
|
99 | + { |
|
100 | + return gmp_init($this->_num, 10); |
|
101 | + } |
|
102 | 102 | |
103 | - /** |
|
104 | - * |
|
105 | - * @return string |
|
106 | - */ |
|
107 | - public function __toString() |
|
108 | - { |
|
109 | - return $this->base10(); |
|
110 | - } |
|
103 | + /** |
|
104 | + * |
|
105 | + * @return string |
|
106 | + */ |
|
107 | + public function __toString() |
|
108 | + { |
|
109 | + return $this->base10(); |
|
110 | + } |
|
111 | 111 | } |
@@ -11,141 +11,141 @@ |
||
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 int|string $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 int|string $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 an integer. |
|
130 | - * |
|
131 | - * @return int |
|
132 | - */ |
|
133 | - public function intNumber(): int |
|
134 | - { |
|
135 | - $num = new BigInt($this->number()); |
|
136 | - return $num->intVal(); |
|
137 | - } |
|
128 | + /** |
|
129 | + * Get flags as an integer. |
|
130 | + * |
|
131 | + * @return int |
|
132 | + */ |
|
133 | + public function intNumber(): int |
|
134 | + { |
|
135 | + $num = new BigInt($this->number()); |
|
136 | + return $num->intVal(); |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Get flags as a BitString. |
|
141 | - * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
142 | - * |
|
143 | - * @return BitString |
|
144 | - */ |
|
145 | - public function bitString(): BitString |
|
146 | - { |
|
147 | - $last_octet_bits = $this->_width % 8; |
|
148 | - $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
149 | - return new BitString($this->_flags, $unused_bits); |
|
150 | - } |
|
139 | + /** |
|
140 | + * Get flags as a BitString. |
|
141 | + * Unused bits are set accordingly. Trailing zeroes are not stripped. |
|
142 | + * |
|
143 | + * @return BitString |
|
144 | + */ |
|
145 | + public function bitString(): BitString |
|
146 | + { |
|
147 | + $last_octet_bits = $this->_width % 8; |
|
148 | + $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; |
|
149 | + return new BitString($this->_flags, $unused_bits); |
|
150 | + } |
|
151 | 151 | } |